]> www.fi.muni.cz Git - evince.git/blob - shell/ev-window.c
shell: Show "filename (title)" in the window title.
[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         static gboolean done = FALSE;
3848
3849         if (!done) {
3850                 gtk_rc_parse_string (
3851                         "\n"
3852                         "   style \"fullscreen-toolbar-style\"\n"
3853                         "   {\n"
3854                         "      GtkToolbar::shadow-type=GTK_SHADOW_NONE\n"
3855                         "   }\n"
3856                         "\n"
3857                         "    widget \"*.fullscreen-toolbar\" style \"fullscreen-toolbar-style\"\n"
3858                         "\n");
3859                 done = TRUE;
3860         }
3861         
3862         gtk_widget_set_name (toolbar, "fullscreen-toolbar");
3863 }
3864
3865 static void
3866 ev_window_run_fullscreen (EvWindow *window)
3867 {
3868         gboolean fullscreen_window = TRUE;
3869
3870         if (ev_document_model_get_fullscreen (window->priv->model))
3871                 return;
3872         
3873         if (!window->priv->fullscreen_toolbar) {
3874                 window->priv->fullscreen_toolbar =
3875                         gtk_ui_manager_get_widget (window->priv->ui_manager,
3876                                                    "/FullscreenToolbar");
3877
3878                 gtk_toolbar_set_style (GTK_TOOLBAR (window->priv->fullscreen_toolbar),
3879                                        GTK_TOOLBAR_BOTH_HORIZ);
3880                 fullscreen_toolbar_remove_shadow (window->priv->fullscreen_toolbar);
3881                 fullscreen_toolbar_setup_item_properties (window->priv->ui_manager);
3882
3883                 gtk_box_pack_start (GTK_BOX (window->priv->main_box),
3884                                     window->priv->fullscreen_toolbar,
3885                                     FALSE, FALSE, 0);
3886                 gtk_box_reorder_child (GTK_BOX (window->priv->main_box),
3887                                        window->priv->fullscreen_toolbar, 1);
3888         }
3889
3890         if (EV_WINDOW_IS_PRESENTATION (window)) {
3891                 ev_window_stop_presentation (window, FALSE);
3892                 fullscreen_window = FALSE;
3893         }
3894
3895         g_object_set (G_OBJECT (window->priv->scrolled_window),
3896                       "shadow-type", GTK_SHADOW_NONE,
3897                       NULL);
3898
3899         ev_document_model_set_fullscreen (window->priv->model, TRUE);
3900         ev_window_update_fullscreen_action (window);
3901
3902         /* If the user doesn't have the main toolbar he/she won't probably want
3903          * the toolbar in fullscreen mode. See bug #483048
3904          */
3905         update_chrome_flag (window, EV_CHROME_FULLSCREEN_TOOLBAR,
3906                             (window->priv->chrome & EV_CHROME_TOOLBAR) != 0);
3907         update_chrome_visibility (window);
3908
3909         if (fullscreen_window)
3910                 gtk_window_fullscreen (GTK_WINDOW (window));
3911         gtk_widget_grab_focus (window->priv->view);
3912
3913         if (window->priv->metadata && !ev_window_is_empty (window))
3914                 ev_metadata_set_boolean (window->priv->metadata, "fullscreen", TRUE);
3915 }
3916
3917 static void
3918 ev_window_stop_fullscreen (EvWindow *window,
3919                            gboolean  unfullscreen_window)
3920 {
3921         if (!ev_document_model_get_fullscreen (window->priv->model))
3922                 return;
3923
3924         g_object_set (G_OBJECT (window->priv->scrolled_window),
3925                       "shadow-type", GTK_SHADOW_IN,
3926                       NULL);
3927
3928         ev_document_model_set_fullscreen (window->priv->model, FALSE);
3929         ev_window_update_fullscreen_action (window);
3930         update_chrome_flag (window, EV_CHROME_FULLSCREEN_TOOLBAR, FALSE);
3931         update_chrome_visibility (window);
3932         if (unfullscreen_window)
3933                 gtk_window_unfullscreen (GTK_WINDOW (window));
3934
3935         if (window->priv->metadata && !ev_window_is_empty (window))
3936                 ev_metadata_set_boolean (window->priv->metadata, "fullscreen", FALSE);
3937 }
3938
3939 static void
3940 ev_window_cmd_view_fullscreen (GtkAction *action, EvWindow *window)
3941 {
3942         gboolean fullscreen;
3943
3944         fullscreen = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
3945         if (fullscreen) {
3946                 ev_window_run_fullscreen (window);
3947         } else {
3948                 ev_window_stop_fullscreen (window, TRUE);
3949         }
3950 }
3951
3952 static void
3953 ev_window_update_presentation_action (EvWindow *window)
3954 {
3955         GtkAction *action;
3956
3957         action = gtk_action_group_get_action (window->priv->action_group, "ViewPresentation");
3958         g_signal_handlers_block_by_func
3959                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
3960         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
3961                                       EV_WINDOW_IS_PRESENTATION (window));
3962         g_signal_handlers_unblock_by_func
3963                 (action, G_CALLBACK (ev_window_cmd_view_presentation), window);
3964 }
3965
3966 static void
3967 ev_window_view_presentation_finished (EvWindow *window)
3968 {
3969         ev_window_stop_presentation (window, TRUE);
3970 }
3971
3972 static void
3973 ev_window_run_presentation (EvWindow *window)
3974 {
3975         gboolean fullscreen_window = TRUE;
3976         guint    current_page;
3977         guint    rotation;
3978         gboolean inverted_colors;
3979
3980         if (EV_WINDOW_IS_PRESENTATION (window))
3981                 return;
3982
3983         if (ev_document_model_get_fullscreen (window->priv->model)) {
3984                 ev_window_stop_fullscreen (window, FALSE);
3985                 fullscreen_window = FALSE;
3986         }
3987
3988         current_page = ev_document_model_get_page (window->priv->model);
3989         rotation = ev_document_model_get_rotation (window->priv->model);
3990         inverted_colors = ev_document_model_get_inverted_colors (window->priv->model);
3991         window->priv->presentation_view = ev_view_presentation_new (window->priv->document,
3992                                                                     current_page,
3993                                                                     rotation,
3994                                                                     inverted_colors);
3995         g_signal_connect_swapped (window->priv->presentation_view, "finished",
3996                                   G_CALLBACK (ev_window_view_presentation_finished),
3997                                   window);
3998
3999         gtk_box_pack_start (GTK_BOX (window->priv->main_box),
4000                             window->priv->presentation_view,
4001                             TRUE, TRUE, 0);
4002
4003         gtk_widget_hide (window->priv->hpaned);
4004         ev_window_update_presentation_action (window);
4005         update_chrome_visibility (window);
4006
4007         gtk_widget_grab_focus (window->priv->presentation_view);
4008         if (fullscreen_window)
4009                 gtk_window_fullscreen (GTK_WINDOW (window));
4010
4011         gtk_widget_show (window->priv->presentation_view);
4012
4013         ev_application_screensaver_disable (EV_APP);
4014
4015         if (window->priv->metadata && !ev_window_is_empty (window))
4016                 ev_metadata_set_boolean (window->priv->metadata, "presentation", TRUE);
4017 }
4018
4019 static void
4020 ev_window_stop_presentation (EvWindow *window,
4021                              gboolean  unfullscreen_window)
4022 {
4023         guint current_page;
4024
4025         if (!EV_WINDOW_IS_PRESENTATION (window))
4026                 return;
4027
4028         current_page = ev_view_presentation_get_current_page (EV_VIEW_PRESENTATION (window->priv->presentation_view));
4029         ev_document_model_set_page (window->priv->model, current_page);
4030
4031         gtk_container_remove (GTK_CONTAINER (window->priv->main_box),
4032                               window->priv->presentation_view);
4033         window->priv->presentation_view = NULL;
4034
4035         gtk_widget_show (window->priv->hpaned);
4036         ev_window_update_presentation_action (window);
4037         update_chrome_visibility (window);
4038         if (unfullscreen_window)
4039                 gtk_window_unfullscreen (GTK_WINDOW (window));
4040
4041         gtk_widget_grab_focus (window->priv->view);
4042
4043         ev_application_screensaver_enable (EV_APP);
4044
4045         if (window->priv->metadata && !ev_window_is_empty (window))
4046                 ev_metadata_set_boolean (window->priv->metadata, "presentation", FALSE);
4047 }
4048
4049 static void
4050 ev_window_cmd_view_presentation (GtkAction *action, EvWindow *window)
4051 {
4052         gboolean presentation;
4053
4054         presentation = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
4055         if (presentation) {
4056                 ev_window_run_presentation (window);
4057         }
4058 }
4059
4060 static void
4061 ev_window_setup_gtk_settings (EvWindow *window)
4062 {
4063         GtkSettings *settings;
4064         GdkScreen   *screen;
4065         gchar       *menubar_accel_accel;
4066
4067         screen = gtk_window_get_screen (GTK_WINDOW (window));
4068         settings = gtk_settings_get_for_screen (screen);
4069
4070         g_object_get (settings,
4071                       "gtk-menu-bar-accel", &menubar_accel_accel,
4072                       NULL);
4073         if (menubar_accel_accel != NULL && menubar_accel_accel[0] != '\0') {
4074                 gtk_accelerator_parse (menubar_accel_accel,
4075                                        &window->priv->menubar_accel_keyval,
4076                                        &window->priv->menubar_accel_modifier);
4077                 if (window->priv->menubar_accel_keyval == 0) {
4078                         g_warning ("Failed to parse menu bar accelerator '%s'\n",
4079                                    menubar_accel_accel);
4080                 }
4081         } else {
4082                 window->priv->menubar_accel_keyval = 0;
4083                 window->priv->menubar_accel_modifier = 0;
4084         }
4085
4086         g_free (menubar_accel_accel);
4087 }
4088
4089 static void
4090 ev_window_update_max_min_scale (EvWindow *window)
4091 {
4092         gdouble    dpi;
4093         GtkAction *action;
4094         gdouble    min_width, min_height;
4095         gdouble    width, height;
4096         gdouble    max_scale;
4097         gint       rotation = ev_document_model_get_rotation (window->priv->model);
4098
4099         if (!window->priv->document)
4100                 return;
4101
4102         dpi = get_screen_dpi (window) / 72.0;
4103
4104         ev_document_get_min_page_size (window->priv->document, &min_width, &min_height);
4105         width = (rotation == 0 || rotation == 180) ? min_width : min_height;
4106         height = (rotation == 0 || rotation == 180) ? min_height : min_width;
4107         max_scale = sqrt (PAGE_CACHE_SIZE / (width * dpi * 4 * height * dpi));
4108
4109         action = gtk_action_group_get_action (window->priv->action_group,
4110                                               ZOOM_CONTROL_ACTION);
4111         ephy_zoom_action_set_max_zoom_level (EPHY_ZOOM_ACTION (action), max_scale * dpi);
4112
4113         ev_document_model_set_min_scale (window->priv->model, MIN_SCALE * dpi);
4114         ev_document_model_set_max_scale (window->priv->model, max_scale * dpi);
4115 }
4116
4117 static void
4118 ev_window_screen_changed (GtkWidget *widget,
4119                           GdkScreen *old_screen)
4120 {
4121         EvWindow *window = EV_WINDOW (widget);
4122         GdkScreen *screen;
4123
4124         screen = gtk_widget_get_screen (widget);
4125         if (screen == old_screen)
4126                 return;
4127
4128         ev_window_setup_gtk_settings (window);
4129         ev_window_update_max_min_scale (window);
4130
4131         if (GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed) {
4132                 GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed (widget, old_screen);
4133         }
4134 }
4135
4136 static gboolean
4137 ev_window_state_event (GtkWidget           *widget,
4138                        GdkEventWindowState *event)
4139 {
4140         EvWindow *window = EV_WINDOW (widget);
4141
4142         if (GTK_WIDGET_CLASS (ev_window_parent_class)->window_state_event) {
4143                 GTK_WIDGET_CLASS (ev_window_parent_class)->window_state_event (widget, event);
4144         }
4145
4146         if ((event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN) == 0)
4147                 return FALSE;
4148
4149         if (event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN) {
4150                 if (ev_document_model_get_fullscreen (window->priv->model) || EV_WINDOW_IS_PRESENTATION (window))
4151                         return FALSE;
4152                 
4153                 ev_window_run_fullscreen (window);
4154         } else {
4155                 if (ev_document_model_get_fullscreen (window->priv->model))
4156                         ev_window_stop_fullscreen (window, FALSE);
4157                 else if (EV_WINDOW_IS_PRESENTATION (window))
4158                         ev_window_stop_presentation (window, FALSE);
4159         }
4160
4161         return FALSE;
4162 }
4163
4164 static void
4165 ev_window_set_page_mode (EvWindow         *window,
4166                          EvWindowPageMode  page_mode)
4167 {
4168         GtkWidget *child = NULL;
4169         GtkWidget *real_child;
4170
4171         if (window->priv->page_mode == page_mode)
4172                 return;
4173
4174         window->priv->page_mode = page_mode;
4175
4176         switch (page_mode) {
4177                 case PAGE_MODE_DOCUMENT:
4178                         child = window->priv->view;
4179                         break;
4180                 case PAGE_MODE_PASSWORD:
4181                         child = window->priv->password_view;
4182                         break;
4183                 default:
4184                         g_assert_not_reached ();
4185         }
4186
4187         real_child = gtk_bin_get_child (GTK_BIN (window->priv->scrolled_window));
4188         if (child != real_child) {
4189                 gtk_container_remove (GTK_CONTAINER (window->priv->scrolled_window),
4190                                       real_child);
4191                 gtk_container_add (GTK_CONTAINER (window->priv->scrolled_window),
4192                                    child);
4193         }
4194         ev_window_update_actions (window);
4195 }
4196
4197
4198 static void
4199 ev_window_cmd_edit_rotate_left (GtkAction *action, EvWindow *ev_window)
4200 {
4201         gint rotation = ev_document_model_get_rotation (ev_window->priv->model);
4202
4203         ev_document_model_set_rotation (ev_window->priv->model, rotation - 90);
4204 }
4205
4206 static void
4207 ev_window_cmd_edit_rotate_right (GtkAction *action, EvWindow *ev_window)
4208 {
4209         gint rotation = ev_document_model_get_rotation (ev_window->priv->model);
4210
4211         ev_document_model_set_rotation (ev_window->priv->model, rotation + 90);
4212 }
4213
4214 static void
4215 ev_window_cmd_view_inverted_colors (GtkAction *action, EvWindow *ev_window)
4216 {
4217         gboolean inverted_colors = ev_document_model_get_inverted_colors (ev_window->priv->model);
4218
4219         ev_document_model_set_inverted_colors (ev_window->priv->model, !inverted_colors);
4220 }
4221
4222 static void
4223 ev_window_cmd_edit_toolbar_cb (GtkDialog *dialog,
4224                                gint       response,
4225                                EvWindow  *ev_window)
4226 {
4227         EggEditableToolbar *toolbar;
4228         gchar              *toolbars_file;
4229
4230         toolbar = EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar);
4231         egg_editable_toolbar_set_edit_mode (toolbar, FALSE);
4232
4233         toolbars_file = g_build_filename (ev_application_get_dot_dir (EV_APP, TRUE),
4234                                           "evince_toolbar.xml", NULL);
4235         egg_toolbars_model_save_toolbars (egg_editable_toolbar_get_model (toolbar),
4236                                           toolbars_file, "1.0");
4237         g_free (toolbars_file);
4238
4239         gtk_widget_destroy (GTK_WIDGET (dialog));
4240 }
4241
4242 static void
4243 ev_window_cmd_edit_toolbar (GtkAction *action, EvWindow *ev_window)
4244 {
4245         GtkWidget          *dialog;
4246         GtkWidget          *editor;
4247         GtkWidget          *content_area;
4248         EggEditableToolbar *toolbar;
4249
4250         dialog = gtk_dialog_new_with_buttons (_("Toolbar Editor"),
4251                                               GTK_WINDOW (ev_window),
4252                                               GTK_DIALOG_DESTROY_WITH_PARENT,
4253                                               GTK_STOCK_CLOSE,
4254                                               GTK_RESPONSE_CLOSE,
4255                                               NULL);
4256         content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
4257         gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
4258         gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dialog)), 5);
4259         gtk_box_set_spacing (GTK_BOX (content_area), 2);
4260         gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 400);
4261
4262         toolbar = EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar);
4263         editor = egg_toolbar_editor_new (ev_window->priv->ui_manager,
4264                                          egg_editable_toolbar_get_model (toolbar));
4265
4266         gtk_container_set_border_width (GTK_CONTAINER (editor), 5);
4267         gtk_box_set_spacing (GTK_BOX (EGG_TOOLBAR_EDITOR (editor)), 5);
4268
4269         gtk_container_add (GTK_CONTAINER (content_area), editor);
4270
4271         egg_editable_toolbar_set_edit_mode (toolbar, TRUE);
4272
4273         g_signal_connect (dialog, "response",
4274                           G_CALLBACK (ev_window_cmd_edit_toolbar_cb),
4275                           ev_window);
4276         gtk_widget_show_all (dialog);
4277 }
4278
4279 static void
4280 ev_window_cmd_edit_save_settings (GtkAction *action, EvWindow *ev_window)
4281 {
4282         EvWindowPrivate *priv = ev_window->priv;
4283         EvDocumentModel *model = priv->model;
4284         GSettings       *settings = priv->default_settings;
4285
4286         g_settings_set_boolean (settings, "continuous",
4287                                 ev_document_model_get_continuous (model));
4288         g_settings_set_boolean (settings, "dual-page",
4289                                 ev_document_model_get_dual_page (model));
4290         g_settings_set_boolean (settings, "fullscreen",
4291                                 ev_document_model_get_fullscreen (model));
4292         g_settings_set_boolean (settings, "inverted-colors",
4293                                 ev_document_model_get_inverted_colors (model));
4294         g_settings_set_enum (settings, "sizing-mode",
4295                              ev_document_model_get_sizing_mode (model));
4296         g_settings_set_boolean (settings, "show-toolbar",
4297                                 gtk_widget_get_visible (priv->toolbar));
4298         g_settings_set_boolean (settings, "show-sidebar",
4299                                 gtk_widget_get_visible (priv->sidebar));
4300         g_settings_set_int (settings, "sidebar-size",
4301                             gtk_paned_get_position (GTK_PANED (priv->hpaned)));
4302         g_settings_set_string (settings, "sidebar-page",
4303                                ev_window_sidebar_get_current_page_id (ev_window));
4304         g_settings_apply (settings);
4305 }
4306
4307 static void
4308 ev_window_cmd_view_zoom_in (GtkAction *action, EvWindow *ev_window)
4309 {
4310         g_return_if_fail (EV_IS_WINDOW (ev_window));
4311
4312         ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_FREE);
4313         ev_view_zoom_in (EV_VIEW (ev_window->priv->view));
4314 }
4315
4316 static void
4317 ev_window_cmd_view_zoom_out (GtkAction *action, EvWindow *ev_window)
4318 {
4319         g_return_if_fail (EV_IS_WINDOW (ev_window));
4320
4321         ev_document_model_set_sizing_mode (ev_window->priv->model, EV_SIZING_FREE);
4322         ev_view_zoom_out (EV_VIEW (ev_window->priv->view));
4323 }
4324
4325 static void
4326 ev_window_cmd_go_previous_page (GtkAction *action, EvWindow *ev_window)
4327 {
4328         g_return_if_fail (EV_IS_WINDOW (ev_window));
4329
4330         ev_view_previous_page (EV_VIEW (ev_window->priv->view));
4331 }
4332
4333 static void
4334 ev_window_cmd_go_next_page (GtkAction *action, EvWindow *ev_window)
4335 {
4336         g_return_if_fail (EV_IS_WINDOW (ev_window));
4337
4338         ev_view_next_page (EV_VIEW (ev_window->priv->view));
4339 }
4340
4341 static void
4342 ev_window_cmd_go_first_page (GtkAction *action, EvWindow *ev_window)
4343 {
4344         g_return_if_fail (EV_IS_WINDOW (ev_window));
4345
4346         ev_document_model_set_page (ev_window->priv->model, 0);
4347 }
4348
4349 static void
4350 ev_window_cmd_go_last_page (GtkAction *action, EvWindow *ev_window)
4351 {
4352         g_return_if_fail (EV_IS_WINDOW (ev_window));
4353
4354         ev_document_model_set_page (ev_window->priv->model,
4355                                     ev_document_get_n_pages (ev_window->priv->document) - 1);
4356 }
4357
4358 static void
4359 ev_window_cmd_go_forward (GtkAction *action, EvWindow *ev_window)
4360 {
4361         int n_pages, current_page;
4362         
4363         g_return_if_fail (EV_IS_WINDOW (ev_window));
4364
4365         n_pages = ev_document_get_n_pages (ev_window->priv->document);
4366         current_page = ev_document_model_get_page (ev_window->priv->model);
4367         
4368         if (current_page + 10 < n_pages) {
4369                 ev_document_model_set_page (ev_window->priv->model, current_page + 10);
4370         }
4371 }
4372
4373 static void
4374 ev_window_cmd_go_backward (GtkAction *action, EvWindow *ev_window)
4375 {
4376         int current_page;
4377         
4378         g_return_if_fail (EV_IS_WINDOW (ev_window));
4379
4380         current_page = ev_document_model_get_page (ev_window->priv->model);
4381         
4382         if (current_page - 10 >= 0) {
4383                 ev_document_model_set_page (ev_window->priv->model, current_page - 10);
4384         }
4385 }
4386
4387 static void
4388 ev_window_cmd_bookmark_activate (GtkAction *action,
4389                                  EvWindow  *window)
4390 {
4391         guint page = ev_bookmark_action_get_page (EV_BOOKMARK_ACTION (action));
4392
4393         ev_document_model_set_page (window->priv->model, page);
4394 }
4395
4396 static gint
4397 compare_bookmarks (EvBookmark *a,
4398                    EvBookmark *b)
4399 {
4400         return strcmp (a->title, b->title);
4401 }
4402
4403 static void
4404 ev_window_setup_bookmarks (EvWindow *window)
4405 {
4406         GList *items, *l;
4407
4408         if (!window->priv->bookmarks)
4409                 return;
4410
4411         if (window->priv->bookmarks_ui_id > 0) {
4412                 gtk_ui_manager_remove_ui (window->priv->ui_manager,
4413                                           window->priv->bookmarks_ui_id);
4414                 gtk_ui_manager_ensure_update (window->priv->ui_manager);
4415         }
4416         window->priv->bookmarks_ui_id = gtk_ui_manager_new_merge_id (window->priv->ui_manager);
4417
4418         if (window->priv->bookmarks_action_group) {
4419                 gtk_ui_manager_remove_action_group (window->priv->ui_manager,
4420                                                     window->priv->bookmarks_action_group);
4421                 g_object_unref (window->priv->bookmarks_action_group);
4422         }
4423         window->priv->bookmarks_action_group = gtk_action_group_new ("BookmarksActions");
4424         gtk_ui_manager_insert_action_group (window->priv->ui_manager,
4425                                             window->priv->bookmarks_action_group, -1);
4426
4427         items = ev_bookmarks_get_bookmarks (window->priv->bookmarks);
4428         items = g_list_sort (items, (GCompareFunc)compare_bookmarks);
4429
4430         for (l = items; l && l->data; l = g_list_next (l)) {
4431                 EvBookmark *bm = (EvBookmark *)l->data;
4432                 GtkAction  *action;
4433
4434                 action = ev_bookmark_action_new (bm);
4435                 g_signal_connect (action, "activate",
4436                                   G_CALLBACK (ev_window_cmd_bookmark_activate),
4437                                   window);
4438                 gtk_action_group_add_action (window->priv->bookmarks_action_group,
4439                                              action);
4440
4441                 gtk_ui_manager_add_ui (window->priv->ui_manager,
4442                                        window->priv->bookmarks_ui_id,
4443                                        "/MainMenu/BookmarksMenu/BookmarksItems",
4444                                        gtk_action_get_label (action),
4445                                        gtk_action_get_name (action),
4446                                        GTK_UI_MANAGER_MENUITEM,
4447                                        FALSE);
4448
4449                 g_object_unref (action);
4450         }
4451
4452         g_list_free (items);
4453 }
4454
4455 static void
4456 ev_window_cmd_bookmarks_add (GtkAction *action,
4457                              EvWindow  *window)
4458 {
4459         EvBookmark bm;
4460         gchar     *page_label;
4461         gchar     *page_title;
4462
4463         bm.page = ev_document_model_get_page (window->priv->model);
4464         page_label = ev_document_get_page_label (window->priv->document, bm.page);
4465         page_title = ev_window_get_page_title (window, page_label);
4466         bm.title = page_title ? page_title : g_strdup_printf (_("Page %s"), page_label);
4467         g_free (page_label);
4468
4469         /* EvBookmarks takes ownership of bookmark */
4470         ev_bookmarks_add (window->priv->bookmarks, &bm);
4471 }
4472
4473 static void
4474 ev_window_cmd_view_reload (GtkAction *action, EvWindow *ev_window)
4475 {
4476         ev_window_reload_document (ev_window, NULL);
4477 }
4478
4479 static void
4480 ev_window_cmd_view_autoscroll (GtkAction *action, EvWindow *ev_window)
4481 {
4482         ev_view_autoscroll_start (EV_VIEW (ev_window->priv->view));
4483 }
4484
4485 #if OFFLINE_HELP_ENABLED
4486 #define EV_HELP "ghelp:evince"
4487 #else
4488 #define EV_HELP "http://library.gnome.org/users/evince/stable/"
4489 #endif
4490
4491 static void
4492 ev_window_cmd_help_contents (GtkAction *action, EvWindow *ev_window)
4493 {
4494         GError  *error = NULL;
4495
4496         gtk_show_uri (gtk_window_get_screen (GTK_WINDOW (ev_window)),
4497                       EV_HELP,
4498                       gtk_get_current_event_time (),
4499                       &error);
4500         if (error) {
4501                 ev_window_error_message (ev_window, error, 
4502                                          "%s", _("There was an error displaying help"));
4503                 g_error_free (error);
4504         }
4505 }
4506
4507 static void
4508 ev_window_cmd_leave_fullscreen (GtkAction *action, EvWindow *window)
4509 {
4510         ev_window_stop_fullscreen (window, TRUE);
4511 }
4512
4513 static void
4514 ev_window_cmd_start_presentation (GtkAction *action, EvWindow *window)
4515 {
4516         ev_window_run_presentation (window);
4517 }
4518
4519 static void
4520 ev_window_cmd_escape (GtkAction *action, EvWindow *window)
4521 {
4522         GtkWidget *widget;
4523
4524         ev_view_autoscroll_stop (EV_VIEW (window->priv->view));
4525         
4526         widget = gtk_window_get_focus (GTK_WINDOW (window));
4527         if (widget && gtk_widget_get_ancestor (widget, EGG_TYPE_FIND_BAR)) {
4528                 update_chrome_flag (window, EV_CHROME_FINDBAR, FALSE);
4529                 update_chrome_visibility (window);
4530                 gtk_widget_grab_focus (window->priv->view);
4531         } else {
4532                 gboolean fullscreen;
4533
4534                 fullscreen = ev_document_model_get_fullscreen (window->priv->model);
4535
4536                 if (fullscreen) {
4537                         ev_window_stop_fullscreen (window, TRUE);
4538                 } else if (EV_WINDOW_IS_PRESENTATION (window)) {
4539                         ev_window_stop_presentation (window, TRUE);
4540                         gtk_widget_grab_focus (window->priv->view);
4541                 } else {
4542                         gtk_widget_grab_focus (window->priv->view);
4543                 }
4544
4545                 if (fullscreen && EV_WINDOW_IS_PRESENTATION (window))
4546                         g_warning ("Both fullscreen and presentation set somehow");
4547         }
4548 }
4549
4550 static void
4551 save_sizing_mode (EvWindow *window)
4552 {
4553         EvSizingMode mode;
4554         GEnumValue *enum_value;
4555
4556         if (!window->priv->metadata || ev_window_is_empty (window))
4557                 return;
4558
4559         mode = ev_document_model_get_sizing_mode (window->priv->model);
4560         enum_value = g_enum_get_value (g_type_class_peek (EV_TYPE_SIZING_MODE), mode);
4561         ev_metadata_set_string (window->priv->metadata, "sizing_mode",
4562                                 enum_value->value_nick);
4563 }
4564
4565 static void
4566 ev_window_document_changed_cb (EvDocumentModel *model,
4567                                GParamSpec      *pspec,
4568                                EvWindow        *ev_window)
4569 {
4570         ev_window_set_document (ev_window,
4571                                 ev_document_model_get_document (model));
4572 }
4573
4574 static void
4575 ev_window_sizing_mode_changed_cb (EvDocumentModel *model,
4576                                   GParamSpec      *pspec,
4577                                   EvWindow        *ev_window)
4578 {
4579         EvSizingMode sizing_mode = ev_document_model_get_sizing_mode (model);
4580
4581         g_object_set (ev_window->priv->scrolled_window,
4582                       "hscrollbar-policy",
4583                       sizing_mode == EV_SIZING_FREE ?
4584                       GTK_POLICY_AUTOMATIC : GTK_POLICY_NEVER,
4585                       "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
4586                       NULL);
4587
4588         update_sizing_buttons (ev_window);
4589         save_sizing_mode (ev_window);
4590 }
4591
4592 static void
4593 ev_window_zoom_changed_cb (EvDocumentModel *model, GParamSpec *pspec, EvWindow *ev_window)
4594 {
4595         ev_window_update_actions (ev_window);
4596
4597         if (!ev_window->priv->metadata)
4598                 return;
4599
4600         if (ev_document_model_get_sizing_mode (model) == EV_SIZING_FREE && !ev_window_is_empty (ev_window)) {
4601                 gdouble zoom;
4602
4603                 zoom = ev_document_model_get_scale (model);
4604                 zoom *= 72.0 / get_screen_dpi (ev_window);
4605                 ev_metadata_set_double (ev_window->priv->metadata, "zoom", zoom);
4606         }
4607 }
4608
4609 static void
4610 ev_window_update_continuous_action (EvWindow *window)
4611 {
4612         GtkAction *action;
4613
4614         action = gtk_action_group_get_action (window->priv->action_group, "ViewContinuous");
4615         g_signal_handlers_block_by_func
4616                 (action, G_CALLBACK (ev_window_cmd_continuous), window);
4617         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
4618                                       ev_document_model_get_continuous (window->priv->model));
4619         g_signal_handlers_unblock_by_func
4620                 (action, G_CALLBACK (ev_window_cmd_continuous), window);
4621 }
4622
4623 static void
4624 ev_window_update_dual_page_action (EvWindow *window)
4625 {
4626         GtkAction *action;
4627
4628         action = gtk_action_group_get_action (window->priv->action_group, "ViewDual");
4629         g_signal_handlers_block_by_func
4630                 (action, G_CALLBACK (ev_window_cmd_dual), window);
4631         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
4632                                       ev_document_model_get_dual_page (window->priv->model));
4633         g_signal_handlers_unblock_by_func
4634                 (action, G_CALLBACK (ev_window_cmd_dual), window);
4635 }
4636
4637 static void
4638 ev_window_continuous_changed_cb (EvDocumentModel *model,
4639                                  GParamSpec      *pspec,
4640                                  EvWindow        *ev_window)
4641 {
4642         ev_window_update_continuous_action (ev_window);
4643
4644         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window))
4645                 ev_metadata_set_boolean (ev_window->priv->metadata, "continuous",
4646                                          ev_document_model_get_continuous (model));
4647 }
4648
4649 static void
4650 ev_window_rotation_changed_cb (EvDocumentModel *model,
4651                                GParamSpec      *pspec,
4652                                EvWindow        *window)
4653 {
4654         gint rotation = ev_document_model_get_rotation (model);
4655
4656         if (window->priv->metadata && !ev_window_is_empty (window))
4657                 ev_metadata_set_int (window->priv->metadata, "rotation",
4658                                      rotation);
4659
4660         ev_window_update_max_min_scale (window);
4661         ev_window_refresh_window_thumbnail (window);
4662 }
4663
4664 static void
4665 ev_window_update_inverted_colors_action (EvWindow *window)
4666 {
4667         GtkAction *action;
4668
4669         action = gtk_action_group_get_action (window->priv->action_group, "ViewInvertedColors");
4670         g_signal_handlers_block_by_func
4671                 (action, G_CALLBACK (ev_window_cmd_view_inverted_colors), window);
4672         gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action),
4673                                       ev_document_model_get_inverted_colors (window->priv->model));
4674         g_signal_handlers_unblock_by_func
4675                 (action, G_CALLBACK (ev_window_cmd_view_inverted_colors), window);
4676 }
4677
4678 static void
4679 ev_window_inverted_colors_changed_cb (EvDocumentModel *model,
4680                                       GParamSpec      *pspec,
4681                                       EvWindow        *window)
4682 {
4683         gboolean inverted_colors = ev_document_model_get_inverted_colors (model);
4684
4685         ev_window_update_inverted_colors_action (window);
4686
4687         if (window->priv->metadata && !ev_window_is_empty (window))
4688                 ev_metadata_set_boolean (window->priv->metadata, "inverted-colors",
4689                                          inverted_colors);
4690
4691         ev_window_refresh_window_thumbnail (window);
4692 }
4693
4694 static void
4695 ev_window_dual_mode_changed_cb (EvDocumentModel *model,
4696                                 GParamSpec      *pspec,
4697                                 EvWindow        *ev_window)
4698 {
4699         ev_window_update_dual_page_action (ev_window);
4700
4701         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window))
4702                 ev_metadata_set_boolean (ev_window->priv->metadata, "dual-page",
4703                                          ev_document_model_get_dual_page (model));
4704 }
4705
4706 static char *
4707 build_comments_string (EvDocument *document)
4708 {
4709         gchar *comments = NULL;
4710         EvDocumentBackendInfo info;
4711
4712         if (document && ev_document_get_backend_info (document, &info)) {
4713                 comments = g_strdup_printf (
4714                         _("Document Viewer\nUsing %s (%s)"),
4715                         info.name, info.version);
4716         } else {
4717                 comments = g_strdup_printf (
4718                         _("Document Viewer"));
4719         }
4720
4721         return comments;
4722 }
4723
4724 static void
4725 ev_window_cmd_help_about (GtkAction *action, EvWindow *ev_window)
4726 {
4727         const char *authors[] = {
4728                 "Martin Kretzschmar <m_kretzschmar@gmx.net>",
4729                 "Jonathan Blandford <jrb@gnome.org>",
4730                 "Marco Pesenti Gritti <marco@gnome.org>",
4731                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
4732                 "Bryan Clark <clarkbw@gnome.org>",
4733                 "Carlos Garcia Campos <carlosgc@gnome.org>",
4734                 "Wouter Bolsterlee <wbolster@gnome.org>",
4735                 "Christian Persch <chpe" "\100" "gnome.org>",
4736                 NULL
4737         };
4738
4739         const char *documenters[] = {
4740                 "Nickolay V. Shmyrev <nshmyrev@yandex.ru>",
4741                 NULL
4742         };
4743
4744         const char *license[] = {
4745                 N_("Evince is free software; you can redistribute it and/or modify "
4746                    "it under the terms of the GNU General Public License as published by "
4747                    "the Free Software Foundation; either version 2 of the License, or "
4748                    "(at your option) any later version.\n"),
4749                 N_("Evince is distributed in the hope that it will be useful, "
4750                    "but WITHOUT ANY WARRANTY; without even the implied warranty of "
4751                    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
4752                    "GNU General Public License for more details.\n"),
4753                 N_("You should have received a copy of the GNU General Public License "
4754                    "along with Evince; if not, write to the Free Software Foundation, Inc., "
4755                    "51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA\n")
4756         };
4757
4758         char *license_trans;
4759         char *comments;
4760
4761 #ifdef ENABLE_NLS
4762         const char **p;
4763
4764         for (p = authors; *p; ++p)
4765                 *p = _(*p);
4766
4767         for (p = documenters; *p; ++p)
4768                 *p = _(*p);
4769 #endif
4770
4771         license_trans = g_strconcat (_(license[0]), "\n", _(license[1]), "\n",
4772                                      _(license[2]), "\n", NULL);
4773
4774         comments = build_comments_string (ev_window->priv->document);
4775
4776         gtk_show_about_dialog (
4777                 GTK_WINDOW (ev_window),
4778                 "name", _("Evince"),
4779                 "version", VERSION,
4780                 "copyright",
4781                 _("© 1996–2010 The Evince authors"),
4782                 "license", license_trans,
4783                 "website", "http://www.gnome.org/projects/evince",
4784                 "comments", comments,
4785                 "authors", authors,
4786                 "documenters", documenters,
4787                 "translator-credits", _("translator-credits"),
4788                 "logo-icon-name", "evince",
4789                 "wrap-license", TRUE,
4790                 NULL);
4791
4792         g_free (comments);
4793         g_free (license_trans);
4794 }
4795
4796 static void
4797 ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
4798 {
4799         gboolean active;
4800         
4801         active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
4802         update_chrome_flag (ev_window, EV_CHROME_TOOLBAR, active);
4803         update_chrome_visibility (ev_window);
4804         if (ev_window->priv->metadata)
4805                 ev_metadata_set_boolean (ev_window->priv->metadata, "show_toolbar", active);
4806 }
4807
4808 static void
4809 ev_window_view_sidebar_cb (GtkAction *action, EvWindow *ev_window)
4810 {
4811         if (EV_WINDOW_IS_PRESENTATION (ev_window))
4812                 return;
4813             
4814         update_chrome_flag (ev_window, EV_CHROME_SIDEBAR,
4815                             gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)));
4816         update_chrome_visibility (ev_window);
4817 }
4818
4819 static void
4820 ev_window_sidebar_current_page_changed_cb (EvSidebar  *ev_sidebar,
4821                                            GParamSpec *pspec,
4822                                            EvWindow   *ev_window)
4823 {
4824         if (ev_window->priv->metadata && !ev_window_is_empty (ev_window)) {
4825                 ev_metadata_set_string (ev_window->priv->metadata,
4826                                         "sidebar_page",
4827                                         ev_window_sidebar_get_current_page_id (ev_window));
4828         }
4829 }
4830
4831 static void
4832 ev_window_sidebar_visibility_changed_cb (EvSidebar  *ev_sidebar,
4833                                          GParamSpec *pspec,
4834                                          EvWindow   *ev_window)
4835 {
4836         GtkAction *action;
4837
4838         action = gtk_action_group_get_action (ev_window->priv->action_group, "ViewSidebar");
4839
4840         if (!EV_WINDOW_IS_PRESENTATION (ev_window)) {
4841                 gboolean visible = gtk_widget_get_visible (GTK_WIDGET (ev_sidebar));
4842
4843                 gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), visible);
4844
4845                 if (ev_window->priv->metadata)
4846                         ev_metadata_set_boolean (ev_window->priv->metadata, "sidebar_visibility",
4847                                                  visible);
4848         }
4849 }
4850
4851 static void
4852 view_menu_link_popup (EvWindow *ev_window,
4853                       EvLink   *link)
4854 {
4855         gboolean   show_external = FALSE;
4856         gboolean   show_internal = FALSE;
4857         GtkAction *action;
4858         
4859         if (ev_window->priv->link)
4860                 g_object_unref (ev_window->priv->link);
4861         
4862         if (link)
4863                 ev_window->priv->link = g_object_ref (link);
4864         else    
4865                 ev_window->priv->link = NULL;
4866
4867         if (ev_window->priv->link) {
4868                 EvLinkAction *ev_action;
4869
4870                 ev_action = ev_link_get_action (link);
4871                 if (ev_action) {
4872                         switch (ev_link_action_get_action_type (ev_action)) {
4873                                 case EV_LINK_ACTION_TYPE_GOTO_DEST:
4874                                 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
4875                                         show_internal = TRUE;
4876                                         break;
4877                                 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
4878                                 case EV_LINK_ACTION_TYPE_LAUNCH:
4879                                         show_external = TRUE;
4880                                         break;
4881                                 default:
4882                                         break;
4883                         }
4884                 }
4885         }
4886         
4887         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4888                                               "OpenLink");
4889         gtk_action_set_visible (action, show_external);
4890
4891         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4892                                               "CopyLinkAddress");
4893         gtk_action_set_visible (action, show_external);
4894
4895         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4896                                               "GoLink");
4897         gtk_action_set_visible (action, show_internal);
4898
4899         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4900                                               "OpenLinkNewWindow");
4901         gtk_action_set_visible (action, show_internal);
4902 }
4903
4904 static void
4905 view_menu_image_popup (EvWindow  *ev_window,
4906                        EvImage   *image)
4907 {
4908         GtkAction *action;
4909         gboolean   show_image = FALSE;
4910         
4911         if (ev_window->priv->image)
4912                 g_object_unref (ev_window->priv->image);
4913         
4914         if (image)
4915                 ev_window->priv->image = g_object_ref (image);
4916         else    
4917                 ev_window->priv->image = NULL;
4918
4919         show_image = (ev_window->priv->image != NULL);
4920         
4921         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4922                                               "SaveImageAs");
4923         gtk_action_set_visible (action, show_image);
4924
4925         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4926                                               "CopyImage");
4927         gtk_action_set_visible (action, show_image);
4928 }
4929
4930 static void
4931 view_menu_annot_popup (EvWindow     *ev_window,
4932                        EvAnnotation *annot)
4933 {
4934         GtkAction *action;
4935         gboolean   show_annot = FALSE;
4936
4937         if (ev_window->priv->annot)
4938                 g_object_unref (ev_window->priv->annot);
4939         ev_window->priv->annot = (annot) ? g_object_ref (annot) : NULL;
4940
4941         action = gtk_action_group_get_action (ev_window->priv->view_popup_action_group,
4942                                               "AnnotProperties");
4943         gtk_action_set_visible (action, (annot != NULL && EV_IS_ANNOTATION_MARKUP (annot)));
4944
4945         if (annot && EV_IS_ANNOTATION_ATTACHMENT (annot)) {
4946                 EvAttachment *attachment;
4947
4948                 attachment = ev_annotation_attachment_get_attachment (EV_ANNOTATION_ATTACHMENT (annot));
4949                 if (attachment) {
4950                         show_annot = TRUE;
4951                         if (ev_window->priv->attach_list) {
4952                                 g_list_foreach (ev_window->priv->attach_list,
4953                                                 (GFunc) g_object_unref, NULL);
4954                                 g_list_free (ev_window->priv->attach_list);
4955                                 ev_window->priv->attach_list = NULL;
4956                         }
4957                         ev_window->priv->attach_list =
4958                                 g_list_prepend (ev_window->priv->attach_list,
4959                                                 g_object_ref (attachment));
4960                 }
4961         }
4962
4963         action = gtk_action_group_get_action (ev_window->priv->attachment_popup_action_group,
4964                                               "OpenAttachment");
4965         gtk_action_set_visible (action, show_annot);
4966
4967         action = gtk_action_group_get_action (ev_window->priv->attachment_popup_action_group,
4968                                               "SaveAttachmentAs");
4969         gtk_action_set_visible (action, show_annot);
4970 }
4971
4972 static gboolean
4973 view_menu_popup_cb (EvView   *view,
4974                     GList    *items,
4975                     EvWindow *ev_window)
4976 {
4977         GList   *l;
4978         gboolean has_link = FALSE;
4979         gboolean has_image = FALSE;
4980         gboolean has_annot = FALSE;
4981
4982         for (l = items; l; l = g_list_next (l)) {
4983                 if (EV_IS_LINK (l->data)) {
4984                         view_menu_link_popup (ev_window, EV_LINK (l->data));
4985                         has_link = TRUE;
4986                 } else if (EV_IS_IMAGE (l->data)) {
4987                         view_menu_image_popup (ev_window, EV_IMAGE (l->data));
4988                         has_image = TRUE;
4989                 } else if (EV_IS_ANNOTATION (l->data)) {
4990                         view_menu_annot_popup (ev_window, EV_ANNOTATION (l->data));
4991                         has_annot = TRUE;
4992                 }
4993         }
4994
4995         if (!has_link)
4996                 view_menu_link_popup (ev_window, NULL);
4997         if (!has_image)
4998                 view_menu_image_popup (ev_window, NULL);
4999         if (!has_annot)
5000                 view_menu_annot_popup (ev_window, NULL);
5001
5002         gtk_menu_popup (GTK_MENU (ev_window->priv->view_popup),
5003                         NULL, NULL, NULL, NULL,
5004                         3, gtk_get_current_event_time ());
5005         return TRUE;
5006 }
5007
5008 static gboolean
5009 attachment_bar_menu_popup_cb (EvSidebarAttachments *attachbar,
5010                               GList           *attach_list,
5011                               EvWindow        *ev_window)
5012 {
5013         GtkWidget *popup;
5014
5015         g_assert (attach_list != NULL);
5016
5017         if (ev_window->priv->attach_list) {
5018                 g_list_foreach (ev_window->priv->attach_list,
5019                                 (GFunc) g_object_unref, NULL);
5020                 g_list_free (ev_window->priv->attach_list);
5021         }
5022         
5023         ev_window->priv->attach_list = attach_list;
5024         
5025         popup = ev_window->priv->attachment_popup;
5026
5027         gtk_menu_popup (GTK_MENU (popup), NULL, NULL,
5028                         NULL, NULL,
5029                         3, gtk_get_current_event_time ());
5030
5031         return TRUE;
5032 }
5033
5034 static void
5035 ev_window_update_find_status_message (EvWindow *ev_window)
5036 {
5037         gchar *message;
5038
5039         if (!ev_window->priv->find_job)
5040                 return;
5041         
5042         if (ev_job_is_finished (ev_window->priv->find_job)) {
5043                 EvJobFind *job_find = EV_JOB_FIND (ev_window->priv->find_job);
5044
5045                 if (ev_job_find_has_results (job_find)) {
5046                         gint n_results;
5047
5048                         n_results = ev_job_find_get_n_results (job_find,
5049                                                                ev_document_model_get_page (ev_window->priv->model));
5050                         /* TRANS: Sometimes this could be better translated as
5051                            "%d hit(s) on this page".  Therefore this string
5052                            contains plural cases. */
5053                         message = g_strdup_printf (ngettext ("%d found on this page",
5054                                                              "%d found on this page",
5055                                                              n_results),
5056                                                    n_results);
5057                 } else {
5058                         message = g_strdup (_("Not found"));
5059                 }
5060         } else {
5061                 gdouble percent;
5062
5063                 percent = ev_job_find_get_progress (EV_JOB_FIND (ev_window->priv->find_job));
5064                 message = g_strdup_printf (_("%3d%% remaining to search"),
5065                                            (gint) ((1.0 - percent) * 100));
5066         }
5067         
5068         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar), message);
5069         g_free (message);
5070 }
5071
5072 static void
5073 ev_window_find_job_finished_cb (EvJobFind *job,
5074                                 EvWindow  *ev_window)
5075 {
5076         ev_window_update_find_status_message (ev_window);
5077 }
5078
5079 static void
5080 ev_window_find_job_updated_cb (EvJobFind *job,
5081                                gint       page,
5082                                EvWindow  *ev_window)
5083 {
5084         ev_window_update_actions (ev_window);
5085         
5086         ev_view_find_changed (EV_VIEW (ev_window->priv->view),
5087                               ev_job_find_get_results (job),
5088                               page);
5089         ev_window_update_find_status_message (ev_window);
5090 }
5091
5092 static void
5093 ev_window_clear_find_job (EvWindow *ev_window)
5094 {
5095         if (ev_window->priv->find_job != NULL) {
5096                 if (!ev_job_is_finished (ev_window->priv->find_job))
5097                         ev_job_cancel (ev_window->priv->find_job);
5098
5099                 g_signal_handlers_disconnect_by_func (ev_window->priv->find_job,
5100                                                       ev_window_find_job_finished_cb,
5101                                                       ev_window);
5102                 g_signal_handlers_disconnect_by_func (ev_window->priv->find_job,
5103                                                       ev_window_find_job_updated_cb,
5104                                                       ev_window);
5105                 g_object_unref (ev_window->priv->find_job);
5106                 ev_window->priv->find_job = NULL;
5107         }
5108 }
5109
5110 static void
5111 find_bar_previous_cb (EggFindBar *find_bar,
5112                       EvWindow   *ev_window)
5113 {
5114         ev_view_find_previous (EV_VIEW (ev_window->priv->view));
5115 }
5116
5117 static void
5118 find_bar_next_cb (EggFindBar *find_bar,
5119                   EvWindow   *ev_window)
5120 {
5121         ev_view_find_next (EV_VIEW (ev_window->priv->view));
5122 }
5123
5124 static void
5125 find_bar_close_cb (EggFindBar *find_bar,
5126                    EvWindow   *ev_window)
5127 {
5128         ev_view_find_cancel (EV_VIEW (ev_window->priv->view));
5129         ev_window_clear_find_job (ev_window);
5130         update_chrome_flag (ev_window, EV_CHROME_FINDBAR, FALSE);
5131         update_chrome_visibility (ev_window);
5132 }
5133
5134 static void
5135 find_bar_search_changed_cb (EggFindBar *find_bar,
5136                             GParamSpec *param,
5137                             EvWindow   *ev_window)
5138 {
5139         gboolean case_sensitive;
5140         const char *search_string;
5141
5142         if (!ev_window->priv->document || !EV_IS_DOCUMENT_FIND (ev_window->priv->document))
5143                 return;
5144         
5145         /* Either the string or case sensitivity could have changed. */
5146         case_sensitive = egg_find_bar_get_case_sensitive (find_bar);
5147         search_string = egg_find_bar_get_search_string (find_bar);
5148
5149         ev_view_find_search_changed (EV_VIEW (ev_window->priv->view));
5150
5151         ev_window_clear_find_job (ev_window);
5152
5153         if (search_string && search_string[0]) {
5154                 ev_window->priv->find_job = ev_job_find_new (ev_window->priv->document,
5155                                                              ev_document_model_get_page (ev_window->priv->model),
5156                                                              ev_document_get_n_pages (ev_window->priv->document),
5157                                                              search_string,
5158                                                              case_sensitive);
5159                 g_signal_connect (ev_window->priv->find_job, "finished",
5160                                   G_CALLBACK (ev_window_find_job_finished_cb),
5161                                   ev_window);
5162                 g_signal_connect (ev_window->priv->find_job, "updated",
5163                                   G_CALLBACK (ev_window_find_job_updated_cb),
5164                                   ev_window);
5165                 ev_job_scheduler_push_job (ev_window->priv->find_job, EV_JOB_PRIORITY_NONE);
5166         } else {
5167                 ev_window_update_actions (ev_window);
5168                 egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar),
5169                                               NULL);
5170                 gtk_widget_queue_draw (GTK_WIDGET (ev_window->priv->view));
5171         }
5172 }
5173
5174 static void
5175 find_bar_visibility_changed_cb (EggFindBar *find_bar,
5176                                 GParamSpec *param,
5177                                 EvWindow   *ev_window)
5178 {
5179         gboolean visible;
5180
5181         visible = gtk_widget_get_visible (GTK_WIDGET (find_bar));
5182
5183         if (ev_window->priv->document &&
5184             EV_IS_DOCUMENT_FIND (ev_window->priv->document)) {
5185                 ev_view_find_set_highlight_search (EV_VIEW (ev_window->priv->view), visible);
5186                 ev_view_find_search_changed (EV_VIEW (ev_window->priv->view));
5187                 ev_window_update_actions (ev_window);
5188
5189                 if (visible)
5190                         find_bar_search_changed_cb (find_bar, NULL, ev_window);
5191                 else
5192                         egg_find_bar_set_status_text (EGG_FIND_BAR (ev_window->priv->find_bar), NULL);
5193         }
5194 }
5195
5196 static void
5197 find_bar_scroll (EggFindBar   *find_bar,
5198                  GtkScrollType scroll,
5199                  EvWindow     *ev_window)
5200 {
5201         ev_view_scroll (EV_VIEW (ev_window->priv->view), scroll, FALSE);
5202 }
5203
5204 static void
5205 zoom_control_changed_cb (EphyZoomAction *action,
5206                          float           zoom,
5207                          EvWindow       *ev_window)
5208 {
5209         EvSizingMode mode;
5210
5211         if (zoom == EPHY_ZOOM_BEST_FIT) {
5212                 mode = EV_SIZING_BEST_FIT;
5213         } else if (zoom == EPHY_ZOOM_FIT_WIDTH) {
5214                 mode = EV_SIZING_FIT_WIDTH;
5215         } else {
5216                 mode = EV_SIZING_FREE;
5217         }
5218
5219         ev_document_model_set_sizing_mode (ev_window->priv->model, mode);
5220
5221         if (mode == EV_SIZING_FREE) {
5222                 ev_document_model_set_scale (ev_window->priv->model,
5223                                              zoom * get_screen_dpi (ev_window) / 72.0);
5224         }
5225 }
5226
5227 static void
5228 ev_window_drag_data_received (GtkWidget        *widget,
5229                               GdkDragContext   *context,
5230                               gint              x,
5231                               gint              y,
5232                               GtkSelectionData *selection_data,
5233                               guint             info,
5234                               guint             time)
5235
5236 {
5237         EvWindow  *window = EV_WINDOW (widget);
5238         gchar    **uris;
5239         gint       i = 0;
5240         GSList    *uri_list = NULL;
5241         GtkWidget *source;
5242
5243         source = gtk_drag_get_source_widget (context);
5244         if (source && widget == gtk_widget_get_toplevel (source)) {
5245                 gtk_drag_finish (context, FALSE, FALSE, time);
5246                 return;
5247         }
5248
5249         uris = gtk_selection_data_get_uris (selection_data);
5250         if (!uris) {
5251                 gtk_drag_finish (context, FALSE, FALSE, time);
5252                 return;
5253         }
5254
5255         for (i = 0; uris[i]; i++) {
5256                 uri_list = g_slist_prepend (uri_list, (gpointer) uris[i]);
5257         }
5258
5259         ev_application_open_uri_list (EV_APP, uri_list,
5260                                       gtk_window_get_screen (GTK_WINDOW (window)),
5261                                       0);
5262         gtk_drag_finish (context, TRUE, FALSE, time);
5263
5264         g_strfreev (uris);
5265         g_slist_free (uri_list);
5266 }
5267
5268 static void
5269 ev_window_finalize (GObject *object)
5270 {
5271         G_OBJECT_CLASS (ev_window_parent_class)->finalize (object);
5272
5273         if (ev_window_n_copies == 0) {
5274                 ev_application_shutdown (EV_APP);
5275         } else {
5276                 ev_window_n_copies--;
5277         }
5278 }
5279
5280 static void
5281 ev_window_dispose (GObject *object)
5282 {
5283         EvWindow *window = EV_WINDOW (object);
5284         EvWindowPrivate *priv = window->priv;
5285         GObject *mpkeys = ev_application_get_media_keys (EV_APP);
5286
5287         if (mpkeys) {
5288                 g_signal_handlers_disconnect_by_func (mpkeys,
5289                                                       ev_window_media_player_key_pressed,
5290                                                       window);
5291         }
5292
5293 #ifdef ENABLE_DBUS
5294         if (priv->dbus_object_id > 0) {
5295                 ev_window_emit_closed (window);
5296                 g_dbus_connection_unregister_object (ev_application_get_dbus_connection (EV_APP),
5297                                                      priv->dbus_object_id);
5298                 priv->dbus_object_id = 0;
5299         }
5300
5301         if (priv->dbus_object_path) {
5302                 g_free (priv->dbus_object_path);
5303                 priv->dbus_object_path = NULL;
5304         }
5305 #endif /* ENABLE_DBUS */
5306
5307         if (priv->bookmarks) {
5308                 g_object_unref (priv->bookmarks);
5309                 priv->bookmarks = NULL;
5310         }
5311
5312         if (priv->metadata) {
5313                 g_object_unref (priv->metadata);
5314                 priv->metadata = NULL;
5315         }
5316
5317         if (priv->setup_document_idle > 0) {
5318                 g_source_remove (priv->setup_document_idle);
5319                 priv->setup_document_idle = 0;
5320         }
5321
5322         if (priv->monitor) {
5323                 g_object_unref (priv->monitor);
5324                 priv->monitor = NULL;
5325         }
5326         
5327         if (priv->title) {
5328                 ev_window_title_free (priv->title);
5329                 priv->title = NULL;
5330         }
5331
5332         if (priv->ui_manager) {
5333                 g_object_unref (priv->ui_manager);
5334                 priv->ui_manager = NULL;
5335         }
5336
5337         if (priv->action_group) {
5338                 g_object_unref (priv->action_group);
5339                 priv->action_group = NULL;
5340         }
5341
5342         if (priv->view_popup_action_group) {
5343                 g_object_unref (priv->view_popup_action_group);
5344                 priv->view_popup_action_group = NULL;
5345         }
5346
5347         if (priv->attachment_popup_action_group) {
5348                 g_object_unref (priv->attachment_popup_action_group);
5349                 priv->attachment_popup_action_group = NULL;
5350         }
5351
5352         if (priv->recent_action_group) {
5353                 g_object_unref (priv->recent_action_group);
5354                 priv->recent_action_group = NULL;
5355         }
5356
5357         if (priv->bookmarks_action_group) {
5358                 g_object_unref (priv->bookmarks_action_group);
5359                 priv->bookmarks_action_group = NULL;
5360         }
5361
5362         if (priv->recent_manager) {
5363                 g_signal_handlers_disconnect_by_func (priv->recent_manager,
5364                                                       ev_window_setup_recent,
5365                                                       window);
5366                 priv->recent_manager = NULL;
5367         }
5368
5369         if (priv->settings) {
5370                 g_object_unref (priv->settings);
5371                 priv->settings = NULL;
5372         }
5373
5374         if (priv->default_settings) {
5375                 g_settings_apply (priv->default_settings);
5376                 g_object_unref (priv->default_settings);
5377                 priv->default_settings = NULL;
5378         }
5379
5380         if (priv->lockdown_settings) {
5381                 g_object_unref (priv->lockdown_settings);
5382                 priv->lockdown_settings = NULL;
5383         }
5384
5385         priv->recent_ui_id = 0;
5386
5387         if (priv->model) {
5388                 g_signal_handlers_disconnect_by_func (priv->model,
5389                                                       ev_window_page_changed_cb,
5390                                                       window);
5391                 g_object_unref (priv->model);
5392                 priv->model = NULL;
5393         }
5394
5395         if (priv->document) {
5396                 g_object_unref (priv->document);
5397                 priv->document = NULL;
5398         }
5399         
5400         if (priv->view) {
5401                 g_object_unref (priv->view);
5402                 priv->view = NULL;
5403         }
5404
5405         if (priv->password_view) {
5406                 g_object_unref (priv->password_view);
5407                 priv->password_view = NULL;
5408         }
5409
5410         if (priv->load_job) {
5411                 ev_window_clear_load_job (window);
5412         }
5413
5414         if (priv->reload_job) {
5415                 ev_window_clear_reload_job (window);
5416         }
5417
5418         if (priv->save_job) {
5419                 ev_window_clear_save_job (window);
5420         }
5421
5422         if (priv->thumbnail_job) {
5423                 ev_window_clear_thumbnail_job (window);
5424         }
5425
5426         if (priv->find_job) {
5427                 ev_window_clear_find_job (window);
5428         }
5429         
5430         if (priv->local_uri) {
5431                 ev_window_clear_local_uri (window);
5432                 priv->local_uri = NULL;
5433         }
5434
5435         ev_window_clear_progress_idle (window);
5436         if (priv->progress_cancellable) {
5437                 g_object_unref (priv->progress_cancellable);
5438                 priv->progress_cancellable = NULL;
5439         }
5440         
5441         ev_window_close_dialogs (window);
5442
5443         if (priv->link) {
5444                 g_object_unref (priv->link);
5445                 priv->link = NULL;
5446         }
5447
5448         if (priv->image) {
5449                 g_object_unref (priv->image);
5450                 priv->image = NULL;
5451         }
5452
5453         if (priv->annot) {
5454                 g_object_unref (priv->annot);
5455                 priv->annot = NULL;
5456         }
5457
5458         if (priv->attach_list) {
5459                 g_list_foreach (priv->attach_list,
5460                                 (GFunc) g_object_unref,
5461                                 NULL);
5462                 g_list_free (priv->attach_list);
5463                 priv->attach_list = NULL;
5464         }
5465
5466         if (priv->find_bar) {
5467                 g_signal_handlers_disconnect_by_func
5468                         (window->priv->find_bar,
5469                          G_CALLBACK (find_bar_close_cb),
5470                          window);
5471                 priv->find_bar = NULL;
5472         }
5473
5474         if (priv->uri) {
5475                 g_free (priv->uri);
5476                 priv->uri = NULL;
5477         }
5478
5479         if (priv->search_string) {
5480                 g_free (priv->search_string);
5481                 priv->search_string = NULL;
5482         }
5483         
5484         if (priv->dest) {
5485                 g_object_unref (priv->dest);
5486                 priv->dest = NULL;
5487         }
5488
5489         if (priv->history) {
5490                 g_object_unref (priv->history);
5491                 priv->history = NULL;
5492         }
5493
5494         if (priv->print_queue) {
5495                 g_queue_free (priv->print_queue);
5496                 priv->print_queue = NULL;
5497         }
5498
5499         G_OBJECT_CLASS (ev_window_parent_class)->dispose (object);
5500 }
5501
5502 static void
5503 menubar_deactivate_cb (GtkWidget *menubar,
5504                        EvWindow  *window)
5505 {
5506         g_signal_handlers_disconnect_by_func (menubar,
5507                                               G_CALLBACK (menubar_deactivate_cb),
5508                                               window);
5509
5510         gtk_menu_shell_deselect (GTK_MENU_SHELL (menubar));
5511
5512         update_chrome_visibility (window);
5513 }
5514
5515 static gboolean
5516 ev_window_key_press_event (GtkWidget   *widget,
5517                            GdkEventKey *event)
5518 {
5519         EvWindow        *ev_window = EV_WINDOW (widget);
5520         EvWindowPrivate *priv = ev_window->priv;
5521         gboolean         handled = FALSE;
5522
5523         /* Propagate the event to the view first
5524          * It's needed to be able to type in
5525          * annot popups windows
5526          */
5527         if (priv->view) {
5528                 g_object_ref (priv->view);
5529                 if (gtk_widget_is_sensitive (priv->view))
5530                         handled = gtk_widget_event (priv->view, (GdkEvent*) event);
5531                 g_object_unref (priv->view);
5532         }
5533
5534         if (!handled && !EV_WINDOW_IS_PRESENTATION (ev_window)) {
5535                 guint modifier = event->state & gtk_accelerator_get_default_mod_mask ();
5536
5537                 if (priv->menubar_accel_keyval != 0 &&
5538                     event->keyval == priv->menubar_accel_keyval &&
5539                     modifier == priv->menubar_accel_modifier) {
5540                         if (!gtk_widget_get_visible (priv->menubar)) {
5541                                 g_signal_connect (priv->menubar, "deactivate",
5542                                                   G_CALLBACK (menubar_deactivate_cb),
5543                                                   ev_window);
5544
5545                                 gtk_widget_show (priv->menubar);
5546                                 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menubar),
5547                                                              FALSE);
5548
5549                                 handled = TRUE;
5550                         }
5551                 }
5552         }
5553
5554         if (!handled)
5555                 handled = GTK_WIDGET_CLASS (ev_window_parent_class)->key_press_event (widget, event);
5556
5557         return handled;
5558 }
5559
5560 static gboolean
5561 ev_window_delete_event (GtkWidget   *widget,
5562                         GdkEventAny *event)
5563 {
5564         return !ev_window_close (EV_WINDOW (widget));
5565 }
5566
5567 static void
5568 ev_window_class_init (EvWindowClass *ev_window_class)
5569 {
5570         GObjectClass *g_object_class = G_OBJECT_CLASS (ev_window_class);
5571         GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (ev_window_class);
5572
5573         g_object_class->dispose = ev_window_dispose;
5574         g_object_class->finalize = ev_window_finalize;
5575
5576         widget_class->delete_event = ev_window_delete_event;
5577         widget_class->key_press_event = ev_window_key_press_event;
5578         widget_class->screen_changed = ev_window_screen_changed;
5579         widget_class->window_state_event = ev_window_state_event;
5580         widget_class->drag_data_received = ev_window_drag_data_received;
5581
5582         g_type_class_add_private (g_object_class, sizeof (EvWindowPrivate));
5583 }
5584
5585 /* Normal items */
5586 static const GtkActionEntry entries[] = {
5587         { "File", NULL, N_("_File") },
5588         { "Edit", NULL, N_("_Edit") },
5589         { "View", NULL, N_("_View") },
5590         { "Go", NULL, N_("_Go") },
5591         { "Bookmarks", NULL, N_("_Bookmarks") },
5592         { "Help", NULL, N_("_Help") },
5593
5594         /* File menu */
5595         { "FileOpen", GTK_STOCK_OPEN, N_("_Open…"), "<control>O",
5596           N_("Open an existing document"),
5597           G_CALLBACK (ev_window_cmd_file_open) },
5598         { "FileOpenCopy", NULL, N_("Op_en a Copy"), "<control>N",
5599           N_("Open a copy of the current document in a new window"),
5600           G_CALLBACK (ev_window_cmd_file_open_copy) },
5601         { "FileSaveAs", GTK_STOCK_SAVE_AS, N_("_Save a Copy…"), "<control>S",
5602           N_("Save a copy of the current document"),
5603           G_CALLBACK (ev_window_cmd_save_as) },
5604         { "FileOpenContainingFolder", GTK_STOCK_DIRECTORY, N_("Open Containing _Folder"), NULL,
5605           N_("Show the folder which contains this file in the file manager"),
5606           G_CALLBACK (ev_window_cmd_open_containing_folder) },
5607         { "FilePrint", GTK_STOCK_PRINT, N_("_Print…"), "<control>P",
5608           N_("Print this document"),
5609           G_CALLBACK (ev_window_cmd_file_print) },
5610         { "FileProperties", GTK_STOCK_PROPERTIES, N_("P_roperties"), "<alt>Return", NULL,
5611           G_CALLBACK (ev_window_cmd_file_properties) },                       
5612         { "FileCloseWindow", GTK_STOCK_CLOSE, NULL, "<control>W", NULL,
5613           G_CALLBACK (ev_window_cmd_file_close_window) },
5614
5615         /* Edit menu */
5616         { "EditCopy", GTK_STOCK_COPY, NULL, "<control>C", NULL,
5617           G_CALLBACK (ev_window_cmd_edit_copy) },
5618         { "EditSelectAll", GTK_STOCK_SELECT_ALL, N_("Select _All"), "<control>A", NULL,
5619           G_CALLBACK (ev_window_cmd_edit_select_all) },
5620         { "EditFind", GTK_STOCK_FIND, N_("_Find…"), "<control>F",
5621           N_("Find a word or phrase in the document"),
5622           G_CALLBACK (ev_window_cmd_edit_find) },
5623         { "EditFindNext", NULL, N_("Find Ne_xt"), "<control>G", NULL,
5624           G_CALLBACK (ev_window_cmd_edit_find_next) },
5625         { "EditFindPrevious", NULL, N_("Find Pre_vious"), "<shift><control>G", NULL,
5626           G_CALLBACK (ev_window_cmd_edit_find_previous) },
5627         { "EditToolbar", NULL, N_("T_oolbar"), NULL, NULL,
5628           G_CALLBACK (ev_window_cmd_edit_toolbar) },
5629         { "EditRotateLeft", EV_STOCK_ROTATE_LEFT, N_("Rotate _Left"), "<control>Left", NULL,
5630           G_CALLBACK (ev_window_cmd_edit_rotate_left) },
5631         { "EditRotateRight", EV_STOCK_ROTATE_RIGHT, N_("Rotate _Right"), "<control>Right", NULL,
5632           G_CALLBACK (ev_window_cmd_edit_rotate_right) },
5633         { "EditSaveSettings", NULL, N_("Save Current Settings as _Default"), "<control>T", NULL,
5634           G_CALLBACK (ev_window_cmd_edit_save_settings) },
5635
5636
5637         /* View menu */
5638         { "ViewZoomIn", GTK_STOCK_ZOOM_IN, NULL, "<control>plus",
5639           N_("Enlarge the document"),
5640           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5641         { "ViewZoomOut", GTK_STOCK_ZOOM_OUT, NULL, "<control>minus",
5642           N_("Shrink the document"),
5643           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5644         { "ViewReload", GTK_STOCK_REFRESH, N_("_Reload"), "<control>R",
5645           N_("Reload the document"),
5646           G_CALLBACK (ev_window_cmd_view_reload) },
5647
5648         { "ViewAutoscroll", GTK_STOCK_MEDIA_PLAY, N_("Auto_scroll"), NULL, NULL,
5649           G_CALLBACK (ev_window_cmd_view_autoscroll) },
5650
5651         /* Go menu */
5652         { "GoPreviousPage", GTK_STOCK_GO_UP, N_("_Previous Page"), "<control>Page_Up",
5653           N_("Go to the previous page"),
5654           G_CALLBACK (ev_window_cmd_go_previous_page) },
5655         { "GoNextPage", GTK_STOCK_GO_DOWN, N_("_Next Page"), "<control>Page_Down",
5656           N_("Go to the next page"),
5657           G_CALLBACK (ev_window_cmd_go_next_page) },
5658         { "GoFirstPage", GTK_STOCK_GOTO_TOP, N_("_First Page"), "<control>Home",
5659           N_("Go to the first page"),
5660           G_CALLBACK (ev_window_cmd_go_first_page) },
5661         { "GoLastPage", GTK_STOCK_GOTO_BOTTOM, N_("_Last Page"), "<control>End",
5662           N_("Go to the last page"),
5663           G_CALLBACK (ev_window_cmd_go_last_page) },
5664
5665         /* Bookmarks menu */
5666         { "BookmarksAdd", GTK_STOCK_ADD, N_("_Add Bookmark"), "<control>D",
5667           N_("Add a bookmark for the current page"),
5668           G_CALLBACK (ev_window_cmd_bookmarks_add) },
5669
5670         /* Help menu */
5671         { "HelpContents", GTK_STOCK_HELP, N_("_Contents"), "F1", NULL,
5672           G_CALLBACK (ev_window_cmd_help_contents) },
5673
5674         { "HelpAbout", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL,
5675           G_CALLBACK (ev_window_cmd_help_about) },
5676
5677         /* Toolbar-only */
5678         { "LeaveFullscreen", GTK_STOCK_LEAVE_FULLSCREEN, N_("Leave Fullscreen"), NULL,
5679           N_("Leave fullscreen mode"),
5680           G_CALLBACK (ev_window_cmd_leave_fullscreen) },
5681         { "StartPresentation", EV_STOCK_RUN_PRESENTATION, N_("Start Presentation"), NULL,
5682           N_("Start a presentation"),
5683           G_CALLBACK (ev_window_cmd_start_presentation) },
5684
5685         /* Accellerators */
5686         { "Escape", NULL, "", "Escape", "",
5687           G_CALLBACK (ev_window_cmd_escape) },
5688         { "Slash", GTK_STOCK_FIND, NULL, "slash", NULL,
5689           G_CALLBACK (ev_window_cmd_edit_find) },
5690         { "F3", NULL, "", "F3", NULL,
5691           G_CALLBACK (ev_window_cmd_edit_find_next) },
5692         { "PageDown", NULL, "", "Page_Down", NULL,
5693           G_CALLBACK (ev_window_cmd_scroll_forward) },
5694         { "PageUp", NULL, "", "Page_Up", NULL,
5695           G_CALLBACK (ev_window_cmd_scroll_backward) },
5696         { "Space", NULL, "", "space", NULL,
5697           G_CALLBACK (ev_window_cmd_scroll_forward) },
5698         { "ShiftSpace", NULL, "", "<shift>space", NULL,
5699           G_CALLBACK (ev_window_cmd_scroll_backward) },
5700         { "BackSpace", NULL, "", "BackSpace", NULL,
5701           G_CALLBACK (ev_window_cmd_scroll_backward) },
5702         { "ShiftBackSpace", NULL, "", "<shift>BackSpace", NULL,
5703           G_CALLBACK (ev_window_cmd_scroll_forward) },
5704         { "Return", NULL, "", "Return", NULL,
5705           G_CALLBACK (ev_window_cmd_scroll_forward) },
5706         { "ShiftReturn", NULL, "", "<shift>Return", NULL,
5707           G_CALLBACK (ev_window_cmd_scroll_backward) },
5708         { "p", GTK_STOCK_GO_UP, "", "p", NULL,
5709           G_CALLBACK (ev_window_cmd_go_previous_page) },
5710         { "n", GTK_STOCK_GO_DOWN, "", "n", NULL,
5711           G_CALLBACK (ev_window_cmd_go_next_page) },
5712         { "Plus", GTK_STOCK_ZOOM_IN, NULL, "plus", NULL,
5713           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5714         { "CtrlEqual", GTK_STOCK_ZOOM_IN, NULL, "<control>equal", NULL,
5715           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5716         { "Equal", GTK_STOCK_ZOOM_IN, NULL, "equal", NULL,
5717           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5718         { "Minus", GTK_STOCK_ZOOM_OUT, NULL, "minus", NULL,
5719           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5720         { "FocusPageSelector", NULL, "", "<control>l", NULL,
5721           G_CALLBACK (ev_window_cmd_focus_page_selector) },
5722         { "GoBackwardFast", NULL, "", "<shift>Page_Up", NULL,
5723           G_CALLBACK (ev_window_cmd_go_backward) },
5724         { "GoForwardFast", NULL, "", "<shift>Page_Down", NULL,
5725           G_CALLBACK (ev_window_cmd_go_forward) },
5726         { "KpPlus", GTK_STOCK_ZOOM_IN, NULL, "KP_Add", NULL,
5727           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5728         { "KpMinus", GTK_STOCK_ZOOM_OUT, NULL, "KP_Subtract", NULL,
5729           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5730         { "CtrlKpPlus", GTK_STOCK_ZOOM_IN, NULL, "<control>KP_Add", NULL,
5731           G_CALLBACK (ev_window_cmd_view_zoom_in) },
5732         { "CtrlKpMinus", GTK_STOCK_ZOOM_OUT, NULL, "<control>KP_Subtract", NULL,
5733           G_CALLBACK (ev_window_cmd_view_zoom_out) },
5734         { "CtrlInsert", GTK_STOCK_COPY, NULL, "<control>Insert", NULL,
5735           G_CALLBACK (ev_window_cmd_edit_copy) },
5736 };
5737
5738 /* Toggle items */
5739 static const GtkToggleActionEntry toggle_entries[] = {
5740         /* View Menu */
5741         { "ViewToolbar", NULL, N_("_Toolbar"), NULL,
5742           N_("Show or hide the toolbar"),
5743           G_CALLBACK (ev_window_view_toolbar_cb), TRUE },
5744         { "ViewSidebar", GTK_STOCK_INDEX, N_("Side _Pane"), "F9",
5745           N_("Show or hide the side pane"),
5746           G_CALLBACK (ev_window_view_sidebar_cb), TRUE },
5747         { "ViewContinuous", EV_STOCK_VIEW_CONTINUOUS, N_("_Continuous"), NULL,
5748           N_("Show the entire document"),
5749           G_CALLBACK (ev_window_cmd_continuous), TRUE },
5750         { "ViewDual", EV_STOCK_VIEW_DUAL, N_("_Dual"), NULL,
5751           N_("Show two pages at once"),
5752           G_CALLBACK (ev_window_cmd_dual), FALSE },
5753         { "ViewFullscreen", GTK_STOCK_FULLSCREEN, N_("_Fullscreen"), "F11",
5754           N_("Expand the window to fill the screen"),
5755           G_CALLBACK (ev_window_cmd_view_fullscreen) },
5756         { "ViewPresentation", EV_STOCK_RUN_PRESENTATION, N_("Pre_sentation"), "F5",
5757           N_("Run document as a presentation"),
5758           G_CALLBACK (ev_window_cmd_view_presentation) },
5759         { "ViewBestFit", EV_STOCK_ZOOM_PAGE, N_("_Best Fit"), NULL,
5760           N_("Make the current document fill the window"),
5761           G_CALLBACK (ev_window_cmd_view_best_fit) },
5762         { "ViewPageWidth", EV_STOCK_ZOOM_WIDTH, N_("Fit Page _Width"), NULL,
5763           N_("Make the current document fill the window width"),
5764           G_CALLBACK (ev_window_cmd_view_page_width) },
5765         { "ViewInvertedColors", EV_STOCK_INVERTED_COLORS, N_("_Inverted Colors"), "<control>I",
5766           N_("Show page contents with the colors inverted"),
5767           G_CALLBACK (ev_window_cmd_view_inverted_colors) },
5768
5769 };
5770
5771 /* Popups specific items */
5772 static const GtkActionEntry view_popup_entries [] = {
5773         /* Links */
5774         { "OpenLink", GTK_STOCK_OPEN, N_("_Open Link"), NULL,
5775           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
5776         { "GoLink", GTK_STOCK_GO_FORWARD, N_("_Go To"), NULL,
5777           NULL, G_CALLBACK (ev_view_popup_cmd_open_link) },
5778         { "OpenLinkNewWindow", NULL, N_("Open in New _Window"), NULL,
5779           NULL, G_CALLBACK (ev_view_popup_cmd_open_link_new_window) },
5780         { "CopyLinkAddress", NULL, N_("_Copy Link Address"), NULL,
5781           NULL, G_CALLBACK (ev_view_popup_cmd_copy_link_address) },
5782         { "SaveImageAs", NULL, N_("_Save Image As…"), NULL,
5783           NULL, G_CALLBACK (ev_view_popup_cmd_save_image_as) },
5784         { "CopyImage", NULL, N_("Copy _Image"), NULL,
5785           NULL, G_CALLBACK (ev_view_popup_cmd_copy_image) },
5786         { "AnnotProperties", NULL, N_("Annotation Properties…"), NULL,
5787           NULL, G_CALLBACK (ev_view_popup_cmd_annot_properties) }
5788 };
5789
5790 static const GtkActionEntry attachment_popup_entries [] = {
5791         { "OpenAttachment", GTK_STOCK_OPEN, N_("_Open Attachment"), NULL,
5792           NULL, G_CALLBACK (ev_attachment_popup_cmd_open_attachment) },
5793         { "SaveAttachmentAs", GTK_STOCK_SAVE_AS, N_("_Save Attachment As…"), NULL,
5794           NULL, G_CALLBACK (ev_attachment_popup_cmd_save_attachment_as) },
5795 };
5796
5797 static void
5798 sidebar_links_link_activated_cb (EvSidebarLinks *sidebar_links, EvLink *link, EvWindow *window)
5799 {
5800         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5801 }
5802
5803 static void
5804 activate_link_cb (EvPageAction *page_action, EvLink *link, EvWindow *window)
5805 {
5806         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5807         gtk_widget_grab_focus (window->priv->view);
5808 }
5809
5810 static void
5811 navigation_action_activate_link_cb (EvNavigationAction *action, EvLink *link, EvWindow *window)
5812 {
5813         
5814         ev_view_handle_link (EV_VIEW (window->priv->view), link);
5815         gtk_widget_grab_focus (window->priv->view);
5816 }
5817
5818 static void
5819 sidebar_layers_visibility_changed (EvSidebarLayers *layers,
5820                                    EvWindow        *window)
5821 {
5822         ev_view_reload (EV_VIEW (window->priv->view));
5823 }
5824
5825 static void
5826 sidebar_annots_annot_activated_cb (EvSidebarAnnotations *sidebar_annots,
5827                                    EvMapping            *annot_mapping,
5828                                    EvWindow             *window)
5829 {
5830         ev_view_focus_annotation (EV_VIEW (window->priv->view), annot_mapping);
5831 }
5832
5833 static void
5834 sidebar_annots_begin_annot_add (EvSidebarAnnotations *sidebar_annots,
5835                                 EvAnnotationType      annot_type,
5836                                 EvWindow             *window)
5837 {
5838         ev_view_begin_add_annotation (EV_VIEW (window->priv->view), annot_type);
5839 }
5840
5841 static void
5842 view_annot_added (EvView       *view,
5843                   EvAnnotation *annot,
5844                   EvWindow     *window)
5845 {
5846         ev_sidebar_annotations_annot_added (EV_SIDEBAR_ANNOTATIONS (window->priv->sidebar_annots),
5847                                             annot);
5848 }
5849
5850 static void
5851 sidebar_annots_annot_add_cancelled (EvSidebarAnnotations *sidebar_annots,
5852                                     EvWindow             *window)
5853 {
5854         ev_view_cancel_add_annotation (EV_VIEW (window->priv->view));
5855 }
5856
5857 static void
5858 sidebar_bookmarks_add_bookmark (EvSidebarBookmarks *sidebar_bookmarks,
5859                                 EvWindow           *window)
5860 {
5861         ev_window_cmd_bookmarks_add (NULL, window);
5862 }
5863
5864 static void
5865 register_custom_actions (EvWindow *window, GtkActionGroup *group)
5866 {
5867         GtkAction *action;
5868
5869         action = g_object_new (EV_TYPE_PAGE_ACTION,
5870                                "name", PAGE_SELECTOR_ACTION,
5871                                "label", _("Page"),
5872                                "tooltip", _("Select Page"),
5873                                "icon_name", "text-x-generic",
5874                                "visible_overflown", FALSE,
5875                                NULL);
5876         ev_page_action_set_model (EV_PAGE_ACTION (action),
5877                                   window->priv->model);
5878         g_signal_connect (action, "activate_link",
5879                           G_CALLBACK (activate_link_cb), window);
5880         gtk_action_group_add_action (group, action);
5881         g_object_unref (action);
5882
5883         action = g_object_new (EPHY_TYPE_ZOOM_ACTION,
5884                                "name", ZOOM_CONTROL_ACTION,
5885                                "label", _("Zoom"),
5886                                "stock_id", EV_STOCK_ZOOM,
5887                                "tooltip", _("Adjust the zoom level"),
5888                                "zoom", 1.0,
5889                                NULL);
5890         g_signal_connect (action, "zoom_to_level",
5891                           G_CALLBACK (zoom_control_changed_cb), window);
5892         gtk_action_group_add_action (group, action);
5893         g_object_unref (action);
5894
5895         action = g_object_new (EV_TYPE_NAVIGATION_ACTION,
5896                                "name", NAVIGATION_ACTION,
5897                                "label", _("Navigation"),
5898                                "is_important", TRUE,
5899                                "short_label", _("Back"),
5900                                "stock_id", GTK_STOCK_GO_DOWN,
5901                                /*translators: this is the history action*/
5902                                "tooltip", _("Move across visited pages"),
5903                                NULL);
5904         g_signal_connect (action, "activate_link",
5905                           G_CALLBACK (navigation_action_activate_link_cb), window);
5906         gtk_action_group_add_action (group, action);
5907         g_object_unref (action);
5908
5909         action = g_object_new (EV_TYPE_OPEN_RECENT_ACTION,
5910                                "name", "FileOpenRecent",
5911                                "label", _("_Open…"),
5912                                "tooltip", _("Open an existing document"),
5913                                "stock_id", GTK_STOCK_OPEN,
5914                                NULL);
5915         g_signal_connect (action, "activate",
5916                           G_CALLBACK (ev_window_cmd_file_open), window);
5917         g_signal_connect (action, "item_activated",
5918                           G_CALLBACK (ev_window_open_recent_action_item_activated),
5919                           window);
5920         gtk_action_group_add_action (group, action);
5921         g_object_unref (action);
5922 }
5923
5924 static void
5925 set_action_properties (GtkActionGroup *action_group)
5926 {
5927         GtkAction *action;
5928
5929         action = gtk_action_group_get_action (action_group, "FileOpenContainingFolder");
5930         /*translators: this is the label for toolbar button*/
5931         g_object_set (action, "short_label", _("Open Folder"), NULL);
5932
5933         action = gtk_action_group_get_action (action_group, "GoPreviousPage");
5934         g_object_set (action, "is-important", TRUE, NULL);
5935         /*translators: this is the label for toolbar button*/
5936         g_object_set (action, "short_label", _("Previous"), NULL);
5937
5938         action = gtk_action_group_get_action (action_group, "GoNextPage");
5939         g_object_set (action, "is-important", TRUE, NULL);
5940         /*translators: this is the label for toolbar button*/
5941         g_object_set (action, "short_label", _("Next"), NULL);
5942
5943         action = gtk_action_group_get_action (action_group, "ViewZoomIn");
5944         /*translators: this is the label for toolbar button*/
5945         g_object_set (action, "short_label", _("Zoom In"), NULL);
5946
5947         action = gtk_action_group_get_action (action_group, "ViewZoomOut");
5948         /*translators: this is the label for toolbar button*/
5949         g_object_set (action, "short_label", _("Zoom Out"), NULL);
5950
5951         action = gtk_action_group_get_action (action_group, "ViewBestFit");
5952         /*translators: this is the label for toolbar button*/
5953         g_object_set (action, "short_label", _("Best Fit"), NULL);
5954
5955         action = gtk_action_group_get_action (action_group, "ViewPageWidth");
5956         /*translators: this is the label for toolbar button*/
5957         g_object_set (action, "short_label", _("Fit Width"), NULL);
5958
5959         action = gtk_action_group_get_action (action_group, "LeaveFullscreen");
5960         g_object_set (action, "is-important", TRUE, NULL);
5961 }
5962
5963 static void
5964 sidebar_widget_model_set (EvSidebarLinks *ev_sidebar_links,
5965                           GParamSpec     *pspec,
5966                           EvWindow       *ev_window)
5967 {
5968         GtkTreeModel *model;
5969         GtkAction *action;
5970
5971         g_object_get (G_OBJECT (ev_sidebar_links),
5972                       "model", &model,
5973                       NULL);
5974
5975         action = gtk_action_group_get_action (ev_window->priv->action_group, PAGE_SELECTOR_ACTION);
5976         ev_page_action_set_links_model (EV_PAGE_ACTION (action), model);
5977         g_object_unref (model);
5978 }
5979
5980 static gboolean
5981 view_actions_focus_in_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
5982 {
5983 #ifdef ENABLE_DBUS
5984         GObject *keys;
5985
5986         keys = ev_application_get_media_keys (EV_APP);
5987         ev_media_player_keys_focused (EV_MEDIA_PLAYER_KEYS (keys));
5988 #endif /* ENABLE_DBUS */
5989
5990         update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, FALSE);
5991         ev_window_set_action_sensitive (window, "ViewToolbar", TRUE);
5992
5993         ev_window_set_view_accels_sensitivity (window, TRUE);
5994
5995         update_chrome_visibility (window);
5996
5997         return FALSE;
5998 }
5999
6000 static gboolean
6001 view_actions_focus_out_cb (GtkWidget *widget, GdkEventFocus *event, EvWindow *window)
6002 {
6003         ev_window_set_view_accels_sensitivity (window, FALSE);
6004
6005         return FALSE;
6006 }
6007
6008 static void
6009 sidebar_page_main_widget_update_cb (GObject *ev_sidebar_page,
6010                                     GParamSpec         *pspec,
6011                                     EvWindow           *ev_window)
6012 {
6013         GtkWidget *widget;
6014         
6015         g_object_get (ev_sidebar_page, "main_widget", &widget, NULL);
6016
6017         if (widget != NULL) {           
6018                 g_signal_connect_object (widget, "focus_in_event",
6019                                          G_CALLBACK (view_actions_focus_in_cb),
6020                                          ev_window, 0);
6021                 g_signal_connect_object (widget, "focus_out_event",
6022                                          G_CALLBACK (view_actions_focus_out_cb),
6023                                          ev_window, 0);
6024                 g_object_unref (widget);
6025         }
6026 }
6027
6028 static gboolean
6029 window_state_event_cb (EvWindow *window, GdkEventWindowState *event, gpointer dummy)
6030 {
6031         if (!(event->new_window_state & GDK_WINDOW_STATE_FULLSCREEN)) {
6032                 gboolean maximized;
6033
6034                 maximized = event->new_window_state & GDK_WINDOW_STATE_MAXIMIZED;
6035                 if (window->priv->metadata && !ev_window_is_empty (window))
6036                         ev_metadata_set_boolean (window->priv->metadata, "window_maximized", maximized);
6037         }
6038
6039         return FALSE;
6040 }
6041
6042 static gboolean
6043 window_configure_event_cb (EvWindow *window, GdkEventConfigure *event, gpointer dummy)
6044 {
6045         GdkWindowState state;
6046         gdouble document_width, document_height;
6047
6048         if (!window->priv->metadata)
6049                 return FALSE;
6050
6051         state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (window)));
6052
6053         if (!(state & GDK_WINDOW_STATE_FULLSCREEN)) {
6054                 if (window->priv->document) {
6055                         ev_document_get_max_page_size (window->priv->document,
6056                                                        &document_width, &document_height);
6057                         g_settings_set (window->priv->default_settings, "window-ratio", "(dd)",
6058                                         (double)event->width / document_width,
6059                                         (double)event->height / document_height);
6060
6061                         ev_metadata_set_int (window->priv->metadata, "window_x", event->x);
6062                         ev_metadata_set_int (window->priv->metadata, "window_y", event->y);
6063                         ev_metadata_set_int (window->priv->metadata, "window_width", event->width);
6064                         ev_metadata_set_int (window->priv->metadata, "window_height", event->height);
6065                 }
6066         }
6067
6068         return FALSE;
6069 }
6070
6071 static void
6072 launch_action (EvWindow *window, EvLinkAction *action)
6073 {
6074         const char *filename = ev_link_action_get_filename (action);
6075         GAppInfo *app_info;
6076         GFile *file;
6077         GList file_list = {NULL};
6078         GAppLaunchContext *context;
6079         GError *error = NULL;
6080
6081         if (filename == NULL)
6082                 return;
6083
6084         if (g_path_is_absolute (filename)) {
6085                 file = g_file_new_for_path (filename);
6086         } else {
6087                 GFile *base_file;
6088                 gchar *dir;
6089
6090                 dir = g_path_get_dirname (window->priv->uri);
6091                 base_file = g_file_new_for_uri (dir);
6092                 g_free (dir);
6093                 
6094                 file = g_file_resolve_relative_path (base_file, filename);
6095                 g_object_unref (base_file);
6096         }
6097
6098         app_info = g_file_query_default_handler (file, NULL, &error);
6099         if (!app_info) {
6100                 ev_window_error_message (window, error,
6101                                          "%s",
6102                                          _("Unable to launch external application."));
6103                 g_object_unref (file);
6104                 g_error_free (error);
6105
6106                 return;
6107         }
6108
6109         context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
6110         gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context),
6111                                            gtk_window_get_screen (GTK_WINDOW (window)));
6112         gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context),
6113                                               gtk_get_current_event_time ());
6114         
6115         file_list.data = file;
6116         if (!g_app_info_launch (app_info, &file_list, context, &error)) {
6117                 ev_window_error_message (window, error,
6118                                          "%s",
6119                                          _("Unable to launch external application."));
6120                 g_error_free (error);
6121         }
6122         
6123         g_object_unref (app_info);
6124         g_object_unref (file);
6125         /* FIXMEchpe: unref launch context? */
6126
6127         /* According to the PDF spec filename can be an executable. I'm not sure
6128            allowing to launch executables is a good idea though. -- marco */
6129 }
6130
6131 static void
6132 launch_external_uri (EvWindow *window, EvLinkAction *action)
6133 {
6134         const gchar *uri = ev_link_action_get_uri (action);
6135         GError *error = NULL;
6136         gboolean ret;
6137         GAppLaunchContext *context;
6138
6139         context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
6140         gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context),
6141                                            gtk_window_get_screen (GTK_WINDOW (window)));
6142         gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context),
6143                                               gtk_get_current_event_time ());
6144
6145         if (!g_strstr_len (uri, strlen (uri), "://") &&
6146             !g_str_has_prefix (uri, "mailto:")) {
6147                 gchar *new_uri;
6148
6149                 /* Not a valid uri, assume http if it starts with www */
6150                 if (g_str_has_prefix (uri, "www.")) {
6151                         new_uri = g_strdup_printf ("http://%s", uri);
6152                 } else {
6153                         GFile *file, *parent;
6154
6155                         file = g_file_new_for_uri (window->priv->uri);
6156                         parent = g_file_get_parent (file);
6157                         g_object_unref (file);
6158                         if (parent) {
6159                                 gchar *parent_uri = g_file_get_uri (parent);
6160
6161                                 new_uri = g_build_filename (parent_uri, uri, NULL);
6162                                 g_free (parent_uri);
6163                                 g_object_unref (parent);
6164                         } else {
6165                                 new_uri = g_strdup_printf ("file:///%s", uri);
6166                         }
6167                 }
6168                 ret = g_app_info_launch_default_for_uri (new_uri, context, &error);
6169                 g_free (new_uri);
6170         } else {
6171                 ret = g_app_info_launch_default_for_uri (uri, context, &error);
6172         }
6173
6174         if (ret == FALSE) {
6175                 ev_window_error_message (window, error,
6176                                          "%s", _("Unable to open external link"));
6177                 g_error_free (error);
6178         }
6179
6180         /* FIXMEchpe: unref launch context? */
6181 }
6182
6183 static void
6184 open_remote_link (EvWindow *window, EvLinkAction *action)
6185 {
6186         gchar *uri;
6187         gchar *dir;
6188
6189         dir = g_path_get_dirname (window->priv->uri);
6190         
6191         uri = g_build_filename (dir, ev_link_action_get_filename (action),
6192                                 NULL);
6193         g_free (dir);
6194
6195         ev_application_open_uri_at_dest (EV_APP, uri,
6196                                          gtk_window_get_screen (GTK_WINDOW (window)),
6197                                          ev_link_action_get_dest (action),
6198                                          0,
6199                                          NULL, 
6200                                          gtk_get_current_event_time ());
6201
6202         g_free (uri);
6203 }
6204
6205 static void
6206 do_action_named (EvWindow *window, EvLinkAction *action)
6207 {
6208         const gchar *name = ev_link_action_get_name (action);
6209
6210         if (g_ascii_strcasecmp (name, "FirstPage") == 0) {
6211                 ev_window_cmd_go_first_page (NULL, window);
6212         } else if (g_ascii_strcasecmp (name, "PrevPage") == 0) {
6213                 ev_window_cmd_go_previous_page (NULL, window);
6214         } else if (g_ascii_strcasecmp (name, "NextPage") == 0) {
6215                 ev_window_cmd_go_next_page (NULL, window);
6216         } else if (g_ascii_strcasecmp (name, "LastPage") == 0) {
6217                 ev_window_cmd_go_last_page (NULL, window);
6218         } else if (g_ascii_strcasecmp (name, "GoToPage") == 0) {
6219                 ev_window_cmd_focus_page_selector (NULL, window);
6220         } else if (g_ascii_strcasecmp (name, "Find") == 0) {
6221                 ev_window_cmd_edit_find (NULL, window);
6222         } else if (g_ascii_strcasecmp (name, "Close") == 0) {
6223                 ev_window_cmd_file_close_window (NULL, window);
6224         } else if (g_ascii_strcasecmp (name, "Print") == 0) {
6225                 ev_window_cmd_file_print (NULL, window);
6226         } else {
6227                 g_warning ("Unimplemented named action: %s, please post a "
6228                            "bug report in Evince bugzilla "
6229                            "(http://bugzilla.gnome.org) with a testcase.",
6230                            name);
6231         }
6232 }
6233
6234 static void
6235 view_external_link_cb (EvView *view, EvLinkAction *action, EvWindow *window)
6236 {
6237         switch (ev_link_action_get_action_type (action)) {
6238                 case EV_LINK_ACTION_TYPE_GOTO_DEST: {
6239                         EvLinkDest *dest;
6240                         
6241                         dest = ev_link_action_get_dest (action);
6242                         if (!dest)
6243                                 return;
6244
6245                         ev_window_open_copy_at_dest (window, dest);
6246                 }
6247                         break;
6248                 case EV_LINK_ACTION_TYPE_EXTERNAL_URI:
6249                         launch_external_uri (window, action);
6250                         break;
6251                 case EV_LINK_ACTION_TYPE_LAUNCH:
6252                         launch_action (window, action);
6253                         break;
6254                 case EV_LINK_ACTION_TYPE_GOTO_REMOTE:
6255                         open_remote_link (window, action);
6256                         break;
6257                 case EV_LINK_ACTION_TYPE_NAMED:
6258                         do_action_named (window, action);
6259                         break;
6260                 default:
6261                         g_assert_not_reached ();
6262         }
6263 }
6264
6265 static void
6266 ev_view_popup_cmd_open_link (GtkAction *action, EvWindow *window)
6267 {
6268         ev_view_handle_link (EV_VIEW (window->priv->view), window->priv->link);
6269 }
6270
6271 static void
6272 ev_view_popup_cmd_open_link_new_window (GtkAction *action, EvWindow *window)
6273 {
6274         EvLinkAction *ev_action = NULL;
6275         EvLinkDest   *dest;
6276
6277         ev_action = ev_link_get_action (window->priv->link);
6278         if (!ev_action)
6279                 return;
6280
6281         dest = ev_link_action_get_dest (ev_action);
6282         if (!dest)
6283                 return;
6284
6285         ev_window_open_copy_at_dest (window, dest);
6286 }
6287
6288 static void
6289 ev_view_popup_cmd_copy_link_address (GtkAction *action, EvWindow *window)
6290 {
6291         EvLinkAction *ev_action;
6292
6293         ev_action = ev_link_get_action (window->priv->link);
6294         if (!ev_action)
6295                 return;
6296
6297         ev_view_copy_link_address (EV_VIEW (window->priv->view),
6298                                    ev_action);
6299 }
6300
6301
6302 static void
6303 image_save_dialog_response_cb (GtkWidget *fc,
6304                                gint       response_id,
6305                                EvWindow  *ev_window)
6306 {
6307         GFile           *target_file;
6308         gboolean         is_native;
6309         GError          *error = NULL;
6310         GdkPixbuf       *pixbuf;
6311         gchar           *uri;
6312         gchar          **extensions;
6313         gchar           *filename;
6314         gchar           *file_format;
6315         GdkPixbufFormat *format;
6316         GtkFileFilter   *filter;
6317         
6318         if (response_id != GTK_RESPONSE_OK) {
6319                 gtk_widget_destroy (fc);
6320                 return;
6321         }
6322
6323         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
6324         filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (fc));
6325         format = g_object_get_data (G_OBJECT (filter), "pixbuf-format");
6326         
6327         if (format == NULL) {
6328                 format = get_gdk_pixbuf_format_by_extension (uri);
6329         }
6330
6331         if (format == NULL && g_strrstr (uri, ".") == NULL) {
6332                 /* no extension found and no extension provided within uri */
6333                 format = get_gdk_pixbuf_format_by_extension (".png");
6334                 if (format == NULL) {
6335                         /* no .png support, try .jpeg */
6336                         format = get_gdk_pixbuf_format_by_extension (".jpeg");
6337                 }
6338         }
6339
6340         if (format == NULL) {
6341                 ev_window_error_message (ev_window, NULL, 
6342                                          "%s",
6343                                          _("Couldn't find appropriate format to save image"));
6344                 g_free (uri);
6345                 gtk_widget_destroy (fc);
6346
6347                 return;
6348         }
6349
6350         extensions = gdk_pixbuf_format_get_extensions (format);
6351         if (!g_str_has_suffix (uri, extensions[0])) {
6352                 gchar *uri_extension;
6353                 
6354                 uri_extension = g_strconcat (uri, ".", extensions[0], NULL);
6355                 target_file = g_file_new_for_uri (uri_extension);
6356                 g_free (uri_extension);
6357         } else {
6358                 target_file = g_file_new_for_uri (uri);
6359         }
6360         g_strfreev (extensions);
6361         g_free (uri);
6362         
6363         is_native = g_file_is_native (target_file);
6364         if (is_native) {
6365                 filename = g_file_get_path (target_file);
6366         } else {
6367                 /* Create a temporary local file to save to */
6368                 if (ev_mkstemp ("saveimage.XXXXXX", &filename, &error) == -1)
6369                         goto has_error;
6370         }
6371
6372         ev_document_doc_mutex_lock ();
6373         pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (ev_window->priv->document),
6374                                                ev_window->priv->image);
6375         ev_document_doc_mutex_unlock ();
6376
6377         file_format = gdk_pixbuf_format_get_name (format);
6378         gdk_pixbuf_save (pixbuf, filename, file_format, &error, NULL);
6379         g_free (file_format);
6380         g_object_unref (pixbuf);
6381         
6382     has_error:
6383         if (error) {
6384                 ev_window_error_message (ev_window, error, 
6385                                          "%s", _("The image could not be saved."));
6386                 g_error_free (error);
6387                 g_free (filename);
6388                 g_object_unref (target_file);
6389                 gtk_widget_destroy (fc);
6390
6391                 return;
6392         }
6393
6394         if (!is_native) {
6395                 GFile *source_file;
6396                 
6397                 source_file = g_file_new_for_path (filename);
6398                 
6399                 ev_window_save_remote (ev_window, EV_SAVE_IMAGE,
6400                                        source_file, target_file);
6401                 g_object_unref (source_file);
6402         }
6403         
6404         g_free (filename);
6405         g_object_unref (target_file);
6406         gtk_widget_destroy (fc);
6407 }
6408
6409 static void
6410 ev_view_popup_cmd_save_image_as (GtkAction *action, EvWindow *window)
6411 {
6412         GtkWidget *fc;
6413
6414         if (!window->priv->image)
6415                 return;
6416
6417         fc = gtk_file_chooser_dialog_new (_("Save Image"),
6418                                           GTK_WINDOW (window),
6419                                           GTK_FILE_CHOOSER_ACTION_SAVE,
6420                                           GTK_STOCK_CANCEL,
6421                                           GTK_RESPONSE_CANCEL,
6422                                           GTK_STOCK_SAVE, GTK_RESPONSE_OK,
6423                                           NULL);
6424
6425         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
6426         gtk_dialog_set_alternative_button_order (GTK_DIALOG (fc),
6427                                                  GTK_RESPONSE_OK,
6428                                                  GTK_RESPONSE_CANCEL,
6429                                                  -1);
6430
6431         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
6432         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
6433         
6434         file_chooser_dialog_add_writable_pixbuf_formats (GTK_FILE_CHOOSER (fc));
6435         
6436         g_signal_connect (fc, "response",
6437                           G_CALLBACK (image_save_dialog_response_cb),
6438                           window);
6439
6440         gtk_widget_show (fc);
6441 }
6442
6443 static void
6444 ev_view_popup_cmd_copy_image (GtkAction *action, EvWindow *window)
6445 {
6446         GtkClipboard *clipboard;
6447         GdkPixbuf    *pixbuf;
6448
6449         if (!window->priv->image)
6450                 return;
6451         
6452         clipboard = gtk_widget_get_clipboard (GTK_WIDGET (window),
6453                                               GDK_SELECTION_CLIPBOARD);
6454         ev_document_doc_mutex_lock ();
6455         pixbuf = ev_document_images_get_image (EV_DOCUMENT_IMAGES (window->priv->document),
6456                                                window->priv->image);
6457         ev_document_doc_mutex_unlock ();
6458         
6459         gtk_clipboard_set_image (clipboard, pixbuf);
6460         g_object_unref (pixbuf);
6461 }
6462
6463 static void
6464 ev_view_popup_cmd_annot_properties (GtkAction *action,
6465                                     EvWindow  *window)
6466 {
6467         const gchar                  *author;
6468         GdkColor                      color;
6469         gdouble                       opacity;
6470         gboolean                      popup_is_open;
6471         EvAnnotationPropertiesDialog *dialog;
6472         EvAnnotation                 *annot = window->priv->annot;
6473         EvAnnotationsSaveMask         mask = EV_ANNOTATIONS_SAVE_NONE;
6474
6475         if (!annot)
6476                 return;
6477
6478         dialog = EV_ANNOTATION_PROPERTIES_DIALOG (ev_annotation_properties_dialog_new_with_annotation (window->priv->annot));
6479         if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_APPLY) {
6480                 gtk_widget_destroy (GTK_WIDGET (dialog));
6481
6482                 return;
6483         }
6484
6485         /* Set annotations changes */
6486         author = ev_annotation_properties_dialog_get_author (dialog);
6487         if (ev_annotation_markup_set_label (EV_ANNOTATION_MARKUP (annot), author))
6488                 mask |= EV_ANNOTATIONS_SAVE_LABEL;
6489
6490         ev_annotation_properties_dialog_get_color (dialog, &color);
6491         if (ev_annotation_set_color (annot, &color))
6492                 mask |= EV_ANNOTATIONS_SAVE_COLOR;
6493
6494         opacity = ev_annotation_properties_dialog_get_opacity (dialog);
6495         if (ev_annotation_markup_set_opacity (EV_ANNOTATION_MARKUP (annot), opacity))
6496                 mask |= EV_ANNOTATIONS_SAVE_OPACITY;
6497
6498         popup_is_open = ev_annotation_properties_dialog_get_popup_is_open (dialog);
6499         if (ev_annotation_markup_set_popup_is_open (EV_ANNOTATION_MARKUP (annot), popup_is_open))
6500                 mask |= EV_ANNOTATIONS_SAVE_POPUP_IS_OPEN;
6501
6502         if (EV_IS_ANNOTATION_TEXT (annot)) {
6503                 EvAnnotationTextIcon icon;
6504
6505                 icon = ev_annotation_properties_dialog_get_text_icon (dialog);
6506                 if (ev_annotation_text_set_icon (EV_ANNOTATION_TEXT (annot), icon))
6507                         mask |= EV_ANNOTATIONS_SAVE_TEXT_ICON;
6508         }
6509
6510         if (mask != EV_ANNOTATIONS_SAVE_NONE) {
6511                 ev_document_doc_mutex_lock ();
6512                 ev_document_annotations_save_annotation (EV_DOCUMENT_ANNOTATIONS (window->priv->document),
6513                                                          window->priv->annot,
6514                                                          mask);
6515                 ev_document_doc_mutex_unlock ();
6516
6517                 /* FIXME: update annot region only */
6518                 ev_view_reload (EV_VIEW (window->priv->view));
6519         }
6520
6521         gtk_widget_destroy (GTK_WIDGET (dialog));
6522 }
6523
6524 static void
6525 ev_attachment_popup_cmd_open_attachment (GtkAction *action, EvWindow *window)
6526 {
6527         GList     *l;
6528         GdkScreen *screen;
6529         
6530         if (!window->priv->attach_list)
6531                 return;
6532
6533         screen = gtk_window_get_screen (GTK_WINDOW (window));
6534
6535         for (l = window->priv->attach_list; l && l->data; l = g_list_next (l)) {
6536                 EvAttachment *attachment;
6537                 GError       *error = NULL;
6538                 
6539                 attachment = (EvAttachment *) l->data;
6540                 
6541                 ev_attachment_open (attachment, screen, gtk_get_current_event_time (), &error);
6542
6543                 if (error) {
6544                         ev_window_error_message (window, error, 
6545                                                  "%s", _("Unable to open attachment"));
6546                         g_error_free (error);
6547                 }
6548         }
6549 }
6550
6551 static void
6552 attachment_save_dialog_response_cb (GtkWidget *fc,
6553                                     gint       response_id,
6554                                     EvWindow  *ev_window)
6555 {
6556         GFile                *target_file;
6557         gchar                *uri;
6558         GList                *l;
6559         GtkFileChooserAction  fc_action;
6560         gboolean              is_dir;
6561         gboolean              is_native;
6562         
6563         if (response_id != GTK_RESPONSE_OK) {
6564                 gtk_widget_destroy (fc);
6565                 return;
6566         }
6567
6568         uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
6569         target_file = g_file_new_for_uri (uri);
6570         g_object_get (G_OBJECT (fc), "action", &fc_action, NULL);
6571         is_dir = (fc_action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
6572         is_native = g_file_is_native (target_file);
6573         
6574         for (l = ev_window->priv->attach_list; l && l->data; l = g_list_next (l)) {
6575                 EvAttachment *attachment;
6576                 GFile        *save_to = NULL;
6577                 GError       *error = NULL;
6578                 
6579                 attachment = (EvAttachment *) l->data;
6580
6581                 if (is_native) {
6582                         if (is_dir) {
6583                                 save_to = g_file_get_child (target_file,
6584                                     /* FIXMEchpe: file name encoding! */
6585                                                             ev_attachment_get_name (attachment));
6586                         } else {
6587                                 save_to = g_object_ref (target_file);
6588                         }
6589                 } else {
6590                         save_to = ev_mkstemp_file ("saveattachment.XXXXXX", &error);
6591                 }
6592
6593                 if (save_to)
6594                         ev_attachment_save (attachment, save_to, &error);
6595                 
6596                 if (error) {
6597                         ev_window_error_message (ev_window, error, 
6598                                                  "%s", _("The attachment could not be saved."));
6599                         g_error_free (error);
6600                         g_object_unref (save_to);
6601
6602                         continue;
6603                 }
6604
6605                 if (!is_native) {
6606                         GFile *dest_file;
6607
6608                         if (is_dir) {
6609                                 dest_file = g_file_get_child (target_file,
6610                                                               ev_attachment_get_name (attachment));
6611                         } else {
6612                                 dest_file = g_object_ref (target_file);
6613                         }
6614
6615                         ev_window_save_remote (ev_window, EV_SAVE_ATTACHMENT,
6616                                                save_to, dest_file);
6617
6618                         g_object_unref (dest_file);
6619                 }
6620
6621                 g_object_unref (save_to);
6622         }
6623
6624         g_free (uri);
6625         g_object_unref (target_file);
6626
6627         gtk_widget_destroy (fc);
6628 }
6629
6630 static void
6631 ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
6632 {
6633         GtkWidget    *fc;
6634         EvAttachment *attachment = NULL;
6635
6636         if (!window->priv->attach_list)
6637                 return;
6638
6639         if (g_list_length (window->priv->attach_list) == 1)
6640                 attachment = (EvAttachment *) window->priv->attach_list->data;
6641         
6642         fc = gtk_file_chooser_dialog_new (
6643                 _("Save Attachment"),
6644                 GTK_WINDOW (window),
6645                 attachment ? GTK_FILE_CHOOSER_ACTION_SAVE : GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
6646                 GTK_STOCK_CANCEL,
6647                 GTK_RESPONSE_CANCEL,
6648                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
6649                 NULL);
6650
6651         gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
6652         gtk_dialog_set_alternative_button_order (GTK_DIALOG (fc),
6653                                                  GTK_RESPONSE_OK,
6654                                                  GTK_RESPONSE_CANCEL,
6655                                                  -1);
6656
6657         gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
6658         gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
6659
6660         if (attachment)
6661                 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc),
6662                                                    ev_attachment_get_name (attachment));
6663
6664         g_signal_connect (fc, "response",
6665                           G_CALLBACK (attachment_save_dialog_response_cb),
6666                           window);
6667
6668         gtk_widget_show (fc);
6669 }
6670
6671 static void
6672 ev_window_media_player_key_pressed (EvWindow    *window,
6673                                     const gchar *key,
6674                                     gpointer     user_data)
6675 {
6676         if (!gtk_window_is_active (GTK_WINDOW (window))) 
6677                 return;
6678         
6679         /* Note how Previous/Next only go to the
6680          * next/previous page despite their icon telling you
6681          * they should go to the beginning/end.
6682          *
6683          * There's very few keyboards with FFW/RWD though,
6684          * so we stick the most useful keybinding on the most
6685          * often seen keys
6686          */
6687         if (strcmp (key, "Play") == 0) {
6688                 ev_window_run_presentation (window);
6689         } else if (strcmp (key, "Previous") == 0) {
6690                 ev_window_cmd_go_previous_page (NULL, window);
6691         } else if (strcmp (key, "Next") == 0) {
6692                 ev_window_cmd_go_next_page (NULL, window);
6693         } else if (strcmp (key, "FastForward") == 0) {
6694                 ev_window_cmd_go_last_page (NULL, window);
6695         } else if (strcmp (key, "Rewind") == 0) {
6696                 ev_window_cmd_go_first_page (NULL, window);
6697         }
6698 }
6699
6700 static EggToolbarsModel *
6701 get_toolbars_model (void)
6702 {
6703         EggToolbarsModel *toolbars_model;
6704         gchar            *toolbars_file;
6705         gchar            *toolbars_path;
6706         gint              i;
6707
6708         toolbars_model = egg_toolbars_model_new ();
6709
6710         toolbars_file = g_build_filename (ev_application_get_dot_dir (EV_APP, FALSE),
6711                                           "evince_toolbar.xml", NULL);
6712         toolbars_path = g_build_filename (ev_application_get_data_dir (EV_APP),
6713                                          "evince-toolbar.xml", NULL);
6714         egg_toolbars_model_load_names (toolbars_model, toolbars_path);
6715
6716         if (!egg_toolbars_model_load_toolbars (toolbars_model, toolbars_file)) {
6717                 egg_toolbars_model_load_toolbars (toolbars_model, toolbars_path);
6718                 goto skip_conversion;
6719         }
6720
6721         /* Open item doesn't exist anymore,
6722          * convert it to OpenRecent for compatibility
6723          */
6724         for (i = 0; i < egg_toolbars_model_n_items (toolbars_model, 0); i++) {
6725                 const gchar *item;
6726
6727                 item = egg_toolbars_model_item_nth (toolbars_model, 0, i);
6728                 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
6729                         egg_toolbars_model_remove_item (toolbars_model, 0, i);
6730                         egg_toolbars_model_add_item (toolbars_model, 0, i,
6731                                                      "FileOpenRecent");
6732                         egg_toolbars_model_save_toolbars (toolbars_model, toolbars_file, "1.0");
6733                         break;
6734                 }
6735         }
6736
6737     skip_conversion:
6738         g_free (toolbars_file);
6739         g_free (toolbars_path);
6740
6741         egg_toolbars_model_set_flags (toolbars_model, 0, EGG_TB_MODEL_NOT_REMOVABLE);
6742
6743         return toolbars_model;
6744 }
6745
6746 #ifdef ENABLE_DBUS
6747 static void
6748 ev_window_sync_source (EvWindow     *window,
6749                        EvSourceLink *link)
6750 {
6751         GDBusConnection *connection;
6752         GError          *error = NULL;
6753         guint32          timestamp;
6754         gchar           *uri_input;
6755         GFile           *input_gfile;
6756
6757         if (window->priv->dbus_object_id <= 0)
6758                 return;
6759
6760         connection = ev_application_get_dbus_connection (EV_APP);
6761         if (!connection)
6762                 return;
6763
6764         timestamp = gtk_get_current_event_time ();
6765         if (g_path_is_absolute (link->filename)) {
6766                 input_gfile = g_file_new_for_path (link->filename);
6767         } else {
6768                 GFile *gfile, *parent_gfile;
6769
6770                 gfile = g_file_new_for_uri (window->priv->uri);
6771                 parent_gfile = g_file_get_parent (gfile);
6772
6773                 /* parent_gfile should never be NULL */
6774                 if (parent_gfile == NULL) {
6775                         g_printerr ("Document URI is '/'\n");
6776                         return;
6777                 }
6778
6779                 input_gfile = g_file_get_child (parent_gfile, link->filename);
6780                 g_object_unref (parent_gfile);
6781                 g_object_unref (gfile);
6782         }
6783
6784         uri_input = g_file_get_uri (input_gfile);
6785         g_object_unref (input_gfile);
6786
6787         g_dbus_connection_emit_signal (connection,
6788                                        NULL,
6789                                        window->priv->dbus_object_path,
6790                                        EV_WINDOW_DBUS_INTERFACE,
6791                                        "SyncSource",
6792                                        g_variant_new ("(s(ii)u)",
6793                                                       uri_input,
6794                                                       link->line,
6795                                                       link->col,
6796                                                       timestamp),
6797                                        &error);
6798         g_free (uri_input);
6799         if (error) {
6800                 g_printerr ("Failed to emit DBus signal SyncSource: %s\n",
6801                             error->message);
6802                 g_error_free (error);
6803         }
6804 }
6805
6806 static void
6807 ev_window_emit_closed (EvWindow *window)
6808 {
6809         GDBusConnection *connection;
6810         GError          *error = NULL;
6811
6812         if (window->priv->dbus_object_id <= 0)
6813                 return;
6814
6815         connection = ev_application_get_dbus_connection (EV_APP);
6816         if (!connection)
6817                 return;
6818
6819         g_dbus_connection_emit_signal (connection,
6820                                        NULL,
6821                                        window->priv->dbus_object_path,
6822                                        EV_WINDOW_DBUS_INTERFACE,
6823                                        "Closed",
6824                                        NULL,
6825                                        &error);
6826         if (error) {
6827                 g_printerr ("Failed to emit DBus signal Closed: %s\n",
6828                             error->message);
6829                 g_error_free (error);
6830
6831                 return;
6832         }
6833
6834         /* If this is the last window call g_dbus_connection_flush_sync()
6835          * to make sure the signal is emitted.
6836          */
6837         if (ev_application_get_n_windows (EV_APP) == 1)
6838                 g_dbus_connection_flush_sync (connection, NULL, NULL);
6839 }
6840
6841 static void
6842 ev_window_emit_doc_loaded (EvWindow *window)
6843 {
6844         GDBusConnection *connection;
6845         GError          *error = NULL;
6846
6847         if (window->priv->dbus_object_id <= 0)
6848                 return;
6849
6850         connection = ev_application_get_dbus_connection (EV_APP);
6851         if (!connection)
6852                 return;
6853
6854         g_dbus_connection_emit_signal (connection,
6855                                        NULL,
6856                                        window->priv->dbus_object_path,
6857                                        EV_WINDOW_DBUS_INTERFACE,
6858                                        "DocumentLoaded",
6859                                        g_variant_new("(s)", window->priv->uri),
6860                                        &error);
6861         if (error) {
6862                 g_printerr ("Failed to emit DBus signal DocumentLoaded: %s\n",
6863                             error->message);
6864                 g_error_free (error);
6865
6866                 return;
6867         }
6868 }
6869
6870 static void
6871 method_call_cb (GDBusConnection       *connection,
6872                 const gchar           *sender,
6873                 const gchar           *object_path,
6874                 const gchar           *interface_name,
6875                 const gchar           *method_name,
6876                 GVariant              *parameters,
6877                 GDBusMethodInvocation *invocation,
6878                 gpointer               user_data)
6879 {
6880         EvWindow *window = EV_WINDOW (user_data);
6881
6882         if (g_strcmp0 (method_name, "SyncView") != 0)
6883                 return;
6884
6885         if (window->priv->document && ev_document_has_synctex (window->priv->document)) {
6886                 EvSourceLink link;
6887                 guint32      timestamp;
6888
6889                 g_variant_get (parameters, "(&s(ii)u)", &link.filename, &link.line, &link.col, &timestamp);
6890                 ev_view_highlight_forward_search (EV_VIEW (window->priv->view), &link);
6891                 gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
6892         }
6893
6894         g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
6895 }
6896
6897 static const char introspection_xml[] =
6898         "<node>"
6899           "<interface name='org.gnome.evince.Window'>"
6900             "<method name='SyncView'>"
6901               "<arg type='s' name='source_file' direction='in'/>"
6902               "<arg type='(ii)' name='source_point' direction='in'/>"
6903               "<arg type='u' name='timestamp' direction='in'/>"
6904             "</method>"
6905             "<signal name='SyncSource'>"
6906               "<arg type='s' name='source_file' direction='out'/>"
6907               "<arg type='(ii)' name='source_point' direction='out'/>"
6908               "<arg type='u' name='timestamp' direction='out'/>"
6909             "</signal>"
6910             "<signal name='Closed'/>"
6911             "<signal name='DocumentLoaded'>"
6912               "<arg type='s' name='uri' direction='out'/>"
6913             "</signal>"
6914           "</interface>"
6915         "</node>";
6916
6917 static const GDBusInterfaceVTable interface_vtable = {
6918         method_call_cb,
6919         NULL,
6920         NULL
6921 };
6922
6923 static GDBusNodeInfo *introspection_data;
6924 #endif /* ENABLE_DBUS */
6925
6926 static void
6927 ev_window_init (EvWindow *ev_window)
6928 {
6929         GtkActionGroup *action_group;
6930         GtkAccelGroup *accel_group;
6931         GError *error = NULL;
6932         GtkWidget *sidebar_widget;
6933         GtkWidget *menuitem;
6934         EggToolbarsModel *toolbars_model;
6935         GObject *mpkeys;
6936         gchar *ui_path;
6937 #ifdef ENABLE_DBUS
6938         GDBusConnection *connection;
6939         static gint window_id = 0;
6940 #endif
6941
6942         g_signal_connect (ev_window, "configure_event",
6943                           G_CALLBACK (window_configure_event_cb), NULL);
6944         g_signal_connect (ev_window, "window_state_event",
6945                           G_CALLBACK (window_state_event_cb), NULL);
6946
6947         ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
6948
6949 #ifdef ENABLE_DBUS
6950         connection = ev_application_get_dbus_connection (EV_APP);
6951         if (connection) {
6952                 if (!introspection_data) {
6953                         introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);
6954                         if (error) g_warning ("%s\n", error->message);
6955                 }
6956                 g_assert (introspection_data != NULL);
6957
6958                 ev_window->priv->dbus_object_path = g_strdup_printf (EV_WINDOW_DBUS_OBJECT_PATH, window_id++);
6959                 ev_window->priv->dbus_object_id =
6960                     g_dbus_connection_register_object (connection,
6961                                                        ev_window->priv->dbus_object_path,
6962                                                        introspection_data->interfaces[0],
6963                                                        &interface_vtable,
6964                                                        ev_window, NULL,
6965                                                        &error);
6966                 if (ev_window->priv->dbus_object_id == 0) {
6967                         g_printerr ("Failed to register bus object %s: %s\n",
6968                                     ev_window->priv->dbus_object_path, error->message);
6969                         g_error_free (error);
6970                         g_free (ev_window->priv->dbus_object_path);
6971                         ev_window->priv->dbus_object_path = NULL;
6972                         error = NULL;
6973                 }
6974         }
6975
6976 #endif /* ENABLE_DBUS */
6977
6978         ev_window->priv->model = ev_document_model_new ();
6979
6980         ev_window->priv->page_mode = PAGE_MODE_DOCUMENT;
6981         ev_window->priv->title = ev_window_title_new (ev_window);
6982
6983         ev_window->priv->main_box = gtk_vbox_new (FALSE, 0);
6984         gtk_container_add (GTK_CONTAINER (ev_window), ev_window->priv->main_box);
6985         gtk_widget_show (ev_window->priv->main_box);
6986
6987         action_group = gtk_action_group_new ("MenuActions");
6988         ev_window->priv->action_group = action_group;
6989         gtk_action_group_set_translation_domain (action_group, NULL);
6990         gtk_action_group_add_actions (action_group, entries,
6991                                       G_N_ELEMENTS (entries), ev_window);
6992         gtk_action_group_add_toggle_actions (action_group, toggle_entries,
6993                                              G_N_ELEMENTS (toggle_entries),
6994                                              ev_window);
6995         set_action_properties (action_group);
6996         register_custom_actions (ev_window, action_group);
6997
6998         ev_window->priv->ui_manager = gtk_ui_manager_new ();
6999         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7000                                             action_group, 0);
7001
7002         accel_group =
7003                 gtk_ui_manager_get_accel_group (ev_window->priv->ui_manager);
7004         gtk_window_add_accel_group (GTK_WINDOW (ev_window), accel_group);
7005
7006         ev_window_set_view_accels_sensitivity (ev_window, FALSE);
7007
7008         action_group = gtk_action_group_new ("ViewPopupActions");
7009         ev_window->priv->view_popup_action_group = action_group;
7010         gtk_action_group_set_translation_domain (action_group, NULL);
7011         gtk_action_group_add_actions (action_group, view_popup_entries,
7012                                       G_N_ELEMENTS (view_popup_entries),
7013                                       ev_window);
7014         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7015                                             action_group, 0);
7016
7017         action_group = gtk_action_group_new ("AttachmentPopupActions");
7018         ev_window->priv->attachment_popup_action_group = action_group;
7019         gtk_action_group_set_translation_domain (action_group, NULL);
7020         gtk_action_group_add_actions (action_group, attachment_popup_entries,
7021                                       G_N_ELEMENTS (attachment_popup_entries),
7022                                       ev_window);
7023         gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
7024                                             action_group, 0);
7025
7026         ui_path = g_build_filename (ev_application_get_data_dir (EV_APP),
7027                                     "evince-ui.xml", NULL);
7028         if (!gtk_ui_manager_add_ui_from_file (
7029                 ev_window->priv->ui_manager, ui_path, &error))
7030         {
7031                 g_warning ("building menus failed: %s", error->message);
7032                 g_error_free (error);
7033         }
7034         g_free (ui_path);
7035
7036         ev_window->priv->recent_manager = gtk_recent_manager_get_default ();
7037         ev_window->priv->recent_action_group = NULL;
7038         ev_window->priv->recent_ui_id = 0;
7039         g_signal_connect_swapped (ev_window->priv->recent_manager,
7040                                   "changed",
7041                                   G_CALLBACK (ev_window_setup_recent),
7042                                   ev_window);
7043
7044         ev_window->priv->menubar =
7045                  gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7046                                             "/MainMenu");
7047         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
7048                             ev_window->priv->menubar,
7049                             FALSE, FALSE, 0);
7050         menuitem = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7051                                               "/MainMenu/EditMenu/EditRotateLeftMenu");
7052         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menuitem), TRUE);
7053         menuitem = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7054                                               "/MainMenu/EditMenu/EditRotateRightMenu");
7055         gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (menuitem), TRUE);
7056
7057         toolbars_model = get_toolbars_model ();
7058         ev_window->priv->toolbar = GTK_WIDGET
7059                 (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
7060                                "ui-manager", ev_window->priv->ui_manager,
7061                                "popup-path", "/ToolbarPopup",
7062                                "model", toolbars_model,
7063                                NULL));
7064         g_object_unref (toolbars_model);
7065
7066         egg_editable_toolbar_show (EGG_EDITABLE_TOOLBAR (ev_window->priv->toolbar),
7067                                    "DefaultToolBar");
7068         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box),
7069                             ev_window->priv->toolbar,
7070                             FALSE, FALSE, 0);
7071         gtk_widget_show (ev_window->priv->toolbar);
7072
7073         /* Add the main area */
7074         ev_window->priv->hpaned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
7075         g_signal_connect (ev_window->priv->hpaned,
7076                           "notify::position",
7077                           G_CALLBACK (ev_window_sidebar_position_change_cb),
7078                           ev_window);
7079         
7080         gtk_paned_set_position (GTK_PANED (ev_window->priv->hpaned), SIDEBAR_DEFAULT_SIZE);
7081         gtk_box_pack_start (GTK_BOX (ev_window->priv->main_box), ev_window->priv->hpaned,
7082                             TRUE, TRUE, 0);
7083         gtk_widget_show (ev_window->priv->hpaned);
7084         
7085         ev_window->priv->sidebar = ev_sidebar_new ();
7086         ev_sidebar_set_model (EV_SIDEBAR (ev_window->priv->sidebar),
7087                               ev_window->priv->model);
7088         gtk_paned_pack1 (GTK_PANED (ev_window->priv->hpaned),
7089                          ev_window->priv->sidebar, FALSE, FALSE);
7090         gtk_widget_show (ev_window->priv->sidebar);
7091
7092         /* Stub sidebar, for now */
7093
7094         sidebar_widget = ev_sidebar_thumbnails_new ();
7095         ev_window->priv->sidebar_thumbs = sidebar_widget;
7096         g_signal_connect (sidebar_widget,
7097                           "notify::main-widget",
7098                           G_CALLBACK (sidebar_page_main_widget_update_cb),
7099                           ev_window);
7100         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
7101         gtk_widget_show (sidebar_widget);
7102         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7103                              sidebar_widget);
7104
7105         sidebar_widget = ev_sidebar_links_new ();
7106         ev_window->priv->sidebar_links = sidebar_widget;
7107         g_signal_connect (sidebar_widget,
7108                           "notify::model",
7109                           G_CALLBACK (sidebar_widget_model_set),
7110                           ev_window);
7111         g_signal_connect (sidebar_widget,
7112                           "link_activated",
7113                           G_CALLBACK (sidebar_links_link_activated_cb),
7114                           ev_window);
7115         sidebar_page_main_widget_update_cb (G_OBJECT (sidebar_widget), NULL, ev_window);
7116         gtk_widget_show (sidebar_widget);
7117         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7118                              sidebar_widget);
7119
7120         sidebar_widget = ev_sidebar_attachments_new ();
7121         ev_window->priv->sidebar_attachments = sidebar_widget;
7122         g_signal_connect_object (sidebar_widget,
7123                                  "popup",
7124                                  G_CALLBACK (attachment_bar_menu_popup_cb),
7125                                  ev_window, 0);
7126         gtk_widget_show (sidebar_widget);
7127         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7128                              sidebar_widget);
7129
7130         sidebar_widget = ev_sidebar_layers_new ();
7131         ev_window->priv->sidebar_layers = sidebar_widget;
7132         g_signal_connect (sidebar_widget,
7133                           "layers_visibility_changed",
7134                           G_CALLBACK (sidebar_layers_visibility_changed),
7135                           ev_window);
7136         gtk_widget_show (sidebar_widget);
7137         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7138                              sidebar_widget);
7139
7140         sidebar_widget = ev_sidebar_annotations_new ();
7141         ev_window->priv->sidebar_annots = sidebar_widget;
7142         g_signal_connect (sidebar_widget,
7143                           "annot_activated",
7144                           G_CALLBACK (sidebar_annots_annot_activated_cb),
7145                           ev_window);
7146         g_signal_connect (sidebar_widget,
7147                           "begin_annot_add",
7148                           G_CALLBACK (sidebar_annots_begin_annot_add),
7149                           ev_window);
7150         g_signal_connect (sidebar_widget,
7151                           "annot_add_cancelled",
7152                           G_CALLBACK (sidebar_annots_annot_add_cancelled),
7153                           ev_window);
7154         gtk_widget_show (sidebar_widget);
7155         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7156                              sidebar_widget);
7157
7158         sidebar_widget = ev_sidebar_bookmarks_new ();
7159         ev_window->priv->sidebar_bookmarks = sidebar_widget;
7160         g_signal_connect (sidebar_widget,
7161                           "add-bookmark",
7162                           G_CALLBACK (sidebar_bookmarks_add_bookmark),
7163                           ev_window);
7164         gtk_widget_show (sidebar_widget);
7165         ev_sidebar_add_page (EV_SIDEBAR (ev_window->priv->sidebar),
7166                              sidebar_widget);
7167
7168         ev_window->priv->view_box = gtk_vbox_new (FALSE, 0);
7169         ev_window->priv->scrolled_window =
7170                 GTK_WIDGET (g_object_new (GTK_TYPE_SCROLLED_WINDOW,
7171                                           "shadow-type", GTK_SHADOW_IN,
7172                                           NULL));
7173         gtk_box_pack_start (GTK_BOX (ev_window->priv->view_box),
7174                             ev_window->priv->scrolled_window,
7175                             TRUE, TRUE, 0);
7176         gtk_widget_show (ev_window->priv->scrolled_window);
7177
7178         gtk_paned_add2 (GTK_PANED (ev_window->priv->hpaned),
7179                         ev_window->priv->view_box);
7180         gtk_widget_show (ev_window->priv->view_box);
7181
7182         ev_window->priv->view = ev_view_new ();
7183         ev_view_set_page_cache_size (EV_VIEW (ev_window->priv->view), PAGE_CACHE_SIZE);
7184         ev_view_set_model (EV_VIEW (ev_window->priv->view), ev_window->priv->model);
7185
7186         ev_window->priv->password_view = ev_password_view_new (GTK_WINDOW (ev_window));
7187         g_signal_connect_swapped (ev_window->priv->password_view,
7188                                   "unlock",
7189                                   G_CALLBACK (ev_window_password_view_unlock),
7190                                   ev_window);
7191         g_signal_connect_object (ev_window->priv->view, "focus_in_event",
7192                                  G_CALLBACK (view_actions_focus_in_cb),
7193                                  ev_window, 0);
7194         g_signal_connect_object (ev_window->priv->view, "focus_out_event",
7195                                  G_CALLBACK (view_actions_focus_out_cb),
7196                                  ev_window, 0);
7197         g_signal_connect_object (ev_window->priv->view, "external-link",
7198                                  G_CALLBACK (view_external_link_cb),
7199                                  ev_window, 0);
7200         g_signal_connect_object (ev_window->priv->view, "handle-link",
7201                                  G_CALLBACK (view_handle_link_cb),
7202                                  ev_window, 0);
7203         g_signal_connect_object (ev_window->priv->view, "popup",
7204                                  G_CALLBACK (view_menu_popup_cb),
7205                                  ev_window, 0);
7206         g_signal_connect_object (ev_window->priv->view, "selection-changed",
7207                                  G_CALLBACK (view_selection_changed_cb),
7208                                  ev_window, 0);
7209         g_signal_connect_object (ev_window->priv->view, "annot-added",
7210                                  G_CALLBACK (view_annot_added),
7211                                  ev_window, 0);
7212         g_signal_connect_object (ev_window->priv->view, "layers-changed",
7213                                  G_CALLBACK (view_layers_changed_cb),
7214                                  ev_window, 0);
7215 #ifdef ENABLE_DBUS
7216         g_signal_connect_swapped (ev_window->priv->view, "sync-source",
7217                                   G_CALLBACK (ev_window_sync_source),
7218                                   ev_window);
7219 #endif
7220         gtk_widget_show (ev_window->priv->view);
7221         gtk_widget_show (ev_window->priv->password_view);
7222
7223         /* Find Bar */
7224         ev_window->priv->find_bar = egg_find_bar_new ();
7225         gtk_box_pack_end (GTK_BOX (ev_window->priv->main_box),
7226                           ev_window->priv->find_bar,
7227                           FALSE, TRUE, 0);
7228
7229         /* We own a ref on these widgets, as we can swap them in and out */
7230         g_object_ref (ev_window->priv->view);
7231         g_object_ref (ev_window->priv->password_view);
7232
7233         gtk_container_add (GTK_CONTAINER (ev_window->priv->scrolled_window),
7234                            ev_window->priv->view);
7235
7236         /* Connect to model signals */
7237         g_signal_connect_swapped (ev_window->priv->model,
7238                                   "page-changed",
7239                                   G_CALLBACK (ev_window_page_changed_cb),
7240                                   ev_window);
7241         g_signal_connect (ev_window->priv->model,
7242                           "notify::document",
7243                           G_CALLBACK (ev_window_document_changed_cb),
7244                           ev_window);
7245         g_signal_connect (ev_window->priv->model,
7246                           "notify::scale",
7247                           G_CALLBACK (ev_window_zoom_changed_cb),
7248                           ev_window);
7249         g_signal_connect (ev_window->priv->model,
7250                           "notify::sizing-mode",
7251                           G_CALLBACK (ev_window_sizing_mode_changed_cb),
7252                           ev_window);
7253         g_signal_connect (ev_window->priv->model,
7254                           "notify::rotation",
7255                           G_CALLBACK (ev_window_rotation_changed_cb),
7256                           ev_window);
7257         g_signal_connect (ev_window->priv->model,
7258                           "notify::continuous",
7259                           G_CALLBACK (ev_window_continuous_changed_cb),
7260                           ev_window);
7261         g_signal_connect (ev_window->priv->model,
7262                           "notify::dual-page",
7263                           G_CALLBACK (ev_window_dual_mode_changed_cb),
7264                           ev_window);
7265         g_signal_connect (ev_window->priv->model,
7266                           "notify::inverted-colors",
7267                           G_CALLBACK (ev_window_inverted_colors_changed_cb),
7268                           ev_window);
7269
7270         /* Connect sidebar signals */
7271         g_signal_connect (ev_window->priv->sidebar,
7272                           "notify::visible",
7273                           G_CALLBACK (ev_window_sidebar_visibility_changed_cb),
7274                           ev_window);
7275         g_signal_connect (ev_window->priv->sidebar,
7276                           "notify::current-page",
7277                           G_CALLBACK (ev_window_sidebar_current_page_changed_cb),
7278                           ev_window);
7279
7280         /* Connect to find bar signals */
7281         g_signal_connect (ev_window->priv->find_bar,
7282                           "previous",
7283                           G_CALLBACK (find_bar_previous_cb),
7284                           ev_window);
7285         g_signal_connect (ev_window->priv->find_bar,
7286                           "next",
7287                           G_CALLBACK (find_bar_next_cb),
7288                           ev_window);
7289         g_signal_connect (ev_window->priv->find_bar,
7290                           "close",
7291                           G_CALLBACK (find_bar_close_cb),
7292                           ev_window);
7293         g_signal_connect (ev_window->priv->find_bar,
7294                           "notify::search-string",
7295                           G_CALLBACK (find_bar_search_changed_cb),
7296                           ev_window);
7297         g_signal_connect (ev_window->priv->find_bar,
7298                           "notify::case-sensitive",
7299                           G_CALLBACK (find_bar_search_changed_cb),
7300                           ev_window);
7301         g_signal_connect (ev_window->priv->find_bar,
7302                           "notify::visible",
7303                           G_CALLBACK (find_bar_visibility_changed_cb),
7304                           ev_window);
7305         g_signal_connect (ev_window->priv->find_bar,
7306                           "scroll",
7307                           G_CALLBACK (find_bar_scroll),
7308                           ev_window);
7309
7310         /* Popups */
7311         ev_window->priv->view_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7312                                                                  "/DocumentPopup");
7313         ev_window->priv->link = NULL;
7314
7315         ev_window->priv->attachment_popup = gtk_ui_manager_get_widget (ev_window->priv->ui_manager,
7316                                                                        "/AttachmentPopup");
7317         ev_window->priv->attach_list = NULL;
7318
7319         /* Media player keys */
7320         mpkeys = ev_application_get_media_keys (EV_APP);
7321         if (mpkeys) {
7322                 g_signal_connect_swapped (mpkeys, "key_pressed",
7323                                           G_CALLBACK (ev_window_media_player_key_pressed),
7324                                           ev_window);
7325         }
7326
7327         /* Give focus to the document view */
7328         gtk_widget_grab_focus (ev_window->priv->view);
7329
7330         ev_window->priv->default_settings = g_settings_new (GS_SCHEMA_NAME".Default");
7331         g_settings_delay (ev_window->priv->default_settings);
7332
7333         /* Set it user interface params */
7334         ev_window_setup_recent (ev_window);
7335
7336         ev_window_setup_gtk_settings (ev_window);
7337
7338         setup_chrome_from_metadata (ev_window);
7339         update_chrome_actions (ev_window);
7340         update_chrome_visibility (ev_window);
7341
7342         gtk_window_set_default_size (GTK_WINDOW (ev_window), 600, 600);
7343
7344         setup_view_from_metadata (ev_window);
7345         setup_sidebar_from_metadata (ev_window);
7346
7347         ev_window_sizing_mode_changed_cb (ev_window->priv->model, NULL, ev_window);
7348         ev_window_setup_action_sensitivity (ev_window);
7349
7350         /* Drag and Drop */
7351         gtk_drag_dest_set (GTK_WIDGET (ev_window),
7352                            GTK_DEST_DEFAULT_ALL,
7353                            NULL, 0,
7354                            GDK_ACTION_COPY);
7355         gtk_drag_dest_add_uri_targets (GTK_WIDGET (ev_window));
7356 }
7357
7358 /**
7359  * ev_window_new:
7360  *
7361  * Creates a #GtkWidget that represents the window.
7362  *
7363  * Returns: the #GtkWidget that represents the window.
7364  */
7365 GtkWidget *
7366 ev_window_new (void)
7367 {
7368         GtkWidget *ev_window;
7369
7370         ev_window = GTK_WIDGET (g_object_new (EV_TYPE_WINDOW,
7371                                               "type", GTK_WINDOW_TOPLEVEL,
7372                                               NULL));
7373
7374         return ev_window;
7375 }
7376
7377 const gchar *
7378 ev_window_get_dbus_object_path (EvWindow *ev_window)
7379 {
7380 #ifdef ENABLE_DBUS
7381         return ev_window->priv->dbus_object_path;
7382 #else
7383         return NULL;
7384 #endif
7385 }