[gvfs] Build shared code for gphoto2 and mtp as a convenience lib



commit 33d1ba069ac55f8fcfe50f67197e09b1285d4350
Author: Ross Lagerwall <rosslagerwall gmail com>
Date:   Tue Sep 9 18:37:15 2014 +0100

    Build shared code for gphoto2 and mtp as a convenience lib
    
    Deduplicate code between gphoto2 and mtp by creating a convenience
    library call libgvfscommon-gphoto2 which is only built if either libmtp
    is used or gphoto2 with gudev is used.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=736285

 common/Makefile.am               |   14 ++++
 common/gvfsgphoto2utils.c        |  142 ++++++++++++++++++++++++++++++++++++++
 common/gvfsgphoto2utils.h        |   30 ++++++++
 configure.ac                     |    3 +
 monitor/gphoto2/Makefile.am      |    5 +-
 monitor/gphoto2/ggphoto2volume.c |  123 ++-------------------------------
 monitor/mtp/Makefile.am          |    3 +-
 monitor/mtp/gmtpvolume.c         |  120 +-------------------------------
 8 files changed, 204 insertions(+), 236 deletions(-)
---
diff --git a/common/Makefile.am b/common/Makefile.am
index 7f06cd4..cfc1c23 100644
--- a/common/Makefile.am
+++ b/common/Makefile.am
@@ -67,6 +67,20 @@ libgvfscommon_dnssd_la_LIBADD =      \
        $(GLIB_LIBS)
 endif
 
+if USE_GPHOTO2UTILS
+noinst_LTLIBRARIES += libgvfscommon-gphoto2.la
+
+libgvfscommon_gphoto2_la_SOURCES = \
+       gvfsgphoto2utils.c gvfsgphoto2utils.h \
+       $(NULL)
+
+libgvfscommon_gphoto2_la_CPPFLAGS = \
+       $(GUDEV_CFLAGS)
+
+libgvfscommon_gphoto2_la_LIBADD = \
+       $(GUDEV_LIBS)
+endif
+
 
 EXTRA_DIST = org.gtk.vfs.xml
 
diff --git a/common/gvfsgphoto2utils.c b/common/gvfsgphoto2utils.c
new file mode 100644
index 0000000..5d8fe17
--- /dev/null
+++ b/common/gvfsgphoto2utils.c
@@ -0,0 +1,142 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright (C) 2009 Martin Pitt <martin pitt ubuntu com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include "gvfsgphoto2utils.h"
+#include <string.h>
+#include <glib/gi18n-lib.h>
+
+static int hexdigit (char c)
+{
+  if (c >= 'a')
+    return c - 'a' + 10;
+  if (c >= 'A')
+   return c - 'A' + 10;
+  g_return_val_if_fail (c >= '0' && c <= '9', 0);
+  return c - '0';
+}
+
+/* Do not free result, it's a static buffer */
+static const char*
+udev_decode_string (const char* encoded)
+{
+  int len;
+  const char* s;
+  static char decoded[4096];
+
+  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 if (s[0] == '_' || s[0] == '-') {
+      decoded[len] = ' ';
+    } else {
+      decoded[len] = *s;
+    }
+  }
+  decoded[len] = '\0';
+
+  return decoded;
+}
+
+char *
+g_vfs_get_volume_name (GUdevDevice *device, const char *device_id)
+{
+  const char *gphoto_name;
+  const char *product = NULL;
+  const char *vendor;
+  const char *model;
+
+  /* our preference: device_id > ID_MEDIA_PLAYER_{VENDOR,PRODUCT} > product >
+   * ID_{VENDOR,MODEL} */
+
+  gphoto_name = g_udev_device_get_property (device, device_id);
+  if (gphoto_name != NULL && strcmp (gphoto_name, "1") != 0)
+    return g_strdup (gphoto_name);
+
+  vendor = g_udev_device_get_property (device, "ID_MEDIA_PLAYER_VENDOR");
+  if (vendor == NULL)
+    vendor = g_udev_device_get_property (device, "ID_VENDOR_ENC");
+  model = g_udev_device_get_property (device, "ID_MEDIA_PLAYER_MODEL");
+  if (model == NULL) {
+    model = g_udev_device_get_property (device, "ID_MODEL_ENC");
+    product = g_udev_device_get_sysfs_attr (device, "product");
+  }
+
+  if (product != NULL && strlen (product) > 0)
+    return g_strdup (udev_decode_string (product));
+  else if (vendor == NULL)
+    {
+      if (model != NULL)
+        return 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_strconcat (vendor, " ", model, NULL);
+          gchar *name = g_strdup (udev_decode_string (temp));
+          g_free (temp);
+          return name;
+        }
+      else
+        {
+          if (g_udev_device_has_property (device, "ID_MEDIA_PLAYER"))
+            {
+              /* Translators: %s is the device vendor */
+              return g_strdup_printf (_("%s Audio Player"), udev_decode_string (vendor));
+            }
+          else
+            {
+              /* Translators: %s is the device vendor */
+              return g_strdup_printf (_("%s Camera"), udev_decode_string (vendor));
+            }
+        }
+    }
+
+  return g_strdup (_("Camera"));
+}
+
+char *
+g_vfs_get_volume_icon (GUdevDevice *device)
+{
+  if (g_udev_device_has_property (device, "ID_MEDIA_PLAYER_ICON_NAME"))
+    return g_strdup (g_udev_device_get_property (device, "ID_MEDIA_PLAYER_ICON_NAME"));
+  else if (g_udev_device_has_property (device, "ID_MEDIA_PLAYER"))
+    return g_strdup ("multimedia-player");
+  else
+    return g_strdup ("camera-photo");
+}
+
+char *
+g_vfs_get_volume_symbolic_icon (GUdevDevice *device)
+{
+  if (g_udev_device_has_property (device, "ID_MEDIA_PLAYER_ICON_NAME"))
+    return g_strconcat (g_udev_device_get_property (device, "ID_MEDIA_PLAYER_ICON_NAME"), "-symbolic", NULL);
+  else if (g_udev_device_has_property (device, "ID_MEDIA_PLAYER"))
+    return g_strdup ("multimedia-player-symbolic");
+  else
+    return g_strdup ("camera-photo-symbolic");
+}
diff --git a/common/gvfsgphoto2utils.h b/common/gvfsgphoto2utils.h
new file mode 100644
index 0000000..d8f9d40
--- /dev/null
+++ b/common/gvfsgphoto2utils.h
@@ -0,0 +1,30 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright (C) 2009 Martin Pitt <martin pitt ubuntu com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __G_VFS_GPHOTO2_UTILS_H__
+#define __G_VFS_GPHOTO2_UTILS_H__
+
+#include <glib.h>
+#include <gudev/gudev.h>
+
+char * g_vfs_get_volume_name (GUdevDevice *device, const char *device_id);
+char * g_vfs_get_volume_icon (GUdevDevice *device);
+char * g_vfs_get_volume_symbolic_icon (GUdevDevice *device);
+
+#endif
diff --git a/configure.ac b/configure.ac
index 79ea3af..021bf9d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -557,6 +557,9 @@ AC_SUBST(LIBMTP_CFLAGS)
 AC_SUBST(LIBMTP_LIBS)
 AM_CONDITIONAL(USE_LIBMTP, [test "$msg_libmtp" = "yes"])
 
+AM_CONDITIONAL(USE_GPHOTO2UTILS,
+               [test "$msg_libmtp" = "yes" -o \( "$msg_gphoto2" = "yes" -a "$msg_gudev" = "yes" \)])
+
 dnl ==========================================================================
 dnl Samba 3.0
 
diff --git a/monitor/gphoto2/Makefile.am b/monitor/gphoto2/Makefile.am
index b671275..106b781 100644
--- a/monitor/gphoto2/Makefile.am
+++ b/monitor/gphoto2/Makefile.am
@@ -58,7 +58,10 @@ gvfs_gphoto2_volume_monitor_LDADD  =                                       \
        $(NULL)
 
 if USE_GUDEV
-gvfs_gphoto2_volume_monitor_LDADD += $(GUDEV_LIBS)
+gvfs_gphoto2_volume_monitor_LDADD +=                                          \
+       $(GUDEV_LIBS)                                                         \
+       $(top_builddir)/common/libgvfscommon-gphoto2.la                       \
+       $(NULL)
 else
 gvfs_gphoto2_volume_monitor_LDADD += $(HAL_LIBS)
 endif
diff --git a/monitor/gphoto2/ggphoto2volume.c b/monitor/gphoto2/ggphoto2volume.c
index 25651ec..6a14737 100644
--- a/monitor/gphoto2/ggphoto2volume.c
+++ b/monitor/gphoto2/ggphoto2volume.c
@@ -33,7 +33,9 @@
 
 #include "ggphoto2volume.h"
 
-#ifndef HAVE_GUDEV
+#ifdef HAVE_GUDEV
+#include "gvfsgphoto2utils.h"
+#else
 #include "hal-utils.h"
 #endif
 
@@ -101,120 +103,7 @@ g_gphoto2_volume_init (GGPhoto2Volume *gphoto2_volume)
 {
 }
 
-#ifdef HAVE_GUDEV
-static int hexdigit(char c)
-{
-  if (c >= 'a')
-      return c - 'a' + 10;
-  if (c >= 'A')
-     return c - 'A' + 10;
-  g_return_val_if_fail (c >= '0' && c <= '9', 0);
-  return c - '0';
-}
-
-/* Do not free result, it's a static buffer */
-static const char*
-udev_decode_string (const char* encoded)
-{
-  static char decoded[4096];
-  int len;
-  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;
-    }
-  decoded[len] = '\0';
-  return decoded;
-}
-
-static void
-set_volume_name (GGPhoto2Volume *v)
-{
-  const char *gphoto_name;
-  const char *product = NULL;
-  const char *vendor;
-  const char *model;
-
-  /* our preference: ID_GPHOTO2 > ID_MEDIA_PLAYER_{VENDOR,PRODUCT} > product >
-   * ID_{VENDOR,MODEL} */
-
-  gphoto_name = g_udev_device_get_property (v->device, "ID_GPHOTO2");
-  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");
-  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");
-    }
-
-  v->name = NULL;
-  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));
-           }
-        }
-    }
-
-  if (v->name == NULL)
-      v->name = g_strdup (_("Camera"));
-}
-
-static void
-set_volume_icon (GGPhoto2Volume *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"));
-  else if (g_udev_device_has_property (volume->device, "ID_MEDIA_PLAYER"))
-      volume->icon = g_strdup ("multimedia-player");
-  else
-      volume->icon = g_strdup ("camera-photo");
-}
-
-#else
+#ifndef HAVE_GUDEV
 static gboolean
 changed_in_idle (gpointer data)
 {
@@ -426,8 +315,8 @@ g_gphoto2_volume_new (GVolumeMonitor   *volume_monitor,
   volume->activation_root = g_object_ref (activation_root);
 
 #ifdef HAVE_GUDEV
-  set_volume_name (volume);
-  set_volume_icon (volume);
+  volume->name = g_vfs_get_volume_name (device, "ID_GPHOTO2");
+  volume->icon = g_vfs_get_volume_icon (device);
   /* we do not really need to listen for changes */
 #else
   g_signal_connect_object (device, "hal_property_changed", (GCallback) hal_changed, volume, 0);
diff --git a/monitor/mtp/Makefile.am b/monitor/mtp/Makefile.am
index a8aba9a..414968d 100644
--- a/monitor/mtp/Makefile.am
+++ b/monitor/mtp/Makefile.am
@@ -28,11 +28,10 @@ gvfs_mtp_volume_monitor_LDFLAGS =   \
 gvfs_mtp_volume_monitor_LDADD  =                                             \
        $(GLIB_LIBS)                                                          \
        $(top_builddir)/common/libgvfscommon.la                               \
+       $(top_builddir)/common/libgvfscommon-gphoto2.la                       \
        $(top_builddir)/monitor/proxy/libgvfsproxyvolumemonitordaemon-noin.la \
        $(NULL)
 
-gvfs_mtp_volume_monitor_LDADD += $(GUDEV_LIBS)
-
 
 remote_volume_monitorsdir = $(datadir)/gvfs/remote-volume-monitors
 remote_volume_monitors_DATA = mtp.monitor
diff --git a/monitor/mtp/gmtpvolume.c b/monitor/mtp/gmtpvolume.c
index c392b5c..cfd7759 100644
--- a/monitor/mtp/gmtpvolume.c
+++ b/monitor/mtp/gmtpvolume.c
@@ -31,6 +31,7 @@
 #include <gio/gio.h>
 
 #include "gmtpvolume.h"
+#include "gvfsgphoto2utils.h"
 
 G_LOCK_DEFINE_STATIC (mtp_volume);
 
@@ -87,119 +88,6 @@ g_mtp_volume_init (GMtpVolume *mtp_volume)
 {
 }
 
-static int hexdigit (char c)
-{
-  if (c >= 'a')
-    return c - 'a' + 10;
-  if (c >= 'A')
-   return c - 'A' + 10;
-  g_return_val_if_fail (c >= '0' && c <= '9', 0);
-  return c - '0';
-}
-
-/* Do not free result, it's a static buffer */
-static const char*
-udev_decode_string (const char* encoded)
-{
-  int len;
-  const char* s;
-  static char decoded[4096];
-
-  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 if (s[0] == '_' || s[0] == '-') {
-      decoded[len] = ' ';
-    } else {
-      decoded[len] = *s;
-    }
-  }
-  decoded[len] = '\0';
-
-  return decoded;
-}
-
-static void
-set_volume_name (GMtpVolume *v)
-{
-  const char *gphoto_name;
-  const char *product = NULL;
-  const char *vendor;
-  const char *model;
-
-  /* our preference: ID_MTP > ID_MEDIA_PLAYER_{VENDOR,PRODUCT} > product >
-   * 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;
-  }
-
-  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");
-  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");
-  }
-
-  v->name = NULL;
-  if (product != NULL && strlen (product) > 0) {
-    v->name = g_strdup (udev_decode_string (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_strconcat (vendor, " ", model, NULL);
-      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"));
-}
-
-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"));
-  else if (g_udev_device_has_property (volume->device, "ID_MEDIA_PLAYER"))
-    volume->icon = g_strdup ("multimedia-player");
-  else
-    volume->icon = g_strdup ("camera-photo");
-}
-
-static void
-set_volume_symbolic_icon (GMtpVolume *volume)
-{
-  if (g_udev_device_has_property (volume->device, "ID_MEDIA_PLAYER_ICON_NAME"))
-    volume->symbolic_icon = g_strconcat (g_udev_device_get_property (volume->device, 
"ID_MEDIA_PLAYER_ICON_NAME"), "-symbolic", NULL);
-  else if (g_udev_device_has_property (volume->device, "ID_MEDIA_PLAYER"))
-    volume->symbolic_icon = g_strdup ("multimedia-player-symbolic");
-  else
-    volume->symbolic_icon = g_strdup ("camera-photo-symbolic");
-}
-
 GMtpVolume *
 g_mtp_volume_new (GVolumeMonitor   *volume_monitor,
                   GUdevDevice      *device,
@@ -225,9 +113,9 @@ g_mtp_volume_new (GVolumeMonitor   *volume_monitor,
   volume->device = g_object_ref (device);
   volume->activation_root = g_object_ref (activation_root);
 
-  set_volume_name (volume);
-  set_volume_icon (volume);
-  set_volume_symbolic_icon (volume);
+  volume->name = g_vfs_get_volume_name (device, "ID_MTP");
+  volume->icon = g_vfs_get_volume_icon (device);
+  volume->symbolic_icon = g_vfs_get_volume_symbolic_icon (device);
   /* we do not really need to listen for changes */
 
   return volume;


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