1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* ev-file-monitor.c
 *  this file is part of lector, a cafe document viewer
 *
 * Copyright (C) 2008 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Lector is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Lector is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include <glib.h>
#include <gio/gio.h>

#include "ev-file-monitor.h"

enum {
	CHANGED,
	N_SIGNALS
};

struct _EvFileMonitorPrivate {
	GFileMonitor *monitor;

	guint         timeout_id;
};

static void ev_file_monitor_timeout_start (EvFileMonitor    *ev_monitor);
static void ev_file_monitor_timeout_stop  (EvFileMonitor    *ev_monitor);
static void ev_file_monitor_changed_cb    (GFileMonitor     *monitor,
					   GFile            *file,
					   GFile            *other_file,
					   GFileMonitorEvent event_type,
					   EvFileMonitor    *ev_monitor);

G_DEFINE_TYPE_WITH_PRIVATE (EvFileMonitor, ev_file_monitor, G_TYPE_OBJECT)<--- There is an unknown macro here somewhere. Configuration is required. If G_DEFINE_TYPE_WITH_PRIVATE is a macro then please configure it.

static guint signals[N_SIGNALS] = { 0 };

static void
ev_file_monitor_init (EvFileMonitor *ev_monitor)
{
	ev_monitor->priv = ev_file_monitor_get_instance_private (ev_monitor);
}

static void
ev_file_monitor_finalize (GObject *object)
{
	EvFileMonitor *ev_monitor = EV_FILE_MONITOR (object);

	ev_file_monitor_timeout_stop (ev_monitor);

	if (ev_monitor->priv->monitor) {
		g_signal_handlers_disconnect_by_func (ev_monitor->priv->monitor,
						      ev_file_monitor_changed_cb,
						      ev_monitor);
		g_object_unref (ev_monitor->priv->monitor);
		ev_monitor->priv->monitor = NULL;
	}

	G_OBJECT_CLASS (ev_file_monitor_parent_class)->finalize (object);
}

static void
ev_file_monitor_class_init (EvFileMonitorClass *klass)
{
	GObjectClass *g_object_class = G_OBJECT_CLASS (klass);

	g_object_class->finalize = ev_file_monitor_finalize;

	/* Signals */
	signals[CHANGED] =
		g_signal_new ("changed",
			      EV_TYPE_FILE_MONITOR,
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (EvFileMonitorClass, changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE, 0);
}

static gboolean
timeout_cb (EvFileMonitor *ev_monitor)
{
	g_signal_emit (ev_monitor, signals[CHANGED], 0);

	ev_monitor->priv->timeout_id = 0;
	return FALSE;
}

static void
ev_file_monitor_timeout_start (EvFileMonitor *ev_monitor)
{
	ev_file_monitor_timeout_stop (ev_monitor);

	ev_monitor->priv->timeout_id =
		g_timeout_add_seconds (5, (GSourceFunc)timeout_cb, ev_monitor);
}

static void
ev_file_monitor_timeout_stop (EvFileMonitor *ev_monitor)
{
	if (ev_monitor->priv->timeout_id > 0) {
		g_source_remove (ev_monitor->priv->timeout_id);
		ev_monitor->priv->timeout_id = 0;
	}
}

static void
ev_file_monitor_changed_cb (GFileMonitor     *monitor,
			    GFile            *file,
			    GFile            *other_file,
			    GFileMonitorEvent event_type,
			    EvFileMonitor    *ev_monitor)
{
	switch (event_type) {
	case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
		ev_file_monitor_timeout_stop (ev_monitor);
		g_signal_emit (ev_monitor, signals[CHANGED], 0);

		break;
	case G_FILE_MONITOR_EVENT_CHANGED:
		ev_file_monitor_timeout_start (ev_monitor);
		break;
	default:
		break;
	}
}

EvFileMonitor *
ev_file_monitor_new (const gchar *uri)
{
	EvFileMonitor *ev_monitor;
	GFile         *file;
	GError        *error = NULL;

	ev_monitor = EV_FILE_MONITOR (g_object_new (EV_TYPE_FILE_MONITOR, NULL));

	file = g_file_new_for_uri (uri);
	ev_monitor->priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
	if (ev_monitor->priv->monitor) {
		g_signal_connect (ev_monitor->priv->monitor, "changed",
				  G_CALLBACK (ev_file_monitor_changed_cb), ev_monitor);
	} else if (error) {
		g_warning ("%s", error->message);
		g_error_free (error);
	}

	g_object_unref (file);

	return ev_monitor;
}