Add new error codes for when compilation fails and make compilation error
[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 #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_assert (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       GUnixMount *mount;
116       
117       /* Emulate unmount detection */
118       
119       mount = g_get_unix_mount_at (local_monitor->dirname, NULL);
120       
121       local_monitor->was_mounted = mount != NULL;
122       
123       if (mount)
124         g_unix_mount_free (mount);
125
126       local_monitor->mount_monitor = g_unix_mount_monitor_new ();
127       g_signal_connect (local_monitor->mount_monitor, "mounts_changed",
128         G_CALLBACK (mounts_changed), local_monitor);
129     }
130
131   return obj;
132 }
133
134 static void
135 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
136 {
137   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
138   GDirectoryMonitorClass *dir_monitor_class = G_DIRECTORY_MONITOR_CLASS (klass);
139   
140   gobject_class->finalize = g_local_directory_monitor_finalize;
141   gobject_class->set_property = g_local_directory_monitor_set_property;
142   gobject_class->constructor = g_local_directory_monitor_constructor;
143
144   dir_monitor_class->cancel = g_local_directory_monitor_cancel;
145
146   g_object_class_install_property (gobject_class, PROP_DIRNAME,
147     g_param_spec_string ("dirname", "Directory name", "Directory to monitor",
148         NULL, G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
149
150   klass->mount_notify = FALSE;
151 }
152
153 static void
154 g_local_directory_monitor_init (GLocalDirectoryMonitor* local_monitor)
155 {
156 }
157
158 static void
159 mounts_changed (GUnixMountMonitor *mount_monitor,
160                 gpointer           user_data)
161 {
162   GLocalDirectoryMonitor *local_monitor = user_data;
163   GUnixMount *mount;
164   gboolean is_mounted;
165   GFile *file;
166   
167   /* Emulate unmount detection */
168   
169   mount = g_get_unix_mount_at (local_monitor->dirname, NULL);
170   
171   is_mounted = mount != NULL;
172   
173   if (mount)
174     g_unix_mount_free (mount);
175
176   if (local_monitor->was_mounted != is_mounted)
177     {
178       if (local_monitor->was_mounted && !is_mounted)
179         {
180           file = g_file_new_for_path (local_monitor->dirname);
181           g_directory_monitor_emit_event (G_DIRECTORY_MONITOR (local_monitor),
182                                           file, NULL,
183                                           G_FILE_MONITOR_EVENT_UNMOUNTED);
184           g_object_unref (file);
185         }
186       local_monitor->was_mounted = is_mounted;
187     }
188 }
189
190 static gint
191 _compare_monitor_class_by_prio (gconstpointer a,
192                                 gconstpointer b,
193                                 gpointer      user_data)
194 {
195   GType *type1 = (GType *) a, *type2 = (GType *) b;
196   GLocalDirectoryMonitorClass *klass1, *klass2;
197   gint ret;
198
199   klass1 = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (*type1));
200   klass2 = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (*type2));
201
202   ret = klass1->prio - klass2->prio;
203
204   g_type_class_unref (klass1);
205   g_type_class_unref (klass2);
206
207   return ret;
208 }
209
210 extern GType _g_inotify_directory_monitor_get_type (void);
211
212 static gpointer
213 get_default_local_directory_monitor (gpointer data)
214 {
215   GType *monitor_impls, chosen_type;
216   guint n_monitor_impls;
217   GType *ret = (GType *) data;
218   gint i;
219
220 #if defined(HAVE_SYS_INOTIFY_H) || defined(HAVE_LINUX_INOTIFY_H)
221   /* Register Inotify monitor */
222   _g_inotify_directory_monitor_get_type ();
223 #endif
224
225   g_io_modules_ensure_loaded (GIO_MODULE_DIR);
226   
227   monitor_impls = g_type_children (G_TYPE_LOCAL_DIRECTORY_MONITOR,
228                                    &n_monitor_impls);
229
230   chosen_type = G_TYPE_INVALID;
231
232   g_qsort_with_data (monitor_impls,
233                      n_monitor_impls,
234                      sizeof (GType),
235                      _compare_monitor_class_by_prio,
236                      NULL);
237
238   for (i = n_monitor_impls - 1; i >= 0 && chosen_type == G_TYPE_INVALID; i--)
239     {    
240       GLocalDirectoryMonitorClass *klass;
241
242       klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_ref (monitor_impls[i]));
243
244       if (klass->is_supported())
245         chosen_type = monitor_impls[i];
246
247       g_type_class_unref (klass);
248     }
249
250   g_free (monitor_impls);
251   *ret = chosen_type;
252
253   return NULL;
254 }
255
256 /**
257  * g_local_directory_monitor_new:
258  * @dirname: filename of the directory to monitor.
259  * @flags: #GFileMonitorFlags.
260  * 
261  * Returns: new #GDirectoryMonitor for the given @dirname.
262  **/
263 GDirectoryMonitor*
264 _g_local_directory_monitor_new (const char*       dirname,
265                                 GFileMonitorFlags flags)
266 {
267   static GOnce once_init = G_ONCE_INIT;
268   static GType monitor_type = G_TYPE_INVALID;
269
270   g_once (&once_init, get_default_local_directory_monitor, &monitor_type);
271
272   if (monitor_type != G_TYPE_INVALID)
273     return G_DIRECTORY_MONITOR (g_object_new (monitor_type, "dirname", dirname, NULL));
274
275   return NULL;
276 }
277
278 static gboolean
279 g_local_directory_monitor_cancel (GDirectoryMonitor* monitor)
280 {
281   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
282
283   if (local_monitor->mount_monitor)
284     {
285       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
286       g_object_unref (local_monitor->mount_monitor);
287       local_monitor->mount_monitor = NULL;
288     }
289
290   return TRUE;
291 }
292
293 #define __G_LOCAL_DIRECTORY_MONITOR_C__
294 #include "gioaliasdef.c"