Fix AKS -> ASK typo
[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 to files in it.
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. Note that
240  * the monitor is automatically cancelled when finalized.
241  *
242  * It is safe to call this multiple times.
243  * 
244  * Returns: %TRUE if the monitor was cancelled successfully. %FALSE otherwise.
245  **/
246 gboolean
247 g_directory_monitor_cancel (GDirectoryMonitor *monitor)
248 {
249   GDirectoryMonitorClass *class;
250
251   g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
252   
253   if (monitor->priv->cancelled)
254     return TRUE;
255   
256   monitor->priv->cancelled = TRUE;
257   g_object_notify (G_OBJECT (monitor), "cancelled");
258   
259   class = G_DIRECTORY_MONITOR_GET_CLASS (monitor);
260   return (* class->cancel) (monitor);
261 }
262
263 /**
264  * g_directory_monitor_set_rate_limit:
265  * @monitor: a #GDirectoryMonitor.
266  * @limit_msecs: the change rate limit of the directory monitor in milliseconds.
267  *
268  * Report same consecutive changes of the same type at most once each @limit_msecs milliseconds.
269  **/
270 void
271 g_directory_monitor_set_rate_limit (GDirectoryMonitor *monitor,
272                                     int                limit_msecs)
273 {
274   GDirectoryMonitorPrivate *priv;
275   g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
276   priv = monitor->priv;
277   if (priv->rate_limit_msec != limit_msecs)
278     {
279       monitor->priv->rate_limit_msec = limit_msecs;
280       g_object_notify (G_OBJECT (monitor), "rate-limit");
281     }
282 }
283
284 /**
285  * g_directory_monitor_is_cancelled:
286  * @monitor: a #GDirectoryMonitor.
287  * 
288  * Checks whether @monitor is cancelled.
289  *
290  * Returns: %TRUE if the monitor on the directory was cancelled. 
291  *     %FALSE otherwise.
292  **/
293 gboolean
294 g_directory_monitor_is_cancelled (GDirectoryMonitor *monitor)
295 {
296   g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
297
298   return monitor->priv->cancelled;
299 }
300
301 static guint32
302 get_time_msecs (void)
303 {
304   return g_thread_gettime() / (1000 * 1000);
305 }
306
307 static guint32
308 time_difference (guint32 from, guint32 to)
309 {
310   if (from > to)
311     return 0;
312   return to - from;
313 }
314
315 static RateLimiter *
316 new_limiter (GDirectoryMonitor *monitor,
317              GFile             *file)
318 {
319   RateLimiter *limiter;
320
321   limiter = g_new0 (RateLimiter, 1);
322   limiter->file = g_object_ref (file);
323   g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
324   
325   return limiter;
326 }
327
328 static void
329 rate_limiter_send_virtual_changes_done_now (GDirectoryMonitor *monitor, 
330                                             RateLimiter       *limiter)
331 {
332   if (limiter->send_virtual_changes_done_at != 0)
333     {
334       g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
335       limiter->send_virtual_changes_done_at = 0;
336     }
337 }
338
339 static void
340 rate_limiter_send_delayed_change_now (GDirectoryMonitor *monitor, 
341                                       RateLimiter       *limiter, 
342                                       guint32            time_now)
343 {
344   if (limiter->send_delayed_change_at != 0)
345     {
346       g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGED);
347       limiter->send_delayed_change_at = 0;
348       limiter->last_sent_change_time = time_now;
349     }
350 }
351
352 typedef struct {
353   guint32 min_time;
354   guint32 time_now;
355   GDirectoryMonitor *monitor;
356 } ForEachData;
357
358 static gboolean
359 calc_min_time (GDirectoryMonitor *monitor, 
360                RateLimiter       *limiter, 
361                guint32            time_now, 
362                guint32           *min_time)
363 {
364   gboolean delete_me;
365   guint32 expire_at;
366
367   delete_me = TRUE;
368
369   if (limiter->last_sent_change_time != 0)
370     {
371       /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventualy */
372       expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
373
374       if (time_difference (time_now, expire_at) > 0)
375         {
376           delete_me = FALSE;
377           *min_time = MIN (*min_time,
378                            time_difference (time_now, expire_at));
379         }
380     }
381
382   if (limiter->send_delayed_change_at != 0)
383     {
384       delete_me = FALSE;
385       *min_time = MIN (*min_time,
386                        time_difference (time_now, limiter->send_delayed_change_at));
387     }
388
389   if (limiter->send_virtual_changes_done_at != 0)
390     {
391       delete_me = FALSE;
392       *min_time = MIN (*min_time,
393                        time_difference (time_now, limiter->send_virtual_changes_done_at));
394     }
395
396   return delete_me;
397 }
398
399 static gboolean
400 foreach_rate_limiter_fire (gpointer key,
401                            gpointer value,
402                            gpointer user_data)
403 {
404   RateLimiter *limiter = value;
405   ForEachData *data = user_data;
406
407   if (limiter->send_delayed_change_at != 0 &&
408       time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
409     rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
410
411   if (limiter->send_virtual_changes_done_at != 0 &&
412       time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
413     rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
414
415   return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
416 }
417
418 static gboolean 
419 rate_limiter_timeout (gpointer timeout_data)
420 {
421   GDirectoryMonitor *monitor = timeout_data;
422   ForEachData data;
423   GSource *source;
424   
425   data.min_time = G_MAXUINT32;
426   data.monitor = monitor;
427   data.time_now = get_time_msecs ();
428   g_hash_table_foreach_remove (monitor->priv->rate_limiter,
429                                foreach_rate_limiter_fire,
430                                &data);
431
432   /* Remove old timeout */
433   if (monitor->priv->timeout)
434     {
435       g_source_destroy (monitor->priv->timeout);
436       g_source_unref (monitor->priv->timeout);
437       monitor->priv->timeout = NULL;
438       monitor->priv->timeout_fires_at = 0;
439     }
440   
441   /* Set up new timeout */
442   if (data.min_time != G_MAXUINT32)
443     {
444       source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
445       g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
446       g_source_attach (source, NULL);
447       
448       monitor->priv->timeout = source;
449       monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
450     }
451   
452   return FALSE;
453 }
454
455 static gboolean
456 foreach_rate_limiter_update (gpointer key,
457                              gpointer value,
458                              gpointer user_data)
459 {
460   RateLimiter *limiter = value;
461   ForEachData *data = user_data;
462
463   return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
464 }
465
466 static void
467 update_rate_limiter_timeout (GDirectoryMonitor *monitor, 
468                              guint              new_time)
469 {
470   ForEachData data;
471   GSource *source;
472   
473   if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
474       time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
475     return; /* Nothing to do, we already fire earlier than that */
476
477   data.min_time = G_MAXUINT32;
478   data.monitor = monitor;
479   data.time_now = get_time_msecs ();
480   g_hash_table_foreach_remove (monitor->priv->rate_limiter,
481                                foreach_rate_limiter_update,
482                                &data);
483
484   /* Remove old timeout */
485   if (monitor->priv->timeout)
486     {
487       g_source_destroy (monitor->priv->timeout);
488       g_source_unref (monitor->priv->timeout);
489       monitor->priv->timeout_fires_at = 0;
490       monitor->priv->timeout = NULL;
491     }
492
493   /* Set up new timeout */
494   if (data.min_time != G_MAXUINT32)
495     {
496       source = g_timeout_source_new (data.min_time + 1);  /* + 1 to make sure we've really passed the time */
497       g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
498       g_source_attach (source, NULL);
499       
500       monitor->priv->timeout = source;
501       monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
502     }
503 }
504
505 /**
506  * g_directory_monitor_emit_event:
507  * @monitor: a #GDirectoryMonitor.
508  * @child: a #GFile.
509  * @other_file: a #GFile.
510  * @event_type: a set of #GFileMonitorEvent flags.
511  * 
512  * Emits the #GDirectoryMonitor::changed signal if a change
513  * has taken place. Should be called from directory monitor 
514  * implementations only.
515  **/
516 void
517 g_directory_monitor_emit_event (GDirectoryMonitor *monitor,
518                                 GFile             *child,
519                                 GFile             *other_file,
520                                 GFileMonitorEvent  event_type)
521 {
522   guint32 time_now, since_last;
523   gboolean emit_now;
524   RateLimiter *limiter;
525
526   g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
527   g_return_if_fail (G_IS_FILE (child));
528
529   limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
530
531   if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
532     {
533       if (limiter)
534         {
535           rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
536           if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
537             limiter->send_virtual_changes_done_at = 0;
538           else
539             rate_limiter_send_virtual_changes_done_now (monitor, limiter);
540           update_rate_limiter_timeout (monitor, 0);
541         }
542       g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
543     }
544   else
545     {
546       /* Changed event, rate limit */
547       time_now = get_time_msecs ();
548       emit_now = TRUE;
549       
550       if (limiter)
551         {
552           since_last = time_difference (limiter->last_sent_change_time, time_now);
553           if (since_last < monitor->priv->rate_limit_msec)
554             {
555               /* We ignore this change, but arm a timer so that we can fire it later if we
556                  don't get any other events (that kill this timeout) */
557               emit_now = FALSE;
558               if (limiter->send_delayed_change_at == 0)
559                 {
560                   limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
561                   update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
562                 }
563             }
564         }
565
566       if (limiter == NULL)
567         limiter = new_limiter (monitor, child);
568       
569       if (emit_now)
570         {
571           g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
572
573           limiter->last_sent_change_time = time_now;
574           limiter->send_delayed_change_at = 0;
575           /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventualy */
576           update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
577         }
578
579       /* Schedule a virtual change done. This is removed if we get a real one, and
580          postponed if we get more change events. */
581
582       limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
583       update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
584     }
585 }
586
587 #define __G_DIRECTORY_MONITOR_C__
588 #include "gioaliasdef.c"