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.
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;
67 #define DEFAULT_RATE_LIMIT_MSECS 800
68 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
70 static guint signals[LAST_SIGNAL] = { 0 };
73 rate_limiter_free (RateLimiter *limiter)
75 g_object_unref (limiter->file);
80 g_directory_monitor_finalize (GObject *object)
82 GDirectoryMonitor *monitor;
84 monitor = G_DIRECTORY_MONITOR (object);
86 if (monitor->priv->timeout)
88 g_source_destroy (monitor->priv->timeout);
89 g_source_unref (monitor->priv->timeout);
92 g_hash_table_destroy (monitor->priv->rate_limiter);
94 if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize)
95 (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize) (object);
99 g_directory_monitor_dispose (GObject *object)
101 GDirectoryMonitor *monitor;
103 monitor = G_DIRECTORY_MONITOR (object);
105 /* Make sure we cancel on last unref */
106 if (!monitor->priv->cancelled)
107 g_directory_monitor_cancel (monitor);
109 if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose)
110 (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose) (object);
114 g_directory_monitor_class_init (GDirectoryMonitorClass *klass)
116 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 g_type_class_add_private (klass, sizeof (GDirectoryMonitorPrivate));
120 gobject_class->finalize = g_directory_monitor_finalize;
121 gobject_class->dispose = g_directory_monitor_dispose;
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
130 * Emitted when a child file changes.
133 g_signal_new (I_("changed"),
134 G_TYPE_DIRECTORY_MONITOR,
136 G_STRUCT_OFFSET (GDirectoryMonitorClass, changed),
138 _gio_marshal_VOID__OBJECT_OBJECT_INT,
146 g_directory_monitor_init (GDirectoryMonitor *monitor)
148 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
149 G_TYPE_DIRECTORY_MONITOR,
150 GDirectoryMonitorPrivate);
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);
159 * g_directory_monitor_cancel:
160 * @monitor: a #GDirectoryMonitor.
162 * Cancels the monitoring activity on @monitor.
164 * Returns: %TRUE if the monitor was cancelled successfully. %FALSE otherwise.
167 g_directory_monitor_cancel (GDirectoryMonitor *monitor)
169 GDirectoryMonitorClass *class;
171 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
173 if (monitor->priv->cancelled)
176 monitor->priv->cancelled = TRUE;
178 class = G_DIRECTORY_MONITOR_GET_CLASS (monitor);
179 return (* class->cancel) (monitor);
183 * g_directory_monitor_set_rate_limit:
184 * @monitor: a #GDirectoryMonitor.
185 * @limit_msecs: an integer to set the limit of the directory monitor
188 * Sets the limit of the directory monitor to watch for changes every
189 * @limit_msecs milliseconds.
192 g_directory_monitor_set_rate_limit (GDirectoryMonitor *monitor,
195 g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
197 monitor->priv->rate_limit_msec = limit_msecs;
201 * g_directory_monitor_is_cancelled:
202 * @monitor: a #GDirectoryMonitor.
204 * Checks whether @monitor is cancelled.
206 * Returns: %TRUE if the monitor on the directory was cancelled.
210 g_directory_monitor_is_cancelled (GDirectoryMonitor *monitor)
212 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
214 return monitor->priv->cancelled;
218 get_time_msecs (void)
220 return g_thread_gettime() / (1000 * 1000);
224 time_difference (guint32 from, guint32 to)
232 new_limiter (GDirectoryMonitor *monitor,
235 RateLimiter *limiter;
237 limiter = g_new0 (RateLimiter, 1);
238 limiter->file = g_object_ref (file);
239 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
245 rate_limiter_send_virtual_changes_done_now (GDirectoryMonitor *monitor,
246 RateLimiter *limiter)
248 if (limiter->send_virtual_changes_done_at != 0)
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;
256 rate_limiter_send_delayed_change_now (GDirectoryMonitor *monitor,
257 RateLimiter *limiter,
260 if (limiter->send_delayed_change_at != 0)
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;
271 GDirectoryMonitor *monitor;
275 calc_min_time (GDirectoryMonitor *monitor,
276 RateLimiter *limiter,
285 if (limiter->last_sent_change_time != 0)
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;
290 if (time_difference (time_now, expire_at) > 0)
293 *min_time = MIN (*min_time,
294 time_difference (time_now, expire_at));
298 if (limiter->send_delayed_change_at != 0)
301 *min_time = MIN (*min_time,
302 time_difference (time_now, limiter->send_delayed_change_at));
305 if (limiter->send_virtual_changes_done_at != 0)
308 *min_time = MIN (*min_time,
309 time_difference (time_now, limiter->send_virtual_changes_done_at));
316 foreach_rate_limiter_fire (gpointer key,
320 RateLimiter *limiter = value;
321 ForEachData *data = user_data;
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);
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);
331 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
335 rate_limiter_timeout (gpointer timeout_data)
337 GDirectoryMonitor *monitor = timeout_data;
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,
348 /* Remove old timeout */
349 if (monitor->priv->timeout)
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;
357 /* Set up new timeout */
358 if (data.min_time != G_MAXUINT32)
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);
364 monitor->priv->timeout = source;
365 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
372 foreach_rate_limiter_update (gpointer key,
376 RateLimiter *limiter = value;
377 ForEachData *data = user_data;
379 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
383 update_rate_limiter_timeout (GDirectoryMonitor *monitor,
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 */
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,
400 /* Remove old timeout */
401 if (monitor->priv->timeout)
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;
409 /* Set up new timeout */
410 if (data.min_time != G_MAXUINT32)
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);
416 monitor->priv->timeout = source;
417 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
422 * g_directory_monitor_emit_event:
423 * @monitor: a #GDirectoryMonitor.
425 * @other_file: a #GFile.
426 * @event_type: a set of #GFileMonitorEvent flags.
428 * Emits the #GDirectoryMonitor::changed signal if a change
429 * has taken place. Should be called from directory monitor
430 * implementations only.
433 g_directory_monitor_emit_event (GDirectoryMonitor *monitor,
436 GFileMonitorEvent event_type)
438 guint32 time_now, since_last;
440 RateLimiter *limiter;
442 g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
443 g_return_if_fail (G_IS_FILE (child));
445 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
447 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
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;
455 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
456 update_rate_limiter_timeout (monitor, 0);
458 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
462 /* Changed event, rate limit */
463 time_now = get_time_msecs ();
468 since_last = time_difference (limiter->last_sent_change_time, time_now);
469 if (since_last < monitor->priv->rate_limit_msec)
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) */
474 if (limiter->send_delayed_change_at == 0)
476 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
477 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
483 limiter = new_limiter (monitor, child);
487 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
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);
495 /* Schedule a virtual change done. This is removed if we get a real one, and
496 postponed if we get more change events. */
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);
503 #define __G_DIRECTORY_MONITOR_C__
504 #include "gioaliasdef.c"