• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
g_volume_monitor_finalize(GObject * object)73 g_volume_monitor_finalize (GObject *object)
74 {
75   G_OBJECT_CLASS (g_volume_monitor_parent_class)->finalize (object);
76 }
77 
78 static void
g_volume_monitor_class_init(GVolumeMonitorClass * klass)79 g_volume_monitor_class_init (GVolumeMonitorClass *klass)
80 {
81   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
82 
83   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   signals[VOLUME_ADDED] = g_signal_new (I_("volume-added"),
93                                         G_TYPE_VOLUME_MONITOR,
94                                         G_SIGNAL_RUN_LAST,
95                                         G_STRUCT_OFFSET (GVolumeMonitorClass, volume_added),
96                                         NULL, NULL,
97                                         NULL,
98                                         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   signals[VOLUME_REMOVED] = g_signal_new (I_("volume-removed"),
108                                           G_TYPE_VOLUME_MONITOR,
109                                           G_SIGNAL_RUN_LAST,
110                                           G_STRUCT_OFFSET (GVolumeMonitorClass, volume_removed),
111                                           NULL, NULL,
112                                           NULL,
113                                           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   signals[VOLUME_CHANGED] = g_signal_new (I_("volume-changed"),
123                                           G_TYPE_VOLUME_MONITOR,
124                                           G_SIGNAL_RUN_LAST,
125                                           G_STRUCT_OFFSET (GVolumeMonitorClass, volume_changed),
126                                           NULL, NULL,
127                                           NULL,
128                                           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   signals[MOUNT_ADDED] = g_signal_new (I_("mount-added"),
138                                        G_TYPE_VOLUME_MONITOR,
139                                        G_SIGNAL_RUN_LAST,
140                                        G_STRUCT_OFFSET (GVolumeMonitorClass, mount_added),
141                                        NULL, NULL,
142                                        NULL,
143                                        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   signals[MOUNT_REMOVED] = g_signal_new (I_("mount-removed"),
153                                          G_TYPE_VOLUME_MONITOR,
154                                          G_SIGNAL_RUN_LAST,
155                                          G_STRUCT_OFFSET (GVolumeMonitorClass, mount_removed),
156                                          NULL, NULL,
157                                          NULL,
158                                          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   signals[MOUNT_PRE_UNMOUNT] = g_signal_new (I_("mount-pre-unmount"),
171                                              G_TYPE_VOLUME_MONITOR,
172                                              G_SIGNAL_RUN_LAST,
173                                              G_STRUCT_OFFSET (GVolumeMonitorClass, mount_pre_unmount),
174                                              NULL, NULL,
175                                              NULL,
176                                              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   signals[MOUNT_CHANGED] = g_signal_new (I_("mount-changed"),
186                                          G_TYPE_VOLUME_MONITOR,
187                                          G_SIGNAL_RUN_LAST,
188                                          G_STRUCT_OFFSET (GVolumeMonitorClass, mount_changed),
189                                          NULL, NULL,
190                                          NULL,
191                                          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   signals[DRIVE_CONNECTED] = g_signal_new (I_("drive-connected"),
201 					   G_TYPE_VOLUME_MONITOR,
202 					   G_SIGNAL_RUN_LAST,
203 					   G_STRUCT_OFFSET (GVolumeMonitorClass, drive_connected),
204 					   NULL, NULL,
205 					   NULL,
206 					   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   signals[DRIVE_DISCONNECTED] = g_signal_new (I_("drive-disconnected"),
216 					      G_TYPE_VOLUME_MONITOR,
217 					      G_SIGNAL_RUN_LAST,
218 					      G_STRUCT_OFFSET (GVolumeMonitorClass, drive_disconnected),
219 					      NULL, NULL,
220 					      NULL,
221 					      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   signals[DRIVE_CHANGED] = g_signal_new (I_("drive-changed"),
231                                          G_TYPE_VOLUME_MONITOR,
232                                          G_SIGNAL_RUN_LAST,
233                                          G_STRUCT_OFFSET (GVolumeMonitorClass, drive_changed),
234                                          NULL, NULL,
235                                          NULL,
236                                          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   signals[DRIVE_EJECT_BUTTON] = g_signal_new (I_("drive-eject-button"),
248                                               G_TYPE_VOLUME_MONITOR,
249                                               G_SIGNAL_RUN_LAST,
250                                               G_STRUCT_OFFSET (GVolumeMonitorClass, drive_eject_button),
251                                               NULL, NULL,
252                                               NULL,
253                                               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   signals[DRIVE_STOP_BUTTON] = g_signal_new (I_("drive-stop-button"),
265                                              G_TYPE_VOLUME_MONITOR,
266                                              G_SIGNAL_RUN_LAST,
267                                              G_STRUCT_OFFSET (GVolumeMonitorClass, drive_stop_button),
268                                              NULL, NULL,
269                                              NULL,
270                                              G_TYPE_NONE, 1, G_TYPE_DRIVE);
271 
272 }
273 
274 static void
g_volume_monitor_init(GVolumeMonitor * monitor)275 g_volume_monitor_init (GVolumeMonitor *monitor)
276 {
277 }
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 *
g_volume_monitor_get_connected_drives(GVolumeMonitor * volume_monitor)292 g_volume_monitor_get_connected_drives (GVolumeMonitor *volume_monitor)
293 {
294   GVolumeMonitorClass *class;
295 
296   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
297 
298   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
299 
300   return class->get_connected_drives (volume_monitor);
301 }
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 *
g_volume_monitor_get_volumes(GVolumeMonitor * volume_monitor)315 g_volume_monitor_get_volumes (GVolumeMonitor *volume_monitor)
316 {
317   GVolumeMonitorClass *class;
318 
319   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
320 
321   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
322 
323   return class->get_volumes (volume_monitor);
324 }
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 *
g_volume_monitor_get_mounts(GVolumeMonitor * volume_monitor)338 g_volume_monitor_get_mounts (GVolumeMonitor *volume_monitor)
339 {
340   GVolumeMonitorClass *class;
341 
342   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
343 
344   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
345 
346   return class->get_mounts (volume_monitor);
347 }
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 *
g_volume_monitor_get_volume_for_uuid(GVolumeMonitor * volume_monitor,const char * uuid)360 g_volume_monitor_get_volume_for_uuid (GVolumeMonitor *volume_monitor,
361                                       const char     *uuid)
362 {
363   GVolumeMonitorClass *class;
364 
365   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
366   g_return_val_if_fail (uuid != NULL, NULL);
367 
368   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
369 
370   return class->get_volume_for_uuid (volume_monitor, uuid);
371 }
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 *
g_volume_monitor_get_mount_for_uuid(GVolumeMonitor * volume_monitor,const char * uuid)384 g_volume_monitor_get_mount_for_uuid (GVolumeMonitor *volume_monitor,
385                                      const char     *uuid)
386 {
387   GVolumeMonitorClass *class;
388 
389   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
390   g_return_val_if_fail (uuid != NULL, NULL);
391 
392   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
393 
394   return class->get_mount_for_uuid (volume_monitor, uuid);
395 }
396