[Xfce4-commits] [apps/mousepad] 01/04: Port to GDBus (Bug #14337)

noreply at xfce.org noreply at xfce.org
Wed Jul 24 02:39:56 CEST 2019


This is an automated email from the git hooks/post-receive script.

a   n   d   r   e       p   u   s   h   e   d       a       c   o   m   m   i   t       t   o       b   r   a   n   c   h       m   a   s   t   e   r   
   in repository apps/mousepad.

commit e9e4e30777a4bb24f9e6045942bdfb8937a1a571
Author: Arkadiy Illarionov <qarkai at gmail.com>
Date:   Sat Apr 14 14:25:41 2018 +0300

    Port to GDBus (Bug #14337)
---
 .gitignore                       |   1 +
 configure.ac.in                  |   2 +-
 mousepad/Makefile.am             |   5 +-
 mousepad/main.c                  |  10 +--
 mousepad/mousepad-dbus-infos.xml |   8 +-
 mousepad/mousepad-dbus.c         | 162 ++++++++++++++++-----------------------
 mousepad/mousepad-dbus.h         |  12 +--
 7 files changed, 77 insertions(+), 123 deletions(-)

diff --git a/.gitignore b/.gitignore
index e2234c4..411f37b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -31,6 +31,7 @@ missing
 mkinstalldirs
 mousepad/.deps/
 mousepad/mousepad
+mousepad/mousepad-dbus-infos.c
 mousepad/mousepad-dbus-infos.h
 mousepad/mousepad-marshal.c
 mousepad/mousepad-marshal.h
diff --git a/configure.ac.in b/configure.ac.in
index 790d7c0..a3d7be7 100644
--- a/configure.ac.in
+++ b/configure.ac.in
@@ -129,7 +129,7 @@ AM_CONDITIONAL([WITH_KEYFILE_SETTINGS],
 dnl **********************************
 dnl *** Optional support for D-BUS ***
 dnl **********************************
-XDT_CHECK_OPTIONAL_PACKAGE([DBUS], [dbus-glib-1],
+XDT_CHECK_OPTIONAL_PACKAGE([DBUS], [dbus-1],
                            [0.34], [dbus], [D-BUS support])
 
 dnl ***************************************************
diff --git a/mousepad/Makefile.am b/mousepad/Makefile.am
index 24e06aa..7bd0f8c 100644
--- a/mousepad/Makefile.am
+++ b/mousepad/Makefile.am
@@ -88,6 +88,7 @@ mousepad_LDADD = \
 
 if HAVE_DBUS
 mousepad_built_sources +=	\
+	mousepad-dbus-infos.c \
 	mousepad-dbus-infos.h
 
 mousepad_dbus_sources = \
@@ -119,8 +120,8 @@ BUILT_SOURCES = \
 	mousepad-prefs-dialog-ui.h
 
 if HAVE_DBUS
-mousepad-dbus-infos.h: mousepad-dbus-infos.xml Makefile
-	$(AM_V_GEN) dbus-binding-tool --prefix=mousepad_dbus_service --mode=glib-server $< > $@
+mousepad-dbus-infos.c mousepad-dbus-infos.h: mousepad-dbus-infos.xml Makefile
+	$(AM_V_GEN) gdbus-codegen --interface-prefix org.xfce. --generate-c-code mousepad-dbus-infos $<
 endif
 
 mousepad-window-ui.h: mousepad-window-ui.xml Makefile
diff --git a/mousepad/main.c b/mousepad/main.c
index b8c5c08..2b5b1cc 100644
--- a/mousepad/main.c
+++ b/mousepad/main.c
@@ -57,9 +57,6 @@ main (gint argc, gchar **argv)
   MousepadApplication *application;
   GError              *error = NULL;
   gchar               *working_directory;
-#ifdef HAVE_DBUS
-  MousepadDBusService *dbus_service;
-#endif
 
   /* bind the text domain to the locale directory */
   bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
@@ -186,7 +183,7 @@ main (gint argc, gchar **argv)
     {
 #ifdef HAVE_DBUS
       /* register with dbus */
-      dbus_service = g_object_new (MOUSEPAD_TYPE_DBUS_SERVICE, NULL);
+      mousepad_dbus_service_init ();
 #endif
 
       /* enter the main loop */
@@ -194,11 +191,6 @@ main (gint argc, gchar **argv)
 
       /* Shutdown xfconf */
       xfconf_shutdown();
-
-#ifdef HAVE_DBUS
-      /* release dbus service reference */
-      g_object_unref (G_OBJECT (dbus_service));
-#endif
     }
 
   /* release application reference */
diff --git a/mousepad/mousepad-dbus-infos.xml b/mousepad/mousepad-dbus-infos.xml
index 3f0fab1..971c797 100644
--- a/mousepad/mousepad-dbus-infos.xml
+++ b/mousepad/mousepad-dbus-infos.xml
@@ -26,19 +26,19 @@
     This inferface is internally used by Mousepad and should not be used externally.
   -->
   <interface name="org.xfce.Mousepad">
-    <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="mousepad_dbus_service" />
+    <annotation name="org.gtk.GDBus.C.Name" value="MousepadDBusService" />
 
     <!--
-      LaunchFiles (working-directory : STRING, filenames : ARRAY OF STRING : VOID
+      LaunchFiles (working_directory : STRING, filenames : ARRAY OF STRING : VOID
 
-      working-directory : the directory, relative to which filenames should
+      working_directory : the directory, relative to which filenames should
                           be interpreted.
       filenames         : an array of file names to launch. The file names may
                           be either file:-URIs, absolute paths or paths relative
                           to the working-directory.
     -->
     <method name="LaunchFiles">
-      <arg direction="in" name="working-directory" type="s" />
+      <arg direction="in" name="working_directory" type="s" />
       <arg direction="in" name="filenames" type="as" />
     </method>
 
diff --git a/mousepad/mousepad-dbus.c b/mousepad/mousepad-dbus.c
index 46ca823..9031d5f 100644
--- a/mousepad/mousepad-dbus.c
+++ b/mousepad/mousepad-dbus.c
@@ -16,11 +16,12 @@
 
 #include <mousepad/mousepad-private.h>
 #include <mousepad/mousepad-dbus.h>
+/* include the dbus glue generated by gdbus-codegen */
+#include <mousepad/mousepad-dbus-infos.h>
 #include <mousepad/mousepad-application.h>
 
-#include <dbus/dbus-glib-lowlevel.h>
-#include <dbus/dbus-glib.h>
 #include <dbus/dbus.h>
+#include <gio/gio.h>
 
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
@@ -36,97 +37,52 @@
 
 
 
-static void      mousepad_dbus_service_finalize      (GObject              *object);
-static gboolean  mousepad_dbus_service_launch_files  (MousepadDBusService  *dbus_service,
-                                                      const gchar          *working_directory,
-                                                      gchar               **filenames,
-                                                      GError              **error);
-static gboolean  mousepad_dbus_service_terminate     (MousepadDBusService  *dbus_service,
-                                                      GError              **error);
-
-
-
-/* include the dbus glue generated by dbus-binding-tool */
-#include <mousepad/mousepad-dbus-infos.h>
-
-
-
-struct _MousepadDBusServiceClass
-{
-  GObjectClass __parent__;
-};
-
-struct _MousepadDBusService
-{
-  GObject __parent__;
-
-  DBusGConnection *connection;
-};
-
-
-
-G_DEFINE_TYPE (MousepadDBusService, mousepad_dbus_service, G_TYPE_OBJECT)
+static gboolean  mousepad_dbus_service_launch_files  (MousepadDBusService    *dbus_service,
+                                                      GDBusMethodInvocation  *invocation,
+                                                      const gchar            *working_directory,
+                                                      gchar                 **filenames,
+                                                      gpointer                user_data);
+static gboolean  mousepad_dbus_service_terminate     (MousepadDBusService   *dbus_service,
+                                                      GDBusMethodInvocation *invocation,
+                                                      gpointer               user_data);
 
 
 
 static void
-mousepad_dbus_service_class_init (MousepadDBusServiceClass *klass)
+on_name_acquired (GDBusConnection *connection,
+                  const gchar *name,
+                  gpointer user_data)
 {
-  GObjectClass *gobject_class;
-
-  gobject_class = G_OBJECT_CLASS (klass);
-  gobject_class->finalize = mousepad_dbus_service_finalize;
-
-  /* install the D-BUS info for our class */
-  dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (klass), &dbus_glib_mousepad_dbus_service_object_info);
+  MousepadDBusService *skeleton;
+
+  skeleton = mousepad_dbus_service_skeleton_new ();
+  g_signal_connect (skeleton,
+                    "handle-launch-files",
+                    G_CALLBACK (mousepad_dbus_service_launch_files),
+                    NULL);
+  g_signal_connect (skeleton,
+                    "handle-terminate",
+                    G_CALLBACK (mousepad_dbus_service_terminate),
+                    NULL);
+  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton),
+                                    connection,
+                                    MOUSEPAD_DBUS_PATH,
+                                    NULL);
 }
 
 
 
-static void
-mousepad_dbus_service_init (MousepadDBusService *dbus_service)
+void
+mousepad_dbus_service_init (void)
 {
-  GError *error = NULL;
-
-  /* try to connect to the session bus */
-  dbus_service->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-
-  if (G_LIKELY (dbus_service->connection != NULL))
-    {
-      /* register the /org/xfce/TextEditor object for Mousepad */
-      dbus_g_connection_register_g_object (dbus_service->connection, MOUSEPAD_DBUS_PATH, G_OBJECT (dbus_service));
-
-      /* request the org.xfce.Mousepad name for Mousepad */
-      dbus_bus_request_name (dbus_g_connection_get_connection (dbus_service->connection),
-                             MOUSEPAD_DBUS_INTERFACE, DBUS_NAME_FLAG_REPLACE_EXISTING, NULL);
-    }
-  else
-    {
-#ifdef NDEBUG
-      /* hide this warning when the user is root and debug is disabled */
-      if (geteuid () != 0)
-#endif
-        {
-          /* notify the user that D-BUS service won't be available */
-          g_message ("Failed to connect to the D-BUS session bus: %s\n", error->message);
-        }
-
-      g_error_free (error);
-    }
-}
-
-
-
-static void
-mousepad_dbus_service_finalize (GObject *object)
-{
-  MousepadDBusService *dbus_service = MOUSEPAD_DBUS_SERVICE (object);
-
-  /* release the D-BUS connection object */
-  if (G_LIKELY (dbus_service->connection != NULL))
-    dbus_g_connection_unref (dbus_service->connection);
-
-  (*G_OBJECT_CLASS (mousepad_dbus_service_parent_class)->finalize) (object);
+  g_bus_own_name (G_BUS_TYPE_SESSION,
+                  MOUSEPAD_DBUS_INTERFACE,
+                  G_BUS_NAME_OWNER_FLAGS_REPLACE,
+                  NULL,
+                  on_name_acquired,
+                  NULL,
+                  NULL,
+                  NULL);
 }
 
 
@@ -134,11 +90,12 @@ mousepad_dbus_service_finalize (GObject *object)
 /**
  * mousepad_dbus_service_launch_files:
  * @dbus_service      : A #MousepadDBusService.
+ * @invocation        : A #GDBusMethodInvocation
  * @working_directory : The default working directory for this window.
  * @filenames         : A list of filenames we try to open in tabs. The file names
  *                      can either be absolute paths, supported URIs or relative file
  *                      names to @working_directory or %NULL for an untitled document.
- * @error             : Return location for errors, not used atm.
+ * @user_data         : User data, not used atm.
  *
  * This function is activated by DBus (service) and opens a new window in this instance of
  * Mousepad.
@@ -146,21 +103,30 @@ mousepad_dbus_service_finalize (GObject *object)
  * Return value: %TRUE on success, %FALSE if @error is set.
  **/
 static gboolean
-mousepad_dbus_service_launch_files (MousepadDBusService  *dbus_service,
-                                    const gchar          *working_directory,
-                                    gchar               **filenames,
-                                    GError              **error)
+mousepad_dbus_service_launch_files (MousepadDBusService    *dbus_service,
+                                    GDBusMethodInvocation  *invocation,
+                                    const gchar            *working_directory,
+                                    gchar                 **filenames,
+                                    gpointer                user_data)
 {
   MousepadApplication *application;
 
-  g_return_val_if_fail (g_path_is_absolute (working_directory), FALSE);
-  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  if (!g_path_is_absolute (working_directory))
+    {
+      g_dbus_method_invocation_return_error_literal (invocation,
+                                                     G_DBUS_ERROR,
+                                                     G_DBUS_ERROR_INVALID_ARGS,
+                                                     "Argument working_directory must be an absolute path");
+      return FALSE;
+    }
 
   /* open a mousepad window */
   application = mousepad_application_get ();
   mousepad_application_new_window_with_files (application, NULL, working_directory, filenames);
   g_object_unref (G_OBJECT (application));
 
+  mousepad_dbus_service_complete_launch_files (dbus_service, invocation);
+
   return TRUE;
 }
 
@@ -169,19 +135,23 @@ mousepad_dbus_service_launch_files (MousepadDBusService  *dbus_service,
 /**
  * mousepad_dbus_service_terminate:
  * @dbus_service : A #MousepadDBusService.
- * @error        : Return location for errors, not used atm.
+ * @invocation   : A #GDBusMethodInvocation
+ * @error        : User data, not used atm.
  *
  * This function quits this instance of Mousepad.
  *
  * Return value: %TRUE on success.
  **/
 static gboolean
-mousepad_dbus_service_terminate (MousepadDBusService  *dbus_service,
-                                 GError              **error)
+mousepad_dbus_service_terminate (MousepadDBusService   *dbus_service,
+                                 GDBusMethodInvocation *invocation,
+                                 gpointer               user_data)
 {
   /* leave the Gtk main loop as soon as possible */
   gtk_main_quit ();
 
+  mousepad_dbus_service_complete_terminate (dbus_service, invocation);
+
   /* we cannot fail */
   return TRUE;
 }
@@ -212,7 +182,7 @@ mousepad_dbus_client_send (DBusMessage  *message,
   connection = dbus_bus_get (DBUS_BUS_SESSION, &derror);
   if (G_UNLIKELY (connection == NULL))
     {
-      dbus_set_g_error (error, &derror);
+      g_dbus_error_set_dbus_error (error, derror.name, derror.message, NULL);
       dbus_error_free (&derror);
       return FALSE;
     }
@@ -225,7 +195,7 @@ mousepad_dbus_client_send (DBusMessage  *message,
     {
       /* check if there was just no instance running */
       if (!dbus_error_has_name (&derror, DBUS_ERROR_NAME_HAS_NO_OWNER))
-        dbus_set_g_error (error, &derror);
+        g_dbus_error_set_dbus_error (error, derror.name, derror.message, NULL);
 
       dbus_error_free (&derror);
       return FALSE;
@@ -235,7 +205,7 @@ mousepad_dbus_client_send (DBusMessage  *message,
   if (G_UNLIKELY (dbus_message_get_type (result) == DBUS_MESSAGE_TYPE_ERROR))
     {
       dbus_set_error_from_message (&derror, result);
-      dbus_set_g_error (error, &derror);
+      g_dbus_error_set_dbus_error (error, derror.name, derror.message, NULL);
       dbus_message_unref (result);
       dbus_error_free (&derror);
       return FALSE;
diff --git a/mousepad/mousepad-dbus.h b/mousepad/mousepad-dbus.h
index 0c92522..2347d52 100644
--- a/mousepad/mousepad-dbus.h
+++ b/mousepad/mousepad-dbus.h
@@ -19,17 +19,7 @@
 
 G_BEGIN_DECLS
 
-typedef struct _MousepadDBusServiceClass MousepadDBusServiceClass;
-typedef struct _MousepadDBusService      MousepadDBusService;
-
-#define MOUSEPAD_TYPE_DBUS_SERVICE            (mousepad_dbus_service_get_type ())
-#define MOUSEPAD_DBUS_SERVICE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), MOUSEPAD_TYPE_DBUS_SERVICE, MousepadDBusService))
-#define MOUSEPAD_DBUS_SERVICE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), MOUSEPAD_TYPE_DBUS_SERVICE, MousepadDBusServiceClass))
-#define MOUSEPAD_IS_DBUS_SERVICE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MOUSEPAD_TYPE_DBUS_SERVICE))
-#define MOUSEPAD_IS_DBUS_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOUSEPAD_TYPE_DBUS_BRIGDE))
-#define MOUSEPAD_DBUS_SERVICE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), MOUSEPAD_TYPE_DBUS_SERVICE, MousepadDBusServiceClass))
-
-GType     mousepad_dbus_service_get_type    (void) G_GNUC_CONST;
+void      mousepad_dbus_service_init        (void);
 
 gboolean  mousepad_dbus_client_terminate    (GError      **error);
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Xfce4-commits mailing list