diff options
author | Dan Nicholson <dbn.lists@gmail.com> | 2012-05-26 21:04:01 -0700 |
---|---|---|
committer | Dan Nicholson <dbn.lists@gmail.com> | 2012-05-26 21:04:01 -0700 |
commit | d4e1b942ff7df3624d3da1c71aded32a3701fb6a (patch) | |
tree | 7dec9946a199793aa4bd4da23da35bfa3ea6668c /src | |
parent | 0b708019d34c60ce009612229841f697ac5b69a4 (diff) |
Add a Save Copy toolbar button
Allow the user to save a copy of the current document. The initial
directory is the XDG Downloads directory. The initial filename is the
document title since the current filename is likely some temporary thing
from the browser. It then falls back to the current filename, though.
Diffstat (limited to 'src')
-rw-r--r-- | src/evbp-viewer.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/evbp-viewer.c b/src/evbp-viewer.c index 7f31e48..befefa6 100644 --- a/src/evbp-viewer.c +++ b/src/evbp-viewer.c @@ -49,6 +49,83 @@ evbp_viewer_toggle_continuous(GtkToggleAction *action, EvbpViewer *viewer) } static void +evbp_chooser_response_cb(GtkDialog *dialog, gint response, EvbpViewer *viewer) +{ + if (response == GTK_RESPONSE_ACCEPT) { + gchar *uri; + GError *error; + + uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); + if (!ev_document_save(viewer->document, uri, &error)) { + g_warning("Failed to save file %s: %s\n", uri, + error->message); + g_error_free(error); + error = NULL; + } + g_free(uri); + } + + gtk_widget_destroy(GTK_WIDGET(dialog)); +} + +static void +evbp_viewer_save_copy(GtkAction *action, EvbpViewer *viewer) +{ + const gchar *title; + const gchar *download_dir; + gchar *filename = NULL; + GtkWindow *parent; + GtkWidget *chooser; + + /* create a filechooser as a child of the viewer */ + parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(viewer))); + chooser = gtk_file_chooser_dialog_new("Save File", parent, + GTK_FILE_CHOOSER_ACTION_SAVE, + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, + GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, + NULL); + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(chooser), + TRUE); + g_signal_connect(G_OBJECT(chooser), "response", + G_CALLBACK(evbp_chooser_response_cb), viewer); + gtk_window_set_modal(GTK_WINDOW(chooser), TRUE); + + /* filter per the current document type */ + ev_document_factory_add_filters(chooser, viewer->document); + + /* Set the initial directory to the xdg download directory. FIXME: This + * should probably check mozilla's download directory first. */ + if ((download_dir = g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD))) + gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), + download_dir); + + /* Try to determine an appropriate initial filename. What we get from + * mozilla is likely a garbage temporary name, so try the document title + * first. */ + title = ev_document_get_title(viewer->document); + if (title) + filename = g_strdup(title); + else { + const gchar *uri; + gchar *path; + + uri = ev_document_get_uri(viewer->document); + path = g_filename_from_uri(uri, NULL, NULL); + if (path) { + filename = g_path_get_basename(path); + g_free(path); + } + } + if (filename) { + gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(chooser), + filename); + g_free(filename); + } + + gtk_widget_show(chooser); +} + +static void evbp_viewer_previous_page(GtkAction *action, EvbpViewer *viewer) { ev_view_previous_page(EV_VIEW(viewer->view)); @@ -200,6 +277,10 @@ evbp_viewer_class_init(EvbpViewerClass *class) } static const GtkActionEntry action_entries[] = { + { "SaveCopy", GTK_STOCK_SAVE_AS, + "_Save Copy", "<control>s", + "Save a copy of the document", + G_CALLBACK(evbp_viewer_save_copy) }, { "GoPreviousPage", GTK_STOCK_GO_UP, "_Previous Page", "<control>Page_Up", "Go to the previous page", @@ -233,6 +314,8 @@ static const GtkActionEntry accel_entries[] = { static const gchar ui_string[] = "<ui>\n" " <toolbar name=\"ViewerToolbar\">\n" + " <toolitem name=\"SaveCopy\" action =\"SaveCopy\"/>\n" + " <separator/>\n" " <toolitem name=\"GoPreviousPage\" action=\"GoPreviousPage\"/>\n" " <toolitem name=\"GoNextPage\" action=\"GoNextPage\"/>\n" " <separator/>\n" |