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