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:
107 * Checks for #EvApplication instance, if it doesn't exist it does create it.
109 * Returns: an instance of the #EvApplication data.
112 ev_application_get_instance (void)
114 static EvApplication *instance;
117 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
124 removed_from_session (GnomeClient *client, EvApplication *application)
126 ev_application_shutdown (application);
130 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
131 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
137 windows = ev_application_get_windows (application);
138 restart_argv = g_new (char *, g_list_length (windows) + 1);
139 restart_argv[argc++] = g_strdup ("evince");
141 for (l = windows; l != NULL; l = l->next) {
142 EvWindow *window = EV_WINDOW (l->data);
143 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
146 gnome_client_set_restart_command (client, argc, restart_argv);
148 for (k = 0; k < argc; k++) {
149 g_free (restart_argv[k]);
152 g_list_free (windows);
153 g_free (restart_argv);
159 init_session (EvApplication *application)
163 client = gnome_master_client ();
165 g_signal_connect (client, "save_yourself",
166 G_CALLBACK (save_session), application);
167 g_signal_connect (client, "die",
168 G_CALLBACK (removed_from_session), application);
172 * ev_display_open_if_needed:
173 * @name: the name of the display to be open if it's needed.
175 * Search among all the open displays if any of them have the same name as the
176 * passed name. If the display isn't found it tries the open it.
178 * Returns: a #GdkDisplay of the display with the passed name.
181 ev_display_open_if_needed (const gchar *name)
185 GdkDisplay *display = NULL;
187 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
189 for (l = displays; l != NULL; l = l->next) {
190 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
192 if (g_ascii_strcasecmp (display_name, name) == 0) {
198 g_slist_free (displays);
200 return display != NULL ? display : gdk_display_open (name);
204 * get_screen_from_args:
205 * @args: a #GHashTable with data passed to the application.
207 * Looks for the screen in the display available in the hash table passed to the
208 * application. If the display isn't opened, it's opened and the #GdkScreen
209 * assigned to the screen in that display returned.
211 * Returns: the #GdkScreen assigned to the screen on the display indicated by
212 * the data on the #GHashTable.
215 get_screen_from_args (GHashTable *args)
217 GValue *value = NULL;
218 GdkDisplay *display = NULL;
219 GdkScreen *screen = NULL;
221 g_assert (args != NULL);
223 value = g_hash_table_lookup (args, "display");
225 const gchar *display_name;
227 display_name = g_value_get_string (value);
228 display = ev_display_open_if_needed (display_name);
231 value = g_hash_table_lookup (args, "screen");
235 screen_number = g_value_get_int (value);
236 screen = gdk_display_get_screen (display, screen_number);
243 * get_window_run_mode_from_args:
244 * @args: a #GHashTable with data passed to the application.
246 * It does look if the mode option has been passed from command line, using it
247 * as the window run mode, otherwise the run mode will be the normal mode.
249 * Returns: The window run mode passed from command line or
250 * EV_WINDOW_MODE_NORMAL in other case.
252 static EvWindowRunMode
253 get_window_run_mode_from_args (GHashTable *args)
255 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
256 GValue *value = NULL;
258 g_assert (args != NULL);
260 value = g_hash_table_lookup (args, "mode");
262 mode = g_value_get_uint (value);
269 * get_destination_from_args:
270 * @args: a #GHashTable with data passed to the application.
272 * It does look for the page-label argument parsed from the command line and
273 * if it does exist, it returns an #EvLinkDest.
275 * Returns: An #EvLinkDest to page-label if it has been passed from the command
276 * line, NULL in other case.
279 get_destination_from_args (GHashTable *args)
281 EvLinkDest *dest = NULL;
282 GValue *value = NULL;
284 g_assert (args != NULL);
286 value = g_hash_table_lookup (args, "page-label");
288 const gchar *page_label;
290 page_label = g_value_get_string (value);
291 dest = ev_link_dest_new_page_label (page_label);
298 * get_unlink_temp_file_from_args:
299 * @args: a #GHashTable with data passed to the application.
301 * It does look if the unlink-temp-file option has been passed from the command
302 * line returning it's boolean representation, otherwise it does return %FALSE.
304 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
308 get_unlink_temp_file_from_args (GHashTable *args)
310 gboolean unlink_temp_file = FALSE;
311 GValue *value = NULL;
313 g_assert (args != NULL);
315 value = g_hash_table_lookup (args, "unlink-temp-file");
317 unlink_temp_file = g_value_get_boolean (value);
320 return unlink_temp_file;
324 * ev_application_open_window:
325 * @application: The instance of the application.
326 * @args: A #GHashTable with the arguments data.
327 * @timestamp: Current time value.
328 * @error: The #GError facility.
330 * Creates a new window and if the args are available, it's not NULL, it gets
331 * the screen from them and assigns the just created window to it. At last it
337 ev_application_open_window (EvApplication *application,
342 GtkWidget *new_window = ev_window_new ();
343 GdkScreen *screen = NULL;
346 screen = get_screen_from_args (args);
350 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
353 gtk_widget_show (new_window);
355 gtk_window_present_with_time (GTK_WINDOW (new_window),
361 * ev_application_get_empty_window:
362 * @application: The instance of the application.
363 * @screen: The screen where the empty window will be search.
365 * It does look if there is any empty window in the indicated screen.
367 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
371 ev_application_get_empty_window (EvApplication *application,
374 EvWindow *empty_window = NULL;
375 GList *windows = ev_application_get_windows (application);
378 for (l = windows; l != NULL; l = l->next) {
379 EvWindow *window = EV_WINDOW (l->data);
381 if (ev_window_is_empty (window) &&
382 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
383 empty_window = window;
388 g_list_free (windows);
394 * ev_application_get_uri_window:
395 * @application: The instance of the application.
396 * @uri: The uri to be opened.
398 * It looks in the list of the windows for the one with the document represented
399 * by the passed uri on it. If the window is empty or the document isn't present
400 * on any window, it will return NULL.
402 * Returns: The #EvWindow where the document represented by the passed uri is
403 * shown, NULL in other case.
406 ev_application_get_uri_window (EvApplication *application, const char *uri)
408 EvWindow *uri_window = NULL;
409 GList *windows = gtk_window_list_toplevels ();
412 g_return_val_if_fail (uri != NULL, NULL);
414 for (l = windows; l != NULL; l = l->next) {
415 if (EV_IS_WINDOW (l->data)) {
416 EvWindow *window = EV_WINDOW (l->data);
417 const char *window_uri = ev_window_get_uri (window);
419 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
426 g_list_free (windows);
432 * ev_application_open_uri_at_dest:
433 * @application: The instance of the application.
434 * @uri: The uri to be opened.
435 * @screen: Thee screen where the link will be shown.
436 * @dest: The #EvLinkDest of the document.
437 * @mode: The run mode of the window.
438 * @unlink_temp_file: The unlink_temp_file option value.
439 * @timestamp: Current time value.
442 ev_application_open_uri_at_dest (EvApplication *application,
446 EvWindowRunMode mode,
447 gboolean unlink_temp_file,
450 EvWindow *new_window;
451 GtkIconTheme *icon_theme;
453 g_return_if_fail (uri != NULL);
455 icon_theme = gtk_icon_theme_get_for_screen (screen);
460 gchar *ev_icons_path;
462 /* GtkIconTheme will then look in Evince custom hicolor dir
463 * for icons as well as the standard search paths
465 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
466 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
467 for (i = n_paths - 1; i >= 0; i--) {
468 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
473 gtk_icon_theme_append_search_path (icon_theme,
476 g_free (ev_icons_path);
480 new_window = ev_application_get_uri_window (application, uri);
482 if (new_window == NULL) {
483 new_window = ev_application_get_empty_window (application, screen);
486 if (new_window == NULL) {
487 new_window = EV_WINDOW (ev_window_new ());
491 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
493 /* We need to load uri before showing the window, so
494 we can restore window size without flickering */
495 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
497 ev_document_fc_mutex_lock ();
498 gtk_widget_show (GTK_WIDGET (new_window));
499 ev_document_fc_mutex_unlock ();
501 gtk_window_present_with_time (GTK_WINDOW (new_window),
506 * ev_application_open_uri:
507 * @application: The instance of the application.
508 * @uri: The uri to be opened
509 * @args: A #GHashTable with the arguments data.
510 * @timestamp: Current time value.
511 * @error: The #GError facility.
514 ev_application_open_uri (EvApplication *application,
520 EvLinkDest *dest = NULL;
521 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
522 gboolean unlink_temp_file = FALSE;
523 GdkScreen *screen = NULL;
526 screen = get_screen_from_args (args);
527 dest = get_destination_from_args (args);
528 mode = get_window_run_mode_from_args (args);
529 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
530 get_unlink_temp_file_from_args (args));
533 ev_application_open_uri_at_dest (application, uri, screen,
534 dest, mode, unlink_temp_file,
538 g_object_unref (dest);
544 ev_application_open_uri_list (EvApplication *application,
551 for (l = uri_list; l != NULL; l = l->next) {
552 ev_application_open_uri_at_dest (application, (char *)l->data,
553 screen, NULL, 0, FALSE,
559 ev_application_shutdown (EvApplication *application)
561 if (application->toolbars_model) {
562 g_object_unref (application->toolbars_model);
563 g_object_unref (application->preview_toolbars_model);
564 g_free (application->toolbars_file);
565 application->toolbars_model = NULL;
566 application->preview_toolbars_model = NULL;
567 application->toolbars_file = NULL;
570 #ifndef HAVE_GTK_RECENT
571 if (application->recent_model) {
572 g_object_unref (application->recent_model);
573 application->recent_model = NULL;
577 g_free (application->last_chooser_uri);
578 g_object_unref (application);
584 ev_application_class_init (EvApplicationClass *ev_application_class)
589 ev_application_init (EvApplication *ev_application)
591 init_session (ev_application);
593 ev_application->toolbars_model = egg_toolbars_model_new ();
595 ev_application->toolbars_file = g_build_filename
596 (ev_dot_dir (), "evince_toolbar.xml", NULL);
598 egg_toolbars_model_load_names (ev_application->toolbars_model,
599 DATADIR "/evince-toolbar.xml");
601 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
602 ev_application->toolbars_file)) {
603 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
604 DATADIR"/evince-toolbar.xml");
607 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
608 EGG_TB_MODEL_NOT_REMOVABLE);
610 ev_application->preview_toolbars_model = egg_toolbars_model_new ();
612 egg_toolbars_model_load_toolbars (ev_application->preview_toolbars_model,
613 DATADIR"/evince-preview-toolbar.xml");
615 egg_toolbars_model_set_flags (ev_application->preview_toolbars_model, 0,
616 EGG_TB_MODEL_NOT_REMOVABLE);
618 #ifndef HAVE_GTK_RECENT
619 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
620 /* FIXME we should add a mime type filter but current eggrecent
621 has only a varargs style api which does not work well when
622 the list of mime types is dynamic */
623 egg_recent_model_set_limit (ev_application->recent_model, 5);
624 egg_recent_model_set_filter_groups (ev_application->recent_model,
626 #endif /* HAVE_GTK_RECENT */
630 * ev_application_get_windows:
631 * @application: The instance of the application.
633 * It creates a list of the top level windows.
635 * Returns: A #GList of the top level windows.
638 ev_application_get_windows (EvApplication *application)
640 GList *l, *toplevels;
641 GList *windows = NULL;
643 toplevels = gtk_window_list_toplevels ();
645 for (l = toplevels; l != NULL; l = l->next) {
646 if (EV_IS_WINDOW (l->data)) {
647 windows = g_list_append (windows, l->data);
651 g_list_free (toplevels);
656 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application,
660 application->preview_toolbars_model : application->toolbars_model;
663 #ifndef HAVE_GTK_RECENT
664 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
666 return application->recent_model;
670 void ev_application_save_toolbars_model (EvApplication *application)
672 egg_toolbars_model_save_toolbars (application->toolbars_model,
673 application->toolbars_file, "1.0");
676 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
678 g_free (application->last_chooser_uri);
679 application->last_chooser_uri = g_strdup (uri);
682 const gchar* ev_application_get_chooser_uri (EvApplication *application)
684 return application->last_chooser_uri;
687 void ev_application_screensaver_enable (EvApplication *application)
689 if (application->scr_saver)
690 totem_scrsaver_enable (application->scr_saver);
693 void ev_application_screensaver_disable (EvApplication *application)
695 if (application->scr_saver)
696 totem_scrsaver_disable (application->scr_saver);