Clarify docs for g_memory_output_stream_get_size. Add
[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   if (G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize)
62     (*G_OBJECT_CLASS (g_local_directory_monitor_parent_class)->finalize) (object);
63 }
64
65 static void
66 g_local_directory_monitor_set_property (GObject      *object,
67                                         guint         property_id,
68                                         const GValue *value,
69                                         GParamSpec   *pspec)
70 {
71   switch (property_id)
72   {
73     case PROP_DIRNAME:
74       /* Do nothing */
75       break;
76     default:
77       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78       break;
79   }
80 }
81
82 static GObject *
83 g_local_directory_monitor_constructor (GType                  type,
84                                        guint                  n_construct_properties,
85                                        GObjectConstructParam *construct_properties)
86 {
87   GObject *obj;
88   GLocalDirectoryMonitorClass *klass;
89   GObjectClass *parent_class;
90   GLocalDirectoryMonitor *local_monitor;
91   const gchar *dirname = NULL;
92   gint i;
93   
94   klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_type_class_peek (G_TYPE_LOCAL_DIRECTORY_MONITOR));
95   parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
96   obj = parent_class->constructor (type,
97                                    n_construct_properties,
98                                    construct_properties);
99
100   local_monitor = G_LOCAL_DIRECTORY_MONITOR (obj);
101
102   for (i = 0; i < n_construct_properties; i++)
103     {
104       if (strcmp ("dirname", g_param_spec_get_name (construct_properties[i].pspec)) == 0)
105         {
106           g_warn_if_fail (G_VALUE_HOLDS_STRING (construct_properties[i].value));
107           dirname = g_value_get_string (construct_properties[i].value);
108           break;
109         }
110     }
111
112   local_monitor->dirname = g_strdup (dirname);
113
114   if (!klass->mount_notify)
115     {
116 #ifdef G_OS_WIN32
117       g_warning ("G_OS_WIN32: no mount emulation");
118 #else
119       GUnixMountEntry *mount;
120       
121       /* Emulate unmount detection */
122       
123       mount = g_unix_mount_at (local_monitor->dirname, NULL);
124       
125       local_monitor->was_mounted = mount != NULL;
126       
127       if (mount)
128         g_unix_mount_free (mount);
129
130       local_monitor->mount_monitor = g_unix_mount_monitor_new ();
131       g_signal_connect_object (local_monitor->mount_monitor, "mounts_changed",
132                                G_CALLBACK (mounts_changed), local_monitor, 0);
133 #endif
134     }
135
136   return obj;
137 }
138
139 static void
140 g_local_directory_monitor_class_init (GLocalDirectoryMonitorClass* klass)
141 {
142   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
143   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
144   
145   gobject_class->finalize = g_local_directory_monitor_finalize;
146   gobject_class->set_property = g_local_directory_monitor_set_property;
147   gobject_class->constructor = g_local_directory_monitor_constructor;
148
149   file_monitor_class->cancel = g_local_directory_monitor_cancel;
150
151   g_object_class_install_property (gobject_class, 
152                                    PROP_DIRNAME,
153                                    g_param_spec_string ("dirname", 
154                                                         P_("Directory name"), 
155                                                         P_("Directory to monitor"),
156                                                         NULL, 
157                                                         G_PARAM_CONSTRUCT_ONLY|
158                                                         G_PARAM_WRITABLE|
159                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
160
161   klass->mount_notify = FALSE;
162 }
163
164 static void
165 g_local_directory_monitor_init (GLocalDirectoryMonitor *local_monitor)
166 {
167 }
168
169 static void
170 mounts_changed (GUnixMountMonitor *mount_monitor,
171                 gpointer           user_data)
172 {
173   GLocalDirectoryMonitor *local_monitor = user_data;
174   GUnixMountEntry *mount;
175   gboolean is_mounted;
176   GFile *file;
177   
178   /* Emulate unmount detection */
179 #ifdef G_OS_WIN32
180   mount = NULL;
181   g_warning ("G_OS_WIN32: no mount emulation");
182 #else  
183   mount = g_unix_mount_at (local_monitor->dirname, NULL);
184   
185   is_mounted = mount != NULL;
186   
187   if (mount)
188     g_unix_mount_free (mount);
189 #endif
190
191   if (local_monitor->was_mounted != is_mounted)
192     {
193       if (local_monitor->was_mounted && !is_mounted)
194         {
195           file = g_file_new_for_path (local_monitor->dirname);
196           g_file_monitor_emit_event (G_FILE_MONITOR (local_monitor),
197                                      file, NULL,
198                                      G_FILE_MONITOR_EVENT_UNMOUNTED);
199           g_object_unref (file);
200         }
201       local_monitor->was_mounted = is_mounted;
202     }
203 }
204
205 static gpointer
206 get_default_local_directory_monitor (gpointer data)
207 {
208   GLocalDirectoryMonitorClass *chosen_class;
209   GLocalDirectoryMonitorClass **ret = data;
210   GIOExtensionPoint *ep;
211   GList *extensions, *l;
212
213   _g_io_modules_ensure_loaded ();
214
215   ep = g_io_extension_point_lookup (G_LOCAL_DIRECTORY_MONITOR_EXTENSION_POINT_NAME);
216
217   extensions = g_io_extension_point_get_extensions (ep);
218   
219   chosen_class = NULL;
220   for (l = extensions; l != NULL; l = l->next)
221     {
222       GIOExtension *extension = l->data;
223       GLocalDirectoryMonitorClass *klass;
224       
225       klass = G_LOCAL_DIRECTORY_MONITOR_CLASS (g_io_extension_ref_class (extension));
226       
227       if (klass->is_supported ())
228         {
229           chosen_class = klass;
230           break;
231         }
232       else
233         g_type_class_unref (klass);
234     }
235   
236   if (chosen_class)
237     {
238       *ret = chosen_class;
239       return (gpointer)G_TYPE_FROM_CLASS (chosen_class);
240     }
241   else
242     return (gpointer)G_TYPE_INVALID;
243 }
244
245 /**
246  * _g_local_directory_monitor_new:
247  * @dirname: filename of the directory to monitor.
248  * @flags: #GFileMonitorFlags.
249  * 
250  * Returns: new #GFileMonitor for the given @dirname.
251  **/
252 GFileMonitor*
253 _g_local_directory_monitor_new (const char         *dirname,
254                                 GFileMonitorFlags   flags,
255                                 GError            **error)
256 {
257   static GOnce once_init = G_ONCE_INIT;
258   GTypeClass *type_class;
259   GFileMonitor *monitor;
260   GType type;
261
262   type_class = NULL;
263   g_once (&once_init, get_default_local_directory_monitor, &type_class);
264   type = (GType)once_init.retval;
265
266   monitor = NULL;
267   if (type != G_TYPE_INVALID)
268     monitor = G_FILE_MONITOR (g_object_new (type, "dirname", dirname, NULL));
269   else
270     g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("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"