[kdbus] Use new API insted of direct call to org.freedesktop.DBus
[platform/upstream/glib.git] / gio / gdbus-tool.c
index f6b709b..09ee399 100644 (file)
@@ -265,10 +265,9 @@ static void
 print_names (GDBusConnection *c,
              gboolean         include_unique_names)
 {
-  GVariant *result;
+  gchar **list_names;
   GError *error;
-  GVariantIter *iter;
-  gchar *str;
+  guint cnt;
   GHashTable *name_set;
   GList *keys;
   GList *l;
@@ -276,52 +275,32 @@ print_names (GDBusConnection *c,
   name_set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
 
   error = NULL;
-  result = g_dbus_connection_call_sync (c,
-                                        "org.freedesktop.DBus",
-                                        "/org/freedesktop/DBus",
-                                        "org.freedesktop.DBus",
-                                        "ListNames",
-                                        NULL,
-                                        G_VARIANT_TYPE ("(as)"),
-                                        G_DBUS_CALL_FLAGS_NONE,
-                                        3000, /* 3 secs */
-                                        NULL,
-                                        &error);
-  if (result == NULL)
+  cnt = 0;
+  list_names = g_dbus_get_list_names (c, &error);
+  if (list_names == NULL)
     {
       g_printerr (_("Error: %s\n"), error->message);
       g_error_free (error);
       goto out;
     }
-  g_variant_get (result, "(as)", &iter);
-  while (g_variant_iter_loop (iter, "s", &str))
-    g_hash_table_insert (name_set, g_strdup (str), NULL);
-  g_variant_iter_free (iter);
-  g_variant_unref (result);
+
+  while (list_names[cnt])
+    g_hash_table_insert (name_set, g_strdup (list_names[cnt++]), NULL);
+  g_strfreev(list_names);
 
   error = NULL;
-  result = g_dbus_connection_call_sync (c,
-                                        "org.freedesktop.DBus",
-                                        "/org/freedesktop/DBus",
-                                        "org.freedesktop.DBus",
-                                        "ListActivatableNames",
-                                        NULL,
-                                        G_VARIANT_TYPE ("(as)"),
-                                        G_DBUS_CALL_FLAGS_NONE,
-                                        3000, /* 3 secs */
-                                        NULL,
-                                        &error);
-  if (result == NULL)
+  cnt = 0;
+  list_names = g_dbus_get_list_activatable_names (c, &error);
+  if (list_names == NULL)
     {
       g_printerr (_("Error: %s\n"), error->message);
       g_error_free (error);
       goto out;
     }
-  g_variant_get (result, "(as)", &iter);
-  while (g_variant_iter_loop (iter, "s", &str))
-    g_hash_table_insert (name_set, g_strdup (str), NULL);
-  g_variant_iter_free (iter);
-  g_variant_unref (result);
+
+  while (list_names[cnt])
+    g_hash_table_insert (name_set, g_strdup (list_names[cnt++]), NULL);
+  g_strfreev(list_names);
 
   keys = g_hash_table_get_keys (name_set);
   keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
@@ -343,12 +322,16 @@ print_names (GDBusConnection *c,
 
 static gboolean  opt_connection_system  = FALSE;
 static gboolean  opt_connection_session = FALSE;
+static gboolean  opt_connection_user    = FALSE;
+static gboolean  opt_connection_machine = FALSE;
 static gchar    *opt_connection_address = NULL;
 
 static const GOptionEntry connection_entries[] =
 {
   { "system", 'y', 0, G_OPTION_ARG_NONE, &opt_connection_system, N_("Connect to the system bus"), NULL},
   { "session", 'e', 0, G_OPTION_ARG_NONE, &opt_connection_session, N_("Connect to the session bus"), NULL},
+  { "machine", 'm', 0, G_OPTION_ARG_NONE, &opt_connection_machine, N_("Connect to the machine bus"), NULL},
+  { "user", 'u', 0, G_OPTION_ARG_NONE, &opt_connection_user, N_("Connect to the user bus"), NULL},
   { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_connection_address, N_("Connect to given D-Bus address"), NULL},
   { NULL }
 };
@@ -373,11 +356,18 @@ static GDBusConnection *
 connection_get_dbus_connection (GError **error)
 {
   GDBusConnection *c;
+  guint count;
 
   c = NULL;
 
+  count = !!opt_connection_system +
+          !!opt_connection_session +
+          !!opt_connection_machine +
+          !!opt_connection_user +
+          !!opt_connection_address;
+
   /* First, ensure we have exactly one connect */
-  if (!opt_connection_system && !opt_connection_session && opt_connection_address == NULL)
+  if (count == 0)
     {
       g_set_error (error,
                    G_IO_ERROR,
@@ -385,9 +375,7 @@ connection_get_dbus_connection (GError **error)
                    _("No connection endpoint specified"));
       goto out;
     }
-  else if ((opt_connection_system && (opt_connection_session || opt_connection_address != NULL)) ||
-           (opt_connection_session && (opt_connection_system || opt_connection_address != NULL)) ||
-           (opt_connection_address != NULL && (opt_connection_system || opt_connection_session)))
+  else if (count > 1)
     {
       g_set_error (error,
                    G_IO_ERROR,
@@ -404,6 +392,14 @@ connection_get_dbus_connection (GError **error)
     {
       c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
     }
+  else if (opt_connection_machine)
+    {
+      c = g_bus_get_sync (G_BUS_TYPE_MACHINE, NULL, error);
+    }
+  else if (opt_connection_user)
+    {
+      c = g_bus_get_sync (G_BUS_TYPE_USER, NULL, error);
+    }
   else if (opt_connection_address != NULL)
     {
       c = g_dbus_connection_new_for_address_sync (opt_connection_address,
@@ -596,7 +592,7 @@ handle_emit (gint        *argc,
             }
           else
             {
-              g_print ("--system \n--session \n--address \n");
+              g_print ("--system \n--session \n--machine \n--user \n--address \n");
             }
         }
       else
@@ -828,7 +824,7 @@ handle_call (gint        *argc,
             }
           else
             {
-              g_print ("--system \n--session \n--address \n");
+              g_print ("--system \n--session \n--machine \n--user \n--address \n");
             }
         }
       else
@@ -1563,7 +1559,7 @@ handle_introspect (gint        *argc,
             }
           else
             {
-              g_print ("--system \n--session \n--address \n");
+              g_print ("--system \n--session \n--machine \n--user \n--address \n");
             }
         }
       else
@@ -1735,16 +1731,12 @@ handle_monitor (gint        *argc,
   gchar *s;
   GError *error;
   GDBusConnection *c;
-  GVariant *result;
-  GDBusNodeInfo *node;
   gboolean complete_names;
   gboolean complete_paths;
   GMainLoop *loop;
 
   ret = FALSE;
   c = NULL;
-  node = NULL;
-  result = NULL;
 
   modify_argv0_for_command (argc, argv, "monitor");
 
@@ -1795,7 +1787,7 @@ handle_monitor (gint        *argc,
             }
           else
             {
-              g_print ("--system \n--session \n--address \n");
+              g_print ("--system \n--session \n--machine \n--user \n--address \n");
             }
         }
       else
@@ -1887,10 +1879,6 @@ handle_monitor (gint        *argc,
   ret = TRUE;
 
  out:
-  if (node != NULL)
-    g_dbus_node_info_unref (node);
-  if (result != NULL)
-    g_variant_unref (result);
   if (c != NULL)
     g_object_unref (c);
   g_option_context_free (o);