2007-11-26 17:13:05 +01:00
|
|
|
/* 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
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "gfilemonitor.h"
|
2008-01-07 14:56:10 +01:00
|
|
|
#include "gioenumtypes.h"
|
2008-07-01 08:32:35 +02:00
|
|
|
#include "gfile.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include "gvfs.h"
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* SECTION:gfilemonitor
|
|
|
|
* @short_description: File Monitor
|
2008-02-21 19:20:17 +01:00
|
|
|
* @include: gio/gio.h
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2008-01-07 14:42:08 +01:00
|
|
|
* Monitors a file or directory for changes.
|
2008-01-25 18:38:04 +01:00
|
|
|
*
|
|
|
|
* To obtain a #GFileMonitor for a file or directory, use
|
2008-08-05 19:51:13 +02:00
|
|
|
* g_file_monitor(), g_file_monitor_file(), or
|
|
|
|
* g_file_monitor_directory().
|
2008-01-25 18:38:04 +01:00
|
|
|
*
|
2009-06-17 02:22:58 +02:00
|
|
|
* To get informed about changes to the file or directory you are
|
|
|
|
* monitoring, connect to the #GFileMonitor::changed signal. The
|
2014-02-08 18:26:56 +01:00
|
|
|
* signal will be emitted in the
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]
|
|
|
|
* of the thread that the monitor was created in
|
2009-06-17 02:22:58 +02:00
|
|
|
* (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).
|
2007-11-27 15:00:13 +01:00
|
|
|
**/
|
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
#define DEFAULT_RATE_LIMIT_MSECS 800
|
2008-01-07 14:42:08 +01:00
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
struct _GFileMonitorPrivate
|
|
|
|
{
|
2007-11-26 17:13:05 +01:00
|
|
|
gboolean cancelled;
|
|
|
|
};
|
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2007-12-01 07:12:45 +01:00
|
|
|
PROP_0,
|
|
|
|
PROP_RATE_LIMIT,
|
2015-01-12 20:59:35 +01:00
|
|
|
PROP_CANCELLED
|
2007-12-01 07:12:45 +01:00
|
|
|
};
|
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
static guint g_file_monitor_changed_signal;
|
2013-06-11 01:29:58 +02:00
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
static void
|
|
|
|
g_file_monitor_set_property (GObject *object,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
2015-01-12 20:59:35 +01:00
|
|
|
//GFileMonitor *monitor;
|
2007-12-01 07:12:45 +01:00
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
//monitor = G_FILE_MONITOR (object);
|
2007-12-01 07:12:45 +01:00
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_RATE_LIMIT:
|
2015-01-12 20:59:35 +01:00
|
|
|
/* not supported by default */
|
2013-07-25 20:17:09 +02:00
|
|
|
break;
|
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
switch (prop_id)
|
|
|
|
{
|
|
|
|
case PROP_RATE_LIMIT:
|
2015-01-12 20:59:35 +01:00
|
|
|
/* we expect this to be overridden... */
|
|
|
|
g_value_set_int (value, DEFAULT_RATE_LIMIT_MSECS);
|
2007-12-01 07:12:45 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_CANCELLED:
|
2015-01-12 20:59:35 +01:00
|
|
|
//g_mutex_lock (&fms->lock);
|
|
|
|
g_value_set_boolean (value, FALSE);//fms->cancelled);
|
|
|
|
//g_mutex_unlock (&fms->lock);
|
2007-12-01 07:12:45 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static void
|
|
|
|
g_file_monitor_dispose (GObject *object)
|
|
|
|
{
|
2015-01-12 20:59:35 +01:00
|
|
|
GFileMonitor *monitor = G_FILE_MONITOR (object);
|
2007-11-26 17:13:05 +01:00
|
|
|
|
|
|
|
/* Make sure we cancel on last unref */
|
2008-03-05 12:50:27 +01:00
|
|
|
g_file_monitor_cancel (monitor);
|
2008-06-16 11:54:04 +02:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
static void
|
|
|
|
g_file_monitor_init (GFileMonitor *monitor)
|
|
|
|
{
|
|
|
|
monitor->priv = g_file_monitor_get_instance_private (monitor);
|
|
|
|
}
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
static void
|
|
|
|
g_file_monitor_class_init (GFileMonitorClass *klass)
|
|
|
|
{
|
2007-12-01 07:12:45 +01:00
|
|
|
GObjectClass *object_class;
|
2013-06-11 01:29:58 +02:00
|
|
|
|
2007-12-01 07:12:45 +01:00
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
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;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* GFileMonitor::changed:
|
|
|
|
* @monitor: a #GFileMonitor.
|
|
|
|
* @file: a #GFile.
|
2012-01-07 18:54:53 +01:00
|
|
|
* @other_file: (allow-none): a #GFile or #NULL.
|
2007-11-27 15:00:13 +01:00
|
|
|
* @event_type: a #GFileMonitorEvent.
|
2010-11-25 18:38:33 +01:00
|
|
|
*
|
|
|
|
* Emitted when @file has been changed.
|
|
|
|
*
|
2015-01-12 20:59:35 +01:00
|
|
|
* If using %G_FILE_MONITOR_WATCH_RENAMES on a directory monitor, and
|
|
|
|
* the information is available (and if supported by the backend),
|
|
|
|
* @event_type may be %G_FILE_MONITOR_EVENT_RENAMED,
|
|
|
|
* %G_FILE_MONITOR_EVENT_MOVED_IN or %G_FILE_MONITOR_EVENT_MOVED_OUT.
|
|
|
|
*
|
|
|
|
* In all cases @file will be a child of the monitored directory. For
|
|
|
|
* renames, @file will be the old name and @other_file is the new
|
|
|
|
* name. For "moved in" events, @file is the name of the file that
|
|
|
|
* appeared and @other_file is the old name that it was moved from (in
|
|
|
|
* another directory). For "moved out" events, @file is the name of
|
|
|
|
* the file that used to be in this directory and @other_file is the
|
|
|
|
* name of the file at its new location.
|
|
|
|
*
|
|
|
|
* It makes sense to treat %G_FILE_MONITOR_EVENT_MOVED_IN as
|
|
|
|
* equivalent to %G_FILE_MONITOR_EVENT_CREATED and
|
|
|
|
* %G_FILE_MONITOR_EVENT_MOVED_OUT as equivalent to
|
|
|
|
* %G_FILE_MONITOR_EVENT_DELETED, with extra information.
|
|
|
|
* %G_FILE_MONITOR_EVENT_RENAMED is equivalent to a delete/create
|
|
|
|
* pair. This is exactly how the events will be reported in the case
|
|
|
|
* that the %G_FILE_MONITOR_WATCH_RENAMES flag is not in use.
|
|
|
|
*
|
|
|
|
* If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is
|
2012-03-27 18:45:27 +02:00
|
|
|
* #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
|
2010-11-25 18:38:33 +01:00
|
|
|
* 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.
|
2007-11-27 15:00:13 +01:00
|
|
|
**/
|
2015-01-12 20:59:35 +01:00
|
|
|
g_file_monitor_changed_signal = g_signal_new (I_("changed"),
|
|
|
|
G_TYPE_FILE_MONITOR,
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (GFileMonitorClass, changed),
|
|
|
|
NULL, NULL,
|
|
|
|
NULL,
|
|
|
|
G_TYPE_NONE, 3,
|
|
|
|
G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_RATE_LIMIT,
|
2007-12-01 07:12:45 +01:00
|
|
|
g_param_spec_int ("rate-limit",
|
|
|
|
P_("Rate limit"),
|
|
|
|
P_("The limit of the monitor to watch for changes, in milliseconds"),
|
2015-01-12 20:59:35 +01:00
|
|
|
0, G_MAXINT, DEFAULT_RATE_LIMIT_MSECS, G_PARAM_READWRITE |
|
|
|
|
G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
|
2007-12-01 07:12:45 +01:00
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
g_object_class_install_property (object_class, PROP_CANCELLED,
|
2007-12-01 07:12:45 +01:00
|
|
|
g_param_spec_boolean ("cancelled",
|
|
|
|
P_("Cancelled"),
|
|
|
|
P_("Whether the monitor has been cancelled"),
|
2015-01-12 20:59:35 +01:00
|
|
|
FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_monitor_is_cancelled:
|
2007-11-28 07:01:13 +01:00
|
|
|
* @monitor: a #GFileMonitor
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-11-28 07:01:13 +01:00
|
|
|
* Returns whether the monitor is canceled.
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* Returns: %TRUE if monitor is canceled. %FALSE otherwise.
|
|
|
|
**/
|
|
|
|
gboolean
|
|
|
|
g_file_monitor_is_cancelled (GFileMonitor *monitor)
|
|
|
|
{
|
2008-03-05 12:50:27 +01:00
|
|
|
gboolean res;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2008-03-14 12:06:01 +01:00
|
|
|
g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
|
|
|
|
|
2008-03-05 12:50:27 +01:00
|
|
|
res = monitor->priv->cancelled;
|
2015-01-12 20:59:35 +01:00
|
|
|
|
2008-03-05 12:50:27 +01:00
|
|
|
return res;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_monitor_cancel:
|
2007-11-27 15:00:13 +01:00
|
|
|
* @monitor: a #GFileMonitor.
|
2015-01-12 20:59:35 +01:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Cancels a file monitor.
|
2015-01-12 20:59:35 +01:00
|
|
|
*
|
|
|
|
* Returns: always %TRUE
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
|
|
|
gboolean
|
2015-01-12 20:59:35 +01:00
|
|
|
g_file_monitor_cancel (GFileMonitor *monitor)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
|
2015-01-12 20:59:35 +01:00
|
|
|
|
|
|
|
if (!monitor->priv->cancelled)
|
2008-03-05 12:50:27 +01:00
|
|
|
{
|
2015-01-12 20:59:35 +01:00
|
|
|
G_FILE_MONITOR_GET_CLASS (monitor)->cancel (monitor);
|
|
|
|
|
|
|
|
monitor->priv->cancelled = TRUE;
|
|
|
|
g_object_notify (G_OBJECT (monitor), "cancelled");
|
2008-03-05 12:50:27 +01:00
|
|
|
}
|
2015-01-12 20:59:35 +01:00
|
|
|
|
|
|
|
return TRUE;
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_file_monitor_set_rate_limit:
|
|
|
|
* @monitor: a #GFileMonitor.
|
2010-11-28 23:14:49 +01:00
|
|
|
* @limit_msecs: a non-negative integer with the limit in milliseconds
|
|
|
|
* to poll for changes
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2007-12-12 13:19:02 +01:00
|
|
|
* Sets the rate limit to which the @monitor will report
|
2010-11-28 23:14:49 +01:00
|
|
|
* consecutive change events to the same file.
|
|
|
|
*/
|
2007-11-26 17:13:05 +01:00
|
|
|
void
|
|
|
|
g_file_monitor_set_rate_limit (GFileMonitor *monitor,
|
2010-11-28 23:14:49 +01:00
|
|
|
gint limit_msecs)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
2015-01-12 20:59:35 +01:00
|
|
|
g_object_set (monitor, "rate-limit", limit_msecs, NULL);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|
|
|
|
|
2015-09-21 19:24:44 +02:00
|
|
|
/**
|
|
|
|
* g_file_monitor_emit_event:
|
|
|
|
* @monitor: a #GFileMonitor.
|
|
|
|
* @child: a #GFile.
|
|
|
|
* @other_file: a #GFile.
|
|
|
|
* @event_type: a set of #GFileMonitorEvent flags.
|
|
|
|
*
|
|
|
|
* Emits the #GFileMonitor::changed signal if a change
|
|
|
|
* has taken place. Should be called from file monitor
|
|
|
|
* implementations only.
|
|
|
|
*
|
|
|
|
* Implementations are responsible to call this method from the
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default] of the
|
|
|
|
* thread that the monitor was created in.
|
|
|
|
**/
|
2007-11-26 17:13:05 +01:00
|
|
|
void
|
2008-01-21 04:49:20 +01:00
|
|
|
g_file_monitor_emit_event (GFileMonitor *monitor,
|
2015-01-12 20:59:35 +01:00
|
|
|
GFile *child,
|
|
|
|
GFile *other_file,
|
|
|
|
GFileMonitorEvent event_type)
|
2007-11-26 17:13:05 +01:00
|
|
|
{
|
|
|
|
g_return_if_fail (G_IS_FILE_MONITOR (monitor));
|
2008-01-07 14:42:08 +01:00
|
|
|
g_return_if_fail (G_IS_FILE (child));
|
2015-01-12 20:59:35 +01:00
|
|
|
g_return_if_fail (!other_file || G_IS_FILE (other_file));
|
2008-01-07 14:42:08 +01:00
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
if (monitor->priv->cancelled)
|
|
|
|
return;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2015-01-12 20:59:35 +01:00
|
|
|
g_signal_emit (monitor, g_file_monitor_changed_signal, 0, child, other_file, event_type);
|
2007-11-26 17:13:05 +01:00
|
|
|
}
|