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