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