Coverage Report

Created: 2025-06-13 06:55

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