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 "gfilemonitor.h"
27 #include "gio-marshal.h"
28 #include "gioenumtypes.h"
36 typedef struct _FileChange FileChange;
37 static void file_change_free (FileChange *change);
40 * SECTION:gfilemonitor
41 * @short_description: File Monitor
44 * Monitors a file or directory for changes.
46 * To obtain a #GFileMonitor for a file or directory, use
47 * g_file_monitor(), g_file_monitor_file(), or
48 * g_file_monitor_directory().
50 * To get informed about changes to the file or directory you
51 * are monitoring, connect to the #GFileMonitor::changed signal.
54 G_LOCK_DEFINE_STATIC(cancelled);
61 /* work around a limitation of the aliasing foo */
64 G_DEFINE_ABSTRACT_TYPE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT);
68 guint32 last_sent_change_time; /* 0 == not sent */
69 guint32 send_delayed_change_at; /* 0 == never */
70 guint32 send_virtual_changes_done_at; /* 0 == never */
73 struct _GFileMonitorPrivate {
77 /* Rate limiting change events */
78 GHashTable *rate_limiter;
80 guint pending_file_change_id;
81 GSList *pending_file_changes; /* FileChange */
84 guint32 timeout_fires_at;
94 g_file_monitor_set_property (GObject *object,
99 GFileMonitor *monitor;
101 monitor = G_FILE_MONITOR (object);
105 case PROP_RATE_LIMIT:
106 g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116 g_file_monitor_get_property (GObject *object,
121 GFileMonitor *monitor;
122 GFileMonitorPrivate *priv;
124 monitor = G_FILE_MONITOR (object);
125 priv = monitor->priv;
129 case PROP_RATE_LIMIT:
130 g_value_set_int (value, priv->rate_limit_msec);
135 g_value_set_boolean (value, priv->cancelled);
136 G_UNLOCK (cancelled);
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 #define DEFAULT_RATE_LIMIT_MSECS 800
146 #define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2
148 static guint signals[LAST_SIGNAL] = { 0 };
151 rate_limiter_free (RateLimiter *limiter)
153 g_object_unref (limiter->file);
154 g_slice_free (RateLimiter, limiter);
158 g_file_monitor_finalize (GObject *object)
160 GFileMonitor *monitor;
162 monitor = G_FILE_MONITOR (object);
164 if (monitor->priv->timeout)
166 g_source_destroy (monitor->priv->timeout);
167 g_source_unref (monitor->priv->timeout);
170 g_hash_table_destroy (monitor->priv->rate_limiter);
172 G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize (object);
176 g_file_monitor_dispose (GObject *object)
178 GFileMonitor *monitor;
179 GFileMonitorPrivate *priv;
181 monitor = G_FILE_MONITOR (object);
182 priv = monitor->priv;
184 if (priv->pending_file_change_id)
186 g_source_remove (priv->pending_file_change_id);
187 priv->pending_file_change_id = 0;
189 g_slist_foreach (priv->pending_file_changes, (GFunc) file_change_free, NULL);
190 g_slist_free (priv->pending_file_changes);
191 priv->pending_file_changes = NULL;
193 /* Make sure we cancel on last unref */
194 g_file_monitor_cancel (monitor);
196 G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
200 g_file_monitor_class_init (GFileMonitorClass *klass)
202 GObjectClass *object_class;
204 g_type_class_add_private (klass, sizeof (GFileMonitorPrivate));
206 object_class = G_OBJECT_CLASS (klass);
207 object_class->finalize = g_file_monitor_finalize;
208 object_class->dispose = g_file_monitor_dispose;
209 object_class->get_property = g_file_monitor_get_property;
210 object_class->set_property = g_file_monitor_set_property;
213 * GFileMonitor::changed:
214 * @monitor: a #GFileMonitor.
216 * @other_file: a #GFile.
217 * @event_type: a #GFileMonitorEvent.
219 * Emitted when a file has been changed.
222 g_signal_new (I_("changed"),
225 G_STRUCT_OFFSET (GFileMonitorClass, changed),
227 _gio_marshal_VOID__OBJECT_OBJECT_ENUM,
229 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
231 g_object_class_install_property (object_class,
233 g_param_spec_int ("rate-limit",
235 P_("The limit of the monitor to watch for changes, in milliseconds"),
237 DEFAULT_RATE_LIMIT_MSECS,
239 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
241 g_object_class_install_property (object_class,
243 g_param_spec_boolean ("cancelled",
245 P_("Whether the monitor has been cancelled"),
248 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
252 g_file_monitor_init (GFileMonitor *monitor)
254 monitor->priv = G_TYPE_INSTANCE_GET_PRIVATE (monitor,
256 GFileMonitorPrivate);
257 monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
258 monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
259 NULL, (GDestroyNotify) rate_limiter_free);
263 * g_file_monitor_is_cancelled:
264 * @monitor: a #GFileMonitor
266 * Returns whether the monitor is canceled.
268 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
271 g_file_monitor_is_cancelled (GFileMonitor *monitor)
275 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
278 res = monitor->priv->cancelled;
279 G_UNLOCK (cancelled);
285 * g_file_monitor_cancel:
286 * @monitor: a #GFileMonitor.
288 * Cancels a file monitor.
290 * Returns: %TRUE if monitor was cancelled.
293 g_file_monitor_cancel (GFileMonitor* monitor)
295 GFileMonitorClass *klass;
297 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
300 if (monitor->priv->cancelled)
302 G_UNLOCK (cancelled);
306 monitor->priv->cancelled = TRUE;
307 G_UNLOCK (cancelled);
309 g_object_notify (G_OBJECT (monitor), "cancelled");
311 klass = G_FILE_MONITOR_GET_CLASS (monitor);
312 return (* klass->cancel) (monitor);
316 * g_file_monitor_set_rate_limit:
317 * @monitor: a #GFileMonitor.
318 * @limit_msecs: a integer with the limit in milliseconds to
321 * Sets the rate limit to which the @monitor will report
322 * consecutive change events to the same file.
326 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
329 GFileMonitorPrivate *priv;
331 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
333 priv = monitor->priv;
334 if (priv->rate_limit_msec != limit_msecs)
336 monitor->priv->rate_limit_msec = limit_msecs;
337 g_object_notify (G_OBJECT (monitor), "rate-limit");
344 GFileMonitorEvent event_type;
348 file_change_free (FileChange *change)
350 g_object_unref (change->child);
351 if (change->other_file)
352 g_object_unref (change->other_file);
354 g_slice_free (FileChange, change);
358 emit_cb (gpointer data)
360 GFileMonitor *monitor = G_FILE_MONITOR (data);
361 GSList *pending, *iter;
363 pending = g_slist_reverse (monitor->priv->pending_file_changes);
364 monitor->priv->pending_file_changes = NULL;
365 monitor->priv->pending_file_change_id = 0;
367 for (iter = pending; iter; iter = iter->next)
369 FileChange *change = iter->data;
370 g_signal_emit (monitor, signals[CHANGED], 0,
371 change->child, change->other_file, change->event_type);
372 file_change_free (change);
374 g_slist_free (pending);
379 emit_in_idle (GFileMonitor *monitor,
382 GFileMonitorEvent event_type)
386 GFileMonitorPrivate *priv;
388 priv = monitor->priv;
390 change = g_slice_new (FileChange);
392 change->child = g_object_ref (child);
394 change->other_file = g_object_ref (other_file);
396 change->other_file = NULL;
397 change->event_type = event_type;
399 if (!priv->pending_file_change_id)
401 source = g_idle_source_new ();
402 g_source_set_priority (source, 0);
404 /* We don't ref here - instead dispose will free any
407 g_source_set_callback (source, emit_cb, monitor, NULL);
408 priv->pending_file_change_id = g_source_attach (source, NULL);
409 g_source_unref (source);
411 /* We reverse this in the processor */
412 priv->pending_file_changes = g_slist_prepend (priv->pending_file_changes, change);
416 get_time_msecs (void)
418 return g_thread_gettime() / (1000 * 1000);
422 time_difference (guint32 from, guint32 to)
429 /* Change event rate limiting support: */
432 new_limiter (GFileMonitor *monitor,
435 RateLimiter *limiter;
437 limiter = g_slice_new0 (RateLimiter);
438 limiter->file = g_object_ref (file);
439 g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
445 rate_limiter_send_virtual_changes_done_now (GFileMonitor *monitor,
446 RateLimiter *limiter)
448 if (limiter->send_virtual_changes_done_at != 0)
450 emit_in_idle (monitor, limiter->file, NULL,
451 G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
452 limiter->send_virtual_changes_done_at = 0;
457 rate_limiter_send_delayed_change_now (GFileMonitor *monitor,
458 RateLimiter *limiter,
461 if (limiter->send_delayed_change_at != 0)
463 emit_in_idle (monitor,
465 G_FILE_MONITOR_EVENT_CHANGED);
466 limiter->send_delayed_change_at = 0;
467 limiter->last_sent_change_time = time_now;
474 GFileMonitor *monitor;
478 calc_min_time (GFileMonitor *monitor,
479 RateLimiter *limiter,
488 if (limiter->last_sent_change_time != 0)
490 /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventualy */
491 expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;
493 if (time_difference (time_now, expire_at) > 0)
496 *min_time = MIN (*min_time,
497 time_difference (time_now, expire_at));
501 if (limiter->send_delayed_change_at != 0)
504 *min_time = MIN (*min_time,
505 time_difference (time_now, limiter->send_delayed_change_at));
508 if (limiter->send_virtual_changes_done_at != 0)
511 *min_time = MIN (*min_time,
512 time_difference (time_now, limiter->send_virtual_changes_done_at));
519 foreach_rate_limiter_fire (gpointer key,
523 RateLimiter *limiter = value;
524 ForEachData *data = user_data;
526 if (limiter->send_delayed_change_at != 0 &&
527 time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
528 rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
530 if (limiter->send_virtual_changes_done_at != 0 &&
531 time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
532 rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
534 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
538 rate_limiter_timeout (gpointer timeout_data)
540 GFileMonitor *monitor = timeout_data;
544 data.min_time = G_MAXUINT32;
545 data.monitor = monitor;
546 data.time_now = get_time_msecs ();
547 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
548 foreach_rate_limiter_fire,
551 /* Remove old timeout */
552 if (monitor->priv->timeout)
554 g_source_destroy (monitor->priv->timeout);
555 g_source_unref (monitor->priv->timeout);
556 monitor->priv->timeout = NULL;
557 monitor->priv->timeout_fires_at = 0;
560 /* Set up new timeout */
561 if (data.min_time != G_MAXUINT32)
563 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
564 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
565 g_source_attach (source, NULL);
567 monitor->priv->timeout = source;
568 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
575 foreach_rate_limiter_update (gpointer key,
579 RateLimiter *limiter = value;
580 ForEachData *data = user_data;
582 return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
586 update_rate_limiter_timeout (GFileMonitor *monitor,
592 if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
593 time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
594 return; /* Nothing to do, we already fire earlier than that */
596 data.min_time = G_MAXUINT32;
597 data.monitor = monitor;
598 data.time_now = get_time_msecs ();
599 g_hash_table_foreach_remove (monitor->priv->rate_limiter,
600 foreach_rate_limiter_update,
603 /* Remove old timeout */
604 if (monitor->priv->timeout)
606 g_source_destroy (monitor->priv->timeout);
607 g_source_unref (monitor->priv->timeout);
608 monitor->priv->timeout_fires_at = 0;
609 monitor->priv->timeout = NULL;
612 /* Set up new timeout */
613 if (data.min_time != G_MAXUINT32)
615 source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
616 g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
617 g_source_attach (source, NULL);
619 monitor->priv->timeout = source;
620 monitor->priv->timeout_fires_at = data.time_now + data.min_time;
625 * g_file_monitor_emit_event:
626 * @monitor: a #GFileMonitor.
628 * @other_file: a #GFile.
629 * @event_type: a set of #GFileMonitorEvent flags.
631 * Emits the #GFileMonitor::changed signal if a change
632 * has taken place. Should be called from file monitor
633 * implementations only.
635 * The signal will be emitted from an idle handler.
638 g_file_monitor_emit_event (GFileMonitor *monitor,
641 GFileMonitorEvent event_type)
643 guint32 time_now, since_last;
645 RateLimiter *limiter;
647 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
648 g_return_if_fail (G_IS_FILE (child));
650 limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
652 if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
656 rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
657 if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
658 limiter->send_virtual_changes_done_at = 0;
660 rate_limiter_send_virtual_changes_done_now (monitor, limiter);
661 update_rate_limiter_timeout (monitor, 0);
663 emit_in_idle (monitor, child, other_file, event_type);
667 /* Changed event, rate limit */
668 time_now = get_time_msecs ();
673 since_last = time_difference (limiter->last_sent_change_time, time_now);
674 if (since_last < monitor->priv->rate_limit_msec)
676 /* We ignore this change, but arm a timer so that we can fire it later if we
677 don't get any other events (that kill this timeout) */
679 if (limiter->send_delayed_change_at == 0)
681 limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
682 update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
688 limiter = new_limiter (monitor, child);
692 emit_in_idle (monitor, child, other_file, event_type);
694 limiter->last_sent_change_time = time_now;
695 limiter->send_delayed_change_at = 0;
696 /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventualy */
697 update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
700 /* Schedule a virtual change done. This is removed if we get a real one, and
701 postponed if we get more change events. */
703 limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
704 update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
708 #define __G_FILE_MONITOR_C__
709 #include "gioaliasdef.c"