[Xfce4-commits] r29342 - xfce4-settings/trunk/xfce4-settings-helper

Nick Schermer nick at xfce.org
Sun Jan 25 15:04:01 CET 2009


Author: nick
Date: 2009-01-25 14:04:00 +0000 (Sun, 25 Jan 2009)
New Revision: 29342

Modified:
   xfce4-settings/trunk/xfce4-settings-helper/workspaces.c
Log:
Set the workspace names through wnck, merge code and fix bug #4853.


Modified: xfce4-settings/trunk/xfce4-settings-helper/workspaces.c
===================================================================
--- xfce4-settings/trunk/xfce4-settings-helper/workspaces.c	2009-01-25 12:44:38 UTC (rev 29341)
+++ xfce4-settings/trunk/xfce4-settings-helper/workspaces.c	2009-01-25 14:04:00 UTC (rev 29342)
@@ -38,7 +38,8 @@
 {
     GObject parent;
 
-    WnckScreen **screens;
+    WnckScreen *screen;
+
     XfconfChannel *channel;
 };
 
@@ -50,10 +51,7 @@
 static void xfce_workspaces_helper_class_init(XfceWorkspacesHelperClass *klass);
 static void xfce_workspaces_helper_init(XfceWorkspacesHelper *helper);
 static void xfce_workspaces_helper_finalize(GObject *obj);
-
-static void xfce_workspaces_helper_set_names_prop(XfceWorkspacesHelper *helper,
-                                                  GdkScreen *screen,
-                                                  gchar **names);
+static void xfce_workspaces_helper_set_workspace_names(XfceWorkspacesHelper *helper);
 static void xfce_workspaces_helper_update_all_names(XfceWorkspacesHelper *helper);
 static void xfce_workspaces_helper_prop_changed(XfconfChannel *channel,
                                                 const gchar *property,
@@ -75,55 +73,18 @@
 static void
 xfce_workspaces_helper_init(XfceWorkspacesHelper *helper)
 {
-    gint w, s, n_screens, n_workspaces;
-    gchar **names;
-    GdkDisplay *display;
-    GdkScreen *screen;
-
     helper->channel = xfconf_channel_new(WORKSPACES_CHANNEL);
-    names = xfconf_channel_get_string_list(helper->channel,
-                                           WORKSPACE_NAMES_PROP);
 
-    /* FIXME: need to do this for all screens? */
-    display = gdk_display_get_default();
-    n_screens = gdk_display_get_n_screens(display);
-    helper->screens = g_new0(WnckScreen *, n_screens + 1);
-    for(s = 0; s < n_screens; ++s) {
-        helper->screens[s] = wnck_screen_get(s);
-        wnck_screen_force_update(helper->screens[s]);
+    xfce_workspaces_helper_set_workspace_names(helper);
 
-        if(s == 0) {
-            /* here we assume that all screens will always have the same
-             * number of workspaces, and any changes to this number will
-             * occur at the same time.  this may not be a great assumption,
-             * but it'll do. */
-            g_signal_connect_swapped(G_OBJECT(helper->screens[s]), "workspace-created",
-                                     G_CALLBACK(xfce_workspaces_helper_update_all_names),
-                                     helper);
-            g_signal_connect_swapped(G_OBJECT(helper->screens[s]), "workspace-destroyed",
-                                     G_CALLBACK(xfce_workspaces_helper_update_all_names),
-                                     helper);
-        }
+    helper->screen = wnck_screen_get_default();
+    g_signal_connect_swapped(G_OBJECT(helper->screen), "workspace-created",
+                             G_CALLBACK(xfce_workspaces_helper_update_all_names),
+                             helper);
+    g_signal_connect_swapped(G_OBJECT(helper->screen), "workspace-destroyed",
+                             G_CALLBACK(xfce_workspaces_helper_update_all_names),
+                             helper);
 
-        n_workspaces = wnck_screen_get_workspace_count(helper->screens[s]);
-        if(G_UNLIKELY(!names))
-            names = g_new0(gchar *, n_workspaces + 1);
-        else if(g_strv_length(names) < n_workspaces)
-            names = g_renew(gchar *, names, n_workspaces + 1);
-
-        for(w = g_strv_length(names); w < n_workspaces; ++w) {
-            /* some of them may not have been set in xfconf */
-            names[w] = g_strdup_printf(_("Workspace %d"), w + 1);
-        }
-        names[w] = NULL;
-
-        screen = gdk_display_get_screen(display, s);
-        xfce_workspaces_helper_set_names_prop(helper, screen, names);
-
-    }
-
-    g_strfreev(names);
-
     g_signal_connect(G_OBJECT(helper->channel),
                      "property-changed::" WORKSPACE_NAMES_PROP,
                      G_CALLBACK(xfce_workspaces_helper_prop_changed), helper);
@@ -134,59 +95,82 @@
 {
     XfceWorkspacesHelper *helper = XFCE_WORKSPACES_HELPER(obj);
 
-    g_signal_handlers_disconnect_by_func(G_OBJECT(helper->channel),
-                                         G_CALLBACK(xfce_workspaces_helper_prop_changed),
-                                         helper);
-    g_object_unref(G_OBJECT(helper->channel));
-
-    g_signal_handlers_disconnect_by_func(G_OBJECT(helper->screens[0]),
+    g_signal_handlers_disconnect_by_func(G_OBJECT(helper->screen),
                                          G_CALLBACK(xfce_workspaces_helper_update_all_names),
                                          helper);
-    g_free(helper->screens);
 
+    g_object_unref(G_OBJECT(helper->channel));
+
     G_OBJECT_CLASS(xfce_workspaces_helper_parent_class)->finalize(obj);
 }
 
-
-
 static void
-xfce_workspaces_helper_set_names_prop(XfceWorkspacesHelper *helper,
-                                      GdkScreen *screen,
-                                      gchar **names)
+xfce_workspaces_helper_set_workspace_names(XfceWorkspacesHelper *helper)
 {
-    GString *names_str;
-    gint i;
+    WnckWorkspace *workspace;
+    WnckScreen *screen;
+    GList *li, *workspaces;
+    guint i, n_names;
+    gchar **names;
+    gint n_screens, n;
+    const gchar *name, *old_name;
+    gchar *tmp_name;
+    
+    /* get the workspace names */
+    names = xfconf_channel_get_string_list(helper->channel, WORKSPACE_NAMES_PROP);
+    n_names = names ? g_strv_length(names) : 0;
 
-    names_str = g_string_new(NULL);
+    /* walk all the displays on this screen */
+    /* FIXME? Is this really needed? */
+    n_screens = gdk_display_get_n_screens(gdk_display_get_default());
+    for(n = 0; n < n_screens; n++) {
+        /* get the wnck screen and force an update */
+        screen = wnck_screen_get(n);
+        wnck_screen_force_update(screen);
 
-    for(i = 0; names[i]; ++i)
-        g_string_append_len(names_str, names[i], strlen(names[i]) + 1);
+        /* walk all the workspaces on this screen */
+        workspaces = wnck_screen_get_workspaces(screen);
+        for(li = workspaces, i = 0; li != NULL; li = li->next, i++) {
+            workspace = WNCK_WORKSPACE(li->data);
 
-    gdk_error_trap_push();
+            /* check if we have a valid name in the array */
+            if(n_names > i && names[i] != NULL && names[i] != '\0') {
+                name = names[i];
+                tmp_name = NULL;
+            } else {
+                tmp_name = g_strdup_printf(_("Workspace %d"), i + 1);
+                name = tmp_name;
+            }
 
-    gdk_property_change(gdk_screen_get_root_window(screen),
-                        gdk_atom_intern_static_string("_NET_DESKTOP_NAMES"),
-                        gdk_atom_intern_static_string("UTF8_STRING"),
-                        8, GDK_PROP_MODE_REPLACE,
-                        (guchar *)names_str->str, names_str->len + 1);
-    gdk_flush();
+            /* update the workspace name if it has changed */
+            old_name = wnck_workspace_get_name(workspace);
+            if(old_name == NULL || strcmp(old_name, name) != 0)
+                wnck_workspace_change_name(workspace, name);
 
-    gdk_error_trap_pop();
-
-    g_string_free(names_str, TRUE);
+            /* cleanup */
+            g_free(tmp_name);
+        }
+    }
+    
+    /* cleanup */
+    g_strfreev(names);
 }
 
 static void
 xfce_workspaces_helper_update_all_names(XfceWorkspacesHelper *helper)
 {
-    gint i, n_workspaces = wnck_screen_get_workspace_count(helper->screens[0]);
+    gint i, n_workspaces;
     gchar const **names;
+    WnckWorkspace *workspace;
 
+    g_return_if_fail (XFCE_IS_WORKSPACES_HELPER (helper));
+
+    n_workspaces = wnck_screen_get_workspace_count(helper->screen);
     names = g_new0(gchar const *, n_workspaces + 1);
 
     for(i = 0; i < n_workspaces; ++i) {
-        WnckWorkspace *space = wnck_screen_get_workspace(helper->screens[0], i);
-        names[i] = wnck_workspace_get_name(space);
+        workspace = wnck_screen_get_workspace(helper->screen, i);
+        names[i] = wnck_workspace_get_name(workspace);
     }
 
     xfconf_channel_set_string_list(helper->channel, WORKSPACE_NAMES_PROP,
@@ -202,49 +186,6 @@
                                     const GValue *value,
                                     gpointer user_data)
 {
-    XfceWorkspacesHelper *helper = user_data;
-    GPtrArray *names_arr;
-    gint s, i, n_workspaces;
-    gchar **names;
-    GdkDisplay *display;
-    GdkScreen *screen;
-
-    if(G_VALUE_TYPE(value) == G_TYPE_INVALID)
-        return;
-
-    if(G_VALUE_TYPE(value) != dbus_g_type_get_collection("GPtrArray",
-                                                         G_TYPE_VALUE))
-    {
-        g_warning("(workspace names) Expected boxed GPtrArray property, got %s",
-                  G_VALUE_TYPE_NAME(value));
-        return;
-    }
-
-    names_arr = g_value_get_boxed(value);
-    if(!names_arr)
-        return;
-
-    display = gdk_display_get_default();
-
-    for(s = 0; helper->screens[s]; ++s) {
-        wnck_screen_force_update(helper->screens[s]);
-        n_workspaces = wnck_screen_get_workspace_count(helper->screens[s]);
-        if(n_workspaces > names_arr->len)
-            names = g_new0(gchar *, n_workspaces + 1);
-        else {
-            names = g_new0(gchar *, names_arr->len + 1);
-            n_workspaces = names_arr->len;
-        }
-
-        for(i = 0; i < n_workspaces; ++i) {
-            if(i < names_arr->len && G_VALUE_HOLDS_STRING(g_ptr_array_index(names_arr, i)))
-                names[i] = g_value_dup_string(g_ptr_array_index(names_arr, i));
-            else
-                names[i] = g_strdup_printf(_("Workspace %d"), i + 1);
-        }
-
-        screen = gdk_display_get_screen(display, s);
-        xfce_workspaces_helper_set_names_prop(helper, screen, names);
-        g_strfreev(names);
-    }
+    g_return_if_fail (XFCE_IS_WORKSPACES_HELPER (user_data));
+    xfce_workspaces_helper_set_workspace_names(XFCE_WORKSPACES_HELPER(user_data));
 }




More information about the Xfce4-commits mailing list