File: gvfsgphoto2utils.c

package info (click to toggle)
gvfs 1.30.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,580 kB
  • ctags: 12,211
  • sloc: ansic: 113,948; sh: 4,957; xml: 2,353; makefile: 1,601; python: 1,448; sed: 16
file content (154 lines) | stat: -rw-r--r-- 4,768 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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 ("phone");
  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 ("phone-symbolic");
  else
    return g_strdup ("camera-photo-symbolic");
}

char **
g_vfs_get_x_content_types (GUdevDevice *device)
{
  char *camera_x_content_types[] = {"x-content/image-dcf", NULL};
  char *media_player_x_content_types[] = {"x-content/audio-player", NULL};

  if (g_udev_device_has_property (device, "ID_MEDIA_PLAYER"))
    return g_strdupv (media_player_x_content_types);
  else
    return g_strdupv (camera_x_content_types);
}