lightmediascannerctl: start daemon if not running.
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Fri, 6 Dec 2013 21:03:17 +0000 (19:03 -0200)
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Fri, 6 Dec 2013 21:03:17 +0000 (19:03 -0200)
While the service would be activated if we did call some method we are
checking for the current nameowner to do some operations (status, etc)
and it would exit if there was no server at that time.

Now we launch the service and make sure it's there.

src/bin/lightmediascannerctl.c

index 8931f33..5764bc0 100644 (file)
@@ -4,6 +4,47 @@
 #include <string.h>
 #include <time.h>
 
+static gboolean
+start_service_by_name(void)
+{
+    GError *error = NULL;
+    GVariant *var;
+    GDBusConnection *conn;
+
+    conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
+    if (error) {
+        fprintf(stderr, "Could not get session bus connection: %s\n",
+                error->message);
+        g_error_free(error);
+        return FALSE;
+    }
+
+    var = g_dbus_connection_call_sync(conn,
+                                      "org.freedesktop.DBus",
+                                      "/org/freedesktop/DBus",
+                                      "org.freedesktop.DBus",
+                                      "StartServiceByName",
+                                      g_variant_new("(su)",
+                                                    "org.lightmediascanner", 0),
+                                      G_VARIANT_TYPE("(u)"),
+                                      G_DBUS_CALL_FLAGS_NONE,
+                                      10000,
+                                      NULL,
+                                      &error);
+    g_object_unref(conn);
+    if (var)
+        g_variant_unref(var);
+
+    if (error) {
+        fprintf(stderr, "Could not start org.lightmediascanner: %s\n",
+                error->message);
+        g_error_free(error);
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
 struct app {
     int ret;
     int argc;
@@ -480,6 +521,9 @@ main(int argc, char *argv[])
         return EXIT_FAILURE;
     }
 
+    if (!start_service_by_name())
+        return EXIT_FAILURE;
+
     app.timer = NULL;
     app.loop = g_main_loop_new(NULL, FALSE);
     app.proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,