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