From 069bdbc331567e7d44b615d265be3e5f05ab62bd Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 3 Feb 2013 03:24:13 +0000 Subject: Refactored string preferences --- src/command/command.c | 8 ++--- src/config/preferences.c | 83 +++++++++++++++++++++++++----------------------- src/config/preferences.h | 13 +++----- src/profanity.c | 18 +++++------ src/ui/windows.c | 12 +++---- 5 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 24c1b8fa..eb8f0fe6 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1492,7 +1492,7 @@ _cmd_theme(gchar **args, struct cmd_help_t help) cons_show("Usage: %s", help.usage); } else if (theme_load(args[1])) { ui_load_colours(); - prefs_set_theme(args[1]); + prefs_set_string(PREF_THEME, args[1]); cons_show("Loaded theme: %s", args[1]); } else { cons_show("Couldn't find theme: %s", args[1]); @@ -2187,7 +2187,7 @@ _cmd_set_autoaway(gchar **args, struct cmd_help_t help) (strcmp(value, "off") != 0)) { cons_show("Mode must be one of 'idle', 'away' or 'off'"); } else { - prefs_set_autoaway_mode(value); + prefs_set_string(PREF_AUTOAWAY_MODE, value); cons_show("Auto away mode set to: %s.", value); } @@ -2205,10 +2205,10 @@ _cmd_set_autoaway(gchar **args, struct cmd_help_t help) if (strcmp(setting, "message") == 0) { if (strcmp(value, "off") == 0) { - prefs_set_autoaway_message(NULL); + prefs_set_string(PREF_AUTOAWAY_MESSAGE, NULL); cons_show("Auto away message cleared."); } else { - prefs_set_autoaway_message(value); + prefs_set_string(PREF_AUTOAWAY_MESSAGE, value); cons_show("Auto away message set to: \"%s\".", value); } diff --git a/src/config/preferences.c b/src/config/preferences.c index 174e825e..276e0841 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -55,6 +55,7 @@ static gchar * _get_preferences_file(void); 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); void prefs_load(void) @@ -122,16 +123,36 @@ prefs_set_boolean(preference_t pref, gboolean value) _save_prefs(); } -gchar * -prefs_get_theme(void) +char * +prefs_get_string(preference_t pref) { - return g_key_file_get_string(prefs, PREF_GROUP_UI, "theme", NULL); + const char *group = _get_group(pref); + const char *key = _get_key(pref); + char *def = _get_default_string(pref); + + if (!g_key_file_has_key(prefs, group, key, NULL)) { + return def; + } + + char *result = g_key_file_get_string(prefs, group, key, NULL); + + if (result == NULL) { + return def; + } else { + return result; + } } void -prefs_set_theme(gchar *value) +prefs_set_string(preference_t pref, char *value) { - g_key_file_set_string(prefs, PREF_GROUP_UI, "theme", 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_string(prefs, group, key, value); + } _save_prefs(); } @@ -217,24 +238,6 @@ prefs_set_autoping(gint value) _save_prefs(); } -gchar * -prefs_get_autoaway_mode(void) -{ - gchar *result = g_key_file_get_string(prefs, PREF_GROUP_PRESENCE, "autoaway.mode", NULL); - if (result == NULL) { - return strdup("off"); - } else { - return result; - } -} - -void -prefs_set_autoaway_mode(gchar *value) -{ - g_key_file_set_string(prefs, PREF_GROUP_PRESENCE, "autoaway.mode", value); - _save_prefs(); -} - gint prefs_get_autoaway_time(void) { @@ -254,23 +257,6 @@ prefs_set_autoaway_time(gint value) _save_prefs(); } -gchar * -prefs_get_autoaway_message(void) -{ - return g_key_file_get_string(prefs, PREF_GROUP_PRESENCE, "autoaway.message", NULL); -} - -void -prefs_set_autoaway_message(gchar *value) -{ - if (value == NULL) { - g_key_file_remove_key(prefs, PREF_GROUP_PRESENCE, "autoaway.message", NULL); - } else { - g_key_file_set_string(prefs, PREF_GROUP_PRESENCE, "autoaway.message", value); - } - _save_prefs(); -} - static void _save_prefs(void) { @@ -317,6 +303,8 @@ _get_group(preference_t pref) case PREF_CHLOG: return "logging"; case PREF_AUTOAWAY_CHECK: + case PREF_AUTOAWAY_MODE: + case PREF_AUTOAWAY_MESSAGE: return "presence"; default: return NULL; @@ -360,6 +348,10 @@ _get_key(preference_t pref) return "chlog"; case PREF_AUTOAWAY_CHECK: return "autoaway.check"; + case PREF_AUTOAWAY_MODE: + return "autoaway.mode"; + case PREF_AUTOAWAY_MESSAGE: + return "autoaway.message"; default: return NULL; } @@ -379,3 +371,14 @@ _get_default_boolean(preference_t pref) } } +static char * +_get_default_string(preference_t pref) +{ + switch (pref) + { + case PREF_AUTOAWAY_MODE: + return "off"; + default: + return NULL; + } +} diff --git a/src/config/preferences.h b/src/config/preferences.h index 57b065b2..70254e25 100644 --- a/src/config/preferences.h +++ b/src/config/preferences.h @@ -51,10 +51,11 @@ typedef enum { PREF_NOTIFY_TYPING, PREF_NOTIFY_MESSAGE, PREF_CHLOG, - PREF_AUTOAWAY_CHECK + PREF_AUTOAWAY_CHECK, + PREF_AUTOAWAY_MODE, + PREF_AUTOAWAY_MESSAGE } preference_t; - void prefs_load(void); void prefs_close(void); @@ -65,8 +66,6 @@ void prefs_reset_boolean_choice(void); gint prefs_get_gone(void); void prefs_set_gone(gint value); -gchar * prefs_get_theme(void); -void prefs_set_theme(gchar *value); void prefs_set_notify_remind(gint period); gint prefs_get_notify_remind(void); @@ -79,16 +78,14 @@ gint prefs_get_reconnect(void); void prefs_set_autoping(gint value); gint prefs_get_autoping(void); -gchar* prefs_get_autoaway_mode(void); -void prefs_set_autoaway_mode(gchar *value); gint prefs_get_autoaway_time(void); void prefs_set_autoaway_time(gint value); -gchar* prefs_get_autoaway_message(void); -void prefs_set_autoaway_message(gchar *value); void prefs_add_login(const char *jid); gboolean prefs_get_boolean(preference_t pref); void prefs_set_boolean(preference_t pref, gboolean value); +char * prefs_get_string(preference_t pref); +void prefs_set_string(preference_t pref, char *value); #endif diff --git a/src/profanity.c b/src/profanity.c index 8ea24825..9bc260a8 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -444,11 +444,11 @@ _handle_idle_time() idle = TRUE; // handle away mode - if (strcmp(prefs_get_autoaway_mode(), "away") == 0) { - presence_update(PRESENCE_AWAY, prefs_get_autoaway_message(), 0); - if (prefs_get_autoaway_message() != NULL) { + if (strcmp(prefs_get_string(PREF_AUTOAWAY_MODE), "away") == 0) { + presence_update(PRESENCE_AWAY, prefs_get_string(PREF_AUTOAWAY_MESSAGE), 0); + if (prefs_get_string(PREF_AUTOAWAY_MESSAGE) != NULL) { cons_show("Idle for %d minutes, status set to away, \"%s\".", - prefs_get_autoaway_time(), prefs_get_autoaway_message()); + prefs_get_autoaway_time(), prefs_get_string(PREF_AUTOAWAY_MESSAGE)); title_bar_set_status(PRESENCE_AWAY); win_current_page_off(); } else { @@ -459,9 +459,9 @@ _handle_idle_time() } // handle idle mode - } else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) { + } else if (strcmp(prefs_get_string(PREF_AUTOAWAY_MODE), "idle") == 0) { presence_update(PRESENCE_ONLINE, - prefs_get_autoaway_message(), idle_ms / 1000); + prefs_get_string(PREF_AUTOAWAY_MESSAGE), idle_ms / 1000); } } @@ -471,12 +471,12 @@ _handle_idle_time() // handle check if (prefs_get_boolean(PREF_AUTOAWAY_CHECK)) { - if (strcmp(prefs_get_autoaway_mode(), "away") == 0) { + if (strcmp(prefs_get_string(PREF_AUTOAWAY_MODE), "away") == 0) { presence_update(PRESENCE_ONLINE, NULL, 0); cons_show("No longer idle, status set to online."); title_bar_set_status(PRESENCE_ONLINE); win_current_page_off(); - } else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) { + } else if (strcmp(prefs_get_string(PREF_AUTOAWAY_MODE), "idle") == 0) { presence_update(PRESENCE_ONLINE, NULL, 0); title_bar_set_status(PRESENCE_ONLINE); } @@ -502,7 +502,7 @@ _init(const int disable_tls, char *log_level) chat_log_init(); prefs_load(); accounts_load(); - gchar *theme = prefs_get_theme(); + gchar *theme = prefs_get_string(PREF_THEME); theme_init(theme); g_free(theme); ui_init(); diff --git a/src/ui/windows.c b/src/ui/windows.c index e71d89a2..fcefdbdc 100644 --- a/src/ui/windows.c +++ b/src/ui/windows.c @@ -1291,7 +1291,7 @@ cons_show_ui_prefs(void) cons_show("UI preferences:"); cons_show(""); - gchar *theme = prefs_get_theme(); + gchar *theme = prefs_get_string(PREF_THEME); if (theme == NULL) { cons_show("Theme (/theme) : default"); } else { @@ -1411,19 +1411,19 @@ cons_show_presence_prefs(void) cons_show("Presence preferences:"); cons_show(""); - if (strcmp(prefs_get_autoaway_mode(), "off") == 0) { + if (strcmp(prefs_get_string(PREF_AUTOAWAY_MODE), "off") == 0) { cons_show("Autoaway (/autoaway mode) : OFF"); } else { - cons_show("Autoaway (/autoaway mode) : %s", prefs_get_autoaway_mode()); + cons_show("Autoaway (/autoaway mode) : %s", prefs_get_string(PREF_AUTOAWAY_MODE)); } cons_show("Autoaway minutes (/autoaway time) : %d minutes", prefs_get_autoaway_time()); - if ((prefs_get_autoaway_message() == NULL) || - (strcmp(prefs_get_autoaway_message(), "") == 0)) { + if ((prefs_get_string(PREF_AUTOAWAY_MESSAGE) == NULL) || + (strcmp(prefs_get_string(PREF_AUTOAWAY_MESSAGE), "") == 0)) { cons_show("Autoaway message (/autoaway message) : OFF"); } else { - cons_show("Autoaway message (/autoaway message) : \"%s\"", prefs_get_autoaway_message()); + cons_show("Autoaway message (/autoaway message) : \"%s\"", prefs_get_string(PREF_AUTOAWAY_MESSAGE)); } if (prefs_get_boolean(PREF_AUTOAWAY_CHECK)) { -- cgit 1.4.1-2-gfad0