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