Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/ev-daemon.c
blob: 4c05cbe2133208f06e5036383d0850bb32172957 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* ev-metadata.c
 *  this file is part of evince, a gnome document viewer
 *
 * Copyright (C) 2009 Carlos Garcia Campos  <carlosgc@gnome.org>
 *
 * Evince 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.
 *
 * Evince 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <dbus/dbus-glib-bindings.h>
#include <dbus/dbus-glib-lowlevel.h>

#define EV_DBUS_DAEMON_NAME        "org.gnome.evince.Daemon"
#define EV_DBUS_DAEMON_OBJECT_PATH "/org/gnome/evince/Daemon"

#define EV_TYPE_DAEMON                     (ev_daemon_get_type ())
#define EV_DAEMON(object)                  (G_TYPE_CHECK_INSTANCE_CAST((object), EV_TYPE_DAEMON, EvDaemon))
#define EV_DAEMON_CLASS(klass)             (G_TYPE_CHECK_CLASS_CAST((klass), EV_TYPE_DAEMON, EvDaemonClass))
#define EV_IS_DAEMON(object)               (G_TYPE_CHECK_INSTANCE_TYPE((object), EV_TYPE_DAEMON))

typedef struct _EvDaemon      EvDaemon;
typedef struct _EvDaemonClass EvDaemonClass;

struct _EvDaemon {
	GObject base;

	DBusGProxy *bus_proxy;

	GList      *docs;
	guint       n_docs;

	guint       timer_id;
};

struct _EvDaemonClass {
	GObjectClass base_class;
};

static GType    ev_daemon_get_type            (void) G_GNUC_CONST;
static gboolean ev_daemon_register_document   (EvDaemon              *ev_daemon,
					       const gchar           *uri,
					       DBusGMethodInvocation *context);
static gboolean ev_daemon_unregister_document (EvDaemon              *ev_daemon,
					       const gchar           *uri,
					       DBusGMethodInvocation *context);
#include "ev-daemon-service.h"

static EvDaemon *ev_daemon = NULL;

G_DEFINE_TYPE(EvDaemon, ev_daemon, G_TYPE_OBJECT)

typedef struct {
	gchar *dbus_name;
	gchar *uri;
} EvDoc;

static void
ev_doc_free (EvDoc *doc)
{
	if (!doc)
		return;

	g_free (doc->dbus_name);
	g_free (doc->uri);

	g_free (doc);
}

static EvDoc *
ev_daemon_find_doc (EvDaemon    *ev_daemon,
		    const gchar *uri)
{
	GList *l;

	for (l = ev_daemon->docs; l; l = g_list_next (l)) {
		EvDoc *doc = (EvDoc *)l->data;

		if (strcmp (doc->uri, uri) == 0)
			return doc;
	}

	return NULL;
}

static void
ev_daemon_finalize (GObject *object)
{
	EvDaemon *ev_daemon = EV_DAEMON (object);

	if (ev_daemon->docs) {
		g_list_foreach (ev_daemon->docs, (GFunc)ev_doc_free, NULL);
		g_list_free (ev_daemon->docs);
		ev_daemon->docs = NULL;
	}

	if (ev_daemon->bus_proxy) {
		g_object_unref (ev_daemon->bus_proxy);
		ev_daemon->bus_proxy = NULL;
	}

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

static void
ev_daemon_init (EvDaemon *ev_daemon)
{
}

static void
ev_daemon_class_init (EvDaemonClass *klass)
{
	GObjectClass *g_object_class = G_OBJECT_CLASS (klass);

	g_object_class->finalize = ev_daemon_finalize;

	dbus_g_object_type_install_info (EV_TYPE_DAEMON,
					 &dbus_glib_ev_daemon_object_info);
}

static gboolean
ev_daemon_shutdown (EvDaemon *ev_daemon)
{
	g_object_unref (ev_daemon);

	return FALSE;
}

static void
ev_daemon_stop_killtimer (EvDaemon *ev_daemon)
{
	if (ev_daemon->timer_id != 0)
		g_source_remove (ev_daemon->timer_id);
	ev_daemon->timer_id = 0;
}

static void
ev_daemon_start_killtimer (EvDaemon *ev_daemon)
{
	ev_daemon_stop_killtimer (ev_daemon);
	ev_daemon->timer_id =
		g_timeout_add_seconds (30,
				       (GSourceFunc) ev_daemon_shutdown,
				       ev_daemon);
}

static void
ev_daemon_name_owner_changed (DBusGProxy  *proxy,
			      const gchar *name,
			      const gchar *old_owner,
			      const gchar *new_owner,
			      EvDaemon    *ev_daemon)
{
	GList *l, *next = NULL;

	if (*name == ':' && *new_owner == '\0') {
		for (l = ev_daemon->docs; l; l = next) {
			EvDoc *doc = (EvDoc *)l->data;

			next = l->next;
			if (strcmp (doc->dbus_name, name) == 0) {
				ev_doc_free (doc);
				ev_daemon->docs = g_list_delete_link (ev_daemon->docs, l);
				if (--ev_daemon->n_docs == 0)
					ev_daemon_start_killtimer (ev_daemon);
			}
		}
	}
}

static EvDaemon *
ev_daemon_get (void)
{
	DBusGConnection *connection;
	guint            request_name_result;
	GError          *error = NULL;

	if (ev_daemon)
		return ev_daemon;

	connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
	if (!connection) {
		g_printerr ("Failed to connect to the D-BUS daemon: %s\n", error->message);
		g_error_free (error);

		return NULL;
	}

	ev_daemon = g_object_new (EV_TYPE_DAEMON, NULL);

	ev_daemon->bus_proxy = dbus_g_proxy_new_for_name (connection,
						       DBUS_SERVICE_DBUS,
						       DBUS_PATH_DBUS,
						       DBUS_INTERFACE_DBUS);
	if (!org_freedesktop_DBus_request_name (ev_daemon->bus_proxy,
						EV_DBUS_DAEMON_NAME,
						DBUS_NAME_FLAG_DO_NOT_QUEUE,
						&request_name_result, &error)) {
		g_printerr ("Failed to acquire daemon name: %s", error->message);
		g_error_free (error);
		g_object_unref (ev_daemon);

		return NULL;
	}

	switch (request_name_result) {
	case DBUS_REQUEST_NAME_REPLY_EXISTS:
		g_printerr ("Evince daemon already running, exiting.\n");
		g_object_unref (ev_daemon);

		return NULL;
	case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
		dbus_g_connection_register_g_object (connection,
						     EV_DBUS_DAEMON_OBJECT_PATH,
						     G_OBJECT (ev_daemon));
		break;
	default:
		g_printerr ("Not primary owner of the service, exiting.\n");
		g_object_unref (ev_daemon);

		return NULL;
	}


	dbus_g_proxy_add_signal (ev_daemon->bus_proxy,
				 "NameOwnerChanged",
				 G_TYPE_STRING,
				 G_TYPE_STRING,
				 G_TYPE_STRING,
				 G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (ev_daemon->bus_proxy, "NameOwnerChanged",
				     G_CALLBACK (ev_daemon_name_owner_changed),
				     ev_daemon, NULL);
	ev_daemon_start_killtimer (ev_daemon);

	return ev_daemon;
}



static gboolean
ev_daemon_register_document (EvDaemon              *ev_daemon,
			     const gchar           *uri,
			     DBusGMethodInvocation *method)
{
	EvDoc       *doc;
	const gchar *owner = NULL;

	doc = ev_daemon_find_doc (ev_daemon, uri);
	if (doc) {
		/* Already registered */
		owner = doc->dbus_name;
	} else {
		doc = g_new (EvDoc, 1);
		doc->dbus_name = dbus_g_method_get_sender (method);
		doc->uri = g_strdup (uri);
		ev_daemon->docs = g_list_prepend (ev_daemon->docs, doc);
		if (ev_daemon->n_docs++ == 0)
			ev_daemon_stop_killtimer (ev_daemon);
	}

	dbus_g_method_return (method, owner);

	return TRUE;
}

static gboolean
ev_daemon_unregister_document (EvDaemon              *ev_daemon,
			       const gchar           *uri,
			       DBusGMethodInvocation *method)
{
	EvDoc *doc;
	gchar *sender;

	doc = ev_daemon_find_doc (ev_daemon, uri);
	if (!doc) {
		g_warning ("Document %s is not registered\n", uri);
		dbus_g_method_return (method);

		return TRUE;
	}

	sender = dbus_g_method_get_sender (method);
	if (strcmp (doc->dbus_name, sender) != 0) {
		g_warning ("Failed to unregister document %s: invalid owner %s, expected %s\n",
			   uri, sender, doc->dbus_name);
		g_free (sender);
		dbus_g_method_return (method);

		return TRUE;
	}
	g_free (sender);

	ev_daemon->docs = g_list_remove (ev_daemon->docs, doc);
	ev_doc_free (doc);
	if (--ev_daemon->n_docs == 0)
		ev_daemon_start_killtimer (ev_daemon);

	dbus_g_method_return (method);

	return TRUE;
}

static void
do_exit (GMainLoop *loop,
	 GObject   *object)
{
	if (g_main_loop_is_running (loop))
		g_main_loop_quit (loop);
}

static gboolean
convert_metadata (const gchar *metadata)
{
	GFile   *file;
	char    *argv[3];
	gint     exit_status;
	GFileAttributeInfoList *namespaces;
	gboolean supported = FALSE;
	GError  *error = NULL;
	gboolean retval;

	/* If metadata is not supported for a local file
	 * is likely because and old gvfs version is running.
	 */
	file = g_file_new_for_path (metadata);
	namespaces = g_file_query_writable_namespaces (file, NULL, NULL);
	if (namespaces) {
		gint i;

		for (i = 0; i < namespaces->n_infos; i++) {
			if (strcmp (namespaces->infos[i].name, "metadata") == 0) {
				supported = TRUE;
				break;
			}
		}
		g_file_attribute_info_list_unref (namespaces);
	}
	if (!supported) {
		g_warning ("GVFS metadata not supported. "
			   "Evince will run without metadata support.\n");
		g_object_unref (file);
		return FALSE;
	}
	g_object_unref (file);

	argv[0] = g_build_filename (LIBEXECDIR, "evince-convert-metadata", NULL);
	argv[1] = (char *) metadata;
	argv[2] = NULL;

	retval = g_spawn_sync (NULL /* wd */, argv, NULL /* env */,
			       0, NULL, NULL, NULL, NULL,
			       &exit_status, &error);
	g_free (argv[0]);

	if (!retval) {
		g_printerr ("Error migrating metadata: %s\n", error->message);
		g_error_free (error);
	}

	return retval && WIFEXITED (exit_status) && WEXITSTATUS (exit_status) == 0;
}

static void
ev_migrate_metadata (void)
{
	gchar *updated;
	gchar *metadata;
	gchar *dot_dir;

	dot_dir = g_build_filename (g_get_home_dir (),
				    ".gnome2",
				    "evince",
				    NULL);

	updated = g_build_filename (dot_dir, "migrated-to-gvfs", NULL);
	if (g_file_test (updated, G_FILE_TEST_EXISTS)) {
		/* Already migrated */
		g_free (updated);
		g_free (dot_dir);
		return;
	}

	metadata = g_build_filename (dot_dir, "ev-metadata.xml", NULL);
	if (g_file_test (metadata, G_FILE_TEST_EXISTS)) {
		if (convert_metadata (metadata)) {
			gint fd;

			fd = g_creat (updated, 0600);
			if (fd != -1) {
				close (fd);
			}
		}
	}

	g_free (dot_dir);
	g_free (updated);
	g_free (metadata);
}

gint
main (gint argc, gchar **argv)
{
	GMainLoop *loop;

	/* Init glib threads asap */
	if (!g_thread_supported ())
		g_thread_init (NULL);

	g_type_init ();

	if (!ev_daemon_get ())
		return 1;

	ev_migrate_metadata ();

	loop = g_main_loop_new (NULL, FALSE);
	g_object_weak_ref (G_OBJECT (ev_daemon),
			   (GWeakNotify) do_exit,
			   loop);
	g_main_loop_run (loop);
	g_main_loop_unref (loop);

	return 0;
}