1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2004 Martin Kretzschmar
6 * Martin Kretzschmar <martink@gnome.org>
8 * Evince is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Evince is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
27 #include "ev-application.h"
29 #include "ev-file-helpers.h"
30 #include "ev-document-factory.h"
33 #include <glib/gi18n.h>
34 #include <glib-object.h>
35 #include <gtk/gtkfilechooserdialog.h>
36 #include <gtk/gtkstock.h>
37 #include <gtk/gtkwidget.h>
38 #include <gtk/gtkmain.h>
39 #include <libgnomeui/gnome-client.h>
43 #include "ev-application-service.h"
44 #include <dbus/dbus-glib-bindings.h>
47 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
49 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
53 ev_application_register_service (EvApplication *application)
55 static DBusGConnection *connection = NULL;
56 DBusGProxy *driver_proxy;
58 guint request_name_result;
61 g_warning ("Service already registered.");
65 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
66 if (connection == NULL) {
67 g_warning ("Service registration failed.");
73 driver_proxy = dbus_g_proxy_new_for_name (connection,
78 #if DBUS_VERSION >= 60
79 if (!org_freedesktop_DBus_request_name (driver_proxy,
80 APPLICATION_SERVICE_NAME,
81 DBUS_NAME_FLAG_DO_NOT_QUEUE,
82 &request_name_result, &err)) {
83 g_warning ("Service registration failed.");
87 if (!org_freedesktop_DBus_request_name (driver_proxy,
88 APPLICATION_SERVICE_NAME,
89 0, &request_name_result, &err)) {
90 g_warning ("Service registration failed.");
95 g_object_unref (driver_proxy);
97 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
101 #if DBUS_VERSION == 33
102 dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
103 &dbus_glib_ev_application_object_info);
105 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
106 &dbus_glib_ev_application_object_info);
109 dbus_g_connection_register_g_object (connection,
110 "/org/gnome/evince/Evince",
111 G_OBJECT (application));
113 application->scr_saver = totem_scrsaver_new (connection);
117 #endif /* ENABLE_DBUS */
120 ev_application_get_instance (void)
122 static EvApplication *instance;
125 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
132 removed_from_session (GnomeClient *client, EvApplication *application)
134 ev_application_shutdown (application);
138 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
139 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
145 windows = ev_application_get_windows (application);
146 restart_argv = g_new (char *, g_list_length (windows) + 1);
147 restart_argv[argc++] = g_strdup ("evince");
149 for (l = windows; l != NULL; l = l->next) {
150 EvWindow *window = EV_WINDOW (l->data);
151 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
154 gnome_client_set_restart_command (client, argc, restart_argv);
156 for (k = 0; k < argc; k++) {
157 g_free (restart_argv[k]);
160 g_list_free (windows);
161 g_free (restart_argv);
167 init_session (EvApplication *application)
171 client = gnome_master_client ();
173 g_signal_connect (client, "save_yourself",
174 G_CALLBACK (save_session), application);
175 g_signal_connect (client, "die",
176 G_CALLBACK (removed_from_session), application);
180 ev_display_open_if_needed (const gchar *name)
184 GdkDisplay *display = NULL;
186 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
188 for (l = displays; l != NULL; l = l->next) {
189 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
191 if (g_ascii_strcasecmp (display_name, name) == 0) {
197 g_slist_free (displays);
199 return display != NULL ? display : gdk_display_open (name);
203 get_screen_from_args (GHashTable *args)
205 GValue *value = NULL;
206 GdkDisplay *display = NULL;
207 GdkScreen *screen = NULL;
209 g_assert (args != NULL);
211 value = g_hash_table_lookup (args, "display");
213 const gchar *display_name;
215 display_name = g_value_get_string (value);
216 display = ev_display_open_if_needed (display_name);
219 value = g_hash_table_lookup (args, "screen");
223 screen_number = g_value_get_int (value);
224 screen = gdk_display_get_screen (display, screen_number);
230 static EvWindowRunMode
231 get_window_run_mode_from_args (GHashTable *args)
233 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
234 GValue *value = NULL;
236 g_assert (args != NULL);
238 value = g_hash_table_lookup (args, "mode");
240 mode = g_value_get_uint (value);
247 get_destination_from_args (GHashTable *args)
249 EvLinkDest *dest = NULL;
250 GValue *value = NULL;
252 g_assert (args != NULL);
254 value = g_hash_table_lookup (args, "page-label");
256 const gchar *page_label;
258 page_label = g_value_get_string (value);
259 dest = ev_link_dest_new_page_label (page_label);
266 get_unlink_temp_file_from_args (GHashTable *args)
268 gboolean unlink_temp_file = FALSE;
269 GValue *value = NULL;
271 g_assert (args != NULL);
273 value = g_hash_table_lookup (args, "unlink-temp-file");
275 unlink_temp_file = g_value_get_boolean (value);
278 return unlink_temp_file;
282 ev_application_open_window (EvApplication *application,
287 GtkWidget *new_window = ev_window_new ();
288 GdkScreen *screen = NULL;
291 screen = get_screen_from_args (args);
295 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
298 gtk_widget_show (new_window);
300 gtk_window_present_with_time (GTK_WINDOW (new_window),
306 ev_application_get_empty_window (EvApplication *application,
309 EvWindow *empty_window = NULL;
310 GList *windows = ev_application_get_windows (application);
313 for (l = windows; l != NULL; l = l->next) {
314 EvWindow *window = EV_WINDOW (l->data);
316 if (ev_window_is_empty (window) &&
317 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
318 empty_window = window;
323 g_list_free (windows);
329 ev_application_get_uri_window (EvApplication *application, const char *uri)
331 EvWindow *uri_window = NULL;
332 GList *windows = gtk_window_list_toplevels ();
335 g_return_val_if_fail (uri != NULL, NULL);
337 for (l = windows; l != NULL; l = l->next) {
338 if (EV_IS_WINDOW (l->data)) {
339 EvWindow *window = EV_WINDOW (l->data);
340 const char *window_uri = ev_window_get_uri (window);
342 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
349 g_list_free (windows);
355 ev_application_open_uri_at_dest (EvApplication *application,
359 EvWindowRunMode mode,
360 gboolean unlink_temp_file,
363 EvWindow *new_window;
365 g_return_if_fail (uri != NULL);
367 new_window = ev_application_get_uri_window (application, uri);
369 if (new_window == NULL) {
370 new_window = ev_application_get_empty_window (application, screen);
373 if (new_window == NULL) {
374 new_window = EV_WINDOW (ev_window_new ());
378 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
380 /* We need to load uri before showing the window, so
381 we can restore window size without flickering */
382 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
384 gtk_widget_show (GTK_WIDGET (new_window));
386 gtk_window_present_with_time (GTK_WINDOW (new_window),
391 ev_application_open_uri (EvApplication *application,
397 EvLinkDest *dest = NULL;
398 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
399 gboolean unlink_temp_file = FALSE;
400 GdkScreen *screen = NULL;
403 screen = get_screen_from_args (args);
404 dest = get_destination_from_args (args);
405 mode = get_window_run_mode_from_args (args);
406 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
407 get_unlink_temp_file_from_args (args));
410 ev_application_open_uri_at_dest (application, uri, screen,
411 dest, mode, unlink_temp_file,
415 g_object_unref (dest);
421 ev_application_open_uri_list (EvApplication *application,
428 for (l = uri_list; l != NULL; l = l->next) {
429 ev_application_open_uri_at_dest (application, (char *)l->data,
430 screen, NULL, 0, FALSE,
436 ev_application_shutdown (EvApplication *application)
438 if (application->toolbars_model) {
439 g_object_unref (application->toolbars_model);
440 g_object_unref (application->preview_toolbars_model);
441 g_free (application->toolbars_file);
442 application->toolbars_model = NULL;
443 application->preview_toolbars_model = NULL;
444 application->toolbars_file = NULL;
447 #ifndef HAVE_GTK_RECENT
448 if (application->recent_model) {
449 g_object_unref (application->recent_model);
450 application->recent_model = NULL;
454 g_free (application->last_chooser_uri);
455 g_object_unref (application);
461 ev_application_class_init (EvApplicationClass *ev_application_class)
466 ev_application_init (EvApplication *ev_application)
468 init_session (ev_application);
470 ev_application->toolbars_model = egg_toolbars_model_new ();
472 ev_application->toolbars_file = g_build_filename
473 (ev_dot_dir (), "evince_toolbar.xml", NULL);
475 egg_toolbars_model_load_names (ev_application->toolbars_model,
476 DATADIR "/evince-toolbar.xml");
478 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
479 ev_application->toolbars_file)) {
480 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
481 DATADIR"/evince-toolbar.xml");
484 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
485 EGG_TB_MODEL_NOT_REMOVABLE);
487 ev_application->preview_toolbars_model = egg_toolbars_model_new ();
489 egg_toolbars_model_load_toolbars (ev_application->preview_toolbars_model,
490 DATADIR"/evince-preview-toolbar.xml");
492 egg_toolbars_model_set_flags (ev_application->preview_toolbars_model, 0,
493 EGG_TB_MODEL_NOT_REMOVABLE);
495 #ifndef HAVE_GTK_RECENT
496 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
497 /* FIXME we should add a mime type filter but current eggrecent
498 has only a varargs style api which does not work well when
499 the list of mime types is dynamic */
500 egg_recent_model_set_limit (ev_application->recent_model, 5);
501 egg_recent_model_set_filter_groups (ev_application->recent_model,
503 #endif /* HAVE_GTK_RECENT */
507 ev_application_get_windows (EvApplication *application)
509 GList *l, *toplevels;
510 GList *windows = NULL;
512 toplevels = gtk_window_list_toplevels ();
514 for (l = toplevels; l != NULL; l = l->next) {
515 if (EV_IS_WINDOW (l->data)) {
516 windows = g_list_append (windows, l->data);
520 g_list_free (toplevels);
525 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application,
529 application->preview_toolbars_model : application->toolbars_model;
532 #ifndef HAVE_GTK_RECENT
533 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
535 return application->recent_model;
539 void ev_application_save_toolbars_model (EvApplication *application)
541 egg_toolbars_model_save_toolbars (application->toolbars_model,
542 application->toolbars_file, "1.0");
545 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
547 g_free (application->last_chooser_uri);
548 application->last_chooser_uri = g_strdup (uri);
551 const gchar* ev_application_get_chooser_uri (EvApplication *application)
553 return application->last_chooser_uri;
556 void ev_application_screensaver_enable (EvApplication *application)
558 if (application->scr_saver)
559 totem_scrsaver_enable (application->scr_saver);
562 void ev_application_screensaver_disable (EvApplication *application)
564 if (application->scr_saver)
565 totem_scrsaver_disable (application->scr_saver);