gio/ docs/reference/gio Merged gio-standalone into glib.
[platform/upstream/glib.git] / gio / gpollfilemonitor.c
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 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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24 #include <string.h>
25
26 #include "gpollfilemonitor.h"
27 #include "gfilemonitor.h"
28
29 static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
30 static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
31
32 struct _GPollFileMonitor
33 {
34   GFileMonitor parent_instance;
35   GFile *file;
36   GFileInfo *last_info;
37   guint timeout;
38 };
39
40 #define POLL_TIME_SECS 5
41
42 G_DEFINE_TYPE (GPollFileMonitor, g_poll_file_monitor, G_TYPE_FILE_MONITOR)
43
44 static void
45 g_poll_file_monitor_finalize (GObject* object)
46 {
47   GPollFileMonitor* poll_monitor;
48   
49   poll_monitor = G_POLL_FILE_MONITOR (object);
50
51   g_object_unref (poll_monitor->file);
52
53   if (G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize)
54     (*G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize) (object);
55 }
56
57
58 static void
59 g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
60 {
61   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
62   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
63   
64   gobject_class->finalize = g_poll_file_monitor_finalize;
65
66   file_monitor_class->cancel = g_poll_file_monitor_cancel;
67 }
68
69 static void
70 g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
71 {
72 }
73
74 static int 
75 safe_strcmp (const char *a, const char *b)
76 {
77   if (a == NULL && b == NULL)
78     return 0;
79   if (a == NULL)
80     return -1;
81   if (b == NULL)
82     return 1;
83   
84   return strcmp (a, b);
85 }
86
87 static int
88 calc_event_type (GFileInfo *last,
89                  GFileInfo *new)
90 {
91   if (last == NULL && new == NULL)
92     return -1;
93
94   if (last == NULL && new != NULL)
95     return G_FILE_MONITOR_EVENT_CREATED;
96   
97   if (last != NULL && new == NULL)
98     return G_FILE_MONITOR_EVENT_DELETED;
99
100   if (safe_strcmp (g_file_info_get_etag (last),
101                    g_file_info_get_etag (new)))
102     return G_FILE_MONITOR_EVENT_CHANGED;
103   
104   if (g_file_info_get_size (last) !=
105       g_file_info_get_size (new))
106     return G_FILE_MONITOR_EVENT_CHANGED;
107
108   return -1;
109 }
110
111 static void
112 got_new_info (GObject *source_object,
113               GAsyncResult *res,
114               gpointer user_data)
115 {
116   GPollFileMonitor* poll_monitor = user_data;
117   GFileInfo *info;
118   int event;
119
120   info = g_file_query_info_finish (poll_monitor->file, res, NULL);
121
122   if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
123     {
124       event = calc_event_type (poll_monitor->last_info, info);
125
126       if (event != -1)
127         {
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);
136         }
137       
138       if (poll_monitor->last_info)
139         {
140           g_object_unref (poll_monitor->last_info);
141           poll_monitor->last_info = NULL;
142         }
143       
144       if (info)
145         poll_monitor->last_info = g_object_ref (info);
146       
147       schedule_poll_timeout (poll_monitor);
148     }
149
150   if (info)
151     g_object_unref (info);
152   
153   g_object_unref (poll_monitor);
154 }
155
156 static gboolean
157 poll_file_timeout (gpointer data)
158 {
159   GPollFileMonitor* poll_monitor = data;
160
161   poll_monitor->timeout = FALSE;
162
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));
165   
166   return FALSE;
167 }
168
169 static void
170 schedule_poll_timeout (GPollFileMonitor* poll_monitor)
171 {
172   poll_monitor->timeout = g_timeout_add_seconds (POLL_TIME_SECS, poll_file_timeout, poll_monitor);
173  }
174
175 static void
176 got_initial_info (GObject *source_object,
177                   GAsyncResult *res,
178                   gpointer user_data)
179 {
180   GPollFileMonitor* poll_monitor = user_data;
181   GFileInfo *info;
182
183   info = g_file_query_info_finish (poll_monitor->file, res, NULL);
184
185   poll_monitor->last_info = info;
186
187   if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
188     schedule_poll_timeout (poll_monitor);
189   
190   g_object_unref (poll_monitor);
191 }
192
193 /**
194  * g_poll_file_monitor_new:
195  * @file:
196  * 
197  * Returns a new #GFileMonitor for the given #GFile. 
198  **/
199 GFileMonitor*
200 g_poll_file_monitor_new (GFile *file)
201 {
202   GPollFileMonitor* poll_monitor;
203   
204   poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
205
206   poll_monitor->file = g_object_ref (file);
207
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));
210   
211   return G_FILE_MONITOR (poll_monitor);
212 }
213
214 static gboolean
215 g_poll_file_monitor_cancel (GFileMonitor* monitor)
216 {
217   GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
218   
219   if (poll_monitor->timeout)
220     {
221       g_source_remove (poll_monitor->timeout);
222       poll_monitor->timeout = 0;
223     }
224   
225   return TRUE;
226 }