gfilemonitor.c 9.17 KB
Newer Older
1 2 3 4 5 6 7
/* 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
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * 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
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23 24
#include <string.h>

#include "gfilemonitor.h"
25
#include "gioenumtypes.h"
26
#include "gfile.h"
27 28 29
#include "gvfs.h"
#include "glibintl.h"

30 31 32
/**
 * SECTION:gfilemonitor
 * @short_description: File Monitor
Matthias Clasen's avatar
Matthias Clasen committed
33
 * @include: gio/gio.h
34
 *
35
 * Monitors a file or directory for changes.
Matthias Clasen's avatar
Matthias Clasen committed
36 37
 *
 * To obtain a #GFileMonitor for a file or directory, use
38 39
 * g_file_monitor(), g_file_monitor_file(), or
 * g_file_monitor_directory().
Matthias Clasen's avatar
Matthias Clasen committed
40
 *
41 42
 * To get informed about changes to the file or directory you are
 * monitoring, connect to the #GFileMonitor::changed signal. The
43 44 45
 * 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
46 47 48
 * (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).
49 50
 **/

51
#define DEFAULT_RATE_LIMIT_MSECS 800
52

53 54
struct _GFileMonitorPrivate
{
55 56 57
  gboolean cancelled;
};

58 59 60 61
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFileMonitor, g_file_monitor, G_TYPE_OBJECT)

enum
{
62 63
  PROP_0,
  PROP_RATE_LIMIT,
64
  PROP_CANCELLED
65 66
};

67
static guint g_file_monitor_changed_signal;
68

69 70 71 72 73 74
static void
g_file_monitor_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
75
  //GFileMonitor *monitor;
76

77
  //monitor = G_FILE_MONITOR (object);
78 79 80 81

  switch (prop_id)
    {
    case PROP_RATE_LIMIT:
82
      /* not supported by default */
83 84
      break;

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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:
100 101
      /* we expect this to be overridden... */
      g_value_set_int (value, DEFAULT_RATE_LIMIT_MSECS);
102 103 104
      break;

    case PROP_CANCELLED:
105 106 107
      //g_mutex_lock (&fms->lock);
      g_value_set_boolean (value, FALSE);//fms->cancelled);
      //g_mutex_unlock (&fms->lock);
108 109 110 111 112 113 114 115
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

116 117 118
static void
g_file_monitor_dispose (GObject *object)
{
119
  GFileMonitor *monitor = G_FILE_MONITOR (object);
120 121

  /* Make sure we cancel on last unref */
122
  g_file_monitor_cancel (monitor);
123 124

  G_OBJECT_CLASS (g_file_monitor_parent_class)->dispose (object);
125 126
}

127 128 129 130 131 132
static void
g_file_monitor_init (GFileMonitor *monitor)
{
  monitor->priv = g_file_monitor_get_instance_private (monitor);
}

133 134 135
static void
g_file_monitor_class_init (GFileMonitorClass *klass)
{
136
  GObjectClass *object_class;
137

138 139 140 141
  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;
142

143 144 145 146
  /**
   * GFileMonitor::changed:
   * @monitor: a #GFileMonitor.
   * @file: a #GFile.
147
   * @other_file: (nullable): a #GFile or #NULL.
148
   * @event_type: a #GFileMonitorEvent.
149 150 151
   *
   * Emitted when @file has been changed.
   *
152
   * If using %G_FILE_MONITOR_WATCH_MOVES on a directory monitor, and
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
   * 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
171
   * that the %G_FILE_MONITOR_WATCH_MOVES flag is not in use.
172 173
   *
   * If using the deprecated flag %G_FILE_MONITOR_SEND_MOVED flag and @event_type is
Dan Winship's avatar
Dan Winship committed
174
   * #G_FILE_MONITOR_EVENT_MOVED, @file will be set to a #GFile containing the
175 176 177
   * 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.
178
   **/
179 180 181 182 183 184 185 186 187 188
  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,
189 190 191
                                   g_param_spec_int ("rate-limit",
                                                     P_("Rate limit"),
                                                     P_("The limit of the monitor to watch for changes, in milliseconds"),
192 193
                                                     0, G_MAXINT, DEFAULT_RATE_LIMIT_MSECS, G_PARAM_READWRITE |
                                                     G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
194

195
  g_object_class_install_property (object_class, PROP_CANCELLED,
196 197 198
                                   g_param_spec_boolean ("cancelled",
                                                         P_("Cancelled"),
                                                         P_("Whether the monitor has been cancelled"),
199
                                                         FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
200 201 202 203
}

/**
 * g_file_monitor_is_cancelled:
Matthias Clasen's avatar
Matthias Clasen committed
204
 * @monitor: a #GFileMonitor
205
 * 
Matthias Clasen's avatar
Matthias Clasen committed
206 207
 * Returns whether the monitor is canceled.
 *
208 209 210 211 212
 * Returns: %TRUE if monitor is canceled. %FALSE otherwise.
 **/
gboolean
g_file_monitor_is_cancelled (GFileMonitor *monitor)
{
213
  gboolean res;
214

215 216
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);

217
  res = monitor->priv->cancelled;
218

219
  return res;
220 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 233
{
  g_return_val_if_fail (G_IS_FILE_MONITOR (monitor), FALSE);
234 235

  if (!monitor->priv->cancelled)
236
    {
237 238 239 240
      G_FILE_MONITOR_GET_CLASS (monitor)->cancel (monitor);

      monitor->priv->cancelled = TRUE;
      g_object_notify (G_OBJECT (monitor), "cancelled");
241
    }
242 243

  return TRUE;
244 245 246 247 248
}

/**
 * g_file_monitor_set_rate_limit:
 * @monitor: a #GFileMonitor.
249 250
 * @limit_msecs: a non-negative integer with the limit in milliseconds
 *     to poll for changes
251
 *
252
 * Sets the rate limit to which the @monitor will report
253 254
 * consecutive change events to the same file.
 */
255 256
void
g_file_monitor_set_rate_limit (GFileMonitor *monitor,
257
                               gint          limit_msecs)
258
{
259
  g_object_set (monitor, "rate-limit", limit_msecs, NULL);
260 261
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/**
 * 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.
 **/
277
void
Matthias Clasen's avatar
Matthias Clasen committed
278
g_file_monitor_emit_event (GFileMonitor      *monitor,
279 280 281
                           GFile             *child,
                           GFile             *other_file,
                           GFileMonitorEvent  event_type)
282 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));
286

287 288
  if (monitor->priv->cancelled)
    return;
289

290
  g_signal_emit (monitor, g_file_monitor_changed_signal, 0, child, other_file, event_type);
291
}