gproxyvolumemonitor.c 46.4 KB
Newer Older
1
2
3
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* gvfs - extensions for gio
 *
David Zeuthen's avatar
David Zeuthen committed
4
 * Copyright (C) 2006-2009 Red Hat, Inc.
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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, write to the
Felix Möller's avatar
Felix Möller committed
18
19
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
20
21
22
23
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

David Zeuthen's avatar
David Zeuthen committed
24
25
26
/* TODO: handle force_rescan in g_mount_guess_content_type(); right now we
 *       just scan in the daemon first time the GMount is seen and
 *       cache that result forever.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 */

#include <config.h>

#include <limits.h>
#include <string.h>
#include <stdlib.h>

#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>

#include "gproxyvolumemonitor.h"
#include "gproxymount.h"
#include "gproxyvolume.h"
#include "gproxydrive.h"
43
#include "gproxymountoperation.h"
44
#include "gvfsvolumemonitordbus.h"
45
46
47

G_LOCK_DEFINE_STATIC(proxy_vm);

48
static GDBusConnection *the_session_bus = NULL;
49
50
51
52
static GHashTable *the_volume_monitors = NULL;

struct _GProxyVolumeMonitor {
  GNativeVolumeMonitor parent;
53
54
55
  
  guint name_owner_id;
  GVfsRemoteVolumeMonitor *proxy;
56
57
58
59

  GHashTable *drives;
  GHashTable *volumes;
  GHashTable *mounts;
David Zeuthen's avatar
David Zeuthen committed
60

61
  gulong name_watcher_id;
62
63
64
65
66
67
68
69
};

G_DEFINE_DYNAMIC_TYPE_EXTENDED (GProxyVolumeMonitor,
                                g_proxy_volume_monitor,
                                G_TYPE_NATIVE_VOLUME_MONITOR,
                                G_TYPE_FLAG_ABSTRACT,
                                {})

70
static gboolean g_proxy_volume_monitor_setup_session_bus_connection (void);
71

72
static void seed_monitor (GProxyVolumeMonitor  *monitor);
73
74
75

static void signal_emit_in_idle (gpointer object, const char *signal_name, gpointer other_object);

76
77
static void dispose_in_idle (gpointer object);

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
static gboolean is_supported (GProxyVolumeMonitorClass *klass);

/* The is_supported API is kinda lame and doesn't pass in the class,
   so we work around this with this hack */
typedef gboolean (*is_supported_func) (void);

static GProxyVolumeMonitorClass *is_supported_classes[10] = { NULL };
static gboolean is_supported_0 (void) { return is_supported (is_supported_classes[0]); };
static gboolean is_supported_1 (void) { return is_supported (is_supported_classes[1]); };
static gboolean is_supported_2 (void) { return is_supported (is_supported_classes[2]); };
static gboolean is_supported_3 (void) { return is_supported (is_supported_classes[3]); };
static gboolean is_supported_4 (void) { return is_supported (is_supported_classes[4]); };
static gboolean is_supported_5 (void) { return is_supported (is_supported_classes[5]); };
static gboolean is_supported_6 (void) { return is_supported (is_supported_classes[6]); };
static gboolean is_supported_7 (void) { return is_supported (is_supported_classes[7]); };
static gboolean is_supported_8 (void) { return is_supported (is_supported_classes[8]); };
static gboolean is_supported_9 (void) { return is_supported (is_supported_classes[9]); };
static is_supported_func is_supported_funcs[] = {
  is_supported_0, is_supported_1, is_supported_2, is_supported_3,
  is_supported_4, is_supported_5, is_supported_6, is_supported_7,
  is_supported_8, is_supported_9,
  NULL
};

102
103
104
static void
g_proxy_volume_monitor_finalize (GObject *object)
{
105
106
107
108
  g_warning ("finalize() called on instance of type %s but instances of this type "
             "are supposed to live forever. This is a reference counting bug in "
             "the application or library using GVolumeMonitor.",
             g_type_name (G_OBJECT_TYPE (object)));
109
110
}

111
112
113
static void
g_proxy_volume_monitor_dispose (GObject *object)
{
114
115
116
  /* since we want instances to live forever, don't even chain up since
   * GObject's default implementation destroys e.g. qdata on the instance
   */
117
118
}

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
static gboolean
drive_compare (GDrive *a, GDrive *b)
{
  return g_strcmp0 (g_drive_get_sort_key (a), g_drive_get_sort_key (b));
}

static gboolean
volume_compare (GVolume *a, GVolume *b)
{
  return g_strcmp0 (g_volume_get_sort_key (a), g_volume_get_sort_key (b));
}

static gboolean
mount_compare (GMount *a, GMount *b)
{
  return g_strcmp0 (g_mount_get_sort_key (a), g_mount_get_sort_key (b));
}

137
138
139
140
141
142
143
static GList *
get_mounts (GVolumeMonitor *volume_monitor)
{
  GProxyVolumeMonitor *monitor;
  GList *l;
  GHashTableIter hash_iter;
  GProxyMount *mount;
144
  GProxyVolume *volume;
145
146
147
148
149
150
151
152
153
154

  monitor = G_PROXY_VOLUME_MONITOR (volume_monitor);
  l = NULL;

  G_LOCK (proxy_vm);

  g_hash_table_iter_init (&hash_iter, monitor->mounts);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &mount))
    l = g_list_append (l, g_object_ref (mount));

155
156
157
158
159
160
161
162
163
164
  /* also return shadow mounts */
  g_hash_table_iter_init (&hash_iter, monitor->volumes);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &volume))
    {
      GProxyShadowMount *shadow_mount;
      shadow_mount = g_proxy_volume_get_shadow_mount (volume);
      if (shadow_mount != NULL)
        l = g_list_append (l, shadow_mount);
    }

165
166
  G_UNLOCK (proxy_vm);

167
168
  l = g_list_sort (l, (GCompareFunc) mount_compare);

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  return l;
}

static GList *
get_volumes (GVolumeMonitor *volume_monitor)
{
  GProxyVolumeMonitor *monitor;
  GList *l;
  GHashTableIter hash_iter;
  GProxyVolume *volume;

  monitor = G_PROXY_VOLUME_MONITOR (volume_monitor);
  l = NULL;

  G_LOCK (proxy_vm);

  g_hash_table_iter_init (&hash_iter, monitor->volumes);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &volume))
    l = g_list_append (l, g_object_ref (volume));

  G_UNLOCK (proxy_vm);

191
192
  l = g_list_sort (l, (GCompareFunc) volume_compare);

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  return l;
}

static GList *
get_connected_drives (GVolumeMonitor *volume_monitor)
{
  GProxyVolumeMonitor *monitor;
  GList *l;
  GHashTableIter hash_iter;
  GProxyDrive *drive;

  monitor = G_PROXY_VOLUME_MONITOR (volume_monitor);
  l = NULL;

  G_LOCK (proxy_vm);

  g_hash_table_iter_init (&hash_iter, monitor->drives);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &drive))
    l = g_list_append (l, g_object_ref (drive));

  G_UNLOCK (proxy_vm);

215
216
  l = g_list_sort (l, (GCompareFunc) drive_compare);

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  return l;
}

static GVolume *
get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
{
  GProxyVolumeMonitor *monitor;
  GHashTableIter hash_iter;
  GVolume *found_volume;
  GVolume *volume;

  monitor = G_PROXY_VOLUME_MONITOR (volume_monitor);

  G_LOCK (proxy_vm);

  found_volume = NULL;
  g_hash_table_iter_init (&hash_iter, monitor->volumes);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &volume) &&
235
         found_volume == NULL)
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
    {
      char *_uuid;
      _uuid = g_volume_get_uuid (volume);
      if (_uuid != NULL)
        {
          if (strcmp (uuid, _uuid) == 0)
            found_volume = g_object_ref (volume);
          g_free (_uuid);
        }
    }

  G_UNLOCK (proxy_vm);

  return found_volume;
}

static GMount *
get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
{
  GProxyVolumeMonitor *monitor;
  GHashTableIter hash_iter;
  GMount *found_mount;
  GMount *mount;

  monitor = G_PROXY_VOLUME_MONITOR (volume_monitor);

  G_LOCK (proxy_vm);

  found_mount = NULL;
  g_hash_table_iter_init (&hash_iter, monitor->mounts);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &mount) &&
267
         found_mount == NULL)
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    {
      char *_uuid;
      _uuid = g_mount_get_uuid (mount);
      if (_uuid != NULL)
        {
          if (strcmp (uuid, _uuid) == 0)
            found_mount = g_object_ref (mount);
          g_free (_uuid);
        }
    }

  G_UNLOCK (proxy_vm);

  return found_mount;
}

static GMount *
get_mount_for_mount_path (const char *mount_path,
                          GCancellable *cancellable)
{
  GMount *mount;
289
290
291
292
293
  GProxyVolumeMonitor *volume_monitor;
  GProxyVolumeMonitorClass *klass;
  GHashTableIter vm_hash_iter;
  GHashTableIter vol_hash_iter;
  GProxyMount *candidate_mount;
David Zeuthen's avatar
David Zeuthen committed
294
  static GVolumeMonitor *union_monitor = NULL;
295

David Zeuthen's avatar
David Zeuthen committed
296
297
  /* There's a problem here insofar that this static method on GNativeVolumeMonitor can
   * be called *before* any of our monitors are constructed. Since this method doesn't
298
   * pass in the class structure we *do not know* which native remote monitor to use.
David Zeuthen's avatar
David Zeuthen committed
299
300
301
302
303
304
305
   *
   * To work around that, we get the singleton GVolumeMonitor... This will trigger
   * construction of a GUnionVolumeMonitor in gio which will construct the *appropriate*
   * remote volume monitors to use (it's up to gio to pick which one to use).
   *
   * Note that we will *hold* on to this reference effectively making us a resident
   * module. And effectively keeping volume monitoring alive.
306
   *
David Zeuthen's avatar
David Zeuthen committed
307
308
309
310
311
312
313
314
315
316
   * The reason we hold on to the reference is that otherwise we'd be constructing/destructing
   * *all* proxy volume monitors (which includes synchronous D-Bus calls to seed the monitor)
   * every time this method is called.
   *
   * Note that *simple* GIO apps that a) don't use volume monitors; and b) don't use the
   * g_file_find_enclosing_mount() method will never see any volume monitor overhead.
   */

  /* Note that g_volume_monitor_get() is thread safe. We don't want to call it while
   * holding the proxy_vm lock since it might end up calling our constructor.
317
   */
David Zeuthen's avatar
David Zeuthen committed
318
319
  if (union_monitor == NULL)
    union_monitor = g_volume_monitor_get ();
320
321

  mount = NULL;
David Zeuthen's avatar
David Zeuthen committed
322

323
  G_LOCK (proxy_vm);
324

325
326
327
328
  /* First find the native volume monitor if one exists */
  g_hash_table_iter_init (&vm_hash_iter, the_volume_monitors);
  while (g_hash_table_iter_next (&vm_hash_iter, NULL, (gpointer) &volume_monitor)) {
    klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (volume_monitor));
329

330
331
332
333
334
335
336
337
338
339
340
341
342
    if (klass->is_native) {
      /* The see if we've got a mount */
      g_hash_table_iter_init (&vol_hash_iter, volume_monitor->mounts);
      while (g_hash_table_iter_next (&vol_hash_iter, NULL, (gpointer) &candidate_mount)) {
        if (g_proxy_mount_has_mount_path (candidate_mount, mount_path))
          {
            mount = g_object_ref (candidate_mount);
            goto out;
          }
      }
      goto out;
    }
  }
343
344

 out:
345
  G_UNLOCK (proxy_vm);
346
347
348
  return mount;
}

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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
static void
drive_changed (GVfsRemoteVolumeMonitor *object,
               const gchar *arg_dbus_name,
               const gchar *arg_id,
               GVariant *arg_drive,
               gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyDrive *d;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  d = g_hash_table_lookup (monitor->drives, arg_id);
  if (d != NULL)
    {
      g_proxy_drive_update (d, arg_drive);
      signal_emit_in_idle (d, "changed", NULL);
      signal_emit_in_idle (monitor, "drive-changed", d);
    }
    
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
drive_connected (GVfsRemoteVolumeMonitor *object,
                 const gchar *arg_dbus_name,
                 const gchar *arg_id,
                 GVariant *arg_drive,
                 gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyDrive *d;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  d = g_hash_table_lookup (monitor->drives, arg_id);
  if (d == NULL)
    {
      d = g_proxy_drive_new (monitor);
      g_proxy_drive_update (d, arg_drive);
      g_hash_table_insert (monitor->drives, g_strdup (g_proxy_drive_get_id (d)), d);
      signal_emit_in_idle (monitor, "drive-connected", d);
    }
    
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
drive_disconnected (GVfsRemoteVolumeMonitor *object,
                    const gchar *arg_dbus_name,
                    const gchar *arg_id,
                    GVariant *arg_drive,
                    gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyDrive *d;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  d = g_hash_table_lookup (monitor->drives, arg_id);
  if (d != NULL)
    {
      g_object_ref (d);
      g_hash_table_remove (monitor->drives, arg_id);
      signal_emit_in_idle (d, "disconnected", NULL);
      signal_emit_in_idle (monitor, "drive-disconnected", d);
      g_object_unref (d);
    }
  
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
drive_eject_button (GVfsRemoteVolumeMonitor *object,
                    const gchar *arg_dbus_name,
                    const gchar *arg_id,
                    GVariant *arg_drive,
                    gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyDrive *d;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  d = g_hash_table_lookup (monitor->drives, arg_id);
  if (d != NULL)
    {
      signal_emit_in_idle (d, "eject-button", NULL);
      signal_emit_in_idle (monitor, "drive-eject-button", d);
    }

  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
drive_stop_button (GVfsRemoteVolumeMonitor *object,
                   const gchar *arg_dbus_name,
                   const gchar *arg_id,
                   GVariant *arg_drive,
                   gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyDrive *d;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  d = g_hash_table_lookup (monitor->drives, arg_id);
  if (d != NULL)
    {
      signal_emit_in_idle (d, "stop-button", NULL);
      signal_emit_in_idle (monitor, "drive-stop-button", d);
    }

  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_added (GVfsRemoteVolumeMonitor *object,
             const gchar *arg_dbus_name,
             const gchar *arg_id,
             GVariant *arg_mount,
             gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyMount *m;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  m = g_hash_table_lookup (monitor->mounts, arg_id);
  if (m == NULL)
    {
      m = g_proxy_mount_new (monitor);
      g_proxy_mount_update (m, arg_mount);
      g_hash_table_insert (monitor->mounts, g_strdup (g_proxy_mount_get_id (m)), m);
      signal_emit_in_idle (monitor, "mount-added", m);
    }
    
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_changed (GVfsRemoteVolumeMonitor *object,
               const gchar *arg_dbus_name,
               const gchar *arg_id,
               GVariant *arg_mount,
               gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyMount *m;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  m = g_hash_table_lookup (monitor->mounts, arg_id);
  if (m != NULL)
    {
      g_proxy_mount_update (m, arg_mount);
      signal_emit_in_idle (m, "changed", NULL);
      signal_emit_in_idle (monitor, "mount-changed", m);
    }
    
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_pre_unmount (GVfsRemoteVolumeMonitor *object,
                   const gchar *arg_dbus_name,
                   const gchar *arg_id,
                   GVariant *arg_mount,
                   gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyMount *m;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  m = g_hash_table_lookup (monitor->mounts, arg_id);
  if (m != NULL)
    {
      signal_emit_in_idle (m, "pre-unmount", NULL);
      signal_emit_in_idle (monitor, "mount-pre-unmount", m);
    }

  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_removed (GVfsRemoteVolumeMonitor *object,
               const gchar *arg_dbus_name,
               const gchar *arg_id,
               GVariant *arg_mount,
               gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyMount *m;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  m = g_hash_table_lookup (monitor->mounts, arg_id);
  if (m != NULL)
    {
      g_object_ref (m);
      g_hash_table_remove (monitor->mounts, arg_id);
      signal_emit_in_idle (m, "unmounted", NULL);
      signal_emit_in_idle (monitor, "mount-removed", m);
      g_object_unref (m);
    }
    
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_op_aborted (GVfsRemoteVolumeMonitor *object,
                  const gchar *arg_dbus_name,
                  const gchar *arg_id,
                  gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  g_proxy_mount_operation_handle_aborted (arg_id);

  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_op_ask_password (GVfsRemoteVolumeMonitor *object,
                       const gchar *arg_dbus_name,
                       const gchar *arg_id,
                       const gchar *arg_message_to_show,
                       const gchar *arg_default_user,
                       const gchar *arg_default_domain,
                       guint arg_flags,
                       gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  g_proxy_mount_operation_handle_ask_password (arg_id,
                                               arg_message_to_show,
                                               arg_default_user,
                                               arg_default_domain,
                                               arg_flags);

  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_op_ask_question (GVfsRemoteVolumeMonitor *object,
                       const gchar *arg_dbus_name,
                       const gchar *arg_id,
                       const gchar *arg_message_to_show,
                       const gchar *const *arg_choices,
                       gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  g_proxy_mount_operation_handle_ask_question (arg_id,
                                               arg_message_to_show,
                                               arg_choices);

  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
mount_op_show_processes (GVfsRemoteVolumeMonitor *object,
                         const gchar *arg_dbus_name,
                         const gchar *arg_id,
                         const gchar *arg_message_to_show,
                         GVariant *arg_pid,
                         const gchar *const *arg_choices,
                         gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  g_proxy_mount_operation_handle_show_processes (arg_id,
                                                 arg_message_to_show,
                                                 arg_pid,
                                                 arg_choices);

  not_for_us:
   G_UNLOCK (proxy_vm);
}

728
729
730
731
732
static void
mount_op_show_unmount_progress (GVfsRemoteVolumeMonitor *object,
                                const gchar *arg_dbus_name,
                                const gchar *arg_id,
                                const gchar *arg_message_to_show,
733
734
735
                                gint64       arg_time_left,
                                gint64       arg_bytes_left,
                                gpointer     user_data)
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  g_proxy_mount_operation_handle_show_unmount_progress (arg_id,
                                                        arg_message_to_show,
                                                        arg_time_left,
                                                        arg_bytes_left);

  not_for_us:
   G_UNLOCK (proxy_vm);
}

756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
static void
volume_added (GVfsRemoteVolumeMonitor *object,
              const gchar *arg_dbus_name,
              const gchar *arg_id,
              GVariant *arg_volume,
              gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyVolume *v;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  v = g_hash_table_lookup (monitor->volumes, arg_id);
  if (v == NULL)
    {
      v = g_proxy_volume_new (monitor);
      g_proxy_volume_update (v, arg_volume);
      g_hash_table_insert (monitor->volumes, g_strdup (g_proxy_volume_get_id (v)), v);
      signal_emit_in_idle (monitor, "volume-added", v);
    }
    
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
volume_changed (GVfsRemoteVolumeMonitor *object,
                const gchar *arg_dbus_name,
                const gchar *arg_id,
                GVariant *arg_volume,
                gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyVolume *v;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  v = g_hash_table_lookup (monitor->volumes, arg_id);
  if (v != NULL)
    {
      GProxyShadowMount *shadow_mount;

      g_proxy_volume_update (v, arg_volume);
      signal_emit_in_idle (v, "changed", NULL);
      signal_emit_in_idle (monitor, "volume-changed", v);

      shadow_mount = g_proxy_volume_get_shadow_mount (v);
      if (shadow_mount != NULL)
        {
          signal_emit_in_idle (shadow_mount, "changed", NULL);
          signal_emit_in_idle (monitor, "mount-changed", shadow_mount);
          g_object_unref (shadow_mount);
        }
    }

  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
volume_removed (GVfsRemoteVolumeMonitor *object,
                const gchar *arg_dbus_name,
                const gchar *arg_id,
                GVariant *arg_volume,
                gpointer user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  GProxyVolume *v;

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  if (strcmp (arg_dbus_name, klass->dbus_name) != 0)
    goto not_for_us;
  
  v = g_hash_table_lookup (monitor->volumes, arg_id);
  if (v != NULL)
    {
      g_object_ref (v);
      g_hash_table_remove (monitor->volumes, arg_id);
      signal_emit_in_idle (v, "removed", NULL);
      signal_emit_in_idle (monitor, "volume-removed", v);
      dispose_in_idle (v);
      g_object_unref (v);
    }
    
  not_for_us:
   G_UNLOCK (proxy_vm);
}

static void
861
name_owner_appeared (GProxyVolumeMonitor *monitor)
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
{
  GHashTableIter hash_iter;
  GProxyDrive *drive;
  GProxyVolume *volume;
  GProxyMount *mount;

  seed_monitor (monitor);

  /* emit signals for all the drives/volumes/mounts "added" */
  g_hash_table_iter_init (&hash_iter, monitor->drives);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &drive))
    signal_emit_in_idle (monitor, "drive-connected", drive);

  g_hash_table_iter_init (&hash_iter, monitor->volumes);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &volume))
    signal_emit_in_idle (monitor, "volume-added", volume);

  g_hash_table_iter_init (&hash_iter, monitor->mounts);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &mount))
    signal_emit_in_idle (monitor, "mount-added", mount);
}

static void
885
name_owner_vanished (GProxyVolumeMonitor *monitor)
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
{
  GHashTableIter hash_iter;
  GProxyDrive *drive;
  GProxyVolume *volume;
  GProxyMount *mount;

  g_hash_table_iter_init (&hash_iter, monitor->mounts);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &mount))
    {
      signal_emit_in_idle (mount, "unmounted", NULL);
      signal_emit_in_idle (monitor, "mount-removed", mount);
    }
  g_hash_table_remove_all (monitor->mounts);

  g_hash_table_iter_init (&hash_iter, monitor->volumes);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &volume))
    {
      signal_emit_in_idle (volume, "removed", NULL);
      signal_emit_in_idle (monitor, "volume-removed", volume);
    }
  g_hash_table_remove_all (monitor->volumes);

  g_hash_table_iter_init (&hash_iter, monitor->drives);
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &drive))
    {
      signal_emit_in_idle (drive, "disconnected", NULL);
      signal_emit_in_idle (monitor, "drive-disconnected", drive);
    }
  g_hash_table_remove_all (monitor->drives);
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
}

static void
name_owner_changed (GObject    *gobject,
                    GParamSpec *pspec,
                    gpointer    user_data)
{
  GProxyVolumeMonitor *monitor = G_PROXY_VOLUME_MONITOR (user_data);
  GProxyVolumeMonitorClass *klass;
  gchar *name_owner = NULL;

  klass = G_PROXY_VOLUME_MONITOR_CLASS (G_OBJECT_GET_CLASS (monitor));

  g_object_get (gobject, "g-name-owner", &name_owner, NULL);

  if (name_owner != NULL)
    {
      name_owner_appeared (monitor);
    }
  else
    {
      g_warning ("Owner of volume monitor %s disconnected from the bus; removing drives/volumes/mounts",
                 klass->dbus_name);

      name_owner_vanished (monitor);

      /* TODO: maybe try to relaunch the monitor? */
  }
943

944
  g_free (name_owner);
945
946
}

947
948
949
950
951
952
953
954
955
static GObject *
g_proxy_volume_monitor_constructor (GType                  type,
                                    guint                  n_construct_properties,
                                    GObjectConstructParam *construct_properties)
{
  GObject *object;
  GProxyVolumeMonitor *monitor;
  GProxyVolumeMonitorClass *klass;
  GObjectClass *parent_class;
956
957
  GError *error;
  const char *dbus_name;
958
  gchar *name_owner;
959
960
961
962
963
964
965
966
967
968
969

  G_LOCK (proxy_vm);

  klass = G_PROXY_VOLUME_MONITOR_CLASS (g_type_class_peek (type));
  object = g_hash_table_lookup (the_volume_monitors, (gpointer) type);
  if (object != NULL)
    {
      g_object_ref (object);
      goto out;
    }

970
971
  dbus_name = klass->dbus_name;

972
973
974
975
976
977
978
979
980
  /* Invoke parent constructor. */
  klass = G_PROXY_VOLUME_MONITOR_CLASS (g_type_class_peek (G_TYPE_PROXY_VOLUME_MONITOR));
  parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
  object = parent_class->constructor (type,
                                      n_construct_properties,
                                      construct_properties);

  monitor = G_PROXY_VOLUME_MONITOR (object);

981
982
  error = NULL;
  monitor->proxy = gvfs_remote_volume_monitor_proxy_new_sync (the_session_bus,
983
                                                              G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
                                                              dbus_name,
                                                              "/org/gtk/Private/RemoteVolumeMonitor",
                                                              NULL,
                                                              &error);
  if (monitor->proxy == NULL)
    {
      g_printerr ("Error creating proxy: %s (%s, %d)\n",
                  error->message, g_quark_to_string (error->domain), error->code);
      g_error_free (error);
      goto out;
    }

  /* listen to volume monitor signals */
  g_signal_connect (monitor->proxy, "drive-changed", G_CALLBACK (drive_changed), monitor);
  g_signal_connect (monitor->proxy, "drive-connected", G_CALLBACK (drive_connected), monitor);
  g_signal_connect (monitor->proxy, "drive-disconnected", G_CALLBACK (drive_disconnected), monitor);
  g_signal_connect (monitor->proxy, "drive-eject-button", G_CALLBACK (drive_eject_button), monitor);