diff options
author | Ross Lagerwall <rosslagerwall@gmail.com> | 2014-09-09 18:37:15 +0100 |
---|---|---|
committer | Ross Lagerwall <rosslagerwall@gmail.com> | 2014-10-13 22:13:29 +0100 |
commit | 33d1ba069ac55f8fcfe50f67197e09b1285d4350 (patch) | |
tree | 9e38fc742497b2a8eda7d2f40a31457a57107ece /monitor | |
parent | 7d3d832326f5114c517100dae6bafbadf5e6648f (diff) | |
download | gvfs-33d1ba069ac55f8fcfe50f67197e09b1285d4350.tar.gz |
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
Diffstat (limited to 'monitor')
-rw-r--r-- | monitor/gphoto2/Makefile.am | 5 | ||||
-rw-r--r-- | monitor/gphoto2/ggphoto2volume.c | 123 | ||||
-rw-r--r-- | monitor/mtp/Makefile.am | 3 | ||||
-rw-r--r-- | monitor/mtp/gmtpvolume.c | 120 |
4 files changed, 15 insertions, 236 deletions
diff --git a/monitor/gphoto2/Makefile.am b/monitor/gphoto2/Makefile.am index b6712751..106b7811 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 25651ec3..6a147376 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 a8aba9a6..414968d4 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 c392b5cf..cfd77590 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; |