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