Add some tests for invalid booleans
[glib.git] / gio / gpollfilemonitor.c
blob0b5a5e79d7b44f488e6668b0db1d02e0f12ced93
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include <config.h>
24 #include <string.h>
26 #include "gpollfilemonitor.h"
27 #include "gfilemonitor.h"
29 static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
30 static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
32 struct _GPollFileMonitor
34 GFileMonitor parent_instance;
35 GFile *file;
36 GFileInfo *last_info;
37 guint timeout;
40 #define POLL_TIME_SECS 5
42 G_DEFINE_TYPE (GPollFileMonitor, g_poll_file_monitor, G_TYPE_FILE_MONITOR)
44 static void
45 g_poll_file_monitor_finalize (GObject* object)
47 GPollFileMonitor* poll_monitor;
49 poll_monitor = G_POLL_FILE_MONITOR (object);
51 g_object_unref (poll_monitor->file);
53 if (G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize)
54 (*G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize) (object);
58 static void
59 g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
61 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
62 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
64 gobject_class->finalize = g_poll_file_monitor_finalize;
66 file_monitor_class->cancel = g_poll_file_monitor_cancel;
69 static void
70 g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
74 static int
75 safe_strcmp (const char *a, const char *b)
77 if (a == NULL && b == NULL)
78 return 0;
79 if (a == NULL)
80 return -1;
81 if (b == NULL)
82 return 1;
84 return strcmp (a, b);
87 static int
88 calc_event_type (GFileInfo *last,
89 GFileInfo *new)
91 if (last == NULL && new == NULL)
92 return -1;
94 if (last == NULL && new != NULL)
95 return G_FILE_MONITOR_EVENT_CREATED;
97 if (last != NULL && new == NULL)
98 return G_FILE_MONITOR_EVENT_DELETED;
100 if (safe_strcmp (g_file_info_get_etag (last),
101 g_file_info_get_etag (new)))
102 return G_FILE_MONITOR_EVENT_CHANGED;
104 if (g_file_info_get_size (last) !=
105 g_file_info_get_size (new))
106 return G_FILE_MONITOR_EVENT_CHANGED;
108 return -1;
111 static void
112 got_new_info (GObject *source_object,
113 GAsyncResult *res,
114 gpointer user_data)
116 GPollFileMonitor* poll_monitor = user_data;
117 GFileInfo *info;
118 int event;
120 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
122 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
124 event = calc_event_type (poll_monitor->last_info, info);
126 if (event != -1)
128 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
129 poll_monitor->file,
130 NULL, event);
131 /* We're polling so slowly anyway, so always emit the done hint */
132 if (event == G_FILE_MONITOR_EVENT_CHANGED)
133 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
134 poll_monitor->file,
135 NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
138 if (poll_monitor->last_info)
140 g_object_unref (poll_monitor->last_info);
141 poll_monitor->last_info = NULL;
144 if (info)
145 poll_monitor->last_info = g_object_ref (info);
147 schedule_poll_timeout (poll_monitor);
150 if (info)
151 g_object_unref (info);
153 g_object_unref (poll_monitor);
156 static gboolean
157 poll_file_timeout (gpointer data)
159 GPollFileMonitor* poll_monitor = data;
161 poll_monitor->timeout = FALSE;
163 g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STD_SIZE,
164 0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
166 return FALSE;
169 static void
170 schedule_poll_timeout (GPollFileMonitor* poll_monitor)
172 poll_monitor->timeout = g_timeout_add_seconds (POLL_TIME_SECS, poll_file_timeout, poll_monitor);
175 static void
176 got_initial_info (GObject *source_object,
177 GAsyncResult *res,
178 gpointer user_data)
180 GPollFileMonitor* poll_monitor = user_data;
181 GFileInfo *info;
183 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
185 poll_monitor->last_info = info;
187 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
188 schedule_poll_timeout (poll_monitor);
190 g_object_unref (poll_monitor);
194 * g_poll_file_monitor_new:
195 * @file:
197 * Returns a new #GFileMonitor for the given #GFile.
199 GFileMonitor*
200 g_poll_file_monitor_new (GFile *file)
202 GPollFileMonitor* poll_monitor;
204 poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
206 poll_monitor->file = g_object_ref (file);
208 g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STD_SIZE,
209 0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
211 return G_FILE_MONITOR (poll_monitor);
214 static gboolean
215 g_poll_file_monitor_cancel (GFileMonitor* monitor)
217 GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
219 if (poll_monitor->timeout)
221 g_source_remove (poll_monitor->timeout);
222 poll_monitor->timeout = 0;
225 return TRUE;