Use g_set_error_literal where appropriate. Patch from bug #535947.
[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 "glibintl.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 (GFileMonitor      *monitor);
41 static void     mounts_changed                   (GUnixMountMonitor *mount_monitor, 
42                                                   gpointer           user_data);
43
44 G_DEFINE_ABSTRACT_TYPE (GLocalDirectoryMonitor, g_local_directory_monitor, G_TYPE_FILE_MONITOR)
45
46 static void
47 g_local_directory_monitor_finalize (GObject *object)
48 {
49   GLocalDirectoryMonitor *local_monitor;
50   local_monitor = G_LOCAL_DIRECTORY_MONITOR (object);
51
52   g_free (local_monitor->dirname);
53
54   if (local_monitor->mount_monitor)
55     {
56       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
57       g_object_unref (local_monitor->mount_monitor);
58       local_monitor->mount_monitor = NULL;
59     }
60
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_object (local_monitor->mount_monitor, "mounts_changed",
131                                G_CALLBACK (mounts_changed), local_monitor, 0);
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   GFileMonitorClass *file_monitor_class = G_FILE_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   file_monitor_class->cancel = g_local_directory_monitor_cancel;
149
150   g_object_class_install_property (gobject_class, 
151                                    PROP_DIRNAME,
152                                    g_param_spec_string ("dirname", 
153                                                         P_("Directory name"), 
154                                                         P_("Directory to monitor"),
155                                                         NULL, 
156                                                         G_PARAM_CONSTRUCT_ONLY|
157                                                         G_PARAM_WRITABLE|
158                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
159
160   klass->mount_notify = FALSE;
161 }
162
163 static void
164 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
165 {
166 }
167
168 static void
169 mounts_changed (GUnixMountMonitor *mount_monitor,
170                 gpointer           user_data)
171 {
172   GLocalDirectoryMonitor *local_monitor = user_data;
173   GUnixMountEntry *mount;
174   gboolean is_mounted;
175   GFile *file;
176   
177   /* Emulate unmount detection */
178 #ifdef G_OS_WIN32
179   mount = NULL;
180   g_warning ("G_OS_WIN32: no mount emulation");
181 #else  
182   mount = g_unix_mount_at (local_monitor->dirname, NULL);
183   
184   is_mounted = mount != NULL;
185   
186   if (mount)
187     g_unix_mount_free (mount);
188 #endif
189
190   if (local_monitor->was_mounted != is_mounted)
191     {
192       if (local_monitor->was_mounted && !is_mounted)
193         {
194           file = g_file_new_for_path (local_monitor->dirname);
195           g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor),
196                                      file, NULL,
197                                      G_FILE_MONITOR_EVENT_UNMOUNTED);
198           g_object_unref (file);
199         }
200       local_monitor->was_mounted = is_mounted;
201     }
202 }
203
204 static gpointer
205 get_default_local_directory_monitor (gpointer data)
206 {
207   GLocalDirectoryMonitorClass *chosen_class;
208   GLocalDirectoryMonitorClass **ret = data;
209   GIOExtensionPoint *ep;
210   GList *extensions, *l;
211
212   _g_io_modules_ensure_loaded ();
213
214   ep = g_io_extension_point_lookup (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
215
216   extensions = g_io_extension_point_get_extensions (ep);
217   
218   chosen_class = NULL;
219   for (l = extensions; l != NULL; l = l->next)
220     {
221       GIOExtension *extension = l->data;
222       GLocalDirectoryMonitorClass *klass;
223       
224       klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_io_extension_ref_class (extension));
225       
226       if (klass->is_supported ())
227         {
228           chosen_class = klass;
229           break;
230         }
231       else
232         g_type_class_unref (klass);
233     }
234   
235   if (chosen_class)
236     {
237       *ret = chosen_class;
238       return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
239     }
240   else
241     return (gpointer)G_TYPE_INVALID;
242 }
243
244 /**
245  * _g_local_directory_monitor_new:
246  * @dirname: filename of the directory to monitor.
247  * @flags: #GFileMonitorFlags.
248  * 
249  * Returns: new #GFileMonitor for the given @dirname.
250  **/
251 GFileMonitor*
252 _g_local_directory_monitor_new (const char         *dirname,
253                                 GFileMonitorFlags   flags,
254                                 GError            **error)
255 {
256   static GOnce once_init = G_ONCE_INIT;
257   GTypeClass *type_class;
258   GFileMonitor *monitor;
259   GType type;
260
261   type_class = NULL;
262   g_once (&once_init, get_default_local_directory_monitor, &type_class);
263   type = (GType)once_init.retval;
264
265   monitor = NULL;
266   if (type != G_TYPE_INVALID)
267     monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, NULL));
268   else
269     g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
270                          _("Unable to find default local directory monitor type"));
271
272   /* This is non-null on first pass here. Unref the class now.
273    * This is to avoid unloading the module and then loading it
274    * again which would happen if we unrefed the class
275    * before creating the monitor.
276    */
277   
278   if (type_class)
279     g_type_class_unref (type_class);
280   
281   return monitor;
282 }
283
284 static gboolean
285 g_local_directory_monitor_cancel (GFileMonitor *monitor)
286 {
287   GLocalDirectoryMonitor *local_monitor = G_LOCAL_DIRECTORY_MONITOR (monitor);
288
289   if (local_monitor->mount_monitor)
290     {
291       g_signal_handlers_disconnect_by_func (local_monitor->mount_monitor, mounts_changed, local_monitor);
292       g_object_unref (local_monitor->mount_monitor);
293       local_monitor->mount_monitor = NULL;
294     }
295
296   return TRUE;
297 }
298
299 #define __G_LOCAL_DIRECTORY_MONITOR_C__
300 #include "gioaliasdef.c"