about summary refs log tree commit diff stats
path: root/src/config
diff options
context:
space:
mode:
authorPierre Mazière <pierre.maziere@gmx.com>2020-06-12 09:07:59 +0200
committerMichael Vetter <jubalh@iodoru.org>2020-06-25 15:03:04 +0200
commit7e652f4ca0f5dd26c841137ac15e23e74f9cc920 (patch)
tree458f88929f67cbe24eb4528b6a60c7f3ef686ee4 /src/config
parent96c877de80f5f208a41ba775c2dd13746a09d7fd (diff)
downloadprofani-tty-7e652f4ca0f5dd26c841137ac15e23e74f9cc920.tar.gz
Add string and string list preferences with option
Where GKeyFile usually use the pref[locale] format to define
locale specific translated data, it is here hijacked to be used
as pref[option] in order to specialize a preference according
to an option:

open.url.cmd[pdf] = pdf-viewer
open.url.cmd[jpg] = image-viewer

Signed-off-by: Pierre Mazière <pierre.maziere@gmx.com>
Diffstat (limited to 'src/config')
-rw-r--r--src/config/preferences.c86
-rw-r--r--src/config/preferences.h4
2 files changed, 89 insertions, 1 deletions
diff --git a/src/config/preferences.c b/src/config/preferences.c
index 093d7679..a3a90692 100644
--- a/src/config/preferences.c
+++ b/src/config/preferences.c
@@ -79,6 +79,7 @@ static const char* _get_group(preference_t pref);
 static const char* _get_key(preference_t pref);
 static gboolean _get_default_boolean(preference_t pref);
 static char* _get_default_string(preference_t pref);
+static char** _get_default_string_list(preference_t pref);
 
 static void _prefs_load(void)
 {
@@ -217,7 +218,7 @@ prefs_load(char *config_file)
     }
 
     prefs = g_key_file_new();
-    g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS, NULL);
+    g_key_file_load_from_file(prefs, prefs_loc, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
 
     _prefs_load();
 }
@@ -493,6 +494,49 @@ prefs_get_string(preference_t pref)
     }
 }
 
+char*
+prefs_get_string_with_option(preference_t pref, gchar *option)
+{
+    const char *group = _get_group(pref);
+    const char *key = _get_key(pref);
+    char *def = _get_default_string(pref);
+
+    char *result = g_key_file_get_locale_string(prefs, group, key, option, NULL);
+
+    if (result == NULL) {
+        if (def) {
+            return g_strdup(def);
+        } else {
+            return NULL;
+        }
+    } else {
+        return result;
+    }
+}
+
+char**
+prefs_get_string_list_with_option(preference_t pref, gchar *option)
+{
+    const char *group = _get_group(pref);
+    const char *key = _get_key(pref);
+    char **def = _get_default_string_list(pref);
+
+    gchar **result = g_key_file_get_locale_string_list(prefs, group, key, option, NULL, NULL);
+
+    if (result == NULL) {
+        if (def) {
+            return def;
+        } else {
+            g_strfreev(def);
+            return NULL;
+        }
+    } else {
+        g_strfreev(def);
+        return result;
+    }
+}
+
+
 void
 prefs_free_string(char *pref)
 {
@@ -513,6 +557,34 @@ prefs_set_string(preference_t pref, char *value)
     }
 }
 
+void
+prefs_set_string_with_option(preference_t pref, char *option, char *value)
+{
+    const char *group = _get_group(pref);
+    const char *key = _get_key(pref);
+    if (value == NULL) {
+        g_key_file_remove_key(prefs, group, key, NULL);
+    } else {
+        g_key_file_set_locale_string(prefs, group, key, option, value);
+    }
+}
+
+void
+prefs_set_string_list_with_option(preference_t pref, char *option, const gchar* const *values)
+{
+    const char *group = _get_group(pref);
+    const char *key = _get_key(pref);
+    if (values == NULL || *values == NULL){
+        g_key_file_set_locale_string_list(prefs, group, key, option, NULL, 0);
+    } else {
+        guint num_values = 0;
+        while(values[num_values]) {
+          num_values++;
+        }
+        g_key_file_set_locale_string_list(prefs, group, key, option, values, num_values);
+    }
+}
+
 char*
 prefs_get_tls_certpath(void)
 {
@@ -2218,3 +2290,15 @@ _get_default_string(preference_t pref)
             return NULL;
     }
 }
+
+// the default setting for a string list type preference
+// if it is not specified in .profrc
+static char**
+_get_default_string_list(preference_t pref)
+{
+    switch (pref)
+    {
+        default:
+            return NULL;
+    }
+}
diff --git a/src/config/preferences.h b/src/config/preferences.h
index 5a38bfec..4c9b7bd8 100644
--- a/src/config/preferences.h
+++ b/src/config/preferences.h
@@ -314,8 +314,12 @@ void prefs_save_win_placement(ProfWinPlacement *placement);
 gboolean prefs_get_boolean(preference_t pref);
 void prefs_set_boolean(preference_t pref, gboolean value);
 char* prefs_get_string(preference_t pref);
+char* prefs_get_string_with_option(preference_t pref, gchar *option);
+char **prefs_get_string_list_with_option(preference_t pref, gchar *option);
 void prefs_free_string(char *pref);
 void prefs_set_string(preference_t pref, char *value);
+void prefs_set_string_with_option(preference_t pref, char *option, char *value);
+void prefs_set_string_list_with_option(preference_t pref, char *option, const gchar* const *values);
 
 char* prefs_get_tls_certpath(void);