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