[Xfce4-commits] <libxfcegui4:xfce-4.6> various SessionClient improvements and bug fixes

Brian J. Tarricone noreply at xfce.org
Fri Oct 16 09:48:01 CEST 2009


Updating branch refs/heads/xfce-4.6
         to caffa2a1e66f44d63ea201f186d5fd0c5337f4bf (commit)
       from 428b8807f6cdd5ab5d39b02f76aff9902871e2c1 (commit)

commit caffa2a1e66f44d63ea201f186d5fd0c5337f4bf
Author: Dimitar Zhekov <hamster at mbox.contact.bg>
Date:   Fri Oct 16 00:43:42 2009 -0700

    various SessionClient improvements and bug fixes
    
    * fix crash in client_session_free()
    * strip previous client id from passsed argv before adding the new one
    * fix crash when client_session_free() is called when not connected to
      the session manager
    * some refactoring to avoid code duplication

 NEWS                         |    1 +
 libxfcegui4/session-client.c |  363 +++++++++++++++--------------------------
 2 files changed, 134 insertions(+), 230 deletions(-)

diff --git a/NEWS b/NEWS
index 2a13891..7ddda54 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,7 @@
 4.6.2
 ==================
 - Fix icon sizing for XfceAppMenuItem (bug #5660).
+- Various improvements and bug fixes for SessionClient (bug #5337).
 
 4.6.1
 ==================
diff --git a/libxfcegui4/session-client.c b/libxfcegui4/session-client.c
index 4f036ec..3c63904 100644
--- a/libxfcegui4/session-client.c
+++ b/libxfcegui4/session-client.c
@@ -408,6 +408,7 @@ save_yourself_possibly_done (SessionClient * session_client)
         || (session_client->current_state ==
             SESSION_CLIENT_DONE_WITH_INTERACT))
     {
+        set_clone_restart_commands (session_client);
         SmcSaveYourselfDone ((SmcConn) session_client->session_connection,
                              True);
 
@@ -468,7 +469,6 @@ save_yourself (SmcConn smc_conn, SmPointer client_data, int save_style,
         }
     }
 
-    set_clone_restart_commands (session_client);
     TRACE ("save_yourself - SESSION_CLIENT_SAVING_PHASE_1");
     session_client->current_state = SESSION_CLIENT_SAVING_PHASE_1;
     switch (interact_style)
@@ -563,168 +563,126 @@ interact (SmcConn smc_conn, SmPointer client_data)
     save_yourself_possibly_done (session_client);
 }
 
+#define APPEND 1
+#define REMOVE 2
+
 static void
-set_clone_restart_commands (SessionClient * session_client)
+set_property_from_command (SessionClient * session_client, char *command,
+                           gchar **ptr, gint alter_sm_id)
 {
     SmProp prop, *props[1];
-    gchar **ptr, **args;
-    gint i = 0;
+    gchar **args;
+    gint i;
     gint argc;
 
     SmPropValue *vals;
 
-    if ((ptr = session_client->restart_command))
-    {
-        gboolean have_sm_id = FALSE;
+    if (!ptr)
+        return;
 
-        /* Restart */
-        ptr = session_client->restart_command;
-        args = ptr;
-        i = 0;
-        for (argc = 0; *ptr; ptr++)
+    args = ptr;
+    for (argc = 0; *args; args++)
+    {
+        if (alter_sm_id == APPEND &&
+            !g_ascii_strncasecmp (*args, SM_ID_ARG, strlen (SM_ID_ARG)))
         {
-            if (!g_ascii_strncasecmp (*ptr, SM_ID_ARG, strlen (SM_ID_ARG)))
-            {
-                have_sm_id = TRUE;
-            }
-            argc++;
+            alter_sm_id = FALSE;
         }
-        if (!have_sm_id)
+        argc++;
+    }
+    if (alter_sm_id == APPEND)
+    {
+        argc += 2;
+    }
+    vals = g_new (SmPropValue, argc);
+
+    i = 0;
+    while (*ptr)
+    {
+        if (alter_sm_id == REMOVE &&
+            !g_ascii_strncasecmp (*ptr, SM_ID_ARG, strlen (SM_ID_ARG)))
         {
-            argc += 2;
+            /* skip the argument after SM_ID_ARG */
+            if (ptr[1] != NULL)
+                ptr++;
         }
-        vals = g_new (SmPropValue, argc);
-        ptr = args;
-        while (*ptr)
+        else
         {
             vals[i].length = strlen (*ptr);
-            vals[i++].value = *ptr++;
+            vals[i++].value = *ptr;
         }
-        if (!have_sm_id)
-        {
-            vals[i].length = strlen (SM_ID_ARG);
-            vals[i++].value = SM_ID_ARG;
-            vals[i].length = strlen (session_client->given_client_id);
-            vals[i++].value = session_client->given_client_id;
-        }
-        prop.name = SmRestartCommand;
-        prop.type = SmLISTofARRAY8;
-        prop.vals = vals;
-        prop.num_vals = argc;
-
-        props[0] = ∝
-        SmcSetProperties ((SmcConn) session_client->session_connection, 1,
-                          props);
-        g_free (vals);
+        ptr++;
     }
 
-    /* Clone */
-    if ((ptr =
-         session_client->clone_command ? session_client->
-         clone_command : session_client->restart_command))
+    if (alter_sm_id == APPEND)
     {
-        args = ptr;
-        for (argc = 0; *ptr; ptr++)
-            argc++;
-        vals = g_new (SmPropValue, argc);
-        ptr = args;
-        i = 0;
-        while (*ptr)
-        {
-            vals[i].length = strlen (*ptr);
-            vals[i++].value = *ptr++;
-        }
+        vals[i].length = strlen (SM_ID_ARG);
+        vals[i++].value = SM_ID_ARG;
+        vals[i].length = strlen (session_client->given_client_id);
+        vals[i++].value = session_client->given_client_id;
+    }
 
-        prop.name = SmCloneCommand;
-        prop.type = SmLISTofARRAY8;
-        prop.vals = vals;
-        prop.num_vals = argc;
+    prop.name = command;
+    prop.type = SmLISTofARRAY8;
+    prop.vals = vals;
+    prop.num_vals = i;
 
-        props[0] = ∝
-        SmcSetProperties ((SmcConn) session_client->session_connection, 1,
-                          props);
-        g_free (vals);
-    }
+    props[0] = ∝
+    SmcSetProperties ((SmcConn) session_client->session_connection, 1, props);
+    g_free (vals);
+}
 
-    /* Resign */
-    if ((ptr = session_client->resign_command))
-    {
-        args = ptr;
-        for (argc = 0; *ptr; ptr++)
-            argc++;
-        vals = g_new (SmPropValue, argc);
-        ptr = args;
-        i = 0;
-        while (*ptr)
-        {
-            vals[i].length = strlen (*ptr);
-            vals[i++].value = *ptr++;
-        }
+static void
+set_clone_restart_commands (SessionClient * session_client)
+{
+    /* Restart */
+    set_property_from_command (session_client, SmRestartCommand,
+                               session_client->restart_command, APPEND);
 
-        prop.name = SmResignCommand;
-        prop.type = SmLISTofARRAY8;
-        prop.vals = vals;
-        prop.num_vals = argc;
+    /* Clone */
+    set_property_from_command (session_client, SmCloneCommand,
+                               session_client->clone_command ?
+                               session_client->clone_command :
+                               session_client->restart_command, REMOVE);
 
-        props[0] = ∝
-        SmcSetProperties ((SmcConn) session_client->session_connection, 1,
-                          props);
-        g_free (prop.vals);
-    }
+    /* Resign */
+    set_property_from_command (session_client, SmResignCommand,
+                               session_client->resign_command, FALSE);
 
     /* Discard */
-    if ((ptr = session_client->discard_command))
-    {
-        args = ptr;
-        for (argc = 0; *ptr; ptr++)
-            argc++;
-        vals = g_new (SmPropValue, argc);
-        ptr = args;
-        i = 0;
-        while (*ptr)
-        {
-            vals[i].length = strlen (*ptr);
-            vals[i++].value = *ptr++;
-        }
+    set_property_from_command (session_client, SmDiscardCommand,
+                               session_client->discard_command, FALSE);
 
-        prop.name = SmDiscardCommand;
-        prop.type = SmLISTofARRAY8;
-        prop.vals = vals;
-        prop.num_vals = argc;
+    /* Shutdown */
+    set_property_from_command (session_client, SmShutdownCommand,
+                               session_client->shutdown_command, FALSE);
+}
 
-        props[0] = ∝
-        SmcSetProperties ((SmcConn) session_client->session_connection, 1,
-                          props);
-        g_free (prop.vals);
-    }
+#undef APPEND
+#undef REMOVE
 
-    /* Shutdown */
-    if ((ptr = session_client->shutdown_command))
-    {
-        args = ptr;
-        for (argc = 0; *ptr; ptr++)
-            argc++;
-        vals = g_new (SmPropValue, argc);
-        ptr = args;
-        i = 0;
-        while (*ptr)
-        {
-            vals[i].length = strlen (*ptr);
-            vals[i++].value = *ptr++;
-        }
+#endif
 
-        prop.name = SmShutdownCommand;
-        prop.type = SmLISTofARRAY8;
-        prop.vals = vals;
-        prop.num_vals = argc;
+static gchar** safe_strvdup(gchar ** const source)
+{
+    int     count;
+    int     i;
+    gchar** result;
+    
+    if (source == NULL) {
+        return NULL;
+    }
 
-        props[0] = ∝
-        SmcSetProperties ((SmcConn) session_client->session_connection, 1,
-                          props);
-        g_free (prop.vals);
+    count = g_strv_length((gchar **)source);
+    result = g_new0(gchar*, count + 1);
+
+    for (i = 0; i < count; ++i) {
+        result[i] = g_strdup(source[i]);
     }
+
+    result[count] = NULL;
+    return result;
 }
-#endif
 
 SessionClient *
 client_session_new_full (gpointer data, SessionRestartStyle restart_style,
@@ -759,11 +717,11 @@ client_session_new_full (gpointer data, SessionRestartStyle restart_style,
     {
         session_client->current_directory = g_strdup (g_get_home_dir ());
     }
-    session_client->clone_command = clone_command;
-    session_client->resign_command = resign_command;
-    session_client->restart_command = restart_command;
-    session_client->discard_command = discard_command;
-    session_client->shutdown_command = shutdown_command;
+    session_client->clone_command = safe_strvdup (clone_command);
+    session_client->resign_command = safe_strvdup (resign_command);
+    session_client->restart_command = safe_strvdup (restart_command);
+    session_client->discard_command = safe_strvdup (discard_command);
+    session_client->shutdown_command = safe_strvdup (shutdown_command);
     session_client->shutdown = FALSE;
 
     session_client->save_phase_2 = NULL;
@@ -781,7 +739,7 @@ client_session_new (gint argc, gchar * argv[], gpointer data,
                     SessionRestartStyle restart_style, gchar priority)
 {
     SessionClient *session_client;
-    gchar **array;
+    gchar **array, **args;
     gchar *client_id = NULL;
     gboolean next_is_client_id = FALSE, had_display = FALSE;
     GdkDisplay *dpy = gdk_display_get_default();
@@ -795,39 +753,41 @@ client_session_new (gint argc, gchar * argv[], gpointer data,
     }
     else
     {
-        array = g_new (gchar *, argc + 3);
-        i = 0;
+        array = args = g_new (gchar *, argc + 3);
 
-        while (i < argc)
+        for (i = 0; i < argc; i++)
         {
-            array[i] = argv[i];
             if (next_is_client_id)
             {
                 client_id = argv[i];
                 next_is_client_id = FALSE;
             }
-            if (!g_ascii_strncasecmp (argv[i], SM_ID_ARG, strlen (SM_ID_ARG)))
+            else if (!g_ascii_strncasecmp (argv[i], SM_ID_ARG, strlen (SM_ID_ARG)))
             {
                 next_is_client_id = TRUE;
             }
-            else if (!g_ascii_strncasecmp (argv[i], DPY_ARG, strlen (DPY_ARG)))
+            else
             {
-                had_display = TRUE;
+                if (!g_ascii_strncasecmp (argv[i], DPY_ARG, strlen (DPY_ARG)))
+                {
+                    had_display = TRUE;
+                }
+                *args++ = argv[i];
             }
-            i++;
         }
 
         if ((!had_display) && (dpy != NULL))
         {
-            array[i++] = "--display";
-            array[i++] = (gchar *)gdk_display_get_name(dpy);
+            *args++ = DPY_ARG;
+            *args++ = (gchar *)gdk_display_get_name(dpy);
         }
 
-        array[i] = NULL;
+        *args = NULL;
     }
     session_client =
         client_session_new_full (data, restart_style, priority, client_id,
-                                 NULL, NULL, array, array, NULL, NULL, NULL);
+                                 NULL, NULL, array, NULL, NULL, NULL, NULL);
+    g_free (array);
 
     return session_client;
 }
@@ -837,7 +797,9 @@ void client_session_free(SessionClient * session_client)
     /* session_client->data; */
     /* session_client->session_connection; */
     
-    if (session_client->current_state != SESSION_CLIENT_DISCONNECTED) {
+    if (session_client->session_connection != NULL &&
+        session_client->current_state != SESSION_CLIENT_DISCONNECTED)
+    {
         disconnect(session_client);
     }
 
@@ -854,27 +816,6 @@ void client_session_free(SessionClient * session_client)
     g_free(session_client);
 }
 
-static gchar** safe_strvdup(gchar ** const source)
-{
-    int     count;
-    int     i;
-    gchar** result;
-    
-    if (source == NULL) {
-        return NULL;
-    }
-    
-    count = g_strv_length((gchar **)source);
-    result = g_new0(gchar*, count + 1);
-    
-    for (i = 0; i < count; ++i) {
-        result[i] = g_strdup(source[i]);
-    }
-
-    result[count] = NULL;
-    return result;
-}
-
 void client_session_set_save_phase_2_callback(SessionClient * session_client, save_phase_2_callback value)
 {
     session_client->save_phase_2 = value;
@@ -1053,84 +994,46 @@ void client_session_set_program(SessionClient * session_client, gchar const* val
 #endif /* HAVE_LIBSM */    
 }
 
-void client_session_set_clone_command(SessionClient * session_client, gchar** const value)
+static gchar **copy_command(gchar **command, gchar** const value)
 {
-    if (session_client->clone_command == value) {
-        return;
-    }
-    
-    if (session_client->clone_command != NULL) {
-        g_strfreev(session_client->clone_command);
-        session_client->clone_command = NULL;
-    }
+    if (command != value)
+    {
+        if (command != NULL) {
+            g_strfreev(command);
+            command = NULL;
+        }
 
-    if (value != NULL) {
-        session_client->clone_command = safe_strvdup(value);
+        if (value != NULL) {
+            command = safe_strvdup(value);
+        }
     }
+
+    return command;
+}
+    
+void client_session_set_clone_command(SessionClient * session_client, gchar** const value)
+{
+    session_client->clone_command = copy_command(session_client->clone_command, value);
 }
 
 void client_session_set_resign_command(SessionClient * session_client, gchar** const value)
 {
-    if (session_client->resign_command == value) {
-        return;
-    }
-    
-    if (session_client->resign_command != NULL) {
-        g_strfreev(session_client->resign_command);
-        session_client->resign_command = NULL;
-    }
-
-    if (value != NULL) {
-        session_client->resign_command = safe_strvdup(value);
-    }
+    session_client->resign_command = copy_command(session_client->resign_command, value);
 }
 
 void client_session_set_restart_command(SessionClient * session_client, gchar** const value)
 {
-    if (session_client->restart_command == value) {
-        return;
-    }
-    
-    if (session_client->restart_command != NULL) {
-        g_strfreev(session_client->restart_command);
-        session_client->restart_command = NULL;
-    }
-
-    if (value != NULL) {
-        session_client->restart_command = safe_strvdup(value);
-    }
+    session_client->restart_command = copy_command(session_client->restart_command, value);
 }
 
 void client_session_set_discard_command(SessionClient * session_client, gchar** const value)
 {
-    if (session_client->discard_command == value) {
-        return;
-    }
-    
-    if (session_client->discard_command != NULL) {
-        g_strfreev(session_client->discard_command);
-        session_client->discard_command = NULL;
-    }
-
-    if (value != NULL) {
-        session_client->discard_command = safe_strvdup(value);
-    }
+    session_client->discard_command = copy_command(session_client->discard_command, value);
 }
 
 void client_session_set_shutdown_command(SessionClient * session_client, gchar** const value)
 {
-    if (session_client->shutdown_command == value) {
-        return;
-    }
-    
-    if (session_client->shutdown_command != NULL) {
-        g_strfreev(session_client->shutdown_command);
-        session_client->shutdown_command = NULL;
-    }
-
-    if (value != NULL) {
-        session_client->shutdown_command = safe_strvdup(value);
-    }
+    session_client->shutdown_command = copy_command(session_client->shutdown_command, value);
 }
 
 save_phase_2_callback client_session_get_save_phase_2_callback(SessionClient * session_client)



More information about the Xfce4-commits mailing list