As I was reading the devkit-client.c code this small came up.

Regards,
M.

-- 
Please note that according to the German law on data retention,
information on every electronic information exchange with me is
retained for a period of six months.
[Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]
diff --git a/src/devkit-gobject/devkit-client.c b/src/devkit-gobject/devkit-client.c
index 47daba9..79e97ef 100644
--- a/src/devkit-gobject/devkit-client.c
+++ b/src/devkit-gobject/devkit-client.c
@@ -80,6 +80,59 @@ _devkit_device_new (const char   *subsystem,
 
 /* ---------------------------------------------------------------------------------------------------- */
 
+#define STRUCT_TYPE (dbus_g_type_get_struct ("GValueArray",                                                    \
+                                             G_TYPE_STRING,                                                    \
+                                             G_TYPE_STRING,                                                    \
+                                             G_TYPE_STRING,                                                    \
+                                             G_TYPE_STRV,                                                      \
+                                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING), \
+                                             G_TYPE_INVALID))
+
+static GList*
+device_list_from_devices (const GPtrArray* devices)
+{
+        unsigned int n;
+        GList *ret = NULL;
+
+        for (n = 0; n < devices->len; n++) {
+                GValue elem = {0};
+                char *subsystem;
+                char *native_path;
+                char *device_file;
+                char **device_file_symlinks;
+                GHashTable *properties;
+                DevkitDevice *device;
+
+                g_value_init (&elem, STRUCT_TYPE);
+                g_value_set_static_boxed (&elem, devices->pdata[n]);
+
+                dbus_g_type_struct_get (&elem,
+                                        0, &subsystem,
+                                        1, &native_path,
+                                        2, &device_file,
+                                        3, &device_file_symlinks,
+                                        4, &properties,
+                                        G_MAXUINT);
+
+                device = _devkit_device_new (subsystem,
+                                             native_path,
+                                             device_file,
+                                             (const char **) device_file_symlinks,
+                                             properties);
+                ret = g_list_prepend (ret, device);
+
+                g_free (subsystem);
+                g_free (native_path);
+                g_free (device_file);
+                g_strfreev (device_file_symlinks);
+                g_hash_table_unref (properties);
+        }
+
+        return ret;
+}
+
+/* ---------------------------------------------------------------------------------------------------- */
+
 static void
 device_event_signal_handler (DBusGProxy *proxy,
                              const char *action,
@@ -395,14 +448,6 @@ out:
         return ret;
 }
 
-#define STRUCT_TYPE (dbus_g_type_get_struct ("GValueArray",                                                    \
-                                             G_TYPE_STRING,                                                    \
-                                             G_TYPE_STRING,                                                    \
-                                             G_TYPE_STRING,                                                    \
-                                             G_TYPE_STRV,                                                      \
-                                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING), \
-                                             G_TYPE_INVALID))
-
 /**
  * devkit_client_enumerate_by_subsystem:
  * @client: A #DevkitClient object.
@@ -420,7 +465,6 @@ devkit_client_enumerate_by_subsystem (DevkitClient  *client,
                                       const char   **subsystems,
                                       GError       **error)
 {
-        unsigned int n;
         GPtrArray *devices;
         GList *ret;
 
@@ -435,39 +479,8 @@ devkit_client_enumerate_by_subsystem (DevkitClient  *client,
                                                                error))
                 goto out;
 
-        for (n = 0; n < devices->len; n++) {
-                GValue elem = {0};
-                char *subsystem;
-                char *native_path;
-                char *device_file;
-                char **device_file_symlinks;
-                GHashTable *properties;
-                DevkitDevice *device;
-
-                g_value_init (&elem, STRUCT_TYPE);
-                g_value_set_static_boxed (&elem, devices->pdata[n]);
-
-                dbus_g_type_struct_get (&elem,
-                                        0, &subsystem,
-                                        1, &native_path,
-                                        2, &device_file,
-                                        3, &device_file_symlinks,
-                                        4, &properties,
-                                        G_MAXUINT);
-
-                device = _devkit_device_new (subsystem,
-                                             native_path,
-                                             device_file,
-                                             (const char **) device_file_symlinks,
-                                             properties);
-                ret = g_list_prepend (ret, device);
-
-                g_free (subsystem);
-                g_free (native_path);
-                g_free (device_file);
-                g_strfreev (device_file_symlinks);
-                g_hash_table_unref (properties);
-        }
+        ret = device_list_from_devices (devices);
+        g_ptr_array_free(devices, TRUE);
 
 out:
         ret = g_list_reverse (ret);
@@ -491,7 +504,6 @@ devkit_client_enumerate_by_native_path (DevkitClient  *client,
                                         const char   **native_paths,
                                         GError       **error)
 {
-        unsigned int n;
         GPtrArray *devices;
         GList *ret;
 
@@ -506,39 +518,8 @@ devkit_client_enumerate_by_native_path (DevkitClient  *client,
                                                                  error))
                 goto out;
 
-        for (n = 0; n < devices->len; n++) {
-                GValue elem = {0};
-                char *subsystem;
-                char *native_path;
-                char *device_file;
-                char **device_file_symlinks;
-                GHashTable *properties;
-                DevkitDevice *device;
-
-                g_value_init (&elem, STRUCT_TYPE);
-                g_value_set_static_boxed (&elem, devices->pdata[n]);
-
-                dbus_g_type_struct_get (&elem,
-                                        0, &subsystem,
-                                        1, &native_path,
-                                        2, &device_file,
-                                        3, &device_file_symlinks,
-                                        4, &properties,
-                                        G_MAXUINT);
-
-                device = _devkit_device_new (subsystem,
-                                             native_path,
-                                             device_file,
-                                             (const char **) device_file_symlinks,
-                                             properties);
-                ret = g_list_prepend (ret, device);
-
-                g_free (subsystem);
-                g_free (native_path);
-                g_free (device_file);
-                g_strfreev (device_file_symlinks);
-                g_hash_table_unref (properties);
-        }
+        ret = device_list_from_devices (devices);
+        g_ptr_array_free(devices, TRUE);
 
 out:
         ret = g_list_reverse (ret);
_______________________________________________
devkit-devel mailing list
devkit-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/devkit-devel

Reply via email to