gfilemonitor.c 20.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

23
#include "config.h"
24 25 26
#include <string.h>

#include "gfilemonitor.h"
27
#include "gioenumtypes.h"
28
#include "gfile.h"
29 30 31
#include "gvfs.h"
#include "glibintl.h"

32

33 34 35 36
struct _FileChange;
typedef struct _FileChange FileChange;
static void file_change_free (FileChange *change);

37 38 39
/**
 * SECTION:gfilemonitor
 * @short_description: File Monitor
Matthias Clasen's avatar
Matthias Clasen committed
40
 * @include: gio/gio.h
41
 *
42
 * Monitors a file or directory for changes.
Matthias Clasen's avatar
Matthias Clasen committed
43 44
 *
 * To obtain a #GFileMonitor for a file or directory, use
45 46
 * g_file_monitor(), g_file_monitor_file(), or
 * g_file_monitor_directory().
Matthias Clasen's avatar
Matthias Clasen committed
47
 *
48 49 50 51 52 53 54 55
 * To get informed about changes to the file or directory you are
 * monitoring, connect to the #GFileMonitor::changed signal. The
 * signal will be emitted in the <link
 * linkend="g-main-context-push-thread-default">thread-default main
 * context</link> of the thread that the monitor was created in
 * (though if the global default main context is blocked, this may
 * cause notifications to be blocked even if the thread-default
 * context is still running).
56 57
 **/

58 59
G_LOCK_DEFINE_STATIC(cancelled);

60 61 62 63 64
enum {
  CHANGED,
  LAST_SIGNAL
};

65 66 67 68 69 70 71
typedef struct {
  GFile *file;
  guint32 last_sent_change_time; /* 0 == not sent */
  guint32 send_delayed_change_at; /* 0 == never */
  guint32 send_virtual_changes_done_at; /* 0 == never */
} RateLimiter;

72 73 74 75 76
struct _GFileMonitorPrivate {
  gboolean cancelled;
  int rate_limit_msec;

  /* Rate limiting change events */
77
  GHashTable *rate_limiter;
78

79
  GMutex mutex;
80
  GSource *pending_file_change_source;
81 82
  GSList *pending_file_changes; /* FileChange */

83 84
  GSource *timeout;
  guint32 timeout_fires_at;
85 86

  GMainContext *context;
87 88
};

89 90 91
enum {
  PROP_0,
  PROP_RATE_LIMIT,
92 93
  PROP_CANCELLED,
  PROP_CONTEXT
94 95
};

96 97 98 99 100
/* work around a limitation of the aliasing foo */
#undef g_file_monitor

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static void
g_file_monitor_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GFileMonitor *monitor;

  monitor = G_FILE_MONITOR (object);

  switch (prop_id)
    {
    case PROP_RATE_LIMIT:
      g_file_monitor_set_rate_limit (monitor, g_value_get_int (value));
      break;

117 118 119 120 121 122
    case PROP_CONTEXT:
      monitor->priv->context = g_value_dup_boxed (value);
      if (monitor->priv->context == NULL)
        monitor->priv->context = g_main_context_ref_thread_default ();
      break;

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
g_file_monitor_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GFileMonitor *monitor;
  GFileMonitorPrivate *priv;

  monitor = G_FILE_MONITOR (object);
  priv = monitor->priv;

  switch (prop_id)
    {
    case PROP_RATE_LIMIT:
      g_value_set_int (value, priv->rate_limit_msec);
      break;

    case PROP_CANCELLED:
148
      G_LOCK (cancelled);
149
      g_value_set_boolean (value, priv->cancelled);
150
      G_UNLOCK (cancelled);
151 152 153 154 155 156 157 158
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

159 160 161 162 163
#define DEFAULT_RATE_LIMIT_MSECS 800
#define DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS 2

static guint signals[LAST_SIGNAL] = { 0 };

164 165 166 167 168 169
static void
rate_limiter_free (RateLimiter *limiter)
{
  g_object_unref (limiter->file);
  g_slice_free (RateLimiter, limiter);
}
170

171 172 173 174 175 176 177
static void
g_file_monitor_finalize (GObject *object)
{
  GFileMonitor *monitor;

  monitor = G_FILE_MONITOR (object);

178 179 180 181 182
  if (monitor->priv->timeout)
    {
      g_source_destroy (monitor->priv->timeout);
      g_source_unref (monitor->priv->timeout);
    }
183

184
  g_hash_table_destroy (monitor->priv->rate_limiter);
185

186
  g_main_context_unref (monitor->priv->context);
187
  g_mutex_clear (&monitor->priv->mutex);
188

189
  G_OBJECT_CLASS (g_file_monitor_parent_class)->finalize (object);
190 191 192 193 194 195
}

static void
g_file_monitor_dispose (GObject *object)
{
  GFileMonitor *monitor;
196
  GFileMonitorPrivate *priv;
197 198
  
  monitor = G_FILE_MONITOR (object);
199 200
  priv = monitor->priv;

201
  if (priv->pending_file_change_source)
202
    {
203 204 205
      g_source_destroy (priv->pending_file_change_source);
      g_source_unref (priv->pending_file_change_source);
      priv->pending_file_change_source = NULL;
206
    }
207
  g_slist_free_full (priv->pending_file_changes, (GDestroyNotify) file_change_free);
208
  priv->pending_file_changes = NULL;
209 210

  /* Make sure we cancel on last unref */
211
  g_file_monitor_cancel (monitor);
212 213

  G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
214 215 216 217 218
}

static void
g_file_monitor_class_init (GFileMonitorClass *klass)
{
219
  GObjectClass *object_class;
220

221 222 223 224 225
  object_class = G_OBJECT_CLASS (klass);
  object_class->finalize = g_file_monitor_finalize;
  object_class->dispose = g_file_monitor_dispose;
  object_class->get_property = g_file_monitor_get_property;
  object_class->set_property = g_file_monitor_set_property;
226

227 228 229 230
  /**
   * GFileMonitor::changed:
   * @monitor: a #GFileMonitor.
   * @file: a #GFile.
231
   * @other_file: (allow-none): a #GFile or #NULL.
232
   * @event_type: a #GFileMonitorEvent.
233 234 235 236
   *
   * Emitted when @file has been changed.
   *
   * If using #G_FILE_MONITOR_SEND_MOVED flag and @event_type is
Dan Winship's avatar
Dan Winship committed
237
   * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
238 239 240
   * old path, and @other_file will be set to a #GFile containing the new path.
   *
   * In all the other cases, @other_file will be set to #NULL.
241
   **/
242 243 244 245 246 247
  signals[CHANGED] =
    g_signal_new (I_("changed"),
		  G_TYPE_FILE_MONITOR,
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GFileMonitorClass, changed),
		  NULL, NULL,
248
		  NULL,
249
		  G_TYPE_NONE, 3,
250
		  G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

  g_object_class_install_property (object_class,
                                   PROP_RATE_LIMIT,
                                   g_param_spec_int ("rate-limit",
                                                     P_("Rate limit"),
                                                     P_("The limit of the monitor to watch for changes, in milliseconds"),
                                                     0, G_MAXINT,
                                                     DEFAULT_RATE_LIMIT_MSECS,
                                                     G_PARAM_READWRITE|
                                                     G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));

  g_object_class_install_property (object_class,
                                   PROP_CANCELLED,
                                   g_param_spec_boolean ("cancelled",
                                                         P_("Cancelled"),
                                                         P_("Whether the monitor has been cancelled"),
                                                         FALSE,
                                                         G_PARAM_READABLE|
                                                         G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
270 271 272 273 274 275 276 277

  g_object_class_install_property (object_class,
                                   PROP_CONTEXT,
                                   g_param_spec_boxed ("context",
                                                       P_("Context"),
                                                       P_("The main context to dispatch from"),
                                                       G_TYPE_MAIN_CONTEXT, G_PARAM_WRITABLE |
                                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
278 279 280 281 282
}

static void
g_file_monitor_init (GFileMonitor *monitor)
{
283
  monitor->priv = g_file_monitor_get_instance_private (monitor);
284
  monitor->priv->rate_limit_msec = DEFAULT_RATE_LIMIT_MSECS;
285 286
  monitor->priv->rate_limiter = g_hash_table_new_full (g_file_hash, (GEqualFunc)g_file_equal,
						       NULL, (GDestroyNotify) rate_limiter_free);
287
  g_mutex_init (&monitor->priv->mutex);
288 289 290 291
}

/**
 * g_file_monitor_is_cancelled:
Matthias Clasen's avatar
Matthias Clasen committed
292
 * @monitor: a #GFileMonitor
293
 * 
Matthias Clasen's avatar
Matthias Clasen committed
294 295
 * Returns whether the monitor is canceled.
 *
296 297 298 299 300
 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
 **/
gboolean
g_file_monitor_is_cancelled (GFileMonitor *monitor)
{
301
  gboolean res;
302

303 304
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);

305 306 307 308 309
  G_LOCK (cancelled);
  res = monitor->priv->cancelled;
  G_UNLOCK (cancelled);
  
  return res;
310 311 312 313
}

/**
 * g_file_monitor_cancel:
314 315 316
 * @monitor: a #GFileMonitor.
 * 
 * Cancels a file monitor.
317 318 319 320 321 322 323 324 325 326
 * 
 * Returns: %TRUE if monitor was cancelled.
 **/
gboolean
g_file_monitor_cancel (GFileMonitor* monitor)
{
  GFileMonitorClass *klass;
  
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
  
327
  G_LOCK (cancelled);
328
  if (monitor->priv->cancelled)
329 330 331 332
    {
      G_UNLOCK (cancelled);
      return TRUE;
    }
333 334
  
  monitor->priv->cancelled = TRUE;
335 336
  G_UNLOCK (cancelled);
  
337 338
  g_object_notify (G_OBJECT (monitor), "cancelled");

339 340 341 342 343 344 345
  klass = G_FILE_MONITOR_GET_CLASS (monitor);
  return (* klass->cancel) (monitor);
}

/**
 * g_file_monitor_set_rate_limit:
 * @monitor: a #GFileMonitor.
346 347
 * @limit_msecs: a non-negative integer with the limit in milliseconds
 *     to poll for changes
348
 *
349
 * Sets the rate limit to which the @monitor will report
350 351
 * consecutive change events to the same file.
 */
352 353
void
g_file_monitor_set_rate_limit (GFileMonitor *monitor,
354
                               gint          limit_msecs)
355
{
356
  GFileMonitorPrivate *priv;
357

358
  g_return_if_fail (G_IS_FILE_MONITOR (monitor));
359 360
  g_return_if_fail (limit_msecs >= 0);

361 362 363 364 365 366
  priv = monitor->priv;
  if (priv->rate_limit_msec != limit_msecs)
    {
      monitor->priv->rate_limit_msec = limit_msecs;
      g_object_notify (G_OBJECT (monitor), "rate-limit");
    }
367 368
}

369
struct _FileChange {
370 371 372
  GFile             *child;
  GFile             *other_file;
  GFileMonitorEvent  event_type;
373
};
374 375 376 377 378 379 380 381 382 383 384

static void
file_change_free (FileChange *change)
{
  g_object_unref (change->child);
  if (change->other_file)
    g_object_unref (change->other_file);
  
  g_slice_free (FileChange, change);
}

385 386 387 388 389
static gboolean
emit_cb (gpointer data)
{
  GFileMonitor *monitor = G_FILE_MONITOR (data);
  GSList *pending, *iter;
390 391

  g_mutex_lock (&monitor->priv->mutex);
392 393
  pending = g_slist_reverse (monitor->priv->pending_file_changes);
  monitor->priv->pending_file_changes = NULL;
394 395 396 397 398
  if (monitor->priv->pending_file_change_source)
    {
      g_source_unref (monitor->priv->pending_file_change_source);
      monitor->priv->pending_file_change_source = NULL;
    }
399
  g_mutex_unlock (&monitor->priv->mutex);
400

401
  g_object_ref (monitor);
402 403 404
  for (iter = pending; iter; iter = iter->next)
    {
       FileChange *change = iter->data;
405

406 407 408 409 410
       g_signal_emit (monitor, signals[CHANGED], 0,
	  	      change->child, change->other_file, change->event_type);
       file_change_free (change);
    }
  g_slist_free (pending);
411 412
  g_object_unref (monitor);

413 414 415
  return FALSE;
}

416 417 418 419 420 421 422 423
static void
emit_in_idle (GFileMonitor      *monitor,
	      GFile             *child,
	      GFile             *other_file,
	      GFileMonitorEvent  event_type)
{
  GSource *source;
  FileChange *change;
424 425 426
  GFileMonitorPrivate *priv;

  priv = monitor->priv;
427 428 429 430 431 432 433 434 435 436

  change = g_slice_new (FileChange);

  change->child = g_object_ref (child);
  if (other_file)
    change->other_file = g_object_ref (other_file);
  else
    change->other_file = NULL;
  change->event_type = event_type;

437
  g_mutex_lock (&monitor->priv->mutex);
438
  if (!priv->pending_file_change_source)
439 440
    {
      source = g_idle_source_new ();
441
      priv->pending_file_change_source = source;
442 443
      g_source_set_priority (source, 0);

444
      /* We don't ref monitor here - instead dispose will free any
445 446 447
       * pending idles.
       */
      g_source_set_callback (source, emit_cb, monitor, NULL);
448
      g_source_attach (source, monitor->priv->context);
449 450 451
    }
  /* We reverse this in the processor */
  priv->pending_file_changes = g_slist_prepend (priv->pending_file_changes, change);
452
  g_mutex_unlock (&monitor->priv->mutex);
453 454
}

455 456 457
static guint32
get_time_msecs (void)
{
458
  return g_get_monotonic_time () / G_TIME_SPAN_MILLISECOND;
459 460 461 462 463 464 465 466 467 468 469 470
}

static guint32
time_difference (guint32 from, guint32 to)
{
  if (from > to)
    return 0;
  return to - from;
}

/* Change event rate limiting support: */

471 472 473 474 475 476 477 478 479 480 481 482 483
static RateLimiter *
new_limiter (GFileMonitor *monitor,
	     GFile             *file)
{
  RateLimiter *limiter;

  limiter = g_slice_new0 (RateLimiter);
  limiter->file = g_object_ref (file);
  g_hash_table_insert (monitor->priv->rate_limiter, file, limiter);
  
  return limiter;
}

484
static void
485 486
rate_limiter_send_virtual_changes_done_now (GFileMonitor *monitor, 
                                            RateLimiter  *limiter)
487
{
488
  if (limiter->send_virtual_changes_done_at != 0)
489
    {
490 491
      emit_in_idle (monitor, limiter->file, NULL,
		    G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
492
      limiter->send_virtual_changes_done_at = 0;
493 494 495 496
    }
}

static void
497 498 499
rate_limiter_send_delayed_change_now (GFileMonitor *monitor, 
                                      RateLimiter *limiter, 
                                      guint32 time_now)
500
{
501
  if (limiter->send_delayed_change_at != 0)
502
    {
503 504 505
      emit_in_idle (monitor, 
		    limiter->file, NULL,
		    G_FILE_MONITOR_EVENT_CHANGED);
506 507
      limiter->send_delayed_change_at = 0;
      limiter->last_sent_change_time = time_now;
508 509 510
    }
}

511 512 513 514 515 516
typedef struct {
  guint32 min_time;
  guint32 time_now;
  GFileMonitor *monitor;
} ForEachData;

517
static gboolean
518 519 520 521
calc_min_time (GFileMonitor *monitor, 
               RateLimiter *limiter, 
               guint32 time_now, 
               guint32 *min_time)
522
{
523 524
  gboolean delete_me;
  guint32 expire_at;
525

526
  delete_me = TRUE;
527

528
  if (limiter->last_sent_change_time != 0)
529
    {
Matthias Clasen's avatar
Matthias Clasen committed
530
      /* Set a timeout at 2*rate limit so that we can clear out the change from the hash eventually */
531 532 533 534 535 536 537 538
      expire_at = limiter->last_sent_change_time + 2 * monitor->priv->rate_limit_msec;

      if (time_difference (time_now, expire_at) > 0)
	{
	  delete_me = FALSE;
	  *min_time = MIN (*min_time,
			   time_difference (time_now, expire_at));
	}
539 540
    }

541
  if (limiter->send_delayed_change_at != 0)
542
    {
543 544 545
      delete_me = FALSE;
      *min_time = MIN (*min_time,
		       time_difference (time_now, limiter->send_delayed_change_at));
546 547
    }

548
  if (limiter->send_virtual_changes_done_at != 0)
549
    {
550 551 552
      delete_me = FALSE;
      *min_time = MIN (*min_time,
		       time_difference (time_now, limiter->send_virtual_changes_done_at));
553
    }
554 555

  return delete_me;
556 557 558
}

static gboolean
559 560 561
foreach_rate_limiter_fire (gpointer key,
			   gpointer value,
			   gpointer user_data)
562
{
563 564
  RateLimiter *limiter = value;
  ForEachData *data = user_data;
565

566 567 568
  if (limiter->send_delayed_change_at != 0 &&
      time_difference (data->time_now, limiter->send_delayed_change_at) == 0)
    rate_limiter_send_delayed_change_now (data->monitor, limiter, data->time_now);
569
  
570 571 572 573 574
  if (limiter->send_virtual_changes_done_at != 0 &&
      time_difference (data->time_now, limiter->send_virtual_changes_done_at) == 0)
    rate_limiter_send_virtual_changes_done_now (data->monitor, limiter);
  
  return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
575 576
}

577 578
static gboolean 
rate_limiter_timeout (gpointer timeout_data)
579
{
580 581
  GFileMonitor *monitor = timeout_data;
  ForEachData data;
582 583
  GSource *source;
  
584 585 586 587 588 589
  data.min_time = G_MAXUINT32;
  data.monitor = monitor;
  data.time_now = get_time_msecs ();
  g_hash_table_foreach_remove (monitor->priv->rate_limiter,
			       foreach_rate_limiter_fire,
			       &data);
590
  
591 592 593 594 595 596 597 598 599 600 601 602 603 604
  /* Remove old timeout */
  if (monitor->priv->timeout)
    {
      g_source_destroy (monitor->priv->timeout);
      g_source_unref (monitor->priv->timeout);
      monitor->priv->timeout = NULL;
      monitor->priv->timeout_fires_at = 0;
    }
  
  /* Set up new timeout */
  if (data.min_time != G_MAXUINT32)
    {
      source = g_timeout_source_new (data.min_time + 1); /* + 1 to make sure we've really passed the time */
      g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
605
      g_source_attach (source, monitor->priv->context);
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
      
      monitor->priv->timeout = source;
      monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
    }
  
  return FALSE;
}

static gboolean
foreach_rate_limiter_update (gpointer key,
			     gpointer value,
			     gpointer user_data)
{
  RateLimiter *limiter = value;
  ForEachData *data = user_data;

  return calc_min_time (data->monitor, limiter, data->time_now, &data->min_time);
623 624 625
}

static void
626 627
update_rate_limiter_timeout (GFileMonitor *monitor, 
                             guint new_time)
628
{
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
  ForEachData data;
  GSource *source;
  
  if (monitor->priv->timeout_fires_at != 0 && new_time != 0 &&
      time_difference (new_time, monitor->priv->timeout_fires_at) == 0)
    return; /* Nothing to do, we already fire earlier than that */

  data.min_time = G_MAXUINT32;
  data.monitor = monitor;
  data.time_now = get_time_msecs ();
  g_hash_table_foreach_remove (monitor->priv->rate_limiter,
			       foreach_rate_limiter_update,
			       &data);

  /* Remove old timeout */
  if (monitor->priv->timeout)
    {
      g_source_destroy (monitor->priv->timeout);
      g_source_unref (monitor->priv->timeout);
      monitor->priv->timeout_fires_at = 0;
      monitor->priv->timeout = NULL;
    }

  /* Set up new timeout */
  if (data.min_time != G_MAXUINT32)
654
    {
655 656
      source = g_timeout_source_new (data.min_time + 1);  /* + 1 to make sure we've really passed the time */
      g_source_set_callback (source, rate_limiter_timeout, monitor, NULL);
657
      g_source_attach (source, monitor->priv->context);
658
      
659 660
      monitor->priv->timeout = source;
      monitor->priv->timeout_fires_at = data.time_now + data.min_time; 
661 662 663 664 665
    }
}

/**
 * g_file_monitor_emit_event:
666
 * @monitor: a #GFileMonitor.
667
 * @child: a #GFile.
668
 * @other_file: a #GFile.
669
 * @event_type: a set of #GFileMonitorEvent flags.
670
 * 
671 672 673
 * Emits the #GFileMonitor::changed signal if a change
 * has taken place. Should be called from file monitor 
 * implementations only.
674
 *
675 676 677
 * The signal will be emitted from an idle handler (in the <link
 * linkend="g-main-context-push-thread-default">thread-default main
 * context</link>).
678 679
 **/
void
Matthias Clasen's avatar
Matthias Clasen committed
680 681 682 683
g_file_monitor_emit_event (GFileMonitor      *monitor,
			   GFile             *child,
			   GFile             *other_file,
			   GFileMonitorEvent  event_type)
684 685 686
{
  guint32 time_now, since_last;
  gboolean emit_now;
687
  RateLimiter *limiter;
688 689

  g_return_if_fail (G_IS_FILE_MONITOR (monitor));
690 691 692
  g_return_if_fail (G_IS_FILE (child));

  limiter = g_hash_table_lookup (monitor->priv->rate_limiter, child);
693 694 695

  if (event_type != G_FILE_MONITOR_EVENT_CHANGED)
    {
696 697 698 699 700 701 702 703 704
      if (limiter)
	{
	  rate_limiter_send_delayed_change_now (monitor, limiter, get_time_msecs ());
	  if (event_type == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
	    limiter->send_virtual_changes_done_at = 0;
	  else
	    rate_limiter_send_virtual_changes_done_now (monitor, limiter);
	  update_rate_limiter_timeout (monitor, 0);
	}
705
      emit_in_idle (monitor, child, other_file, event_type);
706 707 708
    }
  else
    {
709
      /* Changed event, rate limit */
710 711 712
      time_now = get_time_msecs ();
      emit_now = TRUE;
      
713
      if (limiter)
714
	{
715
	  since_last = time_difference (limiter->last_sent_change_time, time_now);
716 717 718 719 720
	  if (since_last < monitor->priv->rate_limit_msec)
	    {
	      /* We ignore this change, but arm a timer so that we can fire it later if we
		 don't get any other events (that kill this timeout) */
	      emit_now = FALSE;
721 722 723 724 725
	      if (limiter->send_delayed_change_at == 0)
		{
		  limiter->send_delayed_change_at = time_now + monitor->priv->rate_limit_msec;
		  update_rate_limiter_timeout (monitor, limiter->send_delayed_change_at);
		}
726 727 728
	    }
	}
      
729 730 731
      if (limiter == NULL)
	limiter = new_limiter (monitor, child);
      
732 733
      if (emit_now)
	{
734
	  emit_in_idle (monitor, child, other_file, event_type);
735
	  
736 737
	  limiter->last_sent_change_time = time_now;
	  limiter->send_delayed_change_at = 0;
Matthias Clasen's avatar
Matthias Clasen committed
738
	  /* Set a timeout of 2*rate limit so that we can clear out the change from the hash eventually */
739
	  update_rate_limiter_timeout (monitor, time_now + 2 * monitor->priv->rate_limit_msec);
740
	}
741
      
742 743
      /* Schedule a virtual change done. This is removed if we get a real one, and
	 postponed if we get more change events. */
744 745 746
      
      limiter->send_virtual_changes_done_at = time_now + DEFAULT_VIRTUAL_CHANGES_DONE_DELAY_SECS * 1000;
      update_rate_limiter_timeout (monitor, limiter->send_virtual_changes_done_at);
747 748
    }
}