]> www.fi.muni.cz Git - evince.git/blobdiff - shell/ev-application.c
[printing] Fix multipage even/odd printing issues
[evince.git] / shell / ev-application.c
index 8ce0899033ab2b0e89b534aca2df1e03add32675..6a3367f712546e6b93fe7f7326e69e9e3694e7b1 100644 (file)
@@ -59,6 +59,8 @@ struct _EvApplication {
        gchar *dot_dir;
        gchar *accel_map_file;
        gchar *toolbars_file;
        gchar *dot_dir;
        gchar *accel_map_file;
        gchar *toolbars_file;
+       gchar *crashed_file;
+       guint  crashed_idle;
 
        EggToolbarsModel *toolbars_model;
 
 
        EggToolbarsModel *toolbars_model;
 
@@ -66,7 +68,8 @@ struct _EvApplication {
 
        EggSMClient *smclient;
 
 
        EggSMClient *smclient;
 
-       gchar *last_chooser_uri;
+       gchar *filechooser_open_uri;
+       gchar *filechooser_save_uri;
 
 #ifdef ENABLE_DBUS
        EvMediaPlayerKeys *keys;
 
 #ifdef ENABLE_DBUS
        EvMediaPlayerKeys *keys;
@@ -164,18 +167,142 @@ ev_application_get_instance (void)
 }
 
 /* Session */
 }
 
 /* Session */
+static void
+save_session (EvApplication *application,
+             GList         *windows_list,
+             GKeyFile      *state_file)
+{
+       GList *l;
+       gint i;
+       const gchar **uri_list;
+       const gchar *empty = "empty-window";
+
+       uri_list = g_new (const gchar *, g_list_length (windows_list));
+       for (l = windows_list, i = 0; l != NULL; l = g_list_next (l), i++) {
+               EvWindow *window = EV_WINDOW (l->data);
+
+               if (ev_window_is_empty (window))
+                       uri_list[i] = empty;
+               else
+                       uri_list[i] = ev_window_get_uri (window);
+       }
+       g_key_file_set_string_list (state_file,
+                                   "Evince",
+                                   "documents",
+                                   (const char **)uri_list,
+                                   i);
+       g_free (uri_list);
+}
+
+static void
+ev_application_save_session_crashed (EvApplication *application)
+{
+       GList *windows;
+
+       windows = ev_application_get_windows (application);
+       if (windows) {
+               GKeyFile *crashed_file;
+               gchar    *data;
+               gssize    data_length;
+               GError   *error = NULL;
+
+               crashed_file = g_key_file_new ();
+               save_session (application, windows, crashed_file);
+
+               data = g_key_file_to_data (crashed_file, (gsize *)&data_length, NULL);
+               g_file_set_contents (application->crashed_file, data, data_length, &error);
+               if (error) {
+                       g_warning ("%s", error->message);
+                       g_error_free (error);
+               }
+               g_free (data);
+               g_key_file_free (crashed_file);
+       } else if (g_file_test (application->crashed_file, G_FILE_TEST_IS_REGULAR)) {
+               GFile *file;
+
+               file = g_file_new_for_path (application->crashed_file);
+               g_file_delete (file, NULL, NULL);
+               g_object_unref (file);
+       }
+}
+
+static gboolean
+save_session_crashed_in_idle_cb (EvApplication *application)
+{
+       ev_application_save_session_crashed (application);
+       application->crashed_idle = 0;
+
+       return FALSE;
+}
+
+static void
+save_session_crashed_in_idle (EvApplication *application)
+{
+       if (application->crashed_idle > 0)
+               g_source_remove (application->crashed_idle);
+       application->crashed_idle =
+               g_idle_add ((GSourceFunc)save_session_crashed_in_idle_cb,
+                           application);
+}
+
+static gboolean
+ev_application_run_crash_recovery_dialog (EvApplication *application)
+{
+       GtkWidget *dialog;
+       gint       response;
+
+       dialog = gtk_message_dialog_new (NULL,
+                                        GTK_DIALOG_MODAL,
+                                        GTK_MESSAGE_WARNING,
+                                        GTK_BUTTONS_NONE,
+                                        _("Recover previous documents?"));
+       gtk_message_dialog_format_secondary_text (
+               GTK_MESSAGE_DIALOG (dialog),
+               _("Evince appears to have exited unexpectedly the last time "
+                 "it was run. You can recover the opened documents."));
+
+       gtk_dialog_add_button (GTK_DIALOG (dialog),
+                              _("_Don't Recover"),
+                              GTK_RESPONSE_CANCEL);
+       gtk_dialog_add_button (GTK_DIALOG (dialog),
+                              _("_Recover"),
+                              GTK_RESPONSE_ACCEPT);
+
+       gtk_window_set_title (GTK_WINDOW (dialog), _("Crash Recovery"));
+       gtk_window_set_icon_name (GTK_WINDOW (dialog), "evince");
+       gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
+       gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), FALSE);
+       gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
+
+       response = gtk_dialog_run (GTK_DIALOG (dialog));
+       gtk_widget_destroy (dialog);
+
+       return response == GTK_RESPONSE_ACCEPT;
+}
+
 gboolean
 ev_application_load_session (EvApplication *application)
 {
        GKeyFile *state_file;
        gchar   **uri_list;
 gboolean
 ev_application_load_session (EvApplication *application)
 {
        GKeyFile *state_file;
        gchar   **uri_list;
-       
-       if (!egg_sm_client_is_resumed (application->smclient))
-               return FALSE;
 
 
-       state_file = egg_sm_client_get_state_file (application->smclient);
-       if (!state_file)
+       if (egg_sm_client_is_resumed (application->smclient)) {
+               state_file = egg_sm_client_get_state_file (application->smclient);
+               if (!state_file)
+                       return FALSE;
+       } else if (g_file_test (application->crashed_file, G_FILE_TEST_IS_REGULAR)) {
+               if (ev_application_run_crash_recovery_dialog (application)) {
+                       state_file = g_key_file_new ();
+                       g_key_file_load_from_file (state_file,
+                                                  application->crashed_file,
+                                                  G_KEY_FILE_NONE,
+                                                  NULL);
+               } else {
+                       return FALSE;
+               }
+       } else {
                return FALSE;
                return FALSE;
+       }
 
        uri_list = g_key_file_get_string_list (state_file,
                                               "Evince",
 
        uri_list = g_key_file_get_string_list (state_file,
                                               "Evince",
@@ -202,30 +329,13 @@ smclient_save_state_cb (EggSMClient   *client,
                        GKeyFile      *state_file,
                        EvApplication *application)
 {
                        GKeyFile      *state_file,
                        EvApplication *application)
 {
-       GList *windows, *l;
-       gint i;
-       const gchar **uri_list;
-       const gchar *empty = "empty-window";
+       GList *windows;
 
        windows = ev_application_get_windows (application);
 
        windows = ev_application_get_windows (application);
-       if (!windows)
-               return;
-
-       uri_list = g_new (const gchar *, g_list_length (windows));
-       for (l = windows, i = 0; l != NULL; l = g_list_next (l), i++) {
-               EvWindow *window = EV_WINDOW (l->data);
-
-               if (ev_window_is_empty (window))
-                       uri_list[i] = empty;
-               else
-                       uri_list[i] = ev_window_get_uri (window);
+       if (windows) {
+               save_session (application, windows, state_file);
+               g_list_free (windows);
        }
        }
-       g_key_file_set_string_list (state_file,
-                                   "Evince",
-                                   "documents", 
-                                   (const char **)uri_list,
-                                   i);
-       g_free (uri_list);
 }
 
 static void
 }
 
 static void
@@ -238,6 +348,9 @@ smclient_quit_cb (EggSMClient   *client,
 static void
 ev_application_init_session (EvApplication *application)
 {
 static void
 ev_application_init_session (EvApplication *application)
 {
+       application->crashed_file = g_build_filename (application->dot_dir,
+                                                     "evince-crashed", NULL);
+
        application->smclient = egg_sm_client_get ();
        g_signal_connect (application->smclient, "save_state",
                          G_CALLBACK (smclient_save_state_cb),
        application->smclient = egg_sm_client_get ();
        g_signal_connect (application->smclient, "save_state",
                          G_CALLBACK (smclient_save_state_cb),
@@ -385,48 +498,6 @@ get_find_string_from_args (GHashTable *args)
        return value ? g_value_get_string (value) : NULL;
 }
 
        return value ? g_value_get_string (value) : NULL;
 }
 
-/**
- * get_unlink_temp_file_from_args:
- * @args: a #GHashTable with data passed to the application.
- *
- * It does look if the unlink-temp-file option has been passed from the command
- * line returning it's boolean representation, otherwise it does return %FALSE.
- *
- * Returns: the boolean representation of the unlink-temp-file value or %FALSE
- *          in other case.
- */
-static gboolean
-get_unlink_temp_file_from_args (GHashTable *args)
-{
-       gboolean unlink_temp_file = FALSE;
-       GValue  *value = NULL;
-
-       g_assert (args != NULL);
-
-       value = g_hash_table_lookup (args, "unlink-temp-file");
-       if (value) {
-               unlink_temp_file = g_value_get_boolean (value);
-       }
-       
-       return unlink_temp_file;
-}
-
-static const gchar *
-get_print_settings_from_args (GHashTable *args)
-{
-       const gchar *print_settings = NULL;
-       GValue      *value = NULL;
-
-       g_assert (args != NULL);
-
-       value = g_hash_table_lookup (args, "print-settings");
-       if (value) {
-               print_settings = g_value_get_string (value);
-       }
-
-       return print_settings;
-}
-
 /**
  * ev_application_open_window:
  * @application: The instance of the application.
 /**
  * ev_application_open_window:
  * @application: The instance of the application.
@@ -454,10 +525,15 @@ ev_application_open_window (EvApplication  *application,
        }
        
        if (screen) {
        }
        
        if (screen) {
-               ev_stock_icons_add_icons_path_for_screen (screen);
+               ev_stock_icons_set_screen (screen);
                gtk_window_set_screen (GTK_WINDOW (new_window), screen);
        }
 
                gtk_window_set_screen (GTK_WINDOW (new_window), screen);
        }
 
+       ev_application_save_session_crashed (application);
+       g_signal_connect_swapped (new_window, "destroy",
+                                 G_CALLBACK (save_session_crashed_in_idle),
+                                 application);
+
        if (!GTK_WIDGET_REALIZED (new_window))
                gtk_widget_realize (new_window);
        
        if (!GTK_WIDGET_REALIZED (new_window))
                gtk_widget_realize (new_window);
        
@@ -552,7 +628,6 @@ ev_application_get_uri_window (EvApplication *application, const char *uri)
  * @screen: Thee screen where the link will be shown.
  * @dest: The #EvLinkDest of the document.
  * @mode: The run mode of the window.
  * @screen: Thee screen where the link will be shown.
  * @dest: The #EvLinkDest of the document.
  * @mode: The run mode of the window.
- * @unlink_temp_file: The unlink_temp_file option value.
  * @timestamp: Current time value.
  */
 void
  * @timestamp: Current time value.
  */
 void
@@ -562,8 +637,6 @@ ev_application_open_uri_at_dest (EvApplication  *application,
                                 EvLinkDest     *dest,
                                 EvWindowRunMode mode,
                                 const gchar    *search_string,
                                 EvLinkDest     *dest,
                                 EvWindowRunMode mode,
                                 const gchar    *search_string,
-                                gboolean        unlink_temp_file,
-                                const gchar    *print_settings, 
                                 guint           timestamp)
 {
        EvWindow *new_window;
                                 guint           timestamp)
 {
        EvWindow *new_window;
@@ -581,14 +654,18 @@ ev_application_open_uri_at_dest (EvApplication  *application,
        }
 
        if (screen) {
        }
 
        if (screen) {
-               ev_stock_icons_add_icons_path_for_screen (screen);
+               ev_stock_icons_set_screen (screen);
                gtk_window_set_screen (GTK_WINDOW (new_window), screen);
        }
 
        /* We need to load uri before showing the window, so
           we can restore window size without flickering */     
                gtk_window_set_screen (GTK_WINDOW (new_window), screen);
        }
 
        /* We need to load uri before showing the window, so
           we can restore window size without flickering */     
-       ev_window_open_uri (new_window, uri, dest, mode, search_string, 
-                           unlink_temp_file, print_settings);
+       ev_window_open_uri (new_window, uri, dest, mode, search_string);
+
+       ev_application_save_session_crashed (application);
+       g_signal_connect_swapped (new_window, "destroy",
+                                 G_CALLBACK (save_session_crashed_in_idle),
+                                 application);
 
        if (!GTK_WIDGET_REALIZED (GTK_WIDGET (new_window)))
                gtk_widget_realize (GTK_WIDGET (new_window));
 
        if (!GTK_WIDGET_REALIZED (GTK_WIDGET (new_window)))
                gtk_widget_realize (GTK_WIDGET (new_window));
@@ -626,8 +703,6 @@ ev_application_open_uri (EvApplication  *application,
        EvLinkDest      *dest = NULL;
        EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
        const gchar     *search_string = NULL;
        EvLinkDest      *dest = NULL;
        EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
        const gchar     *search_string = NULL;
-       gboolean         unlink_temp_file = FALSE;
-       const gchar     *print_settings = NULL;
        GdkScreen       *screen = NULL;
 
        if (args) {
        GdkScreen       *screen = NULL;
 
        if (args) {
@@ -635,15 +710,11 @@ ev_application_open_uri (EvApplication  *application,
                dest = get_destination_from_args (args);
                mode = get_window_run_mode_from_args (args);
                search_string = get_find_string_from_args (args);
                dest = get_destination_from_args (args);
                mode = get_window_run_mode_from_args (args);
                search_string = get_find_string_from_args (args);
-               unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
-                                   get_unlink_temp_file_from_args (args));
-               print_settings = get_print_settings_from_args (args);
        }
        
        ev_application_open_uri_at_dest (application, uri, screen,
                                         dest, mode, search_string,
        }
        
        ev_application_open_uri_at_dest (application, uri, screen,
                                         dest, mode, search_string,
-                                        unlink_temp_file,
-                                        print_settings, timestamp);
+                                        timestamp);
 
        if (dest)
                g_object_unref (dest);
 
        if (dest)
                g_object_unref (dest);
@@ -662,13 +733,19 @@ ev_application_open_uri_list (EvApplication *application,
        for (l = uri_list; l != NULL; l = l->next) {
                ev_application_open_uri_at_dest (application, (char *)l->data,
                                                 screen, NULL, 0, NULL,
        for (l = uri_list; l != NULL; l = l->next) {
                ev_application_open_uri_at_dest (application, (char *)l->data,
                                                 screen, NULL, 0, NULL,
-                                                FALSE, NULL, timestamp);
+                                                timestamp);
        }
 }
 
 void
 ev_application_shutdown (EvApplication *application)
 {
        }
 }
 
 void
 ev_application_shutdown (EvApplication *application)
 {
+       if (application->crashed_file) {
+               ev_application_save_session_crashed (application);
+               g_free (application->crashed_file);
+               application->crashed_file = NULL;
+       }
+
        if (application->accel_map_file) {
                gtk_accel_map_save (application->accel_map_file);
                g_free (application->accel_map_file);
        if (application->accel_map_file) {
                gtk_accel_map_save (application->accel_map_file);
                g_free (application->accel_map_file);
@@ -710,8 +787,10 @@ ev_application_shutdown (EvApplication *application)
 
         g_free (application->dot_dir);
         application->dot_dir = NULL;
 
         g_free (application->dot_dir);
         application->dot_dir = NULL;
-       g_free (application->last_chooser_uri);
-        application->last_chooser_uri = NULL;
+       g_free (application->filechooser_open_uri);
+        application->filechooser_open_uri = NULL;
+       g_free (application->filechooser_save_uri);
+       application->filechooser_save_uri = NULL;
 
        g_object_unref (application);
         instance = NULL;
 
        g_object_unref (application);
         instance = NULL;
@@ -729,8 +808,6 @@ ev_application_init (EvApplication *ev_application)
 {
        gint i;
        const gchar *home_dir;
 {
        gint i;
        const gchar *home_dir;
-       
-       ev_application_init_session (ev_application);
 
         ev_application->dot_dir = g_build_filename (g_get_home_dir (),
                                                     ".gnome2",
 
         ev_application->dot_dir = g_build_filename (g_get_home_dir (),
                                                     ".gnome2",
@@ -741,6 +818,8 @@ ev_application_init (EvApplication *ev_application)
         if (!ev_dir_ensure_exists (ev_application->dot_dir, 0700))
                 exit (1);
 
         if (!ev_dir_ensure_exists (ev_application->dot_dir, 0700))
                 exit (1);
 
+       ev_application_init_session (ev_application);
+
        home_dir = g_get_home_dir ();
        if (home_dir) {
                ev_application->accel_map_file = g_build_filename (home_dir,
        home_dir = g_get_home_dir ();
        if (home_dir) {
                ev_application->accel_map_file = g_build_filename (home_dir,
@@ -848,16 +927,32 @@ ev_application_save_toolbars_model (EvApplication *application)
 }
 
 void
 }
 
 void
-ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
+ev_application_set_filechooser_uri (EvApplication       *application,
+                                   GtkFileChooserAction action,
+                                   const gchar         *uri)
 {
 {
-       g_free (application->last_chooser_uri);
-       application->last_chooser_uri = g_strdup (uri);
+       if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
+               g_free (application->filechooser_open_uri);
+               application->filechooser_open_uri = g_strdup (uri);
+       } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
+               g_free (application->filechooser_save_uri);
+               application->filechooser_save_uri = g_strdup (uri);
+       }
 }
 
 const gchar *
 }
 
 const gchar *
-ev_application_get_chooser_uri (EvApplication *application)
+ev_application_get_filechooser_uri (EvApplication       *application,
+                                   GtkFileChooserAction action)
 {
 {
-       return application->last_chooser_uri;
+       if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
+               if (application->filechooser_open_uri)
+                       return application->filechooser_open_uri;
+       } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
+               if (application->filechooser_save_uri)
+                       return application->filechooser_save_uri;
+       }
+
+       return NULL;
 }
 
 void
 }
 
 void