Support for multiple configuration profiles (#129)
authorJiri Marsicek <jiri.marsicek@gmail.com>
Wed, 29 Jul 2015 18:29:24 +0000 (20:29 +0200)
committerJiri Marsicek <jiri.marsicek@gmail.com>
Wed, 29 Jul 2015 18:36:40 +0000 (20:36 +0200)
New parameter is introduced to allow multiple configuration profiles per
user.

If vimb is started with this parameter new configuration directory
is created under default configuration directory. Cache and socket file
are kept separate per profile as well.

If vimb is started without this parameter, behaviour is unchanged.

Resolves fanglingsu/vimb#129

doc/vimb.1
src/io.c
src/main.c
src/main.h
src/util.c
src/util.h

index b66ef80..bb96e0a 100644 (file)
@@ -44,6 +44,9 @@ vimb --cmd "set cookie-accept=origin|set header=Referer,DNT=1"
 Use custom configuration given as \fICONFIG-FILE\fP.
 This will also be applied on new spawned instances.
 .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
 .BI "\-e, \-\-embed " "WINID"
 .I WINID
 of an XEmbed-aware application, that Vimb will use as its parent.
@@ -1568,11 +1571,20 @@ These file is used if the config variable `stylesheet' is enabled.
 .PD
 .RE
 .TP
+.I $XDG_CONFIG_HOME/vimb/PROFILE-NAME
+Directory for configuration data if executed with \fB-p \fIPROFILE-NAME\fR parameter. It has same structure as default directory for configuration data.
+.TP
 .I $XDG_CACHE_HOME/vimb/
 Default directory for cache data.
 .TP
+.I $XDG_CACHE_HOME/vimb/PROFILE-NAME
+Directory for cache data if executed with \fB-p \fIPROFILE-NAME\fR parameter.
+.TP
 .I $XDG_RUNTIME_DIR/vimb/socket/
 Directory where the control sockets are placed.
+.TP
+.I $XDG_RUNTIME_DIR/vimb/PROFILE-NAME/socket/
+Directory where the control sockets are placed if executed with \fB-p \fIPROFILE-NAME\fR parameter.
 .PP
 There are also some sample scripts installed together with Vimb under
 PREFIX/share/vimb/examples.
index 1f022d3..0abac30 100644 (file)
--- a/src/io.c
+++ b/src/io.c
@@ -43,7 +43,7 @@ gboolean io_init_socket(const char *name)
     struct sockaddr_un local;
 
     /* create socket in runtime directory */
-    dir = g_build_filename(g_get_user_runtime_dir(), PROJECT, "socket", NULL);
+    dir = g_build_filename(util_get_runtime_dir(vb.config.profile), PROJECT, "socket", NULL);
     util_create_dir_if_not_exists(dir);
     path = g_build_filename(dir, name, NULL);
     g_free(dir);
index a5cb913..b981656 100644 (file)
@@ -359,6 +359,7 @@ gboolean vb_load_uri(const Arg *arg)
             3                       /* basename + uri + ending NULL */
             + (vb.embed ? 2 : 0)
             + (vb.config.file ? 2 : 0)
+            + (vb.config.profile ? 2 : 0)
             + (vb.config.kioskmode ? 1 : 0)
 #ifdef FEATURE_SOCKET
             + (vb.config.socket ? 1 : 0)
@@ -379,6 +380,10 @@ gboolean vb_load_uri(const Arg *arg)
             cmd[i++] = "-c";
             cmd[i++] = vb.config.file;
         }
+        if (vb.config.profile) {
+            cmd[i++] = "-p";
+            cmd[i++] = vb.config.profile;
+        }
         for (GSList *l = vb.config.cmdargs; l; l = l->next) {
             cmd[i++] = "-C";
             cmd[i++] = l->data;
@@ -1193,7 +1198,7 @@ static void setup_signals()
 
 static void init_files(void)
 {
-    char *path = util_get_config_dir();
+    char *path = util_get_config_dir(vb.config.profile);
 
     if (vb.config.file) {
         char *rp = realpath(vb.config.file, NULL);
@@ -1260,7 +1265,7 @@ static void session_init(void)
 #endif
 #ifdef FEATURE_SOUP_CACHE
     /* setup the soup cache but without setting the cache size - this is done in setting.c */
-    char *cache_dir      = util_get_cache_dir();
+    char *cache_dir      = util_get_cache_dir(vb.config.profile);
     vb.config.soup_cache = soup_cache_new(cache_dir, SOUP_CACHE_SINGLE_USER);
     soup_session_add_feature(vb.session, SOUP_SESSION_FEATURE(vb.config.soup_cache));
     soup_cache_load(vb.config.soup_cache);
@@ -1791,6 +1796,7 @@ int main(int argc, char *argv[])
     static GOptionEntry opts[] = {
         {"cmd", 'C', 0, G_OPTION_ARG_CALLBACK, autocmdOptionArgFunc, "Ex command run before first page is loaded", NULL},
         {"config", 'c', 0, G_OPTION_ARG_FILENAME, &vb.config.file, "Custom configuration file", NULL},
+        {"profile", 'p', 0, G_OPTION_ARG_STRING, &vb.config.profile, "Profile name", NULL},
         {"embed", 'e', 0, G_OPTION_ARG_STRING, &winid, "Reparents to window specified by xid", NULL},
 #ifdef FEATURE_SOCKET
         {"dump", 'd', 0, G_OPTION_ARG_NONE, &dump, "Dump the socket path to stdout", NULL},
index ddd8870..b112b11 100644 (file)
@@ -335,6 +335,7 @@ typedef struct {
     char         *nextpattern;     /* regex patter nfor prev link matching */
     char         *prevpattern;     /* regex patter nfor next link matching */
     char         *file;            /* path to the custome config file */
+    char         *profile;         /* profile name */
     GSList       *cmdargs;         /* list of commands given by --cmd option */
     char         *cafile;          /* path to the ca file */
     GTlsDatabase *tls_db;          /* tls database */
index 0b24380..b1a72c5 100644 (file)
@@ -32,24 +32,39 @@ static gboolean match(const char *pattern, int patlen, const char *subject);
 static gboolean match_list(const char *pattern, int patlen, const char *subject);
 
 /**
- * Retrieves newly allocated string with vimb config directory.
- * Retruned string must be freed.
+ * Retrieves newly allocated string with vimb config directory with profilename. 
+ * If profilename is NULL, path to default directory is returned.
+ * Returned string must be freed.
+ */
+char *util_get_config_dir(const char *profilename)
+{
+    char *path = g_build_filename(g_get_user_config_dir(), PROJECT, G_DIR_SEPARATOR_S, profilename, NULL);
+    util_create_dir_if_not_exists(path);
+
+    return path;
+}
+
+/**
+ * Retrieves the path to the cache dir with profilename
+ * If profilename is NULL, path to default directory is returned.
+ * Returned string must be freed.
  */
-char *util_get_config_dir(void)
+char *util_get_cache_dir(const char *profilename)
 {
-    char *path = g_build_filename(g_get_user_config_dir(), PROJECT, NULL);
+    char *path = g_build_filename(g_get_user_cache_dir(), PROJECT, G_DIR_SEPARATOR_S, profilename, NULL);
     util_create_dir_if_not_exists(path);
 
     return path;
 }
 
 /**
- * Retrieves the path to the cach dir.
+ * Retrieves the path to the socket dir with profilename
+ * If profilename is NULL, path to default directory is returned.
  * Returned string must be freed.
  */
-char *util_get_cache_dir(void)
+char *util_get_runtime_dir(const char *profilename)
 {
-    char *path = g_build_filename(g_get_user_cache_dir(), PROJECT, NULL);
+    char *path = g_build_filename(g_get_user_runtime_dir(), PROJECT, G_DIR_SEPARATOR_S, profilename, NULL);
     util_create_dir_if_not_exists(path);
 
     return path;
index 653001d..f73a631 100644 (file)
@@ -30,8 +30,9 @@ enum {
 
 typedef void *(*Util_Content_Func)(const char*, const char*);
 
-char* util_get_config_dir(void);
-char* util_get_cache_dir(void);
+char* util_get_config_dir(const char* profilename);
+char* util_get_cache_dir(const char* profilename);
+char* util_get_runtime_dir(const char* profilename);
 const char* util_get_home_dir(void);
 void util_create_dir_if_not_exists(const char* dirpath);
 void util_create_file_if_not_exists(const char* filename);