Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gvolumemonitor.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3
/* GIO - GLib Input, Output and Streaming Library
4
 * 
5
 * Copyright (C) 2006-2007 Red Hat, Inc.
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * Author: Alexander Larsson <alexl@redhat.com>
21
 *         David Zeuthen <davidz@redhat.com>
22
 */
23
24
#include "config.h"
25
#include "gvolumemonitor.h"
26
#include "gvolume.h"
27
#include "gmount.h"
28
#include "gdrive.h"
29
#include "glibintl.h"
30
31
32
/**
33
 * SECTION:gvolumemonitor
34
 * @short_description: Volume Monitor
35
 * @include: gio/gio.h
36
 * @see_also: #GFileMonitor
37
 * 
38
 * #GVolumeMonitor is for listing the user interesting devices and volumes
39
 * on the computer. In other words, what a file selector or file manager
40
 * would show in a sidebar. 
41
 *
42
 * #GVolumeMonitor is not
43
 * [thread-default-context aware][g-main-context-push-thread-default],
44
 * and so should not be used other than from the main thread, with no
45
 * thread-default-context active.
46
 *
47
 * In order to receive updates about volumes and mounts monitored through GVFS,
48
 * a main loop must be running.
49
 **/
50
51
G_DEFINE_TYPE (GVolumeMonitor, g_volume_monitor, G_TYPE_OBJECT)
52
53
enum {
54
  VOLUME_ADDED,
55
  VOLUME_REMOVED,
56
  VOLUME_CHANGED,
57
  MOUNT_ADDED,
58
  MOUNT_REMOVED,
59
  MOUNT_PRE_UNMOUNT,
60
  MOUNT_CHANGED,
61
  DRIVE_CONNECTED,
62
  DRIVE_DISCONNECTED,
63
  DRIVE_CHANGED,
64
  DRIVE_EJECT_BUTTON,
65
  DRIVE_STOP_BUTTON,
66
  LAST_SIGNAL
67
};
68
69
static guint signals[LAST_SIGNAL] = { 0 };
70
71
72
static void
73
g_volume_monitor_finalize (GObject *object)
74
0
{
75
0
  G_OBJECT_CLASS (g_volume_monitor_parent_class)->finalize (object);
76
0
}
77
78
static void
79
g_volume_monitor_class_init (GVolumeMonitorClass *klass)
80
0
{
81
0
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
82
  
83
0
  gobject_class->finalize = g_volume_monitor_finalize;
84
85
  /**
86
   * GVolumeMonitor::volume-added:
87
   * @volume_monitor: The volume monitor emitting the signal.
88
   * @volume: a #GVolume that was added.
89
   * 
90
   * Emitted when a mountable volume is added to the system.
91
   **/
92
0
  signals[VOLUME_ADDED] = g_signal_new (I_("volume-added"),
93
0
                                        G_TYPE_VOLUME_MONITOR,
94
0
                                        G_SIGNAL_RUN_LAST,
95
0
                                        G_STRUCT_OFFSET (GVolumeMonitorClass, volume_added),
96
0
                                        NULL, NULL,
97
0
                                        NULL,
98
0
                                        G_TYPE_NONE, 1, G_TYPE_VOLUME);
99
  
100
  /**
101
   * GVolumeMonitor::volume-removed:
102
   * @volume_monitor: The volume monitor emitting the signal.
103
   * @volume: a #GVolume that was removed.
104
   * 
105
   * Emitted when a mountable volume is removed from the system.
106
   **/  
107
0
  signals[VOLUME_REMOVED] = g_signal_new (I_("volume-removed"),
108
0
                                          G_TYPE_VOLUME_MONITOR,
109
0
                                          G_SIGNAL_RUN_LAST,
110
0
                                          G_STRUCT_OFFSET (GVolumeMonitorClass, volume_removed),
111
0
                                          NULL, NULL,
112
0
                                          NULL,
113
0
                                          G_TYPE_NONE, 1, G_TYPE_VOLUME);
114
  
115
  /**
116
   * GVolumeMonitor::volume-changed:
117
   * @volume_monitor: The volume monitor emitting the signal.
118
   * @volume: a #GVolume that changed.
119
   * 
120
   * Emitted when mountable volume is changed.
121
   **/  
122
0
  signals[VOLUME_CHANGED] = g_signal_new (I_("volume-changed"),
123
0
                                          G_TYPE_VOLUME_MONITOR,
124
0
                                          G_SIGNAL_RUN_LAST,
125
0
                                          G_STRUCT_OFFSET (GVolumeMonitorClass, volume_changed),
126
0
                                          NULL, NULL,
127
0
                                          NULL,
128
0
                                          G_TYPE_NONE, 1, G_TYPE_VOLUME);
129
130
  /**
131
   * GVolumeMonitor::mount-added:
132
   * @volume_monitor: The volume monitor emitting the signal.
133
   * @mount: a #GMount that was added.
134
   * 
135
   * Emitted when a mount is added.
136
   **/
137
0
  signals[MOUNT_ADDED] = g_signal_new (I_("mount-added"),
138
0
                                       G_TYPE_VOLUME_MONITOR,
139
0
                                       G_SIGNAL_RUN_LAST,
140
0
                                       G_STRUCT_OFFSET (GVolumeMonitorClass, mount_added),
141
0
                                       NULL, NULL,
142
0
                                       NULL,
143
0
                                       G_TYPE_NONE, 1, G_TYPE_MOUNT);
144
145
  /**
146
   * GVolumeMonitor::mount-removed:
147
   * @volume_monitor: The volume monitor emitting the signal.
148
   * @mount: a #GMount that was removed.
149
   * 
150
   * Emitted when a mount is removed.
151
   **/
152
0
  signals[MOUNT_REMOVED] = g_signal_new (I_("mount-removed"),
153
0
                                         G_TYPE_VOLUME_MONITOR,
154
0
                                         G_SIGNAL_RUN_LAST,
155
0
                                         G_STRUCT_OFFSET (GVolumeMonitorClass, mount_removed),
156
0
                                         NULL, NULL,
157
0
                                         NULL,
158
0
                                         G_TYPE_NONE, 1, G_TYPE_MOUNT);
159
160
  /**
161
   * GVolumeMonitor::mount-pre-unmount:
162
   * @volume_monitor: The volume monitor emitting the signal.
163
   * @mount: a #GMount that is being unmounted.
164
   *
165
   * May be emitted when a mount is about to be removed.
166
   *
167
   * This signal depends on the backend and is only emitted if
168
   * GIO was used to unmount.
169
   **/ 
170
0
  signals[MOUNT_PRE_UNMOUNT] = g_signal_new (I_("mount-pre-unmount"),
171
0
                                             G_TYPE_VOLUME_MONITOR,
172
0
                                             G_SIGNAL_RUN_LAST,
173
0
                                             G_STRUCT_OFFSET (GVolumeMonitorClass, mount_pre_unmount),
174
0
                                             NULL, NULL,
175
0
                                             NULL,
176
0
                                             G_TYPE_NONE, 1, G_TYPE_MOUNT);
177
178
  /**
179
   * GVolumeMonitor::mount-changed:
180
   * @volume_monitor: The volume monitor emitting the signal.
181
   * @mount: a #GMount that changed.
182
   *
183
   * Emitted when a mount changes.
184
   **/ 
185
0
  signals[MOUNT_CHANGED] = g_signal_new (I_("mount-changed"),
186
0
                                         G_TYPE_VOLUME_MONITOR,
187
0
                                         G_SIGNAL_RUN_LAST,
188
0
                                         G_STRUCT_OFFSET (GVolumeMonitorClass, mount_changed),
189
0
                                         NULL, NULL,
190
0
                                         NULL,
191
0
                                         G_TYPE_NONE, 1, G_TYPE_MOUNT);
192
193
  /**
194
   * GVolumeMonitor::drive-connected:
195
   * @volume_monitor: The volume monitor emitting the signal.
196
   * @drive: a #GDrive that was connected.
197
   * 
198
   * Emitted when a drive is connected to the system.
199
   **/
200
0
  signals[DRIVE_CONNECTED] = g_signal_new (I_("drive-connected"),
201
0
             G_TYPE_VOLUME_MONITOR,
202
0
             G_SIGNAL_RUN_LAST,
203
0
             G_STRUCT_OFFSET (GVolumeMonitorClass, drive_connected),
204
0
             NULL, NULL,
205
0
             NULL,
206
0
             G_TYPE_NONE, 1, G_TYPE_DRIVE);
207
  
208
  /**
209
   * GVolumeMonitor::drive-disconnected:
210
   * @volume_monitor: The volume monitor emitting the signal.
211
   * @drive: a #GDrive that was disconnected.
212
   * 
213
   * Emitted when a drive is disconnected from the system.
214
   **/  
215
0
  signals[DRIVE_DISCONNECTED] = g_signal_new (I_("drive-disconnected"),
216
0
                G_TYPE_VOLUME_MONITOR,
217
0
                G_SIGNAL_RUN_LAST,
218
0
                G_STRUCT_OFFSET (GVolumeMonitorClass, drive_disconnected),
219
0
                NULL, NULL,
220
0
                NULL,
221
0
                G_TYPE_NONE, 1, G_TYPE_DRIVE);
222
223
  /**
224
   * GVolumeMonitor::drive-changed:
225
   * @volume_monitor: The volume monitor emitting the signal.
226
   * @drive: the drive that changed
227
   *
228
   * Emitted when a drive changes.
229
   **/ 
230
0
  signals[DRIVE_CHANGED] = g_signal_new (I_("drive-changed"),
231
0
                                         G_TYPE_VOLUME_MONITOR,
232
0
                                         G_SIGNAL_RUN_LAST,
233
0
                                         G_STRUCT_OFFSET (GVolumeMonitorClass, drive_changed),
234
0
                                         NULL, NULL,
235
0
                                         NULL,
236
0
                                         G_TYPE_NONE, 1, G_TYPE_DRIVE);
237
238
  /**
239
   * GVolumeMonitor::drive-eject-button:
240
   * @volume_monitor: The volume monitor emitting the signal.
241
   * @drive: the drive where the eject button was pressed
242
   *
243
   * Emitted when the eject button is pressed on @drive.
244
   *
245
   * Since: 2.18
246
   **/
247
0
  signals[DRIVE_EJECT_BUTTON] = g_signal_new (I_("drive-eject-button"),
248
0
                                              G_TYPE_VOLUME_MONITOR,
249
0
                                              G_SIGNAL_RUN_LAST,
250
0
                                              G_STRUCT_OFFSET (GVolumeMonitorClass, drive_eject_button),
251
0
                                              NULL, NULL,
252
0
                                              NULL,
253
0
                                              G_TYPE_NONE, 1, G_TYPE_DRIVE);
254
255
  /**
256
   * GVolumeMonitor::drive-stop-button:
257
   * @volume_monitor: The volume monitor emitting the signal.
258
   * @drive: the drive where the stop button was pressed
259
   *
260
   * Emitted when the stop button is pressed on @drive.
261
   *
262
   * Since: 2.22
263
   **/
264
0
  signals[DRIVE_STOP_BUTTON] = g_signal_new (I_("drive-stop-button"),
265
0
                                             G_TYPE_VOLUME_MONITOR,
266
0
                                             G_SIGNAL_RUN_LAST,
267
0
                                             G_STRUCT_OFFSET (GVolumeMonitorClass, drive_stop_button),
268
0
                                             NULL, NULL,
269
0
                                             NULL,
270
0
                                             G_TYPE_NONE, 1, G_TYPE_DRIVE);
271
272
0
}
273
274
static void
275
g_volume_monitor_init (GVolumeMonitor *monitor)
276
0
{
277
0
}
278
279
280
/**
281
 * g_volume_monitor_get_connected_drives:
282
 * @volume_monitor: a #GVolumeMonitor.
283
 * 
284
 * Gets a list of drives connected to the system.
285
 * 
286
 * The returned list should be freed with g_list_free(), after
287
 * its elements have been unreffed with g_object_unref().
288
 *
289
 * Returns: (element-type GDrive) (transfer full): a #GList of connected #GDrive objects.
290
 **/
291
GList *
292
g_volume_monitor_get_connected_drives (GVolumeMonitor *volume_monitor)
293
0
{
294
0
  GVolumeMonitorClass *class;
295
296
0
  g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
297
298
0
  class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
299
300
0
  return class->get_connected_drives (volume_monitor);
301
0
}
302
303
/**
304
 * g_volume_monitor_get_volumes:
305
 * @volume_monitor: a #GVolumeMonitor.
306
 * 
307
 * Gets a list of the volumes on the system.
308
 * 
309
 * The returned list should be freed with g_list_free(), after
310
 * its elements have been unreffed with g_object_unref().
311
 *
312
 * Returns: (element-type GVolume) (transfer full): a #GList of #GVolume objects.
313
 **/
314
GList *
315
g_volume_monitor_get_volumes (GVolumeMonitor *volume_monitor)
316
0
{
317
0
  GVolumeMonitorClass *class;
318
319
0
  g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
320
321
0
  class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
322
323
0
  return class->get_volumes (volume_monitor);
324
0
}
325
326
/**
327
 * g_volume_monitor_get_mounts:
328
 * @volume_monitor: a #GVolumeMonitor.
329
 * 
330
 * Gets a list of the mounts on the system.
331
 *
332
 * The returned list should be freed with g_list_free(), after
333
 * its elements have been unreffed with g_object_unref().
334
 * 
335
 * Returns: (element-type GMount) (transfer full): a #GList of #GMount objects.
336
 **/
337
GList *
338
g_volume_monitor_get_mounts (GVolumeMonitor *volume_monitor)
339
0
{
340
0
  GVolumeMonitorClass *class;
341
342
0
  g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
343
344
0
  class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
345
346
0
  return class->get_mounts (volume_monitor);
347
0
}
348
349
/**
350
 * g_volume_monitor_get_volume_for_uuid:
351
 * @volume_monitor: a #GVolumeMonitor.
352
 * @uuid: the UUID to look for
353
 * 
354
 * Finds a #GVolume object by its UUID (see g_volume_get_uuid())
355
 * 
356
 * Returns: (nullable) (transfer full): a #GVolume or %NULL if no such volume is available.
357
 *     Free the returned object with g_object_unref().
358
 **/
359
GVolume *
360
g_volume_monitor_get_volume_for_uuid (GVolumeMonitor *volume_monitor, 
361
                                      const char     *uuid)
362
0
{
363
0
  GVolumeMonitorClass *class;
364
365
0
  g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
366
0
  g_return_val_if_fail (uuid != NULL, NULL);
367
368
0
  class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
369
370
0
  return class->get_volume_for_uuid (volume_monitor, uuid);
371
0
}
372
373
/**
374
 * g_volume_monitor_get_mount_for_uuid:
375
 * @volume_monitor: a #GVolumeMonitor.
376
 * @uuid: the UUID to look for
377
 * 
378
 * Finds a #GMount object by its UUID (see g_mount_get_uuid())
379
 * 
380
 * Returns: (nullable) (transfer full): a #GMount or %NULL if no such mount is available.
381
 *     Free the returned object with g_object_unref().
382
 **/
383
GMount *
384
g_volume_monitor_get_mount_for_uuid (GVolumeMonitor *volume_monitor, 
385
                                     const char     *uuid)
386
0
{
387
0
  GVolumeMonitorClass *class;
388
389
0
  g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
390
0
  g_return_val_if_fail (uuid != NULL, NULL);
391
392
0
  class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
393
394
0
  return class->get_mount_for_uuid (volume_monitor, uuid);
395
0
}