Skip to content

Commit

Permalink
Added ephemeral mode #562.
Browse files Browse the repository at this point in the history
If vimb is started with --ephemeral option no files are created by
default and no persistent cookie manager is used.
  • Loading branch information
fanglingsu committed Jun 4, 2019
1 parent 67d3063 commit f9c04a2
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 98 deletions.
7 changes: 6 additions & 1 deletion src/bookmark.c
Expand Up @@ -236,7 +236,12 @@ gboolean bookmark_queue_clear(void)

static GList *load(const char *file)
{
return util_file_to_unique_list(file, (Util_Content_Func)line_to_bookmark, 0);
char **lines;
GList *list;
lines = util_get_lines(file);
list = util_strv_to_unique_list(lines, (Util_Content_Func)line_to_bookmark, 0);
g_strfreev(lines);
return list;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ex.c
Expand Up @@ -956,7 +956,7 @@ static VbCmdResult ex_queue(Client *c, const ExArg *arg)

return command_queue(c, &a)
? CMD_SUCCESS | CMD_KEEPINPUT
: CMD_ERROR | CMD_KEEPINPUT;
: CMD_ERROR;
}
#endif

Expand Down
30 changes: 16 additions & 14 deletions src/file-storage.c
Expand Up @@ -23,7 +23,6 @@
#include <glib/gstdio.h>

#include "file-storage.h"
#include "util.h"

struct filestorage {
char *file_path;
Expand All @@ -44,26 +43,19 @@ struct filestorage {
* creating it. If 0 the file is not created and the storage is
* used in read only mode - no data written to the file.
*/
FileStorage *file_storage_new(const char *dir, const char *filename, int mode)
FileStorage *file_storage_new(const char *dir, const char *filename, gboolean readonly)
{
char *fullpath;
FileStorage *storage;

storage = g_slice_new(FileStorage);
storage->readonly = (mode == 0);

/* Built the full path out of dir and given file name. */
fullpath = g_build_filename(dir, filename, NULL);
if (!g_file_test(fullpath, G_FILE_TEST_IS_REGULAR) && mode) {
/* If create option was given - create the file. */
fclose(fopen(fullpath, "a"));
g_chmod(fullpath, mode);
}
storage->file_path = fullpath;
storage = g_slice_new(FileStorage);
storage->readonly = readonly;
storage->file_path = g_build_filename(dir, filename, NULL);

/* Use gstring as storage in case when the file is used read only. */
if (storage->readonly) {
storage->str = g_string_new(NULL);
} else {
storage->str = NULL;
}

return storage;
Expand Down Expand Up @@ -147,3 +139,13 @@ char **file_storage_get_lines(FileStorage *storage)

return lines;
}

const char *file_storage_get_path(FileStorage *storage)
{
return storage->file_path;
}

gboolean file_storage_is_readonly(FileStorage *storage)
{
return storage->readonly;
}
2 changes: 2 additions & 0 deletions src/file-storage.h
Expand Up @@ -27,5 +27,7 @@ FileStorage *file_storage_new(const char *dir, const char *filename, int mode);
void file_storage_free(FileStorage *storage);
gboolean file_storage_append(FileStorage *storage, const char *format, ...);
char **file_storage_get_lines(FileStorage *storage);
const char *file_storage_get_path(FileStorage *storage);
gboolean file_storage_is_readonly(FileStorage *storage);

#endif /* end of include guard: _FILE_STORAGE_H */
47 changes: 26 additions & 21 deletions src/history.c
Expand Up @@ -27,8 +27,9 @@
#include "history.h"
#include "main.h"
#include "util.h"
#include "file-storage.h"

#define HIST_FILE(t) (vb.files[file_map[t]])
#define HIST_STORAGE(t) (vb.storage[storage_map[t]])
typedef struct {
char *first;
char *second;
Expand All @@ -37,14 +38,14 @@ typedef struct {
static gboolean history_item_contains_all_tags(History *item, char **query, guint qlen);
static void free_history(History *item);
static History *line_to_history(const char *uri, const char *title);
static GList *load(const char *file);
static GList *load(FileStorage *s);
static void write_to_file(GList *list, const char *file);

/* map history types to files */
static const int file_map[HISTORY_LAST] = {
FILES_COMMAND,
FILES_SEARCH,
FILES_HISTORY
static const int storage_map[HISTORY_LAST] = {
STORAGE_COMMAND,
STORAGE_SEARCH,
STORAGE_HISTORY
};
extern struct Vimb vb;

Expand All @@ -53,18 +54,18 @@ extern struct Vimb vb;
*/
void history_add(Client *c, HistoryType type, const char *value, const char *additional)
{
const char *file;
FileStorage *s;

/* Don't write a history entry if the history max size is set to 0. */
if (!vb.config.history_max) {
return;
}

file = HIST_FILE(type);
s = HIST_STORAGE(type);
if (additional) {
util_file_append(file, "%s\t%s\n", value, additional);
file_storage_append(s, "%s\t%s\n", value, additional);
} else {
util_file_append(file, "%s\n", value);
file_storage_append(s, "%s\n", value);
}
}

Expand All @@ -74,7 +75,7 @@ void history_add(Client *c, HistoryType type, const char *value, const char *add
*/
void history_cleanup(void)
{
const char *file;
FileStorage *s;
GList *list;

/* don't cleanup the history file if history max size is 0 */
Expand All @@ -83,10 +84,12 @@ void history_cleanup(void)
}

for (HistoryType i = HISTORY_FIRST; i < HISTORY_LAST; i++) {
file = HIST_FILE(i);
list = load(file);
write_to_file(list, file);
g_list_free_full(list, (GDestroyNotify)free_history);
s = HIST_STORAGE(i);
if (!file_storage_is_readonly(s)) {
list = load(s);
write_to_file(list, file_storage_get_path(s));
g_list_free_full(list, (GDestroyNotify)free_history);
}
}
}

Expand All @@ -99,7 +102,7 @@ gboolean history_fill_completion(GtkListStore *store, HistoryType type, const ch
GtkTreeIter iter;
History *item;

src = load(HIST_FILE(type));
src = load(HIST_STORAGE(type));
src = g_list_reverse(src);
if (!input || !*input) {
/* without any tags return all items */
Expand Down Expand Up @@ -168,12 +171,12 @@ GList *history_get_list(VbInputType type, const char *query)

switch (type) {
case INPUT_COMMAND:
src = load(HIST_FILE(HISTORY_COMMAND));
src = load(HIST_STORAGE(HISTORY_COMMAND));
break;

case INPUT_SEARCH_FORWARD:
case INPUT_SEARCH_BACKWARD:
src = load(HIST_FILE(HISTORY_SEARCH));
src = load(HIST_STORAGE(HISTORY_SEARCH));
break;

default:
Expand Down Expand Up @@ -241,10 +244,12 @@ static History *line_to_history(const char *uri, const char *title)
*
* Returned list must be freed with (GDestroyNotify)free_history.
*/
static GList *load(const char *file)
static GList *load(FileStorage *s)
{
return util_file_to_unique_list(
file, (Util_Content_Func)line_to_history, vb.config.history_max
return util_strv_to_unique_list(
file_storage_get_lines(s),
(Util_Content_Func)line_to_history,
vb.config.history_max
);
}

Expand Down
28 changes: 18 additions & 10 deletions src/main.c
Expand Up @@ -46,6 +46,7 @@
#include "shortcut.h"
#include "util.h"
#include "autocmd.h"
#include "file-storage.h"

static void client_destroy(Client *c);
static Client *client_new(WebKitWebView *webview);
Expand Down Expand Up @@ -1768,6 +1769,9 @@ static void vimb_cleanup(void)
/* free memory of other components */
util_cleanup();

for (i = 0; i < STORAGE_LAST; i++) {
file_storage_free(vb.storage[i]);
}
for (i = 0; i < FILES_LAST; i++) {
if (vb.files[i]) {
g_free(vb.files[i]);
Expand All @@ -1794,19 +1798,22 @@ static void vimb_setup(void)
vb.files[FILES_CONFIG] = g_strdup(rp);
free(rp);
} else {
vb.files[FILES_CONFIG] = util_get_filepath(path, "config", FALSE, 0600);
vb.files[FILES_CONFIG] = g_build_filename(path, "config", NULL);
}

/* Setup those files that are use multiple time during runtime */
vb.files[FILES_CLOSED] = util_get_filepath(path, "closed", TRUE, 0600);
vb.files[FILES_COOKIE] = util_get_filepath(path, "cookies.db", TRUE, 0600);
vb.files[FILES_USER_STYLE] = util_get_filepath(path, "style.css", FALSE, 0600);
vb.files[FILES_SCRIPT] = util_get_filepath(path, "scripts.js", FALSE, 0600);
vb.files[FILES_HISTORY] = util_get_filepath(path, "history", TRUE, 0600);
vb.files[FILES_COMMAND] = util_get_filepath(path, "command", TRUE, 0600);
vb.files[FILES_BOOKMARK] = util_get_filepath(path, "bookmark", TRUE, 0600);
vb.files[FILES_QUEUE] = util_get_filepath(path, "queue", TRUE, 0600);
vb.files[FILES_SEARCH] = util_get_filepath(path, "search", TRUE, 0600);
if (!vb.ephemeral) {
vb.files[FILES_CLOSED] = g_build_filename(path, "closed", NULL);
vb.files[FILES_COOKIE] = g_build_filename(path, "cookies.db", NULL);
}
vb.files[FILES_BOOKMARK] = g_build_filename(path, "bookmark", NULL);
vb.files[FILES_QUEUE] = g_build_filename(path, "queue", NULL);
vb.files[FILES_SCRIPT] = g_build_filename(path, "scripts.js", NULL);
vb.files[FILES_USER_STYLE] = g_build_filename(path, "style.css", NULL);

vb.storage[STORAGE_HISTORY] = file_storage_new(path, "history", vb.ephemeral);
vb.storage[STORAGE_COMMAND] = file_storage_new(path, "command", vb.ephemeral);
vb.storage[STORAGE_SEARCH] = file_storage_new(path, "search", vb.ephemeral);
g_free(path);

/* Use seperate rendering processed for the webview of the clients in the
Expand Down Expand Up @@ -2066,6 +2073,7 @@ int main(int argc, char* argv[])

GOptionEntry opts[] = {
{"config", 'c', 0, G_OPTION_ARG_FILENAME, &vb.configfile, "Custom configuration file", NULL},
{"ephemeral", 'E', 0, G_OPTION_ARG_NONE, &vb.ephemeral, "Run in ephemeral mode", NULL},
{"embed", 'e', 0, G_OPTION_ARG_STRING, &winid, "Reparents to window specified by xid", NULL},
{"profile", 'p', 0, G_OPTION_ARG_CALLBACK, (GOptionArgFunc*)profileOptionArgFunc, "Profile name", NULL},
{"version", 'v', 0, G_OPTION_ARG_NONE, &ver, "Print version", NULL},
Expand Down
15 changes: 12 additions & 3 deletions src/main.h
Expand Up @@ -26,6 +26,7 @@
#include <webkit2/webkit2.h>
#include "shortcut.h"
#include "handler.h"
#include "file-storage.h"

#include "config.h"

Expand Down Expand Up @@ -105,17 +106,23 @@ typedef enum {
enum {
FILES_BOOKMARK,
FILES_CLOSED,
FILES_COMMAND,
FILES_CONFIG,
FILES_COOKIE,
FILES_HISTORY,
FILES_QUEUE,
FILES_SCRIPT,
FILES_SEARCH,
FILES_USER_STYLE,
FILES_LAST
};

enum {
STORAGE_CLOSED,
STORAGE_COMMAND,
STORAGE_CONFIG,
STORAGE_HISTORY,
STORAGE_SEARCH,
STORAGE_LAST
};

typedef enum {
LINK_TYPE_NONE,
LINK_TYPE_LINK,
Expand Down Expand Up @@ -268,13 +275,15 @@ struct Vimb {
GHashTable *modes; /* all available browser main modes */
char *configfile; /* config file given as option on startup */
char *files[FILES_LAST];
FileStorage *storage[STORAGE_LAST];
char *profile; /* profile name */
struct {
guint history_max;
guint closed_max;
} config;
GtkCssProvider *style_provider;
gboolean no_maximize;
gboolean ephemeral;
};

gboolean vb_download_set_destination(Client *c, WebKitDownload *download,
Expand Down
4 changes: 2 additions & 2 deletions src/setting.c
Expand Up @@ -674,7 +674,7 @@ static int user_style(Client *c, const char *name, DataType type, void *value, v

ucm = webkit_web_view_get_user_content_manager(c->webview);

if (enabled && vb.files[FILES_USER_STYLE]) {
if (enabled) {
if (g_file_get_contents(vb.files[FILES_USER_STYLE], &source, NULL, NULL)) {
style = webkit_user_style_sheet_new(
source, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
Expand All @@ -685,7 +685,7 @@ static int user_style(Client *c, const char *name, DataType type, void *value, v
webkit_user_style_sheet_unref(style);
g_free(source);
} else {
g_warning("Could not reed style file: %s", vb.files[FILES_USER_STYLE]);
g_message("Could not reed style file: %s", vb.files[FILES_USER_STYLE]);
}
} else {
webkit_user_content_manager_remove_all_style_sheets(ucm);
Expand Down

0 comments on commit f9c04a2

Please sign in to comment.