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