[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22 #include <string.h>
23
24 #include "gpollfilemonitor.h"
25 #include "gfile.h"
26 #include "gfilemonitor.h"
27 #include "gfileinfo.h"
28
29
30 static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
31 static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
32
33 struct _GPollFileMonitor
34 {
35   GFileMonitor parent_instance;
36   GFile *file;
37   GFileInfo *last_info;
38   guint timeout;
39 };
40
41 #define POLL_TIME_SECS 5
42
43 #define g_poll_file_monitor_get_type _g_poll_file_monitor_get_type
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   G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize (object);
56 }
57
58
59 static void
60 g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
61 {
62   GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
63   GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
64   
65   gobject_class->finalize = g_poll_file_monitor_finalize;
66
67   file_monitor_class->cancel = g_poll_file_monitor_cancel;
68 }
69
70 static void
71 g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
72 {
73 }
74
75 static int 
76 safe_strcmp (const char *a, 
77              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_STANDARD_SIZE,
166                          0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
167   
168   return G_SOURCE_REMOVE;
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_STANDARD_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 }