1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "gdirectorymonitor.h"
27 #include "gio-marshal.h"
35 * SECTION:gdirectorymonitor
36 * @short_description: Directory Monitor
37 * @see_also: #GFileMonitor
39 * Monitors a directory for changes to files in it.
48 G_DEFINE_ABSTRACT_TYPE (GDirectoryMonitor, g_directory_monitor, G_TYPE_OBJECT);
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 */
57 struct _GDirectoryMonitorPrivate {
61 GHashTable *rate_limiter;
64 guint32 timeout_fires_at;
73 #define DEFAULT_RATE_LIMIT_MSECS 800
74 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
76 static guint signals[LAST_SIGNAL] = { 0 };
79 rate_limiter_free (RateLimiter *limiter)
81 g_object_unref (limiter->file);
86 g_directory_monitor_set_property (GObject *object,
91 GDirectoryMonitor *monitor;
93 monitor = G_DIRECTORY_MONITOR (object);
98 g_directory_monitor_set_rate_limit (monitor, g_value_get_int (value));
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 g_directory_monitor_get_property (GObject *object,
113 GDirectoryMonitor *monitor;
114 GDirectoryMonitorPrivate *priv;
116 monitor = G_DIRECTORY_MONITOR (object);
117 priv = monitor->priv;
121 case PROP_RATE_LIMIT:
122 g_value_set_int (value, priv->rate_limit_msec);
126 g_value_set_boolean (value, priv->cancelled);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 g_directory_monitor_finalize (GObject *object)
138 GDirectoryMonitor *monitor;
140 monitor = G_DIRECTORY_MONITOR (object);
142 if (monitor->priv->timeout)
144 g_source_destroy (monitor->priv->timeout);
145 g_source_unref (monitor->priv->timeout);
148 g_hash_table_destroy (monitor->priv->rate_limiter);
150 if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize)
151 (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize) (object);
155 g_directory_monitor_dispose (GObject *object)
157 GDirectoryMonitor *monitor;
159 monitor = G_DIRECTORY_MONITOR (object);
161 /* Make sure we cancel on last unref */
162 if (!monitor->priv->cancelled)
163 g_directory_monitor_cancel (monitor);
165 if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose)
166 (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose) (object);
170 g_directory_monitor_class_init (GDirectoryMonitorClass *klass)
172 GObjectClass *object_class;
174 g_type_class_add_private (klass, sizeof (GDirectoryMonitorPrivate));
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;
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
189 * Emitted when a child file changes.
192 g_signal_new (I_("changed"),
193 G_TYPE_DIRECTORY_MONITOR,
195 G_STRUCT_OFFSET (GDirectoryMonitorClass, changed),
197 _gio_marshal_VOID__OBJECT_OBJECT_INT,
199 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_INT);
201 g_object_class_install_property (object_class,
203 g_param_spec_int ("rate-limit",
205 P_("The limit of the monitor to watch for changes, in milliseconds"),
207 DEFAULT_RATE_LIMIT_MSECS,
209 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
211 g_object_class_install_property (object_class,
213 g_param_spec_boolean ("cancelled",
215 P_("Whether the monitor has been cancelled"),
218 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
223 g_directory_monitor_init (GDirectoryMonitor *monitor)
225 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
226 G_TYPE_DIRECTORY_MONITOR,
227 GDirectoryMonitorPrivate);
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);
236 * g_directory_monitor_cancel:
237 * @monitor: a #GDirectoryMonitor.
239 * Cancels the monitoring activity on @monitor. Note that
240 * the monitor is automatically cancelled when finalized.
242 * It is safe to call this multiple times.
244 * Returns: %TRUE if the monitor was cancelled successfully. %FALSE otherwise.
247 g_directory_monitor_cancel (GDirectoryMonitor *monitor)
249 GDirectoryMonitorClass *class;
251 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
253 if (monitor->priv->cancelled)
256 monitor->priv->cancelled = TRUE;
257 g_object_notify (G_OBJECT (monitor), "cancelled");
259 class = G_DIRECTORY_MONITOR_GET_CLASS (monitor);
260 return (* class->cancel) (monitor);
264 * g_directory_monitor_set_rate_limit:
265 * @monitor: a #GDirectoryMonitor.
266 * @limit_msecs: the change rate limit of the directory monitor in milliseconds.
268 * Report same consecutive changes of the same type at most once each @limit_msecs milliseconds.
271 g_directory_monitor_set_rate_limit (GDirectoryMonitor *monitor,
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)
279 monitor->priv->rate_limit_msec = limit_msecs;
280 g_object_notify (G_OBJECT (monitor), "rate-limit");
285 * g_directory_monitor_is_cancelled:
286 * @monitor: a #GDirectoryMonitor.
288 * Checks whether @monitor is cancelled.
290 * Returns: %TRUE if the monitor on the directory was cancelled.
294 g_directory_monitor_is_cancelled (GDirectoryMonitor *monitor)
296 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
298 return monitor->priv->cancelled;
302 get_time_msecs (void)
304 return g_thread_gettime() / (1000 * 1000);
308 time_difference (guint32 from, guint32 to)
316 new_limiter (GDirectoryMonitor *monitor,
319 RateLimiter *limiter;
321 limiter = g_new0 (RateLimiter, 1);
322 limiter->file = g_object_ref (file);
323 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
329 rate_limiter_send_virtual_changes_done_now (GDirectoryMonitor *monitor,
330 RateLimiter *limiter)
332 if (limiter->send_virtual_changes_done_at != 0)
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;
340 rate_limiter_send_delayed_change_now (GDirectoryMonitor *monitor,
341 RateLimiter *limiter,
344 if (limiter->send_delayed_change_at != 0)
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;
355 GDirectoryMonitor *monitor;
359 calc_min_time (GDirectoryMonitor *monitor,
360 RateLimiter *limiter,
369 if (limiter->last_sent_change_time != 0)
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;
374 if (time_difference (time_now, expire_at) > 0)
377 *min_time = MIN (*min_time,
378 time_difference (time_now, expire_at));
382 if (limiter->send_delayed_change_at != 0)
385 *min_time = MIN (*min_time,
386 time_difference (time_now, limiter->send_delayed_change_at));
389 if (limiter->send_virtual_changes_done_at != 0)
392 *min_time = MIN (*min_time,
393 time_difference (time_now, limiter->send_virtual_changes_done_at));
400 foreach_rate_limiter_fire (gpointer key,
404 RateLimiter *limiter = value;
405 ForEachData *data = user_data;
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);
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);
415 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
419 rate_limiter_timeout (gpointer timeout_data)
421 GDirectoryMonitor *monitor = timeout_data;
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,
432 /* Remove old timeout */
433 if (monitor->priv->timeout)
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;
441 /* Set up new timeout */
442 if (data.min_time != G_MAXUINT32)
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);
448 monitor->priv->timeout = source;
449 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
456 foreach_rate_limiter_update (gpointer key,
460 RateLimiter *limiter = value;
461 ForEachData *data = user_data;
463 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
467 update_rate_limiter_timeout (GDirectoryMonitor *monitor,
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 */
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,
484 /* Remove old timeout */
485 if (monitor->priv->timeout)
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;
493 /* Set up new timeout */
494 if (data.min_time != G_MAXUINT32)
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);
500 monitor->priv->timeout = source;
501 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
506 * g_directory_monitor_emit_event:
507 * @monitor: a #GDirectoryMonitor.
509 * @other_file: a #GFile.
510 * @event_type: a set of #GFileMonitorEvent flags.
512 * Emits the #GDirectoryMonitor::changed signal if a change
513 * has taken place. Should be called from directory monitor
514 * implementations only.
517 g_directory_monitor_emit_event (GDirectoryMonitor *monitor,
520 GFileMonitorEvent event_type)
522 guint32 time_now, since_last;
524 RateLimiter *limiter;
526 g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
527 g_return_if_fail (G_IS_FILE (child));
529 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
531 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
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;
539 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
540 update_rate_limiter_timeout (monitor, 0);
542 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
546 /* Changed event, rate limit */
547 time_now = get_time_msecs ();
552 since_last = time_difference (limiter->last_sent_change_time, time_now);
553 if (since_last < monitor->priv->rate_limit_msec)
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) */
558 if (limiter->send_delayed_change_at == 0)
560 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
561 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
567 limiter = new_limiter (monitor, child);
571 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
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);
579 /* Schedule a virtual change done. This is removed if we get a real one, and
580 postponed if we get more change events. */
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);
587 #define __G_DIRECTORY_MONITOR_C__
588 #include "gioaliasdef.c"