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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "gfilemonitor.h"
25 #include "gioenumtypes.h"
31 * SECTION:gfilemonitor
32 * @short_description: File Monitor
35 * Monitors a file or directory for changes.
37 * To obtain a #GFileMonitor for a file or directory, use
38 * g_file_monitor(), g_file_monitor_file(), or
39 * g_file_monitor_directory().
41 * To get informed about changes to the file or directory you are
42 * monitoring, connect to the #GFileMonitor::changed signal. The
43 * signal will be emitted in the
44 * [thread-default main context][g-main-context-push-thread-default]
45 * of the thread that the monitor was created in
46 * (though if the global default main context is blocked, this may
47 * cause notifications to be blocked even if the thread-default
48 * context is still running).
51 #define DEFAULT_RATE_LIMIT_MSECS 800
53 struct _GFileMonitorPrivate
58 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor
, g_file_monitor
, G_TYPE_OBJECT
)
67 static guint g_file_monitor_changed_signal
;
70 g_file_monitor_set_property (GObject
*object
,
75 //GFileMonitor *monitor;
77 //monitor = G_FILE_MONITOR (object);
82 /* not supported by default */
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
92 g_file_monitor_get_property (GObject
*object
,
100 /* we expect this to be overridden... */
101 g_value_set_int (value
, DEFAULT_RATE_LIMIT_MSECS
);
105 //g_mutex_lock (&fms->lock);
106 g_value_set_boolean (value
, FALSE
);//fms->cancelled);
107 //g_mutex_unlock (&fms->lock);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
117 g_file_monitor_dispose (GObject
*object
)
119 GFileMonitor
*monitor
= G_FILE_MONITOR (object
);
121 /* Make sure we cancel on last unref */
122 g_file_monitor_cancel (monitor
);
124 G_OBJECT_CLASS (g_file_monitor_parent_class
)->dispose (object
);
128 g_file_monitor_init (GFileMonitor
*monitor
)
130 monitor
->priv
= g_file_monitor_get_instance_private (monitor
);
134 g_file_monitor_class_init (GFileMonitorClass
*klass
)
136 GObjectClass
*object_class
;
138 object_class
= G_OBJECT_CLASS (klass
);
139 object_class
->dispose
= g_file_monitor_dispose
;
140 object_class
->get_property
= g_file_monitor_get_property
;
141 object_class
->set_property
= g_file_monitor_set_property
;
144 * GFileMonitor::changed:
145 * @monitor: a #GFileMonitor.
147 * @other_file: (nullable): a #GFile or #NULL.
148 * @event_type: a #GFileMonitorEvent.
150 * Emitted when @file has been changed.
152 * If using %G_FILE_MONITOR_WATCH_MOVES on a directory monitor, and
153 * the information is available (and if supported by the backend),
154 * @event_type may be %G_FILE_MONITOR_EVENT_RENAMED,
155 * %G_FILE_MONITOR_EVENT_MOVED_IN or %G_FILE_MONITOR_EVENT_MOVED_OUT.
157 * In all cases @file will be a child of the monitored directory. For
158 * renames, @file will be the old name and @other_file is the new
159 * name. For "moved in" events, @file is the name of the file that
160 * appeared and @other_file is the old name that it was moved from (in
161 * another directory). For "moved out" events, @file is the name of
162 * the file that used to be in this directory and @other_file is the
163 * name of the file at its new location.
165 * It makes sense to treat %G_FILE_MONITOR_EVENT_MOVED_IN as
166 * equivalent to %G_FILE_MONITOR_EVENT_CREATED and
167 * %G_FILE_MONITOR_EVENT_MOVED_OUT as equivalent to
168 * %G_FILE_MONITOR_EVENT_DELETED, with extra information.
169 * %G_FILE_MONITOR_EVENT_RENAMED is equivalent to a delete/create
170 * pair. This is exactly how the events will be reported in the case
171 * that the %G_FILE_MONITOR_WATCH_MOVES flag is not in use.
173 * If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is
174 * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
175 * old path, and @other_file will be set to a #GFile containing the new path.
177 * In all the other cases, @other_file will be set to #NULL.
179 g_file_monitor_changed_signal
= g_signal_new (I_("changed"),
182 G_STRUCT_OFFSET (GFileMonitorClass
, changed
),
186 G_TYPE_FILE
, G_TYPE_FILE
, G_TYPE_FILE_MONITOR_EVENT
);
188 g_object_class_install_property (object_class
, PROP_RATE_LIMIT
,
189 g_param_spec_int ("rate-limit",
191 P_("The limit of the monitor to watch for changes, in milliseconds"),
192 0, G_MAXINT
, DEFAULT_RATE_LIMIT_MSECS
, G_PARAM_READWRITE
|
193 G_PARAM_EXPLICIT_NOTIFY
| G_PARAM_STATIC_STRINGS
));
195 g_object_class_install_property (object_class
, PROP_CANCELLED
,
196 g_param_spec_boolean ("cancelled",
198 P_("Whether the monitor has been cancelled"),
199 FALSE
, G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
));
203 * g_file_monitor_is_cancelled:
204 * @monitor: a #GFileMonitor
206 * Returns whether the monitor is canceled.
208 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
211 g_file_monitor_is_cancelled (GFileMonitor
*monitor
)
215 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor
), FALSE
);
217 res
= monitor
->priv
->cancelled
;
223 * g_file_monitor_cancel:
224 * @monitor: a #GFileMonitor.
226 * Cancels a file monitor.
228 * Returns: always %TRUE
231 g_file_monitor_cancel (GFileMonitor
*monitor
)
233 g_return_val_if_fail (G_IS_FILE_MONITOR (monitor
), FALSE
);
235 if (!monitor
->priv
->cancelled
)
237 G_FILE_MONITOR_GET_CLASS (monitor
)->cancel (monitor
);
239 monitor
->priv
->cancelled
= TRUE
;
240 g_object_notify (G_OBJECT (monitor
), "cancelled");
247 * g_file_monitor_set_rate_limit:
248 * @monitor: a #GFileMonitor.
249 * @limit_msecs: a non-negative integer with the limit in milliseconds
250 * to poll for changes
252 * Sets the rate limit to which the @monitor will report
253 * consecutive change events to the same file.
256 g_file_monitor_set_rate_limit (GFileMonitor
*monitor
,
259 g_object_set (monitor
, "rate-limit", limit_msecs
, NULL
);
263 * g_file_monitor_emit_event:
264 * @monitor: a #GFileMonitor.
266 * @other_file: a #GFile.
267 * @event_type: a set of #GFileMonitorEvent flags.
269 * Emits the #GFileMonitor::changed signal if a change
270 * has taken place. Should be called from file monitor
271 * implementations only.
273 * Implementations are responsible to call this method from the
274 * [thread-default main context][g-main-context-push-thread-default] of the
275 * thread that the monitor was created in.
278 g_file_monitor_emit_event (GFileMonitor
*monitor
,
281 GFileMonitorEvent event_type
)
283 g_return_if_fail (G_IS_FILE_MONITOR (monitor
));
284 g_return_if_fail (G_IS_FILE (child
));
285 g_return_if_fail (!other_file
|| G_IS_FILE (other_file
));
287 if (monitor
->priv
->cancelled
)
290 g_signal_emit (monitor
, g_file_monitor_changed_signal
, 0, child
, other_file
, event_type
);