24 #define G_LOG_DOMAIN "EvinceDaemon"
26 #include <glib/gstdio.h>
29 #include <sys/types.h>
34 #include "ev-daemon-gdbus-generated.h"
36 #define EV_DBUS_DAEMON_NAME "org.gnome.evince.Daemon"
37 #define EV_DBUS_DAEMON_INTERFACE_NAME "org.gnome.evince.Daemon"
38 #define EV_DBUS_DAEMON_OBJECT_PATH "/org/gnome/evince/Daemon"
40 #define EV_DBUS_WINDOW_INTERFACE_NAME "org.gnome.evince.Window"
42 #define DAEMON_TIMEOUT (30)
46 #define EV_TYPE_DAEMON_APPLICATION (ev_daemon_application_get_type ())
47 #define EV_DAEMON_APPLICATION(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), EV_TYPE_DAEMON_APPLICATION, EvDaemonApplication))
48 #define EV_DAEMON_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_DAEMON_APPLICATION, EvDaemonApplicationClass))
49 #define EV_IS_DAEMON_APPLICATION(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), EV_TYPE_DAEMON_APPLICATION))
50 #define EV_IS_DAEMON_APPLICATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EV_TYPE_DAEMON_APPLICATION))
51 #define EV_DAEMON_APPLICATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EV_TYPE_DAEMON_APPLICATION, EvDaemonApplicationClass))
99 for (l = application->
docs; l != NULL; l = l->next) {
102 if (strcmp (doc->
uri, uri) == 0)
114 GError *error = NULL;
117 argv[0] = g_build_filename (BINDIR,
"evince", NULL);
118 argv[1] = (gchar *) uri;
121 retval = g_spawn_async (NULL , argv, NULL ,
122 0, NULL, NULL, NULL, &error);
124 g_printerr (
"Error spawning evince for uri %s: %s\n", uri, error->message);
125 g_error_free (error);
135 const gchar *name_owner,
138 LOG (
"Watch name'%s' appeared with owner '%s'", name, name_owner);
149 LOG (
"Watch name'%s' disappeared", name);
151 for (l = application->
docs; l != NULL; l = l->next) {
157 LOG (
"Watch found URI '%s' for name; removing", doc->
uri);
159 application->
docs = g_list_delete_link (application->
docs, l);
162 g_application_release (G_APPLICATION (application));
171 const gchar *dbus_name)
174 GList *uri_invocations;
176 LOG (
"RegisterDocument process pending invocations for URI %s", uri);
179 for (l = uri_invocations; l != NULL; l = l->next) {
180 GDBusMethodInvocation *invocation;
182 invocation = (GDBusMethodInvocation *)l->data;
183 g_dbus_method_invocation_return_value (invocation,
184 g_variant_new (
"(s)", dbus_name));
187 g_list_free (uri_invocations);
193 const gchar *sender_name,
194 const gchar *object_path,
195 const gchar *interface_name,
196 const gchar *signal_name,
197 GVariant *parameters,
204 g_variant_get (parameters,
"(&s)", &uri);
209 if (strcmp (uri, doc->
uri) == 0) {
213 g_dbus_connection_signal_unsubscribe (connection, doc->
loaded_id);
219 GDBusMethodInvocation *invocation,
223 GDBusConnection *connection;
229 LOG (
"RegisterDocument found owner '%s' for URI '%s'", doc->
dbus_name, uri);
230 ev_daemon_complete_register_document (
object, invocation, doc->
dbus_name);
235 sender = g_dbus_method_invocation_get_sender (invocation);
236 connection = g_dbus_method_invocation_get_connection (invocation);
238 LOG (
"RegisterDocument registered owner '%s' for URI '%s'", sender, uri);
240 doc = g_new (
EvDoc, 1);
242 doc->
uri = g_strdup (uri);
244 application->
docs = g_list_prepend (application->
docs, doc);
246 doc->
loaded_id = g_dbus_connection_signal_subscribe (connection,
255 doc->
watch_id = g_bus_watch_name_on_connection (connection,
257 G_BUS_NAME_WATCHER_FLAGS_NONE,
262 ev_daemon_complete_register_document (
object, invocation,
"");
264 g_application_hold (G_APPLICATION (application));
271 GDBusMethodInvocation *invocation,
278 LOG (
"UnregisterDocument URI '%s'", uri);
282 LOG (
"UnregisterDocument URI was not registered!");
283 g_dbus_method_invocation_return_error_literal (invocation,
285 G_DBUS_ERROR_INVALID_ARGS,
286 "URI not registered");
290 sender = g_dbus_method_invocation_get_sender (invocation);
291 if (strcmp (doc->
dbus_name, sender) != 0) {
292 LOG (
"UnregisterDocument called by non-owner (owner '%s' sender '%s')",
295 g_dbus_method_invocation_return_error_literal (invocation,
297 G_DBUS_ERROR_BAD_ADDRESS,
298 "Only owner can call this method");
302 application->
docs = g_list_remove (application->
docs, doc);
305 g_dbus_connection_signal_unsubscribe (g_dbus_method_invocation_get_connection (invocation),
312 ev_daemon_complete_unregister_document (
object, invocation);
314 g_application_release (G_APPLICATION (application));
321 GDBusMethodInvocation *invocation,
328 LOG (
"FindDocument URI '%s'", uri);
332 ev_daemon_complete_find_document (
object, invocation, doc->
dbus_name);
338 GList *uri_invocations;
339 gboolean ret_val =
TRUE;
343 if (uri_invocations == NULL) {
350 uri_invocations = g_list_prepend (uri_invocations, invocation);
358 LOG (
"FindDocument URI '%s' was not registered!", uri);
360 ev_daemon_complete_find_document (
object, invocation,
"");
369 GDBusConnection *connection,
370 const gchar *object_path,
376 if (!G_APPLICATION_CLASS (ev_daemon_application_parent_class)->dbus_register (gapplication,
382 skeleton = ev_daemon_skeleton_new ();
383 if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (skeleton),
387 g_object_unref (skeleton);
391 application->
daemon = skeleton;
392 g_signal_connect (skeleton,
"handle-register-document",
394 g_signal_connect (skeleton,
"handle-unregister-document",
396 g_signal_connect (skeleton,
"handle-find-document",
403 GDBusConnection *connection,
404 const gchar *object_path)
408 if (application->
daemon) {
409 g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (application->
daemon));
410 g_object_unref (application->
daemon);
411 application->
daemon = NULL;
414 G_APPLICATION_CLASS (ev_daemon_application_parent_class)->dbus_unregister (gapplication,
424 (GDestroyNotify) g_free,
438 G_OBJECT_CLASS (ev_daemon_application_parent_class)->finalize (
object);
444 GObjectClass *object_class = G_OBJECT_CLASS (klass);
445 GApplicationClass *g_application_class = G_APPLICATION_CLASS (klass);
458 GApplication *application;
459 const GApplicationFlags flags = G_APPLICATION_IS_SERVICE;
460 GError *error = NULL;
463 g_set_prgname (
"evince-daemon");
469 g_application_set_inactivity_timeout (application,
DAEMON_TIMEOUT);
471 if (!g_application_register (application, NULL, &error)) {
472 g_printerr (
"Failed to register: %s\n", error->message);
473 g_error_free (error);
474 g_object_unref (application);
479 status = g_application_run (application, 0, NULL);
480 g_object_unref (application);