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>
36 #include <libgnomeui/gnome-client.h>
40 #include "ev-application-service.h"
41 #include <dbus/dbus-glib-bindings.h>
44 struct _EvApplication {
45 GObject base_instance;
49 EggToolbarsModel *toolbars_model;
51 #ifndef HAVE_GTK_RECENT
52 EggRecentModel *recent_model;
55 TotemScrsaver *scr_saver;
57 gchar *last_chooser_uri;
60 struct _EvApplicationClass {
61 GObjectClass base_class;
64 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
66 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
70 ev_application_register_service (EvApplication *application)
72 static DBusGConnection *connection = NULL;
73 DBusGProxy *driver_proxy;
75 guint request_name_result;
78 g_warning ("Service already registered.");
82 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
83 if (connection == NULL) {
84 g_warning ("Service registration failed.");
90 driver_proxy = dbus_g_proxy_new_for_name (connection,
95 if (!org_freedesktop_DBus_request_name (driver_proxy,
96 APPLICATION_SERVICE_NAME,
97 DBUS_NAME_FLAG_DO_NOT_QUEUE,
98 &request_name_result, &err)) {
99 g_warning ("Service registration failed.");
100 g_clear_error (&err);
103 g_object_unref (driver_proxy);
105 if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
109 dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
110 &dbus_glib_ev_application_object_info);
111 dbus_g_connection_register_g_object (connection,
112 "/org/gnome/evince/Evince",
113 G_OBJECT (application));
115 application->scr_saver = totem_scrsaver_new (connection);
119 #endif /* ENABLE_DBUS */
122 * ev_application_get_instance:
124 * Checks for #EvApplication instance, if it doesn't exist it does create it.
126 * Returns: an instance of the #EvApplication data.
129 ev_application_get_instance (void)
131 static EvApplication *instance;
134 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
141 removed_from_session (GnomeClient *client, EvApplication *application)
143 ev_application_shutdown (application);
147 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
148 GnomeInteractStyle interact_style, gint fast, EvApplication *application)
154 windows = ev_application_get_windows (application);
155 restart_argv = g_new (char *, g_list_length (windows) + 1);
156 restart_argv[argc++] = g_strdup ("evince");
158 for (l = windows; l != NULL; l = l->next) {
159 EvWindow *window = EV_WINDOW (l->data);
160 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
163 gnome_client_set_restart_command (client, argc, restart_argv);
165 for (k = 0; k < argc; k++) {
166 g_free (restart_argv[k]);
169 g_list_free (windows);
170 g_free (restart_argv);
176 init_session (EvApplication *application)
180 client = gnome_master_client ();
182 g_signal_connect (client, "save_yourself",
183 G_CALLBACK (save_session), application);
184 g_signal_connect (client, "die",
185 G_CALLBACK (removed_from_session), application);
189 * ev_display_open_if_needed:
190 * @name: the name of the display to be open if it's needed.
192 * Search among all the open displays if any of them have the same name as the
193 * passed name. If the display isn't found it tries the open it.
195 * Returns: a #GdkDisplay of the display with the passed name.
198 ev_display_open_if_needed (const gchar *name)
202 GdkDisplay *display = NULL;
204 displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
206 for (l = displays; l != NULL; l = l->next) {
207 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
209 if (g_ascii_strcasecmp (display_name, name) == 0) {
215 g_slist_free (displays);
217 return display != NULL ? display : gdk_display_open (name);
221 * get_screen_from_args:
222 * @args: a #GHashTable with data passed to the application.
224 * Looks for the screen in the display available in the hash table passed to the
225 * application. If the display isn't opened, it's opened and the #GdkScreen
226 * assigned to the screen in that display returned.
228 * Returns: the #GdkScreen assigned to the screen on the display indicated by
229 * the data on the #GHashTable.
232 get_screen_from_args (GHashTable *args)
234 GValue *value = NULL;
235 GdkDisplay *display = NULL;
236 GdkScreen *screen = NULL;
238 g_assert (args != NULL);
240 value = g_hash_table_lookup (args, "display");
242 const gchar *display_name;
244 display_name = g_value_get_string (value);
245 display = ev_display_open_if_needed (display_name);
248 value = g_hash_table_lookup (args, "screen");
252 screen_number = g_value_get_int (value);
253 screen = gdk_display_get_screen (display, screen_number);
260 * get_window_run_mode_from_args:
261 * @args: a #GHashTable with data passed to the application.
263 * It does look if the mode option has been passed from command line, using it
264 * as the window run mode, otherwise the run mode will be the normal mode.
266 * Returns: The window run mode passed from command line or
267 * EV_WINDOW_MODE_NORMAL in other case.
269 static EvWindowRunMode
270 get_window_run_mode_from_args (GHashTable *args)
272 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
273 GValue *value = NULL;
275 g_assert (args != NULL);
277 value = g_hash_table_lookup (args, "mode");
279 mode = g_value_get_uint (value);
286 * get_destination_from_args:
287 * @args: a #GHashTable with data passed to the application.
289 * It does look for the page-label argument parsed from the command line and
290 * if it does exist, it returns an #EvLinkDest.
292 * Returns: An #EvLinkDest to page-label if it has been passed from the command
293 * line, NULL in other case.
296 get_destination_from_args (GHashTable *args)
298 EvLinkDest *dest = NULL;
299 GValue *value = NULL;
301 g_assert (args != NULL);
303 value = g_hash_table_lookup (args, "page-label");
305 const gchar *page_label;
307 page_label = g_value_get_string (value);
308 dest = ev_link_dest_new_page_label (page_label);
315 * get_unlink_temp_file_from_args:
316 * @args: a #GHashTable with data passed to the application.
318 * It does look if the unlink-temp-file option has been passed from the command
319 * line returning it's boolean representation, otherwise it does return %FALSE.
321 * Returns: the boolean representation of the unlink-temp-file value or %FALSE
325 get_unlink_temp_file_from_args (GHashTable *args)
327 gboolean unlink_temp_file = FALSE;
328 GValue *value = NULL;
330 g_assert (args != NULL);
332 value = g_hash_table_lookup (args, "unlink-temp-file");
334 unlink_temp_file = g_value_get_boolean (value);
337 return unlink_temp_file;
341 * ev_application_open_window:
342 * @application: The instance of the application.
343 * @args: A #GHashTable with the arguments data.
344 * @timestamp: Current time value.
345 * @error: The #GError facility.
347 * Creates a new window and if the args are available, it's not NULL, it gets
348 * the screen from them and assigns the just created window to it. At last it
354 ev_application_open_window (EvApplication *application,
359 GtkWidget *new_window = ev_window_new ();
360 GdkScreen *screen = NULL;
363 screen = get_screen_from_args (args);
367 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
370 gtk_widget_show (new_window);
372 gtk_window_present_with_time (GTK_WINDOW (new_window),
378 * ev_application_get_empty_window:
379 * @application: The instance of the application.
380 * @screen: The screen where the empty window will be search.
382 * It does look if there is any empty window in the indicated screen.
384 * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
388 ev_application_get_empty_window (EvApplication *application,
391 EvWindow *empty_window = NULL;
392 GList *windows = ev_application_get_windows (application);
395 for (l = windows; l != NULL; l = l->next) {
396 EvWindow *window = EV_WINDOW (l->data);
398 if (ev_window_is_empty (window) &&
399 gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
400 empty_window = window;
405 g_list_free (windows);
411 * ev_application_get_uri_window:
412 * @application: The instance of the application.
413 * @uri: The uri to be opened.
415 * It looks in the list of the windows for the one with the document represented
416 * by the passed uri on it. If the window is empty or the document isn't present
417 * on any window, it will return NULL.
419 * Returns: The #EvWindow where the document represented by the passed uri is
420 * shown, NULL in other case.
423 ev_application_get_uri_window (EvApplication *application, const char *uri)
425 EvWindow *uri_window = NULL;
426 GList *windows = gtk_window_list_toplevels ();
429 g_return_val_if_fail (uri != NULL, NULL);
431 for (l = windows; l != NULL; l = l->next) {
432 if (EV_IS_WINDOW (l->data)) {
433 EvWindow *window = EV_WINDOW (l->data);
434 const char *window_uri = ev_window_get_uri (window);
436 if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
443 g_list_free (windows);
449 * ev_application_open_uri_at_dest:
450 * @application: The instance of the application.
451 * @uri: The uri to be opened.
452 * @screen: Thee screen where the link will be shown.
453 * @dest: The #EvLinkDest of the document.
454 * @mode: The run mode of the window.
455 * @unlink_temp_file: The unlink_temp_file option value.
456 * @timestamp: Current time value.
459 ev_application_open_uri_at_dest (EvApplication *application,
463 EvWindowRunMode mode,
464 gboolean unlink_temp_file,
467 EvWindow *new_window;
468 GtkIconTheme *icon_theme;
470 g_return_if_fail (uri != NULL);
472 icon_theme = gtk_icon_theme_get_for_screen (screen);
477 gchar *ev_icons_path;
479 /* GtkIconTheme will then look in Evince custom hicolor dir
480 * for icons as well as the standard search paths
482 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
483 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
484 for (i = n_paths - 1; i >= 0; i--) {
485 if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
490 gtk_icon_theme_append_search_path (icon_theme,
493 g_free (ev_icons_path);
497 new_window = ev_application_get_uri_window (application, uri);
499 if (new_window == NULL) {
500 new_window = ev_application_get_empty_window (application, screen);
503 if (new_window == NULL) {
504 new_window = EV_WINDOW (ev_window_new ());
508 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
510 /* We need to load uri before showing the window, so
511 we can restore window size without flickering */
512 ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
514 ev_document_fc_mutex_lock ();
515 gtk_widget_show (GTK_WIDGET (new_window));
516 ev_document_fc_mutex_unlock ();
518 gtk_window_present_with_time (GTK_WINDOW (new_window),
523 * ev_application_open_uri:
524 * @application: The instance of the application.
525 * @uri: The uri to be opened
526 * @args: A #GHashTable with the arguments data.
527 * @timestamp: Current time value.
528 * @error: The #GError facility.
531 ev_application_open_uri (EvApplication *application,
537 EvLinkDest *dest = NULL;
538 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
539 gboolean unlink_temp_file = FALSE;
540 GdkScreen *screen = NULL;
543 screen = get_screen_from_args (args);
544 dest = get_destination_from_args (args);
545 mode = get_window_run_mode_from_args (args);
546 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
547 get_unlink_temp_file_from_args (args));
550 ev_application_open_uri_at_dest (application, uri, screen,
551 dest, mode, unlink_temp_file,
555 g_object_unref (dest);
561 ev_application_open_uri_list (EvApplication *application,
568 for (l = uri_list; l != NULL; l = l->next) {
569 ev_application_open_uri_at_dest (application, (char *)l->data,
570 screen, NULL, 0, FALSE,
576 ev_application_shutdown (EvApplication *application)
578 if (application->toolbars_model) {
579 g_object_unref (application->toolbars_model);
580 g_free (application->toolbars_file);
581 application->toolbars_model = NULL;
582 application->toolbars_file = NULL;
585 #ifndef HAVE_GTK_RECENT
586 if (application->recent_model) {
587 g_object_unref (application->recent_model);
588 application->recent_model = NULL;
592 g_free (application->last_chooser_uri);
593 g_object_unref (application);
599 ev_application_class_init (EvApplicationClass *ev_application_class)
604 ev_application_init (EvApplication *ev_application)
606 init_session (ev_application);
608 ev_application->toolbars_model = egg_toolbars_model_new ();
610 ev_application->toolbars_file = g_build_filename
611 (ev_dot_dir (), "evince_toolbar.xml", NULL);
613 egg_toolbars_model_load_names (ev_application->toolbars_model,
614 DATADIR "/evince-toolbar.xml");
616 if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
617 ev_application->toolbars_file)) {
618 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
619 DATADIR"/evince-toolbar.xml");
622 egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
623 EGG_TB_MODEL_NOT_REMOVABLE);
625 #ifndef HAVE_GTK_RECENT
626 ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
627 /* FIXME we should add a mime type filter but current eggrecent
628 has only a varargs style api which does not work well when
629 the list of mime types is dynamic */
630 egg_recent_model_set_limit (ev_application->recent_model, 5);
631 egg_recent_model_set_filter_groups (ev_application->recent_model,
633 #endif /* HAVE_GTK_RECENT */
637 * ev_application_get_windows:
638 * @application: The instance of the application.
640 * It creates a list of the top level windows.
642 * Returns: A #GList of the top level windows.
645 ev_application_get_windows (EvApplication *application)
647 GList *l, *toplevels;
648 GList *windows = NULL;
650 toplevels = gtk_window_list_toplevels ();
652 for (l = toplevels; l != NULL; l = l->next) {
653 if (EV_IS_WINDOW (l->data)) {
654 windows = g_list_append (windows, l->data);
658 g_list_free (toplevels);
664 ev_application_get_toolbars_model (EvApplication *application)
666 return application->toolbars_model;
669 #ifndef HAVE_GTK_RECENT
671 ev_application_get_recent_model (EvApplication *application)
673 return application->recent_model;
678 ev_application_save_toolbars_model (EvApplication *application)
680 egg_toolbars_model_save_toolbars (application->toolbars_model,
681 application->toolbars_file, "1.0");
685 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
687 g_free (application->last_chooser_uri);
688 application->last_chooser_uri = g_strdup (uri);
692 ev_application_get_chooser_uri (EvApplication *application)
694 return application->last_chooser_uri;
698 ev_application_screensaver_enable (EvApplication *application)
700 if (application->scr_saver)
701 totem_scrsaver_enable (application->scr_saver);
705 ev_application_screensaver_disable (EvApplication *application)
707 if (application->scr_saver)
708 totem_scrsaver_disable (application->scr_saver);