Add profile option #343.
authorDaniel Carl <danielcarl@gmx.de>
Sat, 22 Apr 2017 20:46:11 +0000 (22:46 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 22 Apr 2017 20:46:11 +0000 (22:46 +0200)
doc/vimb.1
src/config.def.h
src/main.c
src/main.h
src/util.c
src/util.h

index 58ee702..5029790 100644 (file)
@@ -39,6 +39,11 @@ of an XEmbed-aware application, that Vimb will use as its parent.
 .B "\-h, \-\-help"
 Show help options.
 .TP
+.BI "\-p, \-\-profile " "PROFILE-NAME"
+Create or open specified configuration profile.
+Configuration data for the profile is stored in a directory named
+\fIPROFILE-NAME\fP under default directory for configuration data.
+.TP
 .B "\-v, \-\-version"
 Print build and version information.
 .SH MODES
index 999a660..2682a9d 100644 (file)
@@ -18,8 +18,6 @@
  */
 
 /* features */
-/* should the history indicator [+-] be shown in status bar after url */
-#define FEATURE_HISTORY_INDICATOR
 /* show wget style progressbar in status bar */
 #define FEATURE_WGET_PROGRESS_BAR
 /* show load progress in window title */
index 0b35304..6b014ea 100644 (file)
@@ -96,6 +96,8 @@ static void vimb_setup(void);
 static WebKitWebView *webview_new(Client *c, WebKitWebView *webview);
 static void on_script_message_focus(WebKitUserContentManager *manager,
         WebKitJavascriptResult *message, Client *c);
+static gboolean profileOptionArgFunc(const gchar *option_name,
+        const gchar *value, gpointer data, GError **error);
 
 struct Vimb vb;
 
@@ -882,21 +884,38 @@ static void set_title(Client *c, const char *title)
 static void spawn_new_instance(const char *uri, gboolean embed)
 {
     guint i = 0;
-    char xid[64];
-    char *cmd[5];
+    /* memory allocation */
+    char **cmd = g_malloc_n(
+        3                       /* basename + uri + ending NULL */
+#ifndef FEATURE_NO_XEMBED
+        + (vb.embed && embed ? 2 : 0)
+#endif
+        + (vb.profile ? 2 : 0),
+        sizeof(char *)
+    );
 
     cmd[i++] = vb.argv0;
 
+#ifndef FEATURE_NO_XEMBED
     if (vb.embed && embed) {
+        char xid[64];
         cmd[i++] = "-e";
         snprintf(xid, LENGTH(xid), "%d", (int)vb.embed);
         cmd[i++] = xid;
     }
+#endif
+    if (vb.profile) {
+        cmd[i++] = "-p";
+        cmd[i++] = vb.profile;
+    }
     cmd[i++] = (char*)uri;
     cmd[i++] = NULL;
 
     /* spawn a new browser instance */
     g_spawn_async(NULL, cmd, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
+
+    /* free commandline */
+    g_free(cmd);
 }
 
 /**
@@ -1398,6 +1417,7 @@ static void vimb_cleanup(void)
             g_free(vb.files[i]);
         }
     }
+    g_free(vb.profile);
 }
 #endif
 
@@ -1640,6 +1660,14 @@ static void on_script_message_focus(WebKitUserContentManager *manager,
     }
 }
 
+static gboolean profileOptionArgFunc(const gchar *option_name,
+        const gchar *value, gpointer data, GError **error)
+{
+    vb.profile = util_sanitize_filename(g_strdup(value));
+
+    return TRUE;
+}
+
 int main(int argc, char* argv[])
 {
     Client *c;
@@ -1650,6 +1678,7 @@ int main(int argc, char* argv[])
     GOptionEntry opts[] = {
         {"embed", 'e', 0, G_OPTION_ARG_STRING, &winid,  "Reparents to window specified by xid", NULL},
         {"config", 'c', 0, G_OPTION_ARG_FILENAME, &vb.configfile, "Custom configuration file", NULL},
+        {"profile", 'p', 0, G_OPTION_ARG_CALLBACK, profileOptionArgFunc, "Profile name", NULL},
         {"version", 'v', 0, G_OPTION_ARG_NONE, &ver, "Print version", NULL},
         {NULL}
     };
index a2ab13a..36fac57 100644 (file)
@@ -256,6 +256,7 @@ struct Vimb {
     GHashTable  *modes;             /* all available browser main modes */
     char        *configfile;        /* config file given as option on startup */
     char        *files[FILES_LAST];
+    char        *profile;           /* profile name */
     struct {
         guint   history_max;
     } config;
index be3b5c4..8f1c2b4 100644 (file)
@@ -35,6 +35,8 @@ static struct {
     char    *config_dir;
 } util;
 
+extern struct Vimb vb;
+
 static void create_dir_if_not_exists(const char *dirpath);
 
 /**
@@ -248,12 +250,12 @@ char *util_file_pop_line(const char *file, int *item_count)
 }
 
 /**
- * Retrieves the config directory path.
- * Returnes string must be freed.
+ * Retrieves the config directory path according to current used profile.
+ * Returned string must be freed.
  */
 char *util_get_config_dir(void)
 {
-    char *path = g_build_filename(g_get_user_config_dir(), PROJECT, NULL);
+    char *path = g_build_filename(g_get_user_config_dir(), PROJECT, vb.profile, NULL);
     create_dir_if_not_exists(path);
 
     return path;
@@ -645,6 +647,16 @@ gboolean util_parse_expansion(Client *c, const char **input, GString *str,
     return expanded;
 }
 
+/**
+ * Sanituze filename by removeing directory separator by underscore.
+ *
+ * The string is modified in place.
+ */
+char *util_sanitize_filename(char *filename)
+{
+    return g_strdelimit(filename, G_DIR_SEPARATOR_S, '_');
+}
+
 char *util_strcasestr(const char *haystack, const char *needle)
 {
     guchar c1, c2;
index c1830a2..f416e5c 100644 (file)
@@ -49,7 +49,8 @@ char *util_js_result_as_string(WebKitJavascriptResult *result);
 double util_js_result_as_number(WebKitJavascriptResult *result);
 gboolean util_parse_expansion(Client *c, const char **input, GString *str,
         int flags, const char *quoteable);
-char *util_str_replace(const char* search, const char* replace, const char* string);
+char *util_sanitize_filename(char *filename);
 char *util_strcasestr(const char *haystack, const char *needle);
+char *util_str_replace(const char* search, const char* replace, const char* string);
 
 #endif /* end of include guard: _UTIL_H */