[Xfce4-commits] <xfce4-session:session-desktop-files> WIP

Brian J. Tarricone noreply at xfce.org
Fri Jan 29 07:08:10 CET 2010


Updating branch refs/heads/session-desktop-files
         to acad4963f56ca608e130e4c5461491dfc5b38185 (commit)
       from f1b1f61c9929594652a3cbd48afda9312d5fb5a3 (commit)

commit acad4963f56ca608e130e4c5461491dfc5b38185
Author: Brian J. Tarricone <brian at tarricone.org>
Date:   Mon Jul 20 18:36:40 2009 -0700

    WIP

 xfce4-session/xfsm-client.c     |  135 +++++++++++++++++++++++++++++++++++++++
 xfce4-session/xfsm-client.h     |    6 ++
 xfce4-session/xfsm-manager.c    |   73 +++++++++++++++------
 xfce4-session/xfsm-properties.c |   41 +++---------
 xfce4-session/xfsm-properties.h |    3 +-
 5 files changed, 202 insertions(+), 56 deletions(-)

diff --git a/xfce4-session/xfsm-client.c b/xfce4-session/xfsm-client.c
index 491d013..eb8d141 100644
--- a/xfce4-session/xfsm-client.c
+++ b/xfce4-session/xfsm-client.c
@@ -24,10 +24,18 @@
 #include <config.h>
 #endif
 
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #ifdef HAVE_STRING_H
 #include <string.h>
 #endif
 
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
 #include <dbus/dbus-glib.h>
 
 #include <libxfsm/xfsm-util.h>
@@ -48,6 +56,7 @@ struct _XfsmClient
 
   gchar           *id;
   gchar           *object_path;
+  gchar           *desktop_id;
 
   XfsmClientState  state;
   XfsmProperties  *properties;
@@ -162,6 +171,7 @@ xfsm_client_finalize (GObject *obj)
 
   g_free (client->id);
   g_free (client->object_path);
+  g_free (client->desktop_id);
 
   G_OBJECT_CLASS (xfsm_client_parent_class)->finalize (obj);
 }
@@ -387,7 +397,132 @@ xfsm_client_get_object_path (XfsmClient *client)
   return client->object_path;
 }
 
+G_CONST_RETURN gchar *
+xfsm_client_get_desktop_id (XfsmClient *client)
+{
+  g_return_val_if_fail (XFSM_IS_CLIENT (client), NULL);
+
+  if (!client->desktop_id)
+    {
+      client->desktop_id = g_strdup_printf("%s_%s",
+                                           xfsm_properties_get_string (client->properties,
+                                                                       SmProgram),
+                                           client->id);
+    }
+
+  return client->desktop_id;
+}
+
+static gboolean
+xfsm_client_quick_copy (const gchar *src_path,
+                        const gchar *dst_path)
+{
+  gboolean ret = FALSE;
+  gchar *file_contents = NULL;
+  gsize  len;
+
+  if (g_file_get_contents (src_path, &file_contents, &len, NULL))
+    if (g_file_set_contents (dst_path, file_contents, len, NULL))
+      ret = TRUE;
+
+  g_free (file_contents);
+
+  return ret;
+}
+
+gboolean
+xfsm_client_save (XfsmClient  *client,
+                  const gchar *session_name,
+                  GError     **error)
+{
+  gboolean ret = FALSE;
+  gchar *session_resource;
+  XfceRc *rcfile = NULL;
+  gchar session_group[1024];
+  const gchar *desktop_file;
+
+  g_return_val_if_fail (XFSM_IS_CLIENT (client) && (!error || !*error), FALSE);
 
+  session_resource = g_strdup_printf ("autostart/%s.desktop",
+                                      xfsm_client_get_desktop_id (client));
+  rcfile = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, session_resource, FALSE);
+
+  if (G_UNLIKELY (!rcfile))
+    {
+      if (error)
+        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
+                     _("Couldn't write to session data file for client \"%s\""),
+                     xfsm_client_get_desktop_id ());
+      goto out;
+    }
+
+  xfce_rc_set_group (rcfile, "Desktop Entry");
+
+  xfce_rc_write_entry (rcfile, "Version", "1.0");
+  xfce_rc_write_entry (rcfile, "Type", "Application");
+  xfce_rc_write_entry (rcfile, "OnlyShowIn", "XFCE;");
+  xfce_rc_write_bool_entry (rcfile, "NoDisplay", TRUE);
+  xfce_rc_write_bool_entry (rcfile, "Hidden", TRUE);
+
+  desktop_file = xfsm_properties_get_string (client->properties, GsmDesktopFile);
+  if (desktop_file)
+    {
+      XfceRc *rcfile_desktop = xfce_rc_simple_open (desktop_file, TRUE);
+      if (rcfile_desktop)
+        {
+          const gchar *val_str;
+
+          if ((val_str = xfce_rc_read_entry (rcfile_desktop, "Name", NULL)))
+            xfce_rc_write_entry (rcfile, "Name", val_str);
+          if ((val_str = xfce_rc_read_entry (rcfile_desktop, "Comment", NULL)))
+            xfce_rc_write_entry (rcfile, "Comment", val_str);
+          if ((val_str = xfce_rc_read_entry (rcfile_desktop, "Icon", NULL)))
+            xfce_rc_write_entry (rcfile, "Icon", val_str);
+          if ((val_str = xfce_rc_read_entry (rcfile_desktop, "Exec", NULL)))
+            xfce_rc_write_entry (rcfile, "Exec", val_str);
+          if (xfce_rc_has_entry (rcfile_desktop, "StartupNotify"))
+            {
+              xfce_rc_write_bool_entry (rcfile, "StartupNotify",
+                                        xfce_rc_read_bool_entry (rcfile_desktop,
+                                                                 "StartupNotify",
+                                                                 FALSE));
+            }
+
+          xfce_rc_close (rcfile_desktop);
+        }
+    }
+
+  xfce_rc_set_group (rcfile, "X-XfceSession");
+  xfce_rc_write_bool_entry (rcfile, "IsSessionManaged", TRUE);
+
+  if (!session_name)
+    session_name = "Default";
+  g_snprintf (session_group, sizeof (session_group), "X-XfceSession: %s",
+              session_name);
+
+  xfce_rc_delete_group (rcfile, session_group, TRUE);
+  xfce_rc_set_group (rcfile, session_group);
+  xfsm_properties_store (client->properties, rcfile);
+
+  if (rename (session_file_new, session_file))
+    {
+      if (error)
+        g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
+                     _("Unable to rename \"%s\" to \"%s\": %s"),
+                     session_file_new, session_file, strerror (errno));
+    }
+  else
+    ret = TRUE;
+
+out:
+
+  if (G_LIKELY (rcfile))
+    xfce_rc_close (rcfile);
+  g_free (session_file);
+  g_free (session_file_new);
+
+  return ret;
+}
 
 /*
  * dbus server impl
diff --git a/xfce4-session/xfsm-client.h b/xfce4-session/xfsm-client.h
index 39a258e..e8f5e27 100644
--- a/xfce4-session/xfsm-client.h
+++ b/xfce4-session/xfsm-client.h
@@ -77,6 +77,12 @@ void xfsm_client_delete_properties (XfsmClient *client,
 
 G_CONST_RETURN gchar *xfsm_client_get_object_path (XfsmClient *client);
 
+G_CONST_RETURN gchar *xfsm_client_get_desktop_id (XfsmClient *client);
+
+gboolean xfsm_client_save (XfsmClient  *client,
+                           const gchar *session_name,
+                           GError     **error);
+
 G_END_DECLS
 
 #endif /* !__XFSM_CLIENT_H__ */
diff --git a/xfce4-session/xfsm-manager.c b/xfce4-session/xfsm-manager.c
index 4f9e60e..fd86ac3 100644
--- a/xfce4-session/xfsm-manager.c
+++ b/xfce4-session/xfsm-manager.c
@@ -1610,6 +1610,11 @@ xfsm_manager_cancel_client_save_timeout (XfsmManager *manager,
 void
 xfsm_manager_store_session (XfsmManager *manager)
 {
+  const gchar   *session_name;
+  gchar         *autostart_dir;
+  DIR           *dir;
+  struct dirent *de;
+  gchar          path[PATH_MAX];
   WnckWorkspace *workspace;
   GdkDisplay    *display;
   WnckScreen    *screen;
@@ -1621,31 +1626,57 @@ xfsm_manager_store_session (XfsmManager *manager)
   gint           count = 0;
   gint           n, m;
 
-  rc = xfce_rc_simple_open (manager->session_file, FALSE);
-  if (G_UNLIKELY (rc == NULL))
+  autostart_dir = xfce_resource_save_location (XFCE_RESOURCE_CONFIG,
+                                               "autostart/",
+                                               TRUE);
+  if (G_UNLIKELY (!autostart_dir))
     {
-      fprintf (stderr,
-               "xfce4-session: Unable to open session file %s for "
-               "writing. Session data will not be stored. Please check "
-               "your installation.\n",
-               manager->session_file);
+      g_critical ("Unable to find/create autostart directory; session will " \
+                  "not be saved!");
       return;
     }
 
-  /* backup the old session file first */
-  if (g_file_test (manager->session_file, G_FILE_TEST_IS_REGULAR))
+  if (manager->state == XFSM_MANAGER_CHECKPOINT && manager->checkpoint_session_name != NULL)
+    session_name = manager->checkpoint_session_name;
+  else
+    session_name = manager->session_name;
+
+  dir = opendir (autostart_dir);
+  if (G_UNLIKELY (!dir))
     {
-      backup = g_strconcat (manager->session_file, ".bak", NULL);
-      unlink (backup);
-      if (link (manager->session_file, backup))
-          g_warning ("Failed to create session file backup");
-      g_free (backup);
+      g_critical ("Unable to open \"%s\": %s; session will not be saved!",
+                  autostart_dir, strerror (errno));
+      g_free (autostart_dir);
+      return;
     }
 
-  if (manager->state == XFSM_MANAGER_CHECKPOINT && manager->checkpoint_session_name != NULL)
-    group = g_strconcat ("Session: ", manager->checkpoint_session_name, NULL);
-  else
-    group = g_strconcat ("Session: ", manager->session_name, NULL);
+  while ((de = readdir (dir)))
+    {
+      if (G_UNLIKELY (!g_str_has_suffix (de->d_name, ".desktop")))
+        continue;
+
+      g_snprintf (path, sizeof (path), "%s%c%s", autostart_dir,
+                  G_DIR_SEPARATOR, de->d_name);
+      rc = xfce_rc_simple_open (path, FALSE);
+      if (G_UNLIKELY (rc))
+        {
+          g_warning ("Couldn't open \"%s\": %s", path, strerror (errno));
+          continue;
+        }
+
+      if (!xfce_rc_has_group (rc, "X-XfceSession"))
+        {
+          xfce_rc_close (rc);
+          continue;
+        }
+
+      xfce_rc_set_group (rc, "X-XfceSession");
+      if (!xfce_rc_read_bool_entry (rc, "IsSessionManaged", FALSE))
+        {
+          xfce_rc_close (rc);
+          continue;
+        }
+
   xfce_rc_delete_group (rc, group, TRUE);
   xfce_rc_set_group (rc, group);
   g_free (group);
@@ -1655,8 +1686,7 @@ xfsm_manager_store_session (XfsmManager *manager)
        lp = lp->next)
     {
       XfsmProperties *properties = lp->data;
-      g_snprintf (prefix, 64, "Client%d_", count);
-      xfsm_properties_store (properties, rc, prefix);
+      xfsm_properties_store (properties, rc);
       ++count;
     }
 
@@ -1676,8 +1706,7 @@ xfsm_manager_store_session (XfsmManager *manager)
       if (restart_style_hint == SmRestartNever)
         continue;
       
-      g_snprintf (prefix, 64, "Client%d_", count);
-      xfsm_properties_store (xfsm_client_get_properties (client), rc, prefix);
+      xfsm_properties_store (xfsm_client_get_properties (client), rc);
       ++count;
     }
 
diff --git a/xfce4-session/xfsm-properties.c b/xfce4-session/xfsm-properties.c
index 45243dd..a97d1e3 100644
--- a/xfce4-session/xfsm-properties.c
+++ b/xfce4-session/xfsm-properties.c
@@ -103,18 +103,6 @@ strdup (const char *s)
 #endif
 
 
-static gchar*
-compose (gchar       *buffer,
-         gsize        length,
-         const gchar *prefix,
-         const gchar *suffix)
-{
-  g_strlcpy (buffer, prefix, length);
-  g_strlcat (buffer, suffix, length);
-  return buffer;
-}
-
-
 static SmProp*
 strv_to_property (const gchar *name,
                   gchar      **argv)
@@ -249,8 +237,6 @@ XfsmProperties *
 xfsm_properties_load (XfceRc      *rc,
                       const gchar *prefix)
 {
-#define ENTRY(name) (compose(buffer, 256, prefix, (name)))
-
   XfsmProperties *properties;
   const gchar    *client_id;
   const gchar    *hostname;
@@ -258,7 +244,6 @@ xfsm_properties_load (XfceRc      *rc,
   const gchar    *value_str;
   gchar         **value_strv;
   gint            value_int;
-  gchar           buffer[256];
   gint            i;
 
   client_id = xfce_rc_read_entry (rc, ENTRY ("ClientId"), NULL);
@@ -283,7 +268,7 @@ xfsm_properties_load (XfceRc      *rc,
 
   for (i = 0; strv_properties[i].name; ++i)
     {
-      value_strv = xfce_rc_read_list_entry (rc, ENTRY (strv_properties[i].name), NULL);
+      value_strv = xfce_rc_read_list_entry (rc, strv_properties[i].name, NULL);
       if (value_strv)
         {
           xfsm_verbose ("-> Set strv (%s)\n", strv_properties[i].xsmp_name);
@@ -298,14 +283,14 @@ xfsm_properties_load (XfceRc      *rc,
 
   for (i = 0; str_properties[i].name; ++i)
     {
-      value_str = xfce_rc_read_entry (rc, ENTRY (str_properties[i].name), NULL);
+      value_str = xfce_rc_read_entry (rc, str_properties[i].name, NULL);
       if (value_str)
         xfsm_properties_set_string (properties, str_properties[i].xsmp_name, value_str);
     }
 
   for (i = 0; uchar_properties[i].name; ++i)
     {
-      value_int = xfce_rc_read_int_entry (rc, ENTRY (uchar_properties[i].name),
+      value_int = xfce_rc_read_int_entry (rc, uchar_properties[i].name,
                                           uchar_properties[i].default_value);
       xfsm_properties_set_uchar (properties, uchar_properties[i].xsmp_name, value_int);
     }
@@ -317,31 +302,25 @@ xfsm_properties_load (XfceRc      *rc,
     }
 
   return properties;
-  
-#undef ENTRY
 }
 
 
 void
 xfsm_properties_store (XfsmProperties *properties,
-                       XfceRc         *rc,
-                       const gchar    *prefix)
+                       XfceRc         *rc)
 {
-#define ENTRY(name) (compose(buffer, 256, prefix, (name)))
-
   GValue *value;
   gint    i;
-  gchar   buffer[256];
   
-  xfce_rc_write_entry (rc, ENTRY ("ClientId"), properties->client_id);
-  xfce_rc_write_entry (rc, ENTRY ("Hostname"), properties->hostname);
+  xfce_rc_write_entry (rc, "ClientId", properties->client_id);
+  xfce_rc_write_entry (rc, "Hostname", properties->hostname);
 
   for (i = 0; strv_properties[i].name; ++i)
     {
       value = g_tree_lookup (properties->sm_properties, strv_properties[i].xsmp_name);
       if (value)
         {
-          xfce_rc_write_list_entry (rc, ENTRY (strv_properties[i].name),
+          xfce_rc_write_list_entry (rc, strv_properties[i].name,
                                     g_value_get_boxed (value), NULL);
         }
     }
@@ -351,7 +330,7 @@ xfsm_properties_store (XfsmProperties *properties,
       value = g_tree_lookup (properties->sm_properties, str_properties[i].xsmp_name);
       if (value)
         {
-          xfce_rc_write_entry (rc, ENTRY (str_properties[i].name),
+          xfce_rc_write_entry (rc, str_properties[i].name,
                                g_value_get_string (value));
         }
     }
@@ -361,12 +340,10 @@ xfsm_properties_store (XfsmProperties *properties,
       value = g_tree_lookup (properties->sm_properties, uchar_properties[i].xsmp_name);
       if (value)
         {
-          xfce_rc_write_int_entry (rc, ENTRY (uchar_properties[i].name),
+          xfce_rc_write_int_entry (rc, uchar_properties[i].name,
                                    g_value_get_uchar (value));
         }
     }
-
-#undef ENTRY
 }
 
 
diff --git a/xfce4-session/xfsm-properties.h b/xfce4-session/xfsm-properties.h
index f3fd8d3..ff43e43 100644
--- a/xfce4-session/xfsm-properties.h
+++ b/xfce4-session/xfsm-properties.h
@@ -62,8 +62,7 @@ void            xfsm_properties_extract (XfsmProperties *properties,
                                          gint           *num_props,
                                          SmProp       ***props);
 void            xfsm_properties_store   (XfsmProperties *properties,
-                                         XfceRc         *rc,
-                                         const gchar    *prefix);
+                                         XfceRc         *rc);
 
 XfsmProperties* xfsm_properties_load (XfceRc *rc, const gchar *prefix);
 



More information about the Xfce4-commits mailing list