]> www.fi.muni.cz Git - evince.git/commitdiff
Remember page setup options too. Paper size is globally remembered while
authorCarlos Garcia Campos <carlosgc@gnome.org>
Mon, 29 Dec 2008 10:00:24 +0000 (10:00 +0000)
committerCarlos Garcia Campos <carlosgc@src.gnome.org>
Mon, 29 Dec 2008 10:00:24 +0000 (10:00 +0000)
2008-12-29  Carlos Garcia Campos  <carlosgc@gnome.org>

* shell/ev-application.[ch]: (ev_application_shutdown),
(ev_application_get_print_settings_file),
(ev_application_save_print_settings),
(ev_application_get_print_settings),
(ev_application_set_print_settings),
(ev_application_get_page_setup), (ev_application_set_page_setup):
* shell/ev-window.c: (ev_window_save_print_page_setup),
(ev_window_load_print_page_setup_from_metadata),
(ev_window_print_page_setup_done_cb),
(ev_window_cmd_file_print_setup), (ev_window_do_preview_print),
(ev_window_cmd_preview_print):

Remember page setup options too. Paper size is globally remembered
while page margins are stored per document in metadata file. Fixes
bugs #525185 and #349102.

svn path=/trunk/; revision=3313

ChangeLog
shell/ev-application.c
shell/ev-application.h
shell/ev-window.c

index cfa001e9bd885a080f66732fc69ddd833fc6442e..6167fef149b4fb3ce39137c4d06a6293c9fce0ba 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2008-12-29  Carlos Garcia Campos  <carlosgc@gnome.org>
+
+       * shell/ev-application.[ch]: (ev_application_shutdown),
+       (ev_application_get_print_settings_file),
+       (ev_application_save_print_settings),
+       (ev_application_get_print_settings),
+       (ev_application_set_print_settings),
+       (ev_application_get_page_setup), (ev_application_set_page_setup):
+       * shell/ev-window.c: (ev_window_save_print_page_setup),
+       (ev_window_load_print_page_setup_from_metadata),
+       (ev_window_print_page_setup_done_cb),
+       (ev_window_cmd_file_print_setup), (ev_window_do_preview_print),
+       (ev_window_cmd_preview_print):
+
+       Remember page setup options too. Paper size is globally remembered
+       while page margins are stored per document in metadata file. Fixes
+       bugs #525185 and #349102.
+       
 2008-12-29  Carlos Garcia Campos  <carlosgc@gnome.org>
 
        * shell/ev-print-operation.c:
index 9637940e6e270ade87a2e321b463e2bf6a507514..46001f494207d7cf64d5ae126b7d4afa86a73c38 100644 (file)
@@ -46,7 +46,8 @@
 #include "ev-application-service.h"
 #endif
 
-static void ev_application_add_icon_path_for_screen (GdkScreen *screen);
+static void ev_application_add_icon_path_for_screen (GdkScreen     *screen);
+static void ev_application_save_print_settings      (EvApplication *application);
 
 struct _EvApplication {
        GObject base_instance;
@@ -68,7 +69,8 @@ struct _EvApplication {
 #endif /* ENABLE_DBUS */
 
        GtkPrintSettings *print_settings;
-       gchar            *print_settings_file;
+       GtkPageSetup     *page_setup;
+       GKeyFile         *print_settings_file;
 };
 
 struct _EvApplicationClass {
@@ -79,6 +81,10 @@ G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
 
 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
 
+#define EV_PRINT_SETTINGS_FILE "print-settings"
+#define EV_PRINT_SETTINGS_GROUP "Print Settings"
+#define EV_PAGE_SETUP_GROUP "Page Setup"
+
 #ifdef ENABLE_DBUS
 gboolean
 ev_application_register_service (EvApplication *application)
@@ -691,24 +697,21 @@ ev_application_shutdown (EvApplication *application)
                application->toolbars_file = NULL;
        }
 
+       ev_application_save_print_settings (application);
+       
        if (application->print_settings_file) {
-               if (application->print_settings) {
-                       GError *error = NULL;
-                       
-                       gtk_print_settings_to_file (application->print_settings,
-                                                   application->print_settings_file,
-                                                   &error);
-                       if (error) {
-                               g_warning ("%s", error->message);
-                               g_error_free (error);
-                       }
+               g_key_file_free (application->print_settings_file);
+               application->print_settings_file = NULL;
+       }
 
-                       g_object_unref (application->print_settings);
-                       application->print_settings = NULL;
-               }
+       if (application->print_settings) {
+               g_object_unref (application->print_settings);
+               application->print_settings = NULL;
+       }
 
-               g_free (application->print_settings_file);
-               application->print_settings_file = NULL;
+       if (application->page_setup) {
+               g_object_unref (application->page_setup);
+               application->page_setup = NULL;
        }
 
 #ifdef ENABLE_DBUS
@@ -870,32 +873,83 @@ ev_application_screensaver_disable (EvApplication *application)
                totem_scrsaver_disable (application->scr_saver);        
 }
 
-GtkPrintSettings *
-ev_application_get_print_settings (EvApplication *application)
+static GKeyFile *
+ev_application_get_print_settings_file (EvApplication *application)
 {
-       if (application->print_settings)
-               return application->print_settings;
+       gchar *filename;
        
-       if (!application->print_settings_file) {
-               application->print_settings_file =
-                       g_build_filename (ev_dot_dir (), "print-settings", NULL);
-       }
+       if (application->print_settings_file)
+               return application->print_settings_file;
 
-       if (g_file_test (application->print_settings_file, G_FILE_TEST_IS_REGULAR)) {
+       application->print_settings_file = g_key_file_new ();
+       
+       filename = g_build_filename (ev_dot_dir (), EV_PRINT_SETTINGS_FILE, NULL);
+       if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
                GError *error = NULL;
-               
-               application->print_settings =
-                       gtk_print_settings_new_from_file (application->print_settings_file, &error);
-               
+
+               g_key_file_load_from_file (application->print_settings_file,
+                                          filename,
+                                          G_KEY_FILE_KEEP_COMMENTS |
+                                          G_KEY_FILE_KEEP_TRANSLATIONS,
+                                          &error);
                if (error) {
                        g_warning ("%s", error->message);
                        g_error_free (error);
-               } else {
-                       return application->print_settings;
                }
        }
+       g_free (filename);
+
+       return application->print_settings_file;
+}
+
+static void
+ev_application_save_print_settings (EvApplication *application)
+{
+       GKeyFile *key_file;
+       gchar    *filename;
+       gchar    *data;
+       gssize    data_length;
+       GError   *error = NULL;
+
+       if (!application->print_settings && !application->page_setup)
+               return;
        
-       application->print_settings = gtk_print_settings_new ();
+       key_file = ev_application_get_print_settings_file (application);
+       if (application->print_settings)
+               gtk_print_settings_to_key_file (application->print_settings,
+                                               key_file,
+                                               EV_PRINT_SETTINGS_GROUP);
+       if (application->page_setup)
+               gtk_page_setup_to_key_file (application->page_setup,
+                                           key_file,
+                                           EV_PAGE_SETUP_GROUP);
+       
+       filename = g_build_filename (ev_dot_dir (), EV_PRINT_SETTINGS_FILE, NULL);
+       data = g_key_file_to_data (key_file, (gsize *)&data_length, NULL);
+       g_file_set_contents (filename, data, data_length, &error);
+       if (error) {
+               g_warning ("%s", error->message);
+               g_error_free (error);
+       }
+       g_free (data);
+       g_free (filename);
+}
+
+GtkPrintSettings *
+ev_application_get_print_settings (EvApplication *application)
+{
+       GKeyFile         *key_file;
+       GtkPrintSettings *print_settings;
+       
+       if (application->print_settings)
+               return application->print_settings;
+
+       key_file = ev_application_get_print_settings_file (application);
+       print_settings = g_key_file_has_group (key_file, EV_PRINT_SETTINGS_GROUP) ? 
+               gtk_print_settings_new_from_key_file (key_file, EV_PRINT_SETTINGS_GROUP, NULL) :
+               gtk_print_settings_new ();
+
+       application->print_settings = print_settings ? print_settings : gtk_print_settings_new ();
 
        return application->print_settings;
 }
@@ -904,14 +958,57 @@ void
 ev_application_set_print_settings (EvApplication    *application,
                                   GtkPrintSettings *settings)
 {
+       GKeyFile *key_file;
+       
        g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
        
        if (settings == application->print_settings)
                return;
 
+       key_file = ev_application_get_print_settings_file (application);
+       
        if (application->print_settings)
                g_object_unref (application->print_settings);
        
        application->print_settings = g_object_ref (settings);
+       gtk_print_settings_to_key_file (settings, key_file, EV_PRINT_SETTINGS_GROUP);
+}
+
+GtkPageSetup *
+ev_application_get_page_setup (EvApplication *application)
+{
+       GKeyFile     *key_file;
+       GtkPageSetup *page_setup;
+       
+       if (application->page_setup)
+               return application->page_setup;
+
+       key_file = ev_application_get_print_settings_file (application);
+       page_setup = g_key_file_has_group (key_file, EV_PAGE_SETUP_GROUP) ? 
+               gtk_page_setup_new_from_key_file (key_file, EV_PAGE_SETUP_GROUP, NULL) :
+               gtk_page_setup_new ();
+
+       application->page_setup = page_setup ? page_setup : gtk_page_setup_new ();
+
+       return application->page_setup;
 }
 
+void
+ev_application_set_page_setup (EvApplication *application,
+                              GtkPageSetup  *page_setup)
+{
+       GKeyFile *key_file;
+       
+       g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
+       
+       if (page_setup == application->page_setup)
+               return;
+
+       key_file = ev_application_get_print_settings_file (application);
+       
+       if (application->page_setup)
+               g_object_unref (application->page_setup);
+       
+       application->page_setup = g_object_ref (page_setup);
+       gtk_page_setup_to_key_file (page_setup, key_file, EV_PAGE_SETUP_GROUP);
+}
index 108fd82c58c1adceaae6bd6416af9668f56c6594..1dfa321cb8799e5251a17c5135b379a96af126a8 100644 (file)
@@ -89,6 +89,9 @@ void            ev_application_screensaver_disable (EvApplication   *application);
 GtkPrintSettings *ev_application_get_print_settings  (EvApplication   *application);
 void              ev_application_set_print_settings  (EvApplication   *application,
                                                      GtkPrintSettings *settings);
+GtkPageSetup     *ev_application_get_page_setup      (EvApplication   *application);
+void              ev_application_set_page_setup      (EvApplication   *application,
+                                                     GtkPageSetup    *page_setup);
 
 G_END_DECLS
 
index 3c61eabf69fc3d883fe7fa05a614ad939359c2e4..d53d818720eff8b14497c6e7fd1e5bf85b9de43c 100644 (file)
@@ -224,7 +224,8 @@ static const gchar *document_print_settings[] = {
        GTK_PRINT_SETTINGS_SCALE,
        GTK_PRINT_SETTINGS_PRINT_PAGES,
        GTK_PRINT_SETTINGS_PAGE_RANGES,
-       GTK_PRINT_SETTINGS_PAGE_SET
+       GTK_PRINT_SETTINGS_PAGE_SET,
+       GTK_PRINT_SETTINGS_OUTPUT_URI
 };
 
 static void    ev_window_update_actions                (EvWindow         *ev_window);
@@ -294,6 +295,7 @@ 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_save_print_page_setup         (EvWindow         *window);
 
 G_DEFINE_TYPE (EvWindow, ev_window, GTK_TYPE_WINDOW)
 
@@ -2266,30 +2268,6 @@ ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
        gtk_widget_show (fc);
 }
 
-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)
-               g_object_unref (window->priv->print_page_setup);
-       window->priv->print_page_setup = g_object_ref (page_setup);
-}
-
-static void
-ev_window_cmd_file_print_setup (GtkAction *action, EvWindow *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_load_print_settings_from_metadata (EvWindow *window)
 {
@@ -2326,6 +2304,115 @@ ev_window_save_print_settings (EvWindow *window)
        }
 }
 
+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)
 {
@@ -3532,7 +3619,6 @@ static void
 ev_window_do_preview_print (EvWindow *window)
 {
        EvWindowPrivate  *priv = window->priv;
-       GtkPageSetup     *page_setup;
        GtkPrintJob      *job;
        gchar            *filename;
        GError           *error = NULL;
@@ -3540,18 +3626,17 @@ ev_window_do_preview_print (EvWindow *window)
        g_assert (priv->print_settings != NULL);
        g_assert (priv->printer != NULL);
 
-       page_setup = gtk_page_setup_new ();
-
        job = gtk_print_job_new (gtk_window_get_title (GTK_WINDOW (window)),
                                 priv->printer,
                                 priv->print_settings,
-                                page_setup);
+                                priv->print_page_setup);
 
        g_object_unref (priv->print_settings);
        priv->print_settings = NULL;
+       g_object_unref (priv->print_page_setup);
+       priv->print_page_setup = NULL;
        g_object_unref (priv->printer);
        priv->printer = NULL;
-       g_object_unref (page_setup);
 
        filename = g_filename_from_uri (priv->local_uri ?
                                        priv->local_uri : priv->uri,
@@ -3574,31 +3659,50 @@ ev_window_do_preview_print (EvWindow *window)
 static void
 ev_window_cmd_preview_print (GtkAction *action, EvWindow *window)
 {
-       EvWindowPrivate *priv = window->priv;
-       GtkPrintSettings *print_settings = NULL;
+       EvWindowPrivate  *priv = window->priv;
+       GtkPrintSettings *print_settings;
+       GtkPageSetup     *page_setup;
        const gchar      *print_settings_file = priv->print_settings_file;
 
-       if (print_settings_file) {
-               if (g_file_test (print_settings_file, G_FILE_TEST_IS_REGULAR)) {
-                       GError *error = NULL;
-
-                       print_settings = gtk_print_settings_new_from_file (print_settings_file,
-                                                                          &error);
+       if (print_settings_file && g_file_test (print_settings_file, G_FILE_TEST_IS_REGULAR)) {
+               GKeyFile *key_file;
+               GError   *error = NULL;
+
+               key_file = g_key_file_new ();
+               g_key_file_load_from_file (key_file,
+                                          print_settings_file,
+                                          G_KEY_FILE_KEEP_COMMENTS |
+                                          G_KEY_FILE_KEEP_TRANSLATIONS,
+                                          &error);
+               if (!error) {
+                       print_settings =
+                               gtk_print_settings_new_from_key_file (key_file,
+                                                                     "Print Settings",
+                                                                     NULL);
+                       print_settings = print_settings ? print_settings : gtk_print_settings_new ();
                        
-                       if (error) {
-                               g_warning ("%s", error->message);
-                               g_error_free (error);
-                               print_settings = NULL;
-                       }
+                       page_setup = gtk_page_setup_new_from_key_file (key_file,
+                                                                      "Page Setup",
+                                                                      NULL);
+                       page_setup = page_setup ? page_setup : gtk_page_setup_new ();
+               } else {
+                       print_settings = gtk_print_settings_new ();
+                       page_setup = gtk_page_setup_new ();
+                       g_error_free (error);
                }
+
+               g_key_file_free (key_file);
+       } else {
+               print_settings = gtk_print_settings_new ();
+               page_setup = gtk_page_setup_new ();
        }
        
-       if (!print_settings)
-               print_settings = gtk_print_settings_new ();
-
        if (priv->print_settings)
                g_object_unref (priv->print_settings);
        priv->print_settings = print_settings;
+       if (priv->print_page_setup)
+               g_object_unref (priv->print_page_setup);
+       priv->print_page_setup = page_setup;
 
        gtk_enumerate_printers ((GtkPrinterFunc) ev_window_enumerate_printer_cb,
                                window, NULL, FALSE);