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.
23 #include "ev-application.h"
25 #include "ev-file-helpers.h"
26 #include "ev-document-factory.h"
27 #include "totem-scrsaver.h"
30 #include <glib/gi18n.h>
31 #include <glib-object.h>
32 #include <gtk/gtkfilechooserdialog.h>
33 #include <gtk/gtkstock.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkmain.h>
38 #include <libgnomeui/gnome-client.h>
42 #include "ev-application-service.h"
43 #include <dbus/dbus-glib-bindings.h>
48 struct _EvApplication {
49 GObject base_instance;
53 EggToolbarsModel *toolbars_model;
55 #ifndef HAVE_GTK_RECENT
56 EggRecentModel *recent_model;
59 TotemScrsaver *scr_saver;
61 gchar *last_chooser_uri;
64 struct _EvApplicationClass {
65 GObjectClass base_class;
68 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
70 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
74 ev_application_register_service (EvApplication *application)
76 static DBusGConnection *connection = NULL;
77 DBusGProxy *driver_proxy;
79 guint request_name_result;
82 g_warning ("Service already registered.");
86 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
87 if (connection == NULL) {
88 g_warning ("Service registration failed.");
94 driver_proxy = dbus_g_proxy_new_for_name (connection,
99 if (!org_freedesktop_DBus_request_name (driver_proxy,
100 APPLICATION_SERVICE_NAME,
101 DBUS_NAME_FLAG_DO_NOT_QUEUE,
102 &request_name_result, &err)) {
103 g_warning ("Service registration failed.");
104 g_clear_error (&err);
107 g_object_unref (driver_proxy);
109 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
113 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
114 &dbus_glib_ev_application_object_info);
115 dbus_g_connection_register_g_object (connection,
116 "/org/gnome/evince/Evince",
117 G_OBJECT (application));
119 application->scr_saver = totem_scrsaver_new (connection);
123 #endif /* ENABLE_DBUS */
126 * ev_application_get_instance:
128 * Checks for #EvApplication instance, if it doesn't exist it does create it.
130 * Returns: an instance of the #EvApplication data.
133 ev_application_get_instance (void)
135 static EvApplication *instance;
138 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
146 removed_from_session (GnomeClient *client, EvApplication *application)
148 ev_application_shutdown (application);
152 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
153 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
159 windows = ev_application_get_windows (application);
160 restart_argv = g_new (char *, g_list_length (windows) + 1);
161 restart_argv[argc++] = g_strdup ("evince");
163 for (l = windows; l != NULL; l = l->next) {
164 EvWindow *window = EV_WINDOW (l->data);
165 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
168 gnome_client_set_restart_command (client, argc, restart_argv);
170 for (k = 0; k < argc; k++) {
171 g_free (restart_argv[k]);
174 g_list_free (windows);
175 g_free (restart_argv);
181 init_session (EvApplication *application)
185 client = gnome_master_client ();
187 g_signal_connect (client, "save_yourself",
188 G_CALLBACK (save_session), application);
189 g_signal_connect (client, "die",
190 G_CALLBACK (removed_from_session), application);
195 * ev_display_open_if_needed:
196 * @name: the name of the display to be open if it's needed.
198 * Search among all the open displays if any of them have the same name as the
199 * passed name. If the display isn't found it tries the open it.
201 * Returns: a #GdkDisplay of the display with the passed name.
204 ev_display_open_if_needed (const gchar *name)
208 GdkDisplay *display = NULL;
210 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
212 for (l = displays; l != NULL; l = l->next) {
213 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
215 if (g_ascii_strcasecmp (display_name, name) == 0) {
221 g_slist_free (displays);
223 return display != NULL ? display : gdk_display_open (name);
227 * get_screen_from_args:
228 * @args: a #GHashTable with data passed to the application.
230 * Looks for the screen in the display available in the hash table passed to the
231 * application. If the display isn't opened, it's opened and the #GdkScreen
232 * assigned to the screen in that display returned.
234 * Returns: the #GdkScreen assigned to the screen on the display indicated by
235 * the data on the #GHashTable.
238 get_screen_from_args (GHashTable *args)
240 GValue *value = NULL;
241 GdkDisplay *display = NULL;
242 GdkScreen *screen = NULL;
244 g_assert (args != NULL);
246 value = g_hash_table_lookup (args, "display");
248 const gchar *display_name;
250 display_name = g_value_get_string (value);
251 display = ev_display_open_if_needed (display_name);
254 value = g_hash_table_lookup (args, "screen");
258 screen_number = g_value_get_int (value);
259 screen = gdk_display_get_screen (display, screen_number);
266 * get_window_run_mode_from_args:
267 * @args: a #GHashTable with data passed to the application.
269 * It does look if the mode option has been passed from command line, using it
270 * as the window run mode, otherwise the run mode will be the normal mode.
272 * Returns: The window run mode passed from command line or
273 * EV_WINDOW_MODE_NORMAL in other case.
275 static EvWindowRunMode
276 get_window_run_mode_from_args (GHashTable *args)
278 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
279 GValue *value = NULL;
281 g_assert (args != NULL);
283 value = g_hash_table_lookup (args, "mode");
285 mode = g_value_get_uint (value);
292 * get_destination_from_args:
293 * @args: a #GHashTable with data passed to the application.
295 * It does look for the page-label argument parsed from the command line and
296 * if it does exist, it returns an #EvLinkDest.
298 * Returns: An #EvLinkDest to page-label if it has been passed from the command
299 * line, NULL in other case.
302 get_destination_from_args (GHashTable *args)
304 EvLinkDest *dest = NULL;
305 GValue *value = NULL;
307 g_assert (args != NULL);
309 value = g_hash_table_lookup (args, "page-label");
311 const gchar *page_label;
313 page_label = g_value_get_string (value);
314 dest = ev_link_dest_new_page_label (page_label);
321 * get_unlink_temp_file_from_args:
322 * @args: a #GHashTable with data passed to the application.
324 * It does look if the unlink-temp-file option has been passed from the command
325 * line returning it's boolean representation, otherwise it does return %FALSE.
327 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
331 get_unlink_temp_file_from_args (GHashTable *args)
333 gboolean unlink_temp_file = FALSE;
334 GValue *value = NULL;
336 g_assert (args != NULL);
338 value = g_hash_table_lookup (args, "unlink-temp-file");
340 unlink_temp_file = g_value_get_boolean (value);
343 return unlink_temp_file;
347 * ev_application_open_window:
348 * @application: The instance of the application.
349 * @args: A #GHashTable with the arguments data.
350 * @timestamp: Current time value.
351 * @error: The #GError facility.
353 * Creates a new window and if the args are available, it's not NULL, it gets
354 * the screen from them and assigns the just created window to it. At last it
360 ev_application_open_window (EvApplication *application,
365 GtkWidget *new_window = ev_window_new ();
366 GdkScreen *screen = NULL;
369 screen = get_screen_from_args (args);
373 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
376 gtk_widget_show (new_window);
378 gtk_window_present_with_time (GTK_WINDOW (new_window),
384 * ev_application_get_empty_window:
385 * @application: The instance of the application.
386 * @screen: The screen where the empty window will be search.
388 * It does look if there is any empty window in the indicated screen.
390 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
394 ev_application_get_empty_window (EvApplication *application,
397 EvWindow *empty_window = NULL;
398 GList *windows = ev_application_get_windows (application);
401 for (l = windows; l != NULL; l = l->next) {
402 EvWindow *window = EV_WINDOW (l->data);
404 if (ev_window_is_empty (window) &&
405 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
406 empty_window = window;
411 g_list_free (windows);
417 * ev_application_get_uri_window:
418 * @application: The instance of the application.
419 * @uri: The uri to be opened.
421 * It looks in the list of the windows for the one with the document represented
422 * by the passed uri on it. If the window is empty or the document isn't present
423 * on any window, it will return NULL.
425 * Returns: The #EvWindow where the document represented by the passed uri is
426 * shown, NULL in other case.
429 ev_application_get_uri_window (EvApplication *application, const char *uri)
431 EvWindow *uri_window = NULL;
432 GList *windows = gtk_window_list_toplevels ();
435 g_return_val_if_fail (uri != NULL, NULL);
437 for (l = windows; l != NULL; l = l->next) {
438 if (EV_IS_WINDOW (l->data)) {
439 EvWindow *window = EV_WINDOW (l->data);
440 const char *window_uri = ev_window_get_uri (window);
442 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
449 g_list_free (windows);
455 * ev_application_open_uri_at_dest:
456 * @application: The instance of the application.
457 * @uri: The uri to be opened.
458 * @screen: Thee screen where the link will be shown.
459 * @dest: The #EvLinkDest of the document.
460 * @mode: The run mode of the window.
461 * @unlink_temp_file: The unlink_temp_file option value.
462 * @timestamp: Current time value.
465 ev_application_open_uri_at_dest (EvApplication *application,
469 EvWindowRunMode mode,
470 gboolean unlink_temp_file,
473 EvWindow *new_window;
474 GtkIconTheme *icon_theme;
476 g_return_if_fail (uri != NULL);
478 icon_theme = gtk_icon_theme_get_for_screen (screen);
483 gchar *ev_icons_path;
485 /* GtkIconTheme will then look in Evince custom hicolor dir
486 * for icons as well as the standard search paths
488 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
489 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
490 for (i = n_paths - 1; i >= 0; i--) {
491 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
496 gtk_icon_theme_append_search_path (icon_theme,
499 g_free (ev_icons_path);
503 new_window = ev_application_get_uri_window (application, uri);
505 if (new_window == NULL) {
506 new_window = ev_application_get_empty_window (application, screen);
509 if (new_window == NULL) {
510 new_window = EV_WINDOW (ev_window_new ());
514 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
516 /* We need to load uri before showing the window, so
517 we can restore window size without flickering */
518 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
520 ev_document_fc_mutex_lock ();
521 gtk_widget_show (GTK_WIDGET (new_window));
522 ev_document_fc_mutex_unlock ();
524 gtk_window_present_with_time (GTK_WINDOW (new_window),
529 * ev_application_open_uri:
530 * @application: The instance of the application.
531 * @uri: The uri to be opened
532 * @args: A #GHashTable with the arguments data.
533 * @timestamp: Current time value.
534 * @error: The #GError facility.
537 ev_application_open_uri (EvApplication *application,
543 EvLinkDest *dest = NULL;
544 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
545 gboolean unlink_temp_file = FALSE;
546 GdkScreen *screen = NULL;
549 screen = get_screen_from_args (args);
550 dest = get_destination_from_args (args);
551 mode = get_window_run_mode_from_args (args);
552 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
553 get_unlink_temp_file_from_args (args));
556 ev_application_open_uri_at_dest (application, uri, screen,
557 dest, mode, unlink_temp_file,
561 g_object_unref (dest);
567 ev_application_open_uri_list (EvApplication *application,
574 for (l = uri_list; l != NULL; l = l->next) {
575 ev_application_open_uri_at_dest (application, (char *)l->data,
576 screen, NULL, 0, FALSE,
582 ev_application_shutdown (EvApplication *application)
584 if (application->toolbars_model) {
585 g_object_unref (application->toolbars_model);
586 g_free (application->toolbars_file);
587 application->toolbars_model = NULL;
588 application->toolbars_file = NULL;
591 #ifndef HAVE_GTK_RECENT
592 if (application->recent_model) {
593 g_object_unref (application->recent_model);
594 application->recent_model = NULL;
598 g_free (application->last_chooser_uri);
599 g_object_unref (application);
605 ev_application_class_init (EvApplicationClass *ev_application_class)
610 ev_application_init (EvApplication *ev_application)
613 init_session (ev_application);
616 ev_application->toolbars_model = egg_toolbars_model_new ();
618 ev_application->toolbars_file = g_build_filename
619 (ev_dot_dir (), "evince_toolbar.xml", NULL);
621 egg_toolbars_model_load_names (ev_application->toolbars_model,
622 DATADIR "/evince-toolbar.xml");
624 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
625 ev_application->toolbars_file)) {
626 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
627 DATADIR"/evince-toolbar.xml");
630 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
631 EGG_TB_MODEL_NOT_REMOVABLE);
633 #ifndef HAVE_GTK_RECENT
634 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
635 /* FIXME we should add a mime type filter but current eggrecent
636 has only a varargs style api which does not work well when
637 the list of mime types is dynamic */
638 egg_recent_model_set_limit (ev_application->recent_model, 5);
639 egg_recent_model_set_filter_groups (ev_application->recent_model,
641 #endif /* HAVE_GTK_RECENT */
645 * ev_application_get_windows:
646 * @application: The instance of the application.
648 * It creates a list of the top level windows.
650 * Returns: A #GList of the top level windows.
653 ev_application_get_windows (EvApplication *application)
655 GList *l, *toplevels;
656 GList *windows = NULL;
658 toplevels = gtk_window_list_toplevels ();
660 for (l = toplevels; l != NULL; l = l->next) {
661 if (EV_IS_WINDOW (l->data)) {
662 windows = g_list_append (windows, l->data);
666 g_list_free (toplevels);
672 ev_application_get_toolbars_model (EvApplication *application)
674 return application->toolbars_model;
677 #ifndef HAVE_GTK_RECENT
679 ev_application_get_recent_model (EvApplication *application)
681 return application->recent_model;
686 ev_application_save_toolbars_model (EvApplication *application)
688 egg_toolbars_model_save_toolbars (application->toolbars_model,
689 application->toolbars_file, "1.0");
693 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
695 g_free (application->last_chooser_uri);
696 application->last_chooser_uri = g_strdup (uri);
700 ev_application_get_chooser_uri (EvApplication *application)
702 return application->last_chooser_uri;
706 ev_application_screensaver_enable (EvApplication *application)
708 if (application->scr_saver)
709 totem_scrsaver_enable (application->scr_saver);
713 ev_application_screensaver_disable (EvApplication *application)
715 if (application->scr_saver)
716 totem_scrsaver_disable (application->scr_saver);