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;
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.
241 * Returns: %TRUE if the monitor was cancelled successfully. %FALSE otherwise.
244 g_directory_monitor_cancel (GDirectoryMonitor *monitor)
246 GDirectoryMonitorClass *class;
248 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
250 if (monitor->priv->cancelled)
253 monitor->priv->cancelled = TRUE;
254 g_object_notify (G_OBJECT (monitor), "cancelled");
256 class = G_DIRECTORY_MONITOR_GET_CLASS (monitor);
257 return (* class->cancel) (monitor);
261 * g_directory_monitor_set_rate_limit:
262 * @monitor: a #GDirectoryMonitor.
263 * @limit_msecs: an integer to set the limit of the directory monitor
266 * Sets the limit of the directory monitor to watch for changes every
267 * @limit_msecs milliseconds.
270 g_directory_monitor_set_rate_limit (GDirectoryMonitor *monitor,
273 GDirectoryMonitorPrivate *priv;
274 g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
275 priv = monitor->priv;
276 if (priv->rate_limit_msec != limit_msecs)
278 monitor->priv->rate_limit_msec = limit_msecs;
279 g_object_notify (G_OBJECT (monitor), "rate-limit");
284 * g_directory_monitor_is_cancelled:
285 * @monitor: a #GDirectoryMonitor.
287 * Checks whether @monitor is cancelled.
289 * Returns: %TRUE if the monitor on the directory was cancelled.
293 g_directory_monitor_is_cancelled (GDirectoryMonitor *monitor)
295 g_return_val_if_fail (G_IS_DIRECTORY_MONITOR (monitor), FALSE);
297 return monitor->priv->cancelled;
301 get_time_msecs (void)
303 return g_thread_gettime() / (1000 * 1000);
307 time_difference (guint32 from, guint32 to)
315 new_limiter (GDirectoryMonitor *monitor,
318 RateLimiter *limiter;
320 limiter = g_new0 (RateLimiter, 1);
321 limiter->file = g_object_ref (file);
322 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
328 rate_limiter_send_virtual_changes_done_now (GDirectoryMonitor *monitor,
329 RateLimiter *limiter)
331 if (limiter->send_virtual_changes_done_at != 0)
333 g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
334 limiter->send_virtual_changes_done_at = 0;
339 rate_limiter_send_delayed_change_now (GDirectoryMonitor *monitor,
340 RateLimiter *limiter,
343 if (limiter->send_delayed_change_at != 0)
345 g_signal_emit (monitor, signals[CHANGED], 0, limiter->file, NULL, G_FILE_MONITOR_EVENT_CHANGED);
346 limiter->send_delayed_change_at = 0;
347 limiter->last_sent_change_time = time_now;
354 GDirectoryMonitor *monitor;
358 calc_min_time (GDirectoryMonitor *monitor,
359 RateLimiter *limiter,
368 if (limiter->last_sent_change_time != 0)
370 /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventualy */
371 expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
373 if (time_difference (time_now, expire_at) > 0)
376 *min_time = MIN (*min_time,
377 time_difference (time_now, expire_at));
381 if (limiter->send_delayed_change_at != 0)
384 *min_time = MIN (*min_time,
385 time_difference (time_now, limiter->send_delayed_change_at));
388 if (limiter->send_virtual_changes_done_at != 0)
391 *min_time = MIN (*min_time,
392 time_difference (time_now, limiter->send_virtual_changes_done_at));
399 foreach_rate_limiter_fire (gpointer key,
403 RateLimiter *limiter = value;
404 ForEachData *data = user_data;
406 if (limiter->send_delayed_change_at != 0 &&
407 time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
408 rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
410 if (limiter->send_virtual_changes_done_at != 0 &&
411 time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
412 rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
414 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
418 rate_limiter_timeout (gpointer timeout_data)
420 GDirectoryMonitor *monitor = timeout_data;
424 data.min_time = G_MAXUINT32;
425 data.monitor = monitor;
426 data.time_now = get_time_msecs ();
427 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
428 foreach_rate_limiter_fire,
431 /* Remove old timeout */
432 if (monitor->priv->timeout)
434 g_source_destroy (monitor->priv->timeout);
435 g_source_unref (monitor->priv->timeout);
436 monitor->priv->timeout = NULL;
437 monitor->priv->timeout_fires_at = 0;
440 /* Set up new timeout */
441 if (data.min_time != G_MAXUINT32)
443 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
444 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
445 g_source_attach (source, NULL);
447 monitor->priv->timeout = source;
448 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
455 foreach_rate_limiter_update (gpointer key,
459 RateLimiter *limiter = value;
460 ForEachData *data = user_data;
462 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
466 update_rate_limiter_timeout (GDirectoryMonitor *monitor,
472 if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
473 time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
474 return; /* Nothing to do, we already fire earlier than that */
476 data.min_time = G_MAXUINT32;
477 data.monitor = monitor;
478 data.time_now = get_time_msecs ();
479 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
480 foreach_rate_limiter_update,
483 /* Remove old timeout */
484 if (monitor->priv->timeout)
486 g_source_destroy (monitor->priv->timeout);
487 g_source_unref (monitor->priv->timeout);
488 monitor->priv->timeout_fires_at = 0;
489 monitor->priv->timeout = NULL;
492 /* Set up new timeout */
493 if (data.min_time != G_MAXUINT32)
495 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
496 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
497 g_source_attach (source, NULL);
499 monitor->priv->timeout = source;
500 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
505 * g_directory_monitor_emit_event:
506 * @monitor: a #GDirectoryMonitor.
508 * @other_file: a #GFile.
509 * @event_type: a set of #GFileMonitorEvent flags.
511 * Emits the #GDirectoryMonitor::changed signal if a change
512 * has taken place. Should be called from directory monitor
513 * implementations only.
516 g_directory_monitor_emit_event (GDirectoryMonitor *monitor,
519 GFileMonitorEvent event_type)
521 guint32 time_now, since_last;
523 RateLimiter *limiter;
525 g_return_if_fail (G_IS_DIRECTORY_MONITOR (monitor));
526 g_return_if_fail (G_IS_FILE (child));
528 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
530 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
534 rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
535 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
536 limiter->send_virtual_changes_done_at = 0;
538 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
539 update_rate_limiter_timeout (monitor, 0);
541 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
545 /* Changed event, rate limit */
546 time_now = get_time_msecs ();
551 since_last = time_difference (limiter->last_sent_change_time, time_now);
552 if (since_last < monitor->priv->rate_limit_msec)
554 /* We ignore this change, but arm a timer so that we can fire it later if we
555 don't get any other events (that kill this timeout) */
557 if (limiter->send_delayed_change_at == 0)
559 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
560 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
566 limiter = new_limiter (monitor, child);
570 g_signal_emit (monitor, signals[CHANGED], 0, child, other_file, event_type);
572 limiter->last_sent_change_time = time_now;
573 limiter->send_delayed_change_at = 0;
574 /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventualy */
575 update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
578 /* Schedule a virtual change done. This is removed if we get a real one, and
579 postponed if we get more change events. */
581 limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
582 update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
586 #define __G_DIRECTORY_MONITOR_C__
587 #include "gioaliasdef.c"