[Xfce4-commits] <xfce4-appfinder:master> Use GDbus for communication.

Nick Schermer noreply at xfce.org
Sat Jan 5 21:22:01 CET 2013


Updating branch refs/heads/master
         to 4a065a10945c72c985e254ff1ef13df188f3e11e (commit)
       from 7fc2e009ceddc4c0b29e993a437d1a728654070e (commit)

commit 4a065a10945c72c985e254ff1ef13df188f3e11e
Author: Nick Schermer <nick at xfce.org>
Date:   Sat Jan 5 21:20:28 2013 +0100

    Use GDbus for communication.

 configure.ac.in         |    7 +-
 src/Makefile.am         |    6 +-
 src/appfinder-gdbus.c   |  237 +++++++++++++++++++++++++++++++++
 src/appfinder-gdbus.h   |   36 +++++
 src/appfinder-private.h |    3 +
 src/main.c              |  335 ++++++-----------------------------------------
 6 files changed, 320 insertions(+), 304 deletions(-)

diff --git a/configure.ac.in b/configure.ac.in
index 98d4ca3..61245d7 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -71,12 +71,11 @@ XDT_I18N([@LINGUAS@])
 dnl ***********************************
 dnl *** Check for required packages ***
 dnl ***********************************
-XDT_CHECK_PACKAGE([GLIB], [glib-2.0], [2.28.0])
-XDT_CHECK_PACKAGE([GTHREAD], [gthread-2.0], [2.28.0])
-XDT_CHECK_PACKAGE([GIO], [gio-2.0], [2.28.0])
+XDT_CHECK_PACKAGE([GLIB], [glib-2.0], [2.30.0])
+XDT_CHECK_PACKAGE([GTHREAD], [gthread-2.0], [2.30.0])
+XDT_CHECK_PACKAGE([GIO], [gio-2.0], [2.30.0])
 XDT_CHECK_PACKAGE([LIBXFCE4UTIL], [libxfce4util-1.0], [4.10.0])
 XDT_CHECK_PACKAGE([GARCON], [garcon-1], [0.2.0])
-XDT_CHECK_PACKAGE([DBUS_GLIB], [dbus-glib-1], [0.84])
 XDT_CHECK_PACKAGE([XFCONF], [libxfconf-0], [4.10.0])
 
 dnl ************************************
diff --git a/src/Makefile.am b/src/Makefile.am
index 1c08eb9..b312ca2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -17,6 +17,8 @@ xfce4_appfinder_SOURCES = \
 	appfinder-actions.h \
 	appfinder-category-model.c \
 	appfinder-category-model.h \
+	appfinder-gdbus.c \
+	appfinder-gdbus.h \
 	appfinder-model.c \
 	appfinder-model.h \
 	appfinder-preferences.c \
@@ -34,7 +36,6 @@ xfce4_appfinder_CFLAGS = \
 	$(LIBXFCE4UI_CFLAGS) \
 	$(GARCON_CFLAGS) \
 	$(GIO_CFLAGS) \
-	$(DBUS_GLIB_CFLAGS) \
 	$(XFCONF_CFLAGS) \
 	$(PLATFORM_CFLAGS)
 
@@ -46,8 +47,7 @@ xfce4_appfinder_LDADD = \
 	$(LIBXFCE4UI_LIBS) \
 	$(GARCON_LIBS) \
 	$(XFCONF_LIBS) \
-	$(GIO_LIBS) \
-	$(DBUS_GLIB_LIBS)
+	$(GIO_LIBS)
 
 xfce4_appfinder_LDFLAGS = \
 	-no-undefined \
diff --git a/src/appfinder-gdbus.c b/src/appfinder-gdbus.c
new file mode 100644
index 0000000..4ab342d
--- /dev/null
+++ b/src/appfinder-gdbus.c
@@ -0,0 +1,237 @@
+/*
+ * Copyright (C) 2013 Nick Schermer <nick at xfce.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <libxfce4ui/libxfce4ui.h>
+
+#include <src/appfinder-gdbus.h>
+#include <src/appfinder-private.h>
+
+
+
+#define APPFINDER_DBUS_SERVICE     "org.xfce.Appfinder"
+#define APPFINDER_DBUS_INTERFACE   APPFINDER_DBUS_SERVICE
+#define APPFINDER_DBUS_PATH        "/org/xfce/Appfinder"
+#define APPFINDER_DBUS_METHOD_OPEN "OpenWindow"
+#define APPFINDER_DBUS_METHOD_QUIT "Quit"
+
+
+
+static const gchar appfinder_gdbus_introspection_xml[] =
+  "<node>"
+    "<interface name='" APPFINDER_DBUS_INTERFACE "'>"
+      "<method name='" APPFINDER_DBUS_METHOD_OPEN "'>"
+        "<arg type='b' name='expanded' direction='in'/>"
+        "<arg type='s' name='startup-id' direction='in'/>"
+      "</method>"
+      "<method name='" APPFINDER_DBUS_METHOD_QUIT "'/>"
+    "</interface>"
+  "</node>";
+
+
+
+static void
+appfinder_gdbus_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)
+{
+  gboolean  expanded;
+  gchar    *startup_id = NULL;
+
+  g_return_if_fail (!g_strcmp0 (object_path, APPFINDER_DBUS_PATH));
+  g_return_if_fail (!g_strcmp0 (interface_name, APPFINDER_DBUS_INTERFACE));
+
+  APPFINDER_DEBUG ("received dbus method %s", method_name);
+
+  if (g_strcmp0 (method_name, APPFINDER_DBUS_METHOD_OPEN) == 0)
+    {
+      /* get paramenters */
+      g_variant_get (parameters, "(bs)", &expanded, &startup_id);
+
+      appfinder_window_new (startup_id, expanded);
+
+      /* everything went fine */
+      g_dbus_method_invocation_return_value (invocation, NULL);
+
+      g_free (startup_id);
+    }
+  else if (g_strcmp0 (method_name, APPFINDER_DBUS_METHOD_QUIT) == 0)
+    {
+      /* close all windows and quit */
+      g_printerr ("%s: %s.\n", PACKAGE_NAME, _("Forced to quit"));
+
+      gtk_main_quit ();
+    }
+  else
+    {
+      g_dbus_method_invocation_return_error (invocation,
+          G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD,
+          "Unknown method for DBus service " APPFINDER_DBUS_SERVICE);
+    }
+}
+
+
+
+static const GDBusInterfaceVTable appfinder_gdbus_vtable =
+{
+  appfinder_gdbus_method_call,
+  NULL, /* get property */
+  NULL  /* set property */
+};
+
+
+
+static void
+appfinder_gdbus_bus_acquired (GDBusConnection *connection,
+                              const gchar     *name,
+                              gpointer         user_data)
+{
+  guint          register_id;
+  GDBusNodeInfo *info;
+  GError        *error = NULL;
+
+  info = g_dbus_node_info_new_for_xml (appfinder_gdbus_introspection_xml, NULL);
+  g_assert (info != NULL);
+  g_assert (*info->interfaces != NULL);
+
+  register_id = g_dbus_connection_register_object (connection,
+                                                   APPFINDER_DBUS_PATH,
+                                                   *info->interfaces, /* first iface */
+                                                   &appfinder_gdbus_vtable,
+                                                   user_data,
+                                                   NULL,
+                                                   &error);
+
+  APPFINDER_DEBUG ("registered interface with id %d", register_id);
+
+  if (register_id == 0)
+    {
+      g_message ("Failed to register object: %s", error->message);
+      g_error_free (error);
+    }
+
+  g_dbus_node_info_unref (info);
+}
+
+
+
+gboolean
+appfinder_gdbus_service (GError **error)
+{
+  guint owner_id;
+
+  owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+                             APPFINDER_DBUS_SERVICE,
+                             G_BUS_NAME_OWNER_FLAGS_NONE,
+                             appfinder_gdbus_bus_acquired,
+                             NULL,
+                             NULL,
+                             NULL,
+                             NULL);
+
+  return (owner_id != 0);
+}
+
+
+
+gboolean
+appfinder_gdbus_quit (GError **error)
+{
+  GVariant        *reply;
+  GDBusConnection *connection;
+  GError          *err = NULL;
+  gboolean         result;
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+  if (G_UNLIKELY (connection == NULL))
+    return FALSE;
+
+  reply = g_dbus_connection_call_sync (connection,
+                                       APPFINDER_DBUS_SERVICE,
+                                       APPFINDER_DBUS_PATH,
+                                       APPFINDER_DBUS_INTERFACE,
+                                       APPFINDER_DBUS_METHOD_QUIT,
+                                       NULL,
+                                       NULL,
+                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
+                                       2000,
+                                       NULL,
+                                       &err);
+
+  g_object_unref (connection);
+
+  result = (reply != NULL);
+  if (G_LIKELY (result))
+    g_variant_unref (reply);
+  else
+    g_propagate_error (error, err);
+
+  return result;
+}
+
+
+
+gboolean
+appfinder_gdbus_open_window (gboolean      expanded,
+                             const gchar  *startup_id,
+                             GError      **error)
+{
+  GVariant        *reply;
+  GDBusConnection *connection;
+  GError          *err = NULL;
+  gboolean         result;
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
+  if (G_UNLIKELY (connection == NULL))
+    return FALSE;
+
+  if (startup_id == NULL)
+    startup_id = "";
+
+  reply = g_dbus_connection_call_sync (connection,
+                                       APPFINDER_DBUS_SERVICE,
+                                       APPFINDER_DBUS_PATH,
+                                       APPFINDER_DBUS_INTERFACE,
+                                       APPFINDER_DBUS_METHOD_OPEN,
+                                       g_variant_new ("(bs)",
+                                                      expanded,
+                                                      startup_id),
+                                       NULL,
+                                       G_DBUS_CALL_FLAGS_NO_AUTO_START,
+                                       2000,
+                                       NULL,
+                                       &err);
+
+  g_object_unref (connection);
+
+  result = (reply != NULL);
+  if (G_LIKELY (result))
+    g_variant_unref (reply);
+  else
+    g_propagate_error (error, err);
+
+  return result;
+}
diff --git a/src/appfinder-gdbus.h b/src/appfinder-gdbus.h
new file mode 100644
index 0000000..3ebeca6
--- /dev/null
+++ b/src/appfinder-gdbus.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2013 Nick Schermer <nick at xfce.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __XFCE_APPFINDER_GDBUS_H__
+#define __XFCE_APPFINDER_GDBUS_H__
+
+G_BEGIN_DECLS
+
+#include <gio/gio.h>
+
+gboolean appfinder_gdbus_service     (GError      **error);
+
+gboolean appfinder_gdbus_quit        (GError      **error);
+
+gboolean appfinder_gdbus_open_window (gboolean      expanded,
+                                      const gchar  *startup_id,
+                                      GError      **error);
+
+G_END_DECLS
+
+#endif /* !__XFCE_APPFINDER_GDBUS_H__ */
diff --git a/src/appfinder-private.h b/src/appfinder-private.h
index 9cb4d44..0e98d68 100644
--- a/src/appfinder-private.h
+++ b/src/appfinder-private.h
@@ -52,4 +52,7 @@ void    appfinder_refcount_debug_add (GObject     *object,
 #define appfinder_return_val_if_fail(expr,val) G_STMT_START{ (void)0; }G_STMT_END
 #endif
 
+void appfinder_window_new (const gchar *startup_id,
+                           gboolean     expanded);
+
 #endif /* !__XFCE_APPFINDER_PRIVATE_H__ */
diff --git a/src/main.c b/src/main.c
index 58368e7..d50f6bf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -36,21 +36,10 @@
 #include <garcon/garcon.h>
 #include <xfconf/xfconf.h>
 
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
-
 #include <src/appfinder-window.h>
 #include <src/appfinder-private.h>
 #include <src/appfinder-model.h>
-
-
-
-#define APPFINDER_DBUS_SERVICE     "org.xfce.Appfinder"
-#define APPFINDER_DBUS_INTERFACE   APPFINDER_DBUS_SERVICE
-#define APPFINDER_DBUS_PATH        "/org/xfce/Appfinder"
-#define APPFINDER_DBUS_METHOD_OPEN "OpenWindow"
-#define APPFINDER_DBUS_METHOD_QUIT "Quit"
-#define APPFINDER_DBUS_ERROR       APPFINDER_DBUS_SERVICE ".Error"
+#include <src/appfinder-gdbus.h>
 
 
 
@@ -82,10 +71,6 @@ static GOptionEntry option_entries[] =
 
 
 
-static void appfinder_dbus_unregister (DBusConnection *dbus_connection);
-
-
-
 #ifdef DEBUG
 static void
 appfinder_refcount_debug_weak_notify (gpointer  data,
@@ -188,7 +173,7 @@ appfinder_window_destroyed (GtkWidget *window)
 
 
 
-static void
+void
 appfinder_window_new (const gchar *startup_id,
                       gboolean     expanded)
 {
@@ -208,183 +193,6 @@ appfinder_window_new (const gchar *startup_id,
 
 
 
-static DBusHandlerResult
-appfinder_dbus_message (DBusConnection *dbus_connection,
-                        DBusMessage    *message,
-                        gpointer        user_data)
-{
-  DBusMessage *reply;
-  gboolean     expanded;
-  gchar       *startup_id;
-  DBusError    derror;
-
-  if (dbus_message_is_method_call (message, APPFINDER_DBUS_INTERFACE, APPFINDER_DBUS_METHOD_OPEN))
-    {
-      dbus_error_init (&derror);
-      if (dbus_message_get_args (message, &derror,
-                                 DBUS_TYPE_BOOLEAN, &expanded,
-                                 DBUS_TYPE_STRING, &startup_id,
-                                 DBUS_TYPE_INVALID))
-        {
-          appfinder_window_new (startup_id, expanded);
-          reply = dbus_message_new_method_return (message);
-        }
-      else
-        {
-          reply = dbus_message_new_error (message, APPFINDER_DBUS_ERROR, derror.message);
-          dbus_error_free (&derror);
-        }
-
-      dbus_connection_send (dbus_connection, reply, NULL);
-      dbus_message_unref (reply);
-    }
-  else if (dbus_message_is_method_call (message, APPFINDER_DBUS_INTERFACE, APPFINDER_DBUS_METHOD_QUIT))
-    {
-      /* close all windows and quit */
-      g_printerr ("%s: %s.\n", PACKAGE_NAME, _("Forced to quit"));
-      gtk_main_quit ();
-    }
-  else if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected")
-           || dbus_message_is_signal (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
-    {
-      if (windows != NULL)
-        {
-          /* don't respond to dbus signals and close on last window */
-          appfinder_dbus_unregister (dbus_connection);
-        }
-      else
-        {
-          /* no active windows, just exit the instance */
-          gtk_main_quit ();
-        }
-    }
-  else
-    {
-      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-    }
-
-  return DBUS_HANDLER_RESULT_HANDLED;
-}
-
-
-
-static gboolean
-appfinder_dbus_open_window (DBusConnection *dbus_connection,
-                            const gchar    *startup_id)
-{
-
-  DBusError    derror;
-  DBusMessage *method, *result;
-  gboolean     expanded = !opt_collapsed;
-
-  method = dbus_message_new_method_call (APPFINDER_DBUS_SERVICE,
-                                         APPFINDER_DBUS_PATH,
-                                         APPFINDER_DBUS_INTERFACE,
-                                         APPFINDER_DBUS_METHOD_OPEN);
-
-  if (startup_id == NULL)
-    startup_id = "";
-
-  dbus_message_append_args (method,
-                            DBUS_TYPE_BOOLEAN, &expanded,
-                            DBUS_TYPE_STRING, &startup_id,
-                            DBUS_TYPE_INVALID);
-
-  dbus_message_set_auto_start (method, TRUE);
-  dbus_error_init (&derror);
-  result = dbus_connection_send_with_reply_and_block (dbus_connection, method, 5000, &derror);
-  dbus_message_unref (method);
-
-  if (G_UNLIKELY (result == NULL))
-    {
-       g_critical ("Failed to open window: %s", derror.message);
-       dbus_error_free(&derror);
-       return FALSE;
-    }
-
-  dbus_message_unref (result);
-
-  return TRUE;
-}
-
-
-
-static gint
-appfinder_dbus_quit (void)
-{
-  DBusMessage    *method;
-  DBusConnection *dbus_connection;
-  DBusError       derror;
-  gboolean        succeed = FALSE;
-
-  dbus_error_init (&derror);
-  dbus_connection = dbus_bus_get (DBUS_BUS_SESSION, &derror);
-  if (G_LIKELY (dbus_connection != NULL))
-    {
-      method = dbus_message_new_method_call (APPFINDER_DBUS_SERVICE,
-                                             APPFINDER_DBUS_PATH,
-                                             APPFINDER_DBUS_INTERFACE,
-                                             APPFINDER_DBUS_METHOD_QUIT);
-
-      dbus_message_set_auto_start (method, FALSE);
-      succeed = dbus_connection_send (dbus_connection, method, NULL);
-      dbus_message_unref (method);
-
-      dbus_connection_flush (dbus_connection);
-      dbus_connection_unref (dbus_connection);
-    }
-  else
-    {
-      g_warning ("Unable to open D-Bus connection: %s", derror.message);
-      dbus_error_free (&derror);
-    }
-
-  return succeed ? EXIT_SUCCESS : EXIT_FAILURE;
-}
-
-
-
-static void
-appfinder_dbus_unregister (DBusConnection *dbus_connection)
-{
-  if (service_owner)
-    {
-      service_owner = FALSE;
-
-      dbus_connection_remove_filter (dbus_connection, appfinder_dbus_message, NULL);
-      dbus_connection_unregister_object_path (dbus_connection, APPFINDER_DBUS_PATH);
-      dbus_bus_release_name (dbus_connection, APPFINDER_DBUS_SERVICE, NULL);
-    }
-}
-
-
-
-static gint
-appfinder_daemonize (void)
-{
-#ifdef HAVE_DAEMON
-  return daemon (1, 1);
-#else
-  pid_t pid;
-
-  pid = fork ();
-  if (pid < 0)
-    return -1;
-
-  if (pid > 0)
-    _exit (EXIT_SUCCESS);
-
-#ifdef HAVE_SETSID
-  if (setsid () < 0)
-    return -1;
-#endif
-
-  return 0;
-#endif
-}
-
-
-
 static void
 appfinder_signal_handler (gint signum)
 {
@@ -402,104 +210,15 @@ appfinder_signal_handler (gint signum)
 
 
 
-static DBusConnection *
-appfinder_dbus_service (const gchar *startup_id)
-{
-  DBusError             derror;
-  DBusConnection       *dbus_connection;
-  guint                 dbus_flags;
-  DBusObjectPathVTable  vtable = { NULL, appfinder_dbus_message, NULL, };
-  gint                  result;
-
-  /* become the serivce owner or ask the current owner to spawn an instance */
-  dbus_error_init (&derror);
-  dbus_connection = dbus_bus_get (DBUS_BUS_SESSION, &derror);
-  if (G_LIKELY (dbus_connection != NULL))
-    {
-      dbus_connection_set_exit_on_disconnect (dbus_connection, FALSE);
-
-      dbus_flags = DBUS_NAME_FLAG_DO_NOT_QUEUE | DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
-      if (opt_replace)
-        dbus_flags |= DBUS_NAME_FLAG_REPLACE_EXISTING;
-
-      result = dbus_bus_request_name (dbus_connection, APPFINDER_DBUS_SERVICE, dbus_flags, &derror);
-      if (result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
-        {
-          dbus_connection_setup_with_g_main (dbus_connection, NULL);
-
-          /* watch owner changes */
-          dbus_bus_add_match (dbus_connection, "type='signal',member='NameOwnerChanged',"
-                                               "arg0='"APPFINDER_DBUS_SERVICE"'", NULL);
-
-          /* method handling for the appfinder */
-          if (dbus_connection_register_object_path (dbus_connection, APPFINDER_DBUS_PATH, &vtable, NULL)
-              && dbus_connection_add_filter (dbus_connection, appfinder_dbus_message, NULL, NULL))
-            {
-              APPFINDER_DEBUG ("registered dbus service");
-
-              /* successfully registered the service */
-              service_owner = TRUE;
-
-              /* fork to the background */
-              if (appfinder_daemonize () == -1)
-                {
-                  xfce_message_dialog (NULL, _("Application Finder"),
-                                       GTK_STOCK_DIALOG_ERROR,
-                                       _("Unable to daemonize the process"),
-                                       g_strerror (errno),
-                                       GTK_STOCK_QUIT, GTK_RESPONSE_ACCEPT,
-                                       NULL);
-
-                  _exit (EXIT_FAILURE);
-                }
-
-              APPFINDER_DEBUG ("daemonized the process");
-
-            }
-          else
-            {
-              g_warning ("Failed to register D-Bus filter or vtable");
-            }
-        }
-      else if (result == DBUS_REQUEST_NAME_REPLY_EXISTS)
-        {
-          if (appfinder_dbus_open_window (dbus_connection, startup_id))
-            {
-               /* successfully opened a window in the other instance */
-               dbus_connection_unref (dbus_connection);
-               _exit (EXIT_SUCCESS);
-            }
-        }
-      else
-        {
-          g_warning ("Unable to request D-Bus name: %s", derror.message);
-          dbus_error_free (&derror);
-
-          dbus_connection_unref (dbus_connection);
-          dbus_connection = NULL;
-        }
-    }
-  else
-    {
-      g_warning ("Unable to open D-Bus connection: %s", derror.message);
-      dbus_error_free (&derror);
-    }
-
-  return dbus_connection;
-}
-
-
-
 gint
 main (gint argc, gchar **argv)
 {
-  GError         *error = NULL;
-  const gchar    *desktop;
-  DBusConnection *dbus_connection = NULL;
-  const gchar    *startup_id;
-  GSList         *windows_destroy;
-  const gint      signums[] = { SIGINT, SIGQUIT, SIGTERM, SIGABRT };
-  guint           i;
+  GError      *error = NULL;
+  const gchar *desktop;
+  const gchar *startup_id;
+  GSList      *windows_destroy;
+  const gint   signums[] = { SIGINT, SIGQUIT, SIGTERM, SIGABRT };
+  guint        i;
 
   /* set translation domain */
   xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");
@@ -540,7 +259,9 @@ main (gint argc, gchar **argv)
     }
 
   if (opt_quit)
-    return appfinder_dbus_quit ();
+    {
+      return appfinder_gdbus_quit (NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
+    }
 
   /* setup signal handlers to properly quit the main loop */
   for (i = 0; i < G_N_ELEMENTS (signums); i++)
@@ -553,7 +274,33 @@ main (gint argc, gchar **argv)
   /* become the serivce owner or ask the current
    * owner to spawn an instance */
   if (G_LIKELY (!opt_disable_server))
-    dbus_connection = appfinder_dbus_service (startup_id);
+    {
+      /* try to open a new window */
+      if (appfinder_gdbus_open_window (!opt_collapsed, startup_id, &error))
+        {
+          /* looks ok */
+          return EXIT_SUCCESS;
+        }
+      else if (!g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER))
+        {
+          g_warning ("Unknown DBus error: %s", error->message);
+        }
+
+      g_clear_error (&error);
+
+      /* become service owner */
+      if (appfinder_gdbus_service (NULL))
+        {
+          /* successfully registered the service */
+          service_owner = TRUE;
+
+          APPFINDER_DEBUG ("requested dbus service");
+        }
+      else
+        {
+          g_warning ("Failed to register DBus serice");
+        }
+    }
 
   /* if the value is unset, fallback to XFCE, if the
    * value is empty, allow all applications in the menu */
@@ -587,12 +334,6 @@ main (gint argc, gchar **argv)
   if (model_cache != NULL)
     g_object_unref (G_OBJECT (model_cache));
 
-  if (G_LIKELY (dbus_connection != NULL))
-    {
-      appfinder_dbus_unregister (dbus_connection);
-      dbus_connection_unref (dbus_connection);
-    }
-
   if (windows != NULL)
     {
       /* avoid calling appfinder_window_destroyed */


More information about the Xfce4-commits mailing list