Add new error codes for when compilation fails and make compilation error
[platform/upstream/glib.git] / gio / gdirectorymonitor.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 #include <string.h>
25
26 #include "gdirectorymonitor.h"
27 #include "gio-marshal.h"
28 #include "gfile.h"
29 #include "gvfs.h"
30 #include "glibintl.h"
31
32 #include "gioalias.h"
33
34 /**
35  * SECTION:gdirectorymonitor
36  * @short_description: Directory Monitor
37  * @see_also: #GFileMonitor
38  * 
39  * Monitors a directory for changes.
40  *
41  **/
42
43 enum {
44   CHANGED,
45   LAST_SIGNAL
46 };
47
48 G_DEFINE_ABSTRACT_TYPE (GDirectoryMonitor, g_directory_monitor, G_TYPE_OBJECT);
49
50 typedef struct {
51   GFile *file;
52   guint32 last_sent_change_time; /* 0 == not sent */
53   guint32 send_delayed_change_at; /* 0 == never */
54   guint32 send_virtual_changes_done_at; /* 0 == never */
55 } RateLimiter;
56
57 struct _GDirectoryMonitorPrivate {
58   gboolean cancelled;
59   int rate_limit_msec;
60
61   GHashTable *rate_limiter;
62   
63   GSource *timeout;
64   guint32 timeout_fires_at;
65 };
66
67 enum {
68   PROP_0,
69   PROP_RATE_LIMIT,
70   PROP_CANCELLED
71 };
72
73 #define DEFAULT_RATE_LIMIT_MSECS 800
74 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
75
76 static guint signals[LAST_SIGNAL] = { 0 };
77
78 static void
79 rate_limiter_free (RateLimiter *limiter)
80 {
81   g_object_unref (limiter->file);
82   g_free (limiter);
83 }
84
85 static void
86 g_directory_monitor_set_property (GObject      *object,
87                                   guint         prop_id,
88                                   const GValue *value,
89                                   GParamSpec   *pspec)
90 {
91   GDirectoryMonitor *monitor;
92
93   monitor = G_DIRECTORY_MONITOR (object);
94
95   switch (prop_id)
96     {
97     case PROP_RATE_LIMIT:
98       g_directory_monitor_set_rate_limit (monitor, g_value_get_int (value));
99       break;
100
101     default:
102       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103       break;
104     }
105 }
106
107 static void
108 g_directory_monitor_get_property (GObject    *object,
109                                   guint       prop_id,
110                                   GValue     *value,
111                                   GParamSpec *pspec)
112 {
113   GDirectoryMonitor *monitor;
114   GDirectoryMonitorPrivate *priv;
115
116   monitor = G_DIRECTORY_MONITOR (object);
117   priv = monitor->priv;
118
119   switch (prop_id)
120     {
121     case PROP_RATE_LIMIT:
122       g_value_set_int (value, priv->rate_limit_msec);
123       break;
124
125     case PROP_CANCELLED:
126       g_value_set_boolean (value, priv->cancelled);
127       break;
128
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132     }
133 }
134
135 static void
136 g_directory_monitor_finalize (GObject *object)
137 {
138   GDirectoryMonitor *monitor;
139
140   monitor = G_DIRECTORY_MONITOR (object);
141
142   if (monitor->priv->timeout)
143     {
144       g_source_destroy (monitor->priv->timeout);
145       g_source_unref (monitor->priv->timeout);
146     }
147
148   g_hash_table_destroy (monitor->priv->rate_limiter);
149   
150   if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize)
151     (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize) (object);
152 }
153
154 static void
155 g_directory_monitor_dispose (GObject *object)
156 {
157   GDirectoryMonitor *monitor;
158   
159   monitor = G_DIRECTORY_MONITOR (object);
160
161   /* Make sure we cancel on last unref */
162   if (!monitor->priv->cancelled)
163     g_directory_monitor_cancel (monitor);
164   
165   if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose)
166     (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose) (object);
167 }
168
169 static void
170 g_directory_monitor_class_init (GDirectoryMonitorClass *klass)
171 {
172   GObjectClass *object_class;
173   
174   g_type_class_add_private (klass, sizeof (GDirectoryMonitorPrivate));
175   
176   object_class = G_OBJECT_CLASS (klass);
177   object_class->finalize = g_directory_monitor_finalize;
178   object_class->dispose = g_directory_monitor_dispose;
179   object_class->get_property = g_directory_monitor_get_property;
180   object_class->set_property = g_directory_monitor_set_property;
181
182   /**
183    * GDirectoryMonitor::changed:
184    * @monitor: the #GDirectoryMonitor
185    * @child: the #GFile which changed
186    * @other_file: the other #GFile which changed
187    * @event_type: a #GFileMonitorEvent indicating what the event was
188    *
189    * Emitted when a child file changes.
190    */
191   signals[CHANGED] =
192     g_signal_new (I_("changed"),
193                   G_TYPE_DIRECTORY_MONITOR,
194                   G_SIGNAL_RUN_LAST,
195                   G_STRUCT_OFFSET (GDirectoryMonitorClass, changed),
196                   NULL, NULL,
197                   _gio_marshal_VOID__OBJECT_OBJECT_INT,
198                   G_TYPE_NONE, 3,
199                   G_TYPE_FILE, G_TYPE_FILE, G_TYPE_INT);
200
201   g_object_class_install_property (object_class,
202                                    PROP_RATE_LIMIT,
203                                    g_param_spec_int ("rate-limit",
204                                                      P_("Rate limit"),
205                                                      P_("The limit of the monitor to watch for changes, in milliseconds"),
206                                                      0, G_MAXINT,
207                                                      DEFAULT_RATE_LIMIT_MSECS,
208                                                      G_PARAM_READWRITE|
209                                                      G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
210
211   g_object_class_install_property (object_class,
212                                    PROP_CANCELLED,
213                                    g_param_spec_boolean ("cancelled",
214                                                          P_("Cancelled"),
215                                                          P_("Whether the monitor has been cancelled"),
216                                                          FALSE,
217                                                          G_PARAM_READABLE|
218                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
219
220 }
221
222 static void
223 g_directory_monitor_init (GDirectoryMonitor *monitor)
224 {
225   monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
226                                                G_TYPE_DIRECTORY_MONITOR,
227                                                GDirectoryMonitorPrivate);
228
229   monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
230   monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
231                                                        NULL, (GDestroyNotify) rate_limiter_free);
232 }
233
234
235 /**
236  * g_directory_monitor_cancel:
237  * @monitor: a #GDirectoryMonitor.
238  *
239  * Cancels the monitoring activity on @monitor.
240  * 
241  * Returns: %TRUE if the monitor was cancelled successfully. %FALSE otherwise.
242  **/
243 gboolean
244 g_directory_monitor_cancel (GDirectoryMonitor *monitor)
245 {
246   GDirectoryMonitorClass *class;
247
248   g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
249   
250   if (monitor->priv->cancelled)
251     return TRUE;
252   
253   monitor->priv->cancelled = TRUE;
254   g_object_notify (G_OBJECT (monitor), "cancelled");
255   
256   class = G_DIRECTORY_MONITOR_GET_CLASS (monitor);
257   return (* class->cancel) (monitor);
258 }
259
260 /**
261  * g_directory_monitor_set_rate_limit:
262  * @monitor: a #GDirectoryMonitor.
263  * @limit_msecs: an integer to set the limit of the directory monitor 
264  *     in milliseconds.
265  *
266  * Sets the limit of the directory monitor to watch for changes every 
267  * @limit_msecs milliseconds.
268  **/
269 void
270 g_directory_monitor_set_rate_limit (GDirectoryMonitor *monitor,
271                                     int                limit_msecs)
272 {
273   GDirectoryMonitorPrivate *priv;
274   g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
275   priv = monitor->priv;
276   if (priv->rate_limit_msec != limit_msecs)
277     {
278       monitor->priv->rate_limit_msec = limit_msecs;
279       g_object_notify (G_OBJECT (monitor), "rate-limit");
280     }
281 }
282
283 /**
284  * g_directory_monitor_is_cancelled:
285  * @monitor: a #GDirectoryMonitor.
286  * 
287  * Checks whether @monitor is cancelled.
288  *
289  * Returns: %TRUE if the monitor on the directory was cancelled. 
290  *     %FALSE otherwise.
291  **/
292 gboolean
293 g_directory_monitor_is_cancelled (GDirectoryMonitor *monitor)
294 {
295   g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
296
297   return monitor->priv->cancelled;
298 }
299
300 static guint32
301 get_time_msecs (void)
302 {
303   return g_thread_gettime() / (1000 * 1000);
304 }
305
306 static guint32
307 time_difference (guint32 from, guint32 to)
308 {
309   if (from > to)
310     return 0;
311   return to - from;
312 }
313
314 static RateLimiter *
315 new_limiter (GDirectoryMonitor *monitor,
316              GFile             *file)
317 {
318   RateLimiter *limiter;
319
320   limiter = g_new0 (RateLimiter, 1);
321   limiter->file = g_object_ref (file);
322   g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
323   
324   return limiter;
325 }
326
327 static void
328 rate_limiter_send_virtual_changes_done_now (GDirectoryMonitor *monitor, 
329                                             RateLimiter       *limiter)
330 {
331   if (limiter->send_virtual_changes_done_at != 0)
332     {
333       g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
334       limiter->send_virtual_changes_done_at = 0;
335     }
336 }
337
338 static void
339 rate_limiter_send_delayed_change_now (GDirectoryMonitor *monitor, 
340                                       RateLimiter       *limiter, 
341                                       guint32            time_now)
342 {
343   if (limiter->send_delayed_change_at != 0)
344     {
345       g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGED);
346       limiter->send_delayed_change_at = 0;
347       limiter->last_sent_change_time = time_now;
348     }
349 }
350
351 typedef struct {
352   guint32 min_time;
353   guint32 time_now;
354   GDirectoryMonitor *monitor;
355 } ForEachData;
356
357 static gboolean
358 calc_min_time (GDirectoryMonitor *monitor, 
359                RateLimiter       *limiter, 
360                guint32            time_now, 
361                guint32           *min_time)
362 {
363   gboolean delete_me;
364   guint32 expire_at;
365
366   delete_me = TRUE;
367
368   if (limiter->last_sent_change_time != 0)
369     {
370       /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventualy */
371       expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
372
373       if (time_difference (time_now, expire_at) > 0)
374         {
375           delete_me = FALSE;
376           *min_time = MIN (*min_time,
377                            time_difference (time_now, expire_at));
378         }
379     }
380
381   if (limiter->send_delayed_change_at != 0)
382     {
383       delete_me = FALSE;
384       *min_time = MIN (*min_time,
385                        time_difference (time_now, limiter->send_delayed_change_at));
386     }
387
388   if (limiter->send_virtual_changes_done_at != 0)
389     {
390       delete_me = FALSE;
391       *min_time = MIN (*min_time,
392                        time_difference (time_now, limiter->send_virtual_changes_done_at));
393     }
394
395   return delete_me;
396 }
397
398 static gboolean
399 foreach_rate_limiter_fire (gpointer key,
400                            gpointer value,
401                            gpointer user_data)
402 {
403   RateLimiter *limiter = value;
404   ForEachData *data = user_data;
405
406   if (limiter->send_delayed_change_at != 0 &&
407       time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
408     rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
409
410   if (limiter->send_virtual_changes_done_at != 0 &&
411       time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
412     rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
413
414   return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
415 }
416
417 static gboolean 
418 rate_limiter_timeout (gpointer timeout_data)
419 {
420   GDirectoryMonitor *monitor = timeout_data;
421   ForEachData data;
422   GSource *source;
423   
424   data.min_time = G_MAXUINT32;
425   data.monitor = monitor;
426   data.time_now = get_time_msecs ();
427   g_hash_table_foreach_remove (monitor->priv->rate_limiter,
428                                foreach_rate_limiter_fire,
429                                &data);
430
431   /* Remove old timeout */
432   if (monitor->priv->timeout)
433     {
434       g_source_destroy (monitor->priv->timeout);
435       g_source_unref (monitor->priv->timeout);
436       monitor->priv->timeout = NULL;
437       monitor->priv->timeout_fires_at = 0;
438     }
439   
440   /* Set up new timeout */
441   if (data.min_time != G_MAXUINT32)
442     {
443       source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
444       g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
445       g_source_attach (source, NULL);
446       
447       monitor->priv->timeout = source;
448       monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
449     }
450   
451   return FALSE;
452 }
453
454 static gboolean
455 foreach_rate_limiter_update (gpointer key,
456                              gpointer value,
457                              gpointer user_data)
458 {
459   RateLimiter *limiter = value;
460   ForEachData *data = user_data;
461
462   return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
463 }
464
465 static void
466 update_rate_limiter_timeout (GDirectoryMonitor *monitor, 
467                              guint              new_time)
468 {
469   ForEachData data;
470   GSource *source;
471   
472   if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
473       time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
474     return; /* Nothing to do, we already fire earlier than that */
475
476   data.min_time = G_MAXUINT32;
477   data.monitor = monitor;
478   data.time_now = get_time_msecs ();
479   g_hash_table_foreach_remove (monitor->priv->rate_limiter,
480                                foreach_rate_limiter_update,
481                                &data);
482
483   /* Remove old timeout */
484   if (monitor->priv->timeout)
485     {
486       g_source_destroy (monitor->priv->timeout);
487       g_source_unref (monitor->priv->timeout);
488       monitor->priv->timeout_fires_at = 0;
489       monitor->priv->timeout = NULL;
490     }
491
492   /* Set up new timeout */
493   if (data.min_time != G_MAXUINT32)
494     {
495       source = g_timeout_source_new (data.min_time + 1);  /* + 1 to make sure we've really passed the time */
496       g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
497       g_source_attach (source, NULL);
498       
499       monitor->priv->timeout = source;
500       monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
501     }
502 }
503
504 /**
505  * g_directory_monitor_emit_event:
506  * @monitor: a #GDirectoryMonitor.
507  * @child: a #GFile.
508  * @other_file: a #GFile.
509  * @event_type: a set of #GFileMonitorEvent flags.
510  * 
511  * Emits the #GDirectoryMonitor::changed signal if a change
512  * has taken place. Should be called from directory monitor 
513  * implementations only.
514  **/
515 void
516 g_directory_monitor_emit_event (GDirectoryMonitor *monitor,
517                                 GFile             *child,
518                                 GFile             *other_file,
519                                 GFileMonitorEvent  event_type)
520 {
521   guint32 time_now, since_last;
522   gboolean emit_now;
523   RateLimiter *limiter;
524
525   g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
526   g_return_if_fail (G_IS_FILE (child));
527
528   limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
529
530   if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
531     {
532       if (limiter)
533         {
534           rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
535           if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
536             limiter->send_virtual_changes_done_at = 0;
537           else
538             rate_limiter_send_virtual_changes_done_now (monitor, limiter);
539           update_rate_limiter_timeout (monitor, 0);
540         }
541       g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
542     }
543   else
544     {
545       /* Changed event, rate limit */
546       time_now = get_time_msecs ();
547       emit_now = TRUE;
548       
549       if (limiter)
550         {
551           since_last = time_difference (limiter->last_sent_change_time, time_now);
552           if (since_last < monitor->priv->rate_limit_msec)
553             {
554               /* We ignore this change, but arm a timer so that we can fire it later if we
555                  don't get any other events (that kill this timeout) */
556               emit_now = FALSE;
557               if (limiter->send_delayed_change_at == 0)
558                 {
559                   limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
560                   update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
561                 }
562             }
563         }
564
565       if (limiter == NULL)
566         limiter = new_limiter (monitor, child);
567       
568       if (emit_now)
569         {
570           g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
571
572           limiter->last_sent_change_time = time_now;
573           limiter->send_delayed_change_at = 0;
574           /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventualy */
575           update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
576         }
577
578       /* Schedule a virtual change done. This is removed if we get a real one, and
579          postponed if we get more change events. */
580
581       limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
582       update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
583     }
584 }
585
586 #define __G_DIRECTORY_MONITOR_C__
587 #include "gioaliasdef.c"