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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "gpollfilemonitor.h"
28 #include "gfilemonitor.h"
29 #include "gfileinfo.h"
33 static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
34 static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
36 struct _GPollFileMonitor
38 GFileMonitor parent_instance;
44 #define POLL_TIME_SECS 5
46 #define g_poll_file_monitor_get_type _g_poll_file_monitor_get_type
47 G_DEFINE_TYPE (GPollFileMonitor, g_poll_file_monitor, G_TYPE_FILE_MONITOR)
50 g_poll_file_monitor_finalize (GObject* object)
52 GPollFileMonitor* poll_monitor;
54 poll_monitor = G_POLL_FILE_MONITOR (object);
56 g_object_unref (poll_monitor->file);
58 G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize (object);
63 g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
65 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
66 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
68 gobject_class->finalize = g_poll_file_monitor_finalize;
70 file_monitor_class->cancel = g_poll_file_monitor_cancel;
74 g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
79 safe_strcmp (const char *a,
82 if (a == NULL && b == NULL)
93 calc_event_type (GFileInfo *last,
96 if (last == NULL && new == NULL)
99 if (last == NULL && new != NULL)
100 return G_FILE_MONITOR_EVENT_CREATED;
102 if (last != NULL && new == NULL)
103 return G_FILE_MONITOR_EVENT_DELETED;
105 if (safe_strcmp (g_file_info_get_etag (last),
106 g_file_info_get_etag (new)))
107 return G_FILE_MONITOR_EVENT_CHANGED;
109 if (g_file_info_get_size (last) !=
110 g_file_info_get_size (new))
111 return G_FILE_MONITOR_EVENT_CHANGED;
117 got_new_info (GObject *source_object,
121 GPollFileMonitor* poll_monitor = user_data;
125 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
127 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
129 event = calc_event_type (poll_monitor->last_info, info);
133 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
136 /* We're polling so slowly anyway, so always emit the done hint */
137 if (event == G_FILE_MONITOR_EVENT_CHANGED)
138 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
140 NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
143 if (poll_monitor->last_info)
145 g_object_unref (poll_monitor->last_info);
146 poll_monitor->last_info = NULL;
150 poll_monitor->last_info = g_object_ref (info);
152 schedule_poll_timeout (poll_monitor);
156 g_object_unref (info);
158 g_object_unref (poll_monitor);
162 poll_file_timeout (gpointer data)
164 GPollFileMonitor* poll_monitor = data;
166 poll_monitor->timeout = FALSE;
168 g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
169 0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
175 schedule_poll_timeout (GPollFileMonitor* poll_monitor)
177 poll_monitor->timeout = g_timeout_add_seconds (POLL_TIME_SECS, poll_file_timeout, poll_monitor);
181 got_initial_info (GObject *source_object,
185 GPollFileMonitor* poll_monitor = user_data;
188 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
190 poll_monitor->last_info = info;
192 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
193 schedule_poll_timeout (poll_monitor);
195 g_object_unref (poll_monitor);
199 * g_poll_file_monitor_new:
202 * Polls @file for changes.
204 * Returns: a new #GFileMonitor for the given #GFile.
207 _g_poll_file_monitor_new (GFile *file)
209 GPollFileMonitor* poll_monitor;
211 poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
213 poll_monitor->file = g_object_ref (file);
215 g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
216 0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
218 return G_FILE_MONITOR (poll_monitor);
222 g_poll_file_monitor_cancel (GFileMonitor* monitor)
224 GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
226 if (poll_monitor->timeout)
228 g_source_remove (poll_monitor->timeout);
229 poll_monitor->timeout = 0;