[podsleuth] Start the HAL service if needed



commit 2ea7775ae836b42667f12d7afadb90ae954a52c8
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Mon May 10 12:05:32 2010 -0700

    Start the HAL service if needed

 src/PodSleuth.Hal/Hal/Device.cs  |    4 ++--
 src/PodSleuth.Hal/Hal/Manager.cs |   21 ++++++++++++++++++++-
 2 files changed, 22 insertions(+), 3 deletions(-)
---
diff --git a/src/PodSleuth.Hal/Hal/Device.cs b/src/PodSleuth.Hal/Hal/Device.cs
index 0f92ceb..3ca5bf0 100644
--- a/src/PodSleuth.Hal/Hal/Device.cs
+++ b/src/PodSleuth.Hal/Hal/Device.cs
@@ -280,11 +280,11 @@ namespace Hal
             // Fixes a bug where the HAL callout would call into this code on bootup with
             // a device plugged in, but the DBus service isn't yet available.
             int tries = 0;
-            while(!Bus.System.NameHasOwner("org.freedesktop.Hal") && tries++ < 10) {
+            while(!Manager.EnsureHalIsActive () && tries++ < 10) {
                 System.Threading.Thread.Sleep (1000);
             }
 
-            if(!Bus.System.NameHasOwner("org.freedesktop.Hal")) {
+            if(!Manager.EnsureHalIsActive ()) {
                 throw new ApplicationException("Could not find org.freedesktop.Hal");
             }
             
diff --git a/src/PodSleuth.Hal/Hal/Manager.cs b/src/PodSleuth.Hal/Hal/Manager.cs
index 76f899c..21a73f9 100644
--- a/src/PodSleuth.Hal/Hal/Manager.cs
+++ b/src/PodSleuth.Hal/Hal/Manager.cs
@@ -3,6 +3,7 @@ using System.Collections;
 using System.Collections.Generic;
 
 using NDesk.DBus;
+using org.freedesktop.DBus;
 
 namespace Hal
 {
@@ -88,10 +89,28 @@ namespace Hal
         public event DeviceAddedHandler DeviceAdded;
         public event DeviceRemovedHandler DeviceRemoved;
         public event NewCapabilityHandler NewCapability;
+
+        internal static bool EnsureHalIsActive ()
+        {
+            if (!Bus.System.NameHasOwner("org.freedesktop.Hal")) {
+                // try to start it
+                var reply = Bus.System.StartServiceByName ("org.freedesktop.Hal");
+                if (reply != StartReply.Success && reply != StartReply.AlreadyRunning) {
+                    throw new ApplicationException("Could not start org.freedesktop.Hal");
+                }
+
+                // If still not started, we're done
+                if (!Bus.System.NameHasOwner("org.freedesktop.Hal")) {
+                    return false;
+                }
+            }
+
+            return true;
+        }
         
         public Manager()
         {
-            if(!Bus.System.NameHasOwner("org.freedesktop.Hal")) {
+            if(!EnsureHalIsActive ()) {
                 throw new ApplicationException("Could not find org.freedesktop.Hal");
             }
             



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]