lightmediascannerctl: start daemon if not running.
[platform/upstream/lightmediascanner.git] / src / bin / lightmediascannerctl.c
index b2e002d..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;
@@ -275,8 +316,9 @@ on_signal(GDBusProxy *proxy, gchar *sender, gchar *signal, GVariant *params, gpo
                       &skipped,
                       &errors);
 
-        printf("Scan Progress %s:%s uptodate=%llu, processed=%llu, "
-               "deleted=%llu, skipped=%llu, errors=%llu\n",
+        printf("Scan Progress %s:%s uptodate=%"G_GUINT64_FORMAT", "
+               "processed=%"G_GUINT64_FORMAT", deleted=%"G_GUINT64_FORMAT", "
+               "skipped=%"G_GUINT64_FORMAT", errors=%"G_GUINT64_FORMAT"\n",
                category, path, uptodate, processed, deleted, skipped, errors);
     }
 }
@@ -479,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,