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