Coverage Report

Created: 2018-09-25 13:52

/src/glib/gio/gfilemonitor.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 * 
3
 * Copyright (C) 2006-2007 Red Hat, Inc.
4
 *
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.
9
 *
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.
14
 *
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/>.
17
 *
18
 * Author: Alexander Larsson <alexl@redhat.com>
19
 */
20
21
#include "config.h"
22
#include <string.h>
23
24
#include "gfilemonitor.h"
25
#include "gioenumtypes.h"
26
#include "gfile.h"
27
#include "gvfs.h"
28
#include "glibintl.h"
29
30
/**
31
 * SECTION:gfilemonitor
32
 * @short_description: File Monitor
33
 * @include: gio/gio.h
34
 *
35
 * Monitors a file or directory for changes.
36
 *
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().
40
 *
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).
49
 **/
50
51
0
#define DEFAULT_RATE_LIMIT_MSECS 800
52
53
struct _GFileMonitorPrivate
54
{
55
  gboolean cancelled;
56
};
57
58
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)
59
60
enum
61
{
62
  PROP_0,
63
  PROP_RATE_LIMIT,
64
  PROP_CANCELLED
65
};
66
67
static guint g_file_monitor_changed_signal;
68
69
static void
70
g_file_monitor_set_property (GObject      *object,
71
                             guint         prop_id,
72
                             const GValue *value,
73
                             GParamSpec   *pspec)
74
0
{
75
0
  //GFileMonitor *monitor;
76
0
77
0
  //monitor = G_FILE_MONITOR (object);
78
0
79
0
  switch (prop_id)
80
0
    {
81
0
    case PROP_RATE_LIMIT:
82
0
      /* not supported by default */
83
0
      break;
84
0
85
0
    default:
86
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87
0
      break;
88
0
    }
89
0
}
90
91
static void
92
g_file_monitor_get_property (GObject    *object,
93
                             guint       prop_id,
94
                             GValue     *value,
95
                             GParamSpec *pspec)
96
0
{
97
0
  switch (prop_id)
98
0
    {
99
0
    case PROP_RATE_LIMIT:
100
0
      /* we expect this to be overridden... */
101
0
      g_value_set_int (value, DEFAULT_RATE_LIMIT_MSECS);
102
0
      break;
103
0
104
0
    case PROP_CANCELLED:
105
0
      //g_mutex_lock (&fms->lock);
106
0
      g_value_set_boolean (value, FALSE);//fms->cancelled);
107
0
      //g_mutex_unlock (&fms->lock);
108
0
      break;
109
0
110
0
    default:
111
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112
0
      break;
113
0
    }
114
0
}
115
116
static void
117
g_file_monitor_dispose (GObject *object)
118
0
{
119
0
  GFileMonitor *monitor = G_FILE_MONITOR (object);
120
0
121
0
  /* Make sure we cancel on last unref */
122
0
  g_file_monitor_cancel (monitor);
123
0
124
0
  G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
125
0
}
126
127
static void
128
g_file_monitor_init (GFileMonitor *monitor)
129
0
{
130
0
  monitor->priv = g_file_monitor_get_instance_private (monitor);
131
0
}
132
133
static void
134
g_file_monitor_class_init (GFileMonitorClass *klass)
135
0
{
136
0
  GObjectClass *object_class;
137
0
138
0
  object_class = G_OBJECT_CLASS (klass);
139
0
  object_class->dispose = g_file_monitor_dispose;
140
0
  object_class->get_property = g_file_monitor_get_property;
141
0
  object_class->set_property = g_file_monitor_set_property;
142
0
143
0
  /**
144
0
   * GFileMonitor::changed:
145
0
   * @monitor: a #GFileMonitor.
146
0
   * @file: a #GFile.
147
0
   * @other_file: (nullable): a #GFile or #NULL.
148
0
   * @event_type: a #GFileMonitorEvent.
149
0
   *
150
0
   * Emitted when @file has been changed.
151
0
   *
152
0
   * If using %G_FILE_MONITOR_WATCH_MOVES on a directory monitor, and
153
0
   * the information is available (and if supported by the backend),
154
0
   * @event_type may be %G_FILE_MONITOR_EVENT_RENAMED,
155
0
   * %G_FILE_MONITOR_EVENT_MOVED_IN or %G_FILE_MONITOR_EVENT_MOVED_OUT.
156
0
   *
157
0
   * In all cases @file will be a child of the monitored directory.  For
158
0
   * renames, @file will be the old name and @other_file is the new
159
0
   * name.  For "moved in" events, @file is the name of the file that
160
0
   * appeared and @other_file is the old name that it was moved from (in
161
0
   * another directory).  For "moved out" events, @file is the name of
162
0
   * the file that used to be in this directory and @other_file is the
163
0
   * name of the file at its new location.
164
0
   *
165
0
   * It makes sense to treat %G_FILE_MONITOR_EVENT_MOVED_IN as
166
0
   * equivalent to %G_FILE_MONITOR_EVENT_CREATED and
167
0
   * %G_FILE_MONITOR_EVENT_MOVED_OUT as equivalent to
168
0
   * %G_FILE_MONITOR_EVENT_DELETED, with extra information.
169
0
   * %G_FILE_MONITOR_EVENT_RENAMED is equivalent to a delete/create
170
0
   * pair.  This is exactly how the events will be reported in the case
171
0
   * that the %G_FILE_MONITOR_WATCH_MOVES flag is not in use.
172
0
   *
173
0
   * If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is
174
0
   * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
175
0
   * old path, and @other_file will be set to a #GFile containing the new path.
176
0
   *
177
0
   * In all the other cases, @other_file will be set to #NULL.
178
0
   **/
179
0
  g_file_monitor_changed_signal = g_signal_new (I_("changed"),
180
0
                                                G_TYPE_FILE_MONITOR,
181
0
                                                G_SIGNAL_RUN_LAST,
182
0
                                                G_STRUCT_OFFSET (GFileMonitorClass, changed),
183
0
                                                NULL, NULL,
184
0
                                                NULL,
185
0
                                                G_TYPE_NONE, 3,
186
0
                                                G_TYPE_FILE, G_TYPE_FILE, G_TYPE_FILE_MONITOR_EVENT);
187
0
188
0
  g_object_class_install_property (object_class, PROP_RATE_LIMIT,
189
0
                                   g_param_spec_int ("rate-limit",
190
0
                                                     P_("Rate limit"),
191
0
                                                     P_("The limit of the monitor to watch for changes, in milliseconds"),
192
0
                                                     0, G_MAXINT, DEFAULT_RATE_LIMIT_MSECS, G_PARAM_READWRITE |
193
0
                                                     G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
194
0
195
0
  g_object_class_install_property (object_class, PROP_CANCELLED,
196
0
                                   g_param_spec_boolean ("cancelled",
197
0
                                                         P_("Cancelled"),
198
0
                                                         P_("Whether the monitor has been cancelled"),
199
0
                                                         FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
200
0
}
201
202
/**
203
 * g_file_monitor_is_cancelled:
204
 * @monitor: a #GFileMonitor
205
 * 
206
 * Returns whether the monitor is canceled.
207
 *
208
 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
209
 **/
210
gboolean
211
g_file_monitor_is_cancelled (GFileMonitor *monitor)
212
0
{
213
0
  gboolean res;
214
0
215
0
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
216
0
217
0
  res = monitor->priv->cancelled;
218
0
219
0
  return res;
220
0
}
221
222
/**
223
 * g_file_monitor_cancel:
224
 * @monitor: a #GFileMonitor.
225
 *
226
 * Cancels a file monitor.
227
 *
228
 * Returns: always %TRUE
229
 **/
230
gboolean
231
g_file_monitor_cancel (GFileMonitor *monitor)
232
0
{
233
0
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
234
0
235
0
  if (!monitor->priv->cancelled)
236
0
    {
237
0
      G_FILE_MONITOR_GET_CLASS (monitor)->cancel (monitor);
238
0
239
0
      monitor->priv->cancelled = TRUE;
240
0
      g_object_notify (G_OBJECT (monitor), "cancelled");
241
0
    }
242
0
243
0
  return TRUE;
244
0
}
245
246
/**
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
251
 *
252
 * Sets the rate limit to which the @monitor will report
253
 * consecutive change events to the same file.
254
 */
255
void
256
g_file_monitor_set_rate_limit (GFileMonitor *monitor,
257
                               gint          limit_msecs)
258
0
{
259
0
  g_object_set (monitor, "rate-limit", limit_msecs, NULL);
260
0
}
261
262
/**
263
 * g_file_monitor_emit_event:
264
 * @monitor: a #GFileMonitor.
265
 * @child: a #GFile.
266
 * @other_file: a #GFile.
267
 * @event_type: a set of #GFileMonitorEvent flags.
268
 *
269
 * Emits the #GFileMonitor::changed signal if a change
270
 * has taken place. Should be called from file monitor
271
 * implementations only.
272
 *
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.
276
 **/
277
void
278
g_file_monitor_emit_event (GFileMonitor      *monitor,
279
                           GFile             *child,
280
                           GFile             *other_file,
281
                           GFileMonitorEvent  event_type)
282
0
{
283
0
  g_return_if_fail (G_IS_FILE_MONITOR (monitor));
284
0
  g_return_if_fail (G_IS_FILE (child));
285
0
  g_return_if_fail (!other_file || G_IS_FILE (other_file));
286
0
287
0
  if (monitor->priv->cancelled)
288
0
    return;
289
0
290
0
  g_signal_emit (monitor, g_file_monitor_changed_signal, 0, child, other_file, event_type);
291
0
}