[Xfce4-commits] <terminal:master> Improve the D-Bus code a bit.

Nick Schermer noreply at xfce.org
Tue Dec 8 10:56:02 CET 2009


Updating branch refs/heads/master
         to f9852ea0d8af12b0e4e61b8fd6a6806a962ac4d6 (commit)
       from 3ee767f74d27327b3bc813fc17bedff2217bbe73 (commit)

commit f9852ea0d8af12b0e4e61b8fd6a6806a962ac4d6
Author: Nick Schermer <nick at xfce.org>
Date:   Tue Dec 8 09:39:20 2009 +0100

    Improve the D-Bus code a bit.
    
    Also add a new error code when the arguments were
    not parsed successfully.

 terminal/terminal-app.h       |    2 +
 terminal/terminal-config.h.in |    1 +
 terminal/terminal-dbus.c      |   56 +++++++++++++++++-----------------------
 3 files changed, 27 insertions(+), 32 deletions(-)

diff --git a/terminal/terminal-app.h b/terminal/terminal-app.h
index e109f45..8471f2b 100644
--- a/terminal/terminal-app.h
+++ b/terminal/terminal-app.h
@@ -37,6 +37,8 @@ enum _TerminalError
   TERMINAL_ERROR_LINKER_FAILURE,
   /* different user id in service */
   TERMINAL_ERROR_USER_MISMATCH,
+  /* parsing the options failed */
+  TERMINAL_ERROR_OPTIONS,
   /* general failure */
   TERMINAL_ERROR_FAILED,
 };
diff --git a/terminal/terminal-config.h.in b/terminal/terminal-config.h.in
index 8591116..629b0fb 100644
--- a/terminal/terminal-config.h.in
+++ b/terminal/terminal-config.h.in
@@ -31,6 +31,7 @@ G_BEGIN_DECLS
 #define TERMINAL_DBUS_SERVICE       "org.xfce.Terminal at TERMINAL_VERSION_DBUS@"
 #define TERMINAL_DBUS_ERROR_USER    "org.xfce.Terminal at TERMINAL_VERSION_DBUS@.ErrorUser"
 #define TERMINAL_DBUS_ERROR_GENERAL "org.xfce.Terminal at TERMINAL_VERSION_DBUS@.ErrorGeneral"
+#define TERMINAL_DBUS_ERROR_OPTIONS "org.xfce.Terminal at TERMINAL_VERSION_DBUS@.ErrorOptions"
 #define TERMINAL_DBUS_PATH          "/org/xfce/Terminal"
 
 G_END_DECLS
diff --git a/terminal/terminal-dbus.c b/terminal/terminal-dbus.c
index 0477e9f..941cd33 100644
--- a/terminal/terminal-dbus.c
+++ b/terminal/terminal-dbus.c
@@ -80,9 +80,9 @@ handle_message (DBusConnection *connection,
    * error can be TERMINAL_DBUS_ERROR_USER if the user ids
    * doesn't match, where the caller is expected to spawn
    * a new terminal of its own and do _NOT_ establish a
-   * D-BUS service, or TERMINAL_DBUS_ERROR_GENERAL, which
-   * indicates a general problem, e.g. invalid parameters
-   * or something like that.
+   * D-BUS service, a TERMINAL_DBUS_ERROR_OPTIONS when
+   * parsing the options failed or TERMINAL_DBUS_ERROR_GENERAL,
+   * which  indicates a general problem.
    */
 
   if (dbus_message_is_method_call (message,
@@ -100,29 +100,19 @@ handle_message (DBusConnection *connection,
           reply = dbus_message_new_error (message,
                                           TERMINAL_DBUS_ERROR_GENERAL,
                                           derror.message);
-          dbus_connection_send (connection, reply, NULL);
-          dbus_message_unref (reply);
-          dbus_error_free (&derror);
-          return DBUS_HANDLER_RESULT_HANDLED;
         }
-
-      /* check user id */
-      if (user_id != getuid ())
+      else if (user_id != getuid ())
         {
           reply = dbus_message_new_error (message,
                                           TERMINAL_DBUS_ERROR_USER,
                                           _("User id mismatch"));
-          dbus_connection_send (connection, reply, NULL);
-          dbus_free_string_array (argv);
-          dbus_message_unref (reply);
-          return DBUS_HANDLER_RESULT_HANDLED;
         }
-
-      if (!terminal_app_process (app, argv, argc, &error))
+      else if (!terminal_app_process (app, argv, argc, &error))
         {
           reply = dbus_message_new_error (message,
-                                          TERMINAL_DBUS_ERROR_GENERAL,
+                                          TERMINAL_DBUS_ERROR_OPTIONS,
                                           error->message);
+          g_error_free (error);
         }
       else
         {
@@ -234,6 +224,8 @@ terminal_dbus_invoke_launch (gint     argc,
   DBusMessage    *message;
   DBusMessage    *result;
   DBusError       derror;
+  gint            code;
+  gboolean        retval = TRUE;
 
   terminal_return_val_if_fail (argc > 0, FALSE);
   terminal_return_val_if_fail (argv != NULL, FALSE);
@@ -272,34 +264,34 @@ terminal_dbus_invoke_launch (gint     argc,
   result = dbus_connection_send_with_reply_and_block (connection, message, 2000, &derror);
   dbus_message_unref (message);
 
+  /* when we reply with an error, the error is put in the derror
+   * and the result is %NULL */
   if (result == NULL)
     {
-      dbus_set_g_error (error, &derror);
-      dbus_error_free (&derror);
-      return FALSE;
-    }
+set_error:
+      if (dbus_error_has_name (&derror, TERMINAL_DBUS_ERROR_USER))
+        code = TERMINAL_ERROR_USER_MISMATCH;
+      else if (dbus_error_has_name (&derror, TERMINAL_DBUS_ERROR_OPTIONS))
+        code = TERMINAL_ERROR_OPTIONS;
+      else
+        code = TERMINAL_ERROR_FAILED;
 
-  if (dbus_message_is_error (result, TERMINAL_DBUS_ERROR_USER))
-    {
-      dbus_set_error_from_message (&derror, result);
-      g_set_error (error, TERMINAL_ERROR, TERMINAL_ERROR_USER_MISMATCH,
-                   "%s", derror.message);
-      dbus_message_unref (result);
+      g_set_error (error, TERMINAL_ERROR, code, "%s", derror.message);
       dbus_error_free (&derror);
+
       return FALSE;
     }
 
+  /* handle other types of errors */
   if (dbus_message_get_type (result) == DBUS_MESSAGE_TYPE_ERROR)
     {
       dbus_set_error_from_message (&derror, result);
-      g_set_error (error, TERMINAL_ERROR, TERMINAL_ERROR_FAILED,
-                   "%s", derror.message);
       dbus_message_unref (result);
-      dbus_error_free (&derror);
-      return FALSE;
+      goto set_error;
     }
 
   dbus_message_unref (result);
-  return TRUE;
+
+  return retval;
 }
 



More information about the Xfce4-commits mailing list