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 (!org_freedesktop_DBus_request_name (driver_proxy,
79 APPLICATION_SERVICE_NAME,
80 DBUS_NAME_FLAG_DO_NOT_QUEUE,
81 &request_name_result, &err)) {
82 g_warning ("Service registration failed.");
86 g_object_unref (driver_proxy);
88 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
92 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
93 &dbus_glib_ev_application_object_info);
94 dbus_g_connection_register_g_object (connection,
95 "/org/gnome/evince/Evince",
96 G_OBJECT (application));
98 application->scr_saver = totem_scrsaver_new (connection);
102 #endif /* ENABLE_DBUS */
105 ev_application_get_instance (void)
107 static EvApplication *instance;
110 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
117 removed_from_session (GnomeClient *client, EvApplication *application)
119 ev_application_shutdown (application);
123 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
124 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
130 windows = ev_application_get_windows (application);
131 restart_argv = g_new (char *, g_list_length (windows) + 1);
132 restart_argv[argc++] = g_strdup ("evince");
134 for (l = windows; l != NULL; l = l->next) {
135 EvWindow *window = EV_WINDOW (l->data);
136 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
139 gnome_client_set_restart_command (client, argc, restart_argv);
141 for (k = 0; k < argc; k++) {
142 g_free (restart_argv[k]);
145 g_list_free (windows);
146 g_free (restart_argv);
152 init_session (EvApplication *application)
156 client = gnome_master_client ();
158 g_signal_connect (client, "save_yourself",
159 G_CALLBACK (save_session), application);
160 g_signal_connect (client, "die",
161 G_CALLBACK (removed_from_session), application);
165 ev_display_open_if_needed (const gchar *name)
169 GdkDisplay *display = NULL;
171 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
173 for (l = displays; l != NULL; l = l->next) {
174 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
176 if (g_ascii_strcasecmp (display_name, name) == 0) {
182 g_slist_free (displays);
184 return display != NULL ? display : gdk_display_open (name);
188 get_screen_from_args (GHashTable *args)
190 GValue *value = NULL;
191 GdkDisplay *display = NULL;
192 GdkScreen *screen = NULL;
194 g_assert (args != NULL);
196 value = g_hash_table_lookup (args, "display");
198 const gchar *display_name;
200 display_name = g_value_get_string (value);
201 display = ev_display_open_if_needed (display_name);
204 value = g_hash_table_lookup (args, "screen");
208 screen_number = g_value_get_int (value);
209 screen = gdk_display_get_screen (display, screen_number);
215 static EvWindowRunMode
216 get_window_run_mode_from_args (GHashTable *args)
218 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
219 GValue *value = NULL;
221 g_assert (args != NULL);
223 value = g_hash_table_lookup (args, "mode");
225 mode = g_value_get_uint (value);
232 get_destination_from_args (GHashTable *args)
234 EvLinkDest *dest = NULL;
235 GValue *value = NULL;
237 g_assert (args != NULL);
239 value = g_hash_table_lookup (args, "page-label");
241 const gchar *page_label;
243 page_label = g_value_get_string (value);
244 dest = ev_link_dest_new_page_label (page_label);
251 get_unlink_temp_file_from_args (GHashTable *args)
253 gboolean unlink_temp_file = FALSE;
254 GValue *value = NULL;
256 g_assert (args != NULL);
258 value = g_hash_table_lookup (args, "unlink-temp-file");
260 unlink_temp_file = g_value_get_boolean (value);
263 return unlink_temp_file;
267 ev_application_open_window (EvApplication *application,
272 GtkWidget *new_window = ev_window_new ();
273 GdkScreen *screen = NULL;
276 screen = get_screen_from_args (args);
280 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
283 gtk_widget_show (new_window);
285 gtk_window_present_with_time (GTK_WINDOW (new_window),
291 ev_application_get_empty_window (EvApplication *application,
294 EvWindow *empty_window = NULL;
295 GList *windows = ev_application_get_windows (application);
298 for (l = windows; l != NULL; l = l->next) {
299 EvWindow *window = EV_WINDOW (l->data);
301 if (ev_window_is_empty (window) &&
302 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
303 empty_window = window;
308 g_list_free (windows);
314 ev_application_get_uri_window (EvApplication *application, const char *uri)
316 EvWindow *uri_window = NULL;
317 GList *windows = gtk_window_list_toplevels ();
320 g_return_val_if_fail (uri != NULL, NULL);
322 for (l = windows; l != NULL; l = l->next) {
323 if (EV_IS_WINDOW (l->data)) {
324 EvWindow *window = EV_WINDOW (l->data);
325 const char *window_uri = ev_window_get_uri (window);
327 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
334 g_list_free (windows);
340 ev_application_open_uri_at_dest (EvApplication *application,
344 EvWindowRunMode mode,
345 gboolean unlink_temp_file,
348 EvWindow *new_window;
349 GtkIconTheme *icon_theme;
351 g_return_if_fail (uri != NULL);
353 icon_theme = gtk_icon_theme_get_for_screen (screen);
358 gchar *ev_icons_path;
360 /* GtkIconTheme will then look in Evince custom hicolor dir
361 * for icons as well as the standard search paths
363 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
364 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
365 for (i = n_paths - 1; i >= 0; i--) {
366 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
371 gtk_icon_theme_append_search_path (icon_theme,
374 g_free (ev_icons_path);
378 new_window = ev_application_get_uri_window (application, uri);
380 if (new_window == NULL) {
381 new_window = ev_application_get_empty_window (application, screen);
384 if (new_window == NULL) {
385 new_window = EV_WINDOW (ev_window_new ());
389 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
391 /* We need to load uri before showing the window, so
392 we can restore window size without flickering */
393 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
395 ev_document_fc_mutex_lock ();
396 gtk_widget_show (GTK_WIDGET (new_window));
397 ev_document_fc_mutex_unlock ();
399 gtk_window_present_with_time (GTK_WINDOW (new_window),
404 ev_application_open_uri (EvApplication *application,
410 EvLinkDest *dest = NULL;
411 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
412 gboolean unlink_temp_file = FALSE;
413 GdkScreen *screen = NULL;
416 screen = get_screen_from_args (args);
417 dest = get_destination_from_args (args);
418 mode = get_window_run_mode_from_args (args);
419 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
420 get_unlink_temp_file_from_args (args));
423 ev_application_open_uri_at_dest (application, uri, screen,
424 dest, mode, unlink_temp_file,
428 g_object_unref (dest);
434 ev_application_open_uri_list (EvApplication *application,
441 for (l = uri_list; l != NULL; l = l->next) {
442 ev_application_open_uri_at_dest (application, (char *)l->data,
443 screen, NULL, 0, FALSE,
449 ev_application_shutdown (EvApplication *application)
451 if (application->toolbars_model) {
452 g_object_unref (application->toolbars_model);
453 g_object_unref (application->preview_toolbars_model);
454 g_free (application->toolbars_file);
455 application->toolbars_model = NULL;
456 application->preview_toolbars_model = NULL;
457 application->toolbars_file = NULL;
460 #ifndef HAVE_GTK_RECENT
461 if (application->recent_model) {
462 g_object_unref (application->recent_model);
463 application->recent_model = NULL;
467 g_free (application->last_chooser_uri);
468 g_object_unref (application);
474 ev_application_class_init (EvApplicationClass *ev_application_class)
479 ev_application_init (EvApplication *ev_application)
481 init_session (ev_application);
483 ev_application->toolbars_model = egg_toolbars_model_new ();
485 ev_application->toolbars_file = g_build_filename
486 (ev_dot_dir (), "evince_toolbar.xml", NULL);
488 egg_toolbars_model_load_names (ev_application->toolbars_model,
489 DATADIR "/evince-toolbar.xml");
491 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
492 ev_application->toolbars_file)) {
493 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
494 DATADIR"/evince-toolbar.xml");
497 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
498 EGG_TB_MODEL_NOT_REMOVABLE);
500 ev_application->preview_toolbars_model = egg_toolbars_model_new ();
502 egg_toolbars_model_load_toolbars (ev_application->preview_toolbars_model,
503 DATADIR"/evince-preview-toolbar.xml");
505 egg_toolbars_model_set_flags (ev_application->preview_toolbars_model, 0,
506 EGG_TB_MODEL_NOT_REMOVABLE);
508 #ifndef HAVE_GTK_RECENT
509 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
510 /* FIXME we should add a mime type filter but current eggrecent
511 has only a varargs style api which does not work well when
512 the list of mime types is dynamic */
513 egg_recent_model_set_limit (ev_application->recent_model, 5);
514 egg_recent_model_set_filter_groups (ev_application->recent_model,
516 #endif /* HAVE_GTK_RECENT */
520 ev_application_get_windows (EvApplication *application)
522 GList *l, *toplevels;
523 GList *windows = NULL;
525 toplevels = gtk_window_list_toplevels ();
527 for (l = toplevels; l != NULL; l = l->next) {
528 if (EV_IS_WINDOW (l->data)) {
529 windows = g_list_append (windows, l->data);
533 g_list_free (toplevels);
538 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application,
542 application->preview_toolbars_model : application->toolbars_model;
545 #ifndef HAVE_GTK_RECENT
546 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
548 return application->recent_model;
552 void ev_application_save_toolbars_model (EvApplication *application)
554 egg_toolbars_model_save_toolbars (application->toolbars_model,
555 application->toolbars_file, "1.0");
558 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
560 g_free (application->last_chooser_uri);
561 application->last_chooser_uri = g_strdup (uri);
564 const gchar* ev_application_get_chooser_uri (EvApplication *application)
566 return application->last_chooser_uri;
569 void ev_application_screensaver_enable (EvApplication *application)
571 if (application->scr_saver)
572 totem_scrsaver_enable (application->scr_saver);
575 void ev_application_screensaver_disable (EvApplication *application)
577 if (application->scr_saver)
578 totem_scrsaver_disable (application->scr_saver);