about summary refs log tree commit diff stats
path: root/src/config
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-07-02 11:26:18 +0200
committerGitHub <noreply@github.com>2020-07-02 11:26:18 +0200
commit9774b0c5509193027d4f68df9dcb862455699cfc (patch)
tree1ade86abc718ae2cc06199c12c5d6a4f0cb49df0 /src/config
parent3af5f33489d1e065c9da03b0bb99eddff57816a4 (diff)
parent86cd33405e7f67647ce2c171e19bab4120201140 (diff)
downloadprofani-tty-9774b0c5509193027d4f68df9dcb862455699cfc.tar.gz
Merge pull request #1374 from profanity-im/revampUrlopen
Rework /url and /executable for filetypes
Diffstat (limited to 'src/config')
-rw-r--r--src/config/preferences.c147
-rw-r--r--src/config/preferences.h5
2 files changed, 145 insertions, 7 deletions
diff --git a/src/config/preferences.c b/src/config/preferences.c
index 87f1acad..e46cbe45 100644
--- a/src/config/preferences.c
+++ b/src/config/preferences.c
@@ -65,6 +65,7 @@
 #define PREF_GROUP_OX "ox"
 #define PREF_GROUP_MUC "muc"
 #define PREF_GROUP_PLUGINS "plugins"
+#define PREF_GROUP_EXECUTABLES "executables"
 
 #define INPBLOCK_DEFAULT 1000
 
@@ -80,6 +81,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)
 {
@@ -160,6 +162,27 @@ static void _prefs_load(void)
         }
     }
 
+    // 0.9.0 introduced /urlopen. It was saved under "logging" section. Now we have a new "executables" section.
+    if (g_key_file_has_key(prefs, PREF_GROUP_LOGGING, "urlopen.cmd", NULL)) {
+        char *val = g_key_file_get_string(prefs, PREF_GROUP_LOGGING, "urlopen.cmd", NULL);
+
+        GString *value = g_string_new("false;");
+        value = g_string_append(value, val);
+        value = g_string_append(value, " %u;");
+
+        g_key_file_set_locale_string(prefs, PREF_GROUP_EXECUTABLES, "url.open.cmd", "DEF", value->str);
+        g_key_file_remove_key(prefs, PREF_GROUP_LOGGING, "urlopen.cmd", NULL);
+
+        g_string_free(value, TRUE);
+    }
+
+    // 0.9.0 introduced configurable /avatar. It was saved under "logging" section. Now we have a new "executables" section.
+    if (g_key_file_has_key(prefs, PREF_GROUP_LOGGING, "avatar.cmd", NULL)) {
+        char *value = g_key_file_get_string(prefs, PREF_GROUP_LOGGING, "avatar.cmd", NULL);
+        g_key_file_set_string(prefs, PREF_GROUP_EXECUTABLES, "avatar.cmd", value);
+        g_key_file_remove_key(prefs, PREF_GROUP_LOGGING, "avatar.cmd", NULL);
+    }
+
     _save_prefs();
 
     boolean_choice_ac = autocomplete_new();
@@ -218,7 +241,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();
 }
@@ -494,12 +517,62 @@ 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) {
+        // check for user set default
+        result = g_key_file_get_locale_string(prefs, group, key, "DEF", NULL);
+        if (result == NULL) {
+            if (def) {
+                // use hardcoded profanity default
+                return g_strdup(def);
+            } else {
+                return NULL;
+            }
+        }
+    }
+
+    return result;
+}
+
+gchar**
+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) {
+        g_strfreev(def);
+        return result;
+    }
+
+    result = g_key_file_get_string_list(prefs, group, key, NULL, NULL);
+    if (result) {
+        g_strfreev(def);
+        return result;
+    }
+
+    if (def) {
+        return def;
+    } else {
+        g_strfreev(def);
+        return NULL;
+    }
+}
+
 void
 prefs_free_string(char *pref)
 {
-    if (pref) {
-        g_free(pref);
-    }
+    g_free(pref);
 }
 
 void
@@ -514,6 +587,42 @@ 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){
+        if (g_strcmp0(option, "*") == 0) {
+            g_key_file_set_string_list(prefs, group, key, NULL, 0);
+        } else {
+            g_key_file_set_locale_string_list(prefs, group, key, option, NULL, 0);
+        }
+    } else {
+        guint num_values = 0;
+        while(values[num_values]) {
+            num_values++;
+        }
+        if (g_strcmp0(option, "*") == 0) {
+            g_key_file_set_string_list(prefs, group, key, values, num_values);
+        } else {
+            g_key_file_set_locale_string_list(prefs, group, key, option, values, num_values);
+        }
+    }
+}
+
 char*
 prefs_get_tls_certpath(void)
 {
@@ -1796,9 +1905,11 @@ _get_group(preference_t pref)
         case PREF_GRLOG:
         case PREF_LOG_ROTATE:
         case PREF_LOG_SHARED:
+            return PREF_GROUP_LOGGING;
         case PREF_AVATAR_CMD:
         case PREF_URL_OPEN_CMD:
-            return PREF_GROUP_LOGGING;
+        case PREF_URL_SAVE_CMD:
+            return PREF_GROUP_EXECUTABLES;
         case PREF_AUTOAWAY_CHECK:
         case PREF_AUTOAWAY_MODE:
         case PREF_AUTOAWAY_MESSAGE:
@@ -2088,7 +2199,9 @@ _get_key(preference_t pref)
         case PREF_MAM:
             return "mam";
         case PREF_URL_OPEN_CMD:
-            return "urlopen.cmd";
+            return "url.open.cmd";
+        case PREF_URL_SAVE_CMD:
+            return "url.save.cmd";
         default:
             return NULL;
     }
@@ -2225,8 +2338,28 @@ _get_default_string(preference_t pref)
         case PREF_COLOR_NICK:
             return "false";
         case PREF_AVATAR_CMD:
-        case PREF_URL_OPEN_CMD:
             return "xdg-open";
+        case PREF_URL_SAVE_CMD:
+            return "curl -o %p %u";
+        default:
+            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)
+{
+    char **str_array = NULL;
+
+    switch (pref)
+    {
+        case PREF_URL_OPEN_CMD:
+            str_array = g_malloc0(3);
+            str_array[0] = g_strdup("false");
+            str_array[1] = g_strdup("xdg-open %u");
+            return str_array;
         default:
             return NULL;
     }
diff --git a/src/config/preferences.h b/src/config/preferences.h
index 920342b8..e3110904 100644
--- a/src/config/preferences.h
+++ b/src/config/preferences.h
@@ -172,6 +172,7 @@ typedef enum {
     PREF_SLASH_GUARD,
     PREF_MAM,
     PREF_URL_OPEN_CMD,
+    PREF_URL_SAVE_CMD,
 } preference_t;
 
 typedef struct prof_alias_t {
@@ -317,8 +318,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);
+gchar **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);