]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
Don't jump to find result when the findbar is shown with search text
[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 ev_window_search_start (EvWindow *ev_window)
5191 {
5192         EggFindBar *find_bar = EGG_FIND_BAR (ev_window->priv->find_bar);
5193         const char *search_string;
5194
5195         if (!ev_window->priv->document || !EV_IS_DOCUMENT_FIND (ev_window->priv->document))
5196                 return;
5197
5198         search_string = egg_find_bar_get_search_string (find_bar);
5199
5200         ev_window_clear_find_job (ev_window);
5201         if (search_string && search_string[0]) {
5202                 ev_window->priv->find_job = ev_job_find_new (ev_window->priv->document,
5203                                                              ev_document_model_get_page (ev_window->priv->model),
5204                                                              ev_document_get_n_pages (ev_window->priv->document),
5205                                                              search_string,
5206                                                              egg_find_bar_get_case_sensitive (find_bar));
5207                 g_signal_connect (ev_window->priv->find_job, "finished",
5208                                   G_CALLBACK (ev_window_find_job_finished_cb),
5209                                   ev_window);
5210                 g_signal_connect (ev_window->priv->find_job, "updated",
5211                                   G_CALLBACK (ev_window_find_job_updated_cb),
5212                                   ev_window);
5213                 ev_job_scheduler_push_job (ev_window->priv->find_job, EV_JOB_PRIORITY_NONE);
5214         } else {
5215                 ev_window_update_actions (ev_window);
5216                 egg_find_bar_set_status_text (find_bar, NULL);
5217                 gtk_widget_queue_draw (GTK_WIDGET (ev_window->priv->view));
5218         }
5219 }
5220
5221 static void
5222 find_bar_search_changed_cb (EggFindBar *find_bar,
5223                             GParamSpec *param,
5224                             EvWindow   *ev_window)
5225 {
5226         /* Either the string or case sensitivity could have changed. */
5227         ev_view_find_search_changed (EV_VIEW (ev_window->priv->view));
5228         ev_window_search_start (ev_window);
5229 }
5230
5231 static void
5232 find_bar_visibility_changed_cb (EggFindBar *find_bar,
5233                                 GParamSpec *param,
5234                                 EvWindow   *ev_window)
5235 {
5236         gboolean visible;
5237
5238         visible = gtk_widget_get_visible (GTK_WIDGET (find_bar));
5239
5240         if (ev_window->priv->document &&
5241             EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
5242                 ev_view_find_set_highlight_search (EV_VIEW (ev_window->priv->view), visible);
5243                 ev_window_update_actions (ev_window);
5244
5245                 if (visible)
5246                         ev_window_search_start (ev_window);
5247                 else
5248                         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar), NULL);
5249         }
5250 }
5251
5252 static void
5253 find_bar_scroll (EggFindBar   *find_bar,
5254                  GtkScrollType scroll,
5255                  EvWindow     *ev_window)
5256 {
5257         ev_view_scroll (EV_VIEW (ev_window->priv->view), scroll, FALSE);
5258 }
5259
5260 static void
5261 zoom_control_changed_cb (EphyZoomAction *action,
5262                          float           zoom,
5263                          EvWindow       *ev_window)
5264 {
5265         EvSizingMode mode;
5266
5267         if (zoom == EPHY_ZOOM_BEST_FIT) {
5268                 mode = EV_SIZING_BEST_FIT;
5269         } else if (zoom == EPHY_ZOOM_FIT_WIDTH) {
5270                 mode = EV_SIZING_FIT_WIDTH;
5271         } else {
5272                 mode = EV_SIZING_FREE;
5273         }
5274
5275         ev_document_model_set_sizing_mode (ev_window->priv->model, mode);
5276
5277         if (mode == EV_SIZING_FREE) {
5278                 ev_document_model_set_scale (ev_window->priv->model,
5279                                              zoom * get_screen_dpi (ev_window) / 72.0);
5280         }
5281 }
5282
5283 static void
5284 ev_window_drag_data_received (GtkWidget        *widget,
5285                               GdkDragContext   *context,
5286                               gint              x,
5287                               gint              y,
5288                               GtkSelectionData *selection_data,
5289                               guint             info,
5290                               guint             time)
5291
5292 {
5293         EvWindow  *window = EV_WINDOW (widget);
5294         gchar    **uris;
5295         gint       i = 0;
5296         GSList    *uri_list = NULL;
5297         GtkWidget *source;
5298
5299         source = gtk_drag_get_source_widget (context);
5300         if (source && widget == gtk_widget_get_toplevel (source)) {
5301                 gtk_drag_finish (context, FALSE, FALSE, time);
5302                 return;
5303         }
5304
5305         uris = gtk_selection_data_get_uris (selection_data);
5306         if (!uris) {
5307                 gtk_drag_finish (context, FALSE, FALSE, time);
5308                 return;
5309         }
5310
5311         for (i = 0; uris[i]; i++) {
5312                 uri_list = g_slist_prepend (uri_list, (gpointer) uris[i]);
5313         }
5314
5315         ev_application_open_uri_list (EV_APP, uri_list,
5316                                       gtk_window_get_screen (GTK_WINDOW (window)),
5317                                       0);
5318         gtk_drag_finish (context, TRUE, FALSE, time);
5319
5320         g_strfreev (uris);
5321         g_slist_free (uri_list);
5322 }
5323
5324 static void
5325 ev_window_finalize (GObject *object)
5326 {
5327         G_OBJECT_CLASS (ev_window_parent_class)->finalize (object);
5328
5329         if (ev_window_n_copies == 0) {
5330                 ev_application_shutdown (EV_APP);
5331         } else {
5332                 ev_window_n_copies--;
5333         }
5334 }
5335
5336 static void
5337 ev_window_dispose (GObject *object)
5338 {
5339         EvWindow *window = EV_WINDOW (object);
5340         EvWindowPrivate *priv = window->priv;
5341         GObject *mpkeys = ev_application_get_media_keys (EV_APP);
5342
5343         if (mpkeys) {
5344                 g_signal_handlers_disconnect_by_func (mpkeys,
5345                                                       ev_window_media_player_key_pressed,
5346                                                       window);
5347         }
5348
5349 #ifdef ENABLE_DBUS
5350         if (priv->dbus_object_id > 0) {
5351                 ev_window_emit_closed (window);
5352                 g_dbus_connection_unregister_object (ev_application_get_dbus_connection (EV_APP),
5353                                                      priv->dbus_object_id);
5354                 priv->dbus_object_id = 0;
5355         }
5356
5357         if (priv->dbus_object_path) {
5358                 g_free (priv->dbus_object_path);
5359                 priv->dbus_object_path = NULL;
5360         }
5361 #endif /* ENABLE_DBUS */
5362
5363         if (priv->bookmarks) {
5364                 g_object_unref (priv->bookmarks);
5365                 priv->bookmarks = NULL;
5366         }
5367
5368         if (priv->metadata) {
5369                 g_object_unref (priv->metadata);
5370                 priv->metadata = NULL;
5371         }
5372
5373         if (priv->setup_document_idle > 0) {
5374                 g_source_remove (priv->setup_document_idle);
5375                 priv->setup_document_idle = 0;
5376         }
5377
5378         if (priv->monitor) {
5379                 g_object_unref (priv->monitor);
5380                 priv->monitor = NULL;
5381         }
5382         
5383         if (priv->title) {
5384                 ev_window_title_free (priv->title);
5385                 priv->title = NULL;
5386         }
5387
5388         if (priv->ui_manager) {
5389                 g_object_unref (priv->ui_manager);
5390                 priv->ui_manager = NULL;
5391         }
5392
5393         if (priv->action_group) {
5394                 g_object_unref (priv->action_group);
5395                 priv->action_group = NULL;
5396         }
5397
5398         if (priv->view_popup_action_group) {
5399                 g_object_unref (priv->view_popup_action_group);
5400                 priv->view_popup_action_group = NULL;
5401         }
5402
5403         if (priv->attachment_popup_action_group) {
5404                 g_object_unref (priv->attachment_popup_action_group);
5405                 priv->attachment_popup_action_group = NULL;
5406         }
5407
5408         if (priv->recent_action_group) {
5409                 g_object_unref (priv->recent_action_group);
5410                 priv->recent_action_group = NULL;
5411         }
5412
5413         if (priv->bookmarks_action_group) {
5414                 g_object_unref (priv->bookmarks_action_group);
5415                 priv->bookmarks_action_group = NULL;
5416         }
5417
5418         if (priv->recent_manager) {
5419                 g_signal_handlers_disconnect_by_func (priv->recent_manager,
5420                                                       ev_window_setup_recent,
5421                                                       window);
5422                 priv->recent_manager = NULL;
5423         }
5424
5425         if (priv->settings) {
5426                 g_object_unref (priv->settings);
5427                 priv->settings = NULL;
5428         }
5429
5430         if (priv->default_settings) {
5431                 g_settings_apply (priv->default_settings);
5432                 g_object_unref (priv->default_settings);
5433                 priv->default_settings = NULL;
5434         }
5435
5436         if (priv->lockdown_settings) {
5437                 g_object_unref (priv->lockdown_settings);
5438                 priv->lockdown_settings = NULL;
5439         }
5440
5441         priv->recent_ui_id = 0;
5442
5443         if (priv->model) {
5444                 g_signal_handlers_disconnect_by_func (priv->model,
5445                                                       ev_window_page_changed_cb,
5446                                                       window);
5447                 g_object_unref (priv->model);
5448                 priv->model = NULL;
5449         }
5450
5451         if (priv->document) {
5452                 g_object_unref (priv->document);
5453                 priv->document = NULL;
5454         }
5455         
5456         if (priv->view) {
5457                 g_object_unref (priv->view);
5458                 priv->view = NULL;
5459         }
5460
5461         if (priv->password_view) {
5462                 g_object_unref (priv->password_view);
5463                 priv->password_view = NULL;
5464         }
5465
5466         if (priv->load_job) {
5467                 ev_window_clear_load_job (window);
5468         }
5469
5470         if (priv->reload_job) {
5471                 ev_window_clear_reload_job (window);
5472         }
5473
5474         if (priv->save_job) {
5475                 ev_window_clear_save_job (window);
5476         }
5477
5478         if (priv->thumbnail_job) {
5479                 ev_window_clear_thumbnail_job (window);
5480         }
5481
5482         if (priv->find_job) {
5483                 ev_window_clear_find_job (window);
5484         }
5485         
5486         if (priv->local_uri) {
5487                 ev_window_clear_local_uri (window);
5488                 priv->local_uri = NULL;
5489         }
5490
5491         ev_window_clear_progress_idle (window);
5492         if (priv->progress_cancellable) {
5493                 g_object_unref (priv->progress_cancellable);
5494                 priv->progress_cancellable = NULL;
5495         }
5496         
5497         ev_window_close_dialogs (window);
5498
5499         if (priv->link) {
5500                 g_object_unref (priv->link);
5501                 priv->link = NULL;
5502         }
5503
5504         if (priv->image) {
5505                 g_object_unref (priv->image);
5506                 priv->image = NULL;
5507         }
5508
5509         if (priv->annot) {
5510                 g_object_unref (priv->annot);
5511                 priv->annot = NULL;
5512         }
5513
5514         if (priv->attach_list) {
5515                 g_list_foreach (priv->attach_list,
5516                                 (GFunc) g_object_unref,
5517                                 NULL);
5518                 g_list_free (priv->attach_list);
5519                 priv->attach_list = NULL;
5520         }
5521
5522         if (priv->find_bar) {
5523                 g_signal_handlers_disconnect_by_func
5524                         (window->priv->find_bar,
5525                          G_CALLBACK (find_bar_close_cb),
5526                          window);
5527                 priv->find_bar = NULL;
5528         }
5529
5530         if (priv->uri) {
5531                 g_free (priv->uri);
5532                 priv->uri = NULL;
5533         }
5534
5535         if (priv->search_string) {
5536                 g_free (priv->search_string);
5537                 priv->search_string = NULL;
5538         }
5539         
5540         if (priv->dest) {
5541                 g_object_unref (priv->dest);
5542                 priv->dest = NULL;
5543         }
5544
5545         if (priv->history) {
5546                 g_object_unref (priv->history);
5547                 priv->history = NULL;
5548         }
5549
5550         if (priv->print_queue) {
5551                 g_queue_free (priv->print_queue);
5552                 priv->print_queue = NULL;
5553         }
5554
5555         G_OBJECT_CLASS (ev_window_parent_class)->dispose (object);
5556 }
5557
5558 static void
5559 menubar_deactivate_cb (GtkWidget *menubar,
5560                        EvWindow  *window)
5561 {
5562         g_signal_handlers_disconnect_by_func (menubar,
5563                                               G_CALLBACK (menubar_deactivate_cb),
5564                                               window);
5565
5566         gtk_menu_shell_deselect (GTK_MENU_SHELL (menubar));
5567
5568         update_chrome_visibility (window);
5569 }
5570
5571 static gboolean
5572 ev_window_key_press_event (GtkWidget   *widget,
5573                            GdkEventKey *event)
5574 {
5575         EvWindow        *ev_window = EV_WINDOW (widget);
5576         EvWindowPrivate *priv = ev_window->priv;
5577         gboolean         handled = FALSE;
5578
5579         /* Propagate the event to the view first
5580          * It's needed to be able to type in
5581          * annot popups windows
5582          */
5583         if (priv->view) {
5584                 g_object_ref (priv->view);
5585                 if (gtk_widget_is_sensitive (priv->view))
5586                         handled = gtk_widget_event (priv->view, (GdkEvent*) event);
5587                 g_object_unref (priv->view);
5588         }
5589
5590         if (!handled && !EV_WINDOW_IS_PRESENTATION (ev_window)) {
5591                 guint modifier = event->state & gtk_accelerator_get_default_mod_mask ();
5592
5593                 if (priv->menubar_accel_keyval != 0 &&
5594                     event->keyval == priv->menubar_accel_keyval &&
5595                     modifier == priv->menubar_accel_modifier) {
5596                         if (!gtk_widget_get_visible (priv->menubar)) {
5597                                 g_signal_connect (priv->menubar, "deactivate",
5598                                                   G_CALLBACK (menubar_deactivate_cb),
5599                                                   ev_window);
5600
5601                                 gtk_widget_show (priv->menubar);
5602                                 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menubar),
5603                                                              FALSE);
5604
5605                                 handled = TRUE;
5606                         }
5607                 }
5608         }
5609
5610         if (!handled)
5611                 handled = GTK_WIDGET_CLASS (ev_window_parent_class)->key_press_event (widget, event);
5612
5613         return handled;
5614 }
5615
5616 static gboolean
5617 ev_window_delete_event (GtkWidget   *widget,
5618                         GdkEventAny *event)
5619 {
5620         return !ev_window_close (EV_WINDOW (widget));
5621 }
5622
5623 static void
5624 ev_window_class_init (EvWindowClass *ev_window_class)
5625 {
5626         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_window_class);
5627         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (ev_window_class);
5628
5629         g_object_class->dispose = ev_window_dispose;
5630         g_object_class->finalize = ev_window_finalize;
5631
5632         widget_class->delete_event = ev_window_delete_event;
5633         widget_class->key_press_event = ev_window_key_press_event;
5634         widget_class->screen_changed = ev_window_screen_changed;
5635         widget_class->window_state_event = ev_window_state_event;
5636         widget_class->drag_data_received = ev_window_drag_data_received;
5637
5638         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
5639 }
5640
5641 /* Normal items */
5642 static const GtkActionEntry entries[] = {
5643         { "File", NULL, N_("_File") },
5644         { "Edit", NULL, N_("_Edit") },
5645         { "View", NULL, N_("_View") },
5646         { "Go", NULL, N_("_Go") },
5647         { "Bookmarks", NULL, N_("_Bookmarks") },
5648         { "Help", NULL, N_("_Help") },
5649
5650         /* File menu */
5651         { "FileOpen", GTK_STOCK_OPEN, N_("_Open…"), "<control>O",
5652           N_("Open an existing document"),
5653           G_CALLBACK (ev_window_cmd_file_open) },
5654         { "FileOpenCopy", NULL, N_("Op_en a Copy"), "<control>N",
5655           N_("Open a copy of the current document in a new window"),
5656           G_CALLBACK (ev_window_cmd_file_open_copy) },
5657         { "FileSaveAs", GTK_STOCK_SAVE_AS, N_("_Save a Copy…"), "<control>S",
5658           N_("Save a copy of the current document"),
5659           G_CALLBACK (ev_window_cmd_save_as) },
5660         { "FileOpenContainingFolder", GTK_STOCK_DIRECTORY, N_("Open Containing _Folder"), NULL,
5661           N_("Show the folder which contains this file in the file manager"),
5662           G_CALLBACK (ev_window_cmd_open_containing_folder) },
5663         { "FilePrint", GTK_STOCK_PRINT, N_("_Print…"), "<control>P",
5664           N_("Print this document"),
5665           G_CALLBACK (ev_window_cmd_file_print) },
5666         { "FileProperties", GTK_STOCK_PROPERTIES, N_("P_roperties"), "<alt>Return", NULL,
5667           G_CALLBACK (ev_window_cmd_file_properties) },                       
5668         { "FileCloseWindow", GTK_STOCK_CLOSE, NULL, "<control>W", NULL,
5669           G_CALLBACK (ev_window_cmd_file_close_window) },
5670
5671         /* Edit menu */
5672         { "EditCopy", GTK_STOCK_COPY, NULL, "<control>C", NULL,
5673           G_CALLBACK (ev_window_cmd_edit_copy) },
5674         { "EditSelectAll", GTK_STOCK_SELECT_ALL, N_("Select _All"), "<control>A", NULL,
5675           G_CALLBACK (ev_window_cmd_edit_select_all) },
5676         { "EditFind", GTK_STOCK_FIND, N_("_Find…"), "<control>F",
5677           N_("Find a word or phrase in the document"),
5678           G_CALLBACK (ev_window_cmd_edit_find) },
5679         { "EditFindNext", NULL, N_("Find Ne_xt"), "<control>G", NULL,
5680           G_CALLBACK (ev_window_cmd_edit_find_next) },
5681         { "EditFindPrevious", NULL, N_("Find Pre_vious"), "<shift><control>G", NULL,
5682           G_CALLBACK (ev_window_cmd_edit_find_previous) },
5683         { "EditToolbar", NULL, N_("T_oolbar"), NULL, NULL,
5684           G_CALLBACK (ev_window_cmd_edit_toolbar) },
5685         { "EditRotateLeft", EV_STOCK_ROTATE_LEFT, N_("Rotate _Left"), "<control>Left", NULL,
5686           G_CALLBACK (ev_window_cmd_edit_rotate_left) },
5687         { "EditRotateRight", EV_STOCK_ROTATE_RIGHT, N_("Rotate _Right"), "<control>Right", NULL,
5688           G_CALLBACK (ev_window_cmd_edit_rotate_right) },
5689         { "EditSaveSettings", NULL, N_("Save Current Settings as _Default"), "<control>T", NULL,
5690           G_CALLBACK (ev_window_cmd_edit_save_settings) },
5691
5692
5693         /* View menu */
5694         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, NULL, "<control>plus",
5695           N_("Enlarge the document"),
5696           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5697         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, NULL, "<control>minus",
5698           N_("Shrink the document"),
5699           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5700         { "ViewReload", GTK_STOCK_REFRESH, N_("_Reload"), "<control>R",
5701           N_("Reload the document"),
5702           G_CALLBACK (ev_window_cmd_view_reload) },
5703
5704         { "ViewAutoscroll", GTK_STOCK_MEDIA_PLAY, N_("Auto_scroll"), NULL, NULL,
5705           G_CALLBACK (ev_window_cmd_view_autoscroll) },
5706
5707         /* Go menu */
5708         { "GoPreviousPage", GTK_STOCK_GO_UP, N_("_Previous Page"), "<control>Page_Up",
5709           N_("Go to the previous page"),
5710           G_CALLBACK (ev_window_cmd_go_previous_page) },
5711         { "GoNextPage", GTK_STOCK_GO_DOWN, N_("_Next Page"), "<control>Page_Down",
5712           N_("Go to the next page"),
5713           G_CALLBACK (ev_window_cmd_go_next_page) },
5714         { "GoFirstPage", GTK_STOCK_GOTO_TOP, N_("_First Page"), "<control>Home",
5715           N_("Go to the first page"),
5716           G_CALLBACK (ev_window_cmd_go_first_page) },
5717         { "GoLastPage", GTK_STOCK_GOTO_BOTTOM, N_("_Last Page"), "<control>End",
5718           N_("Go to the last page"),
5719           G_CALLBACK (ev_window_cmd_go_last_page) },
5720
5721         /* Bookmarks menu */
5722         { "BookmarksAdd", GTK_STOCK_ADD, N_("_Add Bookmark"), "<control>D",
5723           N_("Add a bookmark for the current page"),
5724           G_CALLBACK (ev_window_cmd_bookmarks_add) },
5725
5726         /* Help menu */
5727         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), "F1", NULL,
5728           G_CALLBACK (ev_window_cmd_help_contents) },
5729
5730         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL,
5731           G_CALLBACK (ev_window_cmd_help_about) },
5732
5733         /* Toolbar-only */
5734         { "LeaveFullscreen", GTK_STOCK_LEAVE_FULLSCREEN, N_("Leave Fullscreen"), NULL,
5735           N_("Leave fullscreen mode"),
5736           G_CALLBACK (ev_window_cmd_leave_fullscreen) },
5737         { "StartPresentation", EV_STOCK_RUN_PRESENTATION, N_("Start Presentation"), NULL,
5738           N_("Start a presentation"),
5739           G_CALLBACK (ev_window_cmd_start_presentation) },
5740
5741         /* Accellerators */
5742         { "Escape", NULL, "", "Escape", "",
5743           G_CALLBACK (ev_window_cmd_escape) },
5744         { "Slash", GTK_STOCK_FIND, NULL, "slash", NULL,
5745           G_CALLBACK (ev_window_cmd_edit_find) },
5746         { "F3", NULL, "", "F3", NULL,
5747           G_CALLBACK (ev_window_cmd_edit_find_next) },
5748         { "PageDown", NULL, "", "Page_Down", NULL,
5749           G_CALLBACK (ev_window_cmd_scroll_forward) },
5750         { "PageUp", NULL, "", "Page_Up", NULL,
5751           G_CALLBACK (ev_window_cmd_scroll_backward) },
5752         { "Space", NULL, "", "space", NULL,
5753           G_CALLBACK (ev_window_cmd_scroll_forward) },
5754         { "ShiftSpace", NULL, "", "<shift>space", NULL,
5755           G_CALLBACK (ev_window_cmd_scroll_backward) },
5756         { "BackSpace", NULL, "", "BackSpace", NULL,
5757           G_CALLBACK (ev_window_cmd_scroll_backward) },
5758         { "ShiftBackSpace", NULL, "", "<shift>BackSpace", NULL,
5759           G_CALLBACK (ev_window_cmd_scroll_forward) },
5760         { "Return", NULL, "", "Return", NULL,
5761           G_CALLBACK (ev_window_cmd_scroll_forward) },
5762         { "ShiftReturn", NULL, "", "<shift>Return", NULL,
5763           G_CALLBACK (ev_window_cmd_scroll_backward) },
5764         { "p", GTK_STOCK_GO_UP, "", "p", NULL,
5765           G_CALLBACK (ev_window_cmd_go_previous_page) },
5766         { "n", GTK_STOCK_GO_DOWN, "", "n", NULL,
5767           G_CALLBACK (ev_window_cmd_go_next_page) },
5768         { "Plus", GTK_STOCK_ZOOM_IN, NULL, "plus", NULL,
5769           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5770         { "CtrlEqual", GTK_STOCK_ZOOM_IN, NULL, "<control>equal", NULL,
5771           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5772         { "Equal", GTK_STOCK_ZOOM_IN, NULL, "equal", NULL,
5773           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5774         { "Minus", GTK_STOCK_ZOOM_OUT, NULL, "minus", NULL,
5775           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5776         { "FocusPageSelector", NULL, "", "<control>l", NULL,
5777           G_CALLBACK (ev_window_cmd_focus_page_selector) },
5778         { "GoBackwardFast", NULL, "", "<shift>Page_Up", NULL,
5779           G_CALLBACK (ev_window_cmd_go_backward) },
5780         { "GoForwardFast", NULL, "", "<shift>Page_Down", NULL,
5781           G_CALLBACK (ev_window_cmd_go_forward) },
5782         { "KpPlus", GTK_STOCK_ZOOM_IN, NULL, "KP_Add", NULL,
5783           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5784         { "KpMinus", GTK_STOCK_ZOOM_OUT, NULL, "KP_Subtract", NULL,
5785           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5786         { "CtrlKpPlus", GTK_STOCK_ZOOM_IN, NULL, "<control>KP_Add", NULL,
5787           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5788         { "CtrlKpMinus", GTK_STOCK_ZOOM_OUT, NULL, "<control>KP_Subtract", NULL,
5789           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5790         { "CtrlInsert", GTK_STOCK_COPY, NULL, "<control>Insert", NULL,
5791           G_CALLBACK (ev_window_cmd_edit_copy) },
5792 };
5793
5794 /* Toggle items */
5795 static const GtkToggleActionEntry toggle_entries[] = {
5796         /* View Menu */
5797         { "ViewToolbar", NULL, N_("_Toolbar"), NULL,
5798           N_("Show or hide the toolbar"),
5799           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
5800         { "ViewSidebar", GTK_STOCK_INDEX, N_("Side _Pane"), "F9",
5801           N_("Show or hide the side pane"),
5802           G_CALLBACK (ev_window_view_sidebar_cb), TRUE },
5803         { "ViewContinuous", EV_STOCK_VIEW_CONTINUOUS, N_("_Continuous"), NULL,
5804           N_("Show the entire document"),
5805           G_CALLBACK (ev_window_cmd_continuous), TRUE },
5806         { "ViewDual", EV_STOCK_VIEW_DUAL, N_("_Dual"), NULL,
5807           N_("Show two pages at once"),
5808           G_CALLBACK (ev_window_cmd_dual), FALSE },
5809         { "ViewFullscreen", GTK_STOCK_FULLSCREEN, N_("_Fullscreen"), "F11",
5810           N_("Expand the window to fill the screen"),
5811           G_CALLBACK (ev_window_cmd_view_fullscreen) },
5812         { "ViewPresentation", EV_STOCK_RUN_PRESENTATION, N_("Pre_sentation"), "F5",
5813           N_("Run document as a presentation"),
5814           G_CALLBACK (ev_window_cmd_view_presentation) },
5815         { "ViewBestFit", EV_STOCK_ZOOM_PAGE, N_("_Best Fit"), NULL,
5816           N_("Make the current document fill the window"),
5817           G_CALLBACK (ev_window_cmd_view_best_fit) },
5818         { "ViewPageWidth", EV_STOCK_ZOOM_WIDTH, N_("Fit Page _Width"), NULL,
5819           N_("Make the current document fill the window width"),
5820           G_CALLBACK (ev_window_cmd_view_page_width) },
5821         { "ViewInvertedColors", EV_STOCK_INVERTED_COLORS, N_("_Inverted Colors"), "<control>I",
5822           N_("Show page contents with the colors inverted"),
5823           G_CALLBACK (ev_window_cmd_view_inverted_colors) },
5824
5825 };
5826
5827 /* Popups specific items */
5828 static const GtkActionEntry view_popup_entries [] = {
5829         /* Links */
5830         { "OpenLink", GTK_STOCK_OPEN, N_("_Open Link"), NULL,
5831           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
5832         { "GoLink", GTK_STOCK_GO_FORWARD, N_("_Go To"), NULL,
5833           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
5834         { "OpenLinkNewWindow", NULL, N_("Open in New _Window"), NULL,
5835           NULL, G_CALLBACK (ev_view_popup_cmd_open_link_new_window) },
5836         { "CopyLinkAddress", NULL, N_("_Copy Link Address"), NULL,
5837           NULL, G_CALLBACK (ev_view_popup_cmd_copy_link_address) },
5838         { "SaveImageAs", NULL, N_("_Save Image As…"), NULL,
5839           NULL, G_CALLBACK (ev_view_popup_cmd_save_image_as) },
5840         { "CopyImage", NULL, N_("Copy _Image"), NULL,
5841           NULL, G_CALLBACK (ev_view_popup_cmd_copy_image) },
5842         { "AnnotProperties", NULL, N_("Annotation Properties…"), NULL,
5843           NULL, G_CALLBACK (ev_view_popup_cmd_annot_properties) }
5844 };
5845
5846 static const GtkActionEntry attachment_popup_entries [] = {
5847         { "OpenAttachment", GTK_STOCK_OPEN, N_("_Open Attachment"), NULL,
5848           NULL, G_CALLBACK (ev_attachment_popup_cmd_open_attachment) },
5849         { "SaveAttachmentAs", GTK_STOCK_SAVE_AS, N_("_Save Attachment As…"), NULL,
5850           NULL, G_CALLBACK (ev_attachment_popup_cmd_save_attachment_as) },
5851 };
5852
5853 static void
5854 sidebar_links_link_activated_cb (EvSidebarLinks *sidebar_links, EvLink *link, EvWindow *window)
5855 {
5856         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5857 }
5858
5859 static void
5860 activate_link_cb (EvPageAction *page_action, EvLink *link, EvWindow *window)
5861 {
5862         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5863         gtk_widget_grab_focus (window->priv->view);
5864 }
5865
5866 static void
5867 navigation_action_activate_link_cb (EvNavigationAction *action, EvLink *link, EvWindow *window)
5868 {
5869         
5870         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5871         gtk_widget_grab_focus (window->priv->view);
5872 }
5873
5874 static void
5875 sidebar_layers_visibility_changed (EvSidebarLayers *layers,
5876                                    EvWindow        *window)
5877 {
5878         ev_view_reload (EV_VIEW (window->priv->view));
5879 }
5880
5881 static void
5882 sidebar_annots_annot_activated_cb (EvSidebarAnnotations *sidebar_annots,
5883                                    EvMapping            *annot_mapping,
5884                                    EvWindow             *window)
5885 {
5886         ev_view_focus_annotation (EV_VIEW (window->priv->view), annot_mapping);
5887 }
5888
5889 static void
5890 sidebar_annots_begin_annot_add (EvSidebarAnnotations *sidebar_annots,
5891                                 EvAnnotationType      annot_type,
5892                                 EvWindow             *window)
5893 {
5894         ev_view_begin_add_annotation (EV_VIEW (window->priv->view), annot_type);
5895 }
5896
5897 static void
5898 view_annot_added (EvView       *view,
5899                   EvAnnotation *annot,
5900                   EvWindow     *window)
5901 {
5902         ev_sidebar_annotations_annot_added (EV_SIDEBAR_ANNOTATIONS (window->priv->sidebar_annots),
5903                                             annot);
5904 }
5905
5906 static void
5907 sidebar_annots_annot_add_cancelled (EvSidebarAnnotations *sidebar_annots,
5908                                     EvWindow             *window)
5909 {
5910         ev_view_cancel_add_annotation (EV_VIEW (window->priv->view));
5911 }
5912
5913 static void
5914 sidebar_bookmarks_add_bookmark (EvSidebarBookmarks *sidebar_bookmarks,
5915                                 EvWindow           *window)
5916 {
5917         ev_window_cmd_bookmarks_add (NULL, window);
5918 }
5919
5920 static void
5921 register_custom_actions (EvWindow *window, GtkActionGroup *group)
5922 {
5923         GtkAction *action;
5924
5925         action = g_object_new (EV_TYPE_PAGE_ACTION,
5926                                "name", PAGE_SELECTOR_ACTION,
5927                                "label", _("Page"),
5928                                "tooltip", _("Select Page"),
5929                                "icon_name", "text-x-generic",
5930                                "visible_overflown", FALSE,
5931                                NULL);
5932         ev_page_action_set_model (EV_PAGE_ACTION (action),
5933                                   window->priv->model);
5934         g_signal_connect (action, "activate_link",
5935                           G_CALLBACK (activate_link_cb), window);
5936         gtk_action_group_add_action (group, action);
5937         g_object_unref (action);
5938
5939         action = g_object_new (EPHY_TYPE_ZOOM_ACTION,
5940                                "name", ZOOM_CONTROL_ACTION,
5941                                "label", _("Zoom"),
5942                                "stock_id", EV_STOCK_ZOOM,
5943                                "tooltip", _("Adjust the zoom level"),
5944                                "zoom", 1.0,
5945                                NULL);
5946         g_signal_connect (action, "zoom_to_level",
5947                           G_CALLBACK (zoom_control_changed_cb), window);
5948         gtk_action_group_add_action (group, action);
5949         g_object_unref (action);
5950
5951         action = g_object_new (EV_TYPE_NAVIGATION_ACTION,
5952                                "name", NAVIGATION_ACTION,
5953                                "label", _("Navigation"),
5954                                "is_important", TRUE,
5955                                "short_label", _("Back"),
5956                                "stock_id", GTK_STOCK_GO_DOWN,
5957                                /*translators: this is the history action*/
5958                                "tooltip", _("Move across visited pages"),
5959                                NULL);
5960         g_signal_connect (action, "activate_link",
5961                           G_CALLBACK (navigation_action_activate_link_cb), window);
5962         gtk_action_group_add_action (group, action);
5963         g_object_unref (action);
5964
5965         action = g_object_new (EV_TYPE_OPEN_RECENT_ACTION,
5966                                "name", "FileOpenRecent",
5967                                "label", _("_Open…"),
5968                                "tooltip", _("Open an existing document"),
5969                                "stock_id", GTK_STOCK_OPEN,
5970                                NULL);
5971         g_signal_connect (action, "activate",
5972                           G_CALLBACK (ev_window_cmd_file_open), window);
5973         g_signal_connect (action, "item_activated",
5974                           G_CALLBACK (ev_window_open_recent_action_item_activated),
5975                           window);
5976         gtk_action_group_add_action (group, action);
5977         g_object_unref (action);
5978 }
5979
5980 static void
5981 set_action_properties (GtkActionGroup *action_group)
5982 {
5983         GtkAction *action;
5984
5985         action = gtk_action_group_get_action (action_group, "FileOpenContainingFolder");
5986         /*translators: this is the label for toolbar button*/
5987         g_object_set (action, "short_label", _("Open Folder"), NULL);
5988
5989         action = gtk_action_group_get_action (action_group, "GoPreviousPage");
5990         g_object_set (action, "is-important", TRUE, NULL);
5991         /*translators: this is the label for toolbar button*/
5992         g_object_set (action, "short_label", _("Previous"), NULL);
5993
5994         action = gtk_action_group_get_action (action_group, "GoNextPage");
5995         g_object_set (action, "is-important", TRUE, NULL);
5996         /*translators: this is the label for toolbar button*/
5997         g_object_set (action, "short_label", _("Next"), NULL);
5998
5999         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
6000         /*translators: this is the label for toolbar button*/
6001         g_object_set (action, "short_label", _("Zoom In"), NULL);
6002
6003         action = gtk_action_group_get_action (action_group, "ViewZoomOut");
6004         /*translators: this is the label for toolbar button*/
6005         g_object_set (action, "short_label", _("Zoom Out"), NULL);
6006
6007         action = gtk_action_group_get_action (action_group, "ViewBestFit");
6008         /*translators: this is the label for toolbar button*/
6009         g_object_set (action, "short_label", _("Best Fit"), NULL);
6010
6011         action = gtk_action_group_get_action (action_group, "ViewPageWidth");
6012         /*translators: this is the label for toolbar button*/
6013         g_object_set (action, "short_label", _("Fit Width"), NULL);
6014
6015         action = gtk_action_group_get_action (action_group, "LeaveFullscreen");
6016         g_object_set (action, "is-important", TRUE, NULL);
6017 }
6018
6019 static void
6020 sidebar_widget_model_set (EvSidebarLinks *ev_sidebar_links,
6021                           GParamSpec     *pspec,
6022                           EvWindow       *ev_window)
6023 {
6024         GtkTreeModel *model;
6025         GtkAction *action;
6026
6027         g_object_get (G_OBJECT (ev_sidebar_links),
6028                       "model", &model,
6029                       NULL);
6030
6031         action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
6032         ev_page_action_set_links_model (EV_PAGE_ACTION (action), model);
6033         g_object_unref (model);
6034 }
6035
6036 static gboolean
6037 view_actions_focus_in_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
6038 {
6039 #ifdef ENABLE_DBUS
6040         GObject *keys;
6041
6042         keys = ev_application_get_media_keys (EV_APP);
6043         ev_media_player_keys_focused (EV_MEDIA_PLAYER_KEYS (keys));
6044 #endif /* ENABLE_DBUS */
6045
6046         update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, FALSE);
6047         ev_window_set_action_sensitive (window, "ViewToolbar", TRUE);
6048
6049         ev_window_set_view_accels_sensitivity (window, TRUE);
6050
6051         update_chrome_visibility (window);
6052
6053         return FALSE;
6054 }
6055
6056 static gboolean
6057 view_actions_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
6058 {
6059         ev_window_set_view_accels_sensitivity (window, FALSE);
6060
6061         return FALSE;
6062 }
6063
6064 static void
6065 sidebar_page_main_widget_update_cb (GObject *ev_sidebar_page,
6066                                     GParamSpec         *pspec,
6067                                     EvWindow           *ev_window)
6068 {
6069         GtkWidget *widget;
6070         
6071         g_object_get (ev_sidebar_page, "main_widget", &widget, NULL);
6072
6073         if (widget != NULL) {           
6074                 g_signal_connect_object (widget, "focus_in_event",
6075                                          G_CALLBACK (view_actions_focus_in_cb),
6076                                          ev_window, 0);
6077                 g_signal_connect_object (widget, "focus_out_event",
6078                                          G_CALLBACK (view_actions_focus_out_cb),
6079                                          ev_window, 0);
6080                 g_object_unref (widget);
6081         }
6082 }
6083
6084 static gboolean
6085 window_state_event_cb (EvWindow *window, GdkEventWindowState *event, gpointer dummy)
6086 {
6087         if (!(event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)) {
6088                 gboolean maximized;
6089
6090                 maximized = event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED;
6091                 if (window->priv->metadata && !ev_window_is_empty (window))
6092                         ev_metadata_set_boolean (window->priv->metadata, "window_maximized", maximized);
6093         }
6094
6095         return FALSE;
6096 }
6097
6098 static gboolean
6099 window_configure_event_cb (EvWindow *window, GdkEventConfigure *event, gpointer dummy)
6100 {
6101         GdkWindowState state;
6102         gdouble document_width, document_height;
6103
6104         if (!window->priv->metadata)
6105                 return FALSE;
6106
6107         state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (window)));
6108
6109         if (!(state & GDK_WINDOW_STATE_FULLSCREEN)) {
6110                 if (window->priv->document) {
6111                         ev_document_get_max_page_size (window->priv->document,
6112                                                        &document_width, &document_height);
6113                         g_settings_set (window->priv->default_settings, "window-ratio", "(dd)",
6114                                         (double)event->width / document_width,
6115                                         (double)event->height / document_height);
6116
6117                         ev_metadata_set_int (window->priv->metadata, "window_x", event->x);
6118                         ev_metadata_set_int (window->priv->metadata, "window_y", event->y);
6119                         ev_metadata_set_int (window->priv->metadata, "window_width", event->width);
6120                         ev_metadata_set_int (window->priv->metadata, "window_height", event->height);
6121                 }
6122         }
6123
6124         return FALSE;
6125 }
6126
6127 static void
6128 launch_action (EvWindow *window, EvLinkAction *action)
6129 {
6130         const char *filename = ev_link_action_get_filename (action);
6131         GAppInfo *app_info;
6132         GFile *file;
6133         GList file_list = {NULL};
6134         GAppLaunchContext *context;
6135         GError *error = NULL;
6136
6137         if (filename == NULL)
6138                 return;
6139
6140         if (g_path_is_absolute (filename)) {
6141                 file = g_file_new_for_path (filename);
6142         } else {
6143                 GFile *base_file;
6144                 gchar *dir;
6145
6146                 dir = g_path_get_dirname (window->priv->uri);
6147                 base_file = g_file_new_for_uri (dir);
6148                 g_free (dir);
6149                 
6150                 file = g_file_resolve_relative_path (base_file, filename);
6151                 g_object_unref (base_file);
6152         }
6153
6154         app_info = g_file_query_default_handler (file, NULL, &error);
6155         if (!app_info) {
6156                 ev_window_error_message (window, error,
6157                                          "%s",
6158                                          _("Unable to launch external application."));
6159                 g_object_unref (file);
6160                 g_error_free (error);
6161
6162                 return;
6163         }
6164
6165         context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
6166         gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context),
6167                                            gtk_window_get_screen (GTK_WINDOW (window)));
6168         gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context),
6169                                               gtk_get_current_event_time ());
6170         
6171         file_list.data = file;
6172         if (!g_app_info_launch (app_info, &file_list, context, &error)) {
6173                 ev_window_error_message (window, error,
6174                                          "%s",
6175                                          _("Unable to launch external application."));
6176                 g_error_free (error);
6177         }
6178         
6179         g_object_unref (app_info);
6180         g_object_unref (file);
6181         /* FIXMEchpe: unref launch context? */
6182
6183         /* According to the PDF spec filename can be an executable. I'm not sure
6184            allowing to launch executables is a good idea though. -- marco */
6185 }
6186
6187 static void
6188 launch_external_uri (EvWindow *window, EvLinkAction *action)
6189 {
6190         const gchar *uri = ev_link_action_get_uri (action);
6191         GError *error = NULL;
6192         gboolean ret;
6193         GAppLaunchContext *context;
6194
6195         context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
6196         gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context),
6197                                            gtk_window_get_screen (GTK_WINDOW (window)));
6198         gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context),
6199                                               gtk_get_current_event_time ());
6200
6201         if (!g_strstr_len (uri, strlen (uri), "://") &&
6202             !g_str_has_prefix (uri, "mailto:")) {
6203                 gchar *new_uri;
6204
6205                 /* Not a valid uri, assume http if it starts with www */
6206                 if (g_str_has_prefix (uri, "www.")) {
6207                         new_uri = g_strdup_printf ("http://%s", uri);
6208                 } else {
6209                         GFile *file, *parent;
6210
6211                         file = g_file_new_for_uri (window->priv->uri);
6212                         parent = g_file_get_parent (file);
6213                         g_object_unref (file);
6214                         if (parent) {
6215                                 gchar *parent_uri = g_file_get_uri (parent);
6216
6217                                 new_uri = g_build_filename (parent_uri, uri, NULL);
6218                                 g_free (parent_uri);
6219                                 g_object_unref (parent);
6220                         } else {
6221                                 new_uri = g_strdup_printf ("file:///%s", uri);
6222                         }
6223                 }
6224                 ret = g_app_info_launch_default_for_uri (new_uri, context, &error);
6225                 g_free (new_uri);
6226         } else {
6227                 ret = g_app_info_launch_default_for_uri (uri, context, &error);
6228         }
6229
6230         if (ret == FALSE) {
6231                 ev_window_error_message (window, error,
6232                                          "%s", _("Unable to open external link"));
6233                 g_error_free (error);
6234         }
6235
6236         /* FIXMEchpe: unref launch context? */
6237 }
6238
6239 static void
6240 open_remote_link (EvWindow *window, EvLinkAction *action)
6241 {
6242         gchar *uri;
6243         gchar *dir;
6244
6245         dir = g_path_get_dirname (window->priv->uri);
6246         
6247         uri = g_build_filename (dir, ev_link_action_get_filename (action),
6248                                 NULL);
6249         g_free (dir);
6250
6251         ev_application_open_uri_at_dest (EV_APP, uri,
6252                                          gtk_window_get_screen (GTK_WINDOW (window)),
6253                                          ev_link_action_get_dest (action),
6254                                          0,
6255                                          NULL, 
6256                                          gtk_get_current_event_time ());
6257
6258         g_free (uri);
6259 }
6260
6261 static void
6262 do_action_named (EvWindow *window, EvLinkAction *action)
6263 {
6264         const gchar *name = ev_link_action_get_name (action);
6265
6266         if (g_ascii_strcasecmp (name, "FirstPage") == 0) {
6267                 ev_window_cmd_go_first_page (NULL, window);
6268         } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) {
6269                 ev_window_cmd_go_previous_page (NULL, window);
6270         } else if (g_ascii_strcasecmp (name, "NextPage") == 0) {
6271                 ev_window_cmd_go_next_page (NULL, window);
6272         } else if (g_ascii_strcasecmp (name, "LastPage") == 0) {
6273                 ev_window_cmd_go_last_page (NULL, window);
6274         } else if (g_ascii_strcasecmp (name, "GoToPage") == 0) {
6275                 ev_window_cmd_focus_page_selector (NULL, window);
6276         } else if (g_ascii_strcasecmp (name, "Find") == 0) {
6277                 ev_window_cmd_edit_find (NULL, window);
6278         } else if (g_ascii_strcasecmp (name, "Close") == 0) {
6279                 ev_window_cmd_file_close_window (NULL, window);
6280         } else if (g_ascii_strcasecmp (name, "Print") == 0) {
6281                 ev_window_cmd_file_print (NULL, window);
6282         } else {
6283                 g_warning ("Unimplemented named action: %s, please post a "
6284                            "bug report in Evince bugzilla "
6285                            "(http://bugzilla.gnome.org) with a testcase.",
6286                            name);
6287         }
6288 }
6289
6290 static void
6291 view_external_link_cb (EvWindow *window, EvLinkAction *action)
6292 {
6293         switch (ev_link_action_get_action_type (action)) {
6294                 case EV_LINK_ACTION_TYPE_GOTO_DEST: {
6295                         EvLinkDest *dest;
6296                         
6297                         dest = ev_link_action_get_dest (action);
6298                         if (!dest)
6299                                 return;
6300
6301                         ev_window_open_copy_at_dest (window, dest);
6302                 }
6303                         break;
6304                 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
6305                         launch_external_uri (window, action);
6306                         break;
6307                 case EV_LINK_ACTION_TYPE_LAUNCH:
6308                         launch_action (window, action);
6309                         break;
6310                 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
6311                         open_remote_link (window, action);
6312                         break;
6313                 case EV_LINK_ACTION_TYPE_NAMED:
6314                         do_action_named (window, action);
6315                         break;
6316                 default:
6317                         g_assert_not_reached ();
6318         }
6319 }
6320
6321 static void
6322 ev_view_popup_cmd_open_link (GtkAction *action, EvWindow *window)
6323 {
6324         ev_view_handle_link (EV_VIEW (window->priv->view), window->priv->link);
6325 }
6326
6327 static void
6328 ev_view_popup_cmd_open_link_new_window (GtkAction *action, EvWindow *window)
6329 {
6330         EvLinkAction *ev_action = NULL;
6331         EvLinkDest   *dest;
6332
6333         ev_action = ev_link_get_action (window->priv->link);
6334         if (!ev_action)
6335                 return;
6336
6337         dest = ev_link_action_get_dest (ev_action);
6338         if (!dest)
6339                 return;
6340
6341         ev_window_open_copy_at_dest (window, dest);
6342 }
6343
6344 static void
6345 ev_view_popup_cmd_copy_link_address (GtkAction *action, EvWindow *window)
6346 {
6347         EvLinkAction *ev_action;
6348
6349         ev_action = ev_link_get_action (window->priv->link);
6350         if (!ev_action)
6351                 return;
6352
6353         ev_view_copy_link_address (EV_VIEW (window->priv->view),
6354                                    ev_action);
6355 }
6356
6357 static GFile *
6358 create_file_from_uri_for_format (const gchar     *uri,
6359                                  GdkPixbufFormat *format)
6360 {
6361         GFile  *target_file;
6362         gchar **extensions;
6363         gchar  *uri_extension;
6364         gint    i;
6365
6366         extensions = gdk_pixbuf_format_get_extensions (format);
6367         for (i = 0; extensions[i]; i++) {
6368                 if (g_str_has_suffix (uri, extensions[i])) {
6369                         g_strfreev (extensions);
6370                         return g_file_new_for_uri (uri);
6371                 }
6372         }
6373
6374         uri_extension = g_strconcat (uri, ".", extensions[0], NULL);
6375         target_file = g_file_new_for_uri (uri_extension);
6376         g_free (uri_extension);
6377         g_strfreev (extensions);
6378
6379         return target_file;
6380 }
6381
6382 static void
6383 image_save_dialog_response_cb (GtkWidget *fc,
6384                                gint       response_id,
6385                                EvWindow  *ev_window)
6386 {
6387         GFile           *target_file;
6388         gboolean         is_native;
6389         GError          *error = NULL;
6390         GdkPixbuf       *pixbuf;
6391         gchar           *uri;
6392         gchar           *filename;
6393         gchar           *file_format;
6394         GdkPixbufFormat *format;
6395         GtkFileFilter   *filter;
6396         
6397         if (response_id != GTK_RESPONSE_OK) {
6398                 gtk_widget_destroy (fc);
6399                 return;
6400         }
6401
6402         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
6403         filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (fc));
6404         format = g_object_get_data (G_OBJECT (filter), "pixbuf-format");
6405         
6406         if (format == NULL) {
6407                 format = get_gdk_pixbuf_format_by_extension (uri);
6408         }
6409
6410         if (format == NULL && g_strrstr (uri, ".") == NULL) {
6411                 /* no extension found and no extension provided within uri */
6412                 format = get_gdk_pixbuf_format_by_extension (".png");
6413                 if (format == NULL) {
6414                         /* no .png support, try .jpeg */
6415                         format = get_gdk_pixbuf_format_by_extension (".jpeg");
6416                 }
6417         }
6418
6419         if (format == NULL) {
6420                 ev_window_error_message (ev_window, NULL, 
6421                                          "%s",
6422                                          _("Couldn't find appropriate format to save image"));
6423                 g_free (uri);
6424                 gtk_widget_destroy (fc);
6425
6426                 return;
6427         }
6428
6429         target_file = create_file_from_uri_for_format (uri, format);
6430         g_free (uri);
6431
6432         is_native = g_file_is_native (target_file);
6433         if (is_native) {
6434                 filename = g_file_get_path (target_file);
6435         } else {
6436                 /* Create a temporary local file to save to */
6437                 if (ev_mkstemp ("saveimage.XXXXXX", &filename, &error) == -1)
6438                         goto has_error;
6439         }
6440
6441         ev_document_doc_mutex_lock ();
6442         pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (ev_window->priv->document),
6443                                                ev_window->priv->image);
6444         ev_document_doc_mutex_unlock ();
6445
6446         file_format = gdk_pixbuf_format_get_name (format);
6447         gdk_pixbuf_save (pixbuf, filename, file_format, &error, NULL);
6448         g_free (file_format);
6449         g_object_unref (pixbuf);
6450         
6451     has_error:
6452         if (error) {
6453                 ev_window_error_message (ev_window, error, 
6454                                          "%s", _("The image could not be saved."));
6455                 g_error_free (error);
6456                 g_free (filename);
6457                 g_object_unref (target_file);
6458                 gtk_widget_destroy (fc);
6459
6460                 return;
6461         }
6462
6463         if (!is_native) {
6464                 GFile *source_file;
6465                 
6466                 source_file = g_file_new_for_path (filename);
6467                 
6468                 ev_window_save_remote (ev_window, EV_SAVE_IMAGE,
6469                                        source_file, target_file);
6470                 g_object_unref (source_file);
6471         }
6472         
6473         g_free (filename);
6474         g_object_unref (target_file);
6475         gtk_widget_destroy (fc);
6476 }
6477
6478 static void
6479 ev_view_popup_cmd_save_image_as (GtkAction *action, EvWindow *window)
6480 {
6481         GtkWidget *fc;
6482
6483         if (!window->priv->image)
6484                 return;
6485
6486         fc = gtk_file_chooser_dialog_new (_("Save Image"),
6487                                           GTK_WINDOW (window),
6488                                           GTK_FILE_CHOOSER_ACTION_SAVE,
6489                                           GTK_STOCK_CANCEL,
6490                                           GTK_RESPONSE_CANCEL,
6491                                           GTK_STOCK_SAVE, GTK_RESPONSE_OK,
6492                                           NULL);
6493
6494         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
6495         gtk_dialog_set_alternative_button_order (GTK_DIALOG (fc),
6496                                                  GTK_RESPONSE_OK,
6497                                                  GTK_RESPONSE_CANCEL,
6498                                                  -1);
6499
6500         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
6501         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
6502         
6503         file_chooser_dialog_add_writable_pixbuf_formats (GTK_FILE_CHOOSER (fc));
6504         
6505         g_signal_connect (fc, "response",
6506                           G_CALLBACK (image_save_dialog_response_cb),
6507                           window);
6508
6509         gtk_widget_show (fc);
6510 }
6511
6512 static void
6513 ev_view_popup_cmd_copy_image (GtkAction *action, EvWindow *window)
6514 {
6515         GtkClipboard *clipboard;
6516         GdkPixbuf    *pixbuf;
6517
6518         if (!window->priv->image)
6519                 return;
6520         
6521         clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window),
6522                                               GDK_SELECTION_CLIPBOARD);
6523         ev_document_doc_mutex_lock ();
6524         pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (window->priv->document),
6525                                                window->priv->image);
6526         ev_document_doc_mutex_unlock ();
6527         
6528         gtk_clipboard_set_image (clipboard, pixbuf);
6529         g_object_unref (pixbuf);
6530 }
6531
6532 static void
6533 ev_view_popup_cmd_annot_properties (GtkAction *action,
6534                                     EvWindow  *window)
6535 {
6536         const gchar                  *author;
6537         GdkColor                      color;
6538         gdouble                       opacity;
6539         gboolean                      popup_is_open;
6540         EvAnnotationPropertiesDialog *dialog;
6541         EvAnnotation                 *annot = window->priv->annot;
6542         EvAnnotationsSaveMask         mask = EV_ANNOTATIONS_SAVE_NONE;
6543
6544         if (!annot)
6545                 return;
6546
6547         dialog = EV_ANNOTATION_PROPERTIES_DIALOG (ev_annotation_properties_dialog_new_with_annotation (window->priv->annot));
6548         if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_APPLY) {
6549                 gtk_widget_destroy (GTK_WIDGET (dialog));
6550
6551                 return;
6552         }
6553
6554         /* Set annotations changes */
6555         author = ev_annotation_properties_dialog_get_author (dialog);
6556         if (ev_annotation_markup_set_label (EV_ANNOTATION_MARKUP (annot), author))
6557                 mask |= EV_ANNOTATIONS_SAVE_LABEL;
6558
6559         ev_annotation_properties_dialog_get_color (dialog, &color);
6560         if (ev_annotation_set_color (annot, &color))
6561                 mask |= EV_ANNOTATIONS_SAVE_COLOR;
6562
6563         opacity = ev_annotation_properties_dialog_get_opacity (dialog);
6564         if (ev_annotation_markup_set_opacity (EV_ANNOTATION_MARKUP (annot), opacity))
6565                 mask |= EV_ANNOTATIONS_SAVE_OPACITY;
6566
6567         popup_is_open = ev_annotation_properties_dialog_get_popup_is_open (dialog);
6568         if (ev_annotation_markup_set_popup_is_open (EV_ANNOTATION_MARKUP (annot), popup_is_open))
6569                 mask |= EV_ANNOTATIONS_SAVE_POPUP_IS_OPEN;
6570
6571         if (EV_IS_ANNOTATION_TEXT (annot)) {
6572                 EvAnnotationTextIcon icon;
6573
6574                 icon = ev_annotation_properties_dialog_get_text_icon (dialog);
6575                 if (ev_annotation_text_set_icon (EV_ANNOTATION_TEXT (annot), icon))
6576                         mask |= EV_ANNOTATIONS_SAVE_TEXT_ICON;
6577         }
6578
6579         if (mask != EV_ANNOTATIONS_SAVE_NONE) {
6580                 ev_document_doc_mutex_lock ();
6581                 ev_document_annotations_save_annotation (EV_DOCUMENT_ANNOTATIONS (window->priv->document),
6582                                                          window->priv->annot,
6583                                                          mask);
6584                 ev_document_doc_mutex_unlock ();
6585
6586                 /* FIXME: update annot region only */
6587                 ev_view_reload (EV_VIEW (window->priv->view));
6588         }
6589
6590         gtk_widget_destroy (GTK_WIDGET (dialog));
6591 }
6592
6593 static void
6594 ev_attachment_popup_cmd_open_attachment (GtkAction *action, EvWindow *window)
6595 {
6596         GList     *l;
6597         GdkScreen *screen;
6598         
6599         if (!window->priv->attach_list)
6600                 return;
6601
6602         screen = gtk_window_get_screen (GTK_WINDOW (window));
6603
6604         for (l = window->priv->attach_list; l && l->data; l = g_list_next (l)) {
6605                 EvAttachment *attachment;
6606                 GError       *error = NULL;
6607                 
6608                 attachment = (EvAttachment *) l->data;
6609                 
6610                 ev_attachment_open (attachment, screen, gtk_get_current_event_time (), &error);
6611
6612                 if (error) {
6613                         ev_window_error_message (window, error, 
6614                                                  "%s", _("Unable to open attachment"));
6615                         g_error_free (error);
6616                 }
6617         }
6618 }
6619
6620 static void
6621 attachment_save_dialog_response_cb (GtkWidget *fc,
6622                                     gint       response_id,
6623                                     EvWindow  *ev_window)
6624 {
6625         GFile                *target_file;
6626         gchar                *uri;
6627         GList                *l;
6628         GtkFileChooserAction  fc_action;
6629         gboolean              is_dir;
6630         gboolean              is_native;
6631         
6632         if (response_id != GTK_RESPONSE_OK) {
6633                 gtk_widget_destroy (fc);
6634                 return;
6635         }
6636
6637         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
6638         target_file = g_file_new_for_uri (uri);
6639         g_object_get (G_OBJECT (fc), "action", &fc_action, NULL);
6640         is_dir = (fc_action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
6641         is_native = g_file_is_native (target_file);
6642         
6643         for (l = ev_window->priv->attach_list; l && l->data; l = g_list_next (l)) {
6644                 EvAttachment *attachment;
6645                 GFile        *save_to = NULL;
6646                 GError       *error = NULL;
6647                 
6648                 attachment = (EvAttachment *) l->data;
6649
6650                 if (is_native) {
6651                         if (is_dir) {
6652                                 save_to = g_file_get_child (target_file,
6653                                     /* FIXMEchpe: file name encoding! */
6654                                                             ev_attachment_get_name (attachment));
6655                         } else {
6656                                 save_to = g_object_ref (target_file);
6657                         }
6658                 } else {
6659                         save_to = ev_mkstemp_file ("saveattachment.XXXXXX", &error);
6660                 }
6661
6662                 if (save_to)
6663                         ev_attachment_save (attachment, save_to, &error);
6664                 
6665                 if (error) {
6666                         ev_window_error_message (ev_window, error, 
6667                                                  "%s", _("The attachment could not be saved."));
6668                         g_error_free (error);
6669                         g_object_unref (save_to);
6670
6671                         continue;
6672                 }
6673
6674                 if (!is_native) {
6675                         GFile *dest_file;
6676
6677                         if (is_dir) {
6678                                 dest_file = g_file_get_child (target_file,
6679                                                               ev_attachment_get_name (attachment));
6680                         } else {
6681                                 dest_file = g_object_ref (target_file);
6682                         }
6683
6684                         ev_window_save_remote (ev_window, EV_SAVE_ATTACHMENT,
6685                                                save_to, dest_file);
6686
6687                         g_object_unref (dest_file);
6688                 }
6689
6690                 g_object_unref (save_to);
6691         }
6692
6693         g_free (uri);
6694         g_object_unref (target_file);
6695
6696         gtk_widget_destroy (fc);
6697 }
6698
6699 static void
6700 ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
6701 {
6702         GtkWidget    *fc;
6703         EvAttachment *attachment = NULL;
6704
6705         if (!window->priv->attach_list)
6706                 return;
6707
6708         if (g_list_length (window->priv->attach_list) == 1)
6709                 attachment = (EvAttachment *) window->priv->attach_list->data;
6710         
6711         fc = gtk_file_chooser_dialog_new (
6712                 _("Save Attachment"),
6713                 GTK_WINDOW (window),
6714                 attachment ? GTK_FILE_CHOOSER_ACTION_SAVE : GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
6715                 GTK_STOCK_CANCEL,
6716                 GTK_RESPONSE_CANCEL,
6717                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
6718                 NULL);
6719
6720         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
6721         gtk_dialog_set_alternative_button_order (GTK_DIALOG (fc),
6722                                                  GTK_RESPONSE_OK,
6723                                                  GTK_RESPONSE_CANCEL,
6724                                                  -1);
6725
6726         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
6727         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
6728
6729         if (attachment)
6730                 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc),
6731                                                    ev_attachment_get_name (attachment));
6732
6733         g_signal_connect (fc, "response",
6734                           G_CALLBACK (attachment_save_dialog_response_cb),
6735                           window);
6736
6737         gtk_widget_show (fc);
6738 }
6739
6740 static void
6741 ev_window_media_player_key_pressed (EvWindow    *window,
6742                                     const gchar *key,
6743                                     gpointer     user_data)
6744 {
6745         if (!gtk_window_is_active (GTK_WINDOW (window))) 
6746                 return;
6747         
6748         /* Note how Previous/Next only go to the
6749          * next/previous page despite their icon telling you
6750          * they should go to the beginning/end.
6751          *
6752          * There's very few keyboards with FFW/RWD though,
6753          * so we stick the most useful keybinding on the most
6754          * often seen keys
6755          */
6756         if (strcmp (key, "Play") == 0) {
6757                 ev_window_run_presentation (window);
6758         } else if (strcmp (key, "Previous") == 0) {
6759                 if (EV_WINDOW_IS_PRESENTATION (window))
6760                         ev_view_presentation_previous_page (EV_VIEW_PRESENTATION (window->priv->presentation_view));
6761                 else
6762                         ev_window_cmd_go_previous_page (NULL, window);
6763         } else if (strcmp (key, "Next") == 0) {
6764                 if (EV_WINDOW_IS_PRESENTATION (window))
6765                         ev_view_presentation_next_page (EV_VIEW_PRESENTATION (window->priv->presentation_view));
6766                 else
6767                         ev_window_cmd_go_next_page (NULL, window);
6768         } else if (strcmp (key, "FastForward") == 0) {
6769                 ev_window_cmd_go_last_page (NULL, window);
6770         } else if (strcmp (key, "Rewind") == 0) {
6771                 ev_window_cmd_go_first_page (NULL, window);
6772         }
6773 }
6774
6775 static EggToolbarsModel *
6776 get_toolbars_model (void)
6777 {
6778         EggToolbarsModel *toolbars_model;
6779         gchar            *toolbars_file;
6780         gchar            *toolbars_path;
6781         gint              i;
6782
6783         toolbars_model = egg_toolbars_model_new ();
6784
6785         toolbars_file = g_build_filename (ev_application_get_dot_dir (EV_APP, FALSE),
6786                                           "evince_toolbar.xml", NULL);
6787         toolbars_path = g_build_filename (ev_application_get_data_dir (EV_APP),
6788                                          "evince-toolbar.xml", NULL);
6789         egg_toolbars_model_load_names (toolbars_model, toolbars_path);
6790
6791         if (!egg_toolbars_model_load_toolbars (toolbars_model, toolbars_file)) {
6792                 egg_toolbars_model_load_toolbars (toolbars_model, toolbars_path);
6793                 goto skip_conversion;
6794         }
6795
6796         /* Open item doesn't exist anymore,
6797          * convert it to OpenRecent for compatibility
6798          */
6799         for (i = 0; i < egg_toolbars_model_n_items (toolbars_model, 0); i++) {
6800                 const gchar *item;
6801
6802                 item = egg_toolbars_model_item_nth (toolbars_model, 0, i);
6803                 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
6804                         egg_toolbars_model_remove_item (toolbars_model, 0, i);
6805                         egg_toolbars_model_add_item (toolbars_model, 0, i,
6806                                                      "FileOpenRecent");
6807                         egg_toolbars_model_save_toolbars (toolbars_model, toolbars_file, "1.0");
6808                         break;
6809                 }
6810         }
6811
6812     skip_conversion:
6813         g_free (toolbars_file);
6814         g_free (toolbars_path);
6815
6816         egg_toolbars_model_set_flags (toolbars_model, 0, EGG_TB_MODEL_NOT_REMOVABLE);
6817
6818         return toolbars_model;
6819 }
6820
6821 #ifdef ENABLE_DBUS
6822 static void
6823 ev_window_sync_source (EvWindow     *window,
6824                        EvSourceLink *link)
6825 {
6826         GDBusConnection *connection;
6827         GError          *error = NULL;
6828         guint32          timestamp;
6829         gchar           *uri_input;
6830         GFile           *input_gfile;
6831
6832         if (window->priv->dbus_object_id <= 0)
6833                 return;
6834
6835         connection = ev_application_get_dbus_connection (EV_APP);
6836         if (!connection)
6837                 return;
6838
6839         timestamp = gtk_get_current_event_time ();
6840         if (g_path_is_absolute (link->filename)) {
6841                 input_gfile = g_file_new_for_path (link->filename);
6842         } else {
6843                 GFile *gfile, *parent_gfile;
6844
6845                 gfile = g_file_new_for_uri (window->priv->uri);
6846                 parent_gfile = g_file_get_parent (gfile);
6847
6848                 /* parent_gfile should never be NULL */
6849                 if (parent_gfile == NULL) {
6850                         g_printerr ("Document URI is '/'\n");
6851                         return;
6852                 }
6853
6854                 input_gfile = g_file_get_child (parent_gfile, link->filename);
6855                 g_object_unref (parent_gfile);
6856                 g_object_unref (gfile);
6857         }
6858
6859         uri_input = g_file_get_uri (input_gfile);
6860         g_object_unref (input_gfile);
6861
6862         g_dbus_connection_emit_signal (connection,
6863                                        NULL,
6864                                        window->priv->dbus_object_path,
6865                                        EV_WINDOW_DBUS_INTERFACE,
6866                                        "SyncSource",
6867                                        g_variant_new ("(s(ii)u)",
6868                                                       uri_input,
6869                                                       link->line,
6870                                                       link->col,
6871                                                       timestamp),
6872                                        &error);
6873         g_free (uri_input);
6874         if (error) {
6875                 g_printerr ("Failed to emit DBus signal SyncSource: %s\n",
6876                             error->message);
6877                 g_error_free (error);
6878         }
6879 }
6880
6881 static void
6882 ev_window_emit_closed (EvWindow *window)
6883 {
6884         GDBusConnection *connection;
6885         GError          *error = NULL;
6886
6887         if (window->priv->dbus_object_id <= 0)
6888                 return;
6889
6890         connection = ev_application_get_dbus_connection (EV_APP);
6891         if (!connection)
6892                 return;
6893
6894         g_dbus_connection_emit_signal (connection,
6895                                        NULL,
6896                                        window->priv->dbus_object_path,
6897                                        EV_WINDOW_DBUS_INTERFACE,
6898                                        "Closed",
6899                                        NULL,
6900                                        &error);
6901         if (error) {
6902                 g_printerr ("Failed to emit DBus signal Closed: %s\n",
6903                             error->message);
6904                 g_error_free (error);
6905
6906                 return;
6907         }
6908
6909         /* If this is the last window call g_dbus_connection_flush_sync()
6910          * to make sure the signal is emitted.
6911          */
6912         if (ev_application_get_n_windows (EV_APP) == 1)
6913                 g_dbus_connection_flush_sync (connection, NULL, NULL);
6914 }
6915
6916 static void
6917 ev_window_emit_doc_loaded (EvWindow *window)
6918 {
6919         GDBusConnection *connection;
6920         GError          *error = NULL;
6921
6922         if (window->priv->dbus_object_id <= 0)
6923                 return;
6924
6925         connection = ev_application_get_dbus_connection (EV_APP);
6926         if (!connection)
6927                 return;
6928
6929         g_dbus_connection_emit_signal (connection,
6930                                        NULL,
6931                                        window->priv->dbus_object_path,
6932                                        EV_WINDOW_DBUS_INTERFACE,
6933                                        "DocumentLoaded",
6934                                        g_variant_new("(s)", window->priv->uri),
6935                                        &error);
6936         if (error) {
6937                 g_printerr ("Failed to emit DBus signal DocumentLoaded: %s\n",
6938                             error->message);
6939                 g_error_free (error);
6940
6941                 return;
6942         }
6943 }
6944
6945 static void
6946 method_call_cb (GDBusConnection       *connection,
6947                 const gchar           *sender,
6948                 const gchar           *object_path,
6949                 const gchar           *interface_name,
6950                 const gchar           *method_name,
6951                 GVariant              *parameters,
6952                 GDBusMethodInvocation *invocation,
6953                 gpointer               user_data)
6954 {
6955         EvWindow *window = EV_WINDOW (user_data);
6956
6957         if (g_strcmp0 (method_name, "SyncView") != 0)
6958                 return;
6959
6960         if (window->priv->document && ev_document_has_synctex (window->priv->document)) {
6961                 EvSourceLink link;
6962                 guint32      timestamp;
6963
6964                 g_variant_get (parameters, "(&s(ii)u)", &link.filename, &link.line, &link.col, &timestamp);
6965                 ev_view_highlight_forward_search (EV_VIEW (window->priv->view), &link);
6966                 gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
6967         }
6968
6969         g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
6970 }
6971
6972 static const char introspection_xml[] =
6973         "<node>"
6974           "<interface name='org.gnome.evince.Window'>"
6975             "<method name='SyncView'>"
6976               "<arg type='s' name='source_file' direction='in'/>"
6977               "<arg type='(ii)' name='source_point' direction='in'/>"
6978               "<arg type='u' name='timestamp' direction='in'/>"
6979             "</method>"
6980             "<signal name='SyncSource'>"
6981               "<arg type='s' name='source_file' direction='out'/>"
6982               "<arg type='(ii)' name='source_point' direction='out'/>"
6983               "<arg type='u' name='timestamp' direction='out'/>"
6984             "</signal>"
6985             "<signal name='Closed'/>"
6986             "<signal name='DocumentLoaded'>"
6987               "<arg type='s' name='uri' direction='out'/>"
6988             "</signal>"
6989           "</interface>"
6990         "</node>";
6991
6992 static const GDBusInterfaceVTable interface_vtable = {
6993         method_call_cb,
6994         NULL,
6995         NULL
6996 };
6997
6998 static GDBusNodeInfo *introspection_data;
6999 #endif /* ENABLE_DBUS */
7000
7001 static void
7002 ev_window_init (EvWindow *ev_window)
7003 {
7004         GtkActionGroup *action_group;
7005         GtkAccelGroup *accel_group;
7006         GError *error = NULL;
7007         GtkWidget *sidebar_widget;
7008         GtkWidget *menuitem;
7009         EggToolbarsModel *toolbars_model;
7010         GObject *mpkeys;
7011         gchar *ui_path;
7012 #ifdef ENABLE_DBUS
7013         GDBusConnection *connection;
7014         static gint window_id = 0;
7015 #endif
7016
7017         g_signal_connect (ev_window, "configure_event",
7018                           G_CALLBACK (window_configure_event_cb), NULL);
7019         g_signal_connect (ev_window, "window_state_event",
7020                           G_CALLBACK (window_state_event_cb), NULL);
7021
7022         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
7023
7024 #ifdef ENABLE_DBUS
7025         connection = ev_application_get_dbus_connection (EV_APP);
7026         if (connection) {
7027                 if (!introspection_data) {
7028                         introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);
7029                         if (error) g_warning ("%s\n", error->message);
7030                 }
7031                 g_assert (introspection_data != NULL);
7032
7033                 ev_window->priv->dbus_object_path = g_strdup_printf (EV_WINDOW_DBUS_OBJECT_PATH, window_id++);
7034                 ev_window->priv->dbus_object_id =
7035                     g_dbus_connection_register_object (connection,
7036                                                        ev_window->priv->dbus_object_path,
7037                                                        introspection_data->interfaces[0],
7038                                                        &interface_vtable,
7039                                                        ev_window, NULL,
7040                                                        &error);
7041                 if (ev_window->priv->dbus_object_id == 0) {
7042                         g_printerr ("Failed to register bus object %s: %s\n",
7043                                     ev_window->priv->dbus_object_path, error->message);
7044                         g_error_free (error);
7045                         g_free (ev_window->priv->dbus_object_path);
7046                         ev_window->priv->dbus_object_path = NULL;
7047                         error = NULL;
7048                 }
7049         }
7050
7051 #endif /* ENABLE_DBUS */
7052
7053         ev_window->priv->model = ev_document_model_new ();
7054
7055         ev_window->priv->page_mode = PAGE_MODE_DOCUMENT;
7056         ev_window->priv->title = ev_window_title_new (ev_window);
7057
7058         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
7059         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
7060         gtk_widget_show (ev_window->priv->main_box);
7061
7062         action_group = gtk_action_group_new ("MenuActions");
7063         ev_window->priv->action_group = action_group;
7064         gtk_action_group_set_translation_domain (action_group, NULL);
7065         gtk_action_group_add_actions (action_group, entries,
7066                                       G_N_ELEMENTS (entries), ev_window);
7067         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
7068                                              G_N_ELEMENTS (toggle_entries),
7069                                              ev_window);
7070         set_action_properties (action_group);
7071         register_custom_actions (ev_window, action_group);
7072
7073         ev_window->priv->ui_manager = gtk_ui_manager_new ();
7074         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7075                                             action_group, 0);
7076
7077         accel_group =
7078                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
7079         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
7080
7081         ev_window_set_view_accels_sensitivity (ev_window, FALSE);
7082
7083         action_group = gtk_action_group_new ("ViewPopupActions");
7084         ev_window->priv->view_popup_action_group = action_group;
7085         gtk_action_group_set_translation_domain (action_group, NULL);
7086         gtk_action_group_add_actions (action_group, view_popup_entries,
7087                                       G_N_ELEMENTS (view_popup_entries),
7088                                       ev_window);
7089         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7090                                             action_group, 0);
7091
7092         action_group = gtk_action_group_new ("AttachmentPopupActions");
7093         ev_window->priv->attachment_popup_action_group = action_group;
7094         gtk_action_group_set_translation_domain (action_group, NULL);
7095         gtk_action_group_add_actions (action_group, attachment_popup_entries,
7096                                       G_N_ELEMENTS (attachment_popup_entries),
7097                                       ev_window);
7098         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7099                                             action_group, 0);
7100
7101         ui_path = g_build_filename (ev_application_get_data_dir (EV_APP),
7102                                     "evince-ui.xml", NULL);
7103         if (!gtk_ui_manager_add_ui_from_file (
7104                 ev_window->priv->ui_manager, ui_path, &error))
7105         {
7106                 g_warning ("building menus failed: %s", error->message);
7107                 g_error_free (error);
7108         }
7109         g_free (ui_path);
7110
7111         ev_window->priv->recent_manager = gtk_recent_manager_get_default ();
7112         ev_window->priv->recent_action_group = NULL;
7113         ev_window->priv->recent_ui_id = 0;
7114         g_signal_connect_swapped (ev_window->priv->recent_manager,
7115                                   "changed",
7116                                   G_CALLBACK (ev_window_setup_recent),
7117                                   ev_window);
7118
7119         ev_window->priv->menubar =
7120                  gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7121                                             "/MainMenu");
7122         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
7123                             ev_window->priv->menubar,
7124                             FALSE, FALSE, 0);
7125         menuitem = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7126                                               "/MainMenu/EditMenu/EditRotateLeftMenu");
7127         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menuitem), TRUE);
7128         menuitem = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7129                                               "/MainMenu/EditMenu/EditRotateRightMenu");
7130         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menuitem), TRUE);
7131
7132         toolbars_model = get_toolbars_model ();
7133         ev_window->priv->toolbar = GTK_WIDGET
7134                 (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
7135                                "ui-manager", ev_window->priv->ui_manager,
7136                                "popup-path", "/ToolbarPopup",
7137                                "model", toolbars_model,
7138                                NULL));
7139         g_object_unref (toolbars_model);
7140
7141         gtk_style_context_add_class
7142                 (gtk_widget_get_style_context (GTK_WIDGET (ev_window->priv->toolbar)),
7143                  GTK_STYLE_CLASS_PRIMARY_TOOLBAR);
7144
7145         egg_editable_toolbar_show (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar),
7146                                    "DefaultToolBar");
7147         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
7148                             ev_window->priv->toolbar,
7149                             FALSE, FALSE, 0);
7150         gtk_widget_show (ev_window->priv->toolbar);
7151
7152         /* Add the main area */
7153         ev_window->priv->hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
7154         g_signal_connect (ev_window->priv->hpaned,
7155                           "notify::position",
7156                           G_CALLBACK (ev_window_sidebar_position_change_cb),
7157                           ev_window);
7158         
7159         gtk_paned_set_position (GTK_PANED (ev_window->priv->hpaned), SIDEBAR_DEFAULT_SIZE);
7160         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
7161                             TRUE, TRUE, 0);
7162         gtk_widget_show (ev_window->priv->hpaned);
7163         
7164         ev_window->priv->sidebar = ev_sidebar_new ();
7165         ev_sidebar_set_model (EV_SIDEBAR (ev_window->priv->sidebar),
7166                               ev_window->priv->model);
7167         gtk_paned_pack1 (GTK_PANED (ev_window->priv->hpaned),
7168                          ev_window->priv->sidebar, FALSE, FALSE);
7169         gtk_widget_show (ev_window->priv->sidebar);
7170
7171         /* Stub sidebar, for now */
7172
7173         sidebar_widget = ev_sidebar_thumbnails_new ();
7174         ev_window->priv->sidebar_thumbs = sidebar_widget;
7175         g_signal_connect (sidebar_widget,
7176                           "notify::main-widget",
7177                           G_CALLBACK (sidebar_page_main_widget_update_cb),
7178                           ev_window);
7179         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
7180         gtk_widget_show (sidebar_widget);
7181         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7182                              sidebar_widget);
7183
7184         sidebar_widget = ev_sidebar_links_new ();
7185         ev_window->priv->sidebar_links = sidebar_widget;
7186         g_signal_connect (sidebar_widget,
7187                           "notify::model",
7188                           G_CALLBACK (sidebar_widget_model_set),
7189                           ev_window);
7190         g_signal_connect (sidebar_widget,
7191                           "link_activated",
7192                           G_CALLBACK (sidebar_links_link_activated_cb),
7193                           ev_window);
7194         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
7195         gtk_widget_show (sidebar_widget);
7196         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7197                              sidebar_widget);
7198
7199         sidebar_widget = ev_sidebar_attachments_new ();
7200         ev_window->priv->sidebar_attachments = sidebar_widget;
7201         g_signal_connect_object (sidebar_widget,
7202                                  "popup",
7203                                  G_CALLBACK (attachment_bar_menu_popup_cb),
7204                                  ev_window, 0);
7205         gtk_widget_show (sidebar_widget);
7206         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7207                              sidebar_widget);
7208
7209         sidebar_widget = ev_sidebar_layers_new ();
7210         ev_window->priv->sidebar_layers = sidebar_widget;
7211         g_signal_connect (sidebar_widget,
7212                           "layers_visibility_changed",
7213                           G_CALLBACK (sidebar_layers_visibility_changed),
7214                           ev_window);
7215         gtk_widget_show (sidebar_widget);
7216         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7217                              sidebar_widget);
7218
7219         sidebar_widget = ev_sidebar_annotations_new ();
7220         ev_window->priv->sidebar_annots = sidebar_widget;
7221         g_signal_connect (sidebar_widget,
7222                           "annot_activated",
7223                           G_CALLBACK (sidebar_annots_annot_activated_cb),
7224                           ev_window);
7225         g_signal_connect (sidebar_widget,
7226                           "begin_annot_add",
7227                           G_CALLBACK (sidebar_annots_begin_annot_add),
7228                           ev_window);
7229         g_signal_connect (sidebar_widget,
7230                           "annot_add_cancelled",
7231                           G_CALLBACK (sidebar_annots_annot_add_cancelled),
7232                           ev_window);
7233         gtk_widget_show (sidebar_widget);
7234         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7235                              sidebar_widget);
7236
7237         sidebar_widget = ev_sidebar_bookmarks_new ();
7238         ev_window->priv->sidebar_bookmarks = sidebar_widget;
7239         g_signal_connect (sidebar_widget,
7240                           "add-bookmark",
7241                           G_CALLBACK (sidebar_bookmarks_add_bookmark),
7242                           ev_window);
7243         gtk_widget_show (sidebar_widget);
7244         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7245                              sidebar_widget);
7246
7247         ev_window->priv->view_box = gtk_vbox_new (FALSE, 0);
7248         ev_window->priv->scrolled_window =
7249                 GTK_WIDGET (g_object_new (GTK_TYPE_SCROLLED_WINDOW,
7250                                           "shadow-type", GTK_SHADOW_IN,
7251                                           NULL));
7252         gtk_box_pack_start (GTK_BOX (ev_window->priv->view_box),
7253                             ev_window->priv->scrolled_window,
7254                             TRUE, TRUE, 0);
7255         gtk_widget_show (ev_window->priv->scrolled_window);
7256
7257         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
7258                         ev_window->priv->view_box);
7259         gtk_widget_show (ev_window->priv->view_box);
7260
7261         ev_window->priv->view = ev_view_new ();
7262         ev_view_set_page_cache_size (EV_VIEW (ev_window->priv->view), PAGE_CACHE_SIZE);
7263         ev_view_set_model (EV_VIEW (ev_window->priv->view), ev_window->priv->model);
7264
7265         ev_window->priv->password_view = ev_password_view_new (GTK_WINDOW (ev_window));
7266         g_signal_connect_swapped (ev_window->priv->password_view,
7267                                   "unlock",
7268                                   G_CALLBACK (ev_window_password_view_unlock),
7269                                   ev_window);
7270         g_signal_connect_object (ev_window->priv->view, "focus_in_event",
7271                                  G_CALLBACK (view_actions_focus_in_cb),
7272                                  ev_window, 0);
7273         g_signal_connect_object (ev_window->priv->view, "focus_out_event",
7274                                  G_CALLBACK (view_actions_focus_out_cb),
7275                                  ev_window, 0);
7276         g_signal_connect_swapped (ev_window->priv->view, "external-link",
7277                                   G_CALLBACK (view_external_link_cb),
7278                                   ev_window);
7279         g_signal_connect_object (ev_window->priv->view, "handle-link",
7280                                  G_CALLBACK (view_handle_link_cb),
7281                                  ev_window, 0);
7282         g_signal_connect_object (ev_window->priv->view, "popup",
7283                                  G_CALLBACK (view_menu_popup_cb),
7284                                  ev_window, 0);
7285         g_signal_connect_object (ev_window->priv->view, "selection-changed",
7286                                  G_CALLBACK (view_selection_changed_cb),
7287                                  ev_window, 0);
7288         g_signal_connect_object (ev_window->priv->view, "annot-added",
7289                                  G_CALLBACK (view_annot_added),
7290                                  ev_window, 0);
7291         g_signal_connect_object (ev_window->priv->view, "layers-changed",
7292                                  G_CALLBACK (view_layers_changed_cb),
7293                                  ev_window, 0);
7294 #ifdef ENABLE_DBUS
7295         g_signal_connect_swapped (ev_window->priv->view, "sync-source",
7296                                   G_CALLBACK (ev_window_sync_source),
7297                                   ev_window);
7298 #endif
7299         gtk_widget_show (ev_window->priv->view);
7300         gtk_widget_show (ev_window->priv->password_view);
7301
7302         /* Find Bar */
7303         ev_window->priv->find_bar = egg_find_bar_new ();
7304         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
7305                           ev_window->priv->find_bar,
7306                           FALSE, TRUE, 0);
7307
7308         /* We own a ref on these widgets, as we can swap them in and out */
7309         g_object_ref (ev_window->priv->view);
7310         g_object_ref (ev_window->priv->password_view);
7311
7312         gtk_container_add (GTK_CONTAINER (ev_window->priv->scrolled_window),
7313                            ev_window->priv->view);
7314
7315         /* Connect to model signals */
7316         g_signal_connect_swapped (ev_window->priv->model,
7317                                   "page-changed",
7318                                   G_CALLBACK (ev_window_page_changed_cb),
7319                                   ev_window);
7320         g_signal_connect (ev_window->priv->model,
7321                           "notify::document",
7322                           G_CALLBACK (ev_window_document_changed_cb),
7323                           ev_window);
7324         g_signal_connect (ev_window->priv->model,
7325                           "notify::scale",
7326                           G_CALLBACK (ev_window_zoom_changed_cb),
7327                           ev_window);
7328         g_signal_connect (ev_window->priv->model,
7329                           "notify::sizing-mode",
7330                           G_CALLBACK (ev_window_sizing_mode_changed_cb),
7331                           ev_window);
7332         g_signal_connect (ev_window->priv->model,
7333                           "notify::rotation",
7334                           G_CALLBACK (ev_window_rotation_changed_cb),
7335                           ev_window);
7336         g_signal_connect (ev_window->priv->model,
7337                           "notify::continuous",
7338                           G_CALLBACK (ev_window_continuous_changed_cb),
7339                           ev_window);
7340         g_signal_connect (ev_window->priv->model,
7341                           "notify::dual-page",
7342                           G_CALLBACK (ev_window_dual_mode_changed_cb),
7343                           ev_window);
7344         g_signal_connect (ev_window->priv->model,
7345                           "notify::inverted-colors",
7346                           G_CALLBACK (ev_window_inverted_colors_changed_cb),
7347                           ev_window);
7348
7349         /* Connect sidebar signals */
7350         g_signal_connect (ev_window->priv->sidebar,
7351                           "notify::visible",
7352                           G_CALLBACK (ev_window_sidebar_visibility_changed_cb),
7353                           ev_window);
7354         g_signal_connect (ev_window->priv->sidebar,
7355                           "notify::current-page",
7356                           G_CALLBACK (ev_window_sidebar_current_page_changed_cb),
7357                           ev_window);
7358
7359         /* Connect to find bar signals */
7360         g_signal_connect (ev_window->priv->find_bar,
7361                           "previous",
7362                           G_CALLBACK (find_bar_previous_cb),
7363                           ev_window);
7364         g_signal_connect (ev_window->priv->find_bar,
7365                           "next",
7366                           G_CALLBACK (find_bar_next_cb),
7367                           ev_window);
7368         g_signal_connect (ev_window->priv->find_bar,
7369                           "close",
7370                           G_CALLBACK (find_bar_close_cb),
7371                           ev_window);
7372         g_signal_connect (ev_window->priv->find_bar,
7373                           "notify::search-string",
7374                           G_CALLBACK (find_bar_search_changed_cb),
7375                           ev_window);
7376         g_signal_connect (ev_window->priv->find_bar,
7377                           "notify::case-sensitive",
7378                           G_CALLBACK (find_bar_search_changed_cb),
7379                           ev_window);
7380         g_signal_connect (ev_window->priv->find_bar,
7381                           "notify::visible",
7382                           G_CALLBACK (find_bar_visibility_changed_cb),
7383                           ev_window);
7384         g_signal_connect (ev_window->priv->find_bar,
7385                           "scroll",
7386                           G_CALLBACK (find_bar_scroll),
7387                           ev_window);
7388
7389         /* Popups */
7390         ev_window->priv->view_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7391                                                                  "/DocumentPopup");
7392         ev_window->priv->link = NULL;
7393
7394         ev_window->priv->attachment_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7395                                                                        "/AttachmentPopup");
7396         ev_window->priv->attach_list = NULL;
7397
7398         /* Media player keys */
7399         mpkeys = ev_application_get_media_keys (EV_APP);
7400         if (mpkeys) {
7401                 g_signal_connect_swapped (mpkeys, "key_pressed",
7402                                           G_CALLBACK (ev_window_media_player_key_pressed),
7403                                           ev_window);
7404         }
7405
7406         /* Give focus to the document view */
7407         gtk_widget_grab_focus (ev_window->priv->view);
7408
7409         ev_window->priv->default_settings = g_settings_new (GS_SCHEMA_NAME".Default");
7410         g_settings_delay (ev_window->priv->default_settings);
7411
7412         /* Set it user interface params */
7413         ev_window_setup_recent (ev_window);
7414
7415         ev_window_setup_gtk_settings (ev_window);
7416
7417         setup_chrome_from_metadata (ev_window);
7418         update_chrome_actions (ev_window);
7419         update_chrome_visibility (ev_window);
7420
7421         gtk_window_set_default_size (GTK_WINDOW (ev_window), 600, 600);
7422
7423         setup_view_from_metadata (ev_window);
7424         setup_sidebar_from_metadata (ev_window);
7425
7426         ev_window_sizing_mode_changed_cb (ev_window->priv->model, NULL, ev_window);
7427         ev_window_setup_action_sensitivity (ev_window);
7428
7429         /* Drag and Drop */
7430         gtk_drag_dest_set (GTK_WIDGET (ev_window),
7431                            GTK_DEST_DEFAULT_ALL,
7432                            NULL, 0,
7433                            GDK_ACTION_COPY);
7434         gtk_drag_dest_add_uri_targets (GTK_WIDGET (ev_window));
7435 }
7436
7437 /**
7438  * ev_window_new:
7439  *
7440  * Creates a #GtkWidget that represents the window.
7441  *
7442  * Returns: the #GtkWidget that represents the window.
7443  */
7444 GtkWidget *
7445 ev_window_new (void)
7446 {
7447         GtkWidget *ev_window;
7448
7449         ev_window = GTK_WIDGET (g_object_new (EV_TYPE_WINDOW,
7450                                               "type", GTK_WINDOW_TOPLEVEL,
7451                                               NULL));
7452
7453         return ev_window;
7454 }
7455
7456 const gchar *
7457 ev_window_get_dbus_object_path (EvWindow *ev_window)
7458 {
7459 #ifdef ENABLE_DBUS
7460         return ev_window->priv->dbus_object_path;
7461 #else
7462         return NULL;
7463 #endif
7464 }