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