]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
Add support for external links to EvViewPresentation
[evince.git] / shell / ev-window.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /* this file is part of evince, a gnome document viewer
3  *
4  *  Copyright (C) 2009 Juanjo Marín <juanj.marin@juntadeandalucia.es>
5  *  Copyright (C) 2008 Carlos Garcia Campos
6  *  Copyright (C) 2004 Martin Kretzschmar
7  *  Copyright (C) 2004 Red Hat, Inc.
8  *  Copyright (C) 2000, 2001, 2002, 2003, 2004 Marco Pesenti Gritti
9  *  Copyright © 2003, 2004, 2005, 2009 Christian Persch
10  *
11  *  Author:
12  *    Martin Kretzschmar <martink@gnome.org>
13  *
14  * Evince is free software; you can redistribute it and/or modify it
15  * under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * Evince is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <errno.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <math.h>
38
39 #include <glib/gstdio.h>
40 #include <glib/gi18n.h>
41 #include <gio/gio.h>
42 #include <gtk/gtk.h>
43
44 #include "egg-editable-toolbar.h"
45 #include "egg-toolbar-editor.h"
46 #include "egg-toolbars-model.h"
47
48 #include "eggfindbar.h"
49
50 #include "ephy-zoom-action.h"
51 #include "ephy-zoom.h"
52
53 #include "ev-application.h"
54 #include "ev-document-factory.h"
55 #include "ev-document-find.h"
56 #include "ev-document-fonts.h"
57 #include "ev-document-images.h"
58 #include "ev-document-links.h"
59 #include "ev-document-annotations.h"
60 #include "ev-document-type-builtins.h"
61 #include "ev-document-misc.h"
62 #include "ev-file-exporter.h"
63 #include "ev-file-helpers.h"
64 #include "ev-file-monitor.h"
65 #include "ev-history.h"
66 #include "ev-image.h"
67 #include "ev-job-scheduler.h"
68 #include "ev-jobs.h"
69 #include "ev-message-area.h"
70 #include "ev-metadata.h"
71 #include "ev-navigation-action.h"
72 #include "ev-open-recent-action.h"
73 #include "ev-page-action.h"
74 #include "ev-password-view.h"
75 #include "ev-properties-dialog.h"
76 #include "ev-sidebar-annotations.h"
77 #include "ev-sidebar-attachments.h"
78 #include "ev-sidebar-bookmarks.h"
79 #include "ev-sidebar.h"
80 #include "ev-sidebar-links.h"
81 #include "ev-sidebar-page.h"
82 #include "ev-sidebar-thumbnails.h"
83 #include "ev-sidebar-layers.h"
84 #include "ev-stock-icons.h"
85 #include "ev-utils.h"
86 #include "ev-keyring.h"
87 #include "ev-view.h"
88 #include "ev-view-presentation.h"
89 #include "ev-view-type-builtins.h"
90 #include "ev-window.h"
91 #include "ev-window-title.h"
92 #include "ev-print-operation.h"
93 #include "ev-progress-message-area.h"
94 #include "ev-annotation-properties-dialog.h"
95 #include "ev-bookmarks.h"
96 #include "ev-bookmark-action.h"
97
98 #ifdef ENABLE_DBUS
99 #include "ev-media-player-keys.h"
100 #endif /* ENABLE_DBUS */
101
102 typedef enum {
103         PAGE_MODE_DOCUMENT,
104         PAGE_MODE_PASSWORD
105 } EvWindowPageMode;
106
107 typedef enum {
108         EV_CHROME_MENUBAR       = 1 << 0,
109         EV_CHROME_TOOLBAR       = 1 << 1,
110         EV_CHROME_FINDBAR       = 1 << 2,
111         EV_CHROME_RAISE_TOOLBAR = 1 << 3,
112         EV_CHROME_FULLSCREEN_TOOLBAR    = 1 << 4,
113         EV_CHROME_SIDEBAR       = 1 << 5,
114         EV_CHROME_NORMAL        = EV_CHROME_MENUBAR | EV_CHROME_TOOLBAR | EV_CHROME_SIDEBAR
115 } EvChrome;
116
117 typedef enum {
118         EV_SAVE_DOCUMENT,
119         EV_SAVE_ATTACHMENT,
120         EV_SAVE_IMAGE
121 } EvSaveType;
122
123 struct _EvWindowPrivate {
124         /* UI */
125         EvChrome chrome;
126
127         GtkWidget *main_box;
128         GtkWidget *menubar;
129         GtkWidget *toolbar;
130         GtkWidget *hpaned;
131         GtkWidget *view_box;
132         GtkWidget *sidebar;
133         GtkWidget *find_bar;
134         GtkWidget *scrolled_window;
135         GtkWidget *view;
136         GtkWidget *presentation_view;
137         GtkWidget *message_area;
138         GtkWidget *password_view;
139         GtkWidget *sidebar_thumbs;
140         GtkWidget *sidebar_links;
141         GtkWidget *sidebar_attachments;
142         GtkWidget *sidebar_layers;
143         GtkWidget *sidebar_annots;
144         GtkWidget *sidebar_bookmarks;
145
146         /* Settings */
147         GSettings *settings;
148         GSettings *default_settings;
149         GSettings *lockdown_settings;
150
151         /* Menubar accels */
152         guint           menubar_accel_keyval;
153         GdkModifierType menubar_accel_modifier;
154
155         /* Progress Messages */
156         guint progress_idle;
157         GCancellable *progress_cancellable;
158
159         /* Dialogs */
160         GtkWidget *properties;
161         GtkWidget *print_dialog;
162
163         /* UI Builders */
164         GtkActionGroup   *action_group;
165         GtkActionGroup   *view_popup_action_group;
166         GtkActionGroup   *attachment_popup_action_group;
167         GtkRecentManager *recent_manager;
168         GtkActionGroup   *recent_action_group;
169         guint             recent_ui_id;
170         GtkActionGroup   *bookmarks_action_group;
171         guint             bookmarks_ui_id;
172         GtkUIManager     *ui_manager;
173
174         /* Fullscreen mode */
175         GtkWidget *fullscreen_toolbar;
176
177         /* Popup view */
178         GtkWidget    *view_popup;
179         EvLink       *link;
180         EvImage      *image;
181         EvAnnotation *annot;
182
183         /* Popup attachment */
184         GtkWidget    *attachment_popup;
185         GList        *attach_list;
186
187         /* Document */
188         EvDocumentModel *model;
189         char *uri;
190         glong uri_mtime;
191         char *local_uri;
192         gboolean in_reload;
193         EvFileMonitor *monitor;
194         guint setup_document_idle;
195         
196         EvDocument *document;
197         EvHistory *history;
198         EvWindowPageMode page_mode;
199         EvWindowTitle *title;
200         EvMetadata *metadata;
201         EvBookmarks *bookmarks;
202         gboolean is_new_doc;
203
204         /* Load params */
205         EvLinkDest       *dest;
206         gchar            *search_string;
207         EvWindowRunMode   window_mode;
208
209         EvJob            *load_job;
210         EvJob            *reload_job;
211         EvJob            *thumbnail_job;
212         EvJob            *save_job;
213         EvJob            *find_job;
214
215         /* Printing */
216         GQueue           *print_queue;
217         GtkPrintSettings *print_settings;
218         GtkPageSetup     *print_page_setup;
219         gboolean          close_after_print;
220
221 #ifdef ENABLE_DBUS
222         /* DBus */
223         guint  dbus_object_id;
224         gchar *dbus_object_path;
225 #endif
226 };
227
228 #define EV_WINDOW_GET_PRIVATE(object) \
229         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_WINDOW, EvWindowPrivate))
230
231 #define EV_WINDOW_IS_PRESENTATION(w) (w->priv->presentation_view != NULL)
232
233 #define PAGE_SELECTOR_ACTION    "PageSelector"
234 #define ZOOM_CONTROL_ACTION     "ViewZoom"
235 #define NAVIGATION_ACTION       "Navigation"
236
237 #define GS_LOCKDOWN_SCHEMA_NAME  "org.gnome.desktop.lockdown"
238 #define GS_LOCKDOWN_SAVE         "disable-save-to-disk"
239 #define GS_LOCKDOWN_PRINT        "disable-printing"
240 #define GS_LOCKDOWN_PRINT_SETUP  "disable-print-setup"
241
242 #ifdef ENABLE_DBUS
243 #define EV_WINDOW_DBUS_OBJECT_PATH "/org/gnome/evince/Window/%d"
244 #define EV_WINDOW_DBUS_INTERFACE   "org.gnome.evince.Window"
245 #endif
246
247 #define GS_SCHEMA_NAME           "org.gnome.Evince"
248 #define GS_OVERRIDE_RESTRICTIONS "override-restrictions"
249
250 #define SIDEBAR_DEFAULT_SIZE    132
251 #define LINKS_SIDEBAR_ID "links"
252 #define THUMBNAILS_SIDEBAR_ID "thumbnails"
253 #define ATTACHMENTS_SIDEBAR_ID "attachments"
254 #define LAYERS_SIDEBAR_ID "layers"
255 #define ANNOTS_SIDEBAR_ID "annotations"
256 #define BOOKMARKS_SIDEBAR_ID "bookmarks"
257
258 #define EV_PRINT_SETTINGS_FILE  "print-settings"
259 #define EV_PRINT_SETTINGS_GROUP "Print Settings"
260 #define EV_PAGE_SETUP_GROUP     "Page Setup"
261
262 #define EV_TOOLBARS_FILENAME "evince-toolbar.xml"
263
264 #define MIN_SCALE 0.05409
265 #define PAGE_CACHE_SIZE 52428800 /* 50MB */
266
267 #define MAX_RECENT_ITEM_LEN (40)
268
269 static const gchar *document_print_settings[] = {
270         GTK_PRINT_SETTINGS_N_COPIES,
271         GTK_PRINT_SETTINGS_COLLATE,
272         GTK_PRINT_SETTINGS_REVERSE,
273         GTK_PRINT_SETTINGS_NUMBER_UP,
274         GTK_PRINT_SETTINGS_SCALE,
275         GTK_PRINT_SETTINGS_PRINT_PAGES,
276         GTK_PRINT_SETTINGS_PAGE_RANGES,
277         GTK_PRINT_SETTINGS_PAGE_SET,
278         GTK_PRINT_SETTINGS_OUTPUT_URI
279 };
280
281 static void     ev_window_update_actions                (EvWindow         *ev_window);
282 static void     ev_window_sidebar_visibility_changed_cb (EvSidebar        *ev_sidebar,
283                                                          GParamSpec       *pspec,
284                                                          EvWindow         *ev_window);
285 static void     ev_window_view_toolbar_cb               (GtkAction        *action,
286                                                          EvWindow         *ev_window);
287 static void     ev_window_set_page_mode                 (EvWindow         *window,
288                                                          EvWindowPageMode  page_mode);
289 static void     ev_window_load_job_cb                   (EvJob            *job,
290                                                          gpointer          data);
291 static void     ev_window_reload_document               (EvWindow         *window,
292                                                          EvLinkDest *dest);
293 static void     ev_window_reload_job_cb                 (EvJob            *job,
294                                                          EvWindow         *window);
295 static void     ev_window_set_icon_from_thumbnail       (EvJobThumbnail   *job,
296                                                          EvWindow         *ev_window);
297 static void     ev_window_save_job_cb                   (EvJob            *save,
298                                                          EvWindow         *window);
299 static void     ev_window_sizing_mode_changed_cb        (EvDocumentModel  *model,
300                                                          GParamSpec       *pspec,
301                                                          EvWindow         *ev_window);
302 static void     ev_window_zoom_changed_cb               (EvDocumentModel  *model,
303                                                          GParamSpec       *pspec,
304                                                          EvWindow         *ev_window);
305 static void     ev_window_add_recent                    (EvWindow         *window,
306                                                          const char       *filename);
307 static void     ev_window_run_fullscreen                (EvWindow         *window);
308 static void     ev_window_stop_fullscreen               (EvWindow         *window,
309                                                          gboolean          unfullscreen_window);
310 static void     ev_window_cmd_view_fullscreen           (GtkAction        *action,
311                                                          EvWindow         *window);
312 static void     ev_window_run_presentation              (EvWindow         *window);
313 static void     ev_window_stop_presentation             (EvWindow         *window,
314                                                          gboolean          unfullscreen_window);
315 static void     ev_window_cmd_view_presentation         (GtkAction        *action,
316                                                          EvWindow         *window);
317 static void     ev_view_popup_cmd_open_link             (GtkAction        *action,
318                                                          EvWindow         *window);
319 static void     ev_view_popup_cmd_open_link_new_window  (GtkAction        *action,
320                                                          EvWindow         *window);
321 static void     ev_view_popup_cmd_copy_link_address     (GtkAction        *action,
322                                                          EvWindow         *window);
323 static void     ev_view_popup_cmd_save_image_as         (GtkAction        *action,
324                                                          EvWindow         *window);
325 static void     ev_view_popup_cmd_copy_image            (GtkAction        *action,
326                                                          EvWindow         *window);
327 static void     ev_view_popup_cmd_annot_properties      (GtkAction        *action,
328                                                          EvWindow         *window);
329 static void     ev_attachment_popup_cmd_open_attachment (GtkAction        *action,
330                                                          EvWindow         *window);
331 static void     ev_attachment_popup_cmd_save_attachment_as (GtkAction     *action, 
332                                                          EvWindow         *window);
333 static void     ev_window_cmd_view_best_fit             (GtkAction        *action, 
334                                                          EvWindow         *ev_window);
335 static void     ev_window_cmd_view_page_width           (GtkAction        *action, 
336                                                          EvWindow         *ev_window);
337 static void     view_handle_link_cb                     (EvView           *view, 
338                                                          EvLink           *link, 
339                                                          EvWindow         *window);
340 static void     ev_window_update_find_status_message    (EvWindow         *ev_window);
341 static void     ev_window_cmd_edit_find                 (GtkAction        *action,
342                                                          EvWindow         *ev_window);
343 static void     find_bar_search_changed_cb              (EggFindBar       *find_bar,
344                                                          GParamSpec       *param,
345                                                          EvWindow         *ev_window);
346 static void     view_external_link_cb                   (EvWindow         *window,
347                                                          EvLinkAction     *action);
348 static void     ev_window_load_file_remote              (EvWindow         *ev_window,
349                                                          GFile            *source_file);
350 static void     ev_window_media_player_key_pressed      (EvWindow         *window,
351                                                          const gchar      *key,
352                                                          gpointer          user_data);
353 static void     ev_window_update_max_min_scale          (EvWindow         *window);
354 #ifdef ENABLE_DBUS
355 static void     ev_window_emit_closed                   (EvWindow         *window);
356 static void     ev_window_emit_doc_loaded               (EvWindow         *window);
357 #endif
358 static void     ev_window_setup_bookmarks               (EvWindow         *window);
359
360 static guint ev_window_n_copies = 0;
361
362 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
363
364 static gdouble
365 get_screen_dpi (EvWindow *window)
366 {
367         GdkScreen *screen;
368
369         screen = gtk_window_get_screen (GTK_WINDOW (window));
370         return ev_document_misc_get_screen_dpi (screen);
371 }
372
373 static void
374 ev_window_set_action_sensitive (EvWindow   *ev_window,
375                                 const char *name,
376                                 gboolean    sensitive)
377 {
378         GtkAction *action = gtk_action_group_get_action (ev_window->priv->action_group,
379                                                          name);
380         gtk_action_set_sensitive (action, sensitive);
381 }
382
383
384 static void
385 ev_window_setup_action_sensitivity (EvWindow *ev_window)
386 {
387         EvDocument *document = ev_window->priv->document;
388         const EvDocumentInfo *info = NULL;
389         gboolean has_document = FALSE;
390         gboolean ok_to_print = TRUE;
391         gboolean ok_to_copy = TRUE;
392         gboolean has_properties = TRUE;
393         gboolean override_restrictions = TRUE;
394         gboolean can_get_text = FALSE;
395         gboolean has_pages = FALSE;
396         gboolean can_find = FALSE;
397
398         if (document) {
399                 has_document = TRUE;
400                 has_pages = ev_document_get_n_pages (document) > 0;
401                 info = ev_document_get_info (document);
402         }
403
404         if (!info || info->fields_mask == 0) {
405                 has_properties = FALSE;
406         }
407
408         if (has_document && EV_IS_SELECTION (document)) {
409                 can_get_text = TRUE;
410         }
411         
412         if (has_pages && EV_IS_DOCUMENT_FIND (document)) {
413                 can_find = TRUE;
414         }
415
416         if (has_document && ev_window->priv->settings) {
417                 override_restrictions =
418                         g_settings_get_boolean (ev_window->priv->settings,
419                                                 GS_OVERRIDE_RESTRICTIONS);
420         }
421
422         if (!override_restrictions && info && info->fields_mask & EV_DOCUMENT_INFO_PERMISSIONS) {
423                 ok_to_print = (info->permissions & EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT);
424                 ok_to_copy = (info->permissions & EV_DOCUMENT_PERMISSIONS_OK_TO_COPY);
425         }
426
427         if (has_document && !ev_print_operation_exists_for_document(document))
428                 ok_to_print = FALSE;
429
430         if (has_document && ev_window->priv->lockdown_settings &&
431             g_settings_get_boolean (ev_window->priv->lockdown_settings, GS_LOCKDOWN_SAVE)) {
432                 ok_to_copy = FALSE;
433         }
434
435         if (has_document && ev_window->priv->lockdown_settings &&
436             g_settings_get_boolean (ev_window->priv->lockdown_settings, GS_LOCKDOWN_PRINT)) {
437                 ok_to_print = FALSE;
438         }
439
440         /* File menu */
441         ev_window_set_action_sensitive (ev_window, "FileOpenCopy", has_document);
442         ev_window_set_action_sensitive (ev_window, "FileSaveAs", has_document && ok_to_copy);
443         ev_window_set_action_sensitive (ev_window, "FilePrint", has_pages && ok_to_print);
444         ev_window_set_action_sensitive (ev_window, "FileProperties", has_document && has_properties);
445
446         /* Edit menu */
447         ev_window_set_action_sensitive (ev_window, "EditSelectAll", has_pages && can_get_text);
448         ev_window_set_action_sensitive (ev_window, "EditFind", can_find);
449         ev_window_set_action_sensitive (ev_window, "Slash", can_find);
450         ev_window_set_action_sensitive (ev_window, "EditRotateLeft", has_pages);
451         ev_window_set_action_sensitive (ev_window, "EditRotateRight", has_pages);
452
453         /* View menu */
454         ev_window_set_action_sensitive (ev_window, "ViewContinuous", has_pages);
455         ev_window_set_action_sensitive (ev_window, "ViewDual", has_pages);
456         ev_window_set_action_sensitive (ev_window, "ViewBestFit", has_pages);
457         ev_window_set_action_sensitive (ev_window, "ViewPageWidth", has_pages);
458         ev_window_set_action_sensitive (ev_window, "ViewReload", has_pages);
459         ev_window_set_action_sensitive (ev_window, "ViewAutoscroll", has_pages);
460         ev_window_set_action_sensitive (ev_window, "ViewInvertedColors", has_pages);
461
462         /* Bookmarks menu */
463         ev_window_set_action_sensitive (ev_window, "BookmarksAdd",
464                                         has_pages && ev_window->priv->bookmarks);
465
466         /* Toolbar-specific actions: */
467         ev_window_set_action_sensitive (ev_window, PAGE_SELECTOR_ACTION, has_pages);
468         ev_window_set_action_sensitive (ev_window, ZOOM_CONTROL_ACTION,  has_pages);
469         ev_window_set_action_sensitive (ev_window, NAVIGATION_ACTION,  FALSE);
470
471         ev_window_update_actions (ev_window);
472 }
473
474 static void
475 ev_window_update_actions (EvWindow *ev_window)
476 {
477         EvView *view = EV_VIEW (ev_window->priv->view);
478         int n_pages = 0, page = -1;
479         gboolean has_pages = FALSE;
480         gboolean presentation_mode;
481         gboolean can_find_in_page = FALSE;
482         EvSizingMode sizing_mode;
483
484         if (ev_window->priv->document) {
485                 page = ev_document_model_get_page (ev_window->priv->model);
486                 n_pages = ev_document_get_n_pages (ev_window->priv->document);
487                 has_pages = n_pages > 0;
488         }
489
490         can_find_in_page = (ev_window->priv->find_job &&
491                             ev_job_find_has_results (EV_JOB_FIND (ev_window->priv->find_job)));
492
493         ev_window_set_action_sensitive (ev_window, "EditCopy",
494                                         has_pages &&
495                                         ev_view_get_has_selection (view));
496         ev_window_set_action_sensitive (ev_window, "EditFindNext",
497                                         has_pages && can_find_in_page);
498         ev_window_set_action_sensitive (ev_window, "EditFindPrevious",
499                                         has_pages && can_find_in_page);
500         ev_window_set_action_sensitive (ev_window, "F3",
501                                         has_pages && can_find_in_page);
502
503         presentation_mode = EV_WINDOW_IS_PRESENTATION (ev_window);
504         
505         ev_window_set_action_sensitive (ev_window, "ViewZoomIn",
506                                         has_pages &&
507                                         ev_view_can_zoom_in (view) &&
508                                         !presentation_mode);
509         ev_window_set_action_sensitive (ev_window, "ViewZoomOut",
510                                         has_pages &&
511                                         ev_view_can_zoom_out (view) &&
512                                         !presentation_mode);
513         
514         /* Go menu */
515         if (has_pages) {
516                 ev_window_set_action_sensitive (ev_window, "GoPreviousPage", page > 0);
517                 ev_window_set_action_sensitive (ev_window, "GoNextPage", page < n_pages - 1);
518                 ev_window_set_action_sensitive (ev_window, "GoFirstPage", page > 0);
519                 ev_window_set_action_sensitive (ev_window, "GoLastPage", page < n_pages - 1);
520         } else {
521                 ev_window_set_action_sensitive (ev_window, "GoFirstPage", FALSE);
522                 ev_window_set_action_sensitive (ev_window, "GoPreviousPage", FALSE);
523                 ev_window_set_action_sensitive (ev_window, "GoNextPage", FALSE);
524                 ev_window_set_action_sensitive (ev_window, "GoLastPage", FALSE);
525         }
526
527         sizing_mode = ev_document_model_get_sizing_mode (ev_window->priv->model);
528         if (has_pages && sizing_mode != EV_SIZING_FIT_WIDTH && sizing_mode != EV_SIZING_BEST_FIT) {
529                 GtkAction *action;
530                 float      zoom;
531                 float      real_zoom;
532
533                 action = gtk_action_group_get_action (ev_window->priv->action_group,
534                                                       ZOOM_CONTROL_ACTION);
535
536                 real_zoom = ev_document_model_get_scale (ev_window->priv->model);
537                 real_zoom *= 72.0 / get_screen_dpi (ev_window);
538                 zoom = ephy_zoom_get_nearest_zoom_level (real_zoom);
539
540                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), zoom);
541         }
542 }
543
544 static void
545 ev_window_set_view_accels_sensitivity (EvWindow *window, gboolean sensitive)
546 {
547         gboolean can_find;
548
549         can_find = window->priv->document && 
550             EV_IS_DOCUMENT_FIND (window->priv->document);
551
552         if (window->priv->action_group) {
553                 ev_window_set_action_sensitive (window, "PageDown", sensitive);
554                 ev_window_set_action_sensitive (window, "PageUp", sensitive);
555                 ev_window_set_action_sensitive (window, "Space", sensitive);
556                 ev_window_set_action_sensitive (window, "ShiftSpace", sensitive);
557                 ev_window_set_action_sensitive (window, "BackSpace", sensitive);
558                 ev_window_set_action_sensitive (window, "ShiftBackSpace", sensitive);
559                 ev_window_set_action_sensitive (window, "Return", sensitive);
560                 ev_window_set_action_sensitive (window, "ShiftReturn", sensitive);
561                 ev_window_set_action_sensitive (window, "Plus", sensitive);
562                 ev_window_set_action_sensitive (window, "Minus", sensitive);
563                 ev_window_set_action_sensitive (window, "KpPlus", sensitive);
564                 ev_window_set_action_sensitive (window, "KpMinus", sensitive);
565                 ev_window_set_action_sensitive (window, "Equal", sensitive);
566                 ev_window_set_action_sensitive (window, "p", sensitive);
567                 ev_window_set_action_sensitive (window, "n", sensitive);
568
569                 ev_window_set_action_sensitive (window, "Slash", sensitive && can_find);
570         }
571 }
572
573 static void
574 set_widget_visibility (GtkWidget *widget, gboolean visible)
575 {
576         g_assert (GTK_IS_WIDGET (widget));
577         
578         if (visible)
579                 gtk_widget_show (widget);
580         else
581                 gtk_widget_hide (widget);
582 }
583
584 static void
585 update_chrome_visibility (EvWindow *window)
586 {
587         EvWindowPrivate *priv = window->priv;
588         gboolean menubar, toolbar, findbar, fullscreen_toolbar, sidebar;
589         gboolean fullscreen_mode, presentation, fullscreen;
590
591         presentation = EV_WINDOW_IS_PRESENTATION (window);
592         fullscreen = ev_document_model_get_fullscreen (priv->model);
593         fullscreen_mode = fullscreen || presentation;
594
595         menubar = (priv->chrome & EV_CHROME_MENUBAR) != 0 && !fullscreen_mode;
596         toolbar = ((priv->chrome & EV_CHROME_TOOLBAR) != 0  || 
597                    (priv->chrome & EV_CHROME_RAISE_TOOLBAR) != 0) && !fullscreen_mode;
598         fullscreen_toolbar = ((priv->chrome & EV_CHROME_FULLSCREEN_TOOLBAR) != 0 || 
599                               (priv->chrome & EV_CHROME_RAISE_TOOLBAR) != 0) && fullscreen;
600         findbar = (priv->chrome & EV_CHROME_FINDBAR) != 0;
601         sidebar = (priv->chrome & EV_CHROME_SIDEBAR) != 0 && priv->document && !presentation;
602
603         set_widget_visibility (priv->menubar, menubar); 
604         set_widget_visibility (priv->toolbar, toolbar);
605         set_widget_visibility (priv->find_bar, findbar);
606         set_widget_visibility (priv->sidebar, sidebar);
607         
608         ev_window_set_action_sensitive (window, "EditToolbar", toolbar);
609
610         if (priv->fullscreen_toolbar != NULL) {
611                 set_widget_visibility (priv->fullscreen_toolbar, fullscreen_toolbar);
612         }
613 }
614
615 static void
616 update_chrome_flag (EvWindow *window, EvChrome flag, gboolean active)
617 {
618         EvWindowPrivate *priv = window->priv;
619         
620         if (active) {
621                 priv->chrome |= flag;
622         } else {
623                 priv->chrome &= ~flag;
624         }
625 }
626
627 static void
628 update_sizing_buttons (EvWindow *window)
629 {
630         GtkActionGroup *action_group = window->priv->action_group;
631         GtkAction *action;
632         gboolean best_fit, page_width;
633
634         switch (ev_document_model_get_sizing_mode (window->priv->model)) {
635                 case EV_SIZING_BEST_FIT:
636                         best_fit = TRUE;
637                         page_width = FALSE;
638                         break;
639                 case EV_SIZING_FIT_WIDTH:
640                         best_fit = FALSE;
641                         page_width = TRUE;
642                         break;
643                 default:
644                         best_fit = page_width = FALSE;
645                         break;
646         }
647
648         action = gtk_action_group_get_action (action_group, "ViewBestFit");
649         g_signal_handlers_block_by_func
650                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
651         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), best_fit);
652         g_signal_handlers_unblock_by_func
653                 (action, G_CALLBACK (ev_window_cmd_view_best_fit), window);
654
655         action = gtk_action_group_get_action (action_group, "ViewPageWidth");   
656         g_signal_handlers_block_by_func
657                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
658         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), page_width);
659         g_signal_handlers_unblock_by_func
660                 (action, G_CALLBACK (ev_window_cmd_view_page_width), window);
661
662         action = gtk_action_group_get_action (window->priv->action_group, 
663                                               ZOOM_CONTROL_ACTION);     
664         if (best_fit) {
665                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), 
666                                                  EPHY_ZOOM_BEST_FIT);
667         } else if (page_width) {
668                 ephy_zoom_action_set_zoom_level (EPHY_ZOOM_ACTION (action), 
669                                                  EPHY_ZOOM_FIT_WIDTH);
670         }
671 }
672
673 static void
674 update_chrome_actions (EvWindow *window)
675 {
676         EvWindowPrivate *priv = window->priv;
677         GtkActionGroup *action_group = priv->action_group;
678         GtkAction *action;
679
680         action= gtk_action_group_get_action (action_group, "ViewToolbar");
681         g_signal_handlers_block_by_func
682                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
683         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
684                                       (priv->chrome & EV_CHROME_TOOLBAR) != 0);
685         g_signal_handlers_unblock_by_func
686                 (action, G_CALLBACK (ev_window_view_toolbar_cb), window);
687 }
688
689 /**
690  * ev_window_is_empty:
691  * @ev_window: The instance of the #EvWindow.
692  *
693  * It does look if there is any document loaded or if there is any job to load
694  * a document.
695  *
696  * Returns: %TRUE if there isn't any document loaded or any any documente to be
697  *          loaded, %FALSE in other case.
698  */
699 gboolean
700 ev_window_is_empty (const EvWindow *ev_window)
701 {
702         g_return_val_if_fail (EV_IS_WINDOW (ev_window), FALSE);
703
704         return (ev_window->priv->document == NULL) && 
705                 (ev_window->priv->load_job == NULL);
706 }
707
708 static void
709 ev_window_set_message_area (EvWindow  *window,
710                             GtkWidget *area)
711 {
712         if (window->priv->message_area == area)
713                 return;
714
715         if (window->priv->message_area)
716                 gtk_widget_destroy (window->priv->message_area);
717         window->priv->message_area = area;
718
719         if (!area)
720                 return;
721
722         gtk_box_pack_start (GTK_BOX (window->priv->view_box),
723                             window->priv->message_area,
724                             FALSE, FALSE, 0);
725         gtk_box_reorder_child (GTK_BOX (window->priv->view_box),
726                                window->priv->message_area, 0);
727         g_object_add_weak_pointer (G_OBJECT (window->priv->message_area),
728                                    (gpointer) &(window->priv->message_area));
729 }
730
731 static void
732 ev_window_message_area_response_cb (EvMessageArea *area,
733                                     gint           response_id,
734                                     EvWindow      *window)
735 {
736         ev_window_set_message_area (window, NULL);
737 }
738
739 static void
740 ev_window_error_message (EvWindow    *window,
741                          GError      *error,
742                          const gchar *format,
743                          ...)
744 {
745         GtkWidget *area;
746         va_list    args;
747         gchar     *msg = NULL;
748
749         if (window->priv->message_area)
750                 return;
751
752         va_start (args, format);
753         msg = g_strdup_vprintf (format, args);
754         va_end (args);
755         
756         area = ev_message_area_new (GTK_MESSAGE_ERROR,
757                                     msg,
758                                     GTK_STOCK_CLOSE,
759                                     GTK_RESPONSE_CLOSE,
760                                     NULL);
761         g_free (msg);
762         
763         if (error)
764                 ev_message_area_set_secondary_text (EV_MESSAGE_AREA (area), error->message);
765         g_signal_connect (area, "response",
766                           G_CALLBACK (ev_window_message_area_response_cb),
767                           window);
768         gtk_widget_show (area);
769         ev_window_set_message_area (window, area);
770 }
771
772 static void
773 ev_window_warning_message (EvWindow    *window,
774                            const gchar *format,
775                            ...)
776 {
777         GtkWidget *area;
778         va_list    args;
779         gchar     *msg = NULL;
780
781         if (window->priv->message_area)
782                 return;
783
784         va_start (args, format);
785         msg = g_strdup_vprintf (format, args);
786         va_end (args);
787
788         area = ev_message_area_new (GTK_MESSAGE_WARNING,
789                                     msg,
790                                     GTK_STOCK_CLOSE,
791                                     GTK_RESPONSE_CLOSE,
792                                     NULL);
793         g_free (msg);
794         
795         g_signal_connect (area, "response",
796                           G_CALLBACK (ev_window_message_area_response_cb),
797                           window);
798         gtk_widget_show (area);
799         ev_window_set_message_area (window, area);
800 }
801
802 typedef struct _PageTitleData {
803         const gchar *page_label;
804         gchar       *page_title;
805 } PageTitleData;
806
807 static gboolean
808 ev_window_find_page_title (GtkTreeModel  *tree_model,
809                            GtkTreePath   *path,
810                            GtkTreeIter   *iter,
811                            PageTitleData *data)
812 {
813         gchar *page_string;
814         
815         gtk_tree_model_get (tree_model, iter,
816                             EV_DOCUMENT_LINKS_COLUMN_PAGE_LABEL, &page_string, 
817                             -1);
818         
819         if (!page_string)
820                 return FALSE;
821         
822         if (!strcmp (page_string, data->page_label)) {
823                 gtk_tree_model_get (tree_model, iter,
824                                     EV_DOCUMENT_LINKS_COLUMN_MARKUP, &data->page_title, 
825                                     -1);
826                 g_free (page_string);
827                 return TRUE;
828         }
829         
830         g_free (page_string);
831         return FALSE;
832 }
833
834 static gchar *
835 ev_window_get_page_title (EvWindow    *window,
836                           const gchar *page_label)
837 {
838         if (EV_IS_DOCUMENT_LINKS (window->priv->document) &&
839             ev_document_links_has_document_links (EV_DOCUMENT_LINKS (window->priv->document))) {
840                 PageTitleData data;
841                 GtkTreeModel *model;
842
843                 data.page_label = page_label;
844                 data.page_title = NULL;
845
846                 g_object_get (G_OBJECT (window->priv->sidebar_links),
847                               "model", &model,
848                               NULL);
849                 if (model) {
850                         gtk_tree_model_foreach (model,
851                                                 (GtkTreeModelForeachFunc)ev_window_find_page_title,
852                                                 &data);
853
854                         g_object_unref (model);
855                 }
856
857                 return data.page_title;
858         }
859
860         return NULL;
861 }
862
863 static void
864 ev_window_add_history (EvWindow *window, gint page, EvLink *link)
865 {
866         gchar *page_label = NULL;
867         gchar *page_title;
868         gchar *link_title;
869         EvLink *real_link;
870         EvLinkAction *action;
871         EvLinkDest *dest;
872         
873         if (window->priv->history == NULL)
874                 return;
875
876         if (!EV_IS_DOCUMENT_LINKS (window->priv->document))
877                 return;
878         
879         if (link) {
880                 action = g_object_ref (ev_link_get_action (link));
881                 dest = ev_link_action_get_dest (action);
882                 page_label = ev_document_links_get_dest_page_label (EV_DOCUMENT_LINKS (window->priv->document), dest);
883         } else {
884                 dest = ev_link_dest_new_page (page);
885                 action = ev_link_action_new_dest (dest);
886                 page_label = ev_document_get_page_label (window->priv->document, page);
887         }
888
889         if (!page_label)
890                 return;
891
892         page_title = ev_window_get_page_title (window, page_label);
893         if (page_title) {
894                 link_title = g_strdup_printf (_("Page %s — %s"), page_label, page_title);
895                 g_free (page_title);
896         } else {
897                 link_title = g_strdup_printf (_("Page %s"), page_label);
898         }
899
900         real_link = ev_link_new (link_title, action);
901         
902         ev_history_add_link (window->priv->history, real_link);
903
904         g_free (link_title);
905         g_free (page_label);
906         g_object_unref (real_link);
907 }
908
909 static void
910 view_handle_link_cb (EvView *view, EvLink *link, EvWindow *window)
911 {
912         int current_page = ev_document_model_get_page (window->priv->model);
913         
914         ev_window_add_history (window, 0, link);
915         ev_window_add_history (window, current_page, NULL);
916 }
917
918 static void
919 view_selection_changed_cb (EvView   *view,
920                            EvWindow *window)
921 {
922         ev_window_set_action_sensitive (window, "EditCopy",
923                                         ev_view_get_has_selection (view));
924 }
925
926 static void
927 view_layers_changed_cb (EvView   *view,
928                         EvWindow *window)
929 {
930         ev_sidebar_layers_update_layers_state (EV_SIDEBAR_LAYERS (window->priv->sidebar_layers));
931 }
932
933 static void
934 ev_window_page_changed_cb (EvWindow        *ev_window,
935                            gint             old_page,
936                            gint             new_page,
937                            EvDocumentModel *model)
938 {
939         ev_window_update_actions (ev_window);
940
941         ev_window_update_find_status_message (ev_window);
942
943         if (abs (new_page - old_page) > 1) {
944                 ev_window_add_history (ev_window, new_page, NULL);
945                 ev_window_add_history (ev_window, old_page, NULL);
946         }
947
948         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window))
949                 ev_metadata_set_int (ev_window->priv->metadata, "page", new_page);
950 }
951
952 static const gchar *
953 ev_window_sidebar_get_current_page_id (EvWindow *ev_window)
954 {
955         GtkWidget   *current_page;
956         const gchar *id;
957
958         g_object_get (ev_window->priv->sidebar,
959                       "current_page", &current_page,
960                       NULL);
961
962         if (current_page == ev_window->priv->sidebar_links) {
963                 id = LINKS_SIDEBAR_ID;
964         } else if (current_page == ev_window->priv->sidebar_thumbs) {
965                 id = THUMBNAILS_SIDEBAR_ID;
966         } else if (current_page == ev_window->priv->sidebar_attachments) {
967                 id = ATTACHMENTS_SIDEBAR_ID;
968         } else if (current_page == ev_window->priv->sidebar_layers) {
969                 id = LAYERS_SIDEBAR_ID;
970         } else if (current_page == ev_window->priv->sidebar_annots) {
971                 id = ANNOTS_SIDEBAR_ID;
972         } else if (current_page == ev_window->priv->sidebar_bookmarks) {
973                 id = BOOKMARKS_SIDEBAR_ID;
974         } else {
975                 g_assert_not_reached();
976         }
977
978         g_object_unref (current_page);
979
980         return id;
981 }
982
983 static void
984 ev_window_sidebar_set_current_page (EvWindow    *window,
985                                     const gchar *page_id)
986 {
987         EvDocument *document = window->priv->document;
988         EvSidebar  *sidebar = EV_SIDEBAR (window->priv->sidebar);
989         GtkWidget  *links = window->priv->sidebar_links;
990         GtkWidget  *thumbs = window->priv->sidebar_thumbs;
991         GtkWidget  *attachments = window->priv->sidebar_attachments;
992         GtkWidget  *annots = window->priv->sidebar_annots;
993         GtkWidget  *layers = window->priv->sidebar_layers;
994         GtkWidget  *bookmarks = window->priv->sidebar_bookmarks;
995
996         if (strcmp (page_id, LINKS_SIDEBAR_ID) == 0 &&
997             ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (links), document)) {
998                 ev_sidebar_set_page (sidebar, links);
999         } else if (strcmp (page_id, THUMBNAILS_SIDEBAR_ID) == 0 &&
1000                    ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (thumbs), document)) {
1001                 ev_sidebar_set_page (sidebar, thumbs);
1002         } else if (strcmp (page_id, ATTACHMENTS_SIDEBAR_ID) == 0 &&
1003                    ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (attachments), document)) {
1004                 ev_sidebar_set_page (sidebar, attachments);
1005         } else if (strcmp (page_id, LAYERS_SIDEBAR_ID) == 0 &&
1006                    ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (layers), document)) {
1007                 ev_sidebar_set_page (sidebar, layers);
1008         } else if (strcmp (page_id, ANNOTS_SIDEBAR_ID) == 0 &&
1009                    ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (annots), document)) {
1010                 ev_sidebar_set_page (sidebar, annots);
1011         } else if (strcmp (page_id, BOOKMARKS_SIDEBAR_ID) == 0 &&
1012                    ev_sidebar_page_support_document (EV_SIDEBAR_PAGE (bookmarks), document)) {
1013                 ev_sidebar_set_page (sidebar, bookmarks);
1014         }
1015 }
1016
1017 static void
1018 update_document_mode (EvWindow *window, EvDocumentMode mode)
1019 {
1020         if (mode == EV_DOCUMENT_MODE_PRESENTATION) {
1021                 ev_window_run_presentation (window);
1022         }
1023         else if (mode == EV_DOCUMENT_MODE_FULL_SCREEN) {
1024                 ev_window_run_fullscreen (window);
1025         }
1026 }
1027
1028 static void
1029 setup_chrome_from_metadata (EvWindow *window)
1030 {
1031         EvChrome chrome = EV_CHROME_NORMAL;
1032         gboolean show_toolbar = TRUE;
1033
1034         if (ev_window_is_empty (window)) {
1035                 window->priv->chrome = chrome;
1036
1037                 return;
1038         }
1039
1040         if (!window->priv->metadata) {
1041                 show_toolbar = g_settings_get_boolean (window->priv->default_settings, "show-toolbar");
1042         } else if (!ev_metadata_get_boolean (window->priv->metadata, "show_toolbar", &show_toolbar)) {
1043                 if (window->priv->is_new_doc)
1044                         show_toolbar = g_settings_get_boolean (window->priv->default_settings, "show-toolbar");
1045         }
1046
1047         if (!show_toolbar)
1048                 chrome &= ~EV_CHROME_TOOLBAR;
1049
1050         window->priv->chrome = chrome;
1051 }
1052
1053 static void
1054 setup_sidebar_from_metadata (EvWindow *window)
1055 {
1056         EvDocument *document = window->priv->document;
1057         GSettings  *settings =  window->priv->default_settings;
1058         gchar      *page_id;
1059         gint        sidebar_size;
1060         gboolean    sidebar_visibility = TRUE;
1061
1062         if (ev_window_is_empty (window))
1063                 return;
1064
1065         if (!window->priv->metadata) {
1066                 sidebar_visibility = g_settings_get_boolean (settings, "show-sidebar");
1067         } else if (!ev_metadata_get_boolean (window->priv->metadata, "sidebar_visibility", &sidebar_visibility)) {
1068                 if (window->priv->is_new_doc)
1069                         sidebar_visibility = g_settings_get_boolean (settings, "show-sidebar");
1070         }
1071
1072         update_chrome_flag (window, EV_CHROME_SIDEBAR, sidebar_visibility);
1073         update_chrome_visibility (window);
1074
1075         if (!window->priv->metadata) {
1076                 /* Set default values */
1077                 gtk_paned_set_position (GTK_PANED (window->priv->hpaned),
1078                                         g_settings_get_int (settings, "sidebar-size"));
1079                 if (document) {
1080                         page_id = g_settings_get_string (settings, "sidebar-page");
1081                         ev_window_sidebar_set_current_page (window, page_id);
1082                         g_free (page_id);
1083                 }
1084
1085                 return;
1086         }
1087
1088         if (ev_metadata_get_int (window->priv->metadata, "sidebar_size", &sidebar_size)) {
1089                 gtk_paned_set_position (GTK_PANED (window->priv->hpaned), sidebar_size);
1090         } else if (window->priv->is_new_doc) {
1091                 gtk_paned_set_position (GTK_PANED (window->priv->hpaned),
1092                                         g_settings_get_int (settings, "sidebar-size"));
1093         }
1094
1095         if (!document)
1096                 return;
1097
1098         if (ev_metadata_get_string (window->priv->metadata, "sidebar_page", &page_id)) {
1099                 ev_window_sidebar_set_current_page (window, page_id);
1100         } else if (window->priv->is_new_doc) {
1101                 page_id = g_settings_get_string (settings, "sidebar-page");
1102                 ev_window_sidebar_set_current_page (window, page_id);
1103                 g_free (page_id);
1104         }
1105 }
1106
1107 static void
1108 setup_model_from_metadata (EvWindow *window)
1109 {
1110         GSettings *settings = window->priv->default_settings;
1111         gint       page;
1112         gchar     *sizing_mode;
1113         gdouble    zoom;
1114         gint       rotation;
1115         gboolean   inverted_colors = FALSE;
1116         gboolean   continuous = FALSE;
1117         gboolean   dual_page = FALSE;
1118         gboolean   fullscreen = FALSE;
1119
1120         if (!window->priv->metadata) {
1121                 /* Set default values */
1122                 ev_document_model_set_sizing_mode (window->priv->model,
1123                                                    g_settings_get_enum (settings, "sizing-mode"));
1124                 ev_document_model_set_inverted_colors (window->priv->model,
1125                                                        g_settings_get_boolean (settings, "inverted-colors"));
1126                 ev_document_model_set_continuous (window->priv->model,
1127                                                   g_settings_get_boolean (settings, "continuous"));
1128                 ev_document_model_set_dual_page (window->priv->model,
1129                                                  g_settings_get_boolean (settings, "dual-page"));
1130                 fullscreen = g_settings_get_boolean (settings, "fullscreen");
1131                 if (fullscreen)
1132                         ev_window_run_fullscreen (window);
1133
1134                 return;
1135         }
1136
1137         /* Current page */
1138         if (!window->priv->dest &&
1139             ev_metadata_get_int (window->priv->metadata, "page", &page)) {
1140                 ev_document_model_set_page (window->priv->model, page);
1141         }
1142
1143         /* Sizing mode */
1144         if (ev_metadata_get_string (window->priv->metadata, "sizing_mode", &sizing_mode)) {
1145                 GEnumValue *enum_value;
1146
1147                 enum_value = g_enum_get_value_by_nick
1148                         (g_type_class_peek (EV_TYPE_SIZING_MODE), sizing_mode);
1149                 ev_document_model_set_sizing_mode (window->priv->model, enum_value->value);
1150         } else if (window->priv->is_new_doc) {
1151                 ev_document_model_set_sizing_mode (window->priv->model,
1152                                                    g_settings_get_enum (settings, "sizing-mode"));
1153         }
1154
1155         /* Zoom */
1156         if (ev_document_model_get_sizing_mode (window->priv->model) == EV_SIZING_FREE &&
1157             ev_metadata_get_double (window->priv->metadata, "zoom", &zoom)) {
1158                 zoom *= get_screen_dpi (window) / 72.0;
1159                 ev_document_model_set_scale (window->priv->model, zoom);
1160         }
1161
1162         /* Rotation */
1163         if (ev_metadata_get_int (window->priv->metadata, "rotation", &rotation)) {
1164                 switch (rotation) {
1165                 case 90:
1166                         rotation = 90;
1167                         break;
1168                 case 180:
1169                         rotation = 180;
1170                         break;
1171                 case 270:
1172                         rotation = 270;
1173                         break;
1174                 default:
1175                         rotation = 0;
1176                         break;
1177                 }
1178                 ev_document_model_set_rotation (window->priv->model, rotation);
1179         }
1180
1181         /* Inverted Colors */
1182         if (ev_metadata_get_boolean (window->priv->metadata, "inverted-colors", &inverted_colors)) {
1183                 ev_document_model_set_inverted_colors (window->priv->model, inverted_colors);
1184         } else if (window->priv->is_new_doc) {
1185                 ev_document_model_set_inverted_colors (window->priv->model,
1186                                                        g_settings_get_boolean (settings, "inverted-colors"));
1187         }
1188
1189         /* Continuous */
1190         if (ev_metadata_get_boolean (window->priv->metadata, "continuous", &continuous)) {
1191                 ev_document_model_set_continuous (window->priv->model, continuous);
1192         } else if (window->priv->is_new_doc) {
1193                 ev_document_model_set_continuous (window->priv->model,
1194                                                   g_settings_get_boolean (settings, "continuous"));
1195         }
1196
1197         /* Dual page */
1198         if (ev_metadata_get_boolean (window->priv->metadata, "dual-page", &dual_page)) {
1199                 ev_document_model_set_dual_page (window->priv->model, dual_page);
1200         } else if (window->priv->is_new_doc) {
1201                 ev_document_model_set_dual_page (window->priv->model,
1202                                                  g_settings_get_boolean (settings, "dual-page"));
1203         }
1204
1205         /* Fullscreen */
1206         if (!ev_metadata_get_boolean (window->priv->metadata, "fullscreen", &fullscreen)) {
1207                 if (window->priv->is_new_doc)
1208                         fullscreen = g_settings_get_boolean (settings, "fullscreen");
1209         }
1210
1211         if (fullscreen)
1212                 ev_window_run_fullscreen (window);
1213 }
1214
1215 static void
1216 setup_document_from_metadata (EvWindow *window)
1217 {
1218         gint    page, n_pages;
1219         gint    width;
1220         gint    height;
1221         gdouble width_ratio;
1222         gdouble height_ratio;
1223
1224         setup_sidebar_from_metadata (window);
1225
1226         if (window->priv->metadata) {
1227                 /* Make sure to not open a document on the last page,
1228                  * since closing it on the last page most likely means the
1229                  * user was finished reading the document. In that case, reopening should
1230                  * show the first page. */
1231                 page = ev_document_model_get_page (window->priv->model);
1232                 n_pages = ev_document_get_n_pages (window->priv->document);
1233                 if (page == n_pages - 1)
1234                         ev_document_model_set_page (window->priv->model, 0);
1235
1236                 if (ev_metadata_get_int (window->priv->metadata, "window_width", &width) &&
1237                     ev_metadata_get_int (window->priv->metadata, "window_height", &height))
1238                         return; /* size was already set in setup_size_from_metadata */
1239         }
1240
1241         g_settings_get (window->priv->default_settings, "window-ratio", "(dd)", &width_ratio, &height_ratio);
1242         if (width_ratio > 0. && height_ratio > 0.) {
1243                 gdouble    document_width;
1244                 gdouble    document_height;
1245                 GdkScreen *screen;
1246                 gint       request_width;
1247                 gint       request_height;
1248
1249                 ev_document_get_max_page_size (window->priv->document,
1250                                                &document_width, &document_height);
1251
1252                 request_width = (gint)(width_ratio * document_width + 0.5);
1253                 request_height = (gint)(height_ratio * document_height + 0.5);
1254
1255                 screen = gtk_window_get_screen (GTK_WINDOW (window));
1256                 if (screen) {
1257                         request_width = MIN (request_width, gdk_screen_get_width (screen));
1258                         request_height = MIN (request_height, gdk_screen_get_height (screen));
1259                 }
1260
1261                 if (request_width > 0 && request_height > 0) {
1262                         gtk_window_resize (GTK_WINDOW (window),
1263                                            request_width,
1264                                            request_height);
1265                 }
1266         }
1267 }
1268
1269 static void
1270 setup_size_from_metadata (EvWindow *window)
1271 {
1272         gint     width;
1273         gint     height;
1274         gboolean maximized;
1275         gint     x;
1276         gint     y;
1277
1278         if (!window->priv->metadata)
1279                 return;
1280
1281         if (ev_metadata_get_boolean (window->priv->metadata, "window_maximized", &maximized)) {
1282                 if (maximized) {
1283                         gtk_window_maximize (GTK_WINDOW (window));
1284                         return;
1285                 } else {
1286                         gtk_window_unmaximize (GTK_WINDOW (window));
1287                 }
1288         }
1289
1290         if (ev_metadata_get_int (window->priv->metadata, "window_x", &x) &&
1291             ev_metadata_get_int (window->priv->metadata, "window_y", &y)) {
1292                 gtk_window_move (GTK_WINDOW (window), x, y);
1293         }
1294
1295         if (ev_metadata_get_int (window->priv->metadata, "window_width", &width) &&
1296             ev_metadata_get_int (window->priv->metadata, "window_height", &height)) {
1297                 gtk_window_resize (GTK_WINDOW (window), width, height);
1298         }
1299 }
1300
1301 static void
1302 setup_view_from_metadata (EvWindow *window)
1303 {
1304         gboolean presentation;
1305
1306         if (!window->priv->metadata)
1307                 return;
1308
1309         /* Presentation */
1310         if (ev_metadata_get_boolean (window->priv->metadata, "presentation", &presentation)) {
1311                 if (presentation) {
1312                         ev_window_run_presentation (window);
1313                 }
1314         }
1315 }
1316
1317 static void
1318 ev_window_clear_thumbnail_job (EvWindow *ev_window)
1319 {
1320         if (ev_window->priv->thumbnail_job != NULL) {
1321                 if (!ev_job_is_finished (ev_window->priv->thumbnail_job))
1322                         ev_job_cancel (ev_window->priv->thumbnail_job);
1323                 
1324                 g_signal_handlers_disconnect_by_func (ev_window->priv->thumbnail_job,
1325                                                       ev_window_set_icon_from_thumbnail,
1326                                                       ev_window);
1327                 g_object_unref (ev_window->priv->thumbnail_job);
1328                 ev_window->priv->thumbnail_job = NULL;
1329         }
1330 }
1331
1332 static void
1333 ev_window_set_icon_from_thumbnail (EvJobThumbnail *job,
1334                                    EvWindow       *ev_window)
1335 {
1336         if (job->thumbnail) {
1337                 if (ev_document_model_get_inverted_colors (ev_window->priv->model))
1338                         ev_document_misc_invert_pixbuf (job->thumbnail);
1339                 gtk_window_set_icon (GTK_WINDOW (ev_window),
1340                                      job->thumbnail);
1341         }
1342
1343         ev_window_clear_thumbnail_job (ev_window);
1344 }
1345
1346 static void
1347 ev_window_refresh_window_thumbnail (EvWindow *ev_window)
1348 {
1349         gdouble page_width;
1350         gdouble scale;
1351         gint rotation;
1352         EvDocument *document = ev_window->priv->document;
1353
1354         if (ev_document_get_n_pages (document) <= 0 ||
1355             !ev_document_check_dimensions (document)) {
1356                 return;
1357         }
1358
1359         ev_window_clear_thumbnail_job (ev_window);
1360
1361         ev_document_get_page_size (document, 0, &page_width, NULL);
1362         scale = 128. / page_width;
1363         rotation = ev_document_model_get_rotation (ev_window->priv->model);
1364
1365         ev_window->priv->thumbnail_job = ev_job_thumbnail_new (document, 0, rotation, scale);
1366         g_signal_connect (ev_window->priv->thumbnail_job, "finished",
1367                           G_CALLBACK (ev_window_set_icon_from_thumbnail),
1368                           ev_window);
1369         ev_job_scheduler_push_job (ev_window->priv->thumbnail_job, EV_JOB_PRIORITY_NONE);
1370 }
1371
1372 static void
1373 override_restrictions_changed (GSettings *settings,
1374                                gchar     *key,
1375                                EvWindow  *ev_window)
1376 {
1377         ev_window_setup_action_sensitivity (ev_window);
1378 }
1379
1380 static void
1381 lockdown_changed (GSettings   *lockdown,
1382                   const gchar *key,
1383                   EvWindow    *ev_window)
1384 {
1385         ev_window_setup_action_sensitivity (ev_window);
1386 }
1387
1388 static gboolean
1389 ev_window_setup_document (EvWindow *ev_window)
1390 {
1391         const EvDocumentInfo *info;
1392         EvDocument *document = ev_window->priv->document;
1393         GtkAction *action;
1394
1395         ev_window->priv->setup_document_idle = 0;
1396
1397         ev_window_refresh_window_thumbnail (ev_window);
1398
1399         ev_window_set_page_mode (ev_window, PAGE_MODE_DOCUMENT);
1400         ev_window_title_set_document (ev_window->priv->title, document);
1401         ev_window_title_set_uri (ev_window->priv->title, ev_window->priv->uri);
1402
1403         if (!ev_window->priv->settings) {
1404                 ev_window->priv->settings = g_settings_new (GS_SCHEMA_NAME);
1405                 g_signal_connect (ev_window->priv->settings,
1406                                   "changed::"GS_OVERRIDE_RESTRICTIONS,
1407                                   G_CALLBACK (override_restrictions_changed),
1408                                   ev_window);
1409         }
1410
1411 #ifdef HAVE_DESKTOP_SCHEMAS
1412         if (!ev_window->priv->lockdown_settings) {
1413                 ev_window->priv->lockdown_settings = g_settings_new (GS_LOCKDOWN_SCHEMA_NAME);
1414                 g_signal_connect (ev_window->priv->lockdown_settings,
1415                                   "changed",
1416                                   G_CALLBACK (lockdown_changed),
1417                                   ev_window);
1418         }
1419 #endif
1420
1421         ev_window_setup_action_sensitivity (ev_window);
1422
1423         if (ev_window->priv->history)
1424                 g_object_unref (ev_window->priv->history);
1425         ev_window->priv->history = ev_history_new ();
1426         action = gtk_action_group_get_action (ev_window->priv->action_group, NAVIGATION_ACTION);
1427         ev_navigation_action_set_history (EV_NAVIGATION_ACTION (action), ev_window->priv->history);
1428         
1429         if (ev_window->priv->properties) {
1430                 ev_properties_dialog_set_document (EV_PROPERTIES_DIALOG (ev_window->priv->properties),
1431                                                    ev_window->priv->uri,
1432                                                    ev_window->priv->document);
1433         }
1434         
1435         info = ev_document_get_info (document);
1436         update_document_mode (ev_window, info->mode);
1437
1438         if (EV_WINDOW_IS_PRESENTATION (ev_window))
1439                 gtk_widget_grab_focus (ev_window->priv->presentation_view);
1440         else
1441                 gtk_widget_grab_focus (ev_window->priv->view);
1442
1443         return FALSE;
1444 }
1445
1446 static void
1447 ev_window_set_document (EvWindow *ev_window, EvDocument *document)
1448 {
1449         if (ev_window->priv->document == document)
1450                 return;
1451
1452         if (ev_window->priv->document)
1453                 g_object_unref (ev_window->priv->document);
1454         ev_window->priv->document = g_object_ref (document);
1455
1456         ev_window_update_max_min_scale (ev_window);
1457
1458         ev_window_set_message_area (ev_window, NULL);
1459
1460         if (ev_document_get_n_pages (document) <= 0) {
1461                 ev_window_warning_message (ev_window, "%s",
1462                                            _("The document contains no pages"));
1463         } else if (!ev_document_check_dimensions (document)) {
1464                 ev_window_warning_message (ev_window, "%s",
1465                                            _("The document contains only empty pages"));
1466         }
1467
1468         if (EV_WINDOW_IS_PRESENTATION (ev_window)) {
1469                 gtk_widget_destroy (ev_window->priv->presentation_view);
1470                 ev_window->priv->presentation_view = NULL;
1471                 ev_window_run_presentation (ev_window);
1472         }
1473
1474         if (ev_window->priv->setup_document_idle > 0)
1475                 g_source_remove (ev_window->priv->setup_document_idle);
1476
1477         ev_window->priv->setup_document_idle = g_idle_add ((GSourceFunc)ev_window_setup_document, ev_window);
1478 }
1479
1480 static void
1481 ev_window_document_changed (EvWindow *ev_window,
1482                             gpointer  user_data)
1483 {
1484         ev_window_reload_document (ev_window, NULL);
1485 }
1486
1487 static void
1488 ev_window_password_view_unlock (EvWindow *ev_window)
1489 {
1490         const gchar *password;
1491         
1492         g_assert (ev_window->priv->load_job);
1493
1494         password = ev_password_view_get_password (EV_PASSWORD_VIEW (ev_window->priv->password_view));
1495         ev_job_load_set_password (EV_JOB_LOAD (ev_window->priv->load_job), password);
1496         ev_job_scheduler_push_job (ev_window->priv->load_job, EV_JOB_PRIORITY_NONE);
1497 }
1498
1499 static void
1500 ev_window_clear_load_job (EvWindow *ev_window)
1501 {
1502         if (ev_window->priv->load_job != NULL) {
1503                 if (!ev_job_is_finished (ev_window->priv->load_job))
1504                         ev_job_cancel (ev_window->priv->load_job);
1505                 
1506                 g_signal_handlers_disconnect_by_func (ev_window->priv->load_job, ev_window_load_job_cb, ev_window);
1507                 g_object_unref (ev_window->priv->load_job);
1508                 ev_window->priv->load_job = NULL;
1509         }
1510 }
1511
1512 static void
1513 ev_window_clear_reload_job (EvWindow *ev_window)
1514 {
1515         if (ev_window->priv->reload_job != NULL) {
1516                 if (!ev_job_is_finished (ev_window->priv->reload_job))
1517                         ev_job_cancel (ev_window->priv->reload_job);
1518                 
1519                 g_signal_handlers_disconnect_by_func (ev_window->priv->reload_job, ev_window_reload_job_cb, ev_window);
1520                 g_object_unref (ev_window->priv->reload_job);
1521                 ev_window->priv->reload_job = NULL;
1522         }
1523 }
1524
1525 static void
1526 ev_window_clear_local_uri (EvWindow *ev_window)
1527 {
1528         if (ev_window->priv->local_uri) {
1529                 ev_tmp_uri_unlink (ev_window->priv->local_uri);
1530                 g_free (ev_window->priv->local_uri);
1531                 ev_window->priv->local_uri = NULL;
1532         }
1533 }
1534
1535 static void
1536 ev_window_handle_link (EvWindow *ev_window,
1537                        EvLinkDest *dest)
1538 {
1539         if (dest) {
1540                 EvLink *link;
1541                 EvLinkAction *link_action;
1542
1543                 link_action = ev_link_action_new_dest (dest);
1544                 link = ev_link_new (NULL, link_action);
1545                 ev_view_handle_link (EV_VIEW (ev_window->priv->view), link);
1546                 g_object_unref (link);
1547         }
1548 }
1549
1550 /* This callback will executed when load job will be finished.
1551  *
1552  * Since the flow of the error dialog is very confusing, we assume that both
1553  * document and uri will go away after this function is called, and thus we need
1554  * to ref/dup them.  Additionally, it needs to clear
1555  * ev_window->priv->password_{uri,document}, and thus people who call this
1556  * function should _not_ necessarily expect those to exist after being
1557  * called. */
1558 static void
1559 ev_window_load_job_cb (EvJob *job,
1560                        gpointer data)
1561 {
1562         EvWindow *ev_window = EV_WINDOW (data);
1563         EvDocument *document = EV_JOB (job)->document;
1564         EvJobLoad *job_load = EV_JOB_LOAD (job);
1565
1566         g_assert (job_load->uri);
1567
1568         ev_view_set_loading (EV_VIEW (ev_window->priv->view), FALSE);
1569
1570         /* Success! */
1571         if (!ev_job_is_failed (job)) {
1572                 ev_document_model_set_document (ev_window->priv->model, document);
1573
1574 #ifdef ENABLE_DBUS
1575                 ev_window_emit_doc_loaded (ev_window);
1576 #endif
1577                 setup_chrome_from_metadata (ev_window);
1578                 update_chrome_actions (ev_window);
1579                 setup_document_from_metadata (ev_window);
1580                 setup_view_from_metadata (ev_window);
1581
1582                 ev_window_add_recent (ev_window, ev_window->priv->uri);
1583
1584                 ev_window_title_set_type (ev_window->priv->title,
1585                                           EV_WINDOW_TITLE_DOCUMENT);
1586                 if (job_load->password) {
1587                         GPasswordSave flags;
1588
1589                         flags = ev_password_view_get_password_save_flags (
1590                                 EV_PASSWORD_VIEW (ev_window->priv->password_view));
1591                         ev_keyring_save_password (ev_window->priv->uri,
1592                                                   job_load->password,
1593                                                   flags);
1594                 }
1595
1596                 ev_window_handle_link (ev_window, ev_window->priv->dest);
1597                 /* Already unrefed by ev_link_action
1598                  * FIXME: link action should inc dest ref counting
1599                  * or not unref it at all
1600                  */
1601                 ev_window->priv->dest = NULL;
1602
1603                 switch (ev_window->priv->window_mode) {
1604                         case EV_WINDOW_MODE_FULLSCREEN:
1605                                 ev_window_run_fullscreen (ev_window);
1606                                 break;
1607                         case EV_WINDOW_MODE_PRESENTATION:
1608                                 ev_window_run_presentation (ev_window);
1609                                 break;
1610                         default:
1611                                 break;
1612                 }
1613
1614                 if (ev_window->priv->search_string && EV_IS_DOCUMENT_FIND (document)) {
1615                         ev_window_cmd_edit_find (NULL, ev_window);
1616                         egg_find_bar_set_search_string (EGG_FIND_BAR (ev_window->priv->find_bar),
1617                                                         ev_window->priv->search_string);
1618                 }
1619
1620                 g_free (ev_window->priv->search_string);
1621                 ev_window->priv->search_string = NULL;
1622
1623                 /* Create a monitor for the document */
1624                 ev_window->priv->monitor = ev_file_monitor_new (ev_window->priv->uri);
1625                 g_signal_connect_swapped (ev_window->priv->monitor, "changed",
1626                                           G_CALLBACK (ev_window_document_changed),
1627                                           ev_window);
1628                 
1629                 ev_window_clear_load_job (ev_window);
1630                 return;
1631         }
1632
1633         if (g_error_matches (job->error, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
1634                 gchar *password;
1635                 
1636                 setup_view_from_metadata (ev_window);
1637                 
1638                 /* First look whether password is in keyring */
1639                 password = ev_keyring_lookup_password (ev_window->priv->uri);
1640                 if (password) {
1641                         if (job_load->password && strcmp (password, job_load->password) == 0) {
1642                                 /* Password in kering is wrong */
1643                                 ev_job_load_set_password (job_load, NULL);
1644                                 /* FIXME: delete password from keyring? */
1645                         } else {
1646                                 ev_job_load_set_password (job_load, password);
1647                                 ev_job_scheduler_push_job (job, EV_JOB_PRIORITY_NONE);
1648                                 g_free (password);
1649                                 return;
1650                         }
1651
1652                         g_free (password);
1653                 }
1654
1655                 /* We need to ask the user for a password */
1656                 ev_window_title_set_uri (ev_window->priv->title,
1657                                          ev_window->priv->uri);
1658                 ev_window_title_set_type (ev_window->priv->title,
1659                                           EV_WINDOW_TITLE_PASSWORD);
1660
1661                 ev_password_view_set_uri (EV_PASSWORD_VIEW (ev_window->priv->password_view),
1662                                           job_load->uri);
1663
1664                 ev_window_set_page_mode (ev_window, PAGE_MODE_PASSWORD);
1665
1666                 ev_job_load_set_password (job_load, NULL);
1667                 ev_password_view_ask_password (EV_PASSWORD_VIEW (ev_window->priv->password_view));
1668         } else {
1669                 ev_window_error_message (ev_window, job->error, 
1670                                          "%s", _("Unable to open document"));
1671                 ev_window_clear_load_job (ev_window);
1672         }       
1673 }
1674
1675 static void
1676 ev_window_reload_job_cb (EvJob    *job,
1677                          EvWindow *ev_window)
1678 {
1679         GtkWidget *widget;
1680
1681         if (ev_job_is_failed (job)) {
1682                 ev_window_clear_reload_job (ev_window);
1683                 ev_window->priv->in_reload = FALSE;
1684                 if (ev_window->priv->dest) {
1685                         g_object_unref (ev_window->priv->dest);
1686                         ev_window->priv->dest = NULL;
1687                 }
1688
1689                 return;
1690         }
1691
1692         ev_document_model_set_document (ev_window->priv->model,
1693                                         job->document);
1694         if (ev_window->priv->dest) {
1695                 ev_window_handle_link (ev_window, ev_window->priv->dest);
1696                 /* Already unrefed by ev_link_action
1697                  * FIXME: link action should inc dest ref counting
1698                  * or not unref it at all
1699                  */
1700                 ev_window->priv->dest = NULL;
1701         }
1702
1703         /* Restart the search after reloading */
1704         widget = gtk_window_get_focus (GTK_WINDOW (ev_window));
1705         if (widget && gtk_widget_get_ancestor (widget, EGG_TYPE_FIND_BAR)) {
1706                 find_bar_search_changed_cb (EGG_FIND_BAR (ev_window->priv->find_bar),
1707                                             NULL, ev_window);
1708         }
1709         
1710         ev_window_clear_reload_job (ev_window);
1711         ev_window->priv->in_reload = FALSE;
1712 }
1713
1714 /**
1715  * ev_window_get_uri:
1716  * @ev_window: The instance of the #EvWindow.
1717  *
1718  * It returns the uri of the document showed in the #EvWindow.
1719  *
1720  * Returns: the uri of the document showed in the #EvWindow.
1721  */
1722 const char *
1723 ev_window_get_uri (EvWindow *ev_window)
1724 {
1725         return ev_window->priv->uri;
1726 }
1727
1728 /**
1729  * ev_window_close_dialogs:
1730  * @ev_window: The window where dialogs will be closed.
1731  *
1732  * It looks for password, print and properties dialogs and closes them and
1733  * frees them from memory. If there is any print job it does free it too.
1734  */
1735 static void
1736 ev_window_close_dialogs (EvWindow *ev_window)
1737 {
1738         if (ev_window->priv->print_dialog)
1739                 gtk_widget_destroy (ev_window->priv->print_dialog);
1740         ev_window->priv->print_dialog = NULL;
1741         
1742         if (ev_window->priv->properties)
1743                 gtk_widget_destroy (ev_window->priv->properties);
1744         ev_window->priv->properties = NULL;
1745 }
1746
1747 static void
1748 ev_window_clear_progress_idle (EvWindow *ev_window)
1749 {
1750         if (ev_window->priv->progress_idle > 0)
1751                 g_source_remove (ev_window->priv->progress_idle);
1752         ev_window->priv->progress_idle = 0;
1753 }
1754
1755 static void
1756 reset_progress_idle (EvWindow *ev_window)
1757 {
1758         ev_window->priv->progress_idle = 0;
1759 }
1760
1761 static void
1762 ev_window_show_progress_message (EvWindow   *ev_window,
1763                                  guint       interval,
1764                                  GSourceFunc function)
1765 {
1766         if (ev_window->priv->progress_idle > 0)
1767                 g_source_remove (ev_window->priv->progress_idle);
1768         ev_window->priv->progress_idle =
1769                 g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
1770                                             interval, function,
1771                                             ev_window,
1772                                             (GDestroyNotify)reset_progress_idle);
1773 }
1774
1775 static void
1776 ev_window_reset_progress_cancellable (EvWindow *ev_window)
1777 {
1778         if (ev_window->priv->progress_cancellable)
1779                 g_cancellable_reset (ev_window->priv->progress_cancellable);
1780         else
1781                 ev_window->priv->progress_cancellable = g_cancellable_new ();
1782 }
1783
1784 static void
1785 ev_window_progress_response_cb (EvProgressMessageArea *area,
1786                                 gint                   response,
1787                                 EvWindow              *ev_window)
1788 {
1789         if (response == GTK_RESPONSE_CANCEL)
1790                 g_cancellable_cancel (ev_window->priv->progress_cancellable);
1791         ev_window_set_message_area (ev_window, NULL);
1792 }
1793
1794 static gboolean 
1795 show_loading_progress (EvWindow *ev_window)
1796 {
1797         GtkWidget *area;
1798         gchar     *text;
1799         gchar     *display_name;
1800         
1801         if (ev_window->priv->message_area)
1802                 return FALSE;
1803
1804         text = g_uri_unescape_string (ev_window->priv->uri, NULL);
1805         display_name = g_markup_escape_text (text, -1);
1806         g_free (text);
1807         text = g_strdup_printf (_("Loading document from “%s”"),
1808                                 display_name);
1809
1810         area = ev_progress_message_area_new (GTK_STOCK_OPEN,
1811                                              text,
1812                                              GTK_STOCK_CLOSE,
1813                                              GTK_RESPONSE_CLOSE,
1814                                              GTK_STOCK_CANCEL,
1815                                              GTK_RESPONSE_CANCEL,
1816                                              NULL);
1817         g_signal_connect (area, "response",
1818                           G_CALLBACK (ev_window_progress_response_cb),
1819                           ev_window);
1820         gtk_widget_show (area);
1821         ev_window_set_message_area (ev_window, area);
1822
1823         g_free (text);
1824         g_free (display_name);
1825
1826         return FALSE;
1827 }
1828
1829 static void
1830 ev_window_load_remote_failed (EvWindow *ev_window,
1831                               GError   *error)
1832 {
1833         ev_view_set_loading (EV_VIEW (ev_window->priv->view), FALSE);
1834         ev_window->priv->in_reload = FALSE;
1835         ev_window_error_message (ev_window, error, 
1836                                  "%s", _("Unable to open document"));
1837         g_free (ev_window->priv->local_uri);
1838         ev_window->priv->local_uri = NULL;
1839         ev_window->priv->uri_mtime = 0;
1840 }
1841
1842 static void
1843 set_uri_mtime (GFile        *source,
1844                GAsyncResult *async_result,
1845                EvWindow     *ev_window)
1846 {
1847         GFileInfo *info;
1848         GError *error = NULL;
1849
1850         info = g_file_query_info_finish (source, async_result, &error);
1851
1852         if (error) {
1853                 ev_window->priv->uri_mtime = 0;
1854                 g_error_free (error);
1855         } else {
1856                 GTimeVal mtime;
1857                 
1858                 g_file_info_get_modification_time (info, &mtime);
1859                 ev_window->priv->uri_mtime = mtime.tv_sec;
1860                 g_object_unref (info);
1861         }
1862
1863         g_object_unref (source);
1864 }
1865
1866 static void
1867 mount_volume_ready_cb (GFile        *source,
1868                        GAsyncResult *async_result,
1869                        EvWindow     *ev_window)
1870 {
1871         GError *error = NULL;
1872
1873         g_file_mount_enclosing_volume_finish (source, async_result, &error);
1874
1875         if (error) {
1876                 ev_window_load_remote_failed (ev_window, error);
1877                 g_object_unref (source);
1878                 g_error_free (error);
1879         } else {
1880                 /* Volume successfully mounted,
1881                    try opening the file again */
1882                 ev_window_load_file_remote (ev_window, source);
1883         }
1884 }
1885
1886 static void
1887 window_open_file_copy_ready_cb (GFile        *source,
1888                                 GAsyncResult *async_result,
1889                                 EvWindow     *ev_window)
1890 {
1891         GError *error = NULL;
1892
1893         ev_window_clear_progress_idle (ev_window);
1894         ev_window_set_message_area (ev_window, NULL);
1895
1896         g_file_copy_finish (source, async_result, &error);
1897         if (!error) {
1898                 ev_job_scheduler_push_job (ev_window->priv->load_job, EV_JOB_PRIORITY_NONE);
1899                 g_file_query_info_async (source,
1900                                          G_FILE_ATTRIBUTE_TIME_MODIFIED,
1901                                          0, G_PRIORITY_DEFAULT,
1902                                          NULL,
1903                                          (GAsyncReadyCallback)set_uri_mtime,
1904                                          ev_window);
1905                 return;
1906         }
1907
1908         if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_MOUNTED)) {
1909                 GMountOperation *operation;
1910
1911                 operation = gtk_mount_operation_new (GTK_WINDOW (ev_window));
1912                 g_file_mount_enclosing_volume (source,
1913                                                G_MOUNT_MOUNT_NONE,
1914                                                operation, NULL,
1915                                                (GAsyncReadyCallback)mount_volume_ready_cb,
1916                                                ev_window);
1917                 g_object_unref (operation);
1918         } else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
1919                 ev_window_clear_load_job (ev_window);
1920                 ev_window_clear_local_uri (ev_window);
1921                 g_free (ev_window->priv->uri);
1922                 ev_window->priv->uri = NULL;
1923                 g_object_unref (source);
1924                 
1925                 ev_view_set_loading (EV_VIEW (ev_window->priv->view), FALSE);
1926         } else {
1927                 ev_window_load_remote_failed (ev_window, error);
1928                 g_object_unref (source);
1929         }
1930         
1931         g_error_free (error);
1932 }
1933
1934 static void
1935 window_open_file_copy_progress_cb (goffset   n_bytes,
1936                                    goffset   total_bytes,
1937                                    EvWindow *ev_window)
1938 {
1939         gchar *status;
1940         gdouble fraction;
1941         
1942         if (!ev_window->priv->message_area)
1943                 return;
1944
1945         if (total_bytes <= 0)
1946                 return;
1947
1948         fraction = n_bytes / (gdouble)total_bytes;
1949         status = g_strdup_printf (_("Downloading document (%d%%)"),
1950                                   (gint)(fraction * 100));
1951         
1952         ev_progress_message_area_set_status (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
1953                                              status);
1954         ev_progress_message_area_set_fraction (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
1955                                                fraction);
1956
1957         g_free (status);
1958 }
1959
1960 static void
1961 ev_window_load_file_remote (EvWindow *ev_window,
1962                             GFile    *source_file)
1963 {
1964         GFile *target_file;
1965         
1966         if (!ev_window->priv->local_uri) {
1967                 char *base_name, *template;
1968                 GFile *tmp_file;
1969                 GError *err = NULL;
1970
1971                 /* We'd like to keep extension of source uri since
1972                  * it helps to resolve some mime types, say cbz.
1973                  */
1974                 base_name = g_file_get_basename (source_file);
1975                 template = g_strdup_printf ("document.XXXXXX-%s", base_name);
1976                 g_free (base_name);
1977
1978                 tmp_file = ev_mkstemp_file (template, &err);
1979                 g_free (template);
1980                 if (tmp_file == NULL) {
1981                         ev_window_error_message (ev_window, err,
1982                                                  "%s", _("Failed to load remote file."));
1983                         g_error_free (err);
1984                         return;
1985                 }
1986
1987                 ev_window->priv->local_uri = g_file_get_uri (tmp_file);
1988                 g_object_unref (tmp_file);
1989
1990                 ev_job_load_set_uri (EV_JOB_LOAD (ev_window->priv->load_job),
1991                                      ev_window->priv->local_uri);
1992         }
1993
1994         ev_window_reset_progress_cancellable (ev_window);
1995         
1996         target_file = g_file_new_for_uri (ev_window->priv->local_uri);
1997         g_file_copy_async (source_file, target_file,
1998                            G_FILE_COPY_OVERWRITE,
1999                            G_PRIORITY_DEFAULT,
2000                            ev_window->priv->progress_cancellable,
2001                            (GFileProgressCallback)window_open_file_copy_progress_cb,
2002                            ev_window, 
2003                            (GAsyncReadyCallback)window_open_file_copy_ready_cb,
2004                            ev_window);
2005         g_object_unref (target_file);
2006
2007         ev_window_show_progress_message (ev_window, 1,
2008                                          (GSourceFunc)show_loading_progress);
2009 }
2010
2011 void
2012 ev_window_open_uri (EvWindow       *ev_window,
2013                     const char     *uri,
2014                     EvLinkDest     *dest,
2015                     EvWindowRunMode mode,
2016                     const gchar    *search_string)
2017 {
2018         GFile *source_file;
2019
2020         ev_window->priv->in_reload = FALSE;
2021         
2022         if (ev_window->priv->uri &&
2023             g_ascii_strcasecmp (ev_window->priv->uri, uri) == 0) {
2024                 ev_window_reload_document (ev_window, dest);
2025                 return;
2026         }
2027
2028         if (ev_window->priv->monitor) {
2029                 g_object_unref (ev_window->priv->monitor);
2030                 ev_window->priv->monitor = NULL;
2031         }
2032         
2033         ev_window_close_dialogs (ev_window);
2034         ev_window_clear_load_job (ev_window);
2035         ev_window_clear_local_uri (ev_window);
2036
2037         ev_window->priv->window_mode = mode;
2038
2039         if (ev_window->priv->uri)
2040                 g_free (ev_window->priv->uri);
2041         ev_window->priv->uri = g_strdup (uri);
2042
2043         if (ev_window->priv->metadata)
2044                 g_object_unref (ev_window->priv->metadata);
2045         if (ev_window->priv->bookmarks)
2046                 g_object_unref (ev_window->priv->bookmarks);
2047
2048         source_file = g_file_new_for_uri (uri);
2049         if (!ev_file_is_temp (source_file) && ev_is_metadata_supported_for_file (source_file)) {
2050                 ev_window->priv->metadata = ev_metadata_new (source_file);
2051                 ev_window->priv->is_new_doc = ev_metadata_is_empty (ev_window->priv->metadata);
2052         } else {
2053                 ev_window->priv->metadata = NULL;
2054         }
2055
2056         if (ev_window->priv->metadata) {
2057                 ev_window->priv->bookmarks = ev_bookmarks_new (ev_window->priv->metadata);
2058                 ev_sidebar_bookmarks_set_bookmarks (EV_SIDEBAR_BOOKMARKS (ev_window->priv->sidebar_bookmarks),
2059                                                     ev_window->priv->bookmarks);
2060                 g_signal_connect_swapped (ev_window->priv->bookmarks, "changed",
2061                                           G_CALLBACK (ev_window_setup_bookmarks),
2062                                           ev_window);
2063         } else {
2064                 ev_window->priv->bookmarks = NULL;
2065         }
2066
2067         if (ev_window->priv->search_string)
2068                 g_free (ev_window->priv->search_string);
2069         ev_window->priv->search_string = search_string ?
2070                 g_strdup (search_string) : NULL;
2071
2072         if (ev_window->priv->dest)
2073                 g_object_unref (ev_window->priv->dest);
2074         ev_window->priv->dest = dest ? g_object_ref (dest) : NULL;
2075
2076         setup_size_from_metadata (ev_window);
2077         setup_model_from_metadata (ev_window);
2078         ev_window_setup_bookmarks (ev_window);
2079
2080         ev_window->priv->load_job = ev_job_load_new (uri);
2081         g_signal_connect (ev_window->priv->load_job,
2082                           "finished",
2083                           G_CALLBACK (ev_window_load_job_cb),
2084                           ev_window);
2085
2086         if (!g_file_is_native (source_file) && !ev_window->priv->local_uri) {
2087                 ev_window_load_file_remote (ev_window, source_file);
2088         } else {
2089                 ev_view_set_loading (EV_VIEW (ev_window->priv->view), TRUE);
2090                 g_object_unref (source_file);
2091                 ev_job_scheduler_push_job (ev_window->priv->load_job, EV_JOB_PRIORITY_NONE);
2092         }
2093 }
2094
2095 void
2096 ev_window_open_document (EvWindow       *ev_window,
2097                          EvDocument     *document,
2098                          EvLinkDest     *dest,
2099                          EvWindowRunMode mode,
2100                          const gchar    *search_string)
2101 {
2102         if (document == ev_window->priv->document)
2103                 return;
2104
2105         ev_window_close_dialogs (ev_window);
2106         ev_window_clear_load_job (ev_window);
2107         ev_window_clear_local_uri (ev_window);
2108
2109         if (ev_window->priv->monitor) {
2110                 g_object_unref (ev_window->priv->monitor);
2111                 ev_window->priv->monitor = NULL;
2112         }
2113
2114         if (ev_window->priv->uri)
2115                 g_free (ev_window->priv->uri);
2116         ev_window->priv->uri = g_strdup (ev_document_get_uri (document));
2117
2118         setup_size_from_metadata (ev_window);
2119         setup_model_from_metadata (ev_window);
2120
2121         ev_document_model_set_document (ev_window->priv->model, document);
2122
2123         setup_document_from_metadata (ev_window);
2124         setup_view_from_metadata (ev_window);
2125
2126         if (dest) {
2127                 EvLink *link;
2128                 EvLinkAction *link_action;
2129
2130                 link_action = ev_link_action_new_dest (dest);
2131                 link = ev_link_new (NULL, link_action);
2132                 ev_view_handle_link (EV_VIEW (ev_window->priv->view), link);
2133                 /* FIXME: link action should inc dest ref counting
2134                  * or not unref it at all
2135                  */
2136                 g_object_ref (dest);
2137                 g_object_unref (link);
2138         }
2139
2140         switch (mode) {
2141         case EV_WINDOW_MODE_FULLSCREEN:
2142                 ev_window_run_fullscreen (ev_window);
2143                 break;
2144         case EV_WINDOW_MODE_PRESENTATION:
2145                 ev_window_run_presentation (ev_window);
2146                 break;
2147         default:
2148                 break;
2149         }
2150
2151         if (search_string && EV_IS_DOCUMENT_FIND (document)) {
2152                 ev_window_cmd_edit_find (NULL, ev_window);
2153                 egg_find_bar_set_search_string (EGG_FIND_BAR (ev_window->priv->find_bar),
2154                                                 search_string);
2155         }
2156
2157         /* Create a monitor for the document */
2158         ev_window->priv->monitor = ev_file_monitor_new (ev_window->priv->uri);
2159         g_signal_connect_swapped (ev_window->priv->monitor, "changed",
2160                                   G_CALLBACK (ev_window_document_changed),
2161                                   ev_window);
2162 }
2163
2164 static void
2165 ev_window_reload_local (EvWindow *ev_window)
2166 {
2167         const gchar *uri;
2168         
2169         uri = ev_window->priv->local_uri ? ev_window->priv->local_uri : ev_window->priv->uri;
2170         ev_window->priv->reload_job = ev_job_load_new (uri);
2171         g_signal_connect (ev_window->priv->reload_job, "finished",
2172                           G_CALLBACK (ev_window_reload_job_cb),
2173                           ev_window);
2174         ev_job_scheduler_push_job (ev_window->priv->reload_job, EV_JOB_PRIORITY_NONE);
2175 }
2176
2177 static gboolean 
2178 show_reloading_progress (EvWindow *ev_window)
2179 {
2180         GtkWidget *area;
2181         gchar     *text;
2182         
2183         if (ev_window->priv->message_area)
2184                 return FALSE;
2185         
2186         text = g_strdup_printf (_("Reloading document from %s"),
2187                                 ev_window->priv->uri);
2188         area = ev_progress_message_area_new (GTK_STOCK_REFRESH,
2189                                              text,
2190                                              GTK_STOCK_CLOSE,
2191                                              GTK_RESPONSE_CLOSE,
2192                                              GTK_STOCK_CANCEL,
2193                                              GTK_RESPONSE_CANCEL,
2194                                              NULL);
2195         g_signal_connect (area, "response",
2196                           G_CALLBACK (ev_window_progress_response_cb),
2197                           ev_window);
2198         gtk_widget_show (area);
2199         ev_window_set_message_area (ev_window, area);
2200         g_free (text);
2201
2202         return FALSE;
2203 }
2204
2205 static void
2206 reload_remote_copy_ready_cb (GFile        *remote,
2207                              GAsyncResult *async_result,
2208                              EvWindow     *ev_window)
2209 {
2210         GError *error = NULL;
2211         
2212         ev_window_clear_progress_idle (ev_window);
2213         
2214         g_file_copy_finish (remote, async_result, &error);
2215         if (error) {
2216                 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
2217                         ev_window_error_message (ev_window, error,
2218                                                  "%s", _("Failed to reload document."));
2219                 g_error_free (error);
2220         } else {
2221                 ev_window_reload_local (ev_window);
2222         }
2223                 
2224         g_object_unref (remote);
2225 }
2226
2227 static void
2228 reload_remote_copy_progress_cb (goffset   n_bytes,
2229                                 goffset   total_bytes,
2230                                 EvWindow *ev_window)
2231 {
2232         gchar *status;
2233         gdouble fraction;
2234         
2235         if (!ev_window->priv->message_area)
2236                 return;
2237
2238         if (total_bytes <= 0)
2239                 return;
2240
2241         fraction = n_bytes / (gdouble)total_bytes;
2242         status = g_strdup_printf (_("Downloading document (%d%%)"),
2243                                   (gint)(fraction * 100));
2244         
2245         ev_progress_message_area_set_status (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
2246                                              status);
2247         ev_progress_message_area_set_fraction (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
2248                                                fraction);
2249
2250         g_free (status);
2251 }
2252
2253 static void
2254 query_remote_uri_mtime_cb (GFile        *remote,
2255                            GAsyncResult *async_result,
2256                            EvWindow     *ev_window)
2257 {
2258         GFileInfo *info;
2259         GTimeVal   mtime;
2260         GError    *error = NULL;
2261
2262         info = g_file_query_info_finish (remote, async_result, &error);
2263         if (error) {
2264                 g_error_free (error);
2265                 g_object_unref (remote);
2266                 ev_window_reload_local (ev_window);
2267
2268                 return;
2269         }
2270         
2271         g_file_info_get_modification_time (info, &mtime);
2272         if (ev_window->priv->uri_mtime != mtime.tv_sec) {
2273                 GFile *target_file;
2274                         
2275                 /* Remote file has changed */
2276                 ev_window->priv->uri_mtime = mtime.tv_sec;
2277
2278                 ev_window_reset_progress_cancellable (ev_window);
2279                 
2280                 target_file = g_file_new_for_uri (ev_window->priv->local_uri);
2281                 g_file_copy_async (remote, target_file,
2282                                    G_FILE_COPY_OVERWRITE,
2283                                    G_PRIORITY_DEFAULT,
2284                                    ev_window->priv->progress_cancellable,
2285                                    (GFileProgressCallback)reload_remote_copy_progress_cb,
2286                                    ev_window, 
2287                                    (GAsyncReadyCallback)reload_remote_copy_ready_cb,
2288                                    ev_window);
2289                 g_object_unref (target_file);
2290                 ev_window_show_progress_message (ev_window, 1,
2291                                                  (GSourceFunc)show_reloading_progress);
2292         } else {
2293                 g_object_unref (remote);
2294                 ev_window_reload_local (ev_window);
2295         }
2296         
2297         g_object_unref (info);
2298 }
2299
2300 static void
2301 ev_window_reload_remote (EvWindow *ev_window)
2302 {
2303         GFile *remote;
2304         
2305         remote = g_file_new_for_uri (ev_window->priv->uri);
2306         /* Reload the remote uri only if it has changed */
2307         g_file_query_info_async (remote,
2308                                  G_FILE_ATTRIBUTE_TIME_MODIFIED,
2309                                  0, G_PRIORITY_DEFAULT,
2310                                  NULL,
2311                                  (GAsyncReadyCallback)query_remote_uri_mtime_cb,
2312                                  ev_window);
2313 }
2314
2315 static void
2316 ev_window_reload_document (EvWindow *ev_window,
2317                            EvLinkDest *dest)
2318 {
2319         gint page;
2320
2321         
2322         ev_window_clear_reload_job (ev_window);
2323         ev_window->priv->in_reload = TRUE;
2324
2325         page = ev_document_model_get_page (ev_window->priv->model);
2326         
2327         if (ev_window->priv->dest)
2328                 g_object_unref (ev_window->priv->dest);
2329         ev_window->priv->dest = dest ? g_object_ref (dest) : NULL;
2330
2331         if (ev_window->priv->local_uri) {
2332                 ev_window_reload_remote (ev_window);
2333         } else {
2334                 ev_window_reload_local (ev_window);
2335         }
2336 }
2337
2338 static void
2339 file_open_dialog_response_cb (GtkWidget *chooser,
2340                               gint       response_id,
2341                               EvWindow  *ev_window)
2342 {
2343         if (response_id == GTK_RESPONSE_OK) {
2344                 GSList *uris;
2345                 gchar  *uri;
2346
2347                 uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (chooser));
2348
2349                 ev_application_open_uri_list (EV_APP, uris,
2350                                               gtk_window_get_screen (GTK_WINDOW (ev_window)),
2351                                               gtk_get_current_event_time ());
2352
2353                 g_slist_foreach (uris, (GFunc)g_free, NULL);
2354                 g_slist_free (uris);
2355
2356                 uri = gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (chooser));
2357                 ev_application_set_filechooser_uri (EV_APP,
2358                                                     GTK_FILE_CHOOSER_ACTION_OPEN,
2359                                                     uri);
2360                 g_free (uri);
2361         }
2362
2363         gtk_widget_destroy (chooser);
2364 }
2365
2366 static void
2367 ev_window_cmd_file_open (GtkAction *action, EvWindow *window)
2368 {
2369         GtkWidget   *chooser;
2370         const gchar *default_uri;
2371         gchar       *parent_uri = NULL;
2372
2373         chooser = gtk_file_chooser_dialog_new (_("Open Document"),
2374                                                GTK_WINDOW (window),
2375                                                GTK_FILE_CHOOSER_ACTION_OPEN,
2376                                                GTK_STOCK_CANCEL,
2377                                                GTK_RESPONSE_CANCEL,
2378                                                GTK_STOCK_OPEN, GTK_RESPONSE_OK,
2379                                                NULL);
2380
2381         ev_document_factory_add_filters (chooser, NULL);
2382         gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser), TRUE);
2383         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), FALSE);
2384
2385         default_uri = ev_application_get_filechooser_uri (EV_APP, GTK_FILE_CHOOSER_ACTION_OPEN);
2386         if (!default_uri && window->priv->uri) {
2387                 GFile *file, *parent;
2388
2389                 file = g_file_new_for_uri (window->priv->uri);
2390                 parent = g_file_get_parent (file);
2391                 if (parent) {
2392                         parent_uri = g_file_get_uri (parent);
2393                         default_uri = parent_uri;
2394                         g_object_unref (parent);
2395                 }
2396                 g_object_unref (file);
2397         }
2398
2399         if (default_uri) {
2400                 gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (chooser), default_uri);
2401         } else {
2402                 const gchar *folder;
2403
2404                 folder = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
2405                 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
2406                                                      folder ? folder : g_get_home_dir ());
2407         }
2408         g_free (parent_uri);
2409
2410         g_signal_connect (chooser, "response",
2411                           G_CALLBACK (file_open_dialog_response_cb),
2412                           window);
2413
2414         gtk_widget_show (chooser);
2415 }
2416
2417 static void
2418 ev_window_open_copy_at_dest (EvWindow   *window,
2419                              EvLinkDest *dest)
2420 {
2421         EvWindow *new_window = EV_WINDOW (ev_window_new ());
2422
2423         ev_window_n_copies++;
2424
2425         if (window->priv->metadata)
2426                 new_window->priv->metadata = g_object_ref (window->priv->metadata);
2427         ev_window_open_document (new_window,
2428                                  window->priv->document,
2429                                  dest, 0, NULL);
2430         gtk_window_present (GTK_WINDOW (new_window));
2431 }
2432
2433 static void
2434 ev_window_cmd_file_open_copy (GtkAction *action, EvWindow *window)
2435 {
2436         ev_window_open_copy_at_dest (window, NULL);
2437 }
2438
2439 static void
2440 ev_window_cmd_recent_file_activate (GtkAction *action,
2441                                     EvWindow  *window)
2442 {
2443         GtkRecentInfo *info;
2444         const gchar   *uri;
2445
2446         info = g_object_get_data (G_OBJECT (action), "gtk-recent-info");
2447         g_assert (info != NULL);
2448         
2449         uri = gtk_recent_info_get_uri (info);
2450         
2451         ev_application_open_uri_at_dest (EV_APP, uri,
2452                                          gtk_window_get_screen (GTK_WINDOW (window)),
2453                                          NULL, 0, NULL, gtk_get_current_event_time ());
2454 }
2455
2456 static void
2457 ev_window_open_recent_action_item_activated (EvOpenRecentAction *action,
2458                                              const gchar        *uri,
2459                                              EvWindow           *window)
2460 {
2461         ev_application_open_uri_at_dest (EV_APP, uri,
2462                                          gtk_window_get_screen (GTK_WINDOW (window)),
2463                                          NULL, 0, NULL, gtk_get_current_event_time ());
2464 }
2465
2466 static void
2467 ev_window_add_recent (EvWindow *window, const char *filename)
2468 {
2469         gtk_recent_manager_add_item (window->priv->recent_manager, filename);
2470 }
2471
2472 static gint
2473 compare_recent_items (GtkRecentInfo *a, GtkRecentInfo *b)
2474 {
2475         gboolean     has_ev_a, has_ev_b;
2476         const gchar *evince = g_get_application_name ();
2477
2478         has_ev_a = gtk_recent_info_has_application (a, evince);
2479         has_ev_b = gtk_recent_info_has_application (b, evince);
2480         
2481         if (has_ev_a && has_ev_b) {
2482                 time_t time_a, time_b;
2483
2484                 time_a = gtk_recent_info_get_modified (a);
2485                 time_b = gtk_recent_info_get_modified (b);
2486
2487                 return (time_b - time_a);
2488         } else if (has_ev_a) {
2489                 return -1;
2490         } else if (has_ev_b) {
2491                 return 1;
2492         }
2493
2494         return 0;
2495 }
2496
2497 /*
2498  * Doubles underscore to avoid spurious menu accels.
2499  */
2500 static gchar * 
2501 ev_window_get_recent_file_label (gint index, const gchar *filename)
2502 {
2503         GString *str;
2504         gint length;
2505         const gchar *p;
2506         const gchar *end;
2507         gboolean is_rtl;
2508         
2509         is_rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);
2510
2511         g_return_val_if_fail (filename != NULL, NULL);
2512         
2513         length = strlen (filename);
2514         str = g_string_sized_new (length + 10);
2515         g_string_printf (str, "%s_%d.  ", is_rtl ? "\xE2\x80\x8F" : "", index);
2516
2517         p = filename;
2518         end = filename + length;
2519  
2520         while (p != end) {
2521                 const gchar *next;
2522                 next = g_utf8_next_char (p);
2523  
2524                 switch (*p) {
2525                         case '_':
2526                                 g_string_append (str, "__");
2527                                 break;
2528                         default:
2529                                 g_string_append_len (str, p, next - p);
2530                                 break;
2531                 }
2532  
2533                 p = next;
2534         }
2535  
2536         return g_string_free (str, FALSE);
2537 }
2538
2539 static void
2540 ev_window_recent_action_connect_proxy_cb (GtkActionGroup *action_group,
2541                                           GtkAction *action,
2542                                           GtkWidget *proxy,
2543                                           gpointer data)
2544 {
2545         GtkLabel *label;
2546
2547         if (!GTK_IS_MENU_ITEM (proxy))
2548                 return;
2549
2550         label = GTK_LABEL (gtk_bin_get_child (GTK_BIN (proxy)));
2551
2552         gtk_label_set_ellipsize (label, PANGO_ELLIPSIZE_MIDDLE);
2553         gtk_label_set_max_width_chars (label, MAX_RECENT_ITEM_LEN);
2554 }
2555
2556 static void
2557 ev_window_setup_recent (EvWindow *ev_window)
2558 {
2559         GList        *items, *l;
2560         guint         n_items = 0;
2561         const gchar  *evince = g_get_application_name ();
2562         static guint  i = 0;
2563
2564         if (ev_window->priv->recent_ui_id > 0) {
2565                 gtk_ui_manager_remove_ui (ev_window->priv->ui_manager,
2566                                           ev_window->priv->recent_ui_id);
2567                 gtk_ui_manager_ensure_update (ev_window->priv->ui_manager);
2568         }
2569         ev_window->priv->recent_ui_id = gtk_ui_manager_new_merge_id (ev_window->priv->ui_manager);
2570
2571         if (ev_window->priv->recent_action_group) {
2572                 gtk_ui_manager_remove_action_group (ev_window->priv->ui_manager,
2573                                                     ev_window->priv->recent_action_group);
2574                 g_object_unref (ev_window->priv->recent_action_group);
2575         }
2576         ev_window->priv->recent_action_group = gtk_action_group_new ("RecentFilesActions");
2577         g_signal_connect (ev_window->priv->recent_action_group, "connect-proxy",
2578                           G_CALLBACK (ev_window_recent_action_connect_proxy_cb), NULL);
2579
2580         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
2581                                             ev_window->priv->recent_action_group, -1);
2582
2583         items = gtk_recent_manager_get_items (ev_window->priv->recent_manager);
2584         items = g_list_sort (items, (GCompareFunc) compare_recent_items);
2585
2586         for (l = items; l && l->data; l = g_list_next (l)) {
2587                 GtkRecentInfo *info;
2588                 GtkAction     *action;
2589                 gchar         *action_name;
2590                 gchar         *label;
2591                 const gchar   *mime_type;
2592                 gchar         *content_type;
2593                 GIcon         *icon = NULL;
2594
2595                 info = (GtkRecentInfo *) l->data;
2596
2597                 if (!gtk_recent_info_has_application (info, evince) ||
2598                     (gtk_recent_info_is_local (info) && !gtk_recent_info_exists (info)))
2599                         continue;
2600
2601                 action_name = g_strdup_printf ("RecentFile%u", i++);
2602                 label = ev_window_get_recent_file_label (
2603                         n_items + 1, gtk_recent_info_get_display_name (info));
2604
2605                 mime_type = gtk_recent_info_get_mime_type (info);
2606                 content_type = g_content_type_from_mime_type (mime_type);
2607                 if (content_type != NULL) {
2608                         icon = g_content_type_get_icon (content_type);
2609                         g_free (content_type);
2610                 }
2611
2612                 action = g_object_new (GTK_TYPE_ACTION,
2613                                        "name", action_name,
2614                                        "label", label,
2615                                        "gicon", icon,
2616                                        "always-show-image", TRUE,
2617                                        NULL);
2618
2619                 g_object_set_data_full (G_OBJECT (action),
2620                                         "gtk-recent-info",
2621                                         gtk_recent_info_ref (info),
2622                                         (GDestroyNotify) gtk_recent_info_unref);
2623                 
2624                 g_signal_connect (action, "activate",
2625                                   G_CALLBACK (ev_window_cmd_recent_file_activate),
2626                                   (gpointer) ev_window);
2627
2628                 gtk_action_group_add_action (ev_window->priv->recent_action_group,
2629                                              action);
2630                 g_object_unref (action);
2631
2632                 gtk_ui_manager_add_ui (ev_window->priv->ui_manager,
2633                                        ev_window->priv->recent_ui_id,
2634                                        "/MainMenu/FileMenu/RecentFilesMenu",
2635                                        label,
2636                                        action_name,
2637                                        GTK_UI_MANAGER_MENUITEM,
2638                                        FALSE);
2639                 g_free (action_name);
2640                 g_free (label);
2641                 if (icon != NULL)
2642                         g_object_unref (icon);
2643
2644                 if (++n_items == 5)
2645                         break;
2646         }
2647         
2648         g_list_foreach (items, (GFunc) gtk_recent_info_unref, NULL);
2649         g_list_free (items);
2650 }
2651
2652 static gboolean 
2653 show_saving_progress (GFile *dst)
2654 {
2655         EvWindow  *ev_window;
2656         GtkWidget *area;
2657         gchar     *text;
2658         gchar     *uri;
2659         EvSaveType save_type;
2660
2661         ev_window = EV_WINDOW (g_object_get_data (G_OBJECT (dst), "ev-window"));
2662         ev_window->priv->progress_idle = 0;
2663         
2664         if (ev_window->priv->message_area)
2665                 return FALSE;
2666
2667         save_type = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dst), "save-type"));
2668         uri = g_file_get_uri (dst);
2669         switch (save_type) {
2670         case EV_SAVE_DOCUMENT:
2671                 text = g_strdup_printf (_("Saving document to %s"), uri);
2672                 break;
2673         case EV_SAVE_ATTACHMENT:
2674                 text = g_strdup_printf (_("Saving attachment to %s"), uri);
2675                 break;
2676         case EV_SAVE_IMAGE:
2677                 text = g_strdup_printf (_("Saving image to %s"), uri);
2678                 break;
2679         default:
2680                 g_assert_not_reached ();
2681         }
2682         g_free (uri);
2683         area = ev_progress_message_area_new (GTK_STOCK_SAVE,
2684                                              text,
2685                                              GTK_STOCK_CLOSE,
2686                                              GTK_RESPONSE_CLOSE,
2687                                              GTK_STOCK_CANCEL,
2688                                              GTK_RESPONSE_CANCEL,
2689                                              NULL);
2690         g_signal_connect (area, "response",
2691                           G_CALLBACK (ev_window_progress_response_cb),
2692                           ev_window);
2693         gtk_widget_show (area);
2694         ev_window_set_message_area (ev_window, area);
2695         g_free (text);
2696
2697         return FALSE;
2698 }
2699
2700 static void
2701 window_save_file_copy_ready_cb (GFile        *src,
2702                                 GAsyncResult *async_result,
2703                                 GFile        *dst)
2704 {
2705         EvWindow *ev_window;
2706         GError   *error = NULL;
2707
2708         ev_window = EV_WINDOW (g_object_get_data (G_OBJECT (dst), "ev-window"));
2709         ev_window_clear_progress_idle (ev_window);
2710         
2711         if (g_file_copy_finish (src, async_result, &error)) {
2712                 ev_tmp_file_unlink (src);
2713                 return;
2714         }
2715
2716         if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
2717                 gchar *name;
2718                 
2719                 name = g_file_get_basename (dst);
2720                 ev_window_error_message (ev_window, error,
2721                                          _("The file could not be saved as “%s”."),
2722                                          name);
2723                 g_free (name);
2724         }
2725         ev_tmp_file_unlink (src);
2726         g_error_free (error);
2727 }
2728
2729 static void
2730 window_save_file_copy_progress_cb (goffset n_bytes,
2731                                    goffset total_bytes,
2732                                    GFile  *dst)
2733 {
2734         EvWindow  *ev_window;
2735         EvSaveType save_type;
2736         gchar     *status;
2737         gdouble    fraction;
2738
2739         ev_window = EV_WINDOW (g_object_get_data (G_OBJECT (dst), "ev-window"));
2740         
2741         if (!ev_window->priv->message_area)
2742                 return;
2743
2744         if (total_bytes <= 0)
2745                 return;
2746
2747         fraction = n_bytes / (gdouble)total_bytes;
2748         save_type = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dst), "save-type"));
2749
2750         switch (save_type) {
2751         case EV_SAVE_DOCUMENT:
2752                 status = g_strdup_printf (_("Uploading document (%d%%)"),
2753                                           (gint)(fraction * 100));
2754                 break;
2755         case EV_SAVE_ATTACHMENT:
2756                 status = g_strdup_printf (_("Uploading attachment (%d%%)"),
2757                                           (gint)(fraction * 100));
2758                 break;
2759         case EV_SAVE_IMAGE:
2760                 status = g_strdup_printf (_("Uploading image (%d%%)"),
2761                                           (gint)(fraction * 100));
2762                 break;
2763         default:
2764                 g_assert_not_reached ();
2765         }
2766         
2767         ev_progress_message_area_set_status (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
2768                                              status);
2769         ev_progress_message_area_set_fraction (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
2770                                                fraction);
2771
2772         g_free (status);
2773 }
2774
2775 static void
2776 ev_window_save_remote (EvWindow  *ev_window,
2777                        EvSaveType save_type,
2778                        GFile     *src,
2779                        GFile     *dst)
2780 {
2781         ev_window_reset_progress_cancellable (ev_window);
2782         g_object_set_data (G_OBJECT (dst), "ev-window", ev_window);
2783         g_object_set_data (G_OBJECT (dst), "save-type", GINT_TO_POINTER (save_type));
2784         g_file_copy_async (src, dst,
2785                            G_FILE_COPY_OVERWRITE,
2786                            G_PRIORITY_DEFAULT,
2787                            ev_window->priv->progress_cancellable,
2788                            (GFileProgressCallback)window_save_file_copy_progress_cb,
2789                            dst,
2790                            (GAsyncReadyCallback)window_save_file_copy_ready_cb,
2791                            dst);
2792         ev_window->priv->progress_idle =
2793                 g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
2794                                             1,
2795                                             (GSourceFunc)show_saving_progress,
2796                                             dst,
2797                                             NULL);
2798 }
2799
2800 static void
2801 ev_window_clear_save_job (EvWindow *ev_window)
2802 {
2803         if (ev_window->priv->save_job != NULL) {
2804                 if (!ev_job_is_finished (ev_window->priv->save_job))
2805                         ev_job_cancel (ev_window->priv->save_job);
2806                 
2807                 g_signal_handlers_disconnect_by_func (ev_window->priv->save_job,
2808                                                       ev_window_save_job_cb,
2809                                                       ev_window);
2810                 g_object_unref (ev_window->priv->save_job);
2811                 ev_window->priv->save_job = NULL;
2812         }
2813 }
2814
2815 static void
2816 ev_window_save_job_cb (EvJob     *job,
2817                        EvWindow  *window)
2818 {
2819         if (ev_job_is_failed (job)) {
2820                 ev_window_error_message (window, job->error,
2821                                          _("The file could not be saved as “%s”."),
2822                                          EV_JOB_SAVE (job)->uri);
2823         } else {
2824                 ev_window_add_recent (window, EV_JOB_SAVE (job)->uri);
2825         }
2826
2827         ev_window_clear_save_job (window);
2828 }
2829
2830 static void
2831 file_save_dialog_response_cb (GtkWidget *fc,
2832                               gint       response_id,
2833                               EvWindow  *ev_window)
2834 {
2835         gchar *uri;
2836         GFile *file, *parent;
2837
2838         if (response_id != GTK_RESPONSE_OK) {
2839                 gtk_widget_destroy (fc);
2840                 return;
2841         }
2842
2843         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
2844         file = g_file_new_for_uri (uri);
2845         parent = g_file_get_parent (file);
2846         g_object_unref (file);
2847         if (parent) {
2848                 gchar *folder_uri;
2849
2850                 folder_uri = g_file_get_uri (parent);
2851                 ev_application_set_filechooser_uri (EV_APP,
2852                                                     GTK_FILE_CHOOSER_ACTION_SAVE,
2853                                                     folder_uri);
2854                 g_free (folder_uri);
2855                 g_object_unref (parent);
2856         }
2857
2858         /* FIXME: remote copy should be done here rather than in the save job, 
2859          * so that we can track progress and cancel the operation
2860          */
2861
2862         ev_window_clear_save_job (ev_window);
2863         ev_window->priv->save_job = ev_job_save_new (ev_window->priv->document,
2864                                                      uri, ev_window->priv->uri);
2865         g_signal_connect (ev_window->priv->save_job, "finished",
2866                           G_CALLBACK (ev_window_save_job_cb),
2867                           ev_window);
2868         /* The priority doesn't matter for this job */
2869         ev_job_scheduler_push_job (ev_window->priv->save_job, EV_JOB_PRIORITY_NONE);
2870
2871         g_free (uri);
2872         gtk_widget_destroy (fc);
2873 }
2874
2875 static void
2876 ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
2877 {
2878         GtkWidget *fc;
2879         gchar *base_name;
2880         GFile *file;
2881         const gchar *default_uri;
2882
2883         fc = gtk_file_chooser_dialog_new (
2884                 _("Save a Copy"),
2885                 GTK_WINDOW (ev_window), GTK_FILE_CHOOSER_ACTION_SAVE,
2886                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2887                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
2888                 NULL);
2889
2890         ev_document_factory_add_filters (fc, ev_window->priv->document);
2891         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
2892         gtk_dialog_set_alternative_button_order (GTK_DIALOG (fc),
2893                                                 GTK_RESPONSE_OK,
2894                                                 GTK_RESPONSE_CANCEL,
2895                                                 -1);
2896
2897         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
2898         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
2899         file = g_file_new_for_uri (ev_window->priv->uri);
2900         base_name = g_file_get_basename (file);
2901         gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc), base_name);
2902
2903         default_uri = ev_application_get_filechooser_uri (EV_APP, GTK_FILE_CHOOSER_ACTION_SAVE);
2904         if (default_uri) {
2905                 gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (fc), default_uri);
2906         } else {
2907                 const gchar *folder;
2908
2909                 folder = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
2910                 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc),
2911                                                      folder ? folder : g_get_home_dir ());
2912         }
2913
2914         g_object_unref (file);
2915         g_free (base_name);
2916
2917         g_signal_connect (fc, "response",
2918                           G_CALLBACK (file_save_dialog_response_cb),
2919                           ev_window);
2920
2921         gtk_widget_show (fc);
2922 }
2923
2924 static void
2925 ev_window_cmd_open_containing_folder (GtkAction *action, EvWindow *ev_window)
2926 {
2927         GtkWidget *ev_window_widget;
2928         GFile *file;
2929         GFile *parent;
2930
2931         ev_window_widget = GTK_WIDGET (ev_window);
2932
2933         file = g_file_new_for_uri (ev_window->priv->uri);
2934         parent = g_file_get_parent (file);
2935
2936         if (parent) {
2937                 char *parent_uri;
2938
2939                 parent_uri = g_file_get_uri (parent);
2940                 if (parent_uri) {
2941                         GdkScreen *screen;
2942                         guint32 timestamp;
2943                         GError *error;
2944
2945                         screen = gtk_widget_get_screen (ev_window_widget);
2946                         timestamp = gtk_get_current_event_time ();
2947
2948                         error = NULL;
2949                         if (!gtk_show_uri (screen, parent_uri, timestamp, &error)) {
2950                                 ev_window_error_message (ev_window, error, _("Could not open the containing folder"));
2951                                 g_error_free (error);
2952                         }
2953
2954                         g_free (parent_uri);
2955                 }
2956         }
2957
2958         if (file)
2959                 g_object_unref (file);
2960
2961         if (parent)
2962                 g_object_unref (parent);
2963         
2964 }
2965
2966 static GKeyFile *
2967 get_print_settings_file (void)
2968 {
2969         GKeyFile *print_settings_file;
2970         gchar    *filename;
2971         GError *error = NULL;
2972
2973         print_settings_file = g_key_file_new ();
2974
2975         filename = g_build_filename (ev_application_get_dot_dir (EV_APP, FALSE),
2976                                      EV_PRINT_SETTINGS_FILE, NULL);
2977         if (!g_key_file_load_from_file (print_settings_file,
2978                                         filename,
2979                                         G_KEY_FILE_KEEP_COMMENTS |
2980                                         G_KEY_FILE_KEEP_TRANSLATIONS,
2981                                         &error)) {
2982
2983                 /* Don't warn if the file simply doesn't exist */
2984                 if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
2985                         g_warning ("%s", error->message);
2986
2987                 g_error_free (error);
2988         }
2989
2990         g_free (filename);
2991
2992         return print_settings_file;
2993 }
2994
2995 static void
2996 save_print_setting_file (GKeyFile *key_file)
2997 {
2998         gchar  *filename;
2999         gchar  *data;
3000         gsize  data_length;
3001         GError *error = NULL;
3002
3003         filename = g_build_filename (ev_application_get_dot_dir (EV_APP, TRUE),
3004                                      EV_PRINT_SETTINGS_FILE, NULL);
3005         data = g_key_file_to_data (key_file, &data_length, NULL);
3006         g_file_set_contents (filename, data, data_length, &error);
3007         if (error) {
3008                 g_warning ("Failed to save print settings: %s", error->message);
3009                 g_error_free (error);
3010         }
3011         g_free (data);
3012         g_free (filename);
3013 }
3014
3015 static void
3016 ev_window_save_print_settings (EvWindow         *window,
3017                                GtkPrintSettings *print_settings)
3018 {
3019         GKeyFile *key_file;
3020         gint      i;
3021
3022         key_file = get_print_settings_file ();
3023         gtk_print_settings_to_key_file (print_settings, key_file, EV_PRINT_SETTINGS_GROUP);
3024
3025         /* Save print settings that are specific to the document */
3026         for (i = 0; i < G_N_ELEMENTS (document_print_settings); i++) {
3027                 /* Remove it from global settings */
3028                 g_key_file_remove_key (key_file, EV_PRINT_SETTINGS_GROUP,
3029                                        document_print_settings[i], NULL);
3030
3031                 if (window->priv->metadata) {
3032                         const gchar *value;
3033
3034                         value = gtk_print_settings_get (print_settings,
3035                                                         document_print_settings[i]);
3036                         ev_metadata_set_string (window->priv->metadata,
3037                                                 document_print_settings[i], value);
3038                 }
3039         }
3040
3041         save_print_setting_file (key_file);
3042         g_key_file_free (key_file);
3043 }
3044
3045 static void
3046 ev_window_save_print_page_setup (EvWindow     *window,
3047                                  GtkPageSetup *page_setup)
3048 {
3049         GKeyFile *key_file;
3050
3051         key_file = get_print_settings_file ();
3052         gtk_page_setup_to_key_file (page_setup, key_file, EV_PAGE_SETUP_GROUP);
3053
3054         /* Do not save document settings in global file */
3055         g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
3056                                "page-setup-orientation", NULL);
3057         g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
3058                                "page-setup-margin-top", NULL);
3059         g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
3060                                "page-setup-margin-bottom", NULL);
3061         g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
3062                                "page-setup-margin-left", NULL);
3063         g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
3064                                "page-setup-margin-right", NULL);
3065
3066         save_print_setting_file (key_file);
3067         g_key_file_free (key_file);
3068
3069         if (!window->priv->metadata)
3070                 return;
3071
3072         /* Save page setup options that are specific to the document */
3073         ev_metadata_set_int (window->priv->metadata, "page-setup-orientation",
3074                              gtk_page_setup_get_orientation (page_setup));
3075         ev_metadata_set_double (window->priv->metadata, "page-setup-margin-top",
3076                                 gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
3077         ev_metadata_set_double (window->priv->metadata, "page-setup-margin-bottom",
3078                                 gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
3079         ev_metadata_set_double (window->priv->metadata, "page-setup-margin-left",
3080                                 gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
3081         ev_metadata_set_double (window->priv->metadata, "page-setup-margin-right",
3082                                 gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
3083 }
3084
3085 static void
3086 ev_window_load_print_settings_from_metadata (EvWindow         *window,
3087                                              GtkPrintSettings *print_settings)
3088 {
3089         gint i;
3090
3091         if (!window->priv->metadata)
3092                 return;
3093
3094         /* Load print setting that are specific to the document */
3095         for (i = 0; i < G_N_ELEMENTS (document_print_settings); i++) {
3096                 gchar *value = NULL;
3097
3098                 ev_metadata_get_string (window->priv->metadata,
3099                                         document_print_settings[i], &value);
3100                 gtk_print_settings_set (print_settings,
3101                                         document_print_settings[i], value);
3102         }
3103 }
3104
3105 static void
3106 ev_window_load_print_page_setup_from_metadata (EvWindow     *window,
3107                                                GtkPageSetup *page_setup)
3108 {
3109         gint          int_value;
3110         gdouble       double_value;
3111         GtkPaperSize *paper_size = gtk_page_setup_get_paper_size (page_setup);
3112
3113         /* Load page setup options that are specific to the document */
3114         if (window->priv->metadata &&
3115             ev_metadata_get_int (window->priv->metadata, "page-setup-orientation", &int_value)) {
3116                 gtk_page_setup_set_orientation (page_setup, int_value);
3117         } else {
3118                 gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_PORTRAIT);
3119         }
3120
3121         if (window->priv->metadata &&
3122             ev_metadata_get_double (window->priv->metadata, "page-setup-margin-top", &double_value)) {
3123                 gtk_page_setup_set_top_margin (page_setup, double_value, GTK_UNIT_MM);
3124         } else {
3125                 gtk_page_setup_set_top_margin (page_setup,
3126                                                gtk_paper_size_get_default_top_margin (paper_size, GTK_UNIT_MM),
3127                                                GTK_UNIT_MM);
3128         }
3129
3130         if (window->priv->metadata &&
3131             ev_metadata_get_double (window->priv->metadata, "page-setup-margin-bottom", &double_value)) {
3132                 gtk_page_setup_set_bottom_margin (page_setup, double_value, GTK_UNIT_MM);
3133         } else {
3134                 gtk_page_setup_set_bottom_margin (page_setup,
3135                                                   gtk_paper_size_get_default_bottom_margin (paper_size, GTK_UNIT_MM),
3136                                                   GTK_UNIT_MM);
3137         }
3138
3139         if (window->priv->metadata &&
3140             ev_metadata_get_double (window->priv->metadata, "page-setup-margin-left", &double_value)) {
3141                 gtk_page_setup_set_left_margin (page_setup, double_value, GTK_UNIT_MM);
3142         } else {
3143                 gtk_page_setup_set_left_margin (page_setup,
3144                                                 gtk_paper_size_get_default_left_margin (paper_size, GTK_UNIT_MM),
3145                                                 GTK_UNIT_MM);
3146         }
3147
3148         if (window->priv->metadata &&
3149             ev_metadata_get_double (window->priv->metadata, "page-setup-margin-right", &double_value)) {
3150                 gtk_page_setup_set_right_margin (page_setup, double_value, GTK_UNIT_MM);
3151         } else {
3152                 gtk_page_setup_set_right_margin (page_setup,
3153                                                  gtk_paper_size_get_default_right_margin (paper_size, GTK_UNIT_MM),
3154                                                  GTK_UNIT_MM);
3155         }
3156 }
3157
3158 static GtkPrintSettings *
3159 get_print_settings (GKeyFile *key_file)
3160 {
3161         GtkPrintSettings *print_settings;
3162
3163         print_settings = g_key_file_has_group (key_file, EV_PRINT_SETTINGS_GROUP) ?
3164                 gtk_print_settings_new_from_key_file (key_file, EV_PRINT_SETTINGS_GROUP, NULL) :
3165                 gtk_print_settings_new ();
3166
3167         return print_settings ? print_settings : gtk_print_settings_new ();
3168 }
3169
3170 static GtkPageSetup *
3171 get_print_page_setup (GKeyFile *key_file)
3172 {
3173         GtkPageSetup *page_setup;
3174
3175         page_setup = g_key_file_has_group (key_file, EV_PAGE_SETUP_GROUP) ?
3176                 gtk_page_setup_new_from_key_file (key_file, EV_PAGE_SETUP_GROUP, NULL) :
3177                 gtk_page_setup_new ();
3178
3179         return page_setup ? page_setup : gtk_page_setup_new ();
3180 }
3181
3182 static void
3183 ev_window_print_cancel (EvWindow *ev_window)
3184 {
3185         EvPrintOperation *op;
3186         
3187         if (!ev_window->priv->print_queue)
3188                 return;
3189
3190         while ((op = g_queue_peek_tail (ev_window->priv->print_queue))) {
3191                 ev_print_operation_cancel (op);
3192         }
3193 }
3194
3195 static void
3196 ev_window_print_update_pending_jobs_message (EvWindow *ev_window,
3197                                              gint      n_jobs)
3198 {
3199         gchar *text = NULL;
3200         
3201         if (!EV_IS_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area) ||
3202             !ev_window->priv->print_queue)
3203                 return;
3204
3205         if (n_jobs == 0) {
3206                 ev_window_set_message_area (ev_window, NULL);
3207                 return;
3208         }
3209         
3210         if (n_jobs > 1) {
3211                 text = g_strdup_printf (ngettext ("%d pending job in queue",
3212                                                   "%d pending jobs in queue",
3213                                                   n_jobs - 1), n_jobs - 1);
3214         }
3215
3216         ev_message_area_set_secondary_text (EV_MESSAGE_AREA (ev_window->priv->message_area),
3217                                             text);
3218         g_free (text);
3219 }
3220
3221 static gboolean
3222 destroy_window (GtkWidget *window)
3223 {
3224         gtk_widget_destroy (window);
3225         
3226         return FALSE;
3227 }
3228
3229 static void
3230 ev_window_print_operation_done (EvPrintOperation       *op,
3231                                 GtkPrintOperationResult result,
3232                                 EvWindow               *ev_window)
3233 {
3234         gint n_jobs;
3235
3236         switch (result) {
3237         case GTK_PRINT_OPERATION_RESULT_APPLY: {
3238                 GtkPrintSettings *print_settings;
3239
3240                 print_settings = ev_print_operation_get_print_settings (op);
3241                 ev_window_save_print_settings (ev_window, print_settings);
3242
3243                 if (ev_print_operation_get_embed_page_setup (op)) {
3244                         GtkPageSetup *page_setup;
3245
3246                         page_setup = ev_print_operation_get_default_page_setup (op);
3247                         ev_window_save_print_page_setup (ev_window, page_setup);
3248                 }
3249         }
3250
3251                 break;
3252         case GTK_PRINT_OPERATION_RESULT_ERROR: {
3253                 GtkWidget *dialog;
3254                 GError    *error = NULL;
3255
3256
3257                 ev_print_operation_get_error (op, &error);
3258                 
3259                 /* The message area is already used by
3260                  * the printing progress, so it's better to
3261                  * use a popup dialog in this case
3262                  */
3263                 dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
3264                                                  GTK_DIALOG_DESTROY_WITH_PARENT,
3265                                                  GTK_MESSAGE_ERROR,
3266                                                  GTK_BUTTONS_CLOSE,
3267                                                  "%s", _("Failed to print document"));
3268                 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
3269                                                           "%s", error->message);
3270                 g_signal_connect (dialog, "response",
3271                                   G_CALLBACK (gtk_widget_destroy),
3272                                   NULL);
3273                 gtk_widget_show (dialog);
3274                 
3275                 g_error_free (error);
3276         }
3277                 break;
3278         case GTK_PRINT_OPERATION_RESULT_CANCEL:
3279         default:
3280                 break;
3281         }
3282
3283         g_queue_remove (ev_window->priv->print_queue, op);
3284         g_object_unref (op);
3285         n_jobs = g_queue_get_length (ev_window->priv->print_queue);
3286         ev_window_print_update_pending_jobs_message (ev_window, n_jobs);
3287
3288         if (n_jobs == 0 && ev_window->priv->close_after_print)
3289                 g_idle_add ((GSourceFunc)destroy_window,
3290                             ev_window);
3291 }
3292
3293 static void
3294 ev_window_print_progress_response_cb (EvProgressMessageArea *area,
3295                                       gint                   response,
3296                                       EvWindow              *ev_window)
3297 {
3298         if (response == GTK_RESPONSE_CANCEL) {
3299                 EvPrintOperation *op;
3300
3301                 op = g_queue_peek_tail (ev_window->priv->print_queue);
3302                 ev_print_operation_cancel (op);
3303         } else {
3304                 gtk_widget_hide (GTK_WIDGET (area));
3305         }
3306 }
3307
3308 static void
3309 ev_window_print_operation_status_changed (EvPrintOperation *op,
3310                                           EvWindow         *ev_window)
3311 {
3312         const gchar *status;
3313         gdouble      fraction;
3314
3315         status = ev_print_operation_get_status (op);
3316         fraction = ev_print_operation_get_progress (op);
3317         
3318         if (!ev_window->priv->message_area) {
3319                 GtkWidget   *area;
3320                 const gchar *job_name;
3321                 gchar       *text;
3322
3323                 job_name = ev_print_operation_get_job_name (op);
3324                 text = g_strdup_printf (_("Printing job “%s”"), job_name);
3325
3326                 area = ev_progress_message_area_new (GTK_STOCK_PRINT,
3327                                                      text,
3328                                                      GTK_STOCK_CLOSE,
3329                                                      GTK_RESPONSE_CLOSE,
3330                                                      GTK_STOCK_CANCEL,
3331                                                      GTK_RESPONSE_CANCEL,
3332                                                      NULL);
3333                 ev_window_print_update_pending_jobs_message (ev_window, 1);
3334                 g_signal_connect (area, "response",
3335                                   G_CALLBACK (ev_window_print_progress_response_cb),
3336                                   ev_window);
3337                 gtk_widget_show (area);
3338                 ev_window_set_message_area (ev_window, area);
3339                 g_free (text);
3340         }
3341
3342         ev_progress_message_area_set_status (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
3343                                              status);
3344         ev_progress_message_area_set_fraction (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
3345                                                fraction);
3346 }
3347
3348 static void
3349 ev_window_print_operation_begin_print (EvPrintOperation *op,
3350                                        EvWindow         *ev_window)
3351 {
3352         if (!ev_window->priv->print_queue)
3353                 ev_window->priv->print_queue = g_queue_new ();
3354
3355         g_queue_push_head (ev_window->priv->print_queue, op);
3356         ev_window_print_update_pending_jobs_message (ev_window,
3357                                                      g_queue_get_length (ev_window->priv->print_queue));
3358 }
3359
3360 void
3361 ev_window_print_range (EvWindow *ev_window,
3362                        gint      first_page,
3363                        gint      last_page)
3364 {
3365         EvPrintOperation *op;
3366         GKeyFile         *print_settings_file;
3367         GtkPrintSettings *print_settings;
3368         GtkPageSetup     *print_page_setup;
3369         gint              current_page;
3370         gint              document_last_page;
3371         gboolean          embed_page_setup;
3372
3373         g_return_if_fail (EV_IS_WINDOW (ev_window));
3374         g_return_if_fail (ev_window->priv->document != NULL);
3375
3376         if (!ev_window->priv->print_queue)
3377                 ev_window->priv->print_queue = g_queue_new ();
3378
3379         op = ev_print_operation_new (ev_window->priv->document);
3380         if (!op) {
3381                 g_warning ("%s", "Printing is not supported for document\n");
3382                 return;
3383         }
3384
3385         g_signal_connect (op, "begin_print",
3386                           G_CALLBACK (ev_window_print_operation_begin_print),
3387                           (gpointer)ev_window);
3388         g_signal_connect (op, "status_changed",
3389                           G_CALLBACK (ev_window_print_operation_status_changed),
3390                           (gpointer)ev_window);
3391         g_signal_connect (op, "done",
3392                           G_CALLBACK (ev_window_print_operation_done),
3393                           (gpointer)ev_window);
3394
3395         current_page = ev_document_model_get_page (ev_window->priv->model);
3396         document_last_page = ev_document_get_n_pages (ev_window->priv->document);
3397
3398         print_settings_file = get_print_settings_file ();
3399
3400         print_settings = get_print_settings (print_settings_file);
3401         ev_window_load_print_settings_from_metadata (ev_window, print_settings);
3402
3403         print_page_setup = get_print_page_setup (print_settings_file);
3404         ev_window_load_print_page_setup_from_metadata (ev_window, print_page_setup);
3405
3406         if (first_page != 1 || last_page != document_last_page) {
3407                 GtkPageRange range;
3408
3409                 /* Ranges in GtkPrint are 0 - N */
3410                 range.start = first_page - 1;
3411                 range.end = last_page - 1;
3412
3413                 gtk_print_settings_set_print_pages (print_settings,
3414                                                     GTK_PRINT_PAGES_RANGES);
3415                 gtk_print_settings_set_page_ranges (print_settings,
3416                                                     &range, 1);
3417         }
3418
3419         ev_print_operation_set_job_name (op, gtk_window_get_title (GTK_WINDOW (ev_window)));
3420         ev_print_operation_set_current_page (op, current_page);
3421         ev_print_operation_set_print_settings (op, print_settings);
3422         ev_print_operation_set_default_page_setup (op, print_page_setup);
3423         embed_page_setup = ev_window->priv->lockdown_settings ?
3424                 !g_settings_get_boolean (ev_window->priv->lockdown_settings,
3425                                          GS_LOCKDOWN_PRINT_SETUP) :
3426                 TRUE;
3427         ev_print_operation_set_embed_page_setup (op, embed_page_setup);
3428
3429         g_object_unref (print_settings);
3430         g_object_unref (print_page_setup);
3431         g_key_file_free (print_settings_file);
3432
3433         ev_print_operation_run (op, GTK_WINDOW (ev_window));
3434 }
3435
3436 static void
3437 ev_window_print (EvWindow *window)
3438 {
3439         ev_window_print_range (window, 1,
3440                                ev_document_get_n_pages (window->priv->document));
3441 }
3442
3443 static void
3444 ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
3445 {
3446         ev_window_print (ev_window);
3447 }
3448
3449 static void
3450 ev_window_cmd_file_properties (GtkAction *action, EvWindow *ev_window)
3451 {
3452         if (ev_window->priv->properties == NULL) {
3453                 ev_window->priv->properties = ev_properties_dialog_new ();
3454                 ev_properties_dialog_set_document (EV_PROPERTIES_DIALOG (ev_window->priv->properties),
3455                                                    ev_window->priv->uri,
3456                                                    ev_window->priv->document);
3457                 g_object_add_weak_pointer (G_OBJECT (ev_window->priv->properties),
3458                                            (gpointer) &(ev_window->priv->properties));
3459                 gtk_window_set_transient_for (GTK_WINDOW (ev_window->priv->properties),
3460                                               GTK_WINDOW (ev_window));
3461         }
3462
3463         ev_document_fc_mutex_lock ();
3464         gtk_widget_show (ev_window->priv->properties);
3465         ev_document_fc_mutex_unlock ();
3466 }
3467
3468 static void
3469 document_modified_confirmation_dialog_response (GtkDialog *dialog,
3470                                                 gint       response,
3471                                                 EvWindow  *ev_window)
3472 {
3473         gtk_widget_destroy (GTK_WIDGET (dialog));
3474
3475         switch (response) {
3476         case GTK_RESPONSE_YES:
3477                 ev_window_cmd_save_as (NULL, ev_window);
3478                 break;
3479         case GTK_RESPONSE_NO:
3480                 gtk_widget_destroy (GTK_WIDGET (ev_window));
3481                 break;
3482         case GTK_RESPONSE_CANCEL:
3483         default:
3484                 break;
3485         }
3486 }
3487
3488 static gboolean
3489 ev_window_check_document_modified (EvWindow *ev_window)
3490 {
3491         EvDocument  *document = ev_window->priv->document;
3492         GtkWidget   *dialog;
3493         gchar       *text, *markup;
3494         const gchar *secondary_text;
3495
3496         if (!document)
3497                 return FALSE;
3498
3499         if (EV_IS_DOCUMENT_FORMS (document) &&
3500             ev_document_forms_document_is_modified (EV_DOCUMENT_FORMS (document))) {
3501                 secondary_text = _("Document contains form fields that have been filled out. "
3502                                    "If you don't save a copy, changes will be permanently lost.");
3503         } else if (EV_IS_DOCUMENT_ANNOTATIONS (document) &&
3504                    ev_document_annotations_document_is_modified (EV_DOCUMENT_ANNOTATIONS (document))) {
3505                 secondary_text = _("Document contains new or modified annotations. "
3506                                    "If you don't save a copy, changes will be permanently lost.");
3507         } else {
3508                 return FALSE;
3509         }
3510
3511
3512         text = g_markup_printf_escaped (_("Save a copy of document “%s” before closing?"),
3513                                         gtk_window_get_title (GTK_WINDOW (ev_window)));
3514
3515         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
3516                                          GTK_DIALOG_MODAL,
3517                                          GTK_MESSAGE_QUESTION,
3518                                          GTK_BUTTONS_NONE,
3519                                          NULL);
3520
3521         markup = g_strdup_printf ("<b>%s</b>", text);
3522         g_free (text);
3523
3524         gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog), markup);
3525         g_free (markup);
3526
3527         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
3528                                                   "%s", secondary_text);
3529
3530         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
3531                                 _("Close _without Saving"),
3532                                 GTK_RESPONSE_NO,
3533                                 GTK_STOCK_CANCEL,
3534                                 GTK_RESPONSE_CANCEL,
3535                                 _("Save a _Copy"),
3536                                 GTK_RESPONSE_YES,
3537                                 NULL);
3538         gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);
3539         gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
3540                                                  GTK_RESPONSE_YES,
3541                                                  GTK_RESPONSE_NO,
3542                                                  GTK_RESPONSE_CANCEL,
3543                                                  -1);
3544
3545         g_signal_connect (dialog, "response",
3546                           G_CALLBACK (document_modified_confirmation_dialog_response),
3547                           ev_window);
3548         gtk_widget_show (dialog);
3549
3550         return TRUE;
3551 }
3552
3553 static void
3554 print_jobs_confirmation_dialog_response (GtkDialog *dialog,
3555                                          gint       response,
3556                                          EvWindow  *ev_window)
3557 {
3558         gtk_widget_destroy (GTK_WIDGET (dialog));       
3559         
3560         switch (response) {
3561         case GTK_RESPONSE_YES:
3562                 if (!ev_window->priv->print_queue ||
3563                     g_queue_is_empty (ev_window->priv->print_queue))
3564                         gtk_widget_destroy (GTK_WIDGET (ev_window));
3565                 else
3566                         ev_window->priv->close_after_print = TRUE;
3567                 break;
3568         case GTK_RESPONSE_NO:
3569                 ev_window->priv->close_after_print = TRUE;
3570                 if (ev_window->priv->print_queue &&
3571                     !g_queue_is_empty (ev_window->priv->print_queue)) {
3572                         gtk_widget_set_sensitive (GTK_WIDGET (ev_window), FALSE);
3573                         ev_window_print_cancel (ev_window);
3574                 } else {
3575                         gtk_widget_destroy (GTK_WIDGET (ev_window));
3576                 }
3577                 break;
3578         case GTK_RESPONSE_CANCEL:
3579         default:
3580                 ev_window->priv->close_after_print = FALSE;
3581         }
3582 }
3583
3584 static gboolean
3585 ev_window_check_print_queue (EvWindow *ev_window)
3586 {
3587         GtkWidget *dialog;
3588         gchar     *text, *markup;
3589         gint       n_print_jobs;
3590
3591         n_print_jobs = ev_window->priv->print_queue ?
3592                 g_queue_get_length (ev_window->priv->print_queue) : 0;
3593
3594         if (n_print_jobs == 0)
3595                 return FALSE;
3596
3597         dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
3598                                          GTK_DIALOG_MODAL,
3599                                          GTK_MESSAGE_QUESTION,
3600                                          GTK_BUTTONS_NONE,
3601                                          NULL);
3602         if (n_print_jobs == 1) {
3603                 EvPrintOperation *op;
3604                 const gchar      *job_name;
3605
3606                 op = g_queue_peek_tail (ev_window->priv->print_queue);
3607                 job_name = ev_print_operation_get_job_name (op);
3608
3609                 text = g_strdup_printf (_("Wait until print job “%s” finishes before closing?"),
3610                                         job_name);
3611         } else {
3612                 text = g_strdup_printf (_("There are %d print jobs active. "
3613                                           "Wait until print finishes before closing?"),
3614                                         n_print_jobs);
3615         }
3616
3617         markup = g_strdup_printf ("<b>%s</b>", text);
3618         g_free (text);
3619
3620         gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog), markup);
3621         g_free (markup);
3622
3623         gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s",
3624                                                   _("If you close the window, pending print "
3625                                                     "jobs will not be printed."));
3626
3627         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
3628                                 _("Cancel _print and Close"),
3629                                 GTK_RESPONSE_NO,
3630                                 GTK_STOCK_CANCEL,
3631                                 GTK_RESPONSE_CANCEL,
3632                                 _("Close _after Printing"),
3633                                 GTK_RESPONSE_YES,
3634                                 NULL);
3635         gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);
3636         gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
3637                                                  GTK_RESPONSE_YES,
3638                                                  GTK_RESPONSE_NO,
3639                                                  GTK_RESPONSE_CANCEL,
3640                                                  -1);
3641
3642         g_signal_connect (dialog, "response",
3643                           G_CALLBACK (print_jobs_confirmation_dialog_response),
3644                           ev_window);
3645         gtk_widget_show (dialog);
3646
3647         return TRUE;
3648 }
3649
3650 static gboolean
3651 ev_window_close (EvWindow *ev_window)
3652 {
3653         if (EV_WINDOW_IS_PRESENTATION (ev_window)) {
3654                 gint current_page;
3655
3656                 /* Save current page */
3657                 current_page = ev_view_presentation_get_current_page (
3658                         EV_VIEW_PRESENTATION (ev_window->priv->presentation_view));
3659                 ev_document_model_set_page (ev_window->priv->model, current_page);
3660         }
3661
3662         if (ev_window_check_document_modified (ev_window))
3663                 return FALSE;
3664
3665         if (ev_window_check_print_queue (ev_window))
3666                 return FALSE;
3667
3668         return TRUE;
3669 }
3670
3671 static void
3672 ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
3673 {
3674         if (ev_window_close (ev_window))
3675                 gtk_widget_destroy (GTK_WIDGET (ev_window));
3676 }
3677
3678 static void
3679 ev_window_cmd_focus_page_selector (GtkAction *act, EvWindow *window)
3680 {
3681         GtkAction *action;
3682         
3683         update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, TRUE);
3684         ev_window_set_action_sensitive (window, "ViewToolbar", FALSE);
3685         update_chrome_visibility (window);
3686         
3687         action = gtk_action_group_get_action (window->priv->action_group,
3688                                               PAGE_SELECTOR_ACTION);
3689         ev_page_action_grab_focus (EV_PAGE_ACTION (action));
3690 }
3691
3692 static void
3693 ev_window_cmd_scroll_forward (GtkAction *action, EvWindow *window)
3694 {
3695         ev_view_scroll (EV_VIEW (window->priv->view), GTK_SCROLL_PAGE_FORWARD, FALSE);
3696 }
3697
3698 static void
3699 ev_window_cmd_scroll_backward (GtkAction *action, EvWindow *window)
3700 {
3701         ev_view_scroll (EV_VIEW (window->priv->view), GTK_SCROLL_PAGE_BACKWARD, FALSE);
3702 }
3703
3704 static void
3705 ev_window_cmd_continuous (GtkAction *action, EvWindow *ev_window)
3706 {
3707         gboolean continuous;
3708
3709         ev_window_stop_presentation (ev_window, TRUE);
3710         continuous = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
3711         ev_document_model_set_continuous (ev_window->priv->model, continuous);
3712 }
3713
3714 static void
3715 ev_window_cmd_dual (GtkAction *action, EvWindow *ev_window)
3716 {
3717         gboolean dual_page;
3718
3719         ev_window_stop_presentation (ev_window, TRUE);
3720         dual_page = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
3721         ev_document_model_set_dual_page (ev_window->priv->model, dual_page);
3722 }
3723
3724 static void
3725 ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
3726 {
3727         ev_window_stop_presentation (ev_window, TRUE);
3728
3729         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
3730                 ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_BEST_FIT);
3731         } else {
3732                 ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_FREE);
3733         }
3734         ev_window_update_actions (ev_window);
3735 }
3736
3737 static void
3738 ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)
3739 {
3740         ev_window_stop_presentation (ev_window, TRUE);
3741
3742         if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
3743                 ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_FIT_WIDTH);
3744         } else {
3745                 ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_FREE);
3746         }
3747         ev_window_update_actions (ev_window);
3748 }
3749
3750
3751 static void
3752 ev_window_cmd_edit_select_all (GtkAction *action, EvWindow *ev_window)
3753 {
3754         g_return_if_fail (EV_IS_WINDOW (ev_window));
3755
3756         ev_view_select_all (EV_VIEW (ev_window->priv->view));
3757 }
3758
3759 static void
3760 ev_window_cmd_edit_find (GtkAction *action, EvWindow *ev_window)
3761 {
3762         g_return_if_fail (EV_IS_WINDOW (ev_window));
3763
3764         if (ev_window->priv->document == NULL || !EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
3765                 g_error ("Find action should be insensitive since document doesn't support find");
3766                 return;
3767         } 
3768
3769         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, TRUE);
3770         update_chrome_visibility (ev_window);
3771         gtk_widget_grab_focus (ev_window->priv->find_bar);
3772 }
3773
3774 static void
3775 ev_window_cmd_edit_find_next (GtkAction *action, EvWindow *ev_window)
3776 {
3777         g_return_if_fail (EV_IS_WINDOW (ev_window));
3778
3779         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, TRUE);
3780         update_chrome_visibility (ev_window);
3781         gtk_widget_grab_focus (ev_window->priv->find_bar);
3782         ev_view_find_next (EV_VIEW (ev_window->priv->view));
3783 }
3784
3785 static void
3786 ev_window_cmd_edit_find_previous (GtkAction *action, EvWindow *ev_window)
3787 {
3788         g_return_if_fail (EV_IS_WINDOW (ev_window));
3789
3790         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, TRUE);
3791         update_chrome_visibility (ev_window);
3792         gtk_widget_grab_focus (ev_window->priv->find_bar);
3793         ev_view_find_previous (EV_VIEW (ev_window->priv->view));
3794 }
3795
3796 static void
3797 ev_window_cmd_edit_copy (GtkAction *action, EvWindow *ev_window)
3798 {
3799         g_return_if_fail (EV_IS_WINDOW (ev_window));
3800
3801         ev_view_copy (EV_VIEW (ev_window->priv->view));
3802 }
3803
3804 static void
3805 ev_window_sidebar_position_change_cb (GObject    *object,
3806                                       GParamSpec *pspec,
3807                                       EvWindow   *ev_window)
3808 {
3809         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window))
3810                 ev_metadata_set_int (ev_window->priv->metadata, "sidebar_size",
3811                                      gtk_paned_get_position (GTK_PANED (object)));
3812 }
3813
3814 static void
3815 ev_window_update_fullscreen_action (EvWindow *window)
3816 {
3817         GtkAction *action;
3818
3819         action = gtk_action_group_get_action (window->priv->action_group, "ViewFullscreen");
3820         g_signal_handlers_block_by_func
3821                 (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
3822         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
3823                                       ev_document_model_get_fullscreen (window->priv->model));
3824         g_signal_handlers_unblock_by_func
3825                 (action, G_CALLBACK (ev_window_cmd_view_fullscreen), window);
3826 }
3827
3828 static void
3829 fullscreen_toolbar_setup_item_properties (GtkUIManager *ui_manager)
3830 {
3831         GtkWidget *item;
3832
3833         item = gtk_ui_manager_get_widget (ui_manager, "/FullscreenToolbar/GoPreviousPage");
3834         g_object_set (item, "is-important", FALSE, NULL);
3835
3836         item = gtk_ui_manager_get_widget (ui_manager, "/FullscreenToolbar/GoNextPage");
3837         g_object_set (item, "is-important", FALSE, NULL);
3838
3839         item = gtk_ui_manager_get_widget (ui_manager, "/FullscreenToolbar/StartPresentation");
3840         g_object_set (item, "is-important", TRUE, NULL);
3841         
3842         item = gtk_ui_manager_get_widget (ui_manager, "/FullscreenToolbar/LeaveFullscreen");
3843         g_object_set (item, "is-important", TRUE, NULL);
3844 }
3845
3846 static void
3847 fullscreen_toolbar_remove_shadow (GtkWidget *toolbar)
3848 {
3849         GtkCssProvider *provider;
3850
3851         gtk_widget_set_name (toolbar, "ev-fullscreen-toolbar");
3852
3853         provider = gtk_css_provider_new ();
3854         gtk_css_provider_load_from_data (provider,
3855                                          "#ev-fullscreen-toolbar {\n"
3856                                          " -GtkToolbar-shadow-type: none; }",
3857                                          -1, NULL);
3858         gtk_style_context_add_provider (gtk_widget_get_style_context (toolbar),
3859                                         GTK_STYLE_PROVIDER (provider),
3860                                         GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
3861         g_object_unref (provider);
3862 }
3863
3864 static void
3865 ev_window_run_fullscreen (EvWindow *window)
3866 {
3867         gboolean fullscreen_window = TRUE;
3868
3869         if (ev_document_model_get_fullscreen (window->priv->model))
3870                 return;
3871         
3872         if (!window->priv->fullscreen_toolbar) {
3873                 window->priv->fullscreen_toolbar =
3874                         gtk_ui_manager_get_widget (window->priv->ui_manager,
3875                                                    "/FullscreenToolbar");
3876
3877                 gtk_toolbar_set_style (GTK_TOOLBAR (window->priv->fullscreen_toolbar),
3878                                        GTK_TOOLBAR_BOTH_HORIZ);
3879                 fullscreen_toolbar_remove_shadow (window->priv->fullscreen_toolbar);
3880                 fullscreen_toolbar_setup_item_properties (window->priv->ui_manager);
3881
3882                 gtk_box_pack_start (GTK_BOX (window->priv->main_box),
3883                                     window->priv->fullscreen_toolbar,
3884                                     FALSE, FALSE, 0);
3885                 gtk_box_reorder_child (GTK_BOX (window->priv->main_box),
3886                                        window->priv->fullscreen_toolbar, 1);
3887         }
3888
3889         if (EV_WINDOW_IS_PRESENTATION (window)) {
3890                 ev_window_stop_presentation (window, FALSE);
3891                 fullscreen_window = FALSE;
3892         }
3893
3894         g_object_set (G_OBJECT (window->priv->scrolled_window),
3895                       "shadow-type", GTK_SHADOW_NONE,
3896                       NULL);
3897
3898         ev_document_model_set_fullscreen (window->priv->model, TRUE);
3899         ev_window_update_fullscreen_action (window);
3900
3901         /* If the user doesn't have the main toolbar he/she won't probably want
3902          * the toolbar in fullscreen mode. See bug #483048
3903          */
3904         update_chrome_flag (window, EV_CHROME_FULLSCREEN_TOOLBAR,
3905                             (window->priv->chrome & EV_CHROME_TOOLBAR) != 0);
3906         update_chrome_visibility (window);
3907
3908         if (fullscreen_window)
3909                 gtk_window_fullscreen (GTK_WINDOW (window));
3910         gtk_widget_grab_focus (window->priv->view);
3911
3912         if (window->priv->metadata && !ev_window_is_empty (window))
3913                 ev_metadata_set_boolean (window->priv->metadata, "fullscreen", TRUE);
3914 }
3915
3916 static void
3917 ev_window_stop_fullscreen (EvWindow *window,
3918                            gboolean  unfullscreen_window)
3919 {
3920         if (!ev_document_model_get_fullscreen (window->priv->model))
3921                 return;
3922
3923         g_object_set (G_OBJECT (window->priv->scrolled_window),
3924                       "shadow-type", GTK_SHADOW_IN,
3925                       NULL);
3926
3927         ev_document_model_set_fullscreen (window->priv->model, FALSE);
3928         ev_window_update_fullscreen_action (window);
3929         update_chrome_flag (window, EV_CHROME_FULLSCREEN_TOOLBAR, FALSE);
3930         update_chrome_visibility (window);
3931         if (unfullscreen_window)
3932                 gtk_window_unfullscreen (GTK_WINDOW (window));
3933
3934         if (window->priv->metadata && !ev_window_is_empty (window))
3935                 ev_metadata_set_boolean (window->priv->metadata, "fullscreen", FALSE);
3936 }
3937
3938 static void
3939 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *window)
3940 {
3941         gboolean fullscreen;
3942
3943         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
3944         if (fullscreen) {
3945                 ev_window_run_fullscreen (window);
3946         } else {
3947                 ev_window_stop_fullscreen (window, TRUE);
3948         }
3949 }
3950
3951 static void
3952 ev_window_update_presentation_action (EvWindow *window)
3953 {
3954         GtkAction *action;
3955
3956         action = gtk_action_group_get_action (window->priv->action_group, "ViewPresentation");
3957         g_signal_handlers_block_by_func
3958                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
3959         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
3960                                       EV_WINDOW_IS_PRESENTATION (window));
3961         g_signal_handlers_unblock_by_func
3962                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
3963 }
3964
3965 static void
3966 ev_window_view_presentation_finished (EvWindow *window)
3967 {
3968         ev_window_stop_presentation (window, TRUE);
3969 }
3970
3971 static void
3972 ev_window_run_presentation (EvWindow *window)
3973 {
3974         gboolean fullscreen_window = TRUE;
3975         guint    current_page;
3976         guint    rotation;
3977         gboolean inverted_colors;
3978
3979         if (EV_WINDOW_IS_PRESENTATION (window))
3980                 return;
3981
3982         if (ev_document_model_get_fullscreen (window->priv->model)) {
3983                 ev_window_stop_fullscreen (window, FALSE);
3984                 fullscreen_window = FALSE;
3985         }
3986
3987         current_page = ev_document_model_get_page (window->priv->model);
3988         rotation = ev_document_model_get_rotation (window->priv->model);
3989         inverted_colors = ev_document_model_get_inverted_colors (window->priv->model);
3990         window->priv->presentation_view = ev_view_presentation_new (window->priv->document,
3991                                                                     current_page,
3992                                                                     rotation,
3993                                                                     inverted_colors);
3994         g_signal_connect_swapped (window->priv->presentation_view, "finished",
3995                                   G_CALLBACK (ev_window_view_presentation_finished),
3996                                   window);
3997         g_signal_connect_swapped (window->priv->presentation_view, "external-link",
3998                                   G_CALLBACK (view_external_link_cb),
3999                                   window);
4000
4001         gtk_box_pack_start (GTK_BOX (window->priv->main_box),
4002                             window->priv->presentation_view,
4003                             TRUE, TRUE, 0);
4004
4005         gtk_widget_hide (window->priv->hpaned);
4006         ev_window_update_presentation_action (window);
4007         update_chrome_visibility (window);
4008
4009         gtk_widget_grab_focus (window->priv->presentation_view);
4010         if (fullscreen_window)
4011                 gtk_window_fullscreen (GTK_WINDOW (window));
4012
4013         gtk_widget_show (window->priv->presentation_view);
4014
4015         ev_application_screensaver_disable (EV_APP);
4016
4017         if (window->priv->metadata && !ev_window_is_empty (window))
4018                 ev_metadata_set_boolean (window->priv->metadata, "presentation", TRUE);
4019 }
4020
4021 static void
4022 ev_window_stop_presentation (EvWindow *window,
4023                              gboolean  unfullscreen_window)
4024 {
4025         guint current_page;
4026
4027         if (!EV_WINDOW_IS_PRESENTATION (window))
4028                 return;
4029
4030         current_page = ev_view_presentation_get_current_page (EV_VIEW_PRESENTATION (window->priv->presentation_view));
4031         ev_document_model_set_page (window->priv->model, current_page);
4032
4033         gtk_container_remove (GTK_CONTAINER (window->priv->main_box),
4034                               window->priv->presentation_view);
4035         window->priv->presentation_view = NULL;
4036
4037         gtk_widget_show (window->priv->hpaned);
4038         ev_window_update_presentation_action (window);
4039         update_chrome_visibility (window);
4040         if (unfullscreen_window)
4041                 gtk_window_unfullscreen (GTK_WINDOW (window));
4042
4043         gtk_widget_grab_focus (window->priv->view);
4044
4045         ev_application_screensaver_enable (EV_APP);
4046
4047         if (window->priv->metadata && !ev_window_is_empty (window))
4048                 ev_metadata_set_boolean (window->priv->metadata, "presentation", FALSE);
4049 }
4050
4051 static void
4052 ev_window_cmd_view_presentation (GtkAction *action, EvWindow *window)
4053 {
4054         gboolean presentation;
4055
4056         presentation = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
4057         if (presentation) {
4058                 ev_window_run_presentation (window);
4059         }
4060 }
4061
4062 static void
4063 ev_window_setup_gtk_settings (EvWindow *window)
4064 {
4065         GtkSettings *settings;
4066         GdkScreen   *screen;
4067         gchar       *menubar_accel_accel;
4068
4069         screen = gtk_window_get_screen (GTK_WINDOW (window));
4070         settings = gtk_settings_get_for_screen (screen);
4071
4072         g_object_get (settings,
4073                       "gtk-menu-bar-accel", &menubar_accel_accel,
4074                       NULL);
4075         if (menubar_accel_accel != NULL && menubar_accel_accel[0] != '\0') {
4076                 gtk_accelerator_parse (menubar_accel_accel,
4077                                        &window->priv->menubar_accel_keyval,
4078                                        &window->priv->menubar_accel_modifier);
4079                 if (window->priv->menubar_accel_keyval == 0) {
4080                         g_warning ("Failed to parse menu bar accelerator '%s'\n",
4081                                    menubar_accel_accel);
4082                 }
4083         } else {
4084                 window->priv->menubar_accel_keyval = 0;
4085                 window->priv->menubar_accel_modifier = 0;
4086         }
4087
4088         g_free (menubar_accel_accel);
4089 }
4090
4091 static void
4092 ev_window_update_max_min_scale (EvWindow *window)
4093 {
4094         gdouble    dpi;
4095         GtkAction *action;
4096         gdouble    min_width, min_height;
4097         gdouble    width, height;
4098         gdouble    max_scale;
4099         gint       rotation = ev_document_model_get_rotation (window->priv->model);
4100
4101         if (!window->priv->document)
4102                 return;
4103
4104         dpi = get_screen_dpi (window) / 72.0;
4105
4106         ev_document_get_min_page_size (window->priv->document, &min_width, &min_height);
4107         width = (rotation == 0 || rotation == 180) ? min_width : min_height;
4108         height = (rotation == 0 || rotation == 180) ? min_height : min_width;
4109         max_scale = sqrt (PAGE_CACHE_SIZE / (width * dpi * 4 * height * dpi));
4110
4111         action = gtk_action_group_get_action (window->priv->action_group,
4112                                               ZOOM_CONTROL_ACTION);
4113         ephy_zoom_action_set_max_zoom_level (EPHY_ZOOM_ACTION (action), max_scale * dpi);
4114
4115         ev_document_model_set_min_scale (window->priv->model, MIN_SCALE * dpi);
4116         ev_document_model_set_max_scale (window->priv->model, max_scale * dpi);
4117 }
4118
4119 static void
4120 ev_window_screen_changed (GtkWidget *widget,
4121                           GdkScreen *old_screen)
4122 {
4123         EvWindow *window = EV_WINDOW (widget);
4124         GdkScreen *screen;
4125
4126         screen = gtk_widget_get_screen (widget);
4127         if (screen == old_screen)
4128                 return;
4129
4130         ev_window_setup_gtk_settings (window);
4131         ev_window_update_max_min_scale (window);
4132
4133         if (GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed) {
4134                 GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed (widget, old_screen);
4135         }
4136 }
4137
4138 static gboolean
4139 ev_window_state_event (GtkWidget           *widget,
4140                        GdkEventWindowState *event)
4141 {
4142         EvWindow *window = EV_WINDOW (widget);
4143
4144         if (GTK_WIDGET_CLASS (ev_window_parent_class)->window_state_event) {
4145                 GTK_WIDGET_CLASS (ev_window_parent_class)->window_state_event (widget, event);
4146         }
4147
4148         if ((event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) == 0)
4149                 return FALSE;
4150
4151         if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) {
4152                 if (ev_document_model_get_fullscreen (window->priv->model) || EV_WINDOW_IS_PRESENTATION (window))
4153                         return FALSE;
4154                 
4155                 ev_window_run_fullscreen (window);
4156         } else {
4157                 if (ev_document_model_get_fullscreen (window->priv->model))
4158                         ev_window_stop_fullscreen (window, FALSE);
4159                 else if (EV_WINDOW_IS_PRESENTATION (window))
4160                         ev_window_stop_presentation (window, FALSE);
4161         }
4162
4163         return FALSE;
4164 }
4165
4166 static void
4167 ev_window_set_page_mode (EvWindow         *window,
4168                          EvWindowPageMode  page_mode)
4169 {
4170         GtkWidget *child = NULL;
4171         GtkWidget *real_child;
4172
4173         if (window->priv->page_mode == page_mode)
4174                 return;
4175
4176         window->priv->page_mode = page_mode;
4177
4178         switch (page_mode) {
4179                 case PAGE_MODE_DOCUMENT:
4180                         child = window->priv->view;
4181                         break;
4182                 case PAGE_MODE_PASSWORD:
4183                         child = window->priv->password_view;
4184                         break;
4185                 default:
4186                         g_assert_not_reached ();
4187         }
4188
4189         real_child = gtk_bin_get_child (GTK_BIN (window->priv->scrolled_window));
4190         if (child != real_child) {
4191                 gtk_container_remove (GTK_CONTAINER (window->priv->scrolled_window),
4192                                       real_child);
4193                 gtk_container_add (GTK_CONTAINER (window->priv->scrolled_window),
4194                                    child);
4195         }
4196         ev_window_update_actions (window);
4197 }
4198
4199
4200 static void
4201 ev_window_cmd_edit_rotate_left (GtkAction *action, EvWindow *ev_window)
4202 {
4203         gint rotation = ev_document_model_get_rotation (ev_window->priv->model);
4204
4205         ev_document_model_set_rotation (ev_window->priv->model, rotation - 90);
4206 }
4207
4208 static void
4209 ev_window_cmd_edit_rotate_right (GtkAction *action, EvWindow *ev_window)
4210 {
4211         gint rotation = ev_document_model_get_rotation (ev_window->priv->model);
4212
4213         ev_document_model_set_rotation (ev_window->priv->model, rotation + 90);
4214 }
4215
4216 static void
4217 ev_window_cmd_view_inverted_colors (GtkAction *action, EvWindow *ev_window)
4218 {
4219         gboolean inverted_colors = ev_document_model_get_inverted_colors (ev_window->priv->model);
4220
4221         ev_document_model_set_inverted_colors (ev_window->priv->model, !inverted_colors);
4222 }
4223
4224 static void
4225 ev_window_cmd_edit_toolbar_cb (GtkDialog *dialog,
4226                                gint       response,
4227                                EvWindow  *ev_window)
4228 {
4229         EggEditableToolbar *toolbar;
4230         gchar              *toolbars_file;
4231
4232         toolbar = EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar);
4233         egg_editable_toolbar_set_edit_mode (toolbar, FALSE);
4234
4235         toolbars_file = g_build_filename (ev_application_get_dot_dir (EV_APP, TRUE),
4236                                           "evince_toolbar.xml", NULL);
4237         egg_toolbars_model_save_toolbars (egg_editable_toolbar_get_model (toolbar),
4238                                           toolbars_file, "1.0");
4239         g_free (toolbars_file);
4240
4241         gtk_widget_destroy (GTK_WIDGET (dialog));
4242 }
4243
4244 static void
4245 ev_window_cmd_edit_toolbar (GtkAction *action, EvWindow *ev_window)
4246 {
4247         GtkWidget          *dialog;
4248         GtkWidget          *editor;
4249         GtkWidget          *content_area;
4250         EggEditableToolbar *toolbar;
4251
4252         dialog = gtk_dialog_new_with_buttons (_("Toolbar Editor"),
4253                                               GTK_WINDOW (ev_window),
4254                                               GTK_DIALOG_DESTROY_WITH_PARENT,
4255                                               GTK_STOCK_CLOSE,
4256                                               GTK_RESPONSE_CLOSE,
4257                                               NULL);
4258         content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
4259         gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
4260         gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)), 5);
4261         gtk_box_set_spacing (GTK_BOX (content_area), 2);
4262         gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 400);
4263
4264         toolbar = EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar);
4265         editor = egg_toolbar_editor_new (ev_window->priv->ui_manager,
4266                                          egg_editable_toolbar_get_model (toolbar));
4267
4268         gtk_container_set_border_width (GTK_CONTAINER (editor), 5);
4269         gtk_box_set_spacing (GTK_BOX (EGG_TOOLBAR_EDITOR (editor)), 5);
4270
4271         gtk_container_add (GTK_CONTAINER (content_area), editor);
4272
4273         egg_editable_toolbar_set_edit_mode (toolbar, TRUE);
4274
4275         g_signal_connect (dialog, "response",
4276                           G_CALLBACK (ev_window_cmd_edit_toolbar_cb),
4277                           ev_window);
4278         gtk_widget_show_all (dialog);
4279 }
4280
4281 static void
4282 ev_window_cmd_edit_save_settings (GtkAction *action, EvWindow *ev_window)
4283 {
4284         EvWindowPrivate *priv = ev_window->priv;
4285         EvDocumentModel *model = priv->model;
4286         GSettings       *settings = priv->default_settings;
4287
4288         g_settings_set_boolean (settings, "continuous",
4289                                 ev_document_model_get_continuous (model));
4290         g_settings_set_boolean (settings, "dual-page",
4291                                 ev_document_model_get_dual_page (model));
4292         g_settings_set_boolean (settings, "fullscreen",
4293                                 ev_document_model_get_fullscreen (model));
4294         g_settings_set_boolean (settings, "inverted-colors",
4295                                 ev_document_model_get_inverted_colors (model));
4296         g_settings_set_enum (settings, "sizing-mode",
4297                              ev_document_model_get_sizing_mode (model));
4298         g_settings_set_boolean (settings, "show-toolbar",
4299                                 gtk_widget_get_visible (priv->toolbar));
4300         g_settings_set_boolean (settings, "show-sidebar",
4301                                 gtk_widget_get_visible (priv->sidebar));
4302         g_settings_set_int (settings, "sidebar-size",
4303                             gtk_paned_get_position (GTK_PANED (priv->hpaned)));
4304         g_settings_set_string (settings, "sidebar-page",
4305                                ev_window_sidebar_get_current_page_id (ev_window));
4306         g_settings_apply (settings);
4307 }
4308
4309 static void
4310 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
4311 {
4312         g_return_if_fail (EV_IS_WINDOW (ev_window));
4313
4314         ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_FREE);
4315         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
4316 }
4317
4318 static void
4319 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
4320 {
4321         g_return_if_fail (EV_IS_WINDOW (ev_window));
4322
4323         ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_FREE);
4324         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
4325 }
4326
4327 static void
4328 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
4329 {
4330         g_return_if_fail (EV_IS_WINDOW (ev_window));
4331
4332         ev_view_previous_page (EV_VIEW (ev_window->priv->view));
4333 }
4334
4335 static void
4336 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
4337 {
4338         g_return_if_fail (EV_IS_WINDOW (ev_window));
4339
4340         ev_view_next_page (EV_VIEW (ev_window->priv->view));
4341 }
4342
4343 static void
4344 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
4345 {
4346         g_return_if_fail (EV_IS_WINDOW (ev_window));
4347
4348         ev_document_model_set_page (ev_window->priv->model, 0);
4349 }
4350
4351 static void
4352 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
4353 {
4354         g_return_if_fail (EV_IS_WINDOW (ev_window));
4355
4356         ev_document_model_set_page (ev_window->priv->model,
4357                                     ev_document_get_n_pages (ev_window->priv->document) - 1);
4358 }
4359
4360 static void
4361 ev_window_cmd_go_forward (GtkAction *action, EvWindow *ev_window)
4362 {
4363         int n_pages, current_page;
4364         
4365         g_return_if_fail (EV_IS_WINDOW (ev_window));
4366
4367         n_pages = ev_document_get_n_pages (ev_window->priv->document);
4368         current_page = ev_document_model_get_page (ev_window->priv->model);
4369         
4370         if (current_page + 10 < n_pages) {
4371                 ev_document_model_set_page (ev_window->priv->model, current_page + 10);
4372         }
4373 }
4374
4375 static void
4376 ev_window_cmd_go_backward (GtkAction *action, EvWindow *ev_window)
4377 {
4378         int current_page;
4379         
4380         g_return_if_fail (EV_IS_WINDOW (ev_window));
4381
4382         current_page = ev_document_model_get_page (ev_window->priv->model);
4383         
4384         if (current_page - 10 >= 0) {
4385                 ev_document_model_set_page (ev_window->priv->model, current_page - 10);
4386         }
4387 }
4388
4389 static void
4390 ev_window_cmd_bookmark_activate (GtkAction *action,
4391                                  EvWindow  *window)
4392 {
4393         guint page = ev_bookmark_action_get_page (EV_BOOKMARK_ACTION (action));
4394
4395         ev_document_model_set_page (window->priv->model, page);
4396 }
4397
4398 static gint
4399 compare_bookmarks (EvBookmark *a,
4400                    EvBookmark *b)
4401 {
4402         return strcmp (a->title, b->title);
4403 }
4404
4405 static void
4406 ev_window_setup_bookmarks (EvWindow *window)
4407 {
4408         GList *items, *l;
4409
4410         if (!window->priv->bookmarks)
4411                 return;
4412
4413         if (window->priv->bookmarks_ui_id > 0) {
4414                 gtk_ui_manager_remove_ui (window->priv->ui_manager,
4415                                           window->priv->bookmarks_ui_id);
4416                 gtk_ui_manager_ensure_update (window->priv->ui_manager);
4417         }
4418         window->priv->bookmarks_ui_id = gtk_ui_manager_new_merge_id (window->priv->ui_manager);
4419
4420         if (window->priv->bookmarks_action_group) {
4421                 gtk_ui_manager_remove_action_group (window->priv->ui_manager,
4422                                                     window->priv->bookmarks_action_group);
4423                 g_object_unref (window->priv->bookmarks_action_group);
4424         }
4425         window->priv->bookmarks_action_group = gtk_action_group_new ("BookmarksActions");
4426         gtk_ui_manager_insert_action_group (window->priv->ui_manager,
4427                                             window->priv->bookmarks_action_group, -1);
4428
4429         items = ev_bookmarks_get_bookmarks (window->priv->bookmarks);
4430         items = g_list_sort (items, (GCompareFunc)compare_bookmarks);
4431
4432         for (l = items; l && l->data; l = g_list_next (l)) {
4433                 EvBookmark *bm = (EvBookmark *)l->data;
4434                 GtkAction  *action;
4435
4436                 action = ev_bookmark_action_new (bm);
4437                 g_signal_connect (action, "activate",
4438                                   G_CALLBACK (ev_window_cmd_bookmark_activate),
4439                                   window);
4440                 gtk_action_group_add_action (window->priv->bookmarks_action_group,
4441                                              action);
4442
4443                 gtk_ui_manager_add_ui (window->priv->ui_manager,
4444                                        window->priv->bookmarks_ui_id,
4445                                        "/MainMenu/BookmarksMenu/BookmarksItems",
4446                                        gtk_action_get_label (action),
4447                                        gtk_action_get_name (action),
4448                                        GTK_UI_MANAGER_MENUITEM,
4449                                        FALSE);
4450
4451                 g_object_unref (action);
4452         }
4453
4454         g_list_free (items);
4455 }
4456
4457 static void
4458 ev_window_cmd_bookmarks_add (GtkAction *action,
4459                              EvWindow  *window)
4460 {
4461         EvBookmark bm;
4462         gchar     *page_label;
4463         gchar     *page_title;
4464
4465         bm.page = ev_document_model_get_page (window->priv->model);
4466         page_label = ev_document_get_page_label (window->priv->document, bm.page);
4467         page_title = ev_window_get_page_title (window, page_label);
4468         bm.title = page_title ? page_title : g_strdup_printf (_("Page %s"), page_label);
4469         g_free (page_label);
4470
4471         /* EvBookmarks takes ownership of bookmark */
4472         ev_bookmarks_add (window->priv->bookmarks, &bm);
4473 }
4474
4475 static void
4476 ev_window_cmd_view_reload (GtkAction *action, EvWindow *ev_window)
4477 {
4478         ev_window_reload_document (ev_window, NULL);
4479 }
4480
4481 static void
4482 ev_window_cmd_view_autoscroll (GtkAction *action, EvWindow *ev_window)
4483 {
4484         ev_view_autoscroll_start (EV_VIEW (ev_window->priv->view));
4485 }
4486
4487 #if OFFLINE_HELP_ENABLED
4488 #define EV_HELP "ghelp:evince"
4489 #else
4490 #define EV_HELP "http://library.gnome.org/users/evince/stable/"
4491 #endif
4492
4493 static void
4494 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
4495 {
4496         GError  *error = NULL;
4497
4498         gtk_show_uri (gtk_window_get_screen (GTK_WINDOW (ev_window)),
4499                       EV_HELP,
4500                       gtk_get_current_event_time (),
4501                       &error);
4502         if (error) {
4503                 ev_window_error_message (ev_window, error, 
4504                                          "%s", _("There was an error displaying help"));
4505                 g_error_free (error);
4506         }
4507 }
4508
4509 static void
4510 ev_window_cmd_leave_fullscreen (GtkAction *action, EvWindow *window)
4511 {
4512         ev_window_stop_fullscreen (window, TRUE);
4513 }
4514
4515 static void
4516 ev_window_cmd_start_presentation (GtkAction *action, EvWindow *window)
4517 {
4518         ev_window_run_presentation (window);
4519 }
4520
4521 static void
4522 ev_window_cmd_escape (GtkAction *action, EvWindow *window)
4523 {
4524         GtkWidget *widget;
4525
4526         ev_view_autoscroll_stop (EV_VIEW (window->priv->view));
4527         
4528         widget = gtk_window_get_focus (GTK_WINDOW (window));
4529         if (widget && gtk_widget_get_ancestor (widget, EGG_TYPE_FIND_BAR)) {
4530                 update_chrome_flag (window, EV_CHROME_FINDBAR, FALSE);
4531                 update_chrome_visibility (window);
4532                 gtk_widget_grab_focus (window->priv->view);
4533         } else {
4534                 gboolean fullscreen;
4535
4536                 fullscreen = ev_document_model_get_fullscreen (window->priv->model);
4537
4538                 if (fullscreen) {
4539                         ev_window_stop_fullscreen (window, TRUE);
4540                 } else if (EV_WINDOW_IS_PRESENTATION (window)) {
4541                         ev_window_stop_presentation (window, TRUE);
4542                         gtk_widget_grab_focus (window->priv->view);
4543                 } else {
4544                         gtk_widget_grab_focus (window->priv->view);
4545                 }
4546
4547                 if (fullscreen && EV_WINDOW_IS_PRESENTATION (window))
4548                         g_warning ("Both fullscreen and presentation set somehow");
4549         }
4550 }
4551
4552 static void
4553 save_sizing_mode (EvWindow *window)
4554 {
4555         EvSizingMode mode;
4556         GEnumValue *enum_value;
4557
4558         if (!window->priv->metadata || ev_window_is_empty (window))
4559                 return;
4560
4561         mode = ev_document_model_get_sizing_mode (window->priv->model);
4562         enum_value = g_enum_get_value (g_type_class_peek (EV_TYPE_SIZING_MODE), mode);
4563         ev_metadata_set_string (window->priv->metadata, "sizing_mode",
4564                                 enum_value->value_nick);
4565 }
4566
4567 static void
4568 ev_window_document_changed_cb (EvDocumentModel *model,
4569                                GParamSpec      *pspec,
4570                                EvWindow        *ev_window)
4571 {
4572         ev_window_set_document (ev_window,
4573                                 ev_document_model_get_document (model));
4574 }
4575
4576 static void
4577 ev_window_sizing_mode_changed_cb (EvDocumentModel *model,
4578                                   GParamSpec      *pspec,
4579                                   EvWindow        *ev_window)
4580 {
4581         EvSizingMode sizing_mode = ev_document_model_get_sizing_mode (model);
4582
4583         g_object_set (ev_window->priv->scrolled_window,
4584                       "hscrollbar-policy",
4585                       sizing_mode == EV_SIZING_FREE ?
4586                       GTK_POLICY_AUTOMATIC : GTK_POLICY_NEVER,
4587                       "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
4588                       NULL);
4589
4590         update_sizing_buttons (ev_window);
4591         save_sizing_mode (ev_window);
4592 }
4593
4594 static void
4595 ev_window_zoom_changed_cb (EvDocumentModel *model, GParamSpec *pspec, EvWindow *ev_window)
4596 {
4597         ev_window_update_actions (ev_window);
4598
4599         if (!ev_window->priv->metadata)
4600                 return;
4601
4602         if (ev_document_model_get_sizing_mode (model) == EV_SIZING_FREE && !ev_window_is_empty (ev_window)) {
4603                 gdouble zoom;
4604
4605                 zoom = ev_document_model_get_scale (model);
4606                 zoom *= 72.0 / get_screen_dpi (ev_window);
4607                 ev_metadata_set_double (ev_window->priv->metadata, "zoom", zoom);
4608         }
4609 }
4610
4611 static void
4612 ev_window_update_continuous_action (EvWindow *window)
4613 {
4614         GtkAction *action;
4615
4616         action = gtk_action_group_get_action (window->priv->action_group, "ViewContinuous");
4617         g_signal_handlers_block_by_func
4618                 (action, G_CALLBACK (ev_window_cmd_continuous), window);
4619         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
4620                                       ev_document_model_get_continuous (window->priv->model));
4621         g_signal_handlers_unblock_by_func
4622                 (action, G_CALLBACK (ev_window_cmd_continuous), window);
4623 }
4624
4625 static void
4626 ev_window_update_dual_page_action (EvWindow *window)
4627 {
4628         GtkAction *action;
4629
4630         action = gtk_action_group_get_action (window->priv->action_group, "ViewDual");
4631         g_signal_handlers_block_by_func
4632                 (action, G_CALLBACK (ev_window_cmd_dual), window);
4633         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
4634                                       ev_document_model_get_dual_page (window->priv->model));
4635         g_signal_handlers_unblock_by_func
4636                 (action, G_CALLBACK (ev_window_cmd_dual), window);
4637 }
4638
4639 static void
4640 ev_window_continuous_changed_cb (EvDocumentModel *model,
4641                                  GParamSpec      *pspec,
4642                                  EvWindow        *ev_window)
4643 {
4644         ev_window_update_continuous_action (ev_window);
4645
4646         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window))
4647                 ev_metadata_set_boolean (ev_window->priv->metadata, "continuous",
4648                                          ev_document_model_get_continuous (model));
4649 }
4650
4651 static void
4652 ev_window_rotation_changed_cb (EvDocumentModel *model,
4653                                GParamSpec      *pspec,
4654                                EvWindow        *window)
4655 {
4656         gint rotation = ev_document_model_get_rotation (model);
4657
4658         if (window->priv->metadata && !ev_window_is_empty (window))
4659                 ev_metadata_set_int (window->priv->metadata, "rotation",
4660                                      rotation);
4661
4662         ev_window_update_max_min_scale (window);
4663         ev_window_refresh_window_thumbnail (window);
4664 }
4665
4666 static void
4667 ev_window_update_inverted_colors_action (EvWindow *window)
4668 {
4669         GtkAction *action;
4670
4671         action = gtk_action_group_get_action (window->priv->action_group, "ViewInvertedColors");
4672         g_signal_handlers_block_by_func
4673                 (action, G_CALLBACK (ev_window_cmd_view_inverted_colors), window);
4674         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
4675                                       ev_document_model_get_inverted_colors (window->priv->model));
4676         g_signal_handlers_unblock_by_func
4677                 (action, G_CALLBACK (ev_window_cmd_view_inverted_colors), window);
4678 }
4679
4680 static void
4681 ev_window_inverted_colors_changed_cb (EvDocumentModel *model,
4682                                       GParamSpec      *pspec,
4683                                       EvWindow        *window)
4684 {
4685         gboolean inverted_colors = ev_document_model_get_inverted_colors (model);
4686
4687         ev_window_update_inverted_colors_action (window);
4688
4689         if (window->priv->metadata && !ev_window_is_empty (window))
4690                 ev_metadata_set_boolean (window->priv->metadata, "inverted-colors",
4691                                          inverted_colors);
4692
4693         ev_window_refresh_window_thumbnail (window);
4694 }
4695
4696 static void
4697 ev_window_dual_mode_changed_cb (EvDocumentModel *model,
4698                                 GParamSpec      *pspec,
4699                                 EvWindow        *ev_window)
4700 {
4701         ev_window_update_dual_page_action (ev_window);
4702
4703         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window))
4704                 ev_metadata_set_boolean (ev_window->priv->metadata, "dual-page",
4705                                          ev_document_model_get_dual_page (model));
4706 }
4707
4708 static char *
4709 build_comments_string (EvDocument *document)
4710 {
4711         gchar *comments = NULL;
4712         EvDocumentBackendInfo info;
4713
4714         if (document && ev_document_get_backend_info (document, &info)) {
4715                 comments = g_strdup_printf (
4716                         _("Document Viewer\nUsing %s (%s)"),
4717                         info.name, info.version);
4718         } else {
4719                 comments = g_strdup_printf (
4720                         _("Document Viewer"));
4721         }
4722
4723         return comments;
4724 }
4725
4726 static void
4727 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
4728 {
4729         const char *authors[] = {
4730                 "Martin Kretzschmar <m_kretzschmar@gmx.net>",
4731                 "Jonathan Blandford <jrb@gnome.org>",
4732                 "Marco Pesenti Gritti <marco@gnome.org>",
4733                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
4734                 "Bryan Clark <clarkbw@gnome.org>",
4735                 "Carlos Garcia Campos <carlosgc@gnome.org>",
4736                 "Wouter Bolsterlee <wbolster@gnome.org>",
4737                 "Christian Persch <chpe" "\100" "gnome.org>",
4738                 NULL
4739         };
4740
4741         const char *documenters[] = {
4742                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
4743                 NULL
4744         };
4745
4746         const char *license[] = {
4747                 N_("Evince is free software; you can redistribute it and/or modify "
4748                    "it under the terms of the GNU General Public License as published by "
4749                    "the Free Software Foundation; either version 2 of the License, or "
4750                    "(at your option) any later version.\n"),
4751                 N_("Evince is distributed in the hope that it will be useful, "
4752                    "but WITHOUT ANY WARRANTY; without even the implied warranty of "
4753                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
4754                    "GNU General Public License for more details.\n"),
4755                 N_("You should have received a copy of the GNU General Public License "
4756                    "along with Evince; if not, write to the Free Software Foundation, Inc., "
4757                    "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA\n")
4758         };
4759
4760         char *license_trans;
4761         char *comments;
4762
4763 #ifdef ENABLE_NLS
4764         const char **p;
4765
4766         for (p = authors; *p; ++p)
4767                 *p = _(*p);
4768
4769         for (p = documenters; *p; ++p)
4770                 *p = _(*p);
4771 #endif
4772
4773         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
4774                                      _(license[2]), "\n", NULL);
4775
4776         comments = build_comments_string (ev_window->priv->document);
4777
4778         gtk_show_about_dialog (
4779                 GTK_WINDOW (ev_window),
4780                 "name", _("Evince"),
4781                 "version", VERSION,
4782                 "copyright",
4783                 _("© 1996–2010 The Evince authors"),
4784                 "license", license_trans,
4785                 "website", "http://www.gnome.org/projects/evince",
4786                 "comments", comments,
4787                 "authors", authors,
4788                 "documenters", documenters,
4789                 "translator-credits", _("translator-credits"),
4790                 "logo-icon-name", "evince",
4791                 "wrap-license", TRUE,
4792                 NULL);
4793
4794         g_free (comments);
4795         g_free (license_trans);
4796 }
4797
4798 static void
4799 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
4800 {
4801         gboolean active;
4802         
4803         active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
4804         update_chrome_flag (ev_window, EV_CHROME_TOOLBAR, active);
4805         update_chrome_visibility (ev_window);
4806         if (ev_window->priv->metadata)
4807                 ev_metadata_set_boolean (ev_window->priv->metadata, "show_toolbar", active);
4808 }
4809
4810 static void
4811 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
4812 {
4813         if (EV_WINDOW_IS_PRESENTATION (ev_window))
4814                 return;
4815             
4816         update_chrome_flag (ev_window, EV_CHROME_SIDEBAR,
4817                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
4818         update_chrome_visibility (ev_window);
4819 }
4820
4821 static void
4822 ev_window_sidebar_current_page_changed_cb (EvSidebar  *ev_sidebar,
4823                                            GParamSpec *pspec,
4824                                            EvWindow   *ev_window)
4825 {
4826         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window)) {
4827                 ev_metadata_set_string (ev_window->priv->metadata,
4828                                         "sidebar_page",
4829                                         ev_window_sidebar_get_current_page_id (ev_window));
4830         }
4831 }
4832
4833 static void
4834 ev_window_sidebar_visibility_changed_cb (EvSidebar  *ev_sidebar,
4835                                          GParamSpec *pspec,
4836                                          EvWindow   *ev_window)
4837 {
4838         GtkAction *action;
4839
4840         action = gtk_action_group_get_action (ev_window->priv->action_group, "ViewSidebar");
4841
4842         if (!EV_WINDOW_IS_PRESENTATION (ev_window)) {
4843                 gboolean visible = gtk_widget_get_visible (GTK_WIDGET (ev_sidebar));
4844
4845                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), visible);
4846
4847                 if (ev_window->priv->metadata)
4848                         ev_metadata_set_boolean (ev_window->priv->metadata, "sidebar_visibility",
4849                                                  visible);
4850         }
4851 }
4852
4853 static void
4854 view_menu_link_popup (EvWindow *ev_window,
4855                       EvLink   *link)
4856 {
4857         gboolean   show_external = FALSE;
4858         gboolean   show_internal = FALSE;
4859         GtkAction *action;
4860         
4861         if (ev_window->priv->link)
4862                 g_object_unref (ev_window->priv->link);
4863         
4864         if (link)
4865                 ev_window->priv->link = g_object_ref (link);
4866         else    
4867                 ev_window->priv->link = NULL;
4868
4869         if (ev_window->priv->link) {
4870                 EvLinkAction *ev_action;
4871
4872                 ev_action = ev_link_get_action (link);
4873                 if (ev_action) {
4874                         switch (ev_link_action_get_action_type (ev_action)) {
4875                                 case EV_LINK_ACTION_TYPE_GOTO_DEST:
4876                                 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
4877                                         show_internal = TRUE;
4878                                         break;
4879                                 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
4880                                 case EV_LINK_ACTION_TYPE_LAUNCH:
4881                                         show_external = TRUE;
4882                                         break;
4883                                 default:
4884                                         break;
4885                         }
4886                 }
4887         }
4888         
4889         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4890                                               "OpenLink");
4891         gtk_action_set_visible (action, show_external);
4892
4893         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4894                                               "CopyLinkAddress");
4895         gtk_action_set_visible (action, show_external);
4896
4897         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4898                                               "GoLink");
4899         gtk_action_set_visible (action, show_internal);
4900
4901         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4902                                               "OpenLinkNewWindow");
4903         gtk_action_set_visible (action, show_internal);
4904 }
4905
4906 static void
4907 view_menu_image_popup (EvWindow  *ev_window,
4908                        EvImage   *image)
4909 {
4910         GtkAction *action;
4911         gboolean   show_image = FALSE;
4912         
4913         if (ev_window->priv->image)
4914                 g_object_unref (ev_window->priv->image);
4915         
4916         if (image)
4917                 ev_window->priv->image = g_object_ref (image);
4918         else    
4919                 ev_window->priv->image = NULL;
4920
4921         show_image = (ev_window->priv->image != NULL);
4922         
4923         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4924                                               "SaveImageAs");
4925         gtk_action_set_visible (action, show_image);
4926
4927         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4928                                               "CopyImage");
4929         gtk_action_set_visible (action, show_image);
4930 }
4931
4932 static void
4933 view_menu_annot_popup (EvWindow     *ev_window,
4934                        EvAnnotation *annot)
4935 {
4936         GtkAction *action;
4937         gboolean   show_annot = FALSE;
4938
4939         if (ev_window->priv->annot)
4940                 g_object_unref (ev_window->priv->annot);
4941         ev_window->priv->annot = (annot) ? g_object_ref (annot) : NULL;
4942
4943         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4944                                               "AnnotProperties");
4945         gtk_action_set_visible (action, (annot != NULL && EV_IS_ANNOTATION_MARKUP (annot)));
4946
4947         if (annot && EV_IS_ANNOTATION_ATTACHMENT (annot)) {
4948                 EvAttachment *attachment;
4949
4950                 attachment = ev_annotation_attachment_get_attachment (EV_ANNOTATION_ATTACHMENT (annot));
4951                 if (attachment) {
4952                         show_annot = TRUE;
4953                         if (ev_window->priv->attach_list) {
4954                                 g_list_foreach (ev_window->priv->attach_list,
4955                                                 (GFunc) g_object_unref, NULL);
4956                                 g_list_free (ev_window->priv->attach_list);
4957                                 ev_window->priv->attach_list = NULL;
4958                         }
4959                         ev_window->priv->attach_list =
4960                                 g_list_prepend (ev_window->priv->attach_list,
4961                                                 g_object_ref (attachment));
4962                 }
4963         }
4964
4965         action = gtk_action_group_get_action (ev_window->priv->attachment_popup_action_group,
4966                                               "OpenAttachment");
4967         gtk_action_set_visible (action, show_annot);
4968
4969         action = gtk_action_group_get_action (ev_window->priv->attachment_popup_action_group,
4970                                               "SaveAttachmentAs");
4971         gtk_action_set_visible (action, show_annot);
4972 }
4973
4974 static gboolean
4975 view_menu_popup_cb (EvView   *view,
4976                     GList    *items,
4977                     EvWindow *ev_window)
4978 {
4979         GList   *l;
4980         gboolean has_link = FALSE;
4981         gboolean has_image = FALSE;
4982         gboolean has_annot = FALSE;
4983
4984         for (l = items; l; l = g_list_next (l)) {
4985                 if (EV_IS_LINK (l->data)) {
4986                         view_menu_link_popup (ev_window, EV_LINK (l->data));
4987                         has_link = TRUE;
4988                 } else if (EV_IS_IMAGE (l->data)) {
4989                         view_menu_image_popup (ev_window, EV_IMAGE (l->data));
4990                         has_image = TRUE;
4991                 } else if (EV_IS_ANNOTATION (l->data)) {
4992                         view_menu_annot_popup (ev_window, EV_ANNOTATION (l->data));
4993                         has_annot = TRUE;
4994                 }
4995         }
4996
4997         if (!has_link)
4998                 view_menu_link_popup (ev_window, NULL);
4999         if (!has_image)
5000                 view_menu_image_popup (ev_window, NULL);
5001         if (!has_annot)
5002                 view_menu_annot_popup (ev_window, NULL);
5003
5004         gtk_menu_popup (GTK_MENU (ev_window->priv->view_popup),
5005                         NULL, NULL, NULL, NULL,
5006                         3, gtk_get_current_event_time ());
5007         return TRUE;
5008 }
5009
5010 static gboolean
5011 attachment_bar_menu_popup_cb (EvSidebarAttachments *attachbar,
5012                               GList           *attach_list,
5013                               EvWindow        *ev_window)
5014 {
5015         GtkWidget *popup;
5016
5017         g_assert (attach_list != NULL);
5018
5019         if (ev_window->priv->attach_list) {
5020                 g_list_foreach (ev_window->priv->attach_list,
5021                                 (GFunc) g_object_unref, NULL);
5022                 g_list_free (ev_window->priv->attach_list);
5023         }
5024         
5025         ev_window->priv->attach_list = attach_list;
5026         
5027         popup = ev_window->priv->attachment_popup;
5028
5029         gtk_menu_popup (GTK_MENU (popup), NULL, NULL,
5030                         NULL, NULL,
5031                         3, gtk_get_current_event_time ());
5032
5033         return TRUE;
5034 }
5035
5036 static void
5037 ev_window_update_find_status_message (EvWindow *ev_window)
5038 {
5039         gchar *message;
5040
5041         if (!ev_window->priv->find_job)
5042                 return;
5043         
5044         if (ev_job_is_finished (ev_window->priv->find_job)) {
5045                 EvJobFind *job_find = EV_JOB_FIND (ev_window->priv->find_job);
5046
5047                 if (ev_job_find_has_results (job_find)) {
5048                         gint n_results;
5049
5050                         n_results = ev_job_find_get_n_results (job_find,
5051                                                                ev_document_model_get_page (ev_window->priv->model));
5052                         /* TRANS: Sometimes this could be better translated as
5053                            "%d hit(s) on this page".  Therefore this string
5054                            contains plural cases. */
5055                         message = g_strdup_printf (ngettext ("%d found on this page",
5056                                                              "%d found on this page",
5057                                                              n_results),
5058                                                    n_results);
5059                 } else {
5060                         message = g_strdup (_("Not found"));
5061                 }
5062         } else {
5063                 gdouble percent;
5064
5065                 percent = ev_job_find_get_progress (EV_JOB_FIND (ev_window->priv->find_job));
5066                 message = g_strdup_printf (_("%3d%% remaining to search"),
5067                                            (gint) ((1.0 - percent) * 100));
5068         }
5069         
5070         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar), message);
5071         g_free (message);
5072 }
5073
5074 static void
5075 ev_window_find_job_finished_cb (EvJobFind *job,
5076                                 EvWindow  *ev_window)
5077 {
5078         ev_window_update_find_status_message (ev_window);
5079 }
5080
5081 static void
5082 ev_window_find_job_updated_cb (EvJobFind *job,
5083                                gint       page,
5084                                EvWindow  *ev_window)
5085 {
5086         ev_window_update_actions (ev_window);
5087         
5088         ev_view_find_changed (EV_VIEW (ev_window->priv->view),
5089                               ev_job_find_get_results (job),
5090                               page);
5091         ev_window_update_find_status_message (ev_window);
5092 }
5093
5094 static void
5095 ev_window_clear_find_job (EvWindow *ev_window)
5096 {
5097         if (ev_window->priv->find_job != NULL) {
5098                 if (!ev_job_is_finished (ev_window->priv->find_job))
5099                         ev_job_cancel (ev_window->priv->find_job);
5100
5101                 g_signal_handlers_disconnect_by_func (ev_window->priv->find_job,
5102                                                       ev_window_find_job_finished_cb,
5103                                                       ev_window);
5104                 g_signal_handlers_disconnect_by_func (ev_window->priv->find_job,
5105                                                       ev_window_find_job_updated_cb,
5106                                                       ev_window);
5107                 g_object_unref (ev_window->priv->find_job);
5108                 ev_window->priv->find_job = NULL;
5109         }
5110 }
5111
5112 static void
5113 find_bar_previous_cb (EggFindBar *find_bar,
5114                       EvWindow   *ev_window)
5115 {
5116         ev_view_find_previous (EV_VIEW (ev_window->priv->view));
5117 }
5118
5119 static void
5120 find_bar_next_cb (EggFindBar *find_bar,
5121                   EvWindow   *ev_window)
5122 {
5123         ev_view_find_next (EV_VIEW (ev_window->priv->view));
5124 }
5125
5126 static void
5127 find_bar_close_cb (EggFindBar *find_bar,
5128                    EvWindow   *ev_window)
5129 {
5130         ev_view_find_cancel (EV_VIEW (ev_window->priv->view));
5131         ev_window_clear_find_job (ev_window);
5132         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, FALSE);
5133         update_chrome_visibility (ev_window);
5134 }
5135
5136 static void
5137 find_bar_search_changed_cb (EggFindBar *find_bar,
5138                             GParamSpec *param,
5139                             EvWindow   *ev_window)
5140 {
5141         gboolean case_sensitive;
5142         const char *search_string;
5143
5144         if (!ev_window->priv->document || !EV_IS_DOCUMENT_FIND (ev_window->priv->document))
5145                 return;
5146         
5147         /* Either the string or case sensitivity could have changed. */
5148         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
5149         search_string = egg_find_bar_get_search_string (find_bar);
5150
5151         ev_view_find_search_changed (EV_VIEW (ev_window->priv->view));
5152
5153         ev_window_clear_find_job (ev_window);
5154
5155         if (search_string && search_string[0]) {
5156                 ev_window->priv->find_job = ev_job_find_new (ev_window->priv->document,
5157                                                              ev_document_model_get_page (ev_window->priv->model),
5158                                                              ev_document_get_n_pages (ev_window->priv->document),
5159                                                              search_string,
5160                                                              case_sensitive);
5161                 g_signal_connect (ev_window->priv->find_job, "finished",
5162                                   G_CALLBACK (ev_window_find_job_finished_cb),
5163                                   ev_window);
5164                 g_signal_connect (ev_window->priv->find_job, "updated",
5165                                   G_CALLBACK (ev_window_find_job_updated_cb),
5166                                   ev_window);
5167                 ev_job_scheduler_push_job (ev_window->priv->find_job, EV_JOB_PRIORITY_NONE);
5168         } else {
5169                 ev_window_update_actions (ev_window);
5170                 egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
5171                                               NULL);
5172                 gtk_widget_queue_draw (GTK_WIDGET (ev_window->priv->view));
5173         }
5174 }
5175
5176 static void
5177 find_bar_visibility_changed_cb (EggFindBar *find_bar,
5178                                 GParamSpec *param,
5179                                 EvWindow   *ev_window)
5180 {
5181         gboolean visible;
5182
5183         visible = gtk_widget_get_visible (GTK_WIDGET (find_bar));
5184
5185         if (ev_window->priv->document &&
5186             EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
5187                 ev_view_find_set_highlight_search (EV_VIEW (ev_window->priv->view), visible);
5188                 ev_view_find_search_changed (EV_VIEW (ev_window->priv->view));
5189                 ev_window_update_actions (ev_window);
5190
5191                 if (visible)
5192                         find_bar_search_changed_cb (find_bar, NULL, ev_window);
5193                 else
5194                         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar), NULL);
5195         }
5196 }
5197
5198 static void
5199 find_bar_scroll (EggFindBar   *find_bar,
5200                  GtkScrollType scroll,
5201                  EvWindow     *ev_window)
5202 {
5203         ev_view_scroll (EV_VIEW (ev_window->priv->view), scroll, FALSE);
5204 }
5205
5206 static void
5207 zoom_control_changed_cb (EphyZoomAction *action,
5208                          float           zoom,
5209                          EvWindow       *ev_window)
5210 {
5211         EvSizingMode mode;
5212
5213         if (zoom == EPHY_ZOOM_BEST_FIT) {
5214                 mode = EV_SIZING_BEST_FIT;
5215         } else if (zoom == EPHY_ZOOM_FIT_WIDTH) {
5216                 mode = EV_SIZING_FIT_WIDTH;
5217         } else {
5218                 mode = EV_SIZING_FREE;
5219         }
5220
5221         ev_document_model_set_sizing_mode (ev_window->priv->model, mode);
5222
5223         if (mode == EV_SIZING_FREE) {
5224                 ev_document_model_set_scale (ev_window->priv->model,
5225                                              zoom * get_screen_dpi (ev_window) / 72.0);
5226         }
5227 }
5228
5229 static void
5230 ev_window_drag_data_received (GtkWidget        *widget,
5231                               GdkDragContext   *context,
5232                               gint              x,
5233                               gint              y,
5234                               GtkSelectionData *selection_data,
5235                               guint             info,
5236                               guint             time)
5237
5238 {
5239         EvWindow  *window = EV_WINDOW (widget);
5240         gchar    **uris;
5241         gint       i = 0;
5242         GSList    *uri_list = NULL;
5243         GtkWidget *source;
5244
5245         source = gtk_drag_get_source_widget (context);
5246         if (source && widget == gtk_widget_get_toplevel (source)) {
5247                 gtk_drag_finish (context, FALSE, FALSE, time);
5248                 return;
5249         }
5250
5251         uris = gtk_selection_data_get_uris (selection_data);
5252         if (!uris) {
5253                 gtk_drag_finish (context, FALSE, FALSE, time);
5254                 return;
5255         }
5256
5257         for (i = 0; uris[i]; i++) {
5258                 uri_list = g_slist_prepend (uri_list, (gpointer) uris[i]);
5259         }
5260
5261         ev_application_open_uri_list (EV_APP, uri_list,
5262                                       gtk_window_get_screen (GTK_WINDOW (window)),
5263                                       0);
5264         gtk_drag_finish (context, TRUE, FALSE, time);
5265
5266         g_strfreev (uris);
5267         g_slist_free (uri_list);
5268 }
5269
5270 static void
5271 ev_window_finalize (GObject *object)
5272 {
5273         G_OBJECT_CLASS (ev_window_parent_class)->finalize (object);
5274
5275         if (ev_window_n_copies == 0) {
5276                 ev_application_shutdown (EV_APP);
5277         } else {
5278                 ev_window_n_copies--;
5279         }
5280 }
5281
5282 static void
5283 ev_window_dispose (GObject *object)
5284 {
5285         EvWindow *window = EV_WINDOW (object);
5286         EvWindowPrivate *priv = window->priv;
5287         GObject *mpkeys = ev_application_get_media_keys (EV_APP);
5288
5289         if (mpkeys) {
5290                 g_signal_handlers_disconnect_by_func (mpkeys,
5291                                                       ev_window_media_player_key_pressed,
5292                                                       window);
5293         }
5294
5295 #ifdef ENABLE_DBUS
5296         if (priv->dbus_object_id > 0) {
5297                 ev_window_emit_closed (window);
5298                 g_dbus_connection_unregister_object (ev_application_get_dbus_connection (EV_APP),
5299                                                      priv->dbus_object_id);
5300                 priv->dbus_object_id = 0;
5301         }
5302
5303         if (priv->dbus_object_path) {
5304                 g_free (priv->dbus_object_path);
5305                 priv->dbus_object_path = NULL;
5306         }
5307 #endif /* ENABLE_DBUS */
5308
5309         if (priv->bookmarks) {
5310                 g_object_unref (priv->bookmarks);
5311                 priv->bookmarks = NULL;
5312         }
5313
5314         if (priv->metadata) {
5315                 g_object_unref (priv->metadata);
5316                 priv->metadata = NULL;
5317         }
5318
5319         if (priv->setup_document_idle > 0) {
5320                 g_source_remove (priv->setup_document_idle);
5321                 priv->setup_document_idle = 0;
5322         }
5323
5324         if (priv->monitor) {
5325                 g_object_unref (priv->monitor);
5326                 priv->monitor = NULL;
5327         }
5328         
5329         if (priv->title) {
5330                 ev_window_title_free (priv->title);
5331                 priv->title = NULL;
5332         }
5333
5334         if (priv->ui_manager) {
5335                 g_object_unref (priv->ui_manager);
5336                 priv->ui_manager = NULL;
5337         }
5338
5339         if (priv->action_group) {
5340                 g_object_unref (priv->action_group);
5341                 priv->action_group = NULL;
5342         }
5343
5344         if (priv->view_popup_action_group) {
5345                 g_object_unref (priv->view_popup_action_group);
5346                 priv->view_popup_action_group = NULL;
5347         }
5348
5349         if (priv->attachment_popup_action_group) {
5350                 g_object_unref (priv->attachment_popup_action_group);
5351                 priv->attachment_popup_action_group = NULL;
5352         }
5353
5354         if (priv->recent_action_group) {
5355                 g_object_unref (priv->recent_action_group);
5356                 priv->recent_action_group = NULL;
5357         }
5358
5359         if (priv->bookmarks_action_group) {
5360                 g_object_unref (priv->bookmarks_action_group);
5361                 priv->bookmarks_action_group = NULL;
5362         }
5363
5364         if (priv->recent_manager) {
5365                 g_signal_handlers_disconnect_by_func (priv->recent_manager,
5366                                                       ev_window_setup_recent,
5367                                                       window);
5368                 priv->recent_manager = NULL;
5369         }
5370
5371         if (priv->settings) {
5372                 g_object_unref (priv->settings);
5373                 priv->settings = NULL;
5374         }
5375
5376         if (priv->default_settings) {
5377                 g_settings_apply (priv->default_settings);
5378                 g_object_unref (priv->default_settings);
5379                 priv->default_settings = NULL;
5380         }
5381
5382         if (priv->lockdown_settings) {
5383                 g_object_unref (priv->lockdown_settings);
5384                 priv->lockdown_settings = NULL;
5385         }
5386
5387         priv->recent_ui_id = 0;
5388
5389         if (priv->model) {
5390                 g_signal_handlers_disconnect_by_func (priv->model,
5391                                                       ev_window_page_changed_cb,
5392                                                       window);
5393                 g_object_unref (priv->model);
5394                 priv->model = NULL;
5395         }
5396
5397         if (priv->document) {
5398                 g_object_unref (priv->document);
5399                 priv->document = NULL;
5400         }
5401         
5402         if (priv->view) {
5403                 g_object_unref (priv->view);
5404                 priv->view = NULL;
5405         }
5406
5407         if (priv->password_view) {
5408                 g_object_unref (priv->password_view);
5409                 priv->password_view = NULL;
5410         }
5411
5412         if (priv->load_job) {
5413                 ev_window_clear_load_job (window);
5414         }
5415
5416         if (priv->reload_job) {
5417                 ev_window_clear_reload_job (window);
5418         }
5419
5420         if (priv->save_job) {
5421                 ev_window_clear_save_job (window);
5422         }
5423
5424         if (priv->thumbnail_job) {
5425                 ev_window_clear_thumbnail_job (window);
5426         }
5427
5428         if (priv->find_job) {
5429                 ev_window_clear_find_job (window);
5430         }
5431         
5432         if (priv->local_uri) {
5433                 ev_window_clear_local_uri (window);
5434                 priv->local_uri = NULL;
5435         }
5436
5437         ev_window_clear_progress_idle (window);
5438         if (priv->progress_cancellable) {
5439                 g_object_unref (priv->progress_cancellable);
5440                 priv->progress_cancellable = NULL;
5441         }
5442         
5443         ev_window_close_dialogs (window);
5444
5445         if (priv->link) {
5446                 g_object_unref (priv->link);
5447                 priv->link = NULL;
5448         }
5449
5450         if (priv->image) {
5451                 g_object_unref (priv->image);
5452                 priv->image = NULL;
5453         }
5454
5455         if (priv->annot) {
5456                 g_object_unref (priv->annot);
5457                 priv->annot = NULL;
5458         }
5459
5460         if (priv->attach_list) {
5461                 g_list_foreach (priv->attach_list,
5462                                 (GFunc) g_object_unref,
5463                                 NULL);
5464                 g_list_free (priv->attach_list);
5465                 priv->attach_list = NULL;
5466         }
5467
5468         if (priv->find_bar) {
5469                 g_signal_handlers_disconnect_by_func
5470                         (window->priv->find_bar,
5471                          G_CALLBACK (find_bar_close_cb),
5472                          window);
5473                 priv->find_bar = NULL;
5474         }
5475
5476         if (priv->uri) {
5477                 g_free (priv->uri);
5478                 priv->uri = NULL;
5479         }
5480
5481         if (priv->search_string) {
5482                 g_free (priv->search_string);
5483                 priv->search_string = NULL;
5484         }
5485         
5486         if (priv->dest) {
5487                 g_object_unref (priv->dest);
5488                 priv->dest = NULL;
5489         }
5490
5491         if (priv->history) {
5492                 g_object_unref (priv->history);
5493                 priv->history = NULL;
5494         }
5495
5496         if (priv->print_queue) {
5497                 g_queue_free (priv->print_queue);
5498                 priv->print_queue = NULL;
5499         }
5500
5501         G_OBJECT_CLASS (ev_window_parent_class)->dispose (object);
5502 }
5503
5504 static void
5505 menubar_deactivate_cb (GtkWidget *menubar,
5506                        EvWindow  *window)
5507 {
5508         g_signal_handlers_disconnect_by_func (menubar,
5509                                               G_CALLBACK (menubar_deactivate_cb),
5510                                               window);
5511
5512         gtk_menu_shell_deselect (GTK_MENU_SHELL (menubar));
5513
5514         update_chrome_visibility (window);
5515 }
5516
5517 static gboolean
5518 ev_window_key_press_event (GtkWidget   *widget,
5519                            GdkEventKey *event)
5520 {
5521         EvWindow        *ev_window = EV_WINDOW (widget);
5522         EvWindowPrivate *priv = ev_window->priv;
5523         gboolean         handled = FALSE;
5524
5525         /* Propagate the event to the view first
5526          * It's needed to be able to type in
5527          * annot popups windows
5528          */
5529         if (priv->view) {
5530                 g_object_ref (priv->view);
5531                 if (gtk_widget_is_sensitive (priv->view))
5532                         handled = gtk_widget_event (priv->view, (GdkEvent*) event);
5533                 g_object_unref (priv->view);
5534         }
5535
5536         if (!handled && !EV_WINDOW_IS_PRESENTATION (ev_window)) {
5537                 guint modifier = event->state & gtk_accelerator_get_default_mod_mask ();
5538
5539                 if (priv->menubar_accel_keyval != 0 &&
5540                     event->keyval == priv->menubar_accel_keyval &&
5541                     modifier == priv->menubar_accel_modifier) {
5542                         if (!gtk_widget_get_visible (priv->menubar)) {
5543                                 g_signal_connect (priv->menubar, "deactivate",
5544                                                   G_CALLBACK (menubar_deactivate_cb),
5545                                                   ev_window);
5546
5547                                 gtk_widget_show (priv->menubar);
5548                                 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menubar),
5549                                                              FALSE);
5550
5551                                 handled = TRUE;
5552                         }
5553                 }
5554         }
5555
5556         if (!handled)
5557                 handled = GTK_WIDGET_CLASS (ev_window_parent_class)->key_press_event (widget, event);
5558
5559         return handled;
5560 }
5561
5562 static gboolean
5563 ev_window_delete_event (GtkWidget   *widget,
5564                         GdkEventAny *event)
5565 {
5566         return !ev_window_close (EV_WINDOW (widget));
5567 }
5568
5569 static void
5570 ev_window_class_init (EvWindowClass *ev_window_class)
5571 {
5572         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_window_class);
5573         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (ev_window_class);
5574
5575         g_object_class->dispose = ev_window_dispose;
5576         g_object_class->finalize = ev_window_finalize;
5577
5578         widget_class->delete_event = ev_window_delete_event;
5579         widget_class->key_press_event = ev_window_key_press_event;
5580         widget_class->screen_changed = ev_window_screen_changed;
5581         widget_class->window_state_event = ev_window_state_event;
5582         widget_class->drag_data_received = ev_window_drag_data_received;
5583
5584         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
5585 }
5586
5587 /* Normal items */
5588 static const GtkActionEntry entries[] = {
5589         { "File", NULL, N_("_File") },
5590         { "Edit", NULL, N_("_Edit") },
5591         { "View", NULL, N_("_View") },
5592         { "Go", NULL, N_("_Go") },
5593         { "Bookmarks", NULL, N_("_Bookmarks") },
5594         { "Help", NULL, N_("_Help") },
5595
5596         /* File menu */
5597         { "FileOpen", GTK_STOCK_OPEN, N_("_Open…"), "<control>O",
5598           N_("Open an existing document"),
5599           G_CALLBACK (ev_window_cmd_file_open) },
5600         { "FileOpenCopy", NULL, N_("Op_en a Copy"), "<control>N",
5601           N_("Open a copy of the current document in a new window"),
5602           G_CALLBACK (ev_window_cmd_file_open_copy) },
5603         { "FileSaveAs", GTK_STOCK_SAVE_AS, N_("_Save a Copy…"), "<control>S",
5604           N_("Save a copy of the current document"),
5605           G_CALLBACK (ev_window_cmd_save_as) },
5606         { "FileOpenContainingFolder", GTK_STOCK_DIRECTORY, N_("Open Containing _Folder"), NULL,
5607           N_("Show the folder which contains this file in the file manager"),
5608           G_CALLBACK (ev_window_cmd_open_containing_folder) },
5609         { "FilePrint", GTK_STOCK_PRINT, N_("_Print…"), "<control>P",
5610           N_("Print this document"),
5611           G_CALLBACK (ev_window_cmd_file_print) },
5612         { "FileProperties", GTK_STOCK_PROPERTIES, N_("P_roperties"), "<alt>Return", NULL,
5613           G_CALLBACK (ev_window_cmd_file_properties) },                       
5614         { "FileCloseWindow", GTK_STOCK_CLOSE, NULL, "<control>W", NULL,
5615           G_CALLBACK (ev_window_cmd_file_close_window) },
5616
5617         /* Edit menu */
5618         { "EditCopy", GTK_STOCK_COPY, NULL, "<control>C", NULL,
5619           G_CALLBACK (ev_window_cmd_edit_copy) },
5620         { "EditSelectAll", GTK_STOCK_SELECT_ALL, N_("Select _All"), "<control>A", NULL,
5621           G_CALLBACK (ev_window_cmd_edit_select_all) },
5622         { "EditFind", GTK_STOCK_FIND, N_("_Find…"), "<control>F",
5623           N_("Find a word or phrase in the document"),
5624           G_CALLBACK (ev_window_cmd_edit_find) },
5625         { "EditFindNext", NULL, N_("Find Ne_xt"), "<control>G", NULL,
5626           G_CALLBACK (ev_window_cmd_edit_find_next) },
5627         { "EditFindPrevious", NULL, N_("Find Pre_vious"), "<shift><control>G", NULL,
5628           G_CALLBACK (ev_window_cmd_edit_find_previous) },
5629         { "EditToolbar", NULL, N_("T_oolbar"), NULL, NULL,
5630           G_CALLBACK (ev_window_cmd_edit_toolbar) },
5631         { "EditRotateLeft", EV_STOCK_ROTATE_LEFT, N_("Rotate _Left"), "<control>Left", NULL,
5632           G_CALLBACK (ev_window_cmd_edit_rotate_left) },
5633         { "EditRotateRight", EV_STOCK_ROTATE_RIGHT, N_("Rotate _Right"), "<control>Right", NULL,
5634           G_CALLBACK (ev_window_cmd_edit_rotate_right) },
5635         { "EditSaveSettings", NULL, N_("Save Current Settings as _Default"), "<control>T", NULL,
5636           G_CALLBACK (ev_window_cmd_edit_save_settings) },
5637
5638
5639         /* View menu */
5640         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, NULL, "<control>plus",
5641           N_("Enlarge the document"),
5642           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5643         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, NULL, "<control>minus",
5644           N_("Shrink the document"),
5645           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5646         { "ViewReload", GTK_STOCK_REFRESH, N_("_Reload"), "<control>R",
5647           N_("Reload the document"),
5648           G_CALLBACK (ev_window_cmd_view_reload) },
5649
5650         { "ViewAutoscroll", GTK_STOCK_MEDIA_PLAY, N_("Auto_scroll"), NULL, NULL,
5651           G_CALLBACK (ev_window_cmd_view_autoscroll) },
5652
5653         /* Go menu */
5654         { "GoPreviousPage", GTK_STOCK_GO_UP, N_("_Previous Page"), "<control>Page_Up",
5655           N_("Go to the previous page"),
5656           G_CALLBACK (ev_window_cmd_go_previous_page) },
5657         { "GoNextPage", GTK_STOCK_GO_DOWN, N_("_Next Page"), "<control>Page_Down",
5658           N_("Go to the next page"),
5659           G_CALLBACK (ev_window_cmd_go_next_page) },
5660         { "GoFirstPage", GTK_STOCK_GOTO_TOP, N_("_First Page"), "<control>Home",
5661           N_("Go to the first page"),
5662           G_CALLBACK (ev_window_cmd_go_first_page) },
5663         { "GoLastPage", GTK_STOCK_GOTO_BOTTOM, N_("_Last Page"), "<control>End",
5664           N_("Go to the last page"),
5665           G_CALLBACK (ev_window_cmd_go_last_page) },
5666
5667         /* Bookmarks menu */
5668         { "BookmarksAdd", GTK_STOCK_ADD, N_("_Add Bookmark"), "<control>D",
5669           N_("Add a bookmark for the current page"),
5670           G_CALLBACK (ev_window_cmd_bookmarks_add) },
5671
5672         /* Help menu */
5673         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), "F1", NULL,
5674           G_CALLBACK (ev_window_cmd_help_contents) },
5675
5676         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL,
5677           G_CALLBACK (ev_window_cmd_help_about) },
5678
5679         /* Toolbar-only */
5680         { "LeaveFullscreen", GTK_STOCK_LEAVE_FULLSCREEN, N_("Leave Fullscreen"), NULL,
5681           N_("Leave fullscreen mode"),
5682           G_CALLBACK (ev_window_cmd_leave_fullscreen) },
5683         { "StartPresentation", EV_STOCK_RUN_PRESENTATION, N_("Start Presentation"), NULL,
5684           N_("Start a presentation"),
5685           G_CALLBACK (ev_window_cmd_start_presentation) },
5686
5687         /* Accellerators */
5688         { "Escape", NULL, "", "Escape", "",
5689           G_CALLBACK (ev_window_cmd_escape) },
5690         { "Slash", GTK_STOCK_FIND, NULL, "slash", NULL,
5691           G_CALLBACK (ev_window_cmd_edit_find) },
5692         { "F3", NULL, "", "F3", NULL,
5693           G_CALLBACK (ev_window_cmd_edit_find_next) },
5694         { "PageDown", NULL, "", "Page_Down", NULL,
5695           G_CALLBACK (ev_window_cmd_scroll_forward) },
5696         { "PageUp", NULL, "", "Page_Up", NULL,
5697           G_CALLBACK (ev_window_cmd_scroll_backward) },
5698         { "Space", NULL, "", "space", NULL,
5699           G_CALLBACK (ev_window_cmd_scroll_forward) },
5700         { "ShiftSpace", NULL, "", "<shift>space", NULL,
5701           G_CALLBACK (ev_window_cmd_scroll_backward) },
5702         { "BackSpace", NULL, "", "BackSpace", NULL,
5703           G_CALLBACK (ev_window_cmd_scroll_backward) },
5704         { "ShiftBackSpace", NULL, "", "<shift>BackSpace", NULL,
5705           G_CALLBACK (ev_window_cmd_scroll_forward) },
5706         { "Return", NULL, "", "Return", NULL,
5707           G_CALLBACK (ev_window_cmd_scroll_forward) },
5708         { "ShiftReturn", NULL, "", "<shift>Return", NULL,
5709           G_CALLBACK (ev_window_cmd_scroll_backward) },
5710         { "p", GTK_STOCK_GO_UP, "", "p", NULL,
5711           G_CALLBACK (ev_window_cmd_go_previous_page) },
5712         { "n", GTK_STOCK_GO_DOWN, "", "n", NULL,
5713           G_CALLBACK (ev_window_cmd_go_next_page) },
5714         { "Plus", GTK_STOCK_ZOOM_IN, NULL, "plus", NULL,
5715           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5716         { "CtrlEqual", GTK_STOCK_ZOOM_IN, NULL, "<control>equal", NULL,
5717           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5718         { "Equal", GTK_STOCK_ZOOM_IN, NULL, "equal", NULL,
5719           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5720         { "Minus", GTK_STOCK_ZOOM_OUT, NULL, "minus", NULL,
5721           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5722         { "FocusPageSelector", NULL, "", "<control>l", NULL,
5723           G_CALLBACK (ev_window_cmd_focus_page_selector) },
5724         { "GoBackwardFast", NULL, "", "<shift>Page_Up", NULL,
5725           G_CALLBACK (ev_window_cmd_go_backward) },
5726         { "GoForwardFast", NULL, "", "<shift>Page_Down", NULL,
5727           G_CALLBACK (ev_window_cmd_go_forward) },
5728         { "KpPlus", GTK_STOCK_ZOOM_IN, NULL, "KP_Add", NULL,
5729           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5730         { "KpMinus", GTK_STOCK_ZOOM_OUT, NULL, "KP_Subtract", NULL,
5731           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5732         { "CtrlKpPlus", GTK_STOCK_ZOOM_IN, NULL, "<control>KP_Add", NULL,
5733           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5734         { "CtrlKpMinus", GTK_STOCK_ZOOM_OUT, NULL, "<control>KP_Subtract", NULL,
5735           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5736         { "CtrlInsert", GTK_STOCK_COPY, NULL, "<control>Insert", NULL,
5737           G_CALLBACK (ev_window_cmd_edit_copy) },
5738 };
5739
5740 /* Toggle items */
5741 static const GtkToggleActionEntry toggle_entries[] = {
5742         /* View Menu */
5743         { "ViewToolbar", NULL, N_("_Toolbar"), NULL,
5744           N_("Show or hide the toolbar"),
5745           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
5746         { "ViewSidebar", GTK_STOCK_INDEX, N_("Side _Pane"), "F9",
5747           N_("Show or hide the side pane"),
5748           G_CALLBACK (ev_window_view_sidebar_cb), TRUE },
5749         { "ViewContinuous", EV_STOCK_VIEW_CONTINUOUS, N_("_Continuous"), NULL,
5750           N_("Show the entire document"),
5751           G_CALLBACK (ev_window_cmd_continuous), TRUE },
5752         { "ViewDual", EV_STOCK_VIEW_DUAL, N_("_Dual"), NULL,
5753           N_("Show two pages at once"),
5754           G_CALLBACK (ev_window_cmd_dual), FALSE },
5755         { "ViewFullscreen", GTK_STOCK_FULLSCREEN, N_("_Fullscreen"), "F11",
5756           N_("Expand the window to fill the screen"),
5757           G_CALLBACK (ev_window_cmd_view_fullscreen) },
5758         { "ViewPresentation", EV_STOCK_RUN_PRESENTATION, N_("Pre_sentation"), "F5",
5759           N_("Run document as a presentation"),
5760           G_CALLBACK (ev_window_cmd_view_presentation) },
5761         { "ViewBestFit", EV_STOCK_ZOOM_PAGE, N_("_Best Fit"), NULL,
5762           N_("Make the current document fill the window"),
5763           G_CALLBACK (ev_window_cmd_view_best_fit) },
5764         { "ViewPageWidth", EV_STOCK_ZOOM_WIDTH, N_("Fit Page _Width"), NULL,
5765           N_("Make the current document fill the window width"),
5766           G_CALLBACK (ev_window_cmd_view_page_width) },
5767         { "ViewInvertedColors", EV_STOCK_INVERTED_COLORS, N_("_Inverted Colors"), "<control>I",
5768           N_("Show page contents with the colors inverted"),
5769           G_CALLBACK (ev_window_cmd_view_inverted_colors) },
5770
5771 };
5772
5773 /* Popups specific items */
5774 static const GtkActionEntry view_popup_entries [] = {
5775         /* Links */
5776         { "OpenLink", GTK_STOCK_OPEN, N_("_Open Link"), NULL,
5777           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
5778         { "GoLink", GTK_STOCK_GO_FORWARD, N_("_Go To"), NULL,
5779           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
5780         { "OpenLinkNewWindow", NULL, N_("Open in New _Window"), NULL,
5781           NULL, G_CALLBACK (ev_view_popup_cmd_open_link_new_window) },
5782         { "CopyLinkAddress", NULL, N_("_Copy Link Address"), NULL,
5783           NULL, G_CALLBACK (ev_view_popup_cmd_copy_link_address) },
5784         { "SaveImageAs", NULL, N_("_Save Image As…"), NULL,
5785           NULL, G_CALLBACK (ev_view_popup_cmd_save_image_as) },
5786         { "CopyImage", NULL, N_("Copy _Image"), NULL,
5787           NULL, G_CALLBACK (ev_view_popup_cmd_copy_image) },
5788         { "AnnotProperties", NULL, N_("Annotation Properties…"), NULL,
5789           NULL, G_CALLBACK (ev_view_popup_cmd_annot_properties) }
5790 };
5791
5792 static const GtkActionEntry attachment_popup_entries [] = {
5793         { "OpenAttachment", GTK_STOCK_OPEN, N_("_Open Attachment"), NULL,
5794           NULL, G_CALLBACK (ev_attachment_popup_cmd_open_attachment) },
5795         { "SaveAttachmentAs", GTK_STOCK_SAVE_AS, N_("_Save Attachment As…"), NULL,
5796           NULL, G_CALLBACK (ev_attachment_popup_cmd_save_attachment_as) },
5797 };
5798
5799 static void
5800 sidebar_links_link_activated_cb (EvSidebarLinks *sidebar_links, EvLink *link, EvWindow *window)
5801 {
5802         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5803 }
5804
5805 static void
5806 activate_link_cb (EvPageAction *page_action, EvLink *link, EvWindow *window)
5807 {
5808         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5809         gtk_widget_grab_focus (window->priv->view);
5810 }
5811
5812 static void
5813 navigation_action_activate_link_cb (EvNavigationAction *action, EvLink *link, EvWindow *window)
5814 {
5815         
5816         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5817         gtk_widget_grab_focus (window->priv->view);
5818 }
5819
5820 static void
5821 sidebar_layers_visibility_changed (EvSidebarLayers *layers,
5822                                    EvWindow        *window)
5823 {
5824         ev_view_reload (EV_VIEW (window->priv->view));
5825 }
5826
5827 static void
5828 sidebar_annots_annot_activated_cb (EvSidebarAnnotations *sidebar_annots,
5829                                    EvMapping            *annot_mapping,
5830                                    EvWindow             *window)
5831 {
5832         ev_view_focus_annotation (EV_VIEW (window->priv->view), annot_mapping);
5833 }
5834
5835 static void
5836 sidebar_annots_begin_annot_add (EvSidebarAnnotations *sidebar_annots,
5837                                 EvAnnotationType      annot_type,
5838                                 EvWindow             *window)
5839 {
5840         ev_view_begin_add_annotation (EV_VIEW (window->priv->view), annot_type);
5841 }
5842
5843 static void
5844 view_annot_added (EvView       *view,
5845                   EvAnnotation *annot,
5846                   EvWindow     *window)
5847 {
5848         ev_sidebar_annotations_annot_added (EV_SIDEBAR_ANNOTATIONS (window->priv->sidebar_annots),
5849                                             annot);
5850 }
5851
5852 static void
5853 sidebar_annots_annot_add_cancelled (EvSidebarAnnotations *sidebar_annots,
5854                                     EvWindow             *window)
5855 {
5856         ev_view_cancel_add_annotation (EV_VIEW (window->priv->view));
5857 }
5858
5859 static void
5860 sidebar_bookmarks_add_bookmark (EvSidebarBookmarks *sidebar_bookmarks,
5861                                 EvWindow           *window)
5862 {
5863         ev_window_cmd_bookmarks_add (NULL, window);
5864 }
5865
5866 static void
5867 register_custom_actions (EvWindow *window, GtkActionGroup *group)
5868 {
5869         GtkAction *action;
5870
5871         action = g_object_new (EV_TYPE_PAGE_ACTION,
5872                                "name", PAGE_SELECTOR_ACTION,
5873                                "label", _("Page"),
5874                                "tooltip", _("Select Page"),
5875                                "icon_name", "text-x-generic",
5876                                "visible_overflown", FALSE,
5877                                NULL);
5878         ev_page_action_set_model (EV_PAGE_ACTION (action),
5879                                   window->priv->model);
5880         g_signal_connect (action, "activate_link",
5881                           G_CALLBACK (activate_link_cb), window);
5882         gtk_action_group_add_action (group, action);
5883         g_object_unref (action);
5884
5885         action = g_object_new (EPHY_TYPE_ZOOM_ACTION,
5886                                "name", ZOOM_CONTROL_ACTION,
5887                                "label", _("Zoom"),
5888                                "stock_id", EV_STOCK_ZOOM,
5889                                "tooltip", _("Adjust the zoom level"),
5890                                "zoom", 1.0,
5891                                NULL);
5892         g_signal_connect (action, "zoom_to_level",
5893                           G_CALLBACK (zoom_control_changed_cb), window);
5894         gtk_action_group_add_action (group, action);
5895         g_object_unref (action);
5896
5897         action = g_object_new (EV_TYPE_NAVIGATION_ACTION,
5898                                "name", NAVIGATION_ACTION,
5899                                "label", _("Navigation"),
5900                                "is_important", TRUE,
5901                                "short_label", _("Back"),
5902                                "stock_id", GTK_STOCK_GO_DOWN,
5903                                /*translators: this is the history action*/
5904                                "tooltip", _("Move across visited pages"),
5905                                NULL);
5906         g_signal_connect (action, "activate_link",
5907                           G_CALLBACK (navigation_action_activate_link_cb), window);
5908         gtk_action_group_add_action (group, action);
5909         g_object_unref (action);
5910
5911         action = g_object_new (EV_TYPE_OPEN_RECENT_ACTION,
5912                                "name", "FileOpenRecent",
5913                                "label", _("_Open…"),
5914                                "tooltip", _("Open an existing document"),
5915                                "stock_id", GTK_STOCK_OPEN,
5916                                NULL);
5917         g_signal_connect (action, "activate",
5918                           G_CALLBACK (ev_window_cmd_file_open), window);
5919         g_signal_connect (action, "item_activated",
5920                           G_CALLBACK (ev_window_open_recent_action_item_activated),
5921                           window);
5922         gtk_action_group_add_action (group, action);
5923         g_object_unref (action);
5924 }
5925
5926 static void
5927 set_action_properties (GtkActionGroup *action_group)
5928 {
5929         GtkAction *action;
5930
5931         action = gtk_action_group_get_action (action_group, "FileOpenContainingFolder");
5932         /*translators: this is the label for toolbar button*/
5933         g_object_set (action, "short_label", _("Open Folder"), NULL);
5934
5935         action = gtk_action_group_get_action (action_group, "GoPreviousPage");
5936         g_object_set (action, "is-important", TRUE, NULL);
5937         /*translators: this is the label for toolbar button*/
5938         g_object_set (action, "short_label", _("Previous"), NULL);
5939
5940         action = gtk_action_group_get_action (action_group, "GoNextPage");
5941         g_object_set (action, "is-important", TRUE, NULL);
5942         /*translators: this is the label for toolbar button*/
5943         g_object_set (action, "short_label", _("Next"), NULL);
5944
5945         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
5946         /*translators: this is the label for toolbar button*/
5947         g_object_set (action, "short_label", _("Zoom In"), NULL);
5948
5949         action = gtk_action_group_get_action (action_group, "ViewZoomOut");
5950         /*translators: this is the label for toolbar button*/
5951         g_object_set (action, "short_label", _("Zoom Out"), NULL);
5952
5953         action = gtk_action_group_get_action (action_group, "ViewBestFit");
5954         /*translators: this is the label for toolbar button*/
5955         g_object_set (action, "short_label", _("Best Fit"), NULL);
5956
5957         action = gtk_action_group_get_action (action_group, "ViewPageWidth");
5958         /*translators: this is the label for toolbar button*/
5959         g_object_set (action, "short_label", _("Fit Width"), NULL);
5960
5961         action = gtk_action_group_get_action (action_group, "LeaveFullscreen");
5962         g_object_set (action, "is-important", TRUE, NULL);
5963 }
5964
5965 static void
5966 sidebar_widget_model_set (EvSidebarLinks *ev_sidebar_links,
5967                           GParamSpec     *pspec,
5968                           EvWindow       *ev_window)
5969 {
5970         GtkTreeModel *model;
5971         GtkAction *action;
5972
5973         g_object_get (G_OBJECT (ev_sidebar_links),
5974                       "model", &model,
5975                       NULL);
5976
5977         action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
5978         ev_page_action_set_links_model (EV_PAGE_ACTION (action), model);
5979         g_object_unref (model);
5980 }
5981
5982 static gboolean
5983 view_actions_focus_in_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
5984 {
5985 #ifdef ENABLE_DBUS
5986         GObject *keys;
5987
5988         keys = ev_application_get_media_keys (EV_APP);
5989         ev_media_player_keys_focused (EV_MEDIA_PLAYER_KEYS (keys));
5990 #endif /* ENABLE_DBUS */
5991
5992         update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, FALSE);
5993         ev_window_set_action_sensitive (window, "ViewToolbar", TRUE);
5994
5995         ev_window_set_view_accels_sensitivity (window, TRUE);
5996
5997         update_chrome_visibility (window);
5998
5999         return FALSE;
6000 }
6001
6002 static gboolean
6003 view_actions_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
6004 {
6005         ev_window_set_view_accels_sensitivity (window, FALSE);
6006
6007         return FALSE;
6008 }
6009
6010 static void
6011 sidebar_page_main_widget_update_cb (GObject *ev_sidebar_page,
6012                                     GParamSpec         *pspec,
6013                                     EvWindow           *ev_window)
6014 {
6015         GtkWidget *widget;
6016         
6017         g_object_get (ev_sidebar_page, "main_widget", &widget, NULL);
6018
6019         if (widget != NULL) {           
6020                 g_signal_connect_object (widget, "focus_in_event",
6021                                          G_CALLBACK (view_actions_focus_in_cb),
6022                                          ev_window, 0);
6023                 g_signal_connect_object (widget, "focus_out_event",
6024                                          G_CALLBACK (view_actions_focus_out_cb),
6025                                          ev_window, 0);
6026                 g_object_unref (widget);
6027         }
6028 }
6029
6030 static gboolean
6031 window_state_event_cb (EvWindow *window, GdkEventWindowState *event, gpointer dummy)
6032 {
6033         if (!(event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)) {
6034                 gboolean maximized;
6035
6036                 maximized = event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED;
6037                 if (window->priv->metadata && !ev_window_is_empty (window))
6038                         ev_metadata_set_boolean (window->priv->metadata, "window_maximized", maximized);
6039         }
6040
6041         return FALSE;
6042 }
6043
6044 static gboolean
6045 window_configure_event_cb (EvWindow *window, GdkEventConfigure *event, gpointer dummy)
6046 {
6047         GdkWindowState state;
6048         gdouble document_width, document_height;
6049
6050         if (!window->priv->metadata)
6051                 return FALSE;
6052
6053         state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (window)));
6054
6055         if (!(state & GDK_WINDOW_STATE_FULLSCREEN)) {
6056                 if (window->priv->document) {
6057                         ev_document_get_max_page_size (window->priv->document,
6058                                                        &document_width, &document_height);
6059                         g_settings_set (window->priv->default_settings, "window-ratio", "(dd)",
6060                                         (double)event->width / document_width,
6061                                         (double)event->height / document_height);
6062
6063                         ev_metadata_set_int (window->priv->metadata, "window_x", event->x);
6064                         ev_metadata_set_int (window->priv->metadata, "window_y", event->y);
6065                         ev_metadata_set_int (window->priv->metadata, "window_width", event->width);
6066                         ev_metadata_set_int (window->priv->metadata, "window_height", event->height);
6067                 }
6068         }
6069
6070         return FALSE;
6071 }
6072
6073 static void
6074 launch_action (EvWindow *window, EvLinkAction *action)
6075 {
6076         const char *filename = ev_link_action_get_filename (action);
6077         GAppInfo *app_info;
6078         GFile *file;
6079         GList file_list = {NULL};
6080         GAppLaunchContext *context;
6081         GError *error = NULL;
6082
6083         if (filename == NULL)
6084                 return;
6085
6086         if (g_path_is_absolute (filename)) {
6087                 file = g_file_new_for_path (filename);
6088         } else {
6089                 GFile *base_file;
6090                 gchar *dir;
6091
6092                 dir = g_path_get_dirname (window->priv->uri);
6093                 base_file = g_file_new_for_uri (dir);
6094                 g_free (dir);
6095                 
6096                 file = g_file_resolve_relative_path (base_file, filename);
6097                 g_object_unref (base_file);
6098         }
6099
6100         app_info = g_file_query_default_handler (file, NULL, &error);
6101         if (!app_info) {
6102                 ev_window_error_message (window, error,
6103                                          "%s",
6104                                          _("Unable to launch external application."));
6105                 g_object_unref (file);
6106                 g_error_free (error);
6107
6108                 return;
6109         }
6110
6111         context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
6112         gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context),
6113                                            gtk_window_get_screen (GTK_WINDOW (window)));
6114         gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context),
6115                                               gtk_get_current_event_time ());
6116         
6117         file_list.data = file;
6118         if (!g_app_info_launch (app_info, &file_list, context, &error)) {
6119                 ev_window_error_message (window, error,
6120                                          "%s",
6121                                          _("Unable to launch external application."));
6122                 g_error_free (error);
6123         }
6124         
6125         g_object_unref (app_info);
6126         g_object_unref (file);
6127         /* FIXMEchpe: unref launch context? */
6128
6129         /* According to the PDF spec filename can be an executable. I'm not sure
6130            allowing to launch executables is a good idea though. -- marco */
6131 }
6132
6133 static void
6134 launch_external_uri (EvWindow *window, EvLinkAction *action)
6135 {
6136         const gchar *uri = ev_link_action_get_uri (action);
6137         GError *error = NULL;
6138         gboolean ret;
6139         GAppLaunchContext *context;
6140
6141         context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
6142         gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context),
6143                                            gtk_window_get_screen (GTK_WINDOW (window)));
6144         gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context),
6145                                               gtk_get_current_event_time ());
6146
6147         if (!g_strstr_len (uri, strlen (uri), "://") &&
6148             !g_str_has_prefix (uri, "mailto:")) {
6149                 gchar *new_uri;
6150
6151                 /* Not a valid uri, assume http if it starts with www */
6152                 if (g_str_has_prefix (uri, "www.")) {
6153                         new_uri = g_strdup_printf ("http://%s", uri);
6154                 } else {
6155                         GFile *file, *parent;
6156
6157                         file = g_file_new_for_uri (window->priv->uri);
6158                         parent = g_file_get_parent (file);
6159                         g_object_unref (file);
6160                         if (parent) {
6161                                 gchar *parent_uri = g_file_get_uri (parent);
6162
6163                                 new_uri = g_build_filename (parent_uri, uri, NULL);
6164                                 g_free (parent_uri);
6165                                 g_object_unref (parent);
6166                         } else {
6167                                 new_uri = g_strdup_printf ("file:///%s", uri);
6168                         }
6169                 }
6170                 ret = g_app_info_launch_default_for_uri (new_uri, context, &error);
6171                 g_free (new_uri);
6172         } else {
6173                 ret = g_app_info_launch_default_for_uri (uri, context, &error);
6174         }
6175
6176         if (ret == FALSE) {
6177                 ev_window_error_message (window, error,
6178                                          "%s", _("Unable to open external link"));
6179                 g_error_free (error);
6180         }
6181
6182         /* FIXMEchpe: unref launch context? */
6183 }
6184
6185 static void
6186 open_remote_link (EvWindow *window, EvLinkAction *action)
6187 {
6188         gchar *uri;
6189         gchar *dir;
6190
6191         dir = g_path_get_dirname (window->priv->uri);
6192         
6193         uri = g_build_filename (dir, ev_link_action_get_filename (action),
6194                                 NULL);
6195         g_free (dir);
6196
6197         ev_application_open_uri_at_dest (EV_APP, uri,
6198                                          gtk_window_get_screen (GTK_WINDOW (window)),
6199                                          ev_link_action_get_dest (action),
6200                                          0,
6201                                          NULL, 
6202                                          gtk_get_current_event_time ());
6203
6204         g_free (uri);
6205 }
6206
6207 static void
6208 do_action_named (EvWindow *window, EvLinkAction *action)
6209 {
6210         const gchar *name = ev_link_action_get_name (action);
6211
6212         if (g_ascii_strcasecmp (name, "FirstPage") == 0) {
6213                 ev_window_cmd_go_first_page (NULL, window);
6214         } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) {
6215                 ev_window_cmd_go_previous_page (NULL, window);
6216         } else if (g_ascii_strcasecmp (name, "NextPage") == 0) {
6217                 ev_window_cmd_go_next_page (NULL, window);
6218         } else if (g_ascii_strcasecmp (name, "LastPage") == 0) {
6219                 ev_window_cmd_go_last_page (NULL, window);
6220         } else if (g_ascii_strcasecmp (name, "GoToPage") == 0) {
6221                 ev_window_cmd_focus_page_selector (NULL, window);
6222         } else if (g_ascii_strcasecmp (name, "Find") == 0) {
6223                 ev_window_cmd_edit_find (NULL, window);
6224         } else if (g_ascii_strcasecmp (name, "Close") == 0) {
6225                 ev_window_cmd_file_close_window (NULL, window);
6226         } else if (g_ascii_strcasecmp (name, "Print") == 0) {
6227                 ev_window_cmd_file_print (NULL, window);
6228         } else {
6229                 g_warning ("Unimplemented named action: %s, please post a "
6230                            "bug report in Evince bugzilla "
6231                            "(http://bugzilla.gnome.org) with a testcase.",
6232                            name);
6233         }
6234 }
6235
6236 static void
6237 view_external_link_cb (EvWindow *window, EvLinkAction *action)
6238 {
6239         switch (ev_link_action_get_action_type (action)) {
6240                 case EV_LINK_ACTION_TYPE_GOTO_DEST: {
6241                         EvLinkDest *dest;
6242                         
6243                         dest = ev_link_action_get_dest (action);
6244                         if (!dest)
6245                                 return;
6246
6247                         ev_window_open_copy_at_dest (window, dest);
6248                 }
6249                         break;
6250                 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
6251                         launch_external_uri (window, action);
6252                         break;
6253                 case EV_LINK_ACTION_TYPE_LAUNCH:
6254                         launch_action (window, action);
6255                         break;
6256                 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
6257                         open_remote_link (window, action);
6258                         break;
6259                 case EV_LINK_ACTION_TYPE_NAMED:
6260                         do_action_named (window, action);
6261                         break;
6262                 default:
6263                         g_assert_not_reached ();
6264         }
6265 }
6266
6267 static void
6268 ev_view_popup_cmd_open_link (GtkAction *action, EvWindow *window)
6269 {
6270         ev_view_handle_link (EV_VIEW (window->priv->view), window->priv->link);
6271 }
6272
6273 static void
6274 ev_view_popup_cmd_open_link_new_window (GtkAction *action, EvWindow *window)
6275 {
6276         EvLinkAction *ev_action = NULL;
6277         EvLinkDest   *dest;
6278
6279         ev_action = ev_link_get_action (window->priv->link);
6280         if (!ev_action)
6281                 return;
6282
6283         dest = ev_link_action_get_dest (ev_action);
6284         if (!dest)
6285                 return;
6286
6287         ev_window_open_copy_at_dest (window, dest);
6288 }
6289
6290 static void
6291 ev_view_popup_cmd_copy_link_address (GtkAction *action, EvWindow *window)
6292 {
6293         EvLinkAction *ev_action;
6294
6295         ev_action = ev_link_get_action (window->priv->link);
6296         if (!ev_action)
6297                 return;
6298
6299         ev_view_copy_link_address (EV_VIEW (window->priv->view),
6300                                    ev_action);
6301 }
6302
6303
6304 static void
6305 image_save_dialog_response_cb (GtkWidget *fc,
6306                                gint       response_id,
6307                                EvWindow  *ev_window)
6308 {
6309         GFile           *target_file;
6310         gboolean         is_native;
6311         GError          *error = NULL;
6312         GdkPixbuf       *pixbuf;
6313         gchar           *uri;
6314         gchar          **extensions;
6315         gchar           *filename;
6316         gchar           *file_format;
6317         GdkPixbufFormat *format;
6318         GtkFileFilter   *filter;
6319         
6320         if (response_id != GTK_RESPONSE_OK) {
6321                 gtk_widget_destroy (fc);
6322                 return;
6323         }
6324
6325         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
6326         filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (fc));
6327         format = g_object_get_data (G_OBJECT (filter), "pixbuf-format");
6328         
6329         if (format == NULL) {
6330                 format = get_gdk_pixbuf_format_by_extension (uri);
6331         }
6332
6333         if (format == NULL && g_strrstr (uri, ".") == NULL) {
6334                 /* no extension found and no extension provided within uri */
6335                 format = get_gdk_pixbuf_format_by_extension (".png");
6336                 if (format == NULL) {
6337                         /* no .png support, try .jpeg */
6338                         format = get_gdk_pixbuf_format_by_extension (".jpeg");
6339                 }
6340         }
6341
6342         if (format == NULL) {
6343                 ev_window_error_message (ev_window, NULL, 
6344                                          "%s",
6345                                          _("Couldn't find appropriate format to save image"));
6346                 g_free (uri);
6347                 gtk_widget_destroy (fc);
6348
6349                 return;
6350         }
6351
6352         extensions = gdk_pixbuf_format_get_extensions (format);
6353         if (!g_str_has_suffix (uri, extensions[0])) {
6354                 gchar *uri_extension;
6355                 
6356                 uri_extension = g_strconcat (uri, ".", extensions[0], NULL);
6357                 target_file = g_file_new_for_uri (uri_extension);
6358                 g_free (uri_extension);
6359         } else {
6360                 target_file = g_file_new_for_uri (uri);
6361         }
6362         g_strfreev (extensions);
6363         g_free (uri);
6364         
6365         is_native = g_file_is_native (target_file);
6366         if (is_native) {
6367                 filename = g_file_get_path (target_file);
6368         } else {
6369                 /* Create a temporary local file to save to */
6370                 if (ev_mkstemp ("saveimage.XXXXXX", &filename, &error) == -1)
6371                         goto has_error;
6372         }
6373
6374         ev_document_doc_mutex_lock ();
6375         pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (ev_window->priv->document),
6376                                                ev_window->priv->image);
6377         ev_document_doc_mutex_unlock ();
6378
6379         file_format = gdk_pixbuf_format_get_name (format);
6380         gdk_pixbuf_save (pixbuf, filename, file_format, &error, NULL);
6381         g_free (file_format);
6382         g_object_unref (pixbuf);
6383         
6384     has_error:
6385         if (error) {
6386                 ev_window_error_message (ev_window, error, 
6387                                          "%s", _("The image could not be saved."));
6388                 g_error_free (error);
6389                 g_free (filename);
6390                 g_object_unref (target_file);
6391                 gtk_widget_destroy (fc);
6392
6393                 return;
6394         }
6395
6396         if (!is_native) {
6397                 GFile *source_file;
6398                 
6399                 source_file = g_file_new_for_path (filename);
6400                 
6401                 ev_window_save_remote (ev_window, EV_SAVE_IMAGE,
6402                                        source_file, target_file);
6403                 g_object_unref (source_file);
6404         }
6405         
6406         g_free (filename);
6407         g_object_unref (target_file);
6408         gtk_widget_destroy (fc);
6409 }
6410
6411 static void
6412 ev_view_popup_cmd_save_image_as (GtkAction *action, EvWindow *window)
6413 {
6414         GtkWidget *fc;
6415
6416         if (!window->priv->image)
6417                 return;
6418
6419         fc = gtk_file_chooser_dialog_new (_("Save Image"),
6420                                           GTK_WINDOW (window),
6421                                           GTK_FILE_CHOOSER_ACTION_SAVE,
6422                                           GTK_STOCK_CANCEL,
6423                                           GTK_RESPONSE_CANCEL,
6424                                           GTK_STOCK_SAVE, GTK_RESPONSE_OK,
6425                                           NULL);
6426
6427         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
6428         gtk_dialog_set_alternative_button_order (GTK_DIALOG (fc),
6429                                                  GTK_RESPONSE_OK,
6430                                                  GTK_RESPONSE_CANCEL,
6431                                                  -1);
6432
6433         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
6434         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
6435         
6436         file_chooser_dialog_add_writable_pixbuf_formats (GTK_FILE_CHOOSER (fc));
6437         
6438         g_signal_connect (fc, "response",
6439                           G_CALLBACK (image_save_dialog_response_cb),
6440                           window);
6441
6442         gtk_widget_show (fc);
6443 }
6444
6445 static void
6446 ev_view_popup_cmd_copy_image (GtkAction *action, EvWindow *window)
6447 {
6448         GtkClipboard *clipboard;
6449         GdkPixbuf    *pixbuf;
6450
6451         if (!window->priv->image)
6452                 return;
6453         
6454         clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window),
6455                                               GDK_SELECTION_CLIPBOARD);
6456         ev_document_doc_mutex_lock ();
6457         pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (window->priv->document),
6458                                                window->priv->image);
6459         ev_document_doc_mutex_unlock ();
6460         
6461         gtk_clipboard_set_image (clipboard, pixbuf);
6462         g_object_unref (pixbuf);
6463 }
6464
6465 static void
6466 ev_view_popup_cmd_annot_properties (GtkAction *action,
6467                                     EvWindow  *window)
6468 {
6469         const gchar                  *author;
6470         GdkColor                      color;
6471         gdouble                       opacity;
6472         gboolean                      popup_is_open;
6473         EvAnnotationPropertiesDialog *dialog;
6474         EvAnnotation                 *annot = window->priv->annot;
6475         EvAnnotationsSaveMask         mask = EV_ANNOTATIONS_SAVE_NONE;
6476
6477         if (!annot)
6478                 return;
6479
6480         dialog = EV_ANNOTATION_PROPERTIES_DIALOG (ev_annotation_properties_dialog_new_with_annotation (window->priv->annot));
6481         if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_APPLY) {
6482                 gtk_widget_destroy (GTK_WIDGET (dialog));
6483
6484                 return;
6485         }
6486
6487         /* Set annotations changes */
6488         author = ev_annotation_properties_dialog_get_author (dialog);
6489         if (ev_annotation_markup_set_label (EV_ANNOTATION_MARKUP (annot), author))
6490                 mask |= EV_ANNOTATIONS_SAVE_LABEL;
6491
6492         ev_annotation_properties_dialog_get_color (dialog, &color);
6493         if (ev_annotation_set_color (annot, &color))
6494                 mask |= EV_ANNOTATIONS_SAVE_COLOR;
6495
6496         opacity = ev_annotation_properties_dialog_get_opacity (dialog);
6497         if (ev_annotation_markup_set_opacity (EV_ANNOTATION_MARKUP (annot), opacity))
6498                 mask |= EV_ANNOTATIONS_SAVE_OPACITY;
6499
6500         popup_is_open = ev_annotation_properties_dialog_get_popup_is_open (dialog);
6501         if (ev_annotation_markup_set_popup_is_open (EV_ANNOTATION_MARKUP (annot), popup_is_open))
6502                 mask |= EV_ANNOTATIONS_SAVE_POPUP_IS_OPEN;
6503
6504         if (EV_IS_ANNOTATION_TEXT (annot)) {
6505                 EvAnnotationTextIcon icon;
6506
6507                 icon = ev_annotation_properties_dialog_get_text_icon (dialog);
6508                 if (ev_annotation_text_set_icon (EV_ANNOTATION_TEXT (annot), icon))
6509                         mask |= EV_ANNOTATIONS_SAVE_TEXT_ICON;
6510         }
6511
6512         if (mask != EV_ANNOTATIONS_SAVE_NONE) {
6513                 ev_document_doc_mutex_lock ();
6514                 ev_document_annotations_save_annotation (EV_DOCUMENT_ANNOTATIONS (window->priv->document),
6515                                                          window->priv->annot,
6516                                                          mask);
6517                 ev_document_doc_mutex_unlock ();
6518
6519                 /* FIXME: update annot region only */
6520                 ev_view_reload (EV_VIEW (window->priv->view));
6521         }
6522
6523         gtk_widget_destroy (GTK_WIDGET (dialog));
6524 }
6525
6526 static void
6527 ev_attachment_popup_cmd_open_attachment (GtkAction *action, EvWindow *window)
6528 {
6529         GList     *l;
6530         GdkScreen *screen;
6531         
6532         if (!window->priv->attach_list)
6533                 return;
6534
6535         screen = gtk_window_get_screen (GTK_WINDOW (window));
6536
6537         for (l = window->priv->attach_list; l && l->data; l = g_list_next (l)) {
6538                 EvAttachment *attachment;
6539                 GError       *error = NULL;
6540                 
6541                 attachment = (EvAttachment *) l->data;
6542                 
6543                 ev_attachment_open (attachment, screen, gtk_get_current_event_time (), &error);
6544
6545                 if (error) {
6546                         ev_window_error_message (window, error, 
6547                                                  "%s", _("Unable to open attachment"));
6548                         g_error_free (error);
6549                 }
6550         }
6551 }
6552
6553 static void
6554 attachment_save_dialog_response_cb (GtkWidget *fc,
6555                                     gint       response_id,
6556                                     EvWindow  *ev_window)
6557 {
6558         GFile                *target_file;
6559         gchar                *uri;
6560         GList                *l;
6561         GtkFileChooserAction  fc_action;
6562         gboolean              is_dir;
6563         gboolean              is_native;
6564         
6565         if (response_id != GTK_RESPONSE_OK) {
6566                 gtk_widget_destroy (fc);
6567                 return;
6568         }
6569
6570         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
6571         target_file = g_file_new_for_uri (uri);
6572         g_object_get (G_OBJECT (fc), "action", &fc_action, NULL);
6573         is_dir = (fc_action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
6574         is_native = g_file_is_native (target_file);
6575         
6576         for (l = ev_window->priv->attach_list; l && l->data; l = g_list_next (l)) {
6577                 EvAttachment *attachment;
6578                 GFile        *save_to = NULL;
6579                 GError       *error = NULL;
6580                 
6581                 attachment = (EvAttachment *) l->data;
6582
6583                 if (is_native) {
6584                         if (is_dir) {
6585                                 save_to = g_file_get_child (target_file,
6586                                     /* FIXMEchpe: file name encoding! */
6587                                                             ev_attachment_get_name (attachment));
6588                         } else {
6589                                 save_to = g_object_ref (target_file);
6590                         }
6591                 } else {
6592                         save_to = ev_mkstemp_file ("saveattachment.XXXXXX", &error);
6593                 }
6594
6595                 if (save_to)
6596                         ev_attachment_save (attachment, save_to, &error);
6597                 
6598                 if (error) {
6599                         ev_window_error_message (ev_window, error, 
6600                                                  "%s", _("The attachment could not be saved."));
6601                         g_error_free (error);
6602                         g_object_unref (save_to);
6603
6604                         continue;
6605                 }
6606
6607                 if (!is_native) {
6608                         GFile *dest_file;
6609
6610                         if (is_dir) {
6611                                 dest_file = g_file_get_child (target_file,
6612                                                               ev_attachment_get_name (attachment));
6613                         } else {
6614                                 dest_file = g_object_ref (target_file);
6615                         }
6616
6617                         ev_window_save_remote (ev_window, EV_SAVE_ATTACHMENT,
6618                                                save_to, dest_file);
6619
6620                         g_object_unref (dest_file);
6621                 }
6622
6623                 g_object_unref (save_to);
6624         }
6625
6626         g_free (uri);
6627         g_object_unref (target_file);
6628
6629         gtk_widget_destroy (fc);
6630 }
6631
6632 static void
6633 ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
6634 {
6635         GtkWidget    *fc;
6636         EvAttachment *attachment = NULL;
6637
6638         if (!window->priv->attach_list)
6639                 return;
6640
6641         if (g_list_length (window->priv->attach_list) == 1)
6642                 attachment = (EvAttachment *) window->priv->attach_list->data;
6643         
6644         fc = gtk_file_chooser_dialog_new (
6645                 _("Save Attachment"),
6646                 GTK_WINDOW (window),
6647                 attachment ? GTK_FILE_CHOOSER_ACTION_SAVE : GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
6648                 GTK_STOCK_CANCEL,
6649                 GTK_RESPONSE_CANCEL,
6650                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
6651                 NULL);
6652
6653         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
6654         gtk_dialog_set_alternative_button_order (GTK_DIALOG (fc),
6655                                                  GTK_RESPONSE_OK,
6656                                                  GTK_RESPONSE_CANCEL,
6657                                                  -1);
6658
6659         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
6660         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
6661
6662         if (attachment)
6663                 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc),
6664                                                    ev_attachment_get_name (attachment));
6665
6666         g_signal_connect (fc, "response",
6667                           G_CALLBACK (attachment_save_dialog_response_cb),
6668                           window);
6669
6670         gtk_widget_show (fc);
6671 }
6672
6673 static void
6674 ev_window_media_player_key_pressed (EvWindow    *window,
6675                                     const gchar *key,
6676                                     gpointer     user_data)
6677 {
6678         if (!gtk_window_is_active (GTK_WINDOW (window))) 
6679                 return;
6680         
6681         /* Note how Previous/Next only go to the
6682          * next/previous page despite their icon telling you
6683          * they should go to the beginning/end.
6684          *
6685          * There's very few keyboards with FFW/RWD though,
6686          * so we stick the most useful keybinding on the most
6687          * often seen keys
6688          */
6689         if (strcmp (key, "Play") == 0) {
6690                 ev_window_run_presentation (window);
6691         } else if (strcmp (key, "Previous") == 0) {
6692                 ev_window_cmd_go_previous_page (NULL, window);
6693         } else if (strcmp (key, "Next") == 0) {
6694                 ev_window_cmd_go_next_page (NULL, window);
6695         } else if (strcmp (key, "FastForward") == 0) {
6696                 ev_window_cmd_go_last_page (NULL, window);
6697         } else if (strcmp (key, "Rewind") == 0) {
6698                 ev_window_cmd_go_first_page (NULL, window);
6699         }
6700 }
6701
6702 static EggToolbarsModel *
6703 get_toolbars_model (void)
6704 {
6705         EggToolbarsModel *toolbars_model;
6706         gchar            *toolbars_file;
6707         gchar            *toolbars_path;
6708         gint              i;
6709
6710         toolbars_model = egg_toolbars_model_new ();
6711
6712         toolbars_file = g_build_filename (ev_application_get_dot_dir (EV_APP, FALSE),
6713                                           "evince_toolbar.xml", NULL);
6714         toolbars_path = g_build_filename (ev_application_get_data_dir (EV_APP),
6715                                          "evince-toolbar.xml", NULL);
6716         egg_toolbars_model_load_names (toolbars_model, toolbars_path);
6717
6718         if (!egg_toolbars_model_load_toolbars (toolbars_model, toolbars_file)) {
6719                 egg_toolbars_model_load_toolbars (toolbars_model, toolbars_path);
6720                 goto skip_conversion;
6721         }
6722
6723         /* Open item doesn't exist anymore,
6724          * convert it to OpenRecent for compatibility
6725          */
6726         for (i = 0; i < egg_toolbars_model_n_items (toolbars_model, 0); i++) {
6727                 const gchar *item;
6728
6729                 item = egg_toolbars_model_item_nth (toolbars_model, 0, i);
6730                 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
6731                         egg_toolbars_model_remove_item (toolbars_model, 0, i);
6732                         egg_toolbars_model_add_item (toolbars_model, 0, i,
6733                                                      "FileOpenRecent");
6734                         egg_toolbars_model_save_toolbars (toolbars_model, toolbars_file, "1.0");
6735                         break;
6736                 }
6737         }
6738
6739     skip_conversion:
6740         g_free (toolbars_file);
6741         g_free (toolbars_path);
6742
6743         egg_toolbars_model_set_flags (toolbars_model, 0, EGG_TB_MODEL_NOT_REMOVABLE);
6744
6745         return toolbars_model;
6746 }
6747
6748 #ifdef ENABLE_DBUS
6749 static void
6750 ev_window_sync_source (EvWindow     *window,
6751                        EvSourceLink *link)
6752 {
6753         GDBusConnection *connection;
6754         GError          *error = NULL;
6755         guint32          timestamp;
6756         gchar           *uri_input;
6757         GFile           *input_gfile;
6758
6759         if (window->priv->dbus_object_id <= 0)
6760                 return;
6761
6762         connection = ev_application_get_dbus_connection (EV_APP);
6763         if (!connection)
6764                 return;
6765
6766         timestamp = gtk_get_current_event_time ();
6767         if (g_path_is_absolute (link->filename)) {
6768                 input_gfile = g_file_new_for_path (link->filename);
6769         } else {
6770                 GFile *gfile, *parent_gfile;
6771
6772                 gfile = g_file_new_for_uri (window->priv->uri);
6773                 parent_gfile = g_file_get_parent (gfile);
6774
6775                 /* parent_gfile should never be NULL */
6776                 if (parent_gfile == NULL) {
6777                         g_printerr ("Document URI is '/'\n");
6778                         return;
6779                 }
6780
6781                 input_gfile = g_file_get_child (parent_gfile, link->filename);
6782                 g_object_unref (parent_gfile);
6783                 g_object_unref (gfile);
6784         }
6785
6786         uri_input = g_file_get_uri (input_gfile);
6787         g_object_unref (input_gfile);
6788
6789         g_dbus_connection_emit_signal (connection,
6790                                        NULL,
6791                                        window->priv->dbus_object_path,
6792                                        EV_WINDOW_DBUS_INTERFACE,
6793                                        "SyncSource",
6794                                        g_variant_new ("(s(ii)u)",
6795                                                       uri_input,
6796                                                       link->line,
6797                                                       link->col,
6798                                                       timestamp),
6799                                        &error);
6800         g_free (uri_input);
6801         if (error) {
6802                 g_printerr ("Failed to emit DBus signal SyncSource: %s\n",
6803                             error->message);
6804                 g_error_free (error);
6805         }
6806 }
6807
6808 static void
6809 ev_window_emit_closed (EvWindow *window)
6810 {
6811         GDBusConnection *connection;
6812         GError          *error = NULL;
6813
6814         if (window->priv->dbus_object_id <= 0)
6815                 return;
6816
6817         connection = ev_application_get_dbus_connection (EV_APP);
6818         if (!connection)
6819                 return;
6820
6821         g_dbus_connection_emit_signal (connection,
6822                                        NULL,
6823                                        window->priv->dbus_object_path,
6824                                        EV_WINDOW_DBUS_INTERFACE,
6825                                        "Closed",
6826                                        NULL,
6827                                        &error);
6828         if (error) {
6829                 g_printerr ("Failed to emit DBus signal Closed: %s\n",
6830                             error->message);
6831                 g_error_free (error);
6832
6833                 return;
6834         }
6835
6836         /* If this is the last window call g_dbus_connection_flush_sync()
6837          * to make sure the signal is emitted.
6838          */
6839         if (ev_application_get_n_windows (EV_APP) == 1)
6840                 g_dbus_connection_flush_sync (connection, NULL, NULL);
6841 }
6842
6843 static void
6844 ev_window_emit_doc_loaded (EvWindow *window)
6845 {
6846         GDBusConnection *connection;
6847         GError          *error = NULL;
6848
6849         if (window->priv->dbus_object_id <= 0)
6850                 return;
6851
6852         connection = ev_application_get_dbus_connection (EV_APP);
6853         if (!connection)
6854                 return;
6855
6856         g_dbus_connection_emit_signal (connection,
6857                                        NULL,
6858                                        window->priv->dbus_object_path,
6859                                        EV_WINDOW_DBUS_INTERFACE,
6860                                        "DocumentLoaded",
6861                                        g_variant_new("(s)", window->priv->uri),
6862                                        &error);
6863         if (error) {
6864                 g_printerr ("Failed to emit DBus signal DocumentLoaded: %s\n",
6865                             error->message);
6866                 g_error_free (error);
6867
6868                 return;
6869         }
6870 }
6871
6872 static void
6873 method_call_cb (GDBusConnection       *connection,
6874                 const gchar           *sender,
6875                 const gchar           *object_path,
6876                 const gchar           *interface_name,
6877                 const gchar           *method_name,
6878                 GVariant              *parameters,
6879                 GDBusMethodInvocation *invocation,
6880                 gpointer               user_data)
6881 {
6882         EvWindow *window = EV_WINDOW (user_data);
6883
6884         if (g_strcmp0 (method_name, "SyncView") != 0)
6885                 return;
6886
6887         if (window->priv->document && ev_document_has_synctex (window->priv->document)) {
6888                 EvSourceLink link;
6889                 guint32      timestamp;
6890
6891                 g_variant_get (parameters, "(&s(ii)u)", &link.filename, &link.line, &link.col, &timestamp);
6892                 ev_view_highlight_forward_search (EV_VIEW (window->priv->view), &link);
6893                 gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
6894         }
6895
6896         g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
6897 }
6898
6899 static const char introspection_xml[] =
6900         "<node>"
6901           "<interface name='org.gnome.evince.Window'>"
6902             "<method name='SyncView'>"
6903               "<arg type='s' name='source_file' direction='in'/>"
6904               "<arg type='(ii)' name='source_point' direction='in'/>"
6905               "<arg type='u' name='timestamp' direction='in'/>"
6906             "</method>"
6907             "<signal name='SyncSource'>"
6908               "<arg type='s' name='source_file' direction='out'/>"
6909               "<arg type='(ii)' name='source_point' direction='out'/>"
6910               "<arg type='u' name='timestamp' direction='out'/>"
6911             "</signal>"
6912             "<signal name='Closed'/>"
6913             "<signal name='DocumentLoaded'>"
6914               "<arg type='s' name='uri' direction='out'/>"
6915             "</signal>"
6916           "</interface>"
6917         "</node>";
6918
6919 static const GDBusInterfaceVTable interface_vtable = {
6920         method_call_cb,
6921         NULL,
6922         NULL
6923 };
6924
6925 static GDBusNodeInfo *introspection_data;
6926 #endif /* ENABLE_DBUS */
6927
6928 static void
6929 ev_window_init (EvWindow *ev_window)
6930 {
6931         GtkActionGroup *action_group;
6932         GtkAccelGroup *accel_group;
6933         GError *error = NULL;
6934         GtkWidget *sidebar_widget;
6935         GtkWidget *menuitem;
6936         EggToolbarsModel *toolbars_model;
6937         GObject *mpkeys;
6938         gchar *ui_path;
6939 #ifdef ENABLE_DBUS
6940         GDBusConnection *connection;
6941         static gint window_id = 0;
6942 #endif
6943
6944         g_signal_connect (ev_window, "configure_event",
6945                           G_CALLBACK (window_configure_event_cb), NULL);
6946         g_signal_connect (ev_window, "window_state_event",
6947                           G_CALLBACK (window_state_event_cb), NULL);
6948
6949         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
6950
6951 #ifdef ENABLE_DBUS
6952         connection = ev_application_get_dbus_connection (EV_APP);
6953         if (connection) {
6954                 if (!introspection_data) {
6955                         introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);
6956                         if (error) g_warning ("%s\n", error->message);
6957                 }
6958                 g_assert (introspection_data != NULL);
6959
6960                 ev_window->priv->dbus_object_path = g_strdup_printf (EV_WINDOW_DBUS_OBJECT_PATH, window_id++);
6961                 ev_window->priv->dbus_object_id =
6962                     g_dbus_connection_register_object (connection,
6963                                                        ev_window->priv->dbus_object_path,
6964                                                        introspection_data->interfaces[0],
6965                                                        &interface_vtable,
6966                                                        ev_window, NULL,
6967                                                        &error);
6968                 if (ev_window->priv->dbus_object_id == 0) {
6969                         g_printerr ("Failed to register bus object %s: %s\n",
6970                                     ev_window->priv->dbus_object_path, error->message);
6971                         g_error_free (error);
6972                         g_free (ev_window->priv->dbus_object_path);
6973                         ev_window->priv->dbus_object_path = NULL;
6974                         error = NULL;
6975                 }
6976         }
6977
6978 #endif /* ENABLE_DBUS */
6979
6980         ev_window->priv->model = ev_document_model_new ();
6981
6982         ev_window->priv->page_mode = PAGE_MODE_DOCUMENT;
6983         ev_window->priv->title = ev_window_title_new (ev_window);
6984
6985         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
6986         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
6987         gtk_widget_show (ev_window->priv->main_box);
6988
6989         action_group = gtk_action_group_new ("MenuActions");
6990         ev_window->priv->action_group = action_group;
6991         gtk_action_group_set_translation_domain (action_group, NULL);
6992         gtk_action_group_add_actions (action_group, entries,
6993                                       G_N_ELEMENTS (entries), ev_window);
6994         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
6995                                              G_N_ELEMENTS (toggle_entries),
6996                                              ev_window);
6997         set_action_properties (action_group);
6998         register_custom_actions (ev_window, action_group);
6999
7000         ev_window->priv->ui_manager = gtk_ui_manager_new ();
7001         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7002                                             action_group, 0);
7003
7004         accel_group =
7005                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
7006         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
7007
7008         ev_window_set_view_accels_sensitivity (ev_window, FALSE);
7009
7010         action_group = gtk_action_group_new ("ViewPopupActions");
7011         ev_window->priv->view_popup_action_group = action_group;
7012         gtk_action_group_set_translation_domain (action_group, NULL);
7013         gtk_action_group_add_actions (action_group, view_popup_entries,
7014                                       G_N_ELEMENTS (view_popup_entries),
7015                                       ev_window);
7016         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7017                                             action_group, 0);
7018
7019         action_group = gtk_action_group_new ("AttachmentPopupActions");
7020         ev_window->priv->attachment_popup_action_group = action_group;
7021         gtk_action_group_set_translation_domain (action_group, NULL);
7022         gtk_action_group_add_actions (action_group, attachment_popup_entries,
7023                                       G_N_ELEMENTS (attachment_popup_entries),
7024                                       ev_window);
7025         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7026                                             action_group, 0);
7027
7028         ui_path = g_build_filename (ev_application_get_data_dir (EV_APP),
7029                                     "evince-ui.xml", NULL);
7030         if (!gtk_ui_manager_add_ui_from_file (
7031                 ev_window->priv->ui_manager, ui_path, &error))
7032         {
7033                 g_warning ("building menus failed: %s", error->message);
7034                 g_error_free (error);
7035         }
7036         g_free (ui_path);
7037
7038         ev_window->priv->recent_manager = gtk_recent_manager_get_default ();
7039         ev_window->priv->recent_action_group = NULL;
7040         ev_window->priv->recent_ui_id = 0;
7041         g_signal_connect_swapped (ev_window->priv->recent_manager,
7042                                   "changed",
7043                                   G_CALLBACK (ev_window_setup_recent),
7044                                   ev_window);
7045
7046         ev_window->priv->menubar =
7047                  gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7048                                             "/MainMenu");
7049         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
7050                             ev_window->priv->menubar,
7051                             FALSE, FALSE, 0);
7052         menuitem = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7053                                               "/MainMenu/EditMenu/EditRotateLeftMenu");
7054         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menuitem), TRUE);
7055         menuitem = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7056                                               "/MainMenu/EditMenu/EditRotateRightMenu");
7057         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menuitem), TRUE);
7058
7059         toolbars_model = get_toolbars_model ();
7060         ev_window->priv->toolbar = GTK_WIDGET
7061                 (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
7062                                "ui-manager", ev_window->priv->ui_manager,
7063                                "popup-path", "/ToolbarPopup",
7064                                "model", toolbars_model,
7065                                NULL));
7066         g_object_unref (toolbars_model);
7067
7068         egg_editable_toolbar_show (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar),
7069                                    "DefaultToolBar");
7070         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
7071                             ev_window->priv->toolbar,
7072                             FALSE, FALSE, 0);
7073         gtk_widget_show (ev_window->priv->toolbar);
7074
7075         /* Add the main area */
7076         ev_window->priv->hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
7077         g_signal_connect (ev_window->priv->hpaned,
7078                           "notify::position",
7079                           G_CALLBACK (ev_window_sidebar_position_change_cb),
7080                           ev_window);
7081         
7082         gtk_paned_set_position (GTK_PANED (ev_window->priv->hpaned), SIDEBAR_DEFAULT_SIZE);
7083         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
7084                             TRUE, TRUE, 0);
7085         gtk_widget_show (ev_window->priv->hpaned);
7086         
7087         ev_window->priv->sidebar = ev_sidebar_new ();
7088         ev_sidebar_set_model (EV_SIDEBAR (ev_window->priv->sidebar),
7089                               ev_window->priv->model);
7090         gtk_paned_pack1 (GTK_PANED (ev_window->priv->hpaned),
7091                          ev_window->priv->sidebar, FALSE, FALSE);
7092         gtk_widget_show (ev_window->priv->sidebar);
7093
7094         /* Stub sidebar, for now */
7095
7096         sidebar_widget = ev_sidebar_thumbnails_new ();
7097         ev_window->priv->sidebar_thumbs = sidebar_widget;
7098         g_signal_connect (sidebar_widget,
7099                           "notify::main-widget",
7100                           G_CALLBACK (sidebar_page_main_widget_update_cb),
7101                           ev_window);
7102         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
7103         gtk_widget_show (sidebar_widget);
7104         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7105                              sidebar_widget);
7106
7107         sidebar_widget = ev_sidebar_links_new ();
7108         ev_window->priv->sidebar_links = sidebar_widget;
7109         g_signal_connect (sidebar_widget,
7110                           "notify::model",
7111                           G_CALLBACK (sidebar_widget_model_set),
7112                           ev_window);
7113         g_signal_connect (sidebar_widget,
7114                           "link_activated",
7115                           G_CALLBACK (sidebar_links_link_activated_cb),
7116                           ev_window);
7117         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
7118         gtk_widget_show (sidebar_widget);
7119         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7120                              sidebar_widget);
7121
7122         sidebar_widget = ev_sidebar_attachments_new ();
7123         ev_window->priv->sidebar_attachments = sidebar_widget;
7124         g_signal_connect_object (sidebar_widget,
7125                                  "popup",
7126                                  G_CALLBACK (attachment_bar_menu_popup_cb),
7127                                  ev_window, 0);
7128         gtk_widget_show (sidebar_widget);
7129         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7130                              sidebar_widget);
7131
7132         sidebar_widget = ev_sidebar_layers_new ();
7133         ev_window->priv->sidebar_layers = sidebar_widget;
7134         g_signal_connect (sidebar_widget,
7135                           "layers_visibility_changed",
7136                           G_CALLBACK (sidebar_layers_visibility_changed),
7137                           ev_window);
7138         gtk_widget_show (sidebar_widget);
7139         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7140                              sidebar_widget);
7141
7142         sidebar_widget = ev_sidebar_annotations_new ();
7143         ev_window->priv->sidebar_annots = sidebar_widget;
7144         g_signal_connect (sidebar_widget,
7145                           "annot_activated",
7146                           G_CALLBACK (sidebar_annots_annot_activated_cb),
7147                           ev_window);
7148         g_signal_connect (sidebar_widget,
7149                           "begin_annot_add",
7150                           G_CALLBACK (sidebar_annots_begin_annot_add),
7151                           ev_window);
7152         g_signal_connect (sidebar_widget,
7153                           "annot_add_cancelled",
7154                           G_CALLBACK (sidebar_annots_annot_add_cancelled),
7155                           ev_window);
7156         gtk_widget_show (sidebar_widget);
7157         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7158                              sidebar_widget);
7159
7160         sidebar_widget = ev_sidebar_bookmarks_new ();
7161         ev_window->priv->sidebar_bookmarks = sidebar_widget;
7162         g_signal_connect (sidebar_widget,
7163                           "add-bookmark",
7164                           G_CALLBACK (sidebar_bookmarks_add_bookmark),
7165                           ev_window);
7166         gtk_widget_show (sidebar_widget);
7167         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7168                              sidebar_widget);
7169
7170         ev_window->priv->view_box = gtk_vbox_new (FALSE, 0);
7171         ev_window->priv->scrolled_window =
7172                 GTK_WIDGET (g_object_new (GTK_TYPE_SCROLLED_WINDOW,
7173                                           "shadow-type", GTK_SHADOW_IN,
7174                                           NULL));
7175         gtk_box_pack_start (GTK_BOX (ev_window->priv->view_box),
7176                             ev_window->priv->scrolled_window,
7177                             TRUE, TRUE, 0);
7178         gtk_widget_show (ev_window->priv->scrolled_window);
7179
7180         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
7181                         ev_window->priv->view_box);
7182         gtk_widget_show (ev_window->priv->view_box);
7183
7184         ev_window->priv->view = ev_view_new ();
7185         ev_view_set_page_cache_size (EV_VIEW (ev_window->priv->view), PAGE_CACHE_SIZE);
7186         ev_view_set_model (EV_VIEW (ev_window->priv->view), ev_window->priv->model);
7187
7188         ev_window->priv->password_view = ev_password_view_new (GTK_WINDOW (ev_window));
7189         g_signal_connect_swapped (ev_window->priv->password_view,
7190                                   "unlock",
7191                                   G_CALLBACK (ev_window_password_view_unlock),
7192                                   ev_window);
7193         g_signal_connect_object (ev_window->priv->view, "focus_in_event",
7194                                  G_CALLBACK (view_actions_focus_in_cb),
7195                                  ev_window, 0);
7196         g_signal_connect_object (ev_window->priv->view, "focus_out_event",
7197                                  G_CALLBACK (view_actions_focus_out_cb),
7198                                  ev_window, 0);
7199         g_signal_connect_swapped (ev_window->priv->view, "external-link",
7200                                   G_CALLBACK (view_external_link_cb),
7201                                   ev_window);
7202         g_signal_connect_object (ev_window->priv->view, "handle-link",
7203                                  G_CALLBACK (view_handle_link_cb),
7204                                  ev_window, 0);
7205         g_signal_connect_object (ev_window->priv->view, "popup",
7206                                  G_CALLBACK (view_menu_popup_cb),
7207                                  ev_window, 0);
7208         g_signal_connect_object (ev_window->priv->view, "selection-changed",
7209                                  G_CALLBACK (view_selection_changed_cb),
7210                                  ev_window, 0);
7211         g_signal_connect_object (ev_window->priv->view, "annot-added",
7212                                  G_CALLBACK (view_annot_added),
7213                                  ev_window, 0);
7214         g_signal_connect_object (ev_window->priv->view, "layers-changed",
7215                                  G_CALLBACK (view_layers_changed_cb),
7216                                  ev_window, 0);
7217 #ifdef ENABLE_DBUS
7218         g_signal_connect_swapped (ev_window->priv->view, "sync-source",
7219                                   G_CALLBACK (ev_window_sync_source),
7220                                   ev_window);
7221 #endif
7222         gtk_widget_show (ev_window->priv->view);
7223         gtk_widget_show (ev_window->priv->password_view);
7224
7225         /* Find Bar */
7226         ev_window->priv->find_bar = egg_find_bar_new ();
7227         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
7228                           ev_window->priv->find_bar,
7229                           FALSE, TRUE, 0);
7230
7231         /* We own a ref on these widgets, as we can swap them in and out */
7232         g_object_ref (ev_window->priv->view);
7233         g_object_ref (ev_window->priv->password_view);
7234
7235         gtk_container_add (GTK_CONTAINER (ev_window->priv->scrolled_window),
7236                            ev_window->priv->view);
7237
7238         /* Connect to model signals */
7239         g_signal_connect_swapped (ev_window->priv->model,
7240                                   "page-changed",
7241                                   G_CALLBACK (ev_window_page_changed_cb),
7242                                   ev_window);
7243         g_signal_connect (ev_window->priv->model,
7244                           "notify::document",
7245                           G_CALLBACK (ev_window_document_changed_cb),
7246                           ev_window);
7247         g_signal_connect (ev_window->priv->model,
7248                           "notify::scale",
7249                           G_CALLBACK (ev_window_zoom_changed_cb),
7250                           ev_window);
7251         g_signal_connect (ev_window->priv->model,
7252                           "notify::sizing-mode",
7253                           G_CALLBACK (ev_window_sizing_mode_changed_cb),
7254                           ev_window);
7255         g_signal_connect (ev_window->priv->model,
7256                           "notify::rotation",
7257                           G_CALLBACK (ev_window_rotation_changed_cb),
7258                           ev_window);
7259         g_signal_connect (ev_window->priv->model,
7260                           "notify::continuous",
7261                           G_CALLBACK (ev_window_continuous_changed_cb),
7262                           ev_window);
7263         g_signal_connect (ev_window->priv->model,
7264                           "notify::dual-page",
7265                           G_CALLBACK (ev_window_dual_mode_changed_cb),
7266                           ev_window);
7267         g_signal_connect (ev_window->priv->model,
7268                           "notify::inverted-colors",
7269                           G_CALLBACK (ev_window_inverted_colors_changed_cb),
7270                           ev_window);
7271
7272         /* Connect sidebar signals */
7273         g_signal_connect (ev_window->priv->sidebar,
7274                           "notify::visible",
7275                           G_CALLBACK (ev_window_sidebar_visibility_changed_cb),
7276                           ev_window);
7277         g_signal_connect (ev_window->priv->sidebar,
7278                           "notify::current-page",
7279                           G_CALLBACK (ev_window_sidebar_current_page_changed_cb),
7280                           ev_window);
7281
7282         /* Connect to find bar signals */
7283         g_signal_connect (ev_window->priv->find_bar,
7284                           "previous",
7285                           G_CALLBACK (find_bar_previous_cb),
7286                           ev_window);
7287         g_signal_connect (ev_window->priv->find_bar,
7288                           "next",
7289                           G_CALLBACK (find_bar_next_cb),
7290                           ev_window);
7291         g_signal_connect (ev_window->priv->find_bar,
7292                           "close",
7293                           G_CALLBACK (find_bar_close_cb),
7294                           ev_window);
7295         g_signal_connect (ev_window->priv->find_bar,
7296                           "notify::search-string",
7297                           G_CALLBACK (find_bar_search_changed_cb),
7298                           ev_window);
7299         g_signal_connect (ev_window->priv->find_bar,
7300                           "notify::case-sensitive",
7301                           G_CALLBACK (find_bar_search_changed_cb),
7302                           ev_window);
7303         g_signal_connect (ev_window->priv->find_bar,
7304                           "notify::visible",
7305                           G_CALLBACK (find_bar_visibility_changed_cb),
7306                           ev_window);
7307         g_signal_connect (ev_window->priv->find_bar,
7308                           "scroll",
7309                           G_CALLBACK (find_bar_scroll),
7310                           ev_window);
7311
7312         /* Popups */
7313         ev_window->priv->view_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7314                                                                  "/DocumentPopup");
7315         ev_window->priv->link = NULL;
7316
7317         ev_window->priv->attachment_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7318                                                                        "/AttachmentPopup");
7319         ev_window->priv->attach_list = NULL;
7320
7321         /* Media player keys */
7322         mpkeys = ev_application_get_media_keys (EV_APP);
7323         if (mpkeys) {
7324                 g_signal_connect_swapped (mpkeys, "key_pressed",
7325                                           G_CALLBACK (ev_window_media_player_key_pressed),
7326                                           ev_window);
7327         }
7328
7329         /* Give focus to the document view */
7330         gtk_widget_grab_focus (ev_window->priv->view);
7331
7332         ev_window->priv->default_settings = g_settings_new (GS_SCHEMA_NAME".Default");
7333         g_settings_delay (ev_window->priv->default_settings);
7334
7335         /* Set it user interface params */
7336         ev_window_setup_recent (ev_window);
7337
7338         ev_window_setup_gtk_settings (ev_window);
7339
7340         setup_chrome_from_metadata (ev_window);
7341         update_chrome_actions (ev_window);
7342         update_chrome_visibility (ev_window);
7343
7344         gtk_window_set_default_size (GTK_WINDOW (ev_window), 600, 600);
7345
7346         setup_view_from_metadata (ev_window);
7347         setup_sidebar_from_metadata (ev_window);
7348
7349         ev_window_sizing_mode_changed_cb (ev_window->priv->model, NULL, ev_window);
7350         ev_window_setup_action_sensitivity (ev_window);
7351
7352         /* Drag and Drop */
7353         gtk_drag_dest_set (GTK_WIDGET (ev_window),
7354                            GTK_DEST_DEFAULT_ALL,
7355                            NULL, 0,
7356                            GDK_ACTION_COPY);
7357         gtk_drag_dest_add_uri_targets (GTK_WIDGET (ev_window));
7358 }
7359
7360 /**
7361  * ev_window_new:
7362  *
7363  * Creates a #GtkWidget that represents the window.
7364  *
7365  * Returns: the #GtkWidget that represents the window.
7366  */
7367 GtkWidget *
7368 ev_window_new (void)
7369 {
7370         GtkWidget *ev_window;
7371
7372         ev_window = GTK_WIDGET (g_object_new (EV_TYPE_WINDOW,
7373                                               "type", GTK_WINDOW_TOPLEVEL,
7374                                               NULL));
7375
7376         return ev_window;
7377 }
7378
7379 const gchar *
7380 ev_window_get_dbus_object_path (EvWindow *ev_window)
7381 {
7382 #ifdef ENABLE_DBUS
7383         return ev_window->priv->dbus_object_path;
7384 #else
7385         return NULL;
7386 #endif
7387 }