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