From: Daniel Carl Date: Sun, 3 Mar 2013 13:33:57 +0000 (+0100) Subject: Save current visited url to history file. X-Git-Url: https://git.owens.tech/about.html/about.html/git?a=commitdiff_plain;h=ef4ddf9c4d810c86970b433f595832eecf5dbb07;p=vimb.git Save current visited url to history file. --- diff --git a/src/main.c b/src/main.c index 3874c34..2096d4c 100644 --- a/src/main.c +++ b/src/main.c @@ -79,6 +79,7 @@ static void vp_set_cookie(SoupCookie* cookie); static const char* vp_get_cookies(SoupURI *uri); static gboolean vp_hide_message(void); static void vp_set_status(const StatusType status); +static void vp_save_url_history(const char* url, const char* title); static void vp_webview_progress_cb(WebKitWebView* view, GParamSpec* pspec, gboolean download) { @@ -146,6 +147,8 @@ static void vp_webview_load_status_cb(WebKitWebView* view, GParamSpec* pspec, gp vp_update_statusbar(); dom_check_auto_insert(); + + vp_save_url_history(uri, webkit_web_view_get_title(vp.gui.webview)); break; case WEBKIT_LOAD_FAILED: @@ -461,6 +464,45 @@ static void vp_set_status(const StatusType status) } } +static void vp_save_url_history(const char* url, const char* title) +{ + FILE* f = NULL; + char buf[512] = {0}; + char* new = NULL; + guint max = core.config.max_history_items; + f = fopen(core.files[FILES_HISTORY], "r"); + if (!f) { + return; + } + + char* title_escaped = g_shell_quote(title ? title : ""); + char* new_item = g_strdup_printf("%s %s\n", url, title); + gint n = strlen(new_item); + g_free(title_escaped); + + new = g_new0(char, max * sizeof(buf)); + /* put the new entry to the top */ + strncpy(new, new_item, n); + + for (guint i = 1; i < max && fgets(buf, sizeof(buf), f); i++) { + if (strncmp(new_item, buf, n) != 0) { + strncat(new, buf, sizeof(buf)); + } + } + fclose(f); + + f = fopen(core.files[FILES_HISTORY], "w"); + if (!f) { + g_free(new); + + return; + } + fprintf(f, "%s", new); + g_free(new); + + fclose(f); +} + /** * Set the base modes. All other mode flags like completion can be set directly * to vp.state.mode. diff --git a/src/setting.c b/src/setting.c index dcd9c7d..5fd604a 100644 --- a/src/setting.c +++ b/src/setting.c @@ -120,7 +120,7 @@ static Setting default_settings[] = { {NULL, "home-page", TYPE_CHAR, setting_home_page, {.s = "https://github.com/fanglingsu/vimp"}}, {NULL, "download-path", TYPE_CHAR, setting_download_path, {.s = "/tmp/vimp"}}, {NULL, "stylesheet", TYPE_BOOLEAN, setting_user_style, {.i = 1}}, - {NULL, "history-max-items", TYPE_INTEGER, setting_history_max_items, {.i = 100}}, + {NULL, "history-max-items", TYPE_INTEGER, setting_history_max_items, {.i = 500}}, };