about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-02-21 09:51:40 +0100
committerMichael Vetter <jubalh@iodoru.org>2020-02-21 09:51:40 +0100
commitfd0472721270f5ab133632fff1a958a52c318a6d (patch)
tree8860dafbc9fc1cef5a7d5d1490aaf07dfff5c55f /src
parent7fdc4ed7f677fcb78163045678dfc2f541795f19 (diff)
downloadprofani-tty-fd0472721270f5ab133632fff1a958a52c318a6d.tar.gz
pref: Use helper functions for setting/getting the encryption char
Dont duplicate code.
Diffstat (limited to 'src')
-rw-r--r--src/config/preferences.c62
1 files changed, 24 insertions, 38 deletions
diff --git a/src/config/preferences.c b/src/config/preferences.c
index a747c911..3e9629e1 100644
--- a/src/config/preferences.c
+++ b/src/config/preferences.c
@@ -868,14 +868,14 @@ prefs_get_roster_size(void)
     }
 }
 
-char*
-prefs_get_otr_char(void)
+static char*
+_prefs_get_encryption_char(const char *const ch, const char *const pref_group, const char *const key)
 {
-    char *result = "~";
+    char *result = NULL;
 
-    char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_OTR, "otr.char", NULL);
+    char *resultstr = g_key_file_get_string(prefs, pref_group, key, NULL);
     if (!resultstr) {
-        result = strdup("~");
+        result = strdup(ch);
     } else {
         result = resultstr;
     }
@@ -883,64 +883,50 @@ prefs_get_otr_char(void)
     return result;
 }
 
-void
-prefs_set_otr_char(char *ch)
+static void
+_prefs_set_encryption_char(const char *const ch, const char *const pref_group, const char *const key)
 {
     if (g_utf8_strlen(ch, 4) == 1) {
-        g_key_file_set_string(prefs, PREF_GROUP_OTR, "otr.char", ch);
+        g_key_file_set_string(prefs, pref_group, key, ch);
     } else {
-        log_error("Could not set otr char: %s", ch);
+        log_error("Could not set %s encryption char to: %s", key, ch);
     }
 }
 
 char*
-prefs_get_pgp_char(void)
+prefs_get_otr_char(void)
 {
-    char *result = "~";
+    return _prefs_get_encryption_char("~", PREF_GROUP_OTR, "otr.char");
+}
 
-    char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_PGP, "pgp.char", NULL);
-    if (!resultstr) {
-        result = strdup("~");
-    } else {
-        result = resultstr;
-    }
+void
+prefs_set_otr_char(char *ch)
+{
+    _prefs_set_encryption_char(ch, PREF_GROUP_OTR, "otr.char");
+}
 
-    return result;
+char*
+prefs_get_pgp_char(void)
+{
+    return _prefs_get_encryption_char("~", PREF_GROUP_PGP, "pgp.char");
 }
 
 void
 prefs_set_pgp_char(char *ch)
 {
-    if (g_utf8_strlen(ch, 4) == 1) {
-        g_key_file_set_string(prefs, PREF_GROUP_PGP, "pgp.char", ch);
-    } else {
-        log_error("Could not set pgp char: %s", ch);
-    }
+    _prefs_set_encryption_char(ch, PREF_GROUP_PGP, "pgp.char");
 }
 
 char*
 prefs_get_omemo_char(void)
 {
-    char *result = "~";
-
-    char *resultstr = g_key_file_get_string(prefs, PREF_GROUP_OMEMO, "omemo.char", NULL);
-    if (!resultstr) {
-        result = strdup("~");
-    } else {
-        result = resultstr;
-    }
-
-    return result;
+    return _prefs_get_encryption_char("~", PREF_GROUP_OMEMO, "omemo.char");
 }
 
 void
 prefs_set_omemo_char(char *ch)
 {
-    if (g_utf8_strlen(ch, 4) == 1) {
-        g_key_file_set_string(prefs, PREF_GROUP_OMEMO, "omemo.char", ch);
-    } else {
-        log_error("Could not set omemo char: %s", ch);
-    }
+    _prefs_set_encryption_char(ch, PREF_GROUP_OMEMO, "omemo.char");
 }
 
 char