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