+ev_window_add_recent (EvWindow *window, const char *filename)
+{
+ gtk_recent_manager_add_item (window->priv->recent_manager, filename);
+}
+
+static gint
+compare_recent_items (GtkRecentInfo *a, GtkRecentInfo *b)
+{
+ gboolean has_ev_a, has_ev_b;
+ const gchar *evince = g_get_application_name ();
+
+ has_ev_a = gtk_recent_info_has_application (a, evince);
+ has_ev_b = gtk_recent_info_has_application (b, evince);
+
+ if (has_ev_a && has_ev_b) {
+ time_t time_a, time_b;
+
+ time_a = gtk_recent_info_get_modified (a);
+ time_b = gtk_recent_info_get_modified (b);
+
+ return (time_b - time_a);
+ } else if (has_ev_a) {
+ return -1;
+ } else if (has_ev_b) {
+ return 1;
+ }
+
+ return 0;
+}
+
+/*
+ * Doubles underscore to avoid spurious menu accels.
+ */
+static gchar *
+ev_window_get_recent_file_label (gint index, const gchar *filename)
+{
+ GString *str;
+ gint length;
+ const gchar *p;
+ const gchar *end;
+ gboolean is_rtl;
+
+ is_rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);
+
+ g_return_val_if_fail (filename != NULL, NULL);
+
+ length = strlen (filename);
+ str = g_string_sized_new (length + 10);
+ g_string_printf (str, "%s_%d. ", is_rtl ? "\xE2\x80\x8F" : "", index);
+
+ p = filename;
+ end = filename + length;
+
+ while (p != end) {
+ const gchar *next;
+ next = g_utf8_next_char (p);
+
+ switch (*p) {
+ case '_':
+ g_string_append (str, "__");
+ break;
+ default:
+ g_string_append_len (str, p, next - p);
+ break;
+ }
+
+ p = next;
+ }
+
+ return g_string_free (str, FALSE);
+}
+
+static void
+ev_window_setup_recent (EvWindow *ev_window)
+{
+ GList *items, *l;
+ guint n_items = 0;
+ const gchar *evince = g_get_application_name ();
+ static guint i = 0;
+
+ if (ev_window->priv->recent_ui_id > 0) {
+ gtk_ui_manager_remove_ui (ev_window->priv->ui_manager,
+ ev_window->priv->recent_ui_id);
+ gtk_ui_manager_ensure_update (ev_window->priv->ui_manager);
+ }
+ ev_window->priv->recent_ui_id = gtk_ui_manager_new_merge_id (ev_window->priv->ui_manager);
+
+ if (ev_window->priv->recent_action_group) {
+ gtk_ui_manager_remove_action_group (ev_window->priv->ui_manager,
+ ev_window->priv->recent_action_group);
+ g_object_unref (ev_window->priv->recent_action_group);
+ }
+ ev_window->priv->recent_action_group = gtk_action_group_new ("RecentFilesActions");
+ gtk_ui_manager_insert_action_group (ev_window->priv->ui_manager,
+ ev_window->priv->recent_action_group, 0);
+
+ items = gtk_recent_manager_get_items (ev_window->priv->recent_manager);
+ items = g_list_sort (items, (GCompareFunc) compare_recent_items);
+
+ for (l = items; l && l->data; l = g_list_next (l)) {
+ GtkRecentInfo *info;
+ GtkAction *action;
+ gchar *action_name;
+ gchar *label;
+
+ info = (GtkRecentInfo *) l->data;
+
+ if (!gtk_recent_info_has_application (info, evince) ||
+ (gtk_recent_info_is_local (info) && !gtk_recent_info_exists (info)))
+ continue;
+
+ action_name = g_strdup_printf ("RecentFile%u", i++);
+ label = ev_window_get_recent_file_label (
+ n_items + 1, gtk_recent_info_get_display_name (info));
+
+ action = g_object_new (GTK_TYPE_ACTION,
+ "name", action_name,
+ "label", label,
+ NULL);
+
+ g_object_set_data_full (G_OBJECT (action),
+ "gtk-recent-info",
+ gtk_recent_info_ref (info),
+ (GDestroyNotify) gtk_recent_info_unref);
+
+ g_signal_connect (action, "activate",
+ G_CALLBACK (ev_window_cmd_recent_file_activate),
+ (gpointer) ev_window);
+
+ gtk_action_group_add_action (ev_window->priv->recent_action_group,
+ action);
+ g_object_unref (action);
+
+ gtk_ui_manager_add_ui (ev_window->priv->ui_manager,
+ ev_window->priv->recent_ui_id,
+ "/MainMenu/FileMenu/RecentFilesMenu",
+ label,
+ action_name,
+ GTK_UI_MANAGER_MENUITEM,
+ FALSE);
+ g_free (action_name);
+ g_free (label);
+
+ if (++n_items == 5)
+ break;
+ }
+
+ g_list_foreach (items, (GFunc) gtk_recent_info_unref, NULL);
+ g_list_free (items);
+}
+
+static gboolean
+show_saving_progress (GFile *dst)
+{
+ EvWindow *ev_window;
+ GtkWidget *area;
+ gchar *text;
+ gchar *uri;
+ EvSaveType save_type;
+
+ ev_window = EV_WINDOW (g_object_get_data (G_OBJECT (dst), "ev-window"));
+ ev_window->priv->progress_idle = 0;
+
+ if (ev_window->priv->message_area)
+ return FALSE;
+
+ save_type = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dst), "save-type"));
+ uri = g_file_get_uri (dst);
+ switch (save_type) {
+ case EV_SAVE_DOCUMENT:
+ text = g_strdup_printf (_("Saving document to %s"), uri);
+ break;
+ case EV_SAVE_ATTACHMENT:
+ text = g_strdup_printf (_("Saving attachment to %s"), uri);
+ break;
+ case EV_SAVE_IMAGE:
+ text = g_strdup_printf (_("Saving image to %s"), uri);
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+ g_free (uri);
+ area = ev_progress_message_area_new (GTK_STOCK_SAVE,
+ text,
+ GTK_STOCK_CLOSE,
+ GTK_RESPONSE_CLOSE,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ NULL);
+ g_signal_connect (area, "response",
+ G_CALLBACK (ev_window_progress_response_cb),
+ ev_window);
+ gtk_widget_show (area);
+ ev_window_set_message_area (ev_window, area);
+ g_free (text);
+
+ return FALSE;
+}
+
+static void
+window_save_file_copy_ready_cb (GFile *src,
+ GAsyncResult *async_result,
+ GFile *dst)
+{
+ EvWindow *ev_window;
+ GError *error = NULL;
+
+ ev_window = EV_WINDOW (g_object_get_data (G_OBJECT (dst), "ev-window"));
+ ev_window_clear_progress_idle (ev_window);
+
+ if (g_file_copy_finish (src, async_result, &error)) {
+ ev_tmp_file_unlink (src);
+ return;
+ }
+
+ if (error->domain != G_IO_ERROR ||
+ error->code != G_IO_ERROR_CANCELLED) {
+ gchar *name;
+
+ name = g_file_get_basename (dst);
+ ev_window_error_message (ev_window, error,
+ _("The file could not be saved as “%s”."),
+ name);
+ g_free (name);
+ }
+ ev_tmp_file_unlink (src);
+ g_error_free (error);
+}
+
+static void
+window_save_file_copy_progress_cb (goffset n_bytes,
+ goffset total_bytes,
+ GFile *dst)
+{
+ EvWindow *ev_window;
+ EvSaveType save_type;
+ gchar *status;
+ gdouble fraction;
+
+ ev_window = EV_WINDOW (g_object_get_data (G_OBJECT (dst), "ev-window"));
+
+ if (!ev_window->priv->message_area)
+ return;
+
+ fraction = n_bytes / (gdouble)total_bytes;
+ save_type = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dst), "save-type"));
+
+ switch (save_type) {
+ case EV_SAVE_DOCUMENT:
+ status = g_strdup_printf (_("Uploading document (%d%%)"),
+ (gint)(fraction * 100));
+ break;
+ case EV_SAVE_ATTACHMENT:
+ status = g_strdup_printf (_("Uploading attachment (%d%%)"),
+ (gint)(fraction * 100));
+ break;
+ case EV_SAVE_IMAGE:
+ status = g_strdup_printf (_("Uploading image (%d%%)"),
+ (gint)(fraction * 100));
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ ev_progress_message_area_set_status (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
+ status);
+ ev_progress_message_area_set_fraction (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
+ fraction);
+
+ g_free (status);
+}
+
+static void
+ev_window_save_remote (EvWindow *ev_window,
+ EvSaveType save_type,
+ GFile *src,
+ GFile *dst)
+{
+ ev_window_reset_progress_cancellable (ev_window);
+ g_object_set_data (G_OBJECT (dst), "ev-window", ev_window);
+ g_object_set_data (G_OBJECT (dst), "save-type", GINT_TO_POINTER (save_type));
+ g_file_copy_async (src, dst,
+ G_FILE_COPY_OVERWRITE,
+ G_PRIORITY_DEFAULT,
+ ev_window->priv->progress_cancellable,
+ (GFileProgressCallback)window_save_file_copy_progress_cb,
+ dst,
+ (GAsyncReadyCallback)window_save_file_copy_ready_cb,
+ dst);
+ ev_window->priv->progress_idle =
+ g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
+ 1,
+ (GSourceFunc)show_saving_progress,
+ dst,
+ NULL);
+}
+
+static void
+ev_window_clear_save_job (EvWindow *ev_window)
+{
+ if (ev_window->priv->save_job != NULL) {
+ if (!ev_job_is_finished (ev_window->priv->save_job))
+ ev_job_cancel (ev_window->priv->save_job);
+
+ g_signal_handlers_disconnect_by_func (ev_window->priv->save_job,
+ ev_window_save_job_cb,
+ ev_window);
+ g_object_unref (ev_window->priv->save_job);
+ ev_window->priv->save_job = NULL;
+ }
+}
+
+static void
+ev_window_save_job_cb (EvJob *job,
+ EvWindow *window)
+{
+ if (ev_job_is_failed (job)) {
+ ev_window_error_message (window, job->error,
+ _("The file could not be saved as “%s”."),
+ EV_JOB_SAVE (job)->uri);
+ }
+
+ ev_window_clear_save_job (window);
+}
+
+static void
+file_save_dialog_response_cb (GtkWidget *fc,
+ gint response_id,
+ EvWindow *ev_window)
+{
+ gchar *uri;
+
+ if (response_id != GTK_RESPONSE_OK) {
+ gtk_widget_destroy (fc);
+ return;
+ }
+
+ uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
+ /* FIXME: remote copy should be done here rather than in the save job,
+ * so that we can track progress and cancel the operation
+ */
+
+ ev_window_clear_save_job (ev_window);
+ ev_window->priv->save_job = ev_job_save_new (ev_window->priv->document,
+ uri, ev_window->priv->uri);
+ g_signal_connect (ev_window->priv->save_job, "finished",
+ G_CALLBACK (ev_window_save_job_cb),
+ ev_window);
+ /* The priority doesn't matter for this job */
+ ev_job_scheduler_push_job (ev_window->priv->save_job, EV_JOB_PRIORITY_NONE);
+
+ g_free (uri);
+ gtk_widget_destroy (fc);
+}
+
+static void
+ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
+{
+ GtkWidget *fc;
+ gchar *base_name;
+ GFile *file;
+ const gchar *folder;
+
+ fc = gtk_file_chooser_dialog_new (
+ _("Save a Copy"),
+ GTK_WINDOW (ev_window), GTK_FILE_CHOOSER_ACTION_SAVE,
+ GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+ GTK_STOCK_SAVE, GTK_RESPONSE_OK,
+ NULL);
+
+ ev_document_factory_add_filters (fc, ev_window->priv->document);
+ gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
+
+ gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
+ gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
+ file = g_file_new_for_uri (ev_window->priv->uri);
+ base_name = g_file_get_basename (file);
+ gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc), base_name);
+
+ folder = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
+ gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (fc),
+ folder ? folder : g_get_home_dir ());
+
+ g_object_unref (file);
+ g_free (base_name);
+
+ g_signal_connect (fc, "response",
+ G_CALLBACK (file_save_dialog_response_cb),
+ ev_window);
+
+ gtk_widget_show (fc);
+}
+
+static void
+ev_window_load_print_settings_from_metadata (EvWindow *window)
+{
+ gchar *uri = window->priv->uri;
+ gint i;
+
+ /* Load print setting that are specific to the document */
+ for (i = 0; i < G_N_ELEMENTS (document_print_settings); i++) {
+ GValue value = { 0, };
+ gboolean success;
+
+ success = ev_metadata_manager_get (uri, document_print_settings[i], &value, TRUE);
+ gtk_print_settings_set (window->priv->print_settings,
+ document_print_settings[i],
+ success ? g_value_get_string (&value) : NULL);
+ if (success)
+ g_value_unset (&value);
+ }
+}
+
+static void
+ev_window_save_print_settings (EvWindow *window)
+{
+ gchar *uri = window->priv->uri;
+ gint i;
+
+ /* Save print settings that are specific to the document */
+ for (i = 0; i < G_N_ELEMENTS (document_print_settings); i++) {
+ const gchar *value;
+
+ value = gtk_print_settings_get (window->priv->print_settings,
+ document_print_settings[i]);
+ ev_metadata_manager_set_string (uri, document_print_settings[i], value);
+ }
+}
+
+static void
+ev_window_save_print_page_setup (EvWindow *window)
+{
+ gchar *uri = window->priv->uri;
+ GtkPageSetup *page_setup = window->priv->print_page_setup;
+
+ /* Save page setup options that are specific to the document */
+ ev_metadata_manager_set_int (uri, "page-setup-orientation",
+ gtk_page_setup_get_orientation (page_setup));
+ ev_metadata_manager_set_double (uri, "page-setup-margin-top",
+ gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
+ ev_metadata_manager_set_double (uri, "page-setup-margin-bottom",
+ gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
+ ev_metadata_manager_set_double (uri, "page-setup-margin-left",
+ gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
+ ev_metadata_manager_set_double (uri, "page-setup-margin-right",
+ gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
+}
+
+static void
+ev_window_load_print_page_setup_from_metadata (EvWindow *window)
+{
+ gchar *uri = window->priv->uri;
+ GtkPageSetup *page_setup = window->priv->print_page_setup;
+ GtkPaperSize *paper_size;
+ GValue value = { 0, };
+
+ paper_size = gtk_page_setup_get_paper_size (page_setup);
+
+ /* Load page setup options that are specific to the document */
+ if (ev_metadata_manager_get (uri, "page-setup-orientation", &value, TRUE)) {
+ gtk_page_setup_set_orientation (page_setup, g_value_get_int (&value));
+ g_value_unset (&value);
+ } else {
+ gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_PORTRAIT);
+ }
+
+ if (ev_metadata_manager_get (uri, "page-setup-margin-top", &value, TRUE)) {
+ gtk_page_setup_set_top_margin (page_setup, g_value_get_double (&value), GTK_UNIT_MM);
+ g_value_unset (&value);
+ } else {
+ gtk_page_setup_set_top_margin (page_setup,
+ gtk_paper_size_get_default_top_margin (paper_size, GTK_UNIT_MM),
+ GTK_UNIT_MM);
+ }
+
+ if (ev_metadata_manager_get (uri, "page-setup-margin-bottom", &value, TRUE)) {
+ gtk_page_setup_set_bottom_margin (page_setup, g_value_get_double (&value), GTK_UNIT_MM);
+ g_value_unset (&value);
+ } else {
+ gtk_page_setup_set_bottom_margin (page_setup,
+ gtk_paper_size_get_default_bottom_margin (paper_size, GTK_UNIT_MM),
+ GTK_UNIT_MM);
+ }
+
+ if (ev_metadata_manager_get (uri, "page-setup-margin-left", &value, TRUE)) {
+ gtk_page_setup_set_left_margin (page_setup, g_value_get_double (&value), GTK_UNIT_MM);
+ g_value_unset (&value);
+ } else {
+ gtk_page_setup_set_left_margin (page_setup,
+ gtk_paper_size_get_default_left_margin (paper_size, GTK_UNIT_MM),
+ GTK_UNIT_MM);
+ }
+
+ if (ev_metadata_manager_get (uri, "page-setup-margin-right", &value, TRUE)) {
+ gtk_page_setup_set_right_margin (page_setup, g_value_get_double (&value), GTK_UNIT_MM);
+ g_value_unset (&value);
+ } else {
+ gtk_page_setup_set_right_margin (page_setup,
+ gtk_paper_size_get_default_right_margin (paper_size, GTK_UNIT_MM),
+ GTK_UNIT_MM);
+ }
+}
+
+static void
+ev_window_print_page_setup_done_cb (GtkPageSetup *page_setup,
+ EvWindow *window)
+{
+ /* Dialog was canceled */
+ if (!page_setup)
+ return;
+
+ if (window->priv->print_page_setup != page_setup) {
+ if (window->priv->print_page_setup)
+ g_object_unref (window->priv->print_page_setup);
+ window->priv->print_page_setup = g_object_ref (page_setup);
+ }
+
+ ev_application_set_page_setup (EV_APP, page_setup);
+ ev_window_save_print_page_setup (window);
+}
+
+static void
+ev_window_cmd_file_print_setup (GtkAction *action, EvWindow *ev_window)
+{
+ if (!ev_window->priv->print_page_setup) {
+ ev_window->priv->print_page_setup = gtk_page_setup_copy (
+ ev_application_get_page_setup (EV_APP));
+ ev_window_load_print_page_setup_from_metadata (ev_window);
+ }
+
+ gtk_print_run_page_setup_dialog_async (
+ GTK_WINDOW (ev_window),
+ ev_window->priv->print_page_setup,
+ ev_window->priv->print_settings,
+ (GtkPageSetupDoneFunc) ev_window_print_page_setup_done_cb,
+ ev_window);
+}
+
+static void
+ev_window_print_cancel (EvWindow *ev_window)
+{
+ EvPrintOperation *op;
+
+ if (!ev_window->priv->print_queue)
+ return;
+
+ while ((op = g_queue_peek_tail (ev_window->priv->print_queue))) {
+ ev_print_operation_cancel (op);
+ }
+}
+
+static void
+ev_window_print_update_pending_jobs_message (EvWindow *ev_window,
+ gint n_jobs)
+{
+ gchar *text = NULL;
+
+ if (!EV_IS_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area) ||
+ !ev_window->priv->print_queue)
+ return;
+
+ if (n_jobs == 0) {
+ ev_window_set_message_area (ev_window, NULL);
+ return;
+ }
+
+ if (n_jobs > 1) {
+ text = g_strdup_printf (ngettext ("%d pending job in queue",
+ "%d pending jobs in queue",
+ n_jobs - 1), n_jobs - 1);
+ }
+
+ ev_message_area_set_secondary_text (EV_MESSAGE_AREA (ev_window->priv->message_area),
+ text);
+ g_free (text);
+}
+
+static gboolean
+destroy_window (GtkWidget *window)
+{
+ gtk_widget_destroy (window);
+
+ return FALSE;
+}
+
+static void
+ev_window_print_operation_done (EvPrintOperation *op,
+ GtkPrintOperationResult result,
+ EvWindow *ev_window)
+{
+ gint n_jobs;
+
+ switch (result) {
+ case GTK_PRINT_OPERATION_RESULT_APPLY: {
+ GtkPrintSettings *print_settings;
+
+ print_settings = ev_print_operation_get_print_settings (op);
+ if (ev_window->priv->print_settings != print_settings) {
+ if (ev_window->priv->print_settings)
+ g_object_unref (ev_window->priv->print_settings);
+ ev_window->priv->print_settings = g_object_ref (print_settings);
+ }
+
+ ev_application_set_print_settings (EV_APP, print_settings);
+ ev_window_save_print_settings (ev_window);
+ }
+
+ break;
+ case GTK_PRINT_OPERATION_RESULT_ERROR: {
+ GtkWidget *dialog;
+ GError *error = NULL;
+
+
+ ev_print_operation_get_error (op, &error);
+
+ /* The message area is already used by
+ * the printing progress, so it's better to
+ * use a popup dialog in this case
+ */
+ dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE,
+ "%s", _("Failed to print document"));
+ gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+ "%s", error->message);
+ g_signal_connect (dialog, "response",
+ G_CALLBACK (gtk_widget_destroy),
+ NULL);
+ gtk_widget_show (dialog);
+
+ g_error_free (error);
+ }
+ break;
+ case GTK_PRINT_OPERATION_RESULT_CANCEL:
+ default:
+ break;
+ }
+
+ g_queue_remove (ev_window->priv->print_queue, op);
+ g_object_unref (op);
+ n_jobs = g_queue_get_length (ev_window->priv->print_queue);
+ ev_window_print_update_pending_jobs_message (ev_window, n_jobs);
+
+ if (n_jobs == 0 && ev_window->priv->close_after_print)
+ g_idle_add ((GSourceFunc)destroy_window,
+ ev_window);
+}
+
+static void
+ev_window_print_progress_response_cb (EvProgressMessageArea *area,
+ gint response,
+ EvWindow *ev_window)
+{
+ if (response == GTK_RESPONSE_CANCEL) {
+ EvPrintOperation *op;
+
+ op = g_queue_peek_tail (ev_window->priv->print_queue);
+ ev_print_operation_cancel (op);
+ } else {
+ gtk_widget_hide (GTK_WIDGET (area));
+ }
+}
+
+static void
+ev_window_print_operation_status_changed (EvPrintOperation *op,
+ EvWindow *ev_window)
+{
+ const gchar *status;
+ gdouble fraction;
+
+ status = ev_print_operation_get_status (op);
+ fraction = ev_print_operation_get_progress (op);
+
+ if (!ev_window->priv->message_area) {
+ GtkWidget *area;
+ const gchar *job_name;
+ gchar *text;
+
+ job_name = ev_print_operation_get_job_name (op);
+ text = g_strdup_printf (_("Printing job “%s”"), job_name);
+
+ area = ev_progress_message_area_new (GTK_STOCK_PRINT,
+ text,
+ GTK_STOCK_CLOSE,
+ GTK_RESPONSE_CLOSE,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ NULL);
+ ev_window_print_update_pending_jobs_message (ev_window, 1);
+ g_signal_connect (area, "response",
+ G_CALLBACK (ev_window_print_progress_response_cb),
+ ev_window);
+ gtk_widget_show (area);
+ ev_window_set_message_area (ev_window, area);
+ g_free (text);
+ }
+
+ ev_progress_message_area_set_status (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
+ status);
+ ev_progress_message_area_set_fraction (EV_PROGRESS_MESSAGE_AREA (ev_window->priv->message_area),
+ fraction);
+}
+
+static void
+ev_window_print_operation_begin_print (EvPrintOperation *op,
+ EvWindow *ev_window)
+{
+ GtkPrintSettings *print_settings;
+
+ if (!ev_window->priv->print_queue)
+ ev_window->priv->print_queue = g_queue_new ();
+
+ g_queue_push_head (ev_window->priv->print_queue, op);
+ ev_window_print_update_pending_jobs_message (ev_window,
+ g_queue_get_length (ev_window->priv->print_queue));
+
+ if (ev_window->priv->print_settings)
+ g_object_unref (ev_window->priv->print_settings);
+ print_settings = ev_print_operation_get_print_settings (op);
+ ev_window->priv->print_settings = g_object_ref (print_settings);
+}
+
+void
+ev_window_print_range (EvWindow *ev_window,
+ gint first_page,
+ gint last_page)
+{
+ EvPrintOperation *op;
+ EvPageCache *page_cache;
+ gint current_page;
+ gint document_last_page;
+
+ g_return_if_fail (EV_IS_WINDOW (ev_window));
+ g_return_if_fail (ev_window->priv->document != NULL);
+
+ if (!ev_window->priv->print_queue)
+ ev_window->priv->print_queue = g_queue_new ();
+
+ op = ev_print_operation_new (ev_window->priv->document);
+ g_signal_connect (op, "begin_print",
+ G_CALLBACK (ev_window_print_operation_begin_print),
+ (gpointer)ev_window);
+ g_signal_connect (op, "status_changed",
+ G_CALLBACK (ev_window_print_operation_status_changed),
+ (gpointer)ev_window);
+ g_signal_connect (op, "done",
+ G_CALLBACK (ev_window_print_operation_done),
+ (gpointer)ev_window);
+
+ page_cache = ev_page_cache_get (ev_window->priv->document);
+ current_page = ev_page_cache_get_current_page (page_cache);
+ document_last_page = ev_page_cache_get_n_pages (page_cache);
+
+ if (!ev_window->priv->print_settings) {
+ ev_window->priv->print_settings = gtk_print_settings_copy (
+ ev_application_get_print_settings (EV_APP));
+ ev_window_load_print_settings_from_metadata (ev_window);
+ }
+
+ if (first_page != 1 || last_page != document_last_page) {
+ GtkPageRange range;
+
+ /* Ranges in GtkPrint are 0 - N */
+ range.start = first_page - 1;
+ range.end = last_page - 1;
+
+ gtk_print_settings_set_print_pages (ev_window->priv->print_settings,
+ GTK_PRINT_PAGES_RANGES);
+ gtk_print_settings_set_page_ranges (ev_window->priv->print_settings,
+ &range, 1);
+ }
+
+ ev_print_operation_set_job_name (op, gtk_window_get_title (GTK_WINDOW (ev_window)));
+ ev_print_operation_set_current_page (op, current_page);
+ ev_print_operation_set_print_settings (op, ev_window->priv->print_settings);
+ if (ev_window->priv->print_page_setup)
+ ev_print_operation_set_default_page_setup (op, ev_window->priv->print_page_setup);
+
+ ev_print_operation_run (op, GTK_WINDOW (ev_window));
+}
+
+static void
+ev_window_print (EvWindow *window)
+{
+ EvPageCache *page_cache;
+ gint last_page;
+
+ page_cache = ev_page_cache_get (window->priv->document);
+ last_page = ev_page_cache_get_n_pages (page_cache);
+
+ ev_window_print_range (window, 1, last_page);
+}
+
+static void
+ev_window_cmd_file_print (GtkAction *action, EvWindow *ev_window)
+{
+ ev_window_print (ev_window);
+}
+
+static void
+ev_window_cmd_file_properties (GtkAction *action, EvWindow *ev_window)
+{
+ if (ev_window->priv->properties == NULL) {
+ ev_window->priv->properties = ev_properties_dialog_new ();
+ ev_properties_dialog_set_document (EV_PROPERTIES_DIALOG (ev_window->priv->properties),
+ ev_window->priv->uri,
+ ev_window->priv->document);
+ g_object_add_weak_pointer (G_OBJECT (ev_window->priv->properties),
+ (gpointer) &(ev_window->priv->properties));
+ gtk_window_set_transient_for (GTK_WINDOW (ev_window->priv->properties),
+ GTK_WINDOW (ev_window));
+ }
+
+ ev_document_fc_mutex_lock ();
+ gtk_widget_show (ev_window->priv->properties);
+ ev_document_fc_mutex_unlock ();
+}
+
+static void
+print_jobs_confirmation_dialog_response (GtkDialog *dialog,
+ gint response,
+ EvWindow *ev_window)
+{
+ gtk_widget_destroy (GTK_WIDGET (dialog));
+
+ switch (response) {
+ case GTK_RESPONSE_YES:
+ if (!ev_window->priv->print_queue ||
+ g_queue_is_empty (ev_window->priv->print_queue))
+ gtk_widget_destroy (GTK_WIDGET (ev_window));
+ else
+ ev_window->priv->close_after_print = TRUE;
+ break;
+ case GTK_RESPONSE_NO:
+ ev_window->priv->close_after_print = TRUE;
+ if (ev_window->priv->print_queue &&
+ !g_queue_is_empty (ev_window->priv->print_queue)) {
+ gtk_widget_set_sensitive (GTK_WIDGET (ev_window), FALSE);
+ ev_window_print_cancel (ev_window);
+ } else {
+ gtk_widget_destroy (GTK_WIDGET (ev_window));
+ }
+ break;
+ case GTK_RESPONSE_CANCEL:
+ default:
+ ev_window->priv->close_after_print = FALSE;
+ }
+}
+
+static void
+ev_window_cmd_file_close_window (GtkAction *action, EvWindow *ev_window)
+{
+ GtkWidget *dialog;
+ gchar *text, *markup;
+ gint n_print_jobs;
+
+ n_print_jobs = ev_window->priv->print_queue ?
+ g_queue_get_length (ev_window->priv->print_queue) : 0;
+
+ if (n_print_jobs == 0) {
+ gtk_widget_destroy (GTK_WIDGET (ev_window));
+ return;
+ }
+
+ dialog = gtk_message_dialog_new (GTK_WINDOW (ev_window),
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_QUESTION,
+ GTK_BUTTONS_NONE,
+ NULL);
+ if (n_print_jobs == 1) {
+ EvPrintOperation *op;
+ const gchar *job_name;
+
+ op = g_queue_peek_tail (ev_window->priv->print_queue);
+ job_name = ev_print_operation_get_job_name (op);
+
+ text = g_strdup_printf (_("Wait until print job “%s” finishes before closing?"),
+ job_name);
+ } else {
+ text = g_strdup_printf (_("There are %d print jobs active. "
+ "Wait until print finishes before closing?"),
+ n_print_jobs);
+ }
+
+ markup = g_strdup_printf ("<b>%s</b>", text);
+ g_free (text);
+
+ gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (dialog), markup);
+ g_free (markup);
+
+ gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s",
+ _("If you close the window, pending print "
+ "jobs will not be printed."));
+
+ gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+ _("Cancel _print and Close"),
+ GTK_RESPONSE_NO,
+ GTK_STOCK_CANCEL,
+ GTK_RESPONSE_CANCEL,
+ _("Close _after Printing"),
+ GTK_RESPONSE_YES,
+ NULL);
+ gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);
+
+ g_signal_connect (dialog, "response",
+ G_CALLBACK (print_jobs_confirmation_dialog_response),
+ ev_window);
+ gtk_widget_show (dialog);
+}
+
+static void
+ev_window_cmd_focus_page_selector (GtkAction *act, EvWindow *window)
+{
+ GtkAction *action;
+
+ update_chrome_flag (window, EV_CHROME_RAISE_TOOLBAR, TRUE);
+ ev_window_set_action_sensitive (window, "ViewToolbar", FALSE);
+ update_chrome_visibility (window);
+
+ action = gtk_action_group_get_action (window->priv->action_group,
+ PAGE_SELECTOR_ACTION);
+ ev_page_action_grab_focus (EV_PAGE_ACTION (action));
+}
+
+static void
+ev_window_cmd_scroll_forward (GtkAction *action, EvWindow *window)
+{
+ ev_view_scroll (EV_VIEW (window->priv->view), GTK_SCROLL_PAGE_FORWARD, FALSE);
+}
+
+static void
+ev_window_cmd_scroll_backward (GtkAction *action, EvWindow *window)
+{
+ ev_view_scroll (EV_VIEW (window->priv->view), GTK_SCROLL_PAGE_BACKWARD, FALSE);
+}
+
+static void
+ev_window_cmd_continuous (GtkAction *action, EvWindow *ev_window)
+{
+ gboolean continuous;
+
+ ev_window_stop_presentation (ev_window, TRUE);
+ continuous = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+ g_object_set (G_OBJECT (ev_window->priv->view),
+ "continuous", continuous,
+ NULL);
+ ev_window_update_actions (ev_window);
+}
+
+static void
+ev_window_cmd_dual (GtkAction *action, EvWindow *ev_window)
+{
+ gboolean dual_page;
+
+ ev_window_stop_presentation (ev_window, TRUE);
+ dual_page = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
+ g_object_set (G_OBJECT (ev_window->priv->view),
+ "dual-page", dual_page,
+ NULL);
+ ev_window_update_actions (ev_window);
+}
+
+static void
+ev_window_cmd_view_best_fit (GtkAction *action, EvWindow *ev_window)
+{
+ ev_window_stop_presentation (ev_window, TRUE);
+
+ if (gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))) {
+ ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_BEST_FIT);
+ } else {
+ ev_view_set_sizing_mode (EV_VIEW (ev_window->priv->view), EV_SIZING_FREE);
+ }
+ ev_window_update_actions (ev_window);
+}
+
+static void
+ev_window_cmd_view_page_width (GtkAction *action, EvWindow *ev_window)