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