[gconf/gdbus: 2/3] Port gconfd-2 to GDBus



commit 12c8f44184c6c635fd93f900aba29378352249cc
Author: Christian Persch <chpe gnome org>
Date:   Sat May 8 02:13:43 2010 +0200

    Port gconfd-2 to GDBus

 gconf/gconfd.c |  290 +++++++++++++++++++++++++-------------------------------
 1 files changed, 131 insertions(+), 159 deletions(-)
---
diff --git a/gconf/gconfd.c b/gconf/gconfd.c
index 66c18ff..bf68592 100644
--- a/gconf/gconfd.c
+++ b/gconf/gconfd.c
@@ -55,7 +55,7 @@
 #endif
 #include <locale.h>
 
-#include <dbus/dbus-glib-lowlevel.h>
+#include <gio/gio.h>
 
 #ifdef G_OS_WIN32
 #include <io.h>
@@ -488,152 +488,140 @@ gconf_get_poa (void)
   return the_poa;
 }
 
-static const char *
-get_introspection_xml (void)
-{
-  return "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
-         "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\";>\n"
-         "<node>\n"
-         "  <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
-         "    <method name=\"Introspect\">\n"
-         "      <arg name=\"introspection_xml\" direction=\"out\" type=\"s\"/>\n"
-         "    </method>\n"
-         "  </interface>\n"
-         "  <interface name=\"org.gnome.GConf\">\n"
-         "    <method name=\"GetIOR\">\n"
-         "      <arg name=\"ior\" direction=\"out\" type=\"s\"/>\n"
-         "    </method>\n"
-         "  </interface>\n"
-         "</node>\n";
-}
-
-static DBusHandlerResult
-bus_message_handler (DBusConnection *connection,
-                     DBusMessage    *message,
-                     void           *user_data)
+static void
+handle_method_call (GDBusConnection       *connection,
+                    const gchar           *sender,
+                    const gchar           *object_path,
+                    const gchar           *interface_name,
+                    const gchar           *method_name,
+                    GVariant              *parameters,
+                    GDBusMethodInvocation *invocation,
+                    gpointer               user_data)
 {
-  DBusMessage *reply;
-
-  reply = NULL;
-
-  if (dbus_message_is_signal (message,
-                              DBUS_INTERFACE_LOCAL,
-                              "Disconnected"))
-    {
-      gconf_main_quit ();
-      return DBUS_HANDLER_RESULT_HANDLED;
-    }
-  else if (dbus_message_is_method_call (message,
-                                        "org.freedesktop.DBus.Introspectable",
-                                        "Introspect"))
-    {
-      const char *introspection_xml;
-
-      introspection_xml = get_introspection_xml ();
-
-      reply = dbus_message_new_method_return (message);
-      dbus_message_append_args (reply, DBUS_TYPE_STRING, &introspection_xml,
-                                DBUS_TYPE_INVALID);
-
-    }
-  else if (dbus_message_is_method_call (message,
-                                        "org.gnome.GConf",
-                                        "GetIOR"))
+  if (g_strcmp0 (method_name, "GetIOR") == 0)
     {
-      const char *ior;
-
-      ior = gconf_get_daemon_ior ();
-
-      reply = dbus_message_new_method_return (message);
-      dbus_message_append_args (reply, DBUS_TYPE_STRING, &ior, DBUS_TYPE_INVALID);
-    }
-
-  if (reply != NULL)
-    {
-      dbus_connection_send (connection, reply, NULL);
-      dbus_message_unref (reply);
-      return DBUS_HANDLER_RESULT_HANDLED;
+      g_dbus_method_invocation_return_value (invocation,
+                                             g_variant_new ("(s)", gconf_get_daemon_ior ()));
     }
+}
 
-  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+static void
+on_name_acquired (GDBusConnection *connection,
+                  const gchar     *name,
+                  gpointer         user_data)
+{
+  * (gboolean *) user_data = TRUE;
+  gconf_main_quit ();
 }
 
-static DBusConnection *
-get_on_d_bus (void)
+static void
+on_name_lost (GDBusConnection *connection,
+              const gchar     *name,
+              gpointer         user_data)
 {
-  DBusConnection *connection;
-  DBusError bus_error;
-  int result;
+  * (gboolean *) user_data = FALSE;
 
-  dbus_error_init (&bus_error);
-  connection = dbus_bus_get (DBUS_BUS_SESSION, &bus_error);
+  gconf_log (GCL_WARNING,
+             _("Failed to get bus name for daemon, exiting."));
 
-  if (dbus_error_is_set (&bus_error))
+  gconf_main_quit ();
+}
+
+static GDBusConnection *
+own_org_gnome_GConf (void)
+{
+  static const char introspection_xml[] =
+    "<node>"
+      "<interface name='org.gnome.GConf'>"
+        "<method name='GetIOR'>"
+          "<arg name='ior' direction='out' type='s'/>"
+        "</method>"
+      "</interface>"
+    "</node>";
+  static const GDBusInterfaceVTable interface_vtable =
+  {
+    handle_method_call,
+    NULL,
+    NULL
+  };
+
+  GDBusConnection *connection;
+  GDBusNodeInfo *introspection_data;
+  guint registration_id;
+  guint owner_id;
+  gboolean *do_own_ptr;
+  GError *error = NULL;
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+  if (connection == NULL)
     {
-      gconf_log (GCL_ERR, _("Could not connect to session bus: %s"), bus_error.message);
-      dbus_error_free (&bus_error);
+      gconf_log (GCL_ERR, _("Could not connect to session bus: %s"), error->message);
+      g_error_free (error);
       return NULL;
     }
 
-  dbus_connection_setup_with_g_main (connection, NULL);
+  introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
+  g_assert (introspection_data != NULL);
 
-  if (!dbus_connection_add_filter (connection, (DBusHandleMessageFunction)
-                                  bus_message_handler, NULL, NULL))
+  registration_id = g_dbus_connection_register_object (connection,
+                                                       "/org/gnome/GConf",
+                                                       "org.gnome.GConf",
+                                                       introspection_data->interfaces[0],
+                                                       &interface_vtable,
+                                                       introspection_data,
+                                                       (GDestroyNotify) g_dbus_node_info_unref,
+                                                       &error);
+  if (registration_id == 0)
     {
-      dbus_connection_unref (connection);
+      gconf_log (GCL_ERR, "Failed to register object: %s", error->message);
+      g_error_free (error);
+      g_object_unref (connection);
       return NULL;
     }
 
-  dbus_connection_set_exit_on_disconnect (connection, FALSE);
+  g_dbus_connection_set_exit_on_close (connection, FALSE);
+  g_signal_connect (connection, "closed", G_CALLBACK (gconf_main_quit), NULL);
 
-  result = dbus_bus_request_name (connection, "org.gnome.GConf",
-                                  0, &bus_error);
+  do_own_ptr = g_new (gboolean, 1);
+  *do_own_ptr = FALSE;
 
-  if (dbus_error_is_set (&bus_error))
-    {
-      gconf_log (GCL_WARNING,
-                 _("Failed to get bus name for daemon, exiting: %s"),
-                 bus_error.message);
-      dbus_error_free (&bus_error);
-    }
+  owner_id = g_bus_own_name_on_connection (connection,
+                                           "org.gnome.GConf",
+                                           G_BUS_NAME_OWNER_FLAGS_NONE,
+                                           on_name_acquired,
+                                           on_name_lost,
+                                           do_own_ptr, (GDestroyNotify) g_free);
 
-  if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
-    {
-      dbus_connection_unref (connection);
-      return NULL;
-    }
+  gconf_main ();
+
+  if (*do_own_ptr)
+    return connection;
 
-  return connection;
+  g_object_unref (connection);
+  return NULL;
 }
 
 #ifdef ENABLE_DEFAULTS_SERVICE
 /* listen on system bus for defaults changes */
 
-static DBusHandlerResult
-system_bus_message_handler (DBusConnection *connection,
-			    DBusMessage    *message,
-			    void           *user_data)
-{
-  DBusMessage *reply;
-
-  reply = NULL;
-
-  if (dbus_message_is_signal (message,
-			      "org.gnome.GConf.Defaults",
-                              "SystemSet"))
-    {
-      DBusError bus_error;
-      char **keys;
-      int n_keys;
-
-      dbus_error_init (&bus_error);
-      if (dbus_message_get_args (message, &bus_error,
-				 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &keys, &n_keys,
-				 DBUS_TYPE_INVALID))
+static void
+system_set_cb (GDBusConnection *connection,
+               const gchar *sender_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *signal_name,
+               GVariant *parameters,
+               gpointer user_data)
+{
+  if (g_strcmp0 (sender_name, "org.gnome.GConf.Defaults") == 0 &&
+      g_strcmp0 (signal_name, "SystemSet") == 0)
+    {
+      if (g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(as)")))
 	{
-	  char **key;
 	  GConfSources *system_sources;
 	  GSList addresses;
+          GVariantIter *iter;
+          const char *key;
 
 	  gconf_log (GCL_DEBUG, "System defaults changed.  Notifying.");
 
@@ -643,61 +631,43 @@ system_bus_message_handler (DBusConnection *connection,
 
 	  gconfd_clear_cache_for_sources (system_sources);
 
-	  for (key = keys; *key; key++)
-	    gconfd_notify_other_listeners (NULL, system_sources, *key);
+          g_variant_get (parameters, "(as)", &iter);
+          while (g_variant_iter_loop (iter, "&s", &key))
+            gconfd_notify_other_listeners (NULL, system_sources, key);
+          g_variant_iter_free (iter);
 
 	  gconf_sources_free (system_sources);
-
-	  dbus_free_string_array (keys);
 	}
       else
         {
-	  gconf_log (GCL_DEBUG, "SystemSet signal received, but error getting message: %s", bus_error.message);
+	  gconf_log (GCL_DEBUG, "SystemSet signal received, but wrong paramters type '%s'",
+                     g_variant_get_type_string (parameters));
 	}
-      dbus_error_free (&bus_error);
-
-      return DBUS_HANDLER_RESULT_HANDLED;
     }
-
-  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
-static DBusConnection *
+static guint
 get_on_system_bus (void)
 {
-  DBusConnection *connection;
-  DBusError bus_error;
-
-  dbus_error_init (&bus_error);
-  connection = dbus_bus_get (DBUS_BUS_SYSTEM, &bus_error);
+  GDBusConnection *connection;
+  GError *error = NULL;
 
-  if (dbus_error_is_set (&bus_error))
-    {
-      gconf_log (GCL_ERR, _("Could not connect to system bus: %s"), bus_error.message);
-      dbus_error_free (&bus_error);
-      return NULL;
-    }
-
-  dbus_connection_setup_with_g_main (connection, NULL);
-
-  dbus_bus_add_match (connection, "type='signal',interface='org.gnome.GConf.Defaults'", &bus_error);
-  dbus_connection_flush(connection);
-  if (dbus_error_is_set (&bus_error))
-    {
-      gconf_log (GCL_DEBUG, "Failed to add signal match to system bus: %s", bus_error.message);
-      dbus_connection_unref (connection);
-      return NULL;
-    }
-
-  if (!dbus_connection_add_filter (connection, (DBusHandleMessageFunction)
-                                   system_bus_message_handler, NULL, NULL))
+  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
+  if (connection == NULL)
     {
-      gconf_log (GCL_DEBUG, "Failed to add message filter to system bus.");
-      dbus_connection_unref (connection);
-      return NULL;
+      gconf_log (GCL_ERR, _("Could not connect to system bus: %s"), error->message);
+      g_error_free (error);
+      return 0;
     }
-
-  return connection;
+    
+  return g_dbus_connection_signal_subscribe (connection,
+                                             NULL /* FIXME? */,
+                                             "org.gnome.GConf.Defaults",
+                                             "SystemSet",
+                                             "/" /* really? */,
+                                             NULL,
+                                             system_set_cb,
+                                             NULL, NULL);
 }
 #endif  /* ENABLE_DEFAULTS_SERVICE */
 
@@ -729,7 +699,7 @@ main(int argc, char** argv)
   GError *err;
   int dev_null_fd;
   int write_byte_fd;
-  DBusConnection *connection;
+  GDBusConnection *connection;
 
   _gconf_init_i18n ();
   setlocale (LC_ALL, "");
@@ -866,8 +836,7 @@ main(int argc, char** argv)
   gconf_set_daemon_ior (ior);
   CORBA_free (ior);
 
-  connection = get_on_d_bus ();
-
+  connection = own_org_gnome_GConf ();
   if (connection != NULL)
     {
       /* This loads backends and so on. It needs to be done before
@@ -909,6 +878,9 @@ main(int argc, char** argv)
 
   gconf_main ();
 
+  g_object_unref (connection);
+  connection = NULL;
+
   if (in_shutdown)
     exit_code = 1; /* means someone already called enter_shutdown() */
   



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