[Telepathy-commits] [telepathy-mission-control/master] Rewrite the locating of client files.

Alberto Mardegan alberto.mardegan at nokia.com
Tue Feb 10 01:10:50 PST 2009


The previous code was leaking the GErrors.
---
 src/mcd-dispatcher.c |  117 +++++++++++++++++++++++++-------------------------
 1 files changed, 59 insertions(+), 58 deletions(-)

diff --git a/src/mcd-dispatcher.c b/src/mcd-dispatcher.c
index 1899655..48b9180 100644
--- a/src/mcd-dispatcher.c
+++ b/src/mcd-dispatcher.c
@@ -2158,100 +2158,101 @@ parse_client_file (McdClient *client, GKeyFile *file)
                                 "BypassApproval", NULL);
 }
 
-static McdClient *
-create_mcd_client (McdDispatcher *self,
-                   const gchar *name,
-                   gboolean activatable)
+static gchar *
+find_client_file (const gchar *client_name)
 {
-    /* McdDispatcherPrivate *priv = MCD_DISPATCHER_PRIV (self); */
     const gchar * const *dirs;
     const gchar *dirname;
     const gchar *env_dirname;
-    McdClient *client;
-    gchar *filename;
-    GKeyFile *file;
-    gboolean file_found = FALSE;
-
-    g_assert (strncmp (MC_FILE_IFACE_CLIENT ".", name,
-          sizeof (MC_FILE_IFACE_CLIENT ".") - 1) == 0);
-
-    client = g_slice_new0 (McdClient);
-    client->name = g_strdup (name + sizeof (MC_FILE_IFACE_CLIENT ".") - 1);
-    client->activatable = activatable;
-    g_debug ("McdClient created for %s", name);
-
-    filename = g_strdup_printf ("%s.client", client->name);
+    gchar *filename, *absolute_filepath;
 
-    /* The .client file is not mandatory as per the spec. However if it
-     * exists, it is better to read it than activating the service to read the
-     * D-Bus properties.
-     * 
+    /* 
      * The full path is $XDG_DATA_DIRS/telepathy/clients/clientname.client
      * or $XDG_DATA_HOME/telepathy/clients/clientname.client
      * For testing purposes, we also look for $MC_CLIENTS_DIR/clientname.client
      * if $MC_CLIENTS_DIR is set.
      */
-    file = g_key_file_new ();
-
+    filename = g_strdup_printf ("%s.client", client_name);
     env_dirname = g_getenv ("MC_CLIENTS_DIR");
     if (env_dirname)
     {
-        GError *error = NULL;
-        gchar *absolute_filepath;
         absolute_filepath = g_build_filename (env_dirname, filename, NULL);
-        g_key_file_load_from_file (file, absolute_filepath, 0, &error);
-
-        if (!error)
-        {
-            g_debug ("File found for %s: %s", name, absolute_filepath);
-            file_found = TRUE;
-        }
+        if (g_file_test (absolute_filepath, G_FILE_TEST_IS_REGULAR))
+            goto finish;
         g_free (absolute_filepath);
     }
 
     dirname = g_get_user_data_dir ();
-    if (!file_found && dirname)
+    if (G_LIKELY (dirname))
     {
-        GError *error = NULL;
-        gchar *absolute_filepath;
         absolute_filepath = g_build_filename (dirname, "telepathy/clients",
                                               filename, NULL);
-        g_key_file_load_from_file (file, absolute_filepath, 0, &error);
-
-        if (!error)
-        {
-            g_debug ("File found for %s: %s", name, absolute_filepath);
-            file_found = TRUE;
-        }
+        if (g_file_test (absolute_filepath, G_FILE_TEST_IS_REGULAR))
+            goto finish;
         g_free (absolute_filepath);
     }
 
     dirs = g_get_system_data_dirs ();
-    for (dirname = *dirs; dirname != NULL && !file_found;
-         dirs++, dirname = *dirs)
+    for (dirname = *dirs; dirname != NULL; dirs++, dirname = *dirs)
     {
-        GError *error = NULL;
-        gchar *absolute_filepath;
         absolute_filepath = g_build_filename (dirname, "telepathy/clients",
                                               filename, NULL);
-        g_key_file_load_from_file (file, absolute_filepath, 0, &error);
-
-        if (!error)
-        {
-            g_debug ("File found for %s: %s", name, absolute_filepath);
-            file_found = TRUE;
-        }
+        if (g_file_test (absolute_filepath, G_FILE_TEST_IS_REGULAR))
+            goto finish;
         g_free (absolute_filepath);
     }
 
+    absolute_filepath = NULL;
+finish:
     g_free (filename);
+    return absolute_filepath;
+}
 
-    if (file_found)
+static McdClient *
+create_mcd_client (McdDispatcher *self,
+                   const gchar *name,
+                   gboolean activatable)
+{
+    /* McdDispatcherPrivate *priv = MCD_DISPATCHER_PRIV (self); */
+    McdClient *client;
+    gchar *filename;
+    gboolean file_found = FALSE;
+
+    g_assert (strncmp (MC_FILE_IFACE_CLIENT ".", name,
+          sizeof (MC_FILE_IFACE_CLIENT ".") - 1) == 0);
+
+    client = g_slice_new0 (McdClient);
+    client->name = g_strdup (name + sizeof (MC_FILE_IFACE_CLIENT ".") - 1);
+    client->activatable = activatable;
+    g_debug ("McdClient created for %s", name);
+
+    /* The .client file is not mandatory as per the spec. However if it
+     * exists, it is better to read it than activating the service to read the
+     * D-Bus properties.
+     */
+    filename = find_client_file (client->name);
+    if (filename)
     {
-        parse_client_file (client, file);
+        GKeyFile *file;
+        GError *error = NULL;
+
+        file = g_key_file_new ();
+        g_key_file_load_from_file (file, filename, 0, &error);
+        if (G_LIKELY (!error))
+        {
+            g_debug ("File found for %s: %s", name, filename);
+            parse_client_file (client, file);
+            file_found = TRUE;
+        }
+        else
+        {
+            g_warning ("Loading file %s failed: %s", filename, error->message);
+            g_error_free (error);
+        }
+        g_key_file_free (file);
+        g_free (filename);
     }
 
-    g_key_file_free (file);
     create_client_proxy (self, client);
 
     if (!file_found)
-- 
1.5.6.5




More information about the telepathy-commits mailing list