Fix up GIO docs
[platform/upstream/glib.git] / gio / glocaldirectorymonitor.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "glocaldirectorymonitor.h"
26 #include "gunixmounts.h"
27 #include "giomodule-priv.h"
28 #include "gfile.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
31
32 #include <string.h>
33
34 #include "gioalias.h"
35
36 enum
37 {
38   PROP_0,
39   PROP_DIRNAME,
40   PROP_FLAGS
41 };
42
43 static gboolean g_local_directory_monitor_cancel (GFileMonitor      *monitor);
44 static void     mounts_changed                   (GUnixMountMonitor *mount_monitor, 
45                                                   gpointer           user_data);
46
47 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_MONITOR)
48
49 static void
50 g_local_directory_monitor_finalize (GObject *object)
51 {
52   GLocalDirectoryMonitor *local_monitor;
53   local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
54
55   g_free (local_monitor->dirname);
56
57   if (local_monitor->mount_monitor)
58     {
59       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
60       g_object_unref (local_monitor->mount_monitor);
61       local_monitor->mount_monitor = NULL;
62     }
63
64   G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize (object);
65 }
66
67 static void
68 g_local_directory_monitor_set_property (GObject      *object,
69                                         guint         property_id,
70                                         const GValue *value,
71                                         GParamSpec   *pspec)
72 {
73   switch (property_id)
74   {
75     case PROP_DIRNAME:
76       /* Do nothing */
77       break;
78     case PROP_FLAGS:
79       /* Do nothing */
80       break;
81     default:
82       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
83       break;
84   }
85 }
86
87 static GObject *
88 g_local_directory_monitor_constructor (GType                  type,
89                                        guint                  n_construct_properties,
90                                        GObjectConstructParam *construct_properties)
91 {
92   GObject *obj;
93   GLocalDirectoryMonitorClass *klass;
94   GObjectClass *parent_class;
95   GLocalDirectoryMonitor *local_monitor;
96   GFileMonitorFlags  flags = 0;
97   const gchar *dirname = NULL;
98   gint i;
99   
100   klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
101   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
102   obj = parent_class->constructor (type,
103                                    n_construct_properties,
104                                    construct_properties);
105
106   local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
107
108   for (i = 0; i < n_construct_properties; i++)
109     {
110       if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
111         {
112           g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
113           dirname = g_value_get_string (construct_properties[i].value);
114         }
115       if (strcmp ("flags", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
116         {
117           g_warn_if_fail (G_VALUE_HOLDS_FLAGS (construct_properties[i].value));
118           flags = g_value_get_flags (construct_properties[i].value);
119         }
120     }
121
122   local_monitor->dirname = g_strdup (dirname);
123   local_monitor->flags = flags;
124
125   if (!klass->mount_notify &&
126       (flags & G_FILE_MONITOR_WATCH_MOUNTS))
127     {
128 #ifdef G_OS_WIN32
129       /*claim everything was mounted */
130       local_monitor->was_mounted = TRUE;
131 #else
132       GUnixMountEntry *mount;
133       
134       /* Emulate unmount detection */
135       
136       mount = g_unix_mount_at (local_monitor->dirname, NULL);
137       
138       local_monitor->was_mounted = mount != NULL;
139       
140       if (mount)
141         g_unix_mount_free (mount);
142
143       local_monitor->mount_monitor = g_unix_mount_monitor_new ();
144       g_signal_connect_object (local_monitor->mount_monitor, "mounts-changed",
145                                G_CALLBACK (mounts_changed), local_monitor, 0);
146 #endif
147     }
148
149   return obj;
150 }
151
152 static void
153 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
154 {
155   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
156   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
157   
158   gobject_class->finalize = g_local_directory_monitor_finalize;
159   gobject_class->set_property = g_local_directory_monitor_set_property;
160   gobject_class->constructor = g_local_directory_monitor_constructor;
161
162   file_monitor_class->cancel = g_local_directory_monitor_cancel;
163
164   g_object_class_install_property (gobject_class, 
165                                    PROP_DIRNAME,
166                                    g_param_spec_string ("dirname", 
167                                                         P_("Directory name"), 
168                                                         P_("Directory to monitor"),
169                                                         NULL, 
170                                                         G_PARAM_CONSTRUCT_ONLY|
171                                                         G_PARAM_WRITABLE|
172                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
173   g_object_class_install_property (gobject_class,
174                                    PROP_FLAGS,
175                                    g_param_spec_flags ("flags",
176                                                        P_("Monitor flags"),
177                                                        P_("Monitor flags"),
178                                                        G_TYPE_FILE_MONITOR_FLAGS,
179                                                        0,
180                                                        G_PARAM_CONSTRUCT_ONLY|
181                                                        G_PARAM_WRITABLE|
182                                                        G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
183
184   klass->mount_notify = FALSE;
185 }
186
187 static void
188 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
189 {
190 }
191
192 static void
193 mounts_changed (GUnixMountMonitor *mount_monitor,
194                 gpointer           user_data)
195 {
196   GLocalDirectoryMonitor *local_monitor = user_data;
197   GUnixMountEntry *mount;
198   gboolean is_mounted;
199   GFile *file;
200   
201   /* Emulate unmount detection */
202 #ifdef G_OS_WIN32
203   mount = NULL;
204   /*claim everything was mounted */
205   is_mounted = TRUE;
206 #else  
207   mount = g_unix_mount_at (local_monitor->dirname, NULL);
208   
209   is_mounted = mount != NULL;
210   
211   if (mount)
212     g_unix_mount_free (mount);
213 #endif
214
215   if (local_monitor->was_mounted != is_mounted)
216     {
217       if (local_monitor->was_mounted && !is_mounted)
218         {
219           file = g_file_new_for_path (local_monitor->dirname);
220           g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor),
221                                      file, NULL,
222                                      G_FILE_MONITOR_EVENT_UNMOUNTED);
223           g_object_unref (file);
224         }
225       local_monitor->was_mounted = is_mounted;
226     }
227 }
228
229 static gpointer
230 get_default_local_directory_monitor (gpointer data)
231 {
232   GLocalDirectoryMonitorClass *chosen_class;
233   GLocalDirectoryMonitorClass **ret = data;
234   GIOExtensionPoint *ep;
235   GList *extensions, *l;
236
237   _g_io_modules_ensure_loaded ();
238
239   ep = g_io_extension_point_lookup (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
240
241   extensions = g_io_extension_point_get_extensions (ep);
242   
243   chosen_class = NULL;
244   for (l = extensions; l != NULL; l = l->next)
245     {
246       GIOExtension *extension = l->data;
247       GLocalDirectoryMonitorClass *klass;
248       
249       klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_io_extension_ref_class (extension));
250       
251       if (klass->is_supported ())
252         {
253           chosen_class = klass;
254           break;
255         }
256       else
257         g_type_class_unref (klass);
258     }
259   
260   if (chosen_class)
261     {
262       *ret = chosen_class;
263       return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
264     }
265   else
266     return (gpointer)G_TYPE_INVALID;
267 }
268
269 GFileMonitor*
270 _g_local_directory_monitor_new (const char         *dirname,
271                                 GFileMonitorFlags   flags,
272                                 GError            **error)
273 {
274   static GOnce once_init = G_ONCE_INIT;
275   GTypeClass *type_class;
276   GFileMonitor *monitor;
277   GType type;
278
279   type_class = NULL;
280   g_once (&once_init, get_default_local_directory_monitor, &type_class);
281   type = (GType)once_init.retval;
282
283   monitor = NULL;
284   if (type != G_TYPE_INVALID)
285     monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, NULL));
286   else
287     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
288                          _("Unable to find default local directory monitor type"));
289
290   /* This is non-null on first pass here. Unref the class now.
291    * This is to avoid unloading the module and then loading it
292    * again which would happen if we unrefed the class
293    * before creating the monitor.
294    */
295   
296   if (type_class)
297     g_type_class_unref (type_class);
298   
299   return monitor;
300 }
301
302 static gboolean
303 g_local_directory_monitor_cancel (GFileMonitor *monitor)
304 {
305   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
306
307   if (local_monitor->mount_monitor)
308     {
309       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
310       g_object_unref (local_monitor->mount_monitor);
311       local_monitor->mount_monitor = NULL;
312     }
313
314   return TRUE;
315 }
316
317 #define __G_LOCAL_DIRECTORY_MONITOR_C__
318 #include "gioaliasdef.c"