Imported Upstream version 2.53.3
[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.1 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   GSource *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 calc_event_type (GFileInfo *last,
77                  GFileInfo *new)
78 {
79   if (last == NULL && new == NULL)
80     return -1;
81
82   if (last == NULL && new != NULL)
83     return G_FILE_MONITOR_EVENT_CREATED;
84   
85   if (last != NULL && new == NULL)
86     return G_FILE_MONITOR_EVENT_DELETED;
87
88   if (g_strcmp0 (g_file_info_get_etag (last), g_file_info_get_etag (new)))
89     return G_FILE_MONITOR_EVENT_CHANGED;
90   
91   if (g_file_info_get_size (last) != g_file_info_get_size (new))
92     return G_FILE_MONITOR_EVENT_CHANGED;
93
94   return -1;
95 }
96
97 static void
98 got_new_info (GObject      *source_object,
99               GAsyncResult *res,
100               gpointer      user_data)
101 {
102   GPollFileMonitor* poll_monitor = user_data;
103   GFileInfo *info;
104   int event;
105
106   info = g_file_query_info_finish (poll_monitor->file, res, NULL);
107
108   if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
109     {
110       event = calc_event_type (poll_monitor->last_info, info);
111
112       if (event != -1)
113         {
114           g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
115                                      poll_monitor->file,
116                                      NULL, event);
117           /* We're polling so slowly anyway, so always emit the done hint */
118           if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)) &&
119              (event == G_FILE_MONITOR_EVENT_CHANGED || event == G_FILE_MONITOR_EVENT_CREATED))
120             g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
121                                        poll_monitor->file,
122                                        NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
123         }
124       
125       if (poll_monitor->last_info)
126         {
127           g_object_unref (poll_monitor->last_info);
128           poll_monitor->last_info = NULL;
129         }
130       
131       if (info)
132         poll_monitor->last_info = g_object_ref (info);
133       
134       schedule_poll_timeout (poll_monitor);
135     }
136
137   if (info)
138     g_object_unref (info);
139   
140   g_object_unref (poll_monitor);
141 }
142
143 static gboolean
144 poll_file_timeout (gpointer data)
145 {
146   GPollFileMonitor* poll_monitor = data;
147
148   poll_monitor->timeout = FALSE;
149
150   g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
151                          0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
152   
153   return G_SOURCE_REMOVE;
154 }
155
156 static void
157 schedule_poll_timeout (GPollFileMonitor* poll_monitor)
158 {
159   poll_monitor->timeout = g_timeout_source_new_seconds (POLL_TIME_SECS);
160   g_source_set_callback (poll_monitor->timeout, poll_file_timeout, poll_monitor, NULL);
161   g_source_attach (poll_monitor->timeout, g_main_context_get_thread_default ());
162 }
163
164 static void
165 got_initial_info (GObject      *source_object,
166                   GAsyncResult *res,
167                   gpointer      user_data)
168 {
169   GPollFileMonitor* poll_monitor = user_data;
170   GFileInfo *info;
171
172   info = g_file_query_info_finish (poll_monitor->file, res, NULL);
173
174   poll_monitor->last_info = info;
175
176   if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
177     schedule_poll_timeout (poll_monitor);
178   
179   g_object_unref (poll_monitor);
180 }
181
182 /**
183  * _g_poll_file_monitor_new:
184  * @file: a #GFile.
185  * 
186  * Polls @file for changes.
187  * 
188  * Returns: a new #GFileMonitor for the given #GFile. 
189  **/
190 GFileMonitor*
191 _g_poll_file_monitor_new (GFile *file)
192 {
193   GPollFileMonitor* poll_monitor;
194   
195   poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
196
197   poll_monitor->file = g_object_ref (file);
198
199   g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
200                            0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
201   
202   return G_FILE_MONITOR (poll_monitor);
203 }
204
205 static gboolean
206 g_poll_file_monitor_cancel (GFileMonitor* monitor)
207 {
208   GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
209   
210   if (poll_monitor->timeout)
211     {
212       g_source_destroy (poll_monitor->timeout);
213       g_source_unref (poll_monitor->timeout);
214       poll_monitor->timeout = NULL;
215     }
216   
217   return TRUE;
218 }