]> www.fi.muni.cz Git - evince.git/blobdiff - shell/ev-window.c
[shell] Emit a DBus signal when window is closed
[evince.git] / shell / ev-window.c
index 1cb21aa6a079d5e4f4af763309e3ca8c4673195d..e79bf00805000b67a9e76e40dfafa546573abd36 100644 (file)
@@ -34,6 +34,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <math.h>
 
 #include <glib/gstdio.h>
 #include <glib/gi18n.h>
@@ -140,6 +141,10 @@ struct _EvWindowPrivate {
        GtkWidget *sidebar_attachments;
        GtkWidget *sidebar_layers;
 
+       /* Settings */
+       GSettings *settings;
+       GSettings *last_settings;
+
        /* Menubar accels */
        guint           menubar_accel_keyval;
        GdkModifierType menubar_accel_modifier;
@@ -207,6 +212,11 @@ struct _EvWindowPrivate {
 #ifdef WITH_GCONF
        GConfClient *gconf_client;
 #endif
+#ifdef ENABLE_DBUS
+       /* DBus */
+       guint  dbus_object_id;
+       gchar *dbus_object_path;
+#endif
 };
 
 #define EV_WINDOW_GET_PRIVATE(object) \
@@ -219,11 +229,18 @@ struct _EvWindowPrivate {
 #define NAVIGATION_ACTION      "Navigation"
 
 #define GCONF_LOCKDOWN_DIR          "/desktop/gnome/lockdown"
-#define GCONF_OVERRIDE_RESTRICTIONS "/apps/evince/override_restrictions"
 #define GCONF_LOCKDOWN_SAVE         "/desktop/gnome/lockdown/disable_save_to_disk"
 #define GCONF_LOCKDOWN_PRINT        "/desktop/gnome/lockdown/disable_printing"
 #define GCONF_LOCKDOWN_PRINT_SETUP  "/desktop/gnome/lockdown/disable_print_setup"
 
+#ifdef ENABLE_DBUS
+#define EV_WINDOW_DBUS_OBJECT_PATH "/org/gnome/evince/Window/%d"
+#define EV_WINDOW_DBUS_INTERFACE   "org.gnome.evince.Window"
+#endif
+
+#define GS_SCHEMA_NAME           "org.gnome.Evince"
+#define GS_OVERRIDE_RESTRICTIONS "override-restrictions"
+
 #define SIDEBAR_DEFAULT_SIZE    132
 #define LINKS_SIDEBAR_ID "links"
 #define THUMBNAILS_SIDEBAR_ID "thumbnails"
@@ -237,7 +254,7 @@ struct _EvWindowPrivate {
 #define EV_TOOLBARS_FILENAME "evince-toolbar.xml"
 
 #define MIN_SCALE 0.05409
-#define MAX_SCALE 4.0
+#define PAGE_CACHE_SIZE 52428800 /* 50MB */
 
 #define MAX_RECENT_ITEM_LEN (40)
 
@@ -319,6 +336,10 @@ static void     ev_window_load_file_remote              (EvWindow         *ev_wi
 static void     ev_window_media_player_key_pressed      (EvWindow         *window,
                                                         const gchar      *key,
                                                         gpointer          user_data);
+static void     ev_window_update_max_min_scale          (EvWindow         *window);
+#ifdef ENABLE_DBUS
+static void    ev_window_emit_closed                   (EvWindow         *window);
+#endif
 
 static guint ev_window_n_copies = 0;
 
@@ -351,7 +372,6 @@ ev_window_setup_action_sensitivity (EvWindow *ev_window)
        const EvDocumentInfo *info = NULL;
        gboolean has_document = FALSE;
        gboolean ok_to_print = TRUE;
-       gboolean ok_to_print_setup = TRUE;
        gboolean ok_to_copy = TRUE;
        gboolean has_properties = TRUE;
        gboolean override_restrictions = TRUE;
@@ -377,12 +397,12 @@ ev_window_setup_action_sensitivity (EvWindow *ev_window)
                can_find = TRUE;
        }
 
-#ifdef WITH_GCONF
-       if (has_document)
-               override_restrictions = gconf_client_get_bool (ev_window->priv->gconf_client,
-                                                              GCONF_OVERRIDE_RESTRICTIONS,
-                                                              NULL);
-#endif
+       if (has_document && ev_window->priv->settings) {
+               override_restrictions =
+                       g_settings_get_boolean (ev_window->priv->settings,
+                                               GS_OVERRIDE_RESTRICTIONS);
+       }
+
        if (!override_restrictions && info && info->fields_mask & EV_DOCUMENT_INFO_PERMISSIONS) {
                ok_to_print = (info->permissions & EV_DOCUMENT_PERMISSIONS_OK_TO_PRINT);
                ok_to_copy = (info->permissions & EV_DOCUMENT_PERMISSIONS_OK_TO_COPY);
@@ -401,11 +421,6 @@ ev_window_setup_action_sensitivity (EvWindow *ev_window)
            gconf_client_get_bool (ev_window->priv->gconf_client, GCONF_LOCKDOWN_PRINT, NULL)) {
                ok_to_print = FALSE;
        }
-
-       if (has_document &&
-           gconf_client_get_bool (ev_window->priv->gconf_client, GCONF_LOCKDOWN_PRINT_SETUP, NULL)) {
-               ok_to_print_setup = FALSE;
-       }
 #endif
 
        /* File menu */
@@ -565,7 +580,7 @@ update_chrome_visibility (EvWindow *window)
        fullscreen_toolbar = ((priv->chrome & EV_CHROME_FULLSCREEN_TOOLBAR) != 0 || 
                              (priv->chrome & EV_CHROME_RAISE_TOOLBAR) != 0) && fullscreen;
        findbar = (priv->chrome & EV_CHROME_FINDBAR) != 0;
-       sidebar = (priv->chrome & EV_CHROME_SIDEBAR) != 0 && !presentation;
+       sidebar = (priv->chrome & EV_CHROME_SIDEBAR) != 0 && priv->document && !presentation;
 
        set_widget_visibility (priv->menubar, menubar); 
        set_widget_visibility (priv->toolbar, toolbar);
@@ -897,11 +912,16 @@ setup_chrome_from_metadata (EvWindow *window)
        EvChrome chrome = EV_CHROME_NORMAL;
        gboolean show_toolbar;
 
-       if (window->priv->metadata &&
-           ev_metadata_get_boolean (window->priv->metadata, "show_toolbar", &show_toolbar)) {
+       if (window->priv->document) {
+               if (!window->priv->metadata ||
+                   !ev_metadata_get_boolean (window->priv->metadata, "show_toolbar", &show_toolbar)) {
+                       show_toolbar = g_settings_get_boolean (window->priv->last_settings, "show-toolbar");
+               }
+
                if (!show_toolbar)
                        chrome &= ~EV_CHROME_TOOLBAR;
        }
+
        window->priv->chrome = chrome;
 }
 
@@ -918,6 +938,15 @@ setup_sidebar_from_metadata (EvWindow *window)
        gint        sidebar_size;
        gboolean    sidebar_visibility;
 
+       if (document) {
+               if (!window->priv->metadata ||
+                   !ev_metadata_get_boolean (window->priv->metadata, "sidebar_visibility", &sidebar_visibility)) {
+                       sidebar_visibility = g_settings_get_boolean (window->priv->last_settings, "show-sidebar");
+               }
+               update_chrome_flag (window, EV_CHROME_SIDEBAR, sidebar_visibility);
+               update_chrome_visibility (window);
+       }
+
        if (!window->priv->metadata)
                return;
 
@@ -946,11 +975,6 @@ setup_sidebar_from_metadata (EvWindow *window)
                        ev_sidebar_set_page (EV_SIDEBAR (sidebar), layers);
                }
        }
-
-       if (ev_metadata_get_boolean (window->priv->metadata, "sidebar_visibility", &sidebar_visibility)) {
-               update_chrome_flag (window, EV_CHROME_SIDEBAR, sidebar_visibility);
-               update_chrome_visibility (window);
-       }
 }
 
 static void
@@ -1040,26 +1064,25 @@ setup_document_from_metadata (EvWindow *window)
        gdouble width_ratio;
        gdouble height_ratio;
 
-       if (!window->priv->metadata)
-               return;
+       if (window->priv->metadata) {
+               /* Make sure to not open a document on the last page,
+                * since closing it on the last page most likely means the
+                * user was finished reading the document. In that case, reopening should
+                * show the first page. */
+               page = ev_document_model_get_page (window->priv->model);
+               n_pages = ev_document_get_n_pages (window->priv->document);
+               if (page == n_pages - 1)
+                       ev_document_model_set_page (window->priv->model, 0);
 
-       /* Make sure to not open a document on the last page,
-        * since closing it on the last page most likely means the
-        * user was finished reading the document. In that case, reopening should
-        * show the first page. */
-       page = ev_document_model_get_page (window->priv->model);
-       n_pages = ev_document_get_n_pages (window->priv->document);
-       if (page == n_pages - 1)
-               ev_document_model_set_page (window->priv->model, 0);
+               setup_sidebar_from_metadata (window);
 
-       setup_sidebar_from_metadata (window);
-
-       if (ev_metadata_get_int (window->priv->metadata, "window_width", &width) &&
-           ev_metadata_get_int (window->priv->metadata, "window_height", &height))
-               return; /* size was already set in setup_size_from_metadata */
+               if (ev_metadata_get_int (window->priv->metadata, "window_width", &width) &&
+                   ev_metadata_get_int (window->priv->metadata, "window_height", &height))
+                       return; /* size was already set in setup_size_from_metadata */
+       }
 
-       if (ev_metadata_get_double (window->priv->metadata, "window_width_ratio", &width_ratio) &&
-           ev_metadata_get_double (window->priv->metadata, "window_height_ratio", &height_ratio)) {
+       g_settings_get (window->priv->last_settings, "window-ratio", "(dd)", &width_ratio, &height_ratio);
+       if (width_ratio > 0. && height_ratio > 0.) {
                gdouble    document_width;
                gdouble    document_height;
                GdkScreen *screen;
@@ -1075,7 +1098,7 @@ setup_document_from_metadata (EvWindow *window)
                screen = gtk_window_get_screen (GTK_WINDOW (window));
                if (screen) {
                        request_width = MIN (request_width, gdk_screen_get_width (screen));
-                       request_height = MIN (request_width, gdk_screen_get_height (screen));
+                       request_height = MIN (request_height, gdk_screen_get_height (screen));
                }
 
                if (request_width > 0 && request_height > 0) {
@@ -1190,6 +1213,14 @@ ev_window_refresh_window_thumbnail (EvWindow *ev_window)
        ev_job_scheduler_push_job (ev_window->priv->thumbnail_job, EV_JOB_PRIORITY_NONE);
 }
 
+static void
+override_restrictions_changed (GSettings *settings,
+                              gchar     *key,
+                              EvWindow  *ev_window)
+{
+       ev_window_setup_action_sensitivity (ev_window);
+}
+
 #ifdef WITH_GCONF
 static void
 lockdown_changed (GConfClient *client,
@@ -1209,13 +1240,19 @@ ev_window_setup_document (EvWindow *ev_window)
        GtkAction *action;
 
        ev_window->priv->setup_document_idle = 0;
-       
+
        ev_window_refresh_window_thumbnail (ev_window);
 
        ev_window_set_page_mode (ev_window, PAGE_MODE_DOCUMENT);
        ev_window_title_set_document (ev_window->priv->title, document);
        ev_window_title_set_uri (ev_window->priv->title, ev_window->priv->uri);
 
+       ev_window->priv->settings = g_settings_new (GS_SCHEMA_NAME);
+       g_signal_connect (ev_window->priv->settings,
+                         "changed::"GS_OVERRIDE_RESTRICTIONS,
+                         G_CALLBACK (override_restrictions_changed),
+                         ev_window);
+
 #ifdef WITH_GCONF
        if (!ev_window->priv->gconf_client)
                ev_window->priv->gconf_client = gconf_client_get_default ();
@@ -1223,18 +1260,10 @@ ev_window_setup_document (EvWindow *ev_window)
                              GCONF_LOCKDOWN_DIR,
                              GCONF_CLIENT_PRELOAD_ONELEVEL,
                              NULL);
-       gconf_client_add_dir (ev_window->priv->gconf_client,
-                             GCONF_OVERRIDE_RESTRICTIONS,
-                             GCONF_CLIENT_PRELOAD_NONE,
-                             NULL);
        gconf_client_notify_add (ev_window->priv->gconf_client,
                                 GCONF_LOCKDOWN_DIR,
                                 (GConfClientNotifyFunc)lockdown_changed,
                                 ev_window, NULL, NULL);
-       gconf_client_notify_add (ev_window->priv->gconf_client,
-                                GCONF_OVERRIDE_RESTRICTIONS,
-                                (GConfClientNotifyFunc)lockdown_changed,
-                                ev_window, NULL, NULL);
 #endif /* WITH_GCONF */
 
        ev_window_setup_action_sensitivity (ev_window);
@@ -1272,6 +1301,8 @@ ev_window_set_document (EvWindow *ev_window, EvDocument *document)
                g_object_unref (ev_window->priv->document);
        ev_window->priv->document = g_object_ref (document);
 
+       ev_window_update_max_min_scale (ev_window);
+
        ev_window_set_message_area (ev_window, NULL);
 
        if (ev_document_get_n_pages (document) <= 0) {
@@ -1282,6 +1313,12 @@ ev_window_set_document (EvWindow *ev_window, EvDocument *document)
                                           _("The document contains only empty pages"));
        }
 
+       if (EV_WINDOW_IS_PRESENTATION (ev_window)) {
+               gtk_widget_destroy (ev_window->priv->presentation_view);
+               ev_window->priv->presentation_view = NULL;
+               ev_window_run_presentation (ev_window);
+       }
+
        if (ev_window->priv->setup_document_idle > 0)
                g_source_remove (ev_window->priv->setup_document_idle);
 
@@ -1839,7 +1876,6 @@ ev_window_open_uri (EvWindow       *ev_window,
        ev_window_close_dialogs (ev_window);
        ev_window_clear_load_job (ev_window);
        ev_window_clear_local_uri (ev_window);
-       ev_view_set_loading (EV_VIEW (ev_window->priv->view), TRUE);
 
        ev_window->priv->window_mode = mode;
 
@@ -1851,7 +1887,8 @@ ev_window_open_uri (EvWindow       *ev_window,
                g_object_unref (ev_window->priv->metadata);
 
        source_file = g_file_new_for_uri (uri);
-       if (ev_is_metadata_supported_for_file (source_file))
+       if (!ev_file_is_temp (source_file) &&
+           ev_is_metadata_supported_for_file (source_file))
                ev_window->priv->metadata = ev_metadata_new (source_file);
        else
                ev_window->priv->metadata = NULL;
@@ -1877,6 +1914,7 @@ ev_window_open_uri (EvWindow       *ev_window,
        if (!g_file_is_native (source_file) && !ev_window->priv->local_uri) {
                ev_window_load_file_remote (ev_window, source_file);
        } else {
+               ev_view_set_loading (EV_VIEW (ev_window->priv->view), TRUE);
                g_object_unref (source_file);
                ev_job_scheduler_push_job (ev_window->priv->load_job, EV_JOB_PRIORITY_NONE);
        }
@@ -2402,12 +2440,8 @@ ev_window_setup_recent (EvWindow *ev_window)
                action = g_object_new (GTK_TYPE_ACTION,
                                       "name", action_name,
                                       "label", label,
-#if GTK_CHECK_VERSION (2, 16, 0)
                                        "gicon", icon,
-#endif
-#if GTK_CHECK_VERSION (2, 20, 0)
                                        "always-show-image", TRUE,
-#endif
                                       NULL);
 
                g_object_set_data_full (G_OBJECT (action),
@@ -2614,6 +2648,8 @@ ev_window_save_job_cb (EvJob     *job,
                ev_window_error_message (window, job->error,
                                         _("The file could not be saved as ā€œ%sā€."),
                                         EV_JOB_SAVE (job)->uri);
+       } else {
+               ev_window_add_recent (window, EV_JOB_SAVE (job)->uri);
        }
 
        ev_window_clear_save_job (window);
@@ -2771,21 +2807,25 @@ ev_window_save_print_settings (EvWindow         *window,
 
        key_file = get_print_settings_file ();
        gtk_print_settings_to_key_file (print_settings, key_file, EV_PRINT_SETTINGS_GROUP);
-       save_print_setting_file (key_file);
-       g_key_file_free (key_file);
-
-       if (!window->priv->metadata)
-               return;
 
        /* Save print settings that are specific to the document */
        for (i = 0; i < G_N_ELEMENTS (document_print_settings); i++) {
-               const gchar *value;
+               /* Remove it from global settings */
+               g_key_file_remove_key (key_file, EV_PRINT_SETTINGS_GROUP,
+                                      document_print_settings[i], NULL);
 
-               value = gtk_print_settings_get (print_settings,
-                                               document_print_settings[i]);
-               ev_metadata_set_string (window->priv->metadata,
-                                       document_print_settings[i], value);
+               if (window->priv->metadata) {
+                       const gchar *value;
+
+                       value = gtk_print_settings_get (print_settings,
+                                                       document_print_settings[i]);
+                       ev_metadata_set_string (window->priv->metadata,
+                                               document_print_settings[i], value);
+               }
        }
+
+       save_print_setting_file (key_file);
+       g_key_file_free (key_file);
 }
 
 static void
@@ -2796,6 +2836,19 @@ ev_window_save_print_page_setup (EvWindow     *window,
 
        key_file = get_print_settings_file ();
        gtk_page_setup_to_key_file (page_setup, key_file, EV_PAGE_SETUP_GROUP);
+
+       /* Do not save document settings in global file */
+       g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
+                              "page-setup-orientation", NULL);
+       g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
+                              "page-setup-margin-top", NULL);
+       g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
+                              "page-setup-margin-bottom", NULL);
+       g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
+                              "page-setup-margin-left", NULL);
+       g_key_file_remove_key (key_file, EV_PAGE_SETUP_GROUP,
+                              "page-setup-margin-right", NULL);
+
        save_print_setting_file (key_file);
        g_key_file_free (key_file);
 
@@ -3718,23 +3771,47 @@ ev_window_setup_gtk_settings (EvWindow *window)
        g_free (menubar_accel_accel);
 }
 
+static void
+ev_window_update_max_min_scale (EvWindow *window)
+{
+       gdouble    dpi;
+       GtkAction *action;
+       gdouble    min_width, min_height;
+       gdouble    width, height;
+       gdouble    max_scale;
+       gint       rotation = ev_document_model_get_rotation (window->priv->model);
+
+       if (!window->priv->document)
+               return;
+
+       dpi = get_screen_dpi (window) / 72.0;
+
+       ev_document_get_min_page_size (window->priv->document, &min_width, &min_height);
+       width = (rotation == 0 || rotation == 180) ? min_width : min_height;
+       height = (rotation == 0 || rotation == 180) ? min_height : min_width;
+       max_scale = sqrt (PAGE_CACHE_SIZE / (width * dpi * 4 * height * dpi));
+
+       action = gtk_action_group_get_action (window->priv->action_group,
+                                             ZOOM_CONTROL_ACTION);
+       ephy_zoom_action_set_max_zoom_level (EPHY_ZOOM_ACTION (action), max_scale * dpi);
+
+       ev_document_model_set_min_scale (window->priv->model, MIN_SCALE * dpi);
+       ev_document_model_set_max_scale (window->priv->model, max_scale * dpi);
+}
+
 static void
 ev_window_screen_changed (GtkWidget *widget,
                          GdkScreen *old_screen)
 {
        EvWindow *window = EV_WINDOW (widget);
-       EvWindowPrivate *priv = window->priv;
        GdkScreen *screen;
-       gdouble dpi;
 
        screen = gtk_widget_get_screen (widget);
        if (screen == old_screen)
                return;
 
        ev_window_setup_gtk_settings (window);
-       dpi = get_screen_dpi (window);
-       ev_document_model_set_min_scale (priv->model, MIN_SCALE * dpi / 72.0);
-       ev_document_model_set_max_scale (priv->model, MAX_SCALE * dpi / 72.0);
+       ev_window_update_max_min_scale (window);
 
        if (GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed) {
                GTK_WIDGET_CLASS (ev_window_parent_class)->screen_changed (widget, old_screen);
@@ -4152,6 +4229,7 @@ ev_window_rotation_changed_cb (EvDocumentModel *model,
                ev_metadata_set_int (window->priv->metadata, "rotation",
                                     rotation);
 
+       ev_window_update_max_min_scale (window);
        ev_window_refresh_window_thumbnail (window);
 }
 
@@ -4297,6 +4375,8 @@ ev_window_view_toolbar_cb (GtkAction *action, EvWindow *ev_window)
        update_chrome_visibility (ev_window);
        if (ev_window->priv->metadata)
                ev_metadata_set_boolean (ev_window->priv->metadata, "show_toolbar", active);
+       if (ev_window->priv->document)
+               g_settings_set_boolean (ev_window->priv->last_settings, "show-toolbar", active);
 }
 
 static void
@@ -4355,6 +4435,8 @@ ev_window_sidebar_visibility_changed_cb (EvSidebar  *ev_sidebar,
                if (ev_window->priv->metadata)
                        ev_metadata_set_boolean (ev_window->priv->metadata, "sidebar_visibility",
                                                 visible);
+               if (ev_window->priv->document)
+                       g_settings_set_boolean (ev_window->priv->last_settings, "show-sidebar", visible);
        }
 }
 
@@ -4785,6 +4867,20 @@ ev_window_dispose (GObject *object)
                                                      window);
        }
 
+#ifdef ENABLE_DBUS
+       if (priv->dbus_object_id > 0) {
+               ev_window_emit_closed (window);
+               g_dbus_connection_unregister_object (ev_application_get_dbus_connection (EV_APP),
+                                                    priv->dbus_object_id);
+               priv->dbus_object_id = 0;
+       }
+
+       if (priv->dbus_object_path) {
+               g_free (priv->dbus_object_path);
+               priv->dbus_object_path = NULL;
+       }
+#endif /* ENABLE_DBUS */
+
        if (priv->metadata) {
                g_object_unref (priv->metadata);
                priv->metadata = NULL;
@@ -4844,6 +4940,17 @@ ev_window_dispose (GObject *object)
                priv->recent_manager = NULL;
        }
 
+       if (priv->settings) {
+               g_object_unref (priv->settings);
+               priv->settings = NULL;
+       }
+
+       if (priv->last_settings) {
+               g_settings_apply (priv->last_settings);
+               g_object_unref (priv->last_settings);
+               priv->last_settings = NULL;
+       }
+
        priv->recent_ui_id = 0;
 
        if (priv->model) {
@@ -5460,13 +5567,13 @@ window_configure_event_cb (EvWindow *window, GdkEventConfigure *event, gpointer
        state = gdk_window_get_state (gtk_widget_get_window (GTK_WIDGET (window)));
 
        if (!(state & GDK_WINDOW_STATE_FULLSCREEN)) {
-               if (!ev_window_is_empty (window) && window->priv->document) {
+               if (window->priv->document) {
                        ev_document_get_max_page_size (window->priv->document,
                                                       &document_width, &document_height);
-                       ev_metadata_set_double (window->priv->metadata, "window_width_ratio",
-                                               (double)event->width / document_width);
-                       ev_metadata_set_double (window->priv->metadata, "window_height_ratio",
-                                               (double)event->height / document_height);
+                       g_settings_set (window->priv->last_settings, "window-ratio", "(dd)",
+                                       (double)event->width / document_width,
+                                       (double)event->height / document_height);
+
                        ev_metadata_set_int (window->priv->metadata, "window_x", event->x);
                        ev_metadata_set_int (window->priv->metadata, "window_y", event->y);
                        ev_metadata_set_int (window->priv->metadata, "window_width", event->width);
@@ -6091,6 +6198,119 @@ get_toolbars_model (void)
        return toolbars_model;
 }
 
+#ifdef ENABLE_DBUS
+static void
+ev_window_sync_source (EvWindow     *window,
+                      EvSourceLink *link)
+{
+       GDBusConnection *connection;
+       GError          *error = NULL;
+
+       if (window->priv->dbus_object_id <= 0)
+               return;
+
+       connection = ev_application_get_dbus_connection (EV_APP);
+       if (!connection)
+               return;
+
+       g_dbus_connection_emit_signal (connection,
+                                      NULL,
+                                      window->priv->dbus_object_path,
+                                      EV_WINDOW_DBUS_INTERFACE,
+                                      "SyncSource",
+                                      g_variant_new ("(s(ii))",
+                                                     link->filename,
+                                                     link->line,
+                                                     link->col),
+                                      &error);
+       if (error) {
+               g_printerr ("Failed to emit DBus signal SyncSource: %s\n",
+                           error->message);
+               g_error_free (error);
+       }
+}
+
+static void
+ev_window_emit_closed (EvWindow *window)
+{
+       GDBusConnection *connection;
+       GError          *error = NULL;
+
+       if (window->priv->dbus_object_id <= 0)
+               return;
+
+       connection = ev_application_get_dbus_connection (EV_APP);
+       if (!connection)
+               return;
+
+       /* TODO: figure out if this is the last window and use
+        * g_dbus_connection_flush_sync() to make sure the signal
+        * is emitted.
+        */
+       g_dbus_connection_emit_signal (connection,
+                                      NULL,
+                                      window->priv->dbus_object_path,
+                                      EV_WINDOW_DBUS_INTERFACE,
+                                      "Closed",
+                                      NULL,
+                                      &error);
+       if (error) {
+               g_printerr ("Failed to emit DBus signal Closed: %s\n",
+                           error->message);
+               g_error_free (error);
+       }
+}
+
+static void
+method_call_cb (GDBusConnection       *connection,
+                const gchar           *sender,
+                const gchar           *object_path,
+                const gchar           *interface_name,
+                const gchar           *method_name,
+                GVariant              *parameters,
+                GDBusMethodInvocation *invocation,
+                gpointer               user_data)
+{
+        EvWindow *window = EV_WINDOW (user_data);
+
+        if (g_strcmp0 (method_name, "SyncView") != 0)
+                return;
+
+       if (window->priv->document && ev_document_has_synctex (window->priv->document)) {
+               EvSourceLink link;
+
+               g_variant_get (parameters, "(&s(ii))", &link.filename, &link.line, &link.col);
+               ev_view_highlight_forward_search (EV_VIEW (window->priv->view), &link);
+               gtk_window_present (GTK_WINDOW (window));
+       }
+
+       g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
+}
+
+static const char introspection_xml[] =
+        "<node>"
+          "<interface name='org.gnome.evince.Window'>"
+            "<method name='SyncView'>"
+              "<arg type='s' name='source_file' direction='in'/>"
+              "<arg type='(ii)' name='source_point' direction='in'/>"
+            "</method>"
+           "<signal name='SyncSource'>"
+             "<arg type='s' name='source_file' direction='out'/>"
+             "<arg type='(ii)' name='source_point' direction='out'/>"
+           "</signal>"
+            "<signal name='Closed'/>"
+          "</interface>"
+        "</node>";
+
+static const GDBusInterfaceVTable interface_vtable = {
+       method_call_cb,
+       NULL,
+       NULL
+};
+
+static GDBusNodeInfo *introspection_data;
+#endif /* ENABLE_DBUS */
+
 static void
 ev_window_init (EvWindow *ev_window)
 {
@@ -6102,7 +6322,10 @@ ev_window_init (EvWindow *ev_window)
        EggToolbarsModel *toolbars_model;
        GObject *mpkeys;
        gchar *ui_path;
-       gdouble dpi;
+#ifdef ENABLE_DBUS
+       GDBusConnection *connection;
+       static gint window_id = 0;
+#endif
 
        g_signal_connect (ev_window, "configure_event",
                          G_CALLBACK (window_configure_event_cb), NULL);
@@ -6111,6 +6334,35 @@ ev_window_init (EvWindow *ev_window)
 
        ev_window->priv = EV_WINDOW_GET_PRIVATE (ev_window);
 
+#ifdef ENABLE_DBUS
+       connection = ev_application_get_dbus_connection (EV_APP);
+        if (connection) {
+               if (!introspection_data) {
+                       introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, &error);
+                       if (error) g_warning ("%s\n", error->message);
+               }
+                g_assert (introspection_data != NULL);
+
+               ev_window->priv->dbus_object_path = g_strdup_printf (EV_WINDOW_DBUS_OBJECT_PATH, window_id++);
+                ev_window->priv->dbus_object_id =
+                    g_dbus_connection_register_object (connection,
+                                                       ev_window->priv->dbus_object_path,
+                                                       introspection_data->interfaces[0],
+                                                       &interface_vtable,
+                                                       ev_window, NULL,
+                                                       &error);
+                if (ev_window->priv->dbus_object_id == 0) {
+                        g_printerr ("Failed to register bus object %s: %s\n",
+                                   ev_window->priv->dbus_object_path, error->message);
+                        g_error_free (error);
+                       g_free (ev_window->priv->dbus_object_path);
+                       ev_window->priv->dbus_object_path = NULL;
+                       error = NULL;
+                }
+        }
+
+#endif /* ENABLE_DBUS */
+
        ev_window->priv->model = ev_document_model_new ();
 
        ev_window->priv->page_mode = PAGE_MODE_DOCUMENT;
@@ -6288,10 +6540,9 @@ ev_window_init (EvWindow *ev_window)
        gtk_widget_show (ev_window->priv->view_box);
 
        ev_window->priv->view = ev_view_new ();
+       ev_view_set_page_cache_size (EV_VIEW (ev_window->priv->view), PAGE_CACHE_SIZE);
        ev_view_set_model (EV_VIEW (ev_window->priv->view), ev_window->priv->model);
-       dpi = get_screen_dpi (ev_window);
-       ev_document_model_set_min_scale (ev_window->priv->model, MIN_SCALE * dpi / 72.0);
-       ev_document_model_set_max_scale (ev_window->priv->model, MAX_SCALE * dpi / 72.0);
+
        ev_window->priv->password_view = ev_password_view_new (GTK_WINDOW (ev_window));
        g_signal_connect_swapped (ev_window->priv->password_view,
                                  "unlock",
@@ -6315,6 +6566,11 @@ ev_window_init (EvWindow *ev_window)
        g_signal_connect_object (ev_window->priv->view, "selection-changed",
                                 G_CALLBACK (view_selection_changed_cb),
                                 ev_window, 0);
+#ifdef ENABLE_DBUS
+       g_signal_connect_swapped (ev_window->priv->view, "sync-source",
+                                 G_CALLBACK (ev_window_sync_source),
+                                 ev_window);
+#endif
        gtk_widget_show (ev_window->priv->view);
        gtk_widget_show (ev_window->priv->password_view);
 
@@ -6425,6 +6681,9 @@ ev_window_init (EvWindow *ev_window)
        /* Give focus to the document view */
        gtk_widget_grab_focus (ev_window->priv->view);
 
+       ev_window->priv->last_settings = g_settings_new (GS_SCHEMA_NAME".Default");
+       g_settings_delay (ev_window->priv->last_settings);
+
        /* Set it user interface params */
        ev_window_setup_recent (ev_window);
 
@@ -6468,3 +6727,4 @@ ev_window_new (void)
 
        return ev_window;
 }
+