Added command to download current page to given path (#32).
authorDaniel Carl <danielcarl@gmx.de>
Fri, 7 Jun 2013 21:51:37 +0000 (23:51 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Fri, 7 Jun 2013 22:55:25 +0000 (00:55 +0200)
doc/vimb.1.txt
src/command.c
src/command.h
src/main.c
src/main.h
src/util.c
src/util.h

index d3f1c1e..0e865c1 100644 (file)
@@ -316,7 +316,7 @@ This are wrapper commands to start completion and to step through the
 completion items, or to focus previous or next hints if hinting is active.
 
 .TP
-.BI run " [COMMAND LIST]"
+.BI "run [" "COMMAND LIST" ]
 Run is a command, that was introduced to have the ability to run multiple
 other commands with a single call. Everything after the `run' is interpreted
 as a `|' seperated list of commands and parameters. The run command allows to
@@ -331,6 +331,12 @@ Example:
 .BI [ N "]search-forward, [" N "]search-backward"
 Search in current page forward or backward.
 .TP
+.BI "save [" PATH "]"
+Download current opened page into configured download directory. If \fIPATH\fP
+is given, download under this file name or path. Possible value for PATH are
+`page.html', `subdir/img1.png', `~/downlod.html' or absolute pathes
+`/tmp/file.html'.
+.TP
 .B inspect
 Toggles the webinspector for current page. This is only available if the config
 "webinspector" is enabled.
index 8fa8c2e..9fb39c6 100644 (file)
@@ -134,6 +134,7 @@ static CommandInfo cmd_list[] = {
     {"prev",                 "p",     command_nextprev,             {1}},
     {"descent",              NULL,    command_descent,              {0}},
     {"descent!",             NULL,    command_descent,              {1}},
+    {"save",                 NULL,    command_save,                 {0}},
 };
 
 static void editor_resume(GPid pid, int status, OpenEditorData *data);
@@ -741,6 +742,21 @@ gboolean command_descent(const Arg *arg)
     return result;
 }
 
+gboolean command_save(const Arg *arg)
+{
+    WebKitDownload *download;
+    const char *uri = webkit_web_view_get_uri(vb.gui.webview);
+
+    if (!uri || *uri == '\0') {
+        return false;
+    }
+
+    download = webkit_download_new(webkit_network_request_new(uri));
+    vb_download(vb.gui.webview, download, arg->s ? arg->s : NULL);
+
+    return true;
+}
+
 gboolean command_editor(const Arg *arg)
 {
     char *file_path = NULL;
index 80316bd..ac41d21 100644 (file)
@@ -53,5 +53,6 @@ gboolean command_eval(const Arg *arg);
 gboolean command_editor(const Arg *arg);
 gboolean command_nextprev(const Arg *arg);
 gboolean command_descent(const Arg *arg);
+gboolean command_save(const Arg *arg);
 
 #endif /* end of include guard: _COMMAND_H */
index 84931b5..00dda90 100644 (file)
@@ -59,7 +59,6 @@ static void title_changed_cb(WebKitWebView *webview, WebKitWebFrame *frame, cons
 static gboolean mimetype_decision_cb(WebKitWebView *webview,
     WebKitWebFrame *frame, WebKitNetworkRequest *request, char*
     mime_type, WebKitWebPolicyDecision *decision);
-static gboolean download_requested_cb(WebKitWebView *view, WebKitDownload *download);
 static void download_progress_cp(WebKitDownload *download, GParamSpec *pspec);
 static void request_start_cb(WebKitWebView *webview, WebKitWebFrame *frame,
     WebKitWebResource *resource, WebKitNetworkRequest *request,
@@ -745,7 +744,7 @@ static void setup_signals()
         "signal::hovering-over-link", G_CALLBACK(hover_link_cb), NULL,
         "signal::title-changed", G_CALLBACK(title_changed_cb), NULL,
         "signal::mime-type-policy-decision-requested", G_CALLBACK(mimetype_decision_cb), NULL,
-        "signal::download-requested", G_CALLBACK(download_requested_cb), NULL,
+        "signal::download-requested", G_CALLBACK(vb_download), NULL,
         "signal::resource-request-starting", G_CALLBACK(request_start_cb), NULL,
         "signal::should-show-delete-interface-for-element", G_CALLBACK(gtk_false), NULL,
         NULL
@@ -906,26 +905,33 @@ static gboolean mimetype_decision_cb(WebKitWebView *webview,
     return false;
 }
 
-static gboolean download_requested_cb(WebKitWebView *view, WebKitDownload *download)
+gboolean vb_download(WebKitWebView *view, WebKitDownload *download, const char *path)
 {
     WebKitDownloadStatus status;
-    char *uri = NULL;
+    char *uri, *file;
 
-    const char *filename = webkit_download_get_suggested_filename(download);
-    if (!filename) {
-        filename = "vimb_donwload";
+    /* prepare the path to save the donwload */
+    if (path) {
+        file = util_buil_path(path, vb.config.download_dir);
+    } else {
+        path = webkit_download_get_suggested_filename(download);
+        if (!path) {
+            path = "vimb_donwload";
+        }
+        file = util_buil_path(path, vb.config.download_dir);
     }
 
-    /* prepare the download target path */
-    uri = g_strdup_printf("file://%s%c%s", vb.config.download_dir, G_DIR_SEPARATOR, filename);
+    /* build the file uri from file path */
+    uri = g_filename_to_uri(file, NULL, NULL);
     webkit_download_set_destination_uri(download, uri);
+    g_free(file);
     g_free(uri);
 
     guint64 size = webkit_download_get_total_size(download);
     if (size > 0) {
-        vb_echo(VB_MSG_NORMAL, false, "Download %s [~%uB] started ...", filename, size);
+        vb_echo(VB_MSG_NORMAL, false, "Download %s [~%uB] started ...", path, size);
     } else {
-        vb_echo(VB_MSG_NORMAL, false, "Download %s started ...", filename);
+        vb_echo(VB_MSG_NORMAL, false, "Download %s started ...", path);
     }
 
     status = webkit_download_get_status(download);
index 011f7e0..07ab814 100644 (file)
@@ -322,5 +322,6 @@ void vb_update_status_style(void);
 void vb_update_input_style(void);
 void vb_update_urlbar(const char *uri);
 VbInputType vb_get_input_parts(const char* input, const char **prefix, const char **clean);
+gboolean vb_download(WebKitWebView *view, WebKitDownload *download, const char *path);
 
 #endif /* end of include guard: _MAIN_H */
index 6c30c5c..3c1bf9e 100644 (file)
@@ -222,3 +222,38 @@ gboolean util_create_tmp_file(const char *content, char **file)
 
     return true;
 }
+
+/**
+ * Build the absolute file path of given path and possible given directory.
+ * If the path is already absolute or uses ~/ for the home directory, the
+ * directory is ignored.
+ *
+ * Returned path must be freed.
+ */
+char *util_buil_path(const char *path, const char *dir)
+{
+    char *fullPath, *p;
+
+    /* creating directory */
+    if (path[0] == '/') {
+        fullPath = g_strdup(path);
+    } else if (path[0] == '~') {
+        if (path[1] == '/') {
+            fullPath = g_strconcat(g_get_home_dir(), &path[1], NULL);
+        } else {
+            fullPath = g_strconcat(g_get_home_dir(), "/", &path[1], NULL);
+        }
+    } else if (dir) {
+        fullPath = g_strconcat(dir, "/", path, NULL);
+    } else {
+        fullPath = g_strconcat(g_get_current_dir(), "/", path, NULL);
+    }
+
+    if ((p = strrchr(fullPath, '/'))) {
+        *p = '\0';
+        g_mkdir_with_parents(fullPath, 0700);
+        *p = '/';
+    }
+
+    return fullPath;
+}
index aca552c..83824e6 100644 (file)
@@ -36,5 +36,6 @@ gboolean util_array_contains_all_tags(char **src, unsigned int s, char **query,
 gboolean util_string_contains_all_tags(char *src, char **query, unsigned int q);
 char *util_str_replace(const char* search, const char* replace, const char* string);
 gboolean util_create_tmp_file(const char *content, char **file);
+char *util_buil_path(const char *path, const char *dir);
 
 #endif /* end of include guard: _UTIL_H */