[gvfs/mtp-backend: 23/64] MTP: Clean up formatting and style for MTP Volume Monitor.



commit 10504d057757943b4cf487d46bf3e191fe911039
Author: Philip Langdale <philipl overt org>
Date:   Sun Aug 19 14:09:44 2012 -0700

    MTP: Clean up formatting and style for MTP Volume Monitor.
    
    We can consider the volume monitor done at this point.

 monitor/mtp/gmtpvolume.c        |  124 ++++++++++++----------------
 monitor/mtp/gmtpvolume.h        |   14 ++--
 monitor/mtp/gmtpvolumemonitor.c |  170 ++++++++++++++++++--------------------
 3 files changed, 141 insertions(+), 167 deletions(-)
---
diff --git a/monitor/mtp/gmtpvolume.c b/monitor/mtp/gmtpvolume.c
index 0c9fbd3..cbec956 100644
--- a/monitor/mtp/gmtpvolume.c
+++ b/monitor/mtp/gmtpvolume.c
@@ -33,11 +33,6 @@
 
 #include "gmtpvolume.h"
 
-#ifndef HAVE_GUDEV
-#include "hal-utils.h"
-#endif
-
-/* Protects all fields of GHalDrive that can change */
 G_LOCK_DEFINE_STATIC(mtp_volume);
 
 struct _GMtpVolume {
@@ -99,9 +94,9 @@ g_mtp_volume_init (GMtpVolume *mtp_volume)
 static int hexdigit(char c)
 {
   if (c >= 'a')
-      return c - 'a' + 10;
+    return c - 'a' + 10;
   if (c >= 'A')
-     return c - 'A' + 10;
+   return c - 'A' + 10;
   g_return_val_if_fail (c >= '0' && c <= '9', 0);
   return c - '0';
 }
@@ -115,19 +110,17 @@ udev_decode_string (const char* encoded)
   const char* s;
 
   if (encoded == NULL)
-      return NULL;
-
-  for (len = 0, s = encoded; *s && len < sizeof(decoded)-1; ++len, ++s)
-    {
-      /* need to check for NUL terminator in advance */
-      if (s[0] == '\\' && s[1] == 'x' && s[2] >= '0' && s[3] >= '0')
-	{
-	  decoded[len] = (hexdigit(s[2]) << 4) | hexdigit(s[3]);
-	  s += 3;
-	}
-      else
-	  decoded[len] = *s;
+    return NULL;
+
+  for (len = 0, s = encoded; *s && len < sizeof(decoded)-1; ++len, ++s) {
+    /* need to check for NUL terminator in advance */
+    if (s[0] == '\\' && s[1] == 'x' && s[2] >= '0' && s[3] >= '0') {
+      decoded[len] = (hexdigit(s[2]) << 4) | hexdigit(s[3]);
+      s += 3;
+    } else {
+      decoded[len] = *s;
     }
+  }
   decoded[len] = '\0';
   return decoded;
 }
@@ -144,75 +137,64 @@ set_volume_name (GMtpVolume *v)
    * ID_{VENDOR,MODEL} */
 
   gphoto_name = g_udev_device_get_property (v->device, "ID_MTP");
-  if (gphoto_name != NULL && strcmp (gphoto_name, "1") != 0)
-    {
-      v->name = g_strdup (gphoto_name);
-      return;
-    }
+  if (gphoto_name != NULL && strcmp (gphoto_name, "1") != 0) {
+    v->name = g_strdup (gphoto_name);
+    return;
+  }
 
   vendor = g_udev_device_get_property (v->device, "ID_MEDIA_PLAYER_VENDOR");
   if (vendor == NULL)
-      vendor = g_udev_device_get_property (v->device, "ID_VENDOR_ENC");
+    vendor = g_udev_device_get_property (v->device, "ID_VENDOR_ENC");
   model = g_udev_device_get_property (v->device, "ID_MEDIA_PLAYER_MODEL");
-  if (model == NULL)
-    {
-      model = g_udev_device_get_property (v->device, "ID_MODEL_ENC");
-      product = g_udev_device_get_sysfs_attr (v->device, "product");
-    }
+  if (model == NULL) {
+    model = g_udev_device_get_property (v->device, "ID_MODEL_ENC");
+    product = g_udev_device_get_sysfs_attr (v->device, "product");
+  }
 
   v->name = NULL;
-  if (product != NULL && strlen (product) > 0)
+  if (product != NULL && strlen (product) > 0) {
     v->name = g_strdup (product);
-  else if (vendor == NULL)
-    {
-      if (model != NULL)
-        v->name = g_strdup (udev_decode_string (model));
-    }
-  else
-    {
-      if (model != NULL)
-	{
-	  /* we can't call udev_decode_string() twice in one g_strdup_printf(),
-	   * it returns a static buffer */
-	  gchar *temp = g_strdup_printf ("%s %s", vendor, model);
-          v->name = g_strdup (udev_decode_string (temp));
-	  g_free (temp);
-        }
-      else
-        {
-          if (g_udev_device_has_property (v->device, "ID_MEDIA_PLAYER"))
-            {
-              /* Translators: %s is the device vendor */
-              v->name = g_strdup_printf (_("%s Audio Player"), udev_decode_string (vendor));
-	    }
-	  else
-	    {
-	      /* Translators: %s is the device vendor */
-	      v->name = g_strdup_printf (_("%s Camera"), udev_decode_string (vendor));
-	    }
-        }
+  } else if (vendor == NULL) {
+    if (model != NULL)
+      v->name = g_strdup (udev_decode_string (model));
+  } else {
+    if (model != NULL) {
+      /* we can't call udev_decode_string() twice in one g_strdup_printf(),
+       * it returns a static buffer */
+      gchar *temp = g_strdup_printf ("%s %s", vendor, model);
+      v->name = g_strdup (udev_decode_string (temp));
+      g_free (temp);
+    } else {
+      if (g_udev_device_has_property (v->device, "ID_MEDIA_PLAYER")) {
+        /* Translators: %s is the device vendor */
+        v->name = g_strdup_printf (_("%s Audio Player"), udev_decode_string (vendor));
+      } else {
+        /* Translators: %s is the device vendor */
+        v->name = g_strdup_printf (_("%s Camera"), udev_decode_string (vendor));
+      }
     }
+  }
 
   if (v->name == NULL)
-      v->name = g_strdup (_("Camera"));
+    v->name = g_strdup (_("Camera"));
 }
 
 static void
 set_volume_icon (GMtpVolume *volume)
 {
   if (g_udev_device_has_property (volume->device, "ID_MEDIA_PLAYER_ICON_NAME"))
-      volume->icon = g_strdup (g_udev_device_get_property (volume->device, "ID_MEDIA_PLAYER_ICON_NAME"));
+    volume->icon = g_strdup (g_udev_device_get_property (volume->device, "ID_MEDIA_PLAYER_ICON_NAME"));
   else if (g_udev_device_has_property (volume->device, "ID_MEDIA_PLAYER"))
-      volume->icon = g_strdup ("multimedia-player");
+    volume->icon = g_strdup ("multimedia-player");
   else
-      volume->icon = g_strdup ("camera-photo");
+    volume->icon = g_strdup ("camera-photo");
 }
 
 GMtpVolume *
 g_mtp_volume_new (GVolumeMonitor   *volume_monitor,
-                      GUdevDevice      *device,
-                      GUdevClient      *gudev_client,
-                      GFile            *activation_root)
+                  GUdevDevice      *device,
+                  GUdevClient      *gudev_client,
+                  GFile            *activation_root)
 {
   GMtpVolume *volume;
   const char *device_path;
@@ -223,7 +205,7 @@ g_mtp_volume_new (GVolumeMonitor   *volume_monitor,
   g_return_val_if_fail (activation_root != NULL, NULL);
 
   if (!g_udev_device_has_property (device, "ID_MTP_DEVICE"))
-      return NULL;
+    return NULL;
   device_path = g_udev_device_get_device_file (device);
 
   volume = g_object_new (G_TYPE_MTP_VOLUME, NULL);
@@ -316,7 +298,7 @@ g_mtp_volume_has_path (GMtpVolume  *volume,
   G_LOCK (mtp_volume);
   res = FALSE;
   if (mtp_volume->device != NULL)
-    res = strcmp (g_udev_device_get_sysfs_path   (mtp_volume->device), sysfs_path) == 0;
+    res = strcmp (g_udev_device_get_sysfs_path (mtp_volume->device), sysfs_path) == 0;
   G_UNLOCK (mtp_volume);
   return res;
 }
@@ -387,15 +369,15 @@ g_mtp_volume_mount_finish (GVolume       *volume,
 }
 
 static char *
-g_mtp_volume_get_identifier (GVolume              *volume,
-                             const char          *kind)
+g_mtp_volume_get_identifier (GVolume    *volume,
+                             const char *kind)
 {
   GMtpVolume *mtp_volume = G_MTP_VOLUME (volume);
   char *id;
 
   G_LOCK (mtp_volume);
   id = NULL;
-    if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE) == 0)
+  if (strcmp (kind, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE) == 0)
     id = g_strdup (mtp_volume->device_path);
   G_UNLOCK (mtp_volume);
 
diff --git a/monitor/mtp/gmtpvolume.h b/monitor/mtp/gmtpvolume.h
index 304dd1b..a1d2e6b 100644
--- a/monitor/mtp/gmtpvolume.h
+++ b/monitor/mtp/gmtpvolume.h
@@ -44,15 +44,15 @@ struct _GMtpVolumeClass {
 
 GType g_mtp_volume_get_type (void) G_GNUC_CONST;
 
-GMtpVolume *g_mtp_volume_new      (GVolumeMonitor   *volume_monitor,
-                                   GUdevDevice      *device,
-                                   GUdevClient      *gudev_client,
-                                   GFile            *activation_root);
+GMtpVolume *g_mtp_volume_new      (GVolumeMonitor *volume_monitor,
+                                   GUdevDevice    *device,
+                                   GUdevClient    *gudev_client,
+                                   GFile          *activation_root);
 
-gboolean    g_mtp_volume_has_path (GMtpVolume       *volume,
-                                   const char       *path);
+gboolean    g_mtp_volume_has_path (GMtpVolume     *volume,
+                                   const char     *path);
 
-void        g_mtp_volume_removed  (GMtpVolume       *volume);
+void        g_mtp_volume_removed  (GMtpVolume     *volume);
 
 G_END_DECLS
 
diff --git a/monitor/mtp/gmtpvolumemonitor.c b/monitor/mtp/gmtpvolumemonitor.c
index 7a5916a..480e15c 100644
--- a/monitor/mtp/gmtpvolumemonitor.c
+++ b/monitor/mtp/gmtpvolumemonitor.c
@@ -36,7 +36,7 @@
 
 #include <gio/gunixmounts.h>
 
-G_LOCK_DEFINE_STATIC(hal_vm);
+G_LOCK_DEFINE_STATIC(vm_lock);
 
 static GMtpVolumeMonitor *the_volume_monitor = NULL;
 
@@ -52,10 +52,10 @@ struct _GMtpVolumeMonitor {
   GList *device_volumes;
 };
 
-static void on_uevent                (GUdevClient *client, 
-                                      gchar *action,
-                                      GUdevDevice *device,
-                                      gpointer user_data);
+static void on_uevent (GUdevClient *client, 
+                       gchar *action,
+                       GUdevDevice *device,
+                       gpointer user_data);
 
 G_DEFINE_TYPE (GMtpVolumeMonitor, g_mtp_volume_monitor, G_TYPE_VOLUME_MONITOR)
 
@@ -69,9 +69,9 @@ list_free (GList *objects)
 static void
 g_mtp_volume_monitor_dispose (GObject *object)
 {
-  G_LOCK (hal_vm);
+  G_LOCK (vm_lock);
   the_volume_monitor = NULL;
-  G_UNLOCK (hal_vm);
+  G_UNLOCK (vm_lock);
 
   if (G_OBJECT_CLASS (g_mtp_volume_monitor_parent_class)->dispose)
     (*G_OBJECT_CLASS (g_mtp_volume_monitor_parent_class)->dispose) (object);
@@ -109,12 +109,12 @@ get_volumes (GVolumeMonitor *volume_monitor)
 
   monitor = G_MTP_VOLUME_MONITOR (volume_monitor);
 
-  G_LOCK (hal_vm);
+  G_LOCK (vm_lock);
 
   l = g_list_copy (monitor->device_volumes);
   g_list_foreach (l, (GFunc)g_object_ref, NULL);
 
-  G_UNLOCK (hal_vm);
+  G_UNLOCK (vm_lock);
 
   return l;
 }
@@ -140,46 +140,45 @@ get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
 static void
 gudev_add_device (GMtpVolumeMonitor *monitor, GUdevDevice *device, gboolean do_emit)
 {
-    GMtpVolume *volume;
-    GList *store_heads, *l;
-    guint num_store_heads;
-    const char *usb_bus_num, *usb_device_num;
-
-    usb_bus_num = g_udev_device_get_property (device, "BUSNUM");
-    if (usb_bus_num == NULL) {
-	g_warning("device %s has no BUSNUM property, ignoring", g_udev_device_get_device_file (device));
-	return;
-    }
-
-    usb_device_num = g_udev_device_get_property (device, "DEVNUM");
-    if (usb_device_num == NULL) {
-	g_warning("device %s has no DEVNUM property, ignoring", g_udev_device_get_device_file (device));
-	return;
-    }
-
-    g_print ("gudev_add_device: device %s (bus: %i, device: %i)", 
-             g_udev_device_get_device_file (device),
-             usb_bus_num, usb_device_num);
-
-    gchar *uri = g_strdup_printf("mtp://[usb:%s,%s]", usb_bus_num, usb_device_num);
-    GFile *activation_mount_root;
-
-    activation_mount_root = g_file_new_for_uri (uri);
-    g_free (uri);
-
-    volume = g_mtp_volume_new (G_VOLUME_MONITOR (monitor),
-                               device, 
-                               monitor->gudev_client,
-                               activation_mount_root);
-        if (volume != NULL)
-          {
-            monitor->device_volumes = g_list_prepend (monitor->device_volumes, volume);
-            if (do_emit)
-                g_signal_emit_by_name (monitor, "volume_added", volume);
-          }
-
-        if (activation_mount_root != NULL)
-          g_object_unref (activation_mount_root);
+  GMtpVolume *volume;
+  const char *usb_bus_num, *usb_device_num;
+
+  usb_bus_num = g_udev_device_get_property (device, "BUSNUM");
+  if (usb_bus_num == NULL) {
+    g_warning ("device %s has no BUSNUM property, ignoring", g_udev_device_get_device_file (device));
+    return;
+  }
+
+  usb_device_num = g_udev_device_get_property (device, "DEVNUM");
+  if (usb_device_num == NULL) {
+    g_warning ("device %s has no DEVNUM property, ignoring", g_udev_device_get_device_file (device));
+    return;
+  }
+
+  /*
+  g_debug ("gudev_add_device: device %s (bus: %i, device: %i)",
+           g_udev_device_get_device_file (device),
+           usb_bus_num, usb_device_num);
+  */
+
+  gchar *uri = g_strdup_printf ("mtp://[usb:%s,%s]", usb_bus_num, usb_device_num);
+  GFile *activation_mount_root;
+
+  activation_mount_root = g_file_new_for_uri (uri);
+  g_free (uri);
+
+  volume = g_mtp_volume_new (G_VOLUME_MONITOR (monitor),
+                             device,
+                             monitor->gudev_client,
+                             activation_mount_root);
+  if (volume != NULL) {
+    monitor->device_volumes = g_list_prepend (monitor->device_volumes, volume);
+    if (do_emit)
+      g_signal_emit_by_name (monitor, "volume_added", volume);
+  }
+
+  if (activation_mount_root != NULL)
+    g_object_unref (activation_mount_root);
 }
 
 static void
@@ -192,29 +191,24 @@ gudev_remove_device (GMtpVolumeMonitor *monitor, GUdevDevice *device)
 
   /* g_debug ("gudev_remove_device: %s", g_udev_device_get_device_file (device)); */
 
-  for (l = monitor->device_volumes; l != NULL; l = ll)
-    {
-      GMtpVolume *volume = G_MTP_VOLUME (l->data);
-
-      ll = l->next;
-
-      if (g_mtp_volume_has_path (volume, sysfs_path))
-        {
-          /* g_debug ("gudev_remove_device: found volume %s, deleting", sysfs_path); */
-          g_signal_emit_by_name (monitor, "volume_removed", volume);
-          g_signal_emit_by_name (volume, "removed");
-          g_mtp_volume_removed (volume);
-          monitor->device_volumes = g_list_remove (monitor->device_volumes, volume);
-          g_object_unref (volume);
-        }
+  for (l = monitor->device_volumes; l != NULL; l = ll) {
+    GMtpVolume *volume = G_MTP_VOLUME (l->data);
+
+    ll = l->next;
+
+    if (g_mtp_volume_has_path (volume, sysfs_path)) {
+      /* g_debug ("gudev_remove_device: found volume %s, deleting", sysfs_path); */
+      g_signal_emit_by_name (monitor, "volume_removed", volume);
+      g_signal_emit_by_name (volume, "removed");
+      g_mtp_volume_removed (volume);
+      monitor->device_volumes = g_list_remove (monitor->device_volumes, volume);
+      g_object_unref (volume);
     }
+  }
 }
 
 static void
-on_uevent (GUdevClient *client, 
-           gchar *action,
-           GUdevDevice *device,
-           gpointer user_data)
+on_uevent (GUdevClient *client, gchar *action, GUdevDevice *device, gpointer user_data)
 {
   GMtpVolumeMonitor *monitor = G_MTP_VOLUME_MONITOR (user_data);
 
@@ -236,15 +230,14 @@ on_uevent (GUdevClient *client,
 static void
 gudev_coldplug_devices (GMtpVolumeMonitor *monitor)
 {
-    GList *usb_devices, *l;
-
-    usb_devices = g_udev_client_query_by_subsystem (monitor->gudev_client, "usb");
-    for (l = usb_devices; l != NULL; l = l->next)
-    {
-        GUdevDevice *d = l->data;
-        if (g_udev_device_has_property (d, "ID_MTP_DEVICE"))
-            gudev_add_device (monitor, d, FALSE);
-    }
+  GList *usb_devices, *l;
+
+  usb_devices = g_udev_client_query_by_subsystem (monitor->gudev_client, "usb");
+  for (l = usb_devices; l != NULL; l = l->next) {
+    GUdevDevice *d = l->data;
+    if (g_udev_device_has_property (d, "ID_MTP_DEVICE"))
+        gudev_add_device (monitor, d, FALSE);
+  }
 }
 
 static GObject *
@@ -257,16 +250,15 @@ g_mtp_volume_monitor_constructor (GType                  type,
   GMtpVolumeMonitorClass *klass;
   GObjectClass *parent_class;
 
-  G_LOCK (hal_vm);
-  if (the_volume_monitor != NULL)
-    {
-      object = g_object_ref (the_volume_monitor);
-      G_UNLOCK (hal_vm);
-      return object;
-    }
-  G_UNLOCK (hal_vm);
+  G_LOCK (vm_lock);
+  if (the_volume_monitor != NULL) {
+    object = g_object_ref (the_volume_monitor);
+    G_UNLOCK (vm_lock);
+    return object;
+  }
+  G_UNLOCK (vm_lock);
 
-  /*g_warning ("creating hal vm");*/
+  /*g_warning ("creating vm singleton");*/
 
   object = NULL;
 
@@ -279,7 +271,7 @@ g_mtp_volume_monitor_constructor (GType                  type,
 
   monitor = G_MTP_VOLUME_MONITOR (object);
 
-  const char *subsystems[] = {"usb", NULL};
+  const char *subsystems[] = { "usb", NULL };
   monitor->gudev_client = g_udev_client_new (subsystems);
 
   g_signal_connect (monitor->gudev_client, 
@@ -288,9 +280,9 @@ g_mtp_volume_monitor_constructor (GType                  type,
 
   gudev_coldplug_devices (monitor);
 
-  G_LOCK (hal_vm);
+  G_LOCK (vm_lock);
   the_volume_monitor = monitor;
-  G_UNLOCK (hal_vm);
+  G_UNLOCK (vm_lock);
 
   return object;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]