Make g_io_modules_load_all_in_directory not unuse loaded modules so that
[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 "gdirectorymonitor.h"
28 #include "giomodule-priv.h"
29
30 #include <string.h>
31
32 #include "gioalias.h"
33
34 enum
35 {
36   PROP_0,
37   PROP_DIRNAME
38 };
39
40 static gboolean g_local_directory_monitor_cancel (GDirectoryMonitor* monitor);
41 static void mounts_changed (GUnixMountMonitor *mount_monitor, gpointer user_data);
42
43 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_DIRECTORY_MONITOR)
44
45 static void
46 g_local_directory_monitor_finalize (GObject* object)
47 {
48   GLocalDirectoryMonitor* local_monitor;
49   local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
50
51   g_free (local_monitor->dirname);
52
53   if (local_monitor->mount_monitor)
54     {
55       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
56       g_object_unref (local_monitor->mount_monitor);
57       local_monitor->mount_monitor = NULL;
58     }
59
60   if (G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize)
61     (*G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize) (object);
62 }
63
64 static void
65 g_local_directory_monitor_set_property (GObject      *object,
66                                         guint         property_id,
67                                         const GValue *value,
68                                         GParamSpec   *pspec)
69 {
70   switch (property_id)
71   {
72     case PROP_DIRNAME:
73       /* Do nothing */
74       break;
75     default:
76       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77       break;
78   }
79 }
80
81 static GObject *
82 g_local_directory_monitor_constructor (GType                  type,
83                                        guint                  n_construct_properties,
84                                        GObjectConstructParam *construct_properties)
85 {
86   GObject *obj;
87   GLocalDirectoryMonitorClass *klass;
88   GObjectClass *parent_class;
89   GLocalDirectoryMonitor *local_monitor;
90   const gchar *dirname = NULL;
91   gint i;
92   
93   klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
94   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
95   obj = parent_class->constructor (type,
96                                    n_construct_properties,
97                                    construct_properties);
98
99   local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
100
101   for (i = 0; i < n_construct_properties; i++)
102     {
103       if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
104         {
105           g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
106           dirname = g_value_get_string (construct_properties[i].value);
107           break;
108         }
109     }
110
111   local_monitor->dirname = g_strdup (dirname);
112
113   if (!klass->mount_notify)
114     {
115 #ifdef G_OS_WIN32
116       g_warning ("G_OS_WIN32: no mount emulation");
117 #else
118       GUnixMountEntry *mount;
119       
120       /* Emulate unmount detection */
121       
122       mount = g_unix_mount_at (local_monitor->dirname, NULL);
123       
124       local_monitor->was_mounted = mount != NULL;
125       
126       if (mount)
127         g_unix_mount_free (mount);
128
129       local_monitor->mount_monitor = g_unix_mount_monitor_new ();
130       g_signal_connect (local_monitor->mount_monitor, "mounts_changed",
131         G_CALLBACK (mounts_changed), local_monitor);
132 #endif
133     }
134
135   return obj;
136 }
137
138 static void
139 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
140 {
141   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
142   GDirectoryMonitorClass *dir_monitor_class = G_DIRECTORY_MONITOR_CLASS (klass);
143   
144   gobject_class->finalize = g_local_directory_monitor_finalize;
145   gobject_class->set_property = g_local_directory_monitor_set_property;
146   gobject_class->constructor = g_local_directory_monitor_constructor;
147
148   dir_monitor_class->cancel = g_local_directory_monitor_cancel;
149
150   g_object_class_install_property (gobject_class, PROP_DIRNAME,
151     g_param_spec_string ("dirname", "Directory name", "Directory to monitor",
152         NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
153
154   klass->mount_notify = FALSE;
155 }
156
157 static void
158 g_local_directory_monitor_init (GLocalDirectoryMonitor* local_monitor)
159 {
160 }
161
162 static void
163 mounts_changed (GUnixMountMonitor *mount_monitor,
164                 gpointer           user_data)
165 {
166   GLocalDirectoryMonitor *local_monitor = user_data;
167   GUnixMountEntry *mount;
168   gboolean is_mounted;
169   GFile *file;
170   
171   /* Emulate unmount detection */
172 #ifdef G_OS_WIN32
173   mount = NULL;
174   g_warning ("G_OS_WIN32: no mount emulation");
175 #else  
176   mount = g_unix_mount_at (local_monitor->dirname, NULL);
177   
178   is_mounted = mount != NULL;
179   
180   if (mount)
181     g_unix_mount_free (mount);
182 #endif
183
184   if (local_monitor->was_mounted != is_mounted)
185     {
186       if (local_monitor->was_mounted && !is_mounted)
187         {
188           file = g_file_new_for_path (local_monitor->dirname);
189           g_directory_monitor_emit_event (G_DIRECTORY_MONITOR (local_monitor),
190                                           file, NULL,
191                                           G_FILE_MONITOR_EVENT_UNMOUNTED);
192           g_object_unref (file);
193         }
194       local_monitor->was_mounted = is_mounted;
195     }
196 }
197
198 static gint
199 _compare_monitor_type_by_prio (gconstpointer _a,
200                                gconstpointer _b,
201                                gpointer      user_data)
202 {
203   const GType *a = _a, *b = _b;
204   int prio_a, prio_b;
205   gint ret;
206   GQuark private_q;
207
208   private_q = g_quark_from_static_string ("gio-prio");
209
210   prio_a = GPOINTER_TO_INT (g_type_get_qdata (*a, private_q));
211   prio_b = GPOINTER_TO_INT (g_type_get_qdata (*b, private_q));
212
213   ret = prio_b - prio_a;
214
215   return ret;
216 }
217
218 static gpointer
219 get_default_local_directory_monitor (gpointer data)
220 {
221   GType *monitor_impls;
222   guint n_monitor_impls;
223   gint i;
224   GLocalDirectoryMonitorClass *chosen_class;
225   GLocalDirectoryMonitorClass **ret = data;
226
227   _g_io_modules_ensure_loaded ();
228   
229   monitor_impls = g_type_children (G_TYPE_LOCAL_DIRECTORY_MONITOR,
230                                    &n_monitor_impls);
231
232   g_qsort_with_data (monitor_impls,
233                      n_monitor_impls,
234                      sizeof (GType),
235                      _compare_monitor_type_by_prio,
236                      NULL);
237
238   chosen_class = NULL;
239   for (i = 0; i < n_monitor_impls; i++)
240     {    
241       GLocalDirectoryMonitorClass *klass;
242       
243       klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
244       
245       if (klass->is_supported())
246         {
247           chosen_class = klass;
248           break;
249         }
250       else
251         g_type_class_unref (klass);
252     }
253
254   g_free (monitor_impls);
255   
256   if (chosen_class)
257     {
258       *ret = chosen_class;
259       return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
260     }
261   else
262     return (gpointer)G_TYPE_INVALID;
263 }
264
265 /**
266  * g_local_directory_monitor_new:
267  * @dirname: filename of the directory to monitor.
268  * @flags: #GFileMonitorFlags.
269  * 
270  * Returns: new #GDirectoryMonitor for the given @dirname.
271  **/
272 GDirectoryMonitor*
273 _g_local_directory_monitor_new (const char*       dirname,
274                                 GFileMonitorFlags flags)
275 {
276   static GOnce once_init = G_ONCE_INIT;
277   GTypeClass *type_class;
278   GDirectoryMonitor *monitor;
279   GType type;
280
281   type_class = NULL;
282   g_once (&once_init, get_default_local_directory_monitor, &type_class);
283   type = (GType)once_init.retval;
284
285   monitor = NULL;
286   if (type != G_TYPE_INVALID)
287     monitor = G_DIRECTORY_MONITOR (g_object_new (type, "dirname", dirname, NULL));
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 (GDirectoryMonitor* 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 }
315
316 #define __G_LOCAL_DIRECTORY_MONITOR_C__
317 #include "gioaliasdef.c"