Merge remote-tracking branch 'gvdb/master'
[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
35 enum
36 {
37   PROP_0,
38   PROP_DIRNAME,
39   PROP_FLAGS
40 };
41
42 static gboolean g_local_directory_monitor_cancel (GFileMonitor      *monitor);
43 static void     mounts_changed                   (GUnixMountMonitor *mount_monitor, 
44                                                   gpointer           user_data);
45
46 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_MONITOR)
47
48 static void
49 g_local_directory_monitor_finalize (GObject *object)
50 {
51   GLocalDirectoryMonitor *local_monitor;
52   local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
53
54   g_free (local_monitor->dirname);
55
56   if (local_monitor->mount_monitor)
57     {
58       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
59       g_object_unref (local_monitor->mount_monitor);
60       local_monitor->mount_monitor = NULL;
61     }
62
63   G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize (object);
64 }
65
66 static void
67 g_local_directory_monitor_set_property (GObject      *object,
68                                         guint         property_id,
69                                         const GValue *value,
70                                         GParamSpec   *pspec)
71 {
72   switch (property_id)
73   {
74     case PROP_DIRNAME:
75       /* Do nothing */
76       break;
77     case PROP_FLAGS:
78       /* Do nothing */
79       break;
80     default:
81       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
82       break;
83   }
84 }
85
86 static GObject *
87 g_local_directory_monitor_constructor (GType                  type,
88                                        guint                  n_construct_properties,
89                                        GObjectConstructParam *construct_properties)
90 {
91   GObject *obj;
92   GLocalDirectoryMonitorClass *klass;
93   GObjectClass *parent_class;
94   GLocalDirectoryMonitor *local_monitor;
95   GFileMonitorFlags  flags = 0;
96   const gchar *dirname = NULL;
97   gint i;
98   
99   klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
100   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
101   obj = parent_class->constructor (type,
102                                    n_construct_properties,
103                                    construct_properties);
104
105   local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
106
107   for (i = 0; i < n_construct_properties; i++)
108     {
109       if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
110         {
111           g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
112           dirname = g_value_get_string (construct_properties[i].value);
113         }
114       if (strcmp ("flags", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
115         {
116           g_warn_if_fail (G_VALUE_HOLDS_FLAGS (construct_properties[i].value));
117           flags = g_value_get_flags (construct_properties[i].value);
118         }
119     }
120
121   local_monitor->dirname = g_strdup (dirname);
122   local_monitor->flags = flags;
123
124   if (!klass->mount_notify &&
125       (flags & G_FILE_MONITOR_WATCH_MOUNTS))
126     {
127 #ifdef G_OS_WIN32
128       /*claim everything was mounted */
129       local_monitor->was_mounted = TRUE;
130 #else
131       GUnixMountEntry *mount;
132       
133       /* Emulate unmount detection */
134       
135       mount = g_unix_mount_at (local_monitor->dirname, NULL);
136       
137       local_monitor->was_mounted = mount != NULL;
138       
139       if (mount)
140         g_unix_mount_free (mount);
141
142       local_monitor->mount_monitor = g_unix_mount_monitor_new ();
143       g_signal_connect_object (local_monitor->mount_monitor, "mounts-changed",
144                                G_CALLBACK (mounts_changed), local_monitor, 0);
145 #endif
146     }
147
148   return obj;
149 }
150
151 static void
152 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
153 {
154   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
155   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
156   
157   gobject_class->finalize = g_local_directory_monitor_finalize;
158   gobject_class->set_property = g_local_directory_monitor_set_property;
159   gobject_class->constructor = g_local_directory_monitor_constructor;
160
161   file_monitor_class->cancel = g_local_directory_monitor_cancel;
162
163   g_object_class_install_property (gobject_class, 
164                                    PROP_DIRNAME,
165                                    g_param_spec_string ("dirname", 
166                                                         P_("Directory name"), 
167                                                         P_("Directory to monitor"),
168                                                         NULL, 
169                                                         G_PARAM_CONSTRUCT_ONLY|
170                                                         G_PARAM_WRITABLE|
171                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
172   g_object_class_install_property (gobject_class,
173                                    PROP_FLAGS,
174                                    g_param_spec_flags ("flags",
175                                                        P_("Monitor flags"),
176                                                        P_("Monitor flags"),
177                                                        G_TYPE_FILE_MONITOR_FLAGS,
178                                                        0,
179                                                        G_PARAM_CONSTRUCT_ONLY|
180                                                        G_PARAM_WRITABLE|
181                                                        G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
182
183   klass->mount_notify = FALSE;
184 }
185
186 static void
187 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
188 {
189 }
190
191 static void
192 mounts_changed (GUnixMountMonitor *mount_monitor,
193                 gpointer           user_data)
194 {
195   GLocalDirectoryMonitor *local_monitor = user_data;
196   GUnixMountEntry *mount;
197   gboolean is_mounted;
198   GFile *file;
199   
200   /* Emulate unmount detection */
201 #ifdef G_OS_WIN32
202   mount = NULL;
203   /*claim everything was mounted */
204   is_mounted = TRUE;
205 #else  
206   mount = g_unix_mount_at (local_monitor->dirname, NULL);
207   
208   is_mounted = mount != NULL;
209   
210   if (mount)
211     g_unix_mount_free (mount);
212 #endif
213
214   if (local_monitor->was_mounted != is_mounted)
215     {
216       if (local_monitor->was_mounted && !is_mounted)
217         {
218           file = g_file_new_for_path (local_monitor->dirname);
219           g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor),
220                                      file, NULL,
221                                      G_FILE_MONITOR_EVENT_UNMOUNTED);
222           g_object_unref (file);
223         }
224       local_monitor->was_mounted = is_mounted;
225     }
226 }
227
228 static gpointer
229 get_default_local_directory_monitor (gpointer data)
230 {
231   GLocalDirectoryMonitorClass *chosen_class;
232   GLocalDirectoryMonitorClass **ret = data;
233   GIOExtensionPoint *ep;
234   GList *extensions, *l;
235
236   _g_io_modules_ensure_loaded ();
237
238   ep = g_io_extension_point_lookup (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
239
240   extensions = g_io_extension_point_get_extensions (ep);
241   
242   chosen_class = NULL;
243   for (l = extensions; l != NULL; l = l->next)
244     {
245       GIOExtension *extension = l->data;
246       GLocalDirectoryMonitorClass *klass;
247       
248       klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_io_extension_ref_class (extension));
249       
250       if (klass->is_supported ())
251         {
252           chosen_class = klass;
253           break;
254         }
255       else
256         g_type_class_unref (klass);
257     }
258   
259   if (chosen_class)
260     {
261       *ret = chosen_class;
262       return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
263     }
264   else
265     return (gpointer)G_TYPE_INVALID;
266 }
267
268 GFileMonitor*
269 _g_local_directory_monitor_new (const char         *dirname,
270                                 GFileMonitorFlags   flags,
271                                 GError            **error)
272 {
273   static GOnce once_init = G_ONCE_INIT;
274   GTypeClass *type_class;
275   GFileMonitor *monitor;
276   GType type;
277
278   type_class = NULL;
279   g_once (&once_init, get_default_local_directory_monitor, &type_class);
280   type = (GType)once_init.retval;
281
282   monitor = NULL;
283   if (type != G_TYPE_INVALID)
284     monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, "flags", flags, NULL));
285   else
286     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
287                          _("Unable to find default local directory monitor type"));
288
289   /* This is non-null on first pass here. Unref the class now.
290    * This is to avoid unloading the module and then loading it
291    * again which would happen if we unrefed the class
292    * before creating the monitor.
293    */
294   
295   if (type_class)
296     g_type_class_unref (type_class);
297   
298   return monitor;
299 }
300
301 static gboolean
302 g_local_directory_monitor_cancel (GFileMonitor *monitor)
303 {
304   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
305
306   if (local_monitor->mount_monitor)
307     {
308       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
309       g_object_unref (local_monitor->mount_monitor);
310       local_monitor->mount_monitor = NULL;
311     }
312
313   return TRUE;
314 }