~ubuntu-branches/ubuntu/trusty/thunar-volman/trusty-proposed

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* $Id: tvm-device.c 2364 2007-01-15 17:57:27Z benny $ */
/*-
 * Copyright (c) 2007 Benedikt Meurer <benny@xfce.org>.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <dbus/dbus-glib-lowlevel.h>

#include <thunar-volman/tvm-block-device.h>
#include <thunar-volman/tvm-camera-device.h>
#include <thunar-volman/tvm-device.h>
#include <thunar-volman/tvm-input-device.h>
#include <thunar-volman/tvm-pda-device.h>
#include <thunar-volman/tvm-printer-device.h>



typedef struct _TvmDeviceHandler TvmDeviceHandler;
struct _TvmDeviceHandler
{
  const gchar      *capability;
  TvmDeviceCallback callback;
};



static const TvmDeviceHandler handlers[] =
{
  { "block",          tvm_block_device_added,   },
  { "camera",         tvm_camera_device_added,  },
  { "input.keyboard", tvm_input_device_added,   },
  { "input.mouse",    tvm_input_device_added,   },
  { "input.tablet",   tvm_input_device_added,   },
  { "pda",            tvm_pda_device_added,     },
  { "printer",        tvm_printer_device_added, },
};



static gint
strptrcmp (gconstpointer strptr1,
           gconstpointer strptr2)
{
	return strcmp (*((const gchar **) strptr1),
                 *((const gchar **) strptr2));
}



/**
 * tvm_device_added:
 * @preferences : a #TvmPreferences.
 * @udi         : the HAL device UDI of the newly added device.
 * @error       : return location for errors or %NULL.
 *
 * Invoked whenever a new device is added, where @udi is the
 * HAL device UDI of the device in question. Returns %FALSE if
 * an unrecoverable error occurred, %TRUE otherwise.
 *
 * Return value: %FALSE in case of an unrecoverable error,
 *               %TRUE otherwise.
 **/
gboolean
tvm_device_added (TvmPreferences *preferences,
                  const gchar    *udi,
                  GError        **error)
{
  DBusConnection *connection;
  LibHalContext  *context;
  DBusError       derror;
  GError         *err = NULL;
  gchar         **capabilities;
  gint            n_capabilities;
  gint            i, j, n;

  g_return_val_if_fail (exo_hal_udi_validate (udi, -1, NULL), FALSE);
  g_return_val_if_fail (TVM_IS_PREFERENCES (preferences), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  /* try to allocate a new HAL context */
  context = libhal_ctx_new ();
  if (G_UNLIKELY (context == NULL))
    {
      /* out of memory */
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOMEM, g_strerror (ENOMEM));
      return FALSE;
    }

  /* initialize D-Bus error */
  dbus_error_init (&derror);

  /* try to connect to the system bus */
  connection = dbus_bus_get (DBUS_BUS_SYSTEM, &derror);
  if (G_UNLIKELY (connection == NULL))
    {
err0: /* release the HAL context */
      libhal_ctx_free (context);

      /* propagate the error */
      dbus_set_g_error (error, &derror);
      return FALSE;
    }

  /* setup the D-Bus connection for the GLib main loop */
  dbus_connection_setup_with_g_main (connection, NULL);

  /* setup the D-Bus connection for the HAL context */
  libhal_ctx_set_dbus_connection (context, connection);

  /* the HAL context now owns the connection */
  dbus_connection_unref (connection);

  /* try to initialize the HAL context */
  if (!libhal_ctx_init (context, &derror))
    goto err0;

  /* query the capabilities of the device */
  capabilities = libhal_device_get_property_strlist (context, udi, "info.capabilities", &derror);
  if (G_UNLIKELY (capabilities == NULL))
    {
      /* shutdown the HAL context */
      libhal_ctx_shutdown (context, NULL);
      goto err0;
    }

  /* determine the number of capabilities */
  n_capabilities = g_strv_length (capabilities);

  /* sort the capabilities */
  qsort (capabilities, n_capabilities, sizeof (*capabilities), strptrcmp);

  /* try various handlers until one of them succeeds */
  for (i = 0, j = 0; err == NULL && i < G_N_ELEMENTS (handlers) && j < n_capabilities; ++i)
    {
      /* search for a handler with the capability */
      for (n = -1; j < n_capabilities; )
        {
          /* check if we have a match here */
          n = strcmp (capabilities[j], handlers[i].capability);
          if (G_LIKELY (n >= 0))
            break;
          ++j;
        }

      /* check if we have a potential match */
      if (n == 0)
        {
          /* try to handle the device */
          if ((*handlers[i].callback) (preferences, context, udi, capabilities[j], &err))
            break;
          ++j;
        }
    }

  /* cleanup the capabilities */
  libhal_free_string_array (capabilities);

  /* shutdown the HAL context */
  libhal_ctx_shutdown (context, NULL);
  libhal_ctx_free (context);

  /* check if we failed */
  if (G_UNLIKELY (err != NULL))
    {
      /* propagate the error */
      g_propagate_error (error, err);
      return FALSE;
    }

  return TRUE;
}