summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorRoss Lagerwall <rosslagerwall@gmail.com>2014-09-09 18:37:15 +0100
committerRoss Lagerwall <rosslagerwall@gmail.com>2014-10-13 22:13:29 +0100
commit33d1ba069ac55f8fcfe50f67197e09b1285d4350 (patch)
tree9e38fc742497b2a8eda7d2f40a31457a57107ece /common
parent7d3d832326f5114c517100dae6bafbadf5e6648f (diff)
downloadgvfs-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 'common')
-rw-r--r--common/Makefile.am14
-rw-r--r--common/gvfsgphoto2utils.c142
-rw-r--r--common/gvfsgphoto2utils.h30
3 files changed, 186 insertions, 0 deletions
diff --git a/common/Makefile.am b/common/Makefile.am
index 7f06cd4b..cfc1c230 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 00000000..5d8fe17e
--- /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 00000000..d8f9d405
--- /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