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"
37 G_DEFINE_ABSTRACT_TYPE (GDirectoryMonitor, g_directory_monitor, G_TYPE_OBJECT);
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 */
46 struct _GDirectoryMonitorPrivate {
50 GHashTable *rate_limiter;
53 guint32 timeout_fires_at;
56 #define DEFAULT_RATE_LIMIT_MSECS 800
57 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
59 static guint signals[LAST_SIGNAL] = { 0 };
62 rate_limiter_free (RateLimiter *limiter)
64 g_object_unref (limiter->file);
69 g_directory_monitor_finalize (GObject *object)
71 GDirectoryMonitor *monitor;
73 monitor = G_DIRECTORY_MONITOR (object);
75 if (monitor->priv->timeout)
77 g_source_destroy (monitor->priv->timeout);
78 g_source_unref (monitor->priv->timeout);
81 g_hash_table_destroy (monitor->priv->rate_limiter);
83 if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize)
84 (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->finalize) (object);
88 g_directory_monitor_dispose (GObject *object)
90 GDirectoryMonitor *monitor;
92 monitor = G_DIRECTORY_MONITOR (object);
94 /* Make sure we cancel on last unref */
95 if (!monitor->priv->cancelled)
96 g_directory_monitor_cancel (monitor);
98 if (G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose)
99 (*G_OBJECT_CLASS (g_directory_monitor_parent_class)->dispose) (object);
103 g_directory_monitor_class_init (GDirectoryMonitorClass *klass)
105 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
107 g_type_class_add_private (klass, sizeof (GDirectoryMonitorPrivate));
109 gobject_class->finalize = g_directory_monitor_finalize;
110 gobject_class->dispose = g_directory_monitor_dispose;
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
119 * Emitted when a child file changes.
122 g_signal_new (I_("changed"),
123 G_TYPE_DIRECTORY_MONITOR,
125 G_STRUCT_OFFSET (GDirectoryMonitorClass, changed),
127 _gio_marshal_VOID__OBJECT_OBJECT_INT,
135 g_directory_monitor_init (GDirectoryMonitor *monitor)
137 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
138 G_TYPE_DIRECTORY_MONITOR,
139 GDirectoryMonitorPrivate);
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);
148 * g_directory_monitor_cancel:
154 g_directory_monitor_cancel (GDirectoryMonitor* monitor)
156 GDirectoryMonitorClass *class;
158 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
160 if (monitor->priv->cancelled)
163 monitor->priv->cancelled = TRUE;
165 class = G_DIRECTORY_MONITOR_GET_CLASS (monitor);
166 return (* class->cancel) (monitor);
170 * g_directory_monitor_set_rate_limit:
176 g_directory_monitor_set_rate_limit (GDirectoryMonitor *monitor,
179 g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
181 monitor->priv->rate_limit_msec = limit_msecs;
185 * g_directory_monitor_is_cancelled:
191 g_directory_monitor_is_cancelled (GDirectoryMonitor *monitor)
193 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
195 return monitor->priv->cancelled;
199 get_time_msecs (void)
201 return g_thread_gettime() / (1000 * 1000);
205 time_difference (guint32 from, guint32 to)
213 new_limiter (GDirectoryMonitor *monitor,
216 RateLimiter *limiter;
218 limiter = g_new0 (RateLimiter, 1);
219 limiter->file = g_object_ref (file);
220 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
226 rate_limiter_send_virtual_changes_done_now (GDirectoryMonitor *monitor, RateLimiter *limiter)
228 if (limiter->send_virtual_changes_done_at != 0)
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;
236 rate_limiter_send_delayed_change_now (GDirectoryMonitor *monitor, RateLimiter *limiter, guint32 time_now)
238 if (limiter->send_delayed_change_at != 0)
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;
249 GDirectoryMonitor *monitor;
253 calc_min_time (GDirectoryMonitor *monitor, RateLimiter *limiter, guint32 time_now, guint32 *min_time)
260 if (limiter->last_sent_change_time != 0)
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;
265 if (time_difference (time_now, expire_at) > 0)
268 *min_time = MIN (*min_time,
269 time_difference (time_now, expire_at));
273 if (limiter->send_delayed_change_at != 0)
276 *min_time = MIN (*min_time,
277 time_difference (time_now, limiter->send_delayed_change_at));
280 if (limiter->send_virtual_changes_done_at != 0)
283 *min_time = MIN (*min_time,
284 time_difference (time_now, limiter->send_virtual_changes_done_at));
291 foreach_rate_limiter_fire (gpointer key,
295 RateLimiter *limiter = value;
296 ForEachData *data = user_data;
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);
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);
306 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
310 rate_limiter_timeout (gpointer timeout_data)
312 GDirectoryMonitor *monitor = timeout_data;
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,
323 /* Remove old timeout */
324 if (monitor->priv->timeout)
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;
332 /* Set up new timeout */
333 if (data.min_time != G_MAXUINT32)
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);
339 monitor->priv->timeout = source;
340 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
347 foreach_rate_limiter_update (gpointer key,
351 RateLimiter *limiter = value;
352 ForEachData *data = user_data;
354 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
358 update_rate_limiter_timeout (GDirectoryMonitor *monitor, guint new_time)
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 */
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,
374 /* Remove old timeout */
375 if (monitor->priv->timeout)
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;
383 /* Set up new timeout */
384 if (data.min_time != G_MAXUINT32)
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);
390 monitor->priv->timeout = source;
391 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
396 * g_directory_monitor_emit_event:
404 g_directory_monitor_emit_event (GDirectoryMonitor *monitor,
407 GFileMonitorEvent event_type)
409 guint32 time_now, since_last;
411 RateLimiter *limiter;
413 g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
414 g_return_if_fail (G_IS_FILE (child));
416 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
418 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
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;
426 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
427 update_rate_limiter_timeout (monitor, 0);
429 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
433 /* Changed event, rate limit */
434 time_now = get_time_msecs ();
439 since_last = time_difference (limiter->last_sent_change_time, time_now);
440 if (since_last < monitor->priv->rate_limit_msec)
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) */
445 if (limiter->send_delayed_change_at == 0)
447 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
448 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
454 limiter = new_limiter (monitor, child);
458 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
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);
466 /* Schedule a virtual change done. This is removed if we get a real one, and
467 postponed if we get more change events. */
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);