Coding style fixups
[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 #define DEFAULT_RATE_LIMIT_MSECS 800
68 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
69
70 static guint signals[LAST_SIGNAL] = { 0 };
71
72 static void
73 rate_limiter_free (RateLimiter *limiter)
74 {
75   g_object_unref (limiter->file);
76   g_free (limiter);
77 }
78
79 static void
80 g_directory_monitor_finalize (GObject *object)
81 {
82   GDirectoryMonitor *monitor;
83
84   monitor = G_DIRECTORY_MONITOR (object);
85
86   if (monitor->priv->timeout)
87     {
88       g_source_destroy (monitor->priv->timeout);
89       g_source_unref (monitor->priv->timeout);
90     }
91
92   g_hash_table_destroy (monitor->priv->rate_limiter);
93   
94   if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize)
95     (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize) (object);
96 }
97
98 static void
99 g_directory_monitor_dispose (GObject *object)
100 {
101   GDirectoryMonitor *monitor;
102   
103   monitor = G_DIRECTORY_MONITOR (object);
104
105   /* Make sure we cancel on last unref */
106   if (!monitor->priv->cancelled)
107     g_directory_monitor_cancel (monitor);
108   
109   if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose)
110     (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose) (object);
111 }
112
113 static void
114 g_directory_monitor_class_init (GDirectoryMonitorClass *klass)
115 {
116   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117   
118   g_type_class_add_private (klass, sizeof (GDirectoryMonitorPrivate));
119   
120   gobject_class->finalize = g_directory_monitor_finalize;
121   gobject_class->dispose = g_directory_monitor_dispose;
122
123   /**
124    * GDirectoryMonitor::changed:
125    * @monitor: the #GDirectoryMonitor
126    * @child: the #GFile which changed
127    * @other_file: the other #GFile which changed
128    * @event_type: a #GFileMonitorEvent indicating what the event was
129    *
130    * Emitted when a child file changes.
131    */
132   signals[CHANGED] =
133     g_signal_new (I_("changed"),
134                   G_TYPE_DIRECTORY_MONITOR,
135                   G_SIGNAL_RUN_LAST,
136                   G_STRUCT_OFFSET (GDirectoryMonitorClass, changed),
137                   NULL, NULL,
138                   _gio_marshal_VOID__OBJECT_OBJECT_INT,
139                   G_TYPE_NONE,3,
140                   G_TYPE_FILE,
141                   G_TYPE_FILE,
142                   G_TYPE_INT);
143 }
144
145 static void
146 g_directory_monitor_init (GDirectoryMonitor *monitor)
147 {
148   monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
149                                                G_TYPE_DIRECTORY_MONITOR,
150                                                GDirectoryMonitorPrivate);
151
152   monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
153   monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
154                                                        NULL, (GDestroyNotify) rate_limiter_free);
155 }
156
157
158 /**
159  * g_directory_monitor_cancel:
160  * @monitor: a #GDirectoryMonitor.
161  *
162  * Cancels the monitoring activity on @monitor.
163  * 
164  * Returns: %TRUE if the monitor was cancelled successfully. %FALSE otherwise.
165  **/
166 gboolean
167 g_directory_monitor_cancel (GDirectoryMonitor *monitor)
168 {
169   GDirectoryMonitorClass *class;
170
171   g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
172   
173   if (monitor->priv->cancelled)
174     return TRUE;
175   
176   monitor->priv->cancelled = TRUE;
177   
178   class = G_DIRECTORY_MONITOR_GET_CLASS (monitor);
179   return (* class->cancel) (monitor);
180 }
181
182 /**
183  * g_directory_monitor_set_rate_limit:
184  * @monitor: a #GDirectoryMonitor.
185  * @limit_msecs: an integer to set the limit of the directory monitor 
186  *     in milliseconds.
187  *
188  * Sets the limit of the directory monitor to watch for changes every 
189  * @limit_msecs milliseconds.
190  **/
191 void
192 g_directory_monitor_set_rate_limit (GDirectoryMonitor *monitor,
193                                     int                limit_msecs)
194 {
195   g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
196
197   monitor->priv->rate_limit_msec = limit_msecs;
198 }
199
200 /**
201  * g_directory_monitor_is_cancelled:
202  * @monitor: a #GDirectoryMonitor.
203  * 
204  * Checks whether @monitor is cancelled.
205  *
206  * Returns: %TRUE if the monitor on the directory was cancelled. 
207  *     %FALSE otherwise.
208  **/
209 gboolean
210 g_directory_monitor_is_cancelled (GDirectoryMonitor *monitor)
211 {
212   g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
213
214   return monitor->priv->cancelled;
215 }
216
217 static guint32
218 get_time_msecs (void)
219 {
220   return g_thread_gettime() / (1000 * 1000);
221 }
222
223 static guint32
224 time_difference (guint32 from, guint32 to)
225 {
226   if (from > to)
227     return 0;
228   return to - from;
229 }
230
231 static RateLimiter *
232 new_limiter (GDirectoryMonitor *monitor,
233              GFile             *file)
234 {
235   RateLimiter *limiter;
236
237   limiter = g_new0 (RateLimiter, 1);
238   limiter->file = g_object_ref (file);
239   g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
240   
241   return limiter;
242 }
243
244 static void
245 rate_limiter_send_virtual_changes_done_now (GDirectoryMonitor *monitor, 
246                                             RateLimiter       *limiter)
247 {
248   if (limiter->send_virtual_changes_done_at != 0)
249     {
250       g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
251       limiter->send_virtual_changes_done_at = 0;
252     }
253 }
254
255 static void
256 rate_limiter_send_delayed_change_now (GDirectoryMonitor *monitor, 
257                                       RateLimiter       *limiter, 
258                                       guint32            time_now)
259 {
260   if (limiter->send_delayed_change_at != 0)
261     {
262       g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGED);
263       limiter->send_delayed_change_at = 0;
264       limiter->last_sent_change_time = time_now;
265     }
266 }
267
268 typedef struct {
269   guint32 min_time;
270   guint32 time_now;
271   GDirectoryMonitor *monitor;
272 } ForEachData;
273
274 static gboolean
275 calc_min_time (GDirectoryMonitor *monitor, 
276                RateLimiter       *limiter, 
277                guint32            time_now, 
278                guint32           *min_time)
279 {
280   gboolean delete_me;
281   guint32 expire_at;
282
283   delete_me = TRUE;
284
285   if (limiter->last_sent_change_time != 0)
286     {
287       /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventualy */
288       expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
289
290       if (time_difference (time_now, expire_at) > 0)
291         {
292           delete_me = FALSE;
293           *min_time = MIN (*min_time,
294                            time_difference (time_now, expire_at));
295         }
296     }
297
298   if (limiter->send_delayed_change_at != 0)
299     {
300       delete_me = FALSE;
301       *min_time = MIN (*min_time,
302                        time_difference (time_now, limiter->send_delayed_change_at));
303     }
304
305   if (limiter->send_virtual_changes_done_at != 0)
306     {
307       delete_me = FALSE;
308       *min_time = MIN (*min_time,
309                        time_difference (time_now, limiter->send_virtual_changes_done_at));
310     }
311
312   return delete_me;
313 }
314
315 static gboolean
316 foreach_rate_limiter_fire (gpointer key,
317                            gpointer value,
318                            gpointer user_data)
319 {
320   RateLimiter *limiter = value;
321   ForEachData *data = user_data;
322
323   if (limiter->send_delayed_change_at != 0 &&
324       time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
325     rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
326
327   if (limiter->send_virtual_changes_done_at != 0 &&
328       time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
329     rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
330
331   return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
332 }
333
334 static gboolean 
335 rate_limiter_timeout (gpointer timeout_data)
336 {
337   GDirectoryMonitor *monitor = timeout_data;
338   ForEachData data;
339   GSource *source;
340   
341   data.min_time = G_MAXUINT32;
342   data.monitor = monitor;
343   data.time_now = get_time_msecs ();
344   g_hash_table_foreach_remove (monitor->priv->rate_limiter,
345                                foreach_rate_limiter_fire,
346                                &data);
347
348   /* Remove old timeout */
349   if (monitor->priv->timeout)
350     {
351       g_source_destroy (monitor->priv->timeout);
352       g_source_unref (monitor->priv->timeout);
353       monitor->priv->timeout = NULL;
354       monitor->priv->timeout_fires_at = 0;
355     }
356   
357   /* Set up new timeout */
358   if (data.min_time != G_MAXUINT32)
359     {
360       source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
361       g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
362       g_source_attach (source, NULL);
363       
364       monitor->priv->timeout = source;
365       monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
366     }
367   
368   return FALSE;
369 }
370
371 static gboolean
372 foreach_rate_limiter_update (gpointer key,
373                              gpointer value,
374                              gpointer user_data)
375 {
376   RateLimiter *limiter = value;
377   ForEachData *data = user_data;
378
379   return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
380 }
381
382 static void
383 update_rate_limiter_timeout (GDirectoryMonitor *monitor, 
384                              guint              new_time)
385 {
386   ForEachData data;
387   GSource *source;
388   
389   if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
390       time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
391     return; /* Nothing to do, we already fire earlier than that */
392
393   data.min_time = G_MAXUINT32;
394   data.monitor = monitor;
395   data.time_now = get_time_msecs ();
396   g_hash_table_foreach_remove (monitor->priv->rate_limiter,
397                                foreach_rate_limiter_update,
398                                &data);
399
400   /* Remove old timeout */
401   if (monitor->priv->timeout)
402     {
403       g_source_destroy (monitor->priv->timeout);
404       g_source_unref (monitor->priv->timeout);
405       monitor->priv->timeout_fires_at = 0;
406       monitor->priv->timeout = NULL;
407     }
408
409   /* Set up new timeout */
410   if (data.min_time != G_MAXUINT32)
411     {
412       source = g_timeout_source_new (data.min_time + 1);  /* + 1 to make sure we've really passed the time */
413       g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
414       g_source_attach (source, NULL);
415       
416       monitor->priv->timeout = source;
417       monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
418     }
419 }
420
421 /**
422  * g_directory_monitor_emit_event:
423  * @monitor: a #GDirectoryMonitor.
424  * @child: a #GFile.
425  * @other_file: a #GFile.
426  * @event_type: a set of #GFileMonitorEvent flags.
427  * 
428  * Emits the #GDirectoryMonitor::changed signal if a change
429  * has taken place. Should be called from directory monitor 
430  * implementations only.
431  **/
432 void
433 g_directory_monitor_emit_event (GDirectoryMonitor *monitor,
434                                 GFile             *child,
435                                 GFile             *other_file,
436                                 GFileMonitorEvent  event_type)
437 {
438   guint32 time_now, since_last;
439   gboolean emit_now;
440   RateLimiter *limiter;
441
442   g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
443   g_return_if_fail (G_IS_FILE (child));
444
445   limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
446
447   if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
448     {
449       if (limiter)
450         {
451           rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
452           if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
453             limiter->send_virtual_changes_done_at = 0;
454           else
455             rate_limiter_send_virtual_changes_done_now (monitor, limiter);
456           update_rate_limiter_timeout (monitor, 0);
457         }
458       g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
459     }
460   else
461     {
462       /* Changed event, rate limit */
463       time_now = get_time_msecs ();
464       emit_now = TRUE;
465       
466       if (limiter)
467         {
468           since_last = time_difference (limiter->last_sent_change_time, time_now);
469           if (since_last < monitor->priv->rate_limit_msec)
470             {
471               /* We ignore this change, but arm a timer so that we can fire it later if we
472                  don't get any other events (that kill this timeout) */
473               emit_now = FALSE;
474               if (limiter->send_delayed_change_at == 0)
475                 {
476                   limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
477                   update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
478                 }
479             }
480         }
481
482       if (limiter == NULL)
483         limiter = new_limiter (monitor, child);
484       
485       if (emit_now)
486         {
487           g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
488
489           limiter->last_sent_change_time = time_now;
490           limiter->send_delayed_change_at = 0;
491           /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventualy */
492           update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
493         }
494
495       /* Schedule a virtual change done. This is removed if we get a real one, and
496          postponed if we get more change events. */
497
498       limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
499       update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
500     }
501 }
502
503 #define __G_DIRECTORY_MONITOR_C__
504 #include "gioaliasdef.c"