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"
27 #include "gfilemonitor.h"
31 static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
32 static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
34 struct _GPollFileMonitor
36 GFileMonitor parent_instance;
42 #define POLL_TIME_SECS 5
44 G_DEFINE_TYPE (GPollFileMonitor, g_poll_file_monitor, G_TYPE_FILE_MONITOR)
47 g_poll_file_monitor_finalize (GObject* object)
49 GPollFileMonitor* poll_monitor;
51 poll_monitor = G_POLL_FILE_MONITOR (object);
53 g_object_unref (poll_monitor->file);
55 if (G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize)
56 (*G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize) (object);
61 g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
63 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
64 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
66 gobject_class->finalize = g_poll_file_monitor_finalize;
68 file_monitor_class->cancel = g_poll_file_monitor_cancel;
72 g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
77 safe_strcmp (const char *a, const char *b)
79 if (a == NULL && b == NULL)
90 calc_event_type (GFileInfo *last,
93 if (last == NULL && new == NULL)
96 if (last == NULL && new != NULL)
97 return G_FILE_MONITOR_EVENT_CREATED;
99 if (last != NULL && new == NULL)
100 return G_FILE_MONITOR_EVENT_DELETED;
102 if (safe_strcmp (g_file_info_get_etag (last),
103 g_file_info_get_etag (new)))
104 return G_FILE_MONITOR_EVENT_CHANGED;
106 if (g_file_info_get_size (last) !=
107 g_file_info_get_size (new))
108 return G_FILE_MONITOR_EVENT_CHANGED;
114 got_new_info (GObject *source_object,
118 GPollFileMonitor* poll_monitor = user_data;
122 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
124 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
126 event = calc_event_type (poll_monitor->last_info, info);
130 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
133 /* We're polling so slowly anyway, so always emit the done hint */
134 if (event == G_FILE_MONITOR_EVENT_CHANGED)
135 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
137 NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
140 if (poll_monitor->last_info)
142 g_object_unref (poll_monitor->last_info);
143 poll_monitor->last_info = NULL;
147 poll_monitor->last_info = g_object_ref (info);
149 schedule_poll_timeout (poll_monitor);
153 g_object_unref (info);
155 g_object_unref (poll_monitor);
159 poll_file_timeout (gpointer data)
161 GPollFileMonitor* poll_monitor = data;
163 poll_monitor->timeout = FALSE;
165 g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STD_SIZE,
166 0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
172 schedule_poll_timeout (GPollFileMonitor* poll_monitor)
174 poll_monitor->timeout = g_timeout_add_seconds (POLL_TIME_SECS, poll_file_timeout, poll_monitor);
178 got_initial_info (GObject *source_object,
182 GPollFileMonitor* poll_monitor = user_data;
185 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
187 poll_monitor->last_info = info;
189 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
190 schedule_poll_timeout (poll_monitor);
192 g_object_unref (poll_monitor);
196 * g_poll_file_monitor_new:
199 * Polls @file for changes.
201 * Returns: a new #GFileMonitor for the given #GFile.
204 g_poll_file_monitor_new (GFile *file)
206 GPollFileMonitor* poll_monitor;
208 poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
210 poll_monitor->file = g_object_ref (file);
212 g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STD_SIZE,
213 0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
215 return G_FILE_MONITOR (poll_monitor);
219 g_poll_file_monitor_cancel (GFileMonitor* monitor)
221 GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
223 if (poll_monitor->timeout)
225 g_source_remove (poll_monitor->timeout);
226 poll_monitor->timeout = 0;