Blame gio/gfilemonitor.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gfilemonitor.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gvfs.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gfilemonitor
Packit ae235b
 * @short_description: File Monitor
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Monitors a file or directory for changes.
Packit ae235b
 *
Packit ae235b
 * To obtain a #GFileMonitor for a file or directory, use
Packit ae235b
 * g_file_monitor(), g_file_monitor_file(), or
Packit ae235b
 * g_file_monitor_directory().
Packit ae235b
 *
Packit ae235b
 * To get informed about changes to the file or directory you are
Packit ae235b
 * monitoring, connect to the #GFileMonitor::changed signal. The
Packit ae235b
 * signal will be emitted in the
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * of the thread that the monitor was created in
Packit ae235b
 * (though if the global default main context is blocked, this may
Packit ae235b
 * cause notifications to be blocked even if the thread-default
Packit ae235b
 * context is still running).
Packit ae235b
 **/
Packit ae235b
Packit ae235b
#define DEFAULT_RATE_LIMIT_MSECS 800
Packit ae235b
Packit ae235b
struct _GFileMonitorPrivate
Packit ae235b
{
Packit ae235b
  gboolean cancelled;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  PROP_0,
Packit ae235b
  PROP_RATE_LIMIT,
Packit ae235b
  PROP_CANCELLED
Packit ae235b
};
Packit ae235b
Packit ae235b
static guint g_file_monitor_changed_signal;
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_monitor_set_property (GObject      *object,
Packit ae235b
                             guint         prop_id,
Packit ae235b
                             const GValue *value,
Packit ae235b
                             GParamSpec   *pspec)
Packit ae235b
{
Packit ae235b
  //GFileMonitor *monitor;
Packit ae235b
Packit ae235b
  //monitor = G_FILE_MONITOR (object);
Packit ae235b
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_RATE_LIMIT:
Packit ae235b
      /* not supported by default */
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_monitor_get_property (GObject    *object,
Packit ae235b
                             guint       prop_id,
Packit ae235b
                             GValue     *value,
Packit ae235b
                             GParamSpec *pspec)
Packit ae235b
{
Packit ae235b
  switch (prop_id)
Packit ae235b
    {
Packit ae235b
    case PROP_RATE_LIMIT:
Packit ae235b
      /* we expect this to be overridden... */
Packit ae235b
      g_value_set_int (value, DEFAULT_RATE_LIMIT_MSECS);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case PROP_CANCELLED:
Packit ae235b
      //g_mutex_lock (&fms->lock);
Packit ae235b
      g_value_set_boolean (value, FALSE);//fms->cancelled);
Packit ae235b
      //g_mutex_unlock (&fms->lock);
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_monitor_dispose (GObject *object)
Packit ae235b
{
Packit ae235b
  GFileMonitor *monitor = G_FILE_MONITOR (object);
Packit ae235b
Packit ae235b
  /* Make sure we cancel on last unref */
Packit ae235b
  g_file_monitor_cancel (monitor);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_monitor_init (GFileMonitor *monitor)
Packit ae235b
{
Packit ae235b
  monitor->priv = g_file_monitor_get_instance_private (monitor);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_file_monitor_class_init (GFileMonitorClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class;
Packit ae235b
Packit ae235b
  object_class = G_OBJECT_CLASS (klass);
Packit ae235b
  object_class->dispose = g_file_monitor_dispose;
Packit ae235b
  object_class->get_property = g_file_monitor_get_property;
Packit ae235b
  object_class->set_property = g_file_monitor_set_property;
Packit ae235b
Packit ae235b
  /**
Packit ae235b
   * GFileMonitor::changed:
Packit ae235b
   * @monitor: a #GFileMonitor.
Packit ae235b
   * @file: a #GFile.
Packit ae235b
   * @other_file: (nullable): a #GFile or #NULL.
Packit ae235b
   * @event_type: a #GFileMonitorEvent.
Packit ae235b
   *
Packit ae235b
   * Emitted when @file has been changed.
Packit ae235b
   *
Packit ae235b
   * If using %G_FILE_MONITOR_WATCH_MOVES on a directory monitor, and
Packit ae235b
   * the information is available (and if supported by the backend),
Packit ae235b
   * @event_type may be %G_FILE_MONITOR_EVENT_RENAMED,
Packit ae235b
   * %G_FILE_MONITOR_EVENT_MOVED_IN or %G_FILE_MONITOR_EVENT_MOVED_OUT.
Packit ae235b
   *
Packit ae235b
   * In all cases @file will be a child of the monitored directory.  For
Packit ae235b
   * renames, @file will be the old name and @other_file is the new
Packit ae235b
   * name.  For "moved in" events, @file is the name of the file that
Packit ae235b
   * appeared and @other_file is the old name that it was moved from (in
Packit ae235b
   * another directory).  For "moved out" events, @file is the name of
Packit ae235b
   * the file that used to be in this directory and @other_file is the
Packit ae235b
   * name of the file at its new location.
Packit ae235b
   *
Packit ae235b
   * It makes sense to treat %G_FILE_MONITOR_EVENT_MOVED_IN as
Packit ae235b
   * equivalent to %G_FILE_MONITOR_EVENT_CREATED and
Packit ae235b
   * %G_FILE_MONITOR_EVENT_MOVED_OUT as equivalent to
Packit ae235b
   * %G_FILE_MONITOR_EVENT_DELETED, with extra information.
Packit ae235b
   * %G_FILE_MONITOR_EVENT_RENAMED is equivalent to a delete/create
Packit ae235b
   * pair.  This is exactly how the events will be reported in the case
Packit ae235b
   * that the %G_FILE_MONITOR_WATCH_MOVES flag is not in use.
Packit ae235b
   *
Packit ae235b
   * If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is
Packit ae235b
   * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
Packit ae235b
   * old path, and @other_file will be set to a #GFile containing the new path.
Packit ae235b
   *
Packit ae235b
   * In all the other cases, @other_file will be set to #NULL.
Packit ae235b
   **/
Packit ae235b
  g_file_monitor_changed_signal = g_signal_new (I_("changed"),
Packit ae235b
                                                G_TYPE_FILE_MONITOR,
Packit ae235b
                                                G_SIGNAL_RUN_LAST,
Packit ae235b
                                                G_STRUCT_OFFSET (GFileMonitorClass, changed),
Packit ae235b
                                                NULL, NULL,
Packit ae235b
                                                NULL,
Packit ae235b
                                                G_TYPE_NONE, 3,
Packit ae235b
                                                G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
Packit ae235b
Packit ae235b
  g_object_class_install_property (object_class, PROP_RATE_LIMIT,
Packit ae235b
                                   g_param_spec_int ("rate-limit",
Packit ae235b
                                                     P_("Rate limit"),
Packit ae235b
                                                     P_("The limit of the monitor to watch for changes, in milliseconds"),
Packit ae235b
                                                     0, G_MAXINT, DEFAULT_RATE_LIMIT_MSECS, G_PARAM_READWRITE |
Packit ae235b
                                                     G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
Packit ae235b
Packit ae235b
  g_object_class_install_property (object_class, PROP_CANCELLED,
Packit ae235b
                                   g_param_spec_boolean ("cancelled",
Packit ae235b
                                                         P_("Cancelled"),
Packit ae235b
                                                         P_("Whether the monitor has been cancelled"),
Packit ae235b
                                                         FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_monitor_is_cancelled:
Packit ae235b
 * @monitor: a #GFileMonitor
Packit ae235b
 * 
Packit ae235b
 * Returns whether the monitor is canceled.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_monitor_is_cancelled (GFileMonitor *monitor)
Packit ae235b
{
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
Packit ae235b
Packit ae235b
  res = monitor->priv->cancelled;
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_monitor_cancel:
Packit ae235b
 * @monitor: a #GFileMonitor.
Packit ae235b
 *
Packit ae235b
 * Cancels a file monitor.
Packit ae235b
 *
Packit ae235b
 * Returns: always %TRUE
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_file_monitor_cancel (GFileMonitor *monitor)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
Packit ae235b
Packit ae235b
  if (!monitor->priv->cancelled)
Packit ae235b
    {
Packit ae235b
      G_FILE_MONITOR_GET_CLASS (monitor)->cancel (monitor);
Packit ae235b
Packit ae235b
      monitor->priv->cancelled = TRUE;
Packit ae235b
      g_object_notify (G_OBJECT (monitor), "cancelled");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_monitor_set_rate_limit:
Packit ae235b
 * @monitor: a #GFileMonitor.
Packit ae235b
 * @limit_msecs: a non-negative integer with the limit in milliseconds
Packit ae235b
 *     to poll for changes
Packit ae235b
 *
Packit ae235b
 * Sets the rate limit to which the @monitor will report
Packit ae235b
 * consecutive change events to the same file.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_file_monitor_set_rate_limit (GFileMonitor *monitor,
Packit ae235b
                               gint          limit_msecs)
Packit ae235b
{
Packit ae235b
  g_object_set (monitor, "rate-limit", limit_msecs, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_file_monitor_emit_event:
Packit ae235b
 * @monitor: a #GFileMonitor.
Packit ae235b
 * @child: a #GFile.
Packit ae235b
 * @other_file: a #GFile.
Packit ae235b
 * @event_type: a set of #GFileMonitorEvent flags.
Packit ae235b
 *
Packit ae235b
 * Emits the #GFileMonitor::changed signal if a change
Packit ae235b
 * has taken place. Should be called from file monitor
Packit ae235b
 * implementations only.
Packit ae235b
 *
Packit ae235b
 * Implementations are responsible to call this method from the
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default] of the
Packit ae235b
 * thread that the monitor was created in.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_file_monitor_emit_event (GFileMonitor      *monitor,
Packit ae235b
                           GFile             *child,
Packit ae235b
                           GFile             *other_file,
Packit ae235b
                           GFileMonitorEvent  event_type)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_FILE_MONITOR (monitor));
Packit ae235b
  g_return_if_fail (G_IS_FILE (child));
Packit ae235b
  g_return_if_fail (!other_file || G_IS_FILE (other_file));
Packit ae235b
Packit ae235b
  if (monitor->priv->cancelled)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  g_signal_emit (monitor, g_file_monitor_changed_signal, 0, child, other_file, event_type);
Packit ae235b
}