1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "gfilemonitor.h"
27 #include "gioenumtypes.h"
28 #include "gmarshal-internal.h"
34 * SECTION:gfilemonitor
35 * @short_description: File Monitor
38 * Monitors a file or directory for changes.
40 * To obtain a #GFileMonitor for a file or directory, use
41 * g_file_monitor(), g_file_monitor_file(), or
42 * g_file_monitor_directory().
44 * To get informed about changes to the file or directory you are
45 * monitoring, connect to the #GFileMonitor::changed signal. The
46 * signal will be emitted in the
47 * [thread-default main context][g-main-context-push-thread-default]
48 * of the thread that the monitor was created in
49 * (though if the global default main context is blocked, this may
50 * cause notifications to be blocked even if the thread-default
51 * context is still running).
54 #define DEFAULT_RATE_LIMIT_MSECS 800
56 struct _GFileMonitorPrivate
61 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)
70 static guint g_file_monitor_changed_signal;
73 g_file_monitor_set_property (GObject *object,
78 //GFileMonitor *monitor;
80 //monitor = G_FILE_MONITOR (object);
85 /* not supported by default */
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 g_file_monitor_get_property (GObject *object,
102 case PROP_RATE_LIMIT:
103 /* we expect this to be overridden... */
104 g_value_set_int (value, DEFAULT_RATE_LIMIT_MSECS);
108 //g_mutex_lock (&fms->lock);
109 g_value_set_boolean (value, FALSE);//fms->cancelled);
110 //g_mutex_unlock (&fms->lock);
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
120 g_file_monitor_dispose (GObject *object)
122 GFileMonitor *monitor = G_FILE_MONITOR (object);
124 /* Make sure we cancel on last unref */
125 g_file_monitor_cancel (monitor);
127 G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
131 g_file_monitor_init (GFileMonitor *monitor)
133 monitor->priv = g_file_monitor_get_instance_private (monitor);
137 g_file_monitor_class_init (GFileMonitorClass *klass)
139 GObjectClass *object_class;
141 object_class = G_OBJECT_CLASS (klass);
142 object_class->dispose = g_file_monitor_dispose;
143 object_class->get_property = g_file_monitor_get_property;
144 object_class->set_property = g_file_monitor_set_property;
147 * GFileMonitor::changed:
148 * @monitor: a #GFileMonitor.
150 * @other_file: (nullable): a #GFile or #NULL.
151 * @event_type: a #GFileMonitorEvent.
153 * Emitted when @file has been changed.
155 * If using %G_FILE_MONITOR_WATCH_MOVES on a directory monitor, and
156 * the information is available (and if supported by the backend),
157 * @event_type may be %G_FILE_MONITOR_EVENT_RENAMED,
158 * %G_FILE_MONITOR_EVENT_MOVED_IN or %G_FILE_MONITOR_EVENT_MOVED_OUT.
160 * In all cases @file will be a child of the monitored directory. For
161 * renames, @file will be the old name and @other_file is the new
162 * name. For "moved in" events, @file is the name of the file that
163 * appeared and @other_file is the old name that it was moved from (in
164 * another directory). For "moved out" events, @file is the name of
165 * the file that used to be in this directory and @other_file is the
166 * name of the file at its new location.
168 * It makes sense to treat %G_FILE_MONITOR_EVENT_MOVED_IN as
169 * equivalent to %G_FILE_MONITOR_EVENT_CREATED and
170 * %G_FILE_MONITOR_EVENT_MOVED_OUT as equivalent to
171 * %G_FILE_MONITOR_EVENT_DELETED, with extra information.
172 * %G_FILE_MONITOR_EVENT_RENAMED is equivalent to a delete/create
173 * pair. This is exactly how the events will be reported in the case
174 * that the %G_FILE_MONITOR_WATCH_MOVES flag is not in use.
176 * If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is
177 * %G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
178 * old path, and @other_file will be set to a #GFile containing the new path.
180 * In all the other cases, @other_file will be set to #NULL.
182 g_file_monitor_changed_signal = g_signal_new (I_("changed"),
185 G_STRUCT_OFFSET (GFileMonitorClass, changed),
187 _g_cclosure_marshal_VOID__OBJECT_OBJECT_ENUM,
189 G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
190 g_signal_set_va_marshaller (g_file_monitor_changed_signal,
191 G_TYPE_FROM_CLASS (klass),
192 _g_cclosure_marshal_VOID__OBJECT_OBJECT_ENUMv);
194 g_object_class_install_property (object_class, PROP_RATE_LIMIT,
195 g_param_spec_int ("rate-limit",
197 P_("The limit of the monitor to watch for changes, in milliseconds"),
198 0, G_MAXINT, DEFAULT_RATE_LIMIT_MSECS, G_PARAM_READWRITE |
199 G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
201 g_object_class_install_property (object_class, PROP_CANCELLED,
202 g_param_spec_boolean ("cancelled",
204 P_("Whether the monitor has been cancelled"),
205 FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
209 * g_file_monitor_is_cancelled:
210 * @monitor: a #GFileMonitor
212 * Returns whether the monitor is canceled.
214 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
217 g_file_monitor_is_cancelled (GFileMonitor *monitor)
221 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
223 res = monitor->priv->cancelled;
229 * g_file_monitor_cancel:
230 * @monitor: a #GFileMonitor.
232 * Cancels a file monitor.
234 * Returns: always %TRUE
237 g_file_monitor_cancel (GFileMonitor *monitor)
239 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
241 if (!monitor->priv->cancelled)
243 G_FILE_MONITOR_GET_CLASS (monitor)->cancel (monitor);
245 monitor->priv->cancelled = TRUE;
246 g_object_notify (G_OBJECT (monitor), "cancelled");
253 * g_file_monitor_set_rate_limit:
254 * @monitor: a #GFileMonitor.
255 * @limit_msecs: a non-negative integer with the limit in milliseconds
256 * to poll for changes
258 * Sets the rate limit to which the @monitor will report
259 * consecutive change events to the same file.
262 g_file_monitor_set_rate_limit (GFileMonitor *monitor,
265 g_object_set (monitor, "rate-limit", limit_msecs, NULL);
269 * g_file_monitor_emit_event:
270 * @monitor: a #GFileMonitor.
272 * @other_file: a #GFile.
273 * @event_type: a set of #GFileMonitorEvent flags.
275 * Emits the #GFileMonitor::changed signal if a change
276 * has taken place. Should be called from file monitor
277 * implementations only.
279 * Implementations are responsible to call this method from the
280 * [thread-default main context][g-main-context-push-thread-default] of the
281 * thread that the monitor was created in.
284 g_file_monitor_emit_event (GFileMonitor *monitor,
287 GFileMonitorEvent event_type)
289 g_return_if_fail (G_IS_FILE_MONITOR (monitor));
290 g_return_if_fail (G_IS_FILE (child));
291 g_return_if_fail (!other_file || G_IS_FILE (other_file));
293 if (monitor->priv->cancelled)
296 g_signal_emit (monitor, g_file_monitor_changed_signal, 0, child, other_file, event_type);