summaryrefslogtreecommitdiff
path: root/src/e_mod_udisks_eldbus.c
blob: e448361ffceb36fb85f0021bee21b9263c2cdc0f (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_ELDBUS

#include <e.h>
#include <Eldbus.h>
#include "e_mod_main.h"
#include "e_mod_places.h"


/* Local Function Prototypes */
static void _places_udisks_name_start(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending);
static void _places_udisks_enumerate_devices_cb(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending);
static void _places_udisks_device_add_cb(void *data, const Eldbus_Message *msg);
static void _places_udisks_device_del_cb(void *data, const Eldbus_Message *msg);
static void _places_udisks_device_changed_cb(void *data, const Eldbus_Message *msg);
static void _places_udisks_vol_props_cb(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending);

static Volume* _places_udisks_volume_add(const char *devpath, Eina_Bool first_time);
static void _places_udisks_mount_func(Volume *vol, Eina_List *opts);
static void _places_udisks_unmount_func(Volume *vol, Eina_List *opts);
static void _places_udisks_eject_func(Volume *vol, Eina_List *opts);
static void _places_udisks_free_func(Volume *vol);


/* Local Variables */
#define UDISKS_BUS "org.freedesktop.UDisks"
#define UDISKS_PATH "/org/freedesktop/UDisks"
#define UDISKS_IFACE "org.freedesktop.UDisks"
#define UDISKS_DEVICE_IFACE "org.freedesktop.UDisks.Device"

static Eldbus_Connection *_places_dbus_conn = NULL;
static Eldbus_Proxy *_places_udisks_proxy = NULL;


/* API */
Eina_Bool
places_udisks_eldbus_init(void)
{
   printf("PLACES: udisks2: init()\n");

   if (!eldbus_init())
      return EINA_FALSE;

   _places_dbus_conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SYSTEM);
   if (!_places_dbus_conn)
   {
      printf("PLACES: udisks2: Error connecting to system bus. Is it running?\n");
      return EINA_FALSE;
   }

   eldbus_name_start(_places_dbus_conn, UDISKS_BUS, 0,
                    _places_udisks_name_start, NULL);

   return EINA_TRUE;
}

void
places_udisks_eldbus_shutdown(void)
{
   if (_places_udisks_proxy) eldbus_proxy_unref(_places_udisks_proxy);
   if (_places_dbus_conn) eldbus_connection_unref(_places_dbus_conn);
   eldbus_shutdown();
}

/* Implementation */
static void
_places_udisks_name_start(void *data, const Eldbus_Message *msg,
                          Eldbus_Pending *pending)
{
   Eldbus_Object *obj;
   unsigned flag;

   EINA_SAFETY_ON_FALSE_RETURN(eldbus_message_arguments_get(msg, "u", &flag));

   obj = eldbus_object_get(_places_dbus_conn, UDISKS_BUS, UDISKS_PATH);
   _places_udisks_proxy = eldbus_proxy_get(obj, UDISKS_IFACE);

   eldbus_proxy_call(_places_udisks_proxy, "EnumerateDevices",
                     _places_udisks_enumerate_devices_cb, NULL, -1, "");

   eldbus_proxy_signal_handler_add(_places_udisks_proxy, "DeviceAdded",
                                   _places_udisks_device_add_cb, NULL);
   eldbus_proxy_signal_handler_add(_places_udisks_proxy, "DeviceRemoved",
                                   _places_udisks_device_del_cb, NULL);
}

static Volume*
_places_udisks_volume_add(const char *devpath, Eina_Bool first_time)
{
   Eldbus_Object *obj;
   Eldbus_Proxy *proxy;
   Volume *vol;

   // create the Eldbus object and proxy
   obj = eldbus_object_get(_places_dbus_conn, UDISKS_BUS, devpath);
   if (!obj) return NULL;
   proxy = eldbus_proxy_get(obj, UDISKS_DEVICE_IFACE);
   if (!proxy) return NULL;

   // create the places volume
   vol = places_volume_add(devpath, first_time);
   if (!vol) return NULL;
   vol->backend_data = proxy;
   vol->mount_func = _places_udisks_mount_func;
   vol->unmount_func = _places_udisks_unmount_func;
   vol->eject_func = _places_udisks_eject_func;
   vol->free_func = _places_udisks_free_func;

   // get notification when the dive change
   eldbus_proxy_signal_handler_add(proxy, "Changed",
                                   _places_udisks_device_changed_cb, vol);

   return vol;
}

/* Callback of UDisks method "EnumerateDevices()" */
static void
_places_udisks_enumerate_devices_cb(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending)
{
   Eldbus_Message_Iter *ao;
   const char *devpath;

   EINA_SAFETY_ON_TRUE_RETURN(eldbus_message_error_get(msg, NULL, NULL));
   EINA_SAFETY_ON_FALSE_RETURN(eldbus_message_arguments_get(msg, "ao", &ao));

   while (eldbus_message_iter_get_and_next(ao, 'o', &devpath))
   {
      Volume *vol;
      vol = _places_udisks_volume_add(devpath, EINA_TRUE);
      if (!vol) return;
      eldbus_proxy_property_get_all((Eldbus_Proxy *)vol->backend_data,
                                    _places_udisks_vol_props_cb, vol);
   }
}

/* Callback for UDisks signal "DeviceAdded" */
static void
_places_udisks_device_add_cb(void *data, const Eldbus_Message *msg)
{
   Volume *vol;
   char *devpath;

   EINA_SAFETY_ON_FALSE_RETURN(eldbus_message_arguments_get(msg, "o", &devpath));
   printf("PLACES udisks: DeviceAdded [%s]\n", devpath);

   if ((vol = _places_udisks_volume_add(devpath, EINA_FALSE)))
      eldbus_proxy_property_get_all((Eldbus_Proxy *)vol->backend_data,
                                     _places_udisks_vol_props_cb, vol);
}

/* Callback for UDisks signal "DeviceRemoved" */
static void
_places_udisks_device_del_cb(void *data, const Eldbus_Message *msg)
{
   Volume *vol;
   char *devpath;

   EINA_SAFETY_ON_FALSE_RETURN(eldbus_message_arguments_get(msg, "o", &devpath));

   if ((vol = places_volume_by_id_get(devpath)))
      places_volume_del(vol);
}

/* Callback for UDisks signal "Changed" */
static void
_places_udisks_device_changed_cb(void *data, const Eldbus_Message *msg)
{
   Volume *vol = data;

   if (!vol) return;
   eldbus_proxy_property_get_all((Eldbus_Proxy *)vol->backend_data,
                                 _places_udisks_vol_props_cb, vol);
}

/* Callback for eldbus_proxy_property_get_all() */
static void
_places_udisks_vol_props_cb(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending)
{
   Volume *vol = data;
   Eldbus_Message_Iter *array, *dict;

   const char *label = NULL;
   const char *partition_label = NULL;
   const char *device = NULL;
   const char *mount_point = NULL;
   const char *drive_type = NULL;
   const char *fstype = NULL;
   const char *bus = NULL;
   const char *model = NULL;
   const char *serial = NULL;
   const char *vendor = NULL;
   const char *id_usage = NULL;
   Eina_Bool mounted = EINA_FALSE;
   Eina_Bool unlocked = EINA_FALSE;
   Eina_Bool removable = EINA_FALSE;
   Eina_Bool requires_eject = EINA_FALSE;
   Eina_Bool can_detach = EINA_FALSE;
   Eina_Bool encrypted = EINA_FALSE;
   Eina_Bool media_available = EINA_FALSE;
   unsigned long long size = 0;

   EINA_SAFETY_ON_TRUE_RETURN(eldbus_message_error_get(msg, NULL, NULL));
   EINA_SAFETY_ON_FALSE_RETURN(eldbus_message_arguments_get(msg, "a{sv}", &array));

   // collect usefull props iterating over the dict
   while (eldbus_message_iter_get_and_next(array, 'e', &dict))
   {
      Eldbus_Message_Iter *var;
      const char *key;
      Eina_Bool bool;

      if (!eldbus_message_iter_arguments_get(dict, "sv", &key, &var))
         continue;

      // skip volumes with volume.ignore set
      if (!strcmp(key, "DeviceIsMediaChangeDetectionInhibited"))
      {
         eldbus_message_iter_arguments_get(var, "b", &bool);
         if (bool) return;
      }
      // skip volumes with presentation.hide set
      if (!strcmp(key, "DevicePresentationHide"))
      {
         eldbus_message_iter_arguments_get(var, "b", &bool);
         if (bool) return;
      }
      else if (!strcmp(key, "IdUsage"))
         eldbus_message_iter_arguments_get(var, "s", &id_usage);
      else if (!strcmp(key, "DeviceFile"))
         eldbus_message_iter_arguments_get(var, "s", &device);
      else if (!strcmp(key, "IdLabel"))
         eldbus_message_iter_arguments_get(var, "s", &label);
      else if (!strcmp(key, "PartitionLabel"))
         eldbus_message_iter_arguments_get(var, "s", &partition_label);
      else if (!strcmp(key, "IdType"))
         eldbus_message_iter_arguments_get(var, "s", &fstype);
      else if (!strcmp(key, "DriveModel"))
         eldbus_message_iter_arguments_get(var, "s", &model);
      else if (!strcmp(key, "DriveSerial"))
         eldbus_message_iter_arguments_get(var, "s", &serial);
      else if (!strcmp(key, "DriveVendor"))
         eldbus_message_iter_arguments_get(var, "s", &vendor);
      else if (!strcmp(key, "DriveConnectionInterface"))
         eldbus_message_iter_arguments_get(var, "s", &bus);
      else if (!strcmp(key, "DeviceMountPaths"))
      {
         Eldbus_Message_Iter *inner_array;
         if (!eldbus_message_iter_arguments_get(var, "as", &inner_array))
            continue;
         eldbus_message_iter_get_and_next(inner_array, 's', &mount_point);
      }
      else if (!strcmp(key, "DriveMediaCompatibility"))
      {
         Eldbus_Message_Iter *inner_array;
         if (!eldbus_message_iter_arguments_get(var, "as", &inner_array))
            continue;
         eldbus_message_iter_get_and_next(inner_array, 's', &drive_type);
      }
      else if (!strcmp(key, "DeviceIsLuks"))
         eldbus_message_iter_arguments_get(var, "b", &encrypted);
      else if (!strcmp(key, "DeviceIsMounted"))
         eldbus_message_iter_arguments_get(var, "b", &mounted);
      else if (!strcmp(key, "DeviceIsLuksCleartext"))
         eldbus_message_iter_arguments_get(var, "b", &unlocked);
      else if (!strcmp(key, "DeviceIsRemovable"))
         eldbus_message_iter_arguments_get(var, "b", &removable);
      else if (!strcmp(key, "DriveIsMediaEjectable"))
         eldbus_message_iter_arguments_get(var, "b", &requires_eject);
      else if (!strcmp(key, "DeviceIsMediaAvailable"))
         eldbus_message_iter_arguments_get(var, "b", &media_available);
      else if (!strcmp(key, "DriveCanDetach"))
         eldbus_message_iter_arguments_get(var, "b", &can_detach);
      else if (!strcmp(key, "DeviceSize"))
         eldbus_message_iter_arguments_get(var, "t", &size);
   }

   // a cdrom has been ejected, invalidate the drive to 'hide' it
   if (!media_available && vol->valid)
   {
      vol->valid = EINA_FALSE;
      places_update_all_gadgets();
      return;
   }

   // skip volumes that aren't filesystems or crypto
   if (!id_usage || !id_usage[0]) return;
   if (!strcmp(id_usage, "crypto") && !encrypted)
      return;
   else if (strcmp(id_usage, "filesystem"))
     return;

   // choose the best label
   if (partition_label && partition_label[0])
      eina_stringshare_replace(&vol->label, partition_label);
   else if (label && label[0])
      eina_stringshare_replace(&vol->label, label);
   else if (mount_point && mount_point[0])
      eina_stringshare_replace(&vol->label, mount_point);
   else if (device && device[0])
      eina_stringshare_replace(&vol->label, device);

   // store all other props in the Volume*
   if (device) eina_stringshare_replace(&vol->device, device);
   if (mount_point) eina_stringshare_replace(&vol->mount_point, mount_point);
   if (fstype) eina_stringshare_replace(&vol->fstype, fstype);
   if (model) eina_stringshare_replace(&vol->model, model);
   if (serial) eina_stringshare_replace(&vol->serial, serial);
   if (vendor) eina_stringshare_replace(&vol->vendor, vendor);
   if (bus) eina_stringshare_replace(&vol->bus, bus);
   if (drive_type) eina_stringshare_replace(&vol->drive_type, drive_type);
   vol->mounted = mounted;
   vol->unlocked = unlocked;
   vol->removable = removable;
   vol->requires_eject = requires_eject || can_detach;
   vol->encrypted = encrypted;
   vol->size = size;

   if (!vol->valid)
   {
      vol->valid = EINA_TRUE;
      // trigger a full redraw, is the only way to show a new device
      places_update_all_gadgets();
   }

   // the update is always needed to trigger auto_mount/auto_open
   places_volume_update(vol);
}

static void
_places_udisks_volume_task_cb(void *data, const Eldbus_Message *msg,
                              Eldbus_Pending *pending)
{
   char *str;
   Eina_Bool ret;

   if (eldbus_message_error_get(msg, NULL, NULL))
   {
      ret = eldbus_message_arguments_get(msg, "s", &str);
      e_util_dialog_internal(D_("Operation failed"),
                             ret ? str : D_("Unknown error"));
   }
}

static void
_places_udisks_mount_func(Volume *vol, Eina_List *opts)
{
   Eldbus_Proxy *proxy = vol->backend_data;
   Eldbus_Message_Iter *array, *main_iter;
   Eldbus_Message *msg;
   Eina_List *l;
   const char *opt_txt;

   if (!proxy) return;

   msg = eldbus_proxy_method_call_new(proxy, "FilesystemMount");
   main_iter = eldbus_message_iter_get(msg);
   eldbus_message_iter_arguments_append(main_iter, "sas", vol->fstype, &array);
   EINA_LIST_FOREACH(opts, l, opt_txt)
   {
      // printf("  opt: '%s'\n", opt_txt);
      eldbus_message_iter_basic_append(array, 's', opt_txt);
   }
   eldbus_message_iter_container_close(main_iter, array);

   eldbus_proxy_send(proxy, msg, _places_udisks_volume_task_cb, vol, -1);
}

static void
_places_udisks_unmount_func(Volume *vol, Eina_List *opts)
{
   Eldbus_Proxy *proxy = vol->backend_data;
   Eldbus_Message_Iter *array, *main_iter;
   Eldbus_Message *msg;

   if (!proxy) return;

   msg = eldbus_proxy_method_call_new(proxy, "FilesystemUnmount");
   main_iter = eldbus_message_iter_get(msg);
   eldbus_message_iter_arguments_append(main_iter, "as", &array);
   // here we can use the force option if needed
   eldbus_message_iter_container_close(main_iter, array);

   eldbus_proxy_send(proxy, msg, _places_udisks_volume_task_cb, vol, -1);
}

static void
_places_udisks_eject_func(Volume *vol, Eina_List *opts)
{
   Eldbus_Proxy *proxy = vol->backend_data;
   Eldbus_Message_Iter *array, *main_iter;
   Eldbus_Message *msg;

   if (!proxy) return;

   msg = eldbus_proxy_method_call_new(proxy, "DriveEject");
   // msg = eldbus_proxy_method_call_new(proxy, "DriveDetach");
   main_iter = eldbus_message_iter_get(msg);
   eldbus_message_iter_arguments_append(main_iter, "as", &array);
   eldbus_message_iter_container_close(main_iter, array);

   eldbus_proxy_send(proxy, msg, _places_udisks_volume_task_cb, vol, -1);
}

static void
_places_udisks_free_func(Volume *vol)
{
   Eldbus_Object *obj;
   Eldbus_Proxy *proxy;

   if (vol->backend_data)
   {
      proxy = vol->backend_data;
      obj = eldbus_proxy_object_get(proxy);

      if (proxy) eldbus_proxy_unref(proxy);
      if (obj) eldbus_object_unref(obj);

      vol->backend_data = NULL;
   }
}

#endif