2 * this file is part of evince, a gnome document viewer
4 * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
6 * Evince is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Evince is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include "ev-file-monitor.h"
33 struct _EvFileMonitorPrivate {
34 GFileMonitor *monitor;
39 static void ev_file_monitor_timeout_start (EvFileMonitor *ev_monitor);
40 static void ev_file_monitor_timeout_stop (EvFileMonitor *ev_monitor);
41 static void ev_file_monitor_changed_cb (GFileMonitor *monitor,
44 GFileMonitorEvent event_type,
45 EvFileMonitor *ev_monitor);
47 #define EV_FILE_MONITOR_GET_PRIVATE(object) \
48 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_FILE_MONITOR, EvFileMonitorPrivate))
50 G_DEFINE_TYPE (EvFileMonitor, ev_file_monitor, G_TYPE_OBJECT)
52 static guint signals[N_SIGNALS];
55 ev_file_monitor_init (EvFileMonitor *ev_monitor)
57 ev_monitor->priv = EV_FILE_MONITOR_GET_PRIVATE (ev_monitor);
61 ev_file_monitor_finalize (GObject *object)
63 EvFileMonitor *ev_monitor = EV_FILE_MONITOR (object);
65 ev_file_monitor_timeout_stop (ev_monitor);
67 if (ev_monitor->priv->monitor) {
68 g_signal_handlers_disconnect_by_func (ev_monitor->priv->monitor,
69 ev_file_monitor_changed_cb,
71 g_object_unref (ev_monitor->priv->monitor);
72 ev_monitor->priv->monitor = NULL;
75 G_OBJECT_CLASS (ev_file_monitor_parent_class)->finalize (object);
79 ev_file_monitor_class_init (EvFileMonitorClass *klass)
81 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
83 g_type_class_add_private (g_object_class, sizeof (EvFileMonitorPrivate));
85 g_object_class->finalize = ev_file_monitor_finalize;
89 g_signal_new ("changed",
92 G_STRUCT_OFFSET (EvFileMonitorClass, changed),
94 g_cclosure_marshal_VOID__VOID,
99 timeout_cb (EvFileMonitor *ev_monitor)
101 g_signal_emit (ev_monitor, signals[CHANGED], 0);
103 ev_monitor->priv->timeout_id = 0;
108 ev_file_monitor_timeout_start (EvFileMonitor *ev_monitor)
110 ev_file_monitor_timeout_stop (ev_monitor);
112 ev_monitor->priv->timeout_id =
113 g_timeout_add_seconds (5, (GSourceFunc)timeout_cb, ev_monitor);
117 ev_file_monitor_timeout_stop (EvFileMonitor *ev_monitor)
119 if (ev_monitor->priv->timeout_id > 0) {
120 g_source_remove (ev_monitor->priv->timeout_id);
121 ev_monitor->priv->timeout_id = 0;
126 ev_file_monitor_changed_cb (GFileMonitor *monitor,
129 GFileMonitorEvent event_type,
130 EvFileMonitor *ev_monitor)
132 switch (event_type) {
133 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
134 ev_file_monitor_timeout_stop (ev_monitor);
135 g_signal_emit (ev_monitor, signals[CHANGED], 0);
138 case G_FILE_MONITOR_EVENT_CHANGED:
139 ev_file_monitor_timeout_start (ev_monitor);
147 ev_file_monitor_new (const gchar *uri)
149 EvFileMonitor *ev_monitor;
151 GError *error = NULL;
153 ev_monitor = EV_FILE_MONITOR (g_object_new (EV_TYPE_FILE_MONITOR, NULL));
155 file = g_file_new_for_uri (uri);
156 ev_monitor->priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
157 if (ev_monitor->priv->monitor) {
158 g_signal_connect (ev_monitor->priv->monitor, "changed",
159 G_CALLBACK (ev_file_monitor_changed_cb), ev_monitor);
161 g_warning ("%s", error->message);
162 g_error_free (error);
165 g_object_unref (file);