Add private _g_bus_get_singleton_if_exists() function
[platform/upstream/glib.git] / gio / gvolumemonitor.c
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 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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
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 <link
45  * linkend="g-main-context-push-thread-default">thread-default-context
46  * aware</link>, and so should not be used other than from the main
47  * thread, with no thread-default-context active.
48  **/
49
50 G_DEFINE_TYPE (GVolumeMonitor, g_volume_monitor, G_TYPE_OBJECT);
51
52 enum {
53   VOLUME_ADDED,
54   VOLUME_REMOVED,
55   VOLUME_CHANGED,
56   MOUNT_ADDED,
57   MOUNT_REMOVED,
58   MOUNT_PRE_UNMOUNT,
59   MOUNT_CHANGED,
60   DRIVE_CONNECTED,
61   DRIVE_DISCONNECTED,
62   DRIVE_CHANGED,
63   DRIVE_EJECT_BUTTON,
64   DRIVE_STOP_BUTTON,
65   LAST_SIGNAL
66 };
67
68 static guint signals[LAST_SIGNAL] = { 0 };
69
70
71 static void
72 g_volume_monitor_finalize (GObject *object)
73 {
74   G_OBJECT_CLASS (g_volume_monitor_parent_class)->finalize (object);
75 }
76
77 static void
78 g_volume_monitor_class_init (GVolumeMonitorClass *klass)
79 {
80   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
81   
82   gobject_class->finalize = g_volume_monitor_finalize;
83
84   /**
85    * GVolumeMonitor::volume-added:
86    * @volume_monitor: The volume monitor emitting the signal.
87    * @volume: a #GVolume that was added.
88    * 
89    * Emitted when a mountable volume is added to the system.
90    **/
91   signals[VOLUME_ADDED] = g_signal_new (I_("volume-added"),
92                                         G_TYPE_VOLUME_MONITOR,
93                                         G_SIGNAL_RUN_LAST,
94                                         G_STRUCT_OFFSET (GVolumeMonitorClass, volume_added),
95                                         NULL, NULL,
96                                         g_cclosure_marshal_VOID__OBJECT,
97                                         G_TYPE_NONE, 1, G_TYPE_VOLUME);
98   
99   /**
100    * GVolumeMonitor::volume-removed:
101    * @volume_monitor: The volume monitor emitting the signal.
102    * @volume: a #GVolume that was removed.
103    * 
104    * Emitted when a mountable volume is removed from the system.
105    **/  
106   signals[VOLUME_REMOVED] = g_signal_new (I_("volume-removed"),
107                                           G_TYPE_VOLUME_MONITOR,
108                                           G_SIGNAL_RUN_LAST,
109                                           G_STRUCT_OFFSET (GVolumeMonitorClass, volume_removed),
110                                           NULL, NULL,
111                                           g_cclosure_marshal_VOID__OBJECT,
112                                           G_TYPE_NONE, 1, G_TYPE_VOLUME);
113   
114   /**
115    * GVolumeMonitor::volume-changed:
116    * @volume_monitor: The volume monitor emitting the signal.
117    * @volume: a #GVolume that changed.
118    * 
119    * Emitted when mountable volume is changed.
120    **/  
121   signals[VOLUME_CHANGED] = g_signal_new (I_("volume-changed"),
122                                           G_TYPE_VOLUME_MONITOR,
123                                           G_SIGNAL_RUN_LAST,
124                                           G_STRUCT_OFFSET (GVolumeMonitorClass, volume_changed),
125                                           NULL, NULL,
126                                           g_cclosure_marshal_VOID__OBJECT,
127                                           G_TYPE_NONE, 1, G_TYPE_VOLUME);
128
129   /**
130    * GVolumeMonitor::mount-added:
131    * @volume_monitor: The volume monitor emitting the signal.
132    * @mount: a #GMount that was added.
133    * 
134    * Emitted when a mount is added.
135    **/
136   signals[MOUNT_ADDED] = g_signal_new (I_("mount-added"),
137                                        G_TYPE_VOLUME_MONITOR,
138                                        G_SIGNAL_RUN_LAST,
139                                        G_STRUCT_OFFSET (GVolumeMonitorClass, mount_added),
140                                        NULL, NULL,
141                                        g_cclosure_marshal_VOID__OBJECT,
142                                        G_TYPE_NONE, 1, G_TYPE_MOUNT);
143
144   /**
145    * GVolumeMonitor::mount-removed:
146    * @volume_monitor: The volume monitor emitting the signal.
147    * @mount: a #GMount that was removed.
148    * 
149    * Emitted when a mount is removed.
150    **/
151   signals[MOUNT_REMOVED] = g_signal_new (I_("mount-removed"),
152                                          G_TYPE_VOLUME_MONITOR,
153                                          G_SIGNAL_RUN_LAST,
154                                          G_STRUCT_OFFSET (GVolumeMonitorClass, mount_removed),
155                                          NULL, NULL,
156                                          g_cclosure_marshal_VOID__OBJECT,
157                                          G_TYPE_NONE, 1, G_TYPE_MOUNT);
158
159   /**
160    * GVolumeMonitor::mount-pre-unmount:
161    * @volume_monitor: The volume monitor emitting the signal.
162    * @mount: a #GMount that is being unmounted.
163    *
164    * Emitted when a mount is about to be removed.
165    **/ 
166   signals[MOUNT_PRE_UNMOUNT] = g_signal_new (I_("mount-pre-unmount"),
167                                              G_TYPE_VOLUME_MONITOR,
168                                              G_SIGNAL_RUN_LAST,
169                                              G_STRUCT_OFFSET (GVolumeMonitorClass, mount_pre_unmount),
170                                              NULL, NULL,
171                                              g_cclosure_marshal_VOID__OBJECT,
172                                              G_TYPE_NONE, 1, G_TYPE_MOUNT);
173
174   /**
175    * GVolumeMonitor::mount-changed:
176    * @volume_monitor: The volume monitor emitting the signal.
177    * @mount: a #GMount that changed.
178    *
179    * Emitted when a mount changes.
180    **/ 
181   signals[MOUNT_CHANGED] = g_signal_new (I_("mount-changed"),
182                                          G_TYPE_VOLUME_MONITOR,
183                                          G_SIGNAL_RUN_LAST,
184                                          G_STRUCT_OFFSET (GVolumeMonitorClass, mount_changed),
185                                          NULL, NULL,
186                                          g_cclosure_marshal_VOID__OBJECT,
187                                          G_TYPE_NONE, 1, G_TYPE_MOUNT);
188
189   /**
190    * GVolumeMonitor::drive-connected:
191    * @volume_monitor: The volume monitor emitting the signal.
192    * @drive: a #GDrive that was connected.
193    * 
194    * Emitted when a drive is connected to the system.
195    **/
196   signals[DRIVE_CONNECTED] = g_signal_new (I_("drive-connected"),
197                                            G_TYPE_VOLUME_MONITOR,
198                                            G_SIGNAL_RUN_LAST,
199                                            G_STRUCT_OFFSET (GVolumeMonitorClass, drive_connected),
200                                            NULL, NULL,
201                                            g_cclosure_marshal_VOID__OBJECT,
202                                            G_TYPE_NONE, 1, G_TYPE_DRIVE);
203   
204   /**
205    * GVolumeMonitor::drive-disconnected:
206    * @volume_monitor: The volume monitor emitting the signal.
207    * @drive: a #GDrive that was disconnected.
208    * 
209    * Emitted when a drive is disconnected from the system.
210    **/  
211   signals[DRIVE_DISCONNECTED] = g_signal_new (I_("drive-disconnected"),
212                                               G_TYPE_VOLUME_MONITOR,
213                                               G_SIGNAL_RUN_LAST,
214                                               G_STRUCT_OFFSET (GVolumeMonitorClass, drive_disconnected),
215                                               NULL, NULL,
216                                               g_cclosure_marshal_VOID__OBJECT,
217                                               G_TYPE_NONE, 1, G_TYPE_DRIVE);
218
219   /**
220    * GVolumeMonitor::drive-changed:
221    * @volume_monitor: The volume monitor emitting the signal.
222    * @drive: the drive that changed
223    *
224    * Emitted when a drive changes.
225    **/ 
226   signals[DRIVE_CHANGED] = g_signal_new (I_("drive-changed"),
227                                          G_TYPE_VOLUME_MONITOR,
228                                          G_SIGNAL_RUN_LAST,
229                                          G_STRUCT_OFFSET (GVolumeMonitorClass, drive_changed),
230                                          NULL, NULL,
231                                          g_cclosure_marshal_VOID__OBJECT,
232                                          G_TYPE_NONE, 1, G_TYPE_DRIVE);
233
234   /**
235    * GVolumeMonitor::drive-eject-button:
236    * @volume_monitor: The volume monitor emitting the signal.
237    * @drive: the drive where the eject button was pressed
238    *
239    * Emitted when the eject button is pressed on @drive.
240    *
241    * Since: 2.18
242    **/
243   signals[DRIVE_EJECT_BUTTON] = g_signal_new (I_("drive-eject-button"),
244                                               G_TYPE_VOLUME_MONITOR,
245                                               G_SIGNAL_RUN_LAST,
246                                               G_STRUCT_OFFSET (GVolumeMonitorClass, drive_eject_button),
247                                               NULL, NULL,
248                                               g_cclosure_marshal_VOID__OBJECT,
249                                               G_TYPE_NONE, 1, G_TYPE_DRIVE);
250
251   /**
252    * GVolumeMonitor::drive-stop-button:
253    * @volume_monitor: The volume monitor emitting the signal.
254    * @drive: the drive where the stop button was pressed
255    *
256    * Emitted when the stop button is pressed on @drive.
257    *
258    * Since: 2.22
259    **/
260   signals[DRIVE_STOP_BUTTON] = g_signal_new (I_("drive-stop-button"),
261                                              G_TYPE_VOLUME_MONITOR,
262                                              G_SIGNAL_RUN_LAST,
263                                              G_STRUCT_OFFSET (GVolumeMonitorClass, drive_stop_button),
264                                              NULL, NULL,
265                                              g_cclosure_marshal_VOID__OBJECT,
266                                              G_TYPE_NONE, 1, G_TYPE_DRIVE);
267
268 }
269
270 static void
271 g_volume_monitor_init (GVolumeMonitor *monitor)
272 {
273 }
274
275
276 /**
277  * g_volume_monitor_get_connected_drives:
278  * @volume_monitor: a #GVolumeMonitor.
279  * 
280  * Gets a list of drives connected to the system.
281  * 
282  * The returned list should be freed with g_list_free(), after
283  * its elements have been unreffed with g_object_unref().
284  *
285  * Returns: (element-type GDrive) (transfer full): a #GList of connected #GDrive objects.
286  **/
287 GList *
288 g_volume_monitor_get_connected_drives (GVolumeMonitor *volume_monitor)
289 {
290   GVolumeMonitorClass *class;
291
292   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
293
294   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
295
296   return class->get_connected_drives (volume_monitor);
297 }
298
299 /**
300  * g_volume_monitor_get_volumes:
301  * @volume_monitor: a #GVolumeMonitor.
302  * 
303  * Gets a list of the volumes on the system.
304  * 
305  * The returned list should be freed with g_list_free(), after
306  * its elements have been unreffed with g_object_unref().
307  *
308  * Returns: (element-type GVolume) (transfer full): a #GList of #GVolume objects.
309  **/
310 GList *
311 g_volume_monitor_get_volumes (GVolumeMonitor *volume_monitor)
312 {
313   GVolumeMonitorClass *class;
314
315   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
316
317   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
318
319   return class->get_volumes (volume_monitor);
320 }
321
322 /**
323  * g_volume_monitor_get_mounts:
324  * @volume_monitor: a #GVolumeMonitor.
325  * 
326  * Gets a list of the mounts on the system.
327  *
328  * The returned list should be freed with g_list_free(), after
329  * its elements have been unreffed with g_object_unref().
330  * 
331  * Returns: (element-type GMount) (transfer full): a #GList of #GMount objects.
332  **/
333 GList *
334 g_volume_monitor_get_mounts (GVolumeMonitor *volume_monitor)
335 {
336   GVolumeMonitorClass *class;
337
338   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
339
340   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
341
342   return class->get_mounts (volume_monitor);
343 }
344
345 /**
346  * g_volume_monitor_get_volume_for_uuid:
347  * @volume_monitor: a #GVolumeMonitor.
348  * @uuid: the UUID to look for
349  * 
350  * Finds a #GVolume object by its UUID (see g_volume_get_uuid())
351  * 
352  * Returns: (transfer full): a #GVolume or %NULL if no such volume is available.
353  *     Free the returned object with g_object_unref().
354  **/
355 GVolume *
356 g_volume_monitor_get_volume_for_uuid (GVolumeMonitor *volume_monitor, 
357                                       const char     *uuid)
358 {
359   GVolumeMonitorClass *class;
360
361   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
362   g_return_val_if_fail (uuid != NULL, NULL);
363
364   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
365
366   return class->get_volume_for_uuid (volume_monitor, uuid);
367 }
368
369 /**
370  * g_volume_monitor_get_mount_for_uuid:
371  * @volume_monitor: a #GVolumeMonitor.
372  * @uuid: the UUID to look for
373  * 
374  * Finds a #GMount object by its UUID (see g_mount_get_uuid())
375  * 
376  * Returns: (transfer full): a #GMount or %NULL if no such mount is available.
377  *     Free the returned object with g_object_unref().
378  **/
379 GMount *
380 g_volume_monitor_get_mount_for_uuid (GVolumeMonitor *volume_monitor, 
381                                      const char     *uuid)
382 {
383   GVolumeMonitorClass *class;
384
385   g_return_val_if_fail (G_IS_VOLUME_MONITOR (volume_monitor), NULL);
386   g_return_val_if_fail (uuid != NULL, NULL);
387
388   class = G_VOLUME_MONITOR_GET_CLASS (volume_monitor);
389
390   return class->get_mount_for_uuid (volume_monitor, uuid);
391 }