Added queue commands :push and :pop.
authorDaniel Carl <danielcarl@gmx.de>
Tue, 30 Jul 2013 19:33:43 +0000 (21:33 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Tue, 30 Jul 2013 20:27:28 +0000 (22:27 +0200)
doc/vimb.1
src/bookmark.c
src/bookmark.h
src/command.c
src/command.h
src/history.c
src/main.c
src/main.h
src/util.c
src/util.h

index 27e0f04..4553632 100644 (file)
@@ -325,6 +325,17 @@ Save the current opened uri with \fITAGS\fP to the bookmark file.
 Removes all bookmarks for given \fIURI\fP or if not given the current opened
 page.
 
+.SS Queue
+The queue allows to mark URLs for later reding (something like a read it later
+list). This list is shared between the single instances of PROJECT.
+.TP
+.BI "push [" URI ]
+Push \fIURI\fP or if not given current URI into the queue.
+.TP
+.B pop
+Open the oldest queue entry in current browser window and remove it from the
+queue.
+
 .SS Misc
 .TP
 .B next, n, prev, p
@@ -653,6 +664,10 @@ box.
 .RS
 Holds the bookmarks saved with command `bookmark-add'.
 .RE
+.I $XDG_CONFIG_HOME/PROJECT/queue
+.RS
+Holds the read it later queue filled by `hint-push' or `push'.
+.RE
 .I $XDG_CONFIG_HOME/PROJECT/scripts.js
 .RS
 This file can be used to run user scripts, that are injected into every paged
index e3aa925..337ad2f 100644 (file)
@@ -43,25 +43,14 @@ static void free_bookmark(Bookmark *bm);
  */
 gboolean bookmark_add(const char *uri, const char *title, const char *tags)
 {
-    FILE *f;
-
-    if ((f = fopen(vb.files[FILES_BOOKMARK], "a+"))) {
-        file_lock_set(fileno(f), F_WRLCK);
-
-        if (tags) {
-            fprintf(f, "%s\t%s\t%s\n", uri, title ? title : "", tags);
-        } else if (title) {
-            fprintf(f, "%s\t%s\n", uri, title);
-        } else {
-            fprintf(f, "%s\n", uri);
-        }
-
-        file_lock_set(fileno(f), F_UNLCK);
-        fclose(f);
-
-        return true;
+    const char *file = vb.files[FILES_BOOKMARK];
+    if (tags) {
+        return util_file_append(file, "%s\t%s\t%s\n", uri, title ? title : "", tags);
+    }
+    if (title) {
+        return util_file_append(file, "%s\t%s\n", uri, title);
     }
-    return false;
+    return util_file_append(file, "%s\n", uri);
 }
 
 gboolean bookmark_remove(const char *uri)
@@ -159,6 +148,43 @@ gboolean bookmark_fill_completion(GtkListStore *store, const char *input)
     return found;
 }
 
+/**
+ * Push a uri to the end of the queue.
+ *
+ * @uri: URI to put into the queue
+ */
+gboolean bookmark_queue_push(const char *uri)
+{
+    return util_file_append(vb.files[FILES_QUEUE], "%s\n", uri);
+}
+
+/**
+ * Retrieves the oldest entry from queue.
+ * Retruned uri must be freed with g_free.
+ */
+char *bookmark_queue_pop(void)
+{
+    int len, i;
+    char **lines, *uri = NULL;
+
+    lines = util_get_lines(vb.files[FILES_QUEUE]);
+    if (lines && (len = g_strv_length(lines))) {
+        GString *new = g_string_new(NULL);
+
+        uri = g_strdup(lines[0]);
+        /* don't process last empty line */
+        len -= 1; 
+        /* skip the first list that should be removed */
+        for (i = 1; i < len; i++) {
+            g_string_append_printf(new, "%s\n", lines[i]);
+        }
+        g_strfreev(lines);
+        g_file_set_contents(vb.files[FILES_QUEUE], new->str, -1, NULL);
+        g_string_free(new, true);
+    }
+    return uri;
+}
+
 static GList *load(const char *file)
 {
     return util_file_to_unique_list(
index 1a2e1d1..c1986d4 100644 (file)
@@ -23,5 +23,7 @@
 gboolean bookmark_add(const char *uri, const char *title, const char *tags);
 gboolean bookmark_remove(const char *uri);
 gboolean bookmark_fill_completion(GtkListStore *store, const char *input);
+gboolean bookmark_queue_push(const char *uri);
+char *bookmark_queue_pop(void);
 
 #endif /* end of include guard: _BOOKMARK_H */
index 24feb1d..6f33e19 100644 (file)
@@ -121,6 +121,8 @@ static CommandInfo cmd_list[] = {
     {"descent!",                  NULL,    command_descent,              {1}},
     {"save",                      NULL,    command_save,                 {COMMAND_SAVE_CURRENT}},
     {"shellcmd",                  NULL,    command_shellcmd,             {0}},
+    {"push",                      NULL,    command_queue,                {COMMAND_QUEUE_PUSH}},
+    {"pop",                       NULL,    command_queue,                {COMMAND_QUEUE_POP}},
 };
 
 static void editor_resume(GPid pid, int status, OpenEditorData *data);
@@ -731,12 +733,12 @@ gboolean command_bookmark(const Arg *arg)
 
     if (!arg->i) {
         if (bookmark_remove(arg->s ? arg->s : GET_URI())) {
-            vb_echo_force(VB_MSG_NORMAL, false, "Bookmark removed");
+            vb_echo_force(VB_MSG_NORMAL, false, "  Bookmark removed");
 
             return true;
         }
     } else if (bookmark_add(GET_URI(), webkit_web_view_get_title(vb.gui.webview), arg->s)) {
-        vb_echo_force(VB_MSG_NORMAL, false, "Bookmark added");
+        vb_echo_force(VB_MSG_NORMAL, false, "  Bookmark added");
 
         return true;
     }
@@ -886,6 +888,31 @@ gboolean command_shellcmd(const Arg *arg)
     return false;
 }
 
+gboolean command_queue(const Arg *arg)
+{
+    gboolean res = false;
+    char *uri;
+
+    vb_set_mode(VB_MODE_NORMAL, false);
+
+    if (arg->i == COMMAND_QUEUE_PUSH) {
+        res = bookmark_queue_push(arg->s ? arg->s : GET_URI());
+        if (res) {
+            vb_echo(VB_MSG_NORMAL, false, "  Pushed to queue");
+        }
+        return res;
+    }
+
+    /* pop last added url from queue and open it */
+    if ((uri = bookmark_queue_pop())) {
+        res = vb_load_uri(&(Arg){VB_TARGET_CURRENT, uri});
+        g_free(uri);
+    } else {
+        vb_echo(VB_MSG_NORMAL, false, "  Queue is empty");
+    }
+    return res;
+}
+
 gboolean command_editor(const Arg *arg)
 {
     char *file_path = NULL;
index 32c8f0b..b12dccb 100644 (file)
@@ -44,6 +44,11 @@ enum {
     COMMAND_SAVE_URI
 };
 
+enum {
+    COMMAND_QUEUE_PUSH,
+    COMMAND_QUEUE_POP
+};
+
 typedef gboolean (*Command)(const Arg *arg);
 
 void command_init(void);
@@ -81,5 +86,6 @@ gboolean command_nextprev(const Arg *arg);
 gboolean command_descent(const Arg *arg);
 gboolean command_save(const Arg *arg);
 gboolean command_shellcmd(const Arg *arg);
+gboolean command_queue(const Arg *arg);
 
 #endif /* end of include guard: _COMMAND_H */
index aedf71b..e7335e3 100644 (file)
@@ -75,20 +75,12 @@ void history_cleanup(void)
  */
 void history_add(HistoryType type, const char *value, const char *additional)
 {
-    FILE *f;
     const char *file = get_file_by_type(type);
 
-    if ((f = fopen(file, "a+"))) {
-        file_lock_set(fileno(f), F_WRLCK);
-        if (additional) {
-            fprintf(f, "%s\t%s\n", value, additional);
-        } else {
-            fprintf(f, "%s\n", value);
-        }
-
-        file_lock_set(fileno(f), F_UNLCK);
-        fclose(f);
+    if (additional) {
+        util_file_append(file, "%s\t%s\n", value, additional);
     }
+    util_file_append(file, "%s\n", value);
 }
 
 /**
index b6cd352..c95256e 100644 (file)
@@ -863,6 +863,9 @@ static void init_files(void)
     vb.files[FILES_BOOKMARK] = g_build_filename(path, "bookmark", NULL);
     util_create_file_if_not_exists(vb.files[FILES_BOOKMARK]);
 
+    vb.files[FILES_QUEUE] = g_build_filename(path, "queue", NULL);
+    util_create_file_if_not_exists(vb.files[FILES_QUEUE]);
+
     vb.files[FILES_SCRIPT] = g_build_filename(path, "scripts.js", NULL);
 
     vb.files[FILES_USER_STYLE] = g_build_filename(path, "style.css", NULL);
index 15fc24a..060aa5f 100644 (file)
@@ -210,6 +210,7 @@ typedef enum {
     FILES_COMMAND,
     FILES_SEARCH,
     FILES_BOOKMARK,
+    FILES_QUEUE,
     FILES_USER_STYLE,
     FILES_LAST
 } VbFile;
index 8b536a1..4d2de65 100644 (file)
@@ -154,6 +154,32 @@ GList *util_file_to_unique_list(const char *filename, Util_Content_Func func,
     return gl;
 }
 
+/**
+ * Append new data to file.
+ *
+ * @file:   File to appen the data
+ * @format: Format string used to process va_list
+ */
+gboolean util_file_append(const char *file, const char *format, ...)
+{
+    va_list args;
+    FILE *f;
+
+    if ((f = fopen(file, "a+"))) {
+        file_lock_set(fileno(f), F_WRLCK);
+
+        va_start(args, format);
+        vfprintf(f, format, args);
+        va_end(args);
+
+        file_lock_set(fileno(f), F_UNLCK);
+        fclose(f);
+
+        return true;
+    }
+    return false;
+}
+
 char *util_strcasestr(const char *haystack, const char *needle)
 {
     unsigned char c1, c2;
index 388c064..7c9d65b 100644 (file)
@@ -34,6 +34,7 @@ char* util_get_file_contents(const char* filename, gsize* length);
 char** util_get_lines(const char* filename);
 GList *util_file_to_unique_list(const char *filename, Util_Content_Func func,
     GCompareFunc unique_func, GDestroyNotify free_func, unsigned int max_items);
+gboolean util_file_append(const char *file, const char *format, ...);
 char* util_strcasestr(const char* haystack, const char* needle);
 char *util_str_replace(const char* search, const char* replace, const char* string);
 gboolean util_create_tmp_file(const char *content, char **file);