1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include "gpollfilemonitor.h"
28 #include "gfilemonitor.h"
29 #include "gfileinfo.h"
32 static gboolean g_poll_file_monitor_cancel (GFileMonitor* monitor);
33 static void schedule_poll_timeout (GPollFileMonitor* poll_monitor);
35 struct _GPollFileMonitor
37 GFileMonitor parent_instance;
43 #define POLL_TIME_SECS 5
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)
49 g_poll_file_monitor_finalize (GObject* object)
51 GPollFileMonitor* poll_monitor;
53 poll_monitor = G_POLL_FILE_MONITOR (object);
55 g_poll_file_monitor_cancel (G_FILE_MONITOR (poll_monitor));
56 g_object_unref (poll_monitor->file);
57 g_clear_object (&poll_monitor->last_info);
59 G_OBJECT_CLASS (g_poll_file_monitor_parent_class)->finalize (object);
64 g_poll_file_monitor_class_init (GPollFileMonitorClass* klass)
66 GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
67 GFileMonitorClass *file_monitor_class = G_FILE_MONITOR_CLASS (klass);
69 gobject_class->finalize = g_poll_file_monitor_finalize;
71 file_monitor_class->cancel = g_poll_file_monitor_cancel;
75 g_poll_file_monitor_init (GPollFileMonitor* poll_monitor)
80 calc_event_type (GFileInfo *last,
83 if (last == NULL && new == NULL)
86 if (last == NULL && new != NULL)
87 return G_FILE_MONITOR_EVENT_CREATED;
89 if (last != NULL && new == NULL)
90 return G_FILE_MONITOR_EVENT_DELETED;
92 if (g_file_info_has_attribute (last, G_FILE_ATTRIBUTE_ETAG_VALUE) &&
93 g_file_info_has_attribute (new, G_FILE_ATTRIBUTE_ETAG_VALUE) &&
94 g_strcmp0 (g_file_info_get_etag (last), g_file_info_get_etag (new)) != 0)
95 return G_FILE_MONITOR_EVENT_CHANGED;
97 if (g_file_info_has_attribute (last, G_FILE_ATTRIBUTE_STANDARD_SIZE) &&
98 g_file_info_has_attribute (new, G_FILE_ATTRIBUTE_STANDARD_SIZE) &&
99 g_file_info_get_size (last) != g_file_info_get_size (new))
100 return G_FILE_MONITOR_EVENT_CHANGED;
106 got_new_info (GObject *source_object,
110 GPollFileMonitor* poll_monitor = user_data;
114 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
116 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
118 event = calc_event_type (poll_monitor->last_info, info);
122 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
125 /* We're polling so slowly anyway, so always emit the done hint */
126 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)) &&
127 (event == G_FILE_MONITOR_EVENT_CHANGED || event == G_FILE_MONITOR_EVENT_CREATED))
128 g_file_monitor_emit_event (G_FILE_MONITOR (poll_monitor),
130 NULL, G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT);
133 if (poll_monitor->last_info)
135 g_object_unref (poll_monitor->last_info);
136 poll_monitor->last_info = NULL;
140 poll_monitor->last_info = g_object_ref (info);
142 schedule_poll_timeout (poll_monitor);
146 g_object_unref (info);
148 g_object_unref (poll_monitor);
152 poll_file_timeout (gpointer data)
154 GPollFileMonitor* poll_monitor = data;
156 g_source_unref (poll_monitor->timeout);
157 poll_monitor->timeout = NULL;
159 g_file_query_info_async (poll_monitor->file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
160 0, 0, NULL, got_new_info, g_object_ref (poll_monitor));
162 return G_SOURCE_REMOVE;
166 schedule_poll_timeout (GPollFileMonitor* poll_monitor)
168 poll_monitor->timeout = g_timeout_source_new_seconds (POLL_TIME_SECS);
169 g_source_set_callback (poll_monitor->timeout, poll_file_timeout, poll_monitor, NULL);
170 g_source_attach (poll_monitor->timeout, g_main_context_get_thread_default ());
174 got_initial_info (GObject *source_object,
178 GPollFileMonitor* poll_monitor = user_data;
181 info = g_file_query_info_finish (poll_monitor->file, res, NULL);
183 poll_monitor->last_info = info;
185 if (!g_file_monitor_is_cancelled (G_FILE_MONITOR (poll_monitor)))
186 schedule_poll_timeout (poll_monitor);
188 g_object_unref (poll_monitor);
192 * _g_poll_file_monitor_new:
195 * Polls @file for changes.
197 * Returns: a new #GFileMonitor for the given #GFile.
200 _g_poll_file_monitor_new (GFile *file)
202 GPollFileMonitor* poll_monitor;
204 poll_monitor = g_object_new (G_TYPE_POLL_FILE_MONITOR, NULL);
206 poll_monitor->file = g_object_ref (file);
208 g_file_query_info_async (file, G_FILE_ATTRIBUTE_ETAG_VALUE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
209 0, 0, NULL, got_initial_info, g_object_ref (poll_monitor));
211 return G_FILE_MONITOR (poll_monitor);
215 g_poll_file_monitor_cancel (GFileMonitor* monitor)
217 GPollFileMonitor *poll_monitor = G_POLL_FILE_MONITOR (monitor);
219 if (poll_monitor->timeout)
221 g_source_destroy (poll_monitor->timeout);
222 g_source_unref (poll_monitor->timeout);
223 poll_monitor->timeout = NULL;