Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ev-file-monitor.c
Go to the documentation of this file.
1 /* ev-file-monitor.c
2  * this file is part of evince, a gnome document viewer
3  *
4  * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "config.h"
22 
23 #include <glib.h>
24 #include <gio/gio.h>
25 
26 #include "ev-file-monitor.h"
27 
28 enum {
31 };
32 
34  GFileMonitor *monitor;
35 
36  guint timeout_id;
37 };
38 
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,
42  GFile *file,
43  GFile *other_file,
44  GFileMonitorEvent event_type,
45  EvFileMonitor *ev_monitor);
46 
47 #define EV_FILE_MONITOR_GET_PRIVATE(object) \
48  (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_FILE_MONITOR, EvFileMonitorPrivate))
49 
50 G_DEFINE_TYPE (EvFileMonitor, ev_file_monitor, G_TYPE_OBJECT)
51 
52 static guint signals[N_SIGNALS];
53 
54 static void
56 {
57  ev_monitor->priv = EV_FILE_MONITOR_GET_PRIVATE (ev_monitor);
58 }
59 
60 static void
61 ev_file_monitor_finalize (GObject *object)
62 {
63  EvFileMonitor *ev_monitor = EV_FILE_MONITOR (object);
64 
65  ev_file_monitor_timeout_stop (ev_monitor);
66 
67  if (ev_monitor->priv->monitor) {
68  g_signal_handlers_disconnect_by_func (ev_monitor->priv->monitor,
70  ev_monitor);
71  g_object_unref (ev_monitor->priv->monitor);
72  ev_monitor->priv->monitor = NULL;
73  }
74 
75  G_OBJECT_CLASS (ev_file_monitor_parent_class)->finalize (object);
76 }
77 
78 static void
80 {
81  GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
82 
83  g_type_class_add_private (g_object_class, sizeof (EvFileMonitorPrivate));
84 
85  g_object_class->finalize = ev_file_monitor_finalize;
86 
87  /* Signals */
88  signals[CHANGED] =
89  g_signal_new ("changed",
91  G_SIGNAL_RUN_LAST,
92  G_STRUCT_OFFSET (EvFileMonitorClass, changed),
93  NULL, NULL,
94  g_cclosure_marshal_VOID__VOID,
95  G_TYPE_NONE, 0);
96 }
97 
98 static gboolean
100 {
101  g_signal_emit (ev_monitor, signals[CHANGED], 0);
102 
103  ev_monitor->priv->timeout_id = 0;
104  return FALSE;
105 }
106 
107 static void
109 {
110  ev_file_monitor_timeout_stop (ev_monitor);
111 
112  ev_monitor->priv->timeout_id =
113  g_timeout_add_seconds (5, (GSourceFunc)timeout_cb, ev_monitor);
114 }
115 
116 static void
118 {
119  if (ev_monitor->priv->timeout_id > 0) {
120  g_source_remove (ev_monitor->priv->timeout_id);
121  ev_monitor->priv->timeout_id = 0;
122  }
123 }
124 
125 static void
126 ev_file_monitor_changed_cb (GFileMonitor *monitor,
127  GFile *file,
128  GFile *other_file,
129  GFileMonitorEvent event_type,
130  EvFileMonitor *ev_monitor)
131 {
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);
136 
137  break;
138  case G_FILE_MONITOR_EVENT_CHANGED:
139  ev_file_monitor_timeout_start (ev_monitor);
140  break;
141  default:
142  break;
143  }
144 }
145 
147 ev_file_monitor_new (const gchar *uri)
148 {
149  EvFileMonitor *ev_monitor;
150  GFile *file;
151  GError *error = NULL;
152 
153  ev_monitor = EV_FILE_MONITOR (g_object_new (EV_TYPE_FILE_MONITOR, NULL));
154 
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);
160  } else if (error) {
161  g_warning ("%s", error->message);
162  g_error_free (error);
163  }
164 
165  g_object_unref (file);
166 
167  return ev_monitor;
168 }