1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Refactored setting boolean preferences

This commit is contained in:
James Booth 2013-02-03 02:51:15 +00:00
parent ca26452af6
commit 7982b8e413
3 changed files with 218 additions and 326 deletions

View File

@ -71,8 +71,7 @@ static struct cmd_t * _cmd_get_command(const char * const command);
static void _update_presence(const jabber_presence_t presence,
const char * const show, gchar **args);
static gboolean _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help,
const char * const display,
void (*set_func)(gboolean));
const char * const display, preference_t pref);
static void _cmd_complete_parameters(char *input, int *size);
static void _notify_autocomplete(char *input, int *size);
@ -2006,14 +2005,14 @@ _cmd_close(gchar **args, struct cmd_help_t help)
static gboolean
_cmd_set_beep(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help, "Sound", prefs_set_beep);
return _cmd_set_boolean_preference(args[0], help, "Sound", PREF_BEEP);
}
static gboolean
_cmd_set_states(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help, "Sending chat states",
prefs_set_states);
PREF_STATES);
}
static gboolean
@ -2024,7 +2023,7 @@ _cmd_set_titlebar(gchar **args, struct cmd_help_t help)
return TRUE;
} else {
return _cmd_set_boolean_preference(args[1], help,
"Show version in window title", prefs_set_titlebarversion);
"Show version in window title", PREF_TITLEBARVERSION);
}
}
@ -2032,7 +2031,7 @@ static gboolean
_cmd_set_outtype(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Sending typing notifications", prefs_set_outtype);
"Sending typing notifications", PREF_OUTTYPE);
}
static gboolean
@ -2069,10 +2068,10 @@ _cmd_set_notify(gchar **args, struct cmd_help_t help)
} else if (strcmp(kind, "message") == 0) {
if (strcmp(value, "on") == 0) {
cons_show("Message notifications enabled.");
prefs_set_notify_message(TRUE);
prefs_set_boolean(PREF_NOTIFY_MESSAGE, TRUE);
} else if (strcmp(value, "off") == 0) {
cons_show("Message notifications disabled.");
prefs_set_notify_message(FALSE);
prefs_set_boolean(PREF_NOTIFY_MESSAGE, FALSE);
} else {
cons_show("Usage: /notify message on|off");
}
@ -2081,10 +2080,10 @@ _cmd_set_notify(gchar **args, struct cmd_help_t help)
} else if (strcmp(kind, "typing") == 0) {
if (strcmp(value, "on") == 0) {
cons_show("Typing notifications enabled.");
prefs_set_notify_typing(TRUE);
prefs_set_boolean(PREF_NOTIFY_TYPING, TRUE);
} else if (strcmp(value, "off") == 0) {
cons_show("Typing notifications disabled.");
prefs_set_notify_typing(FALSE);
prefs_set_boolean(PREF_NOTIFY_TYPING, FALSE);
} else {
cons_show("Usage: /notify typing on|off");
}
@ -2218,7 +2217,7 @@ _cmd_set_autoaway(gchar **args, struct cmd_help_t help)
if (strcmp(setting, "check") == 0) {
return _cmd_set_boolean_preference(value, help, "Online check",
prefs_set_autoaway_check);
PREF_AUTOAWAY_CHECK);
}
return TRUE;
@ -2252,7 +2251,7 @@ static gboolean
_cmd_set_statuses(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Status notifications", prefs_set_statuses);
"Status notifications", PREF_STATUSES);
}
static gboolean
@ -2265,7 +2264,7 @@ _cmd_vercheck(gchar **args, struct cmd_help_t help)
return TRUE;
} else {
return _cmd_set_boolean_preference(args[0], help,
"Version checking", prefs_set_vercheck);
"Version checking", PREF_VERCHECK);
}
}
@ -2273,42 +2272,42 @@ static gboolean
_cmd_set_flash(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Screen flash", prefs_set_flash);
"Screen flash", PREF_FLASH);
}
static gboolean
_cmd_set_intype(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Show contact typing", prefs_set_intype);
"Show contact typing", PREF_INTYPE);
}
static gboolean
_cmd_set_splash(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Splash screen", prefs_set_splash);
"Splash screen", PREF_SPLASH);
}
static gboolean
_cmd_set_chlog(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Chat logging", prefs_set_chlog);
"Chat logging", PREF_CHLOG);
}
static gboolean
_cmd_set_mouse(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Mouse handling", prefs_set_mouse);
"Mouse handling", PREF_MOUSE);
}
static gboolean
_cmd_set_history(gchar **args, struct cmd_help_t help)
{
return _cmd_set_boolean_preference(args[0], help,
"Chat history", prefs_set_history);
"Chat history", PREF_HISTORY);
}
static gboolean
@ -2379,8 +2378,7 @@ _update_presence(const jabber_presence_t presence,
static gboolean
_cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help,
const char * const display,
void (*set_func)(gboolean))
const char * const display, preference_t pref)
{
GString *enabled = g_string_new(display);
g_string_append(enabled, " enabled.");
@ -2390,10 +2388,10 @@ _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help,
if (strcmp(arg, "on") == 0) {
cons_show(enabled->str);
set_func(TRUE);
prefs_set_boolean(pref, TRUE);
} else if (strcmp(arg, "off") == 0) {
cons_show(disabled->str);
set_func(FALSE);
prefs_set_boolean(pref, FALSE);
} else {
char usage[strlen(help.usage) + 8];
sprintf(usage, "Usage: %s", help.usage);

View File

@ -52,6 +52,9 @@ static Autocomplete boolean_choice_ac;
static void _save_prefs(void);
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);
void
prefs_load(void)
@ -96,6 +99,199 @@ prefs_reset_boolean_choice(void)
autocomplete_reset(boolean_choice_ac);
}
gboolean
prefs_get_boolean(preference_t pref)
{
const char *group = _get_group(pref);
const char *key = _get_key(pref);
gboolean def = _get_default_boolean(pref);
if (!g_key_file_has_key(prefs, group, key, NULL)) {
return def;
}
return g_key_file_get_boolean(prefs, group, key, NULL);
}
void
prefs_set_boolean(preference_t pref, gboolean value)
{
const char *group = _get_group(pref);
const char *key = _get_key(pref);
g_key_file_set_boolean(prefs, group, key, value);
_save_prefs();
}
gchar *
prefs_get_theme(void)
{
return g_key_file_get_string(prefs, PREF_GROUP_UI, "theme", NULL);
}
void
prefs_set_theme(gchar *value)
{
g_key_file_set_string(prefs, PREF_GROUP_UI, "theme", value);
_save_prefs();
}
gint
prefs_get_gone(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_CHATSTATES, "gone", NULL);
}
void
prefs_set_gone(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CHATSTATES, "gone", value);
_save_prefs();
}
gint
prefs_get_notify_remind(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", NULL);
}
void
prefs_set_notify_remind(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", value);
_save_prefs();
}
gint
prefs_get_max_log_size(void)
{
if (log_maxsize < PREFS_MIN_LOG_SIZE)
return PREFS_MAX_LOG_SIZE;
else
return log_maxsize;
}
void
prefs_set_max_log_size(gint value)
{
log_maxsize = value;
g_key_file_set_integer(prefs, PREF_GROUP_LOGGING, "maxsize", value);
_save_prefs();
}
gint
prefs_get_priority(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "priority", NULL);
}
void
prefs_set_priority(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "priority", value);
_save_prefs();
}
gint
prefs_get_reconnect(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", NULL);
}
void
prefs_set_reconnect(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", value);
_save_prefs();
}
gint
prefs_get_autoping(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "autoping", NULL);
}
void
prefs_set_autoping(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "autoping", 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)
{
gint result = g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.time", NULL);
if (result == 0) {
return 15;
} else {
return result;
}
}
void
prefs_set_autoaway_time(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.time", 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)
{
gsize g_data_size;
char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
g_file_set_contents(prefs_loc, g_prefs_data, g_data_size, NULL);
}
static gchar *
_get_preferences_file(void)
{
gchar *xdg_config = xdg_get_config_home();
GString *prefs_file = g_string_new(xdg_config);
g_string_append(prefs_file, "/profanity/profrc");
gchar *result = strdup(prefs_file->str);
g_free(xdg_config);
g_string_free(prefs_file, TRUE);
return result;
}
static const char *
_get_group(preference_t pref)
{
@ -183,291 +379,3 @@ _get_default_boolean(preference_t pref)
}
}
gboolean
prefs_get_boolean(preference_t pref)
{
const char *group = _get_group(pref);
const char *key = _get_key(pref);
gboolean def = _get_default_boolean(pref);
if (!g_key_file_has_key(prefs, group, key, NULL)) {
return def;
}
return g_key_file_get_boolean(prefs, group, key, NULL);
}
void
prefs_set_beep(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "beep", value);
_save_prefs();
}
gchar *
prefs_get_theme(void)
{
return g_key_file_get_string(prefs, PREF_GROUP_UI, "theme", NULL);
}
void
prefs_set_theme(gchar *value)
{
g_key_file_set_string(prefs, PREF_GROUP_UI, "theme", value);
_save_prefs();
}
void
prefs_set_states(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_CHATSTATES, "enabled", value);
_save_prefs();
}
void
prefs_set_outtype(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_CHATSTATES, "outtype", value);
_save_prefs();
}
gint
prefs_get_gone(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_CHATSTATES, "gone", NULL);
}
void
prefs_set_gone(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CHATSTATES, "gone", value);
_save_prefs();
}
void
prefs_set_notify_typing(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_NOTIFICATIONS, "typing", value);
_save_prefs();
}
void
prefs_set_notify_message(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_NOTIFICATIONS, "message", value);
_save_prefs();
}
gint
prefs_get_notify_remind(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", NULL);
}
void
prefs_set_notify_remind(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_NOTIFICATIONS, "remind", value);
_save_prefs();
}
gint
prefs_get_max_log_size(void)
{
if (log_maxsize < PREFS_MIN_LOG_SIZE)
return PREFS_MAX_LOG_SIZE;
else
return log_maxsize;
}
void
prefs_set_max_log_size(gint value)
{
log_maxsize = value;
g_key_file_set_integer(prefs, PREF_GROUP_LOGGING, "maxsize", value);
_save_prefs();
}
gint
prefs_get_priority(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "priority", NULL);
}
void
prefs_set_priority(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "priority", value);
_save_prefs();
}
gint
prefs_get_reconnect(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", NULL);
}
void
prefs_set_reconnect(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "reconnect", value);
_save_prefs();
}
gint
prefs_get_autoping(void)
{
return g_key_file_get_integer(prefs, PREF_GROUP_CONNECTION, "autoping", NULL);
}
void
prefs_set_autoping(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_CONNECTION, "autoping", value);
_save_prefs();
}
void
prefs_set_vercheck(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "vercheck", value);
_save_prefs();
}
void
prefs_set_titlebarversion(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "titlebar.version", value);
_save_prefs();
}
void
prefs_set_flash(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "flash", value);
_save_prefs();
}
void
prefs_set_intype(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "intype", value);
_save_prefs();
}
void
prefs_set_chlog(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_LOGGING, "chlog", value);
_save_prefs();
}
void
prefs_set_history(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "history", 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)
{
gint result = g_key_file_get_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.time", NULL);
if (result == 0) {
return 15;
} else {
return result;
}
}
void
prefs_set_autoaway_time(gint value)
{
g_key_file_set_integer(prefs, PREF_GROUP_PRESENCE, "autoaway.time", 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();
}
void
prefs_set_autoaway_check(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_PRESENCE, "autoaway.check", value);
_save_prefs();
}
void
prefs_set_splash(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "splash", value);
_save_prefs();
}
void
prefs_set_mouse(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "mouse", value);
_save_prefs();
}
void
prefs_set_statuses(gboolean value)
{
g_key_file_set_boolean(prefs, PREF_GROUP_UI, "statuses", value);
_save_prefs();
}
static void
_save_prefs(void)
{
gsize g_data_size;
char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
g_file_set_contents(prefs_loc, g_prefs_data, g_data_size, NULL);
}
static gchar *
_get_preferences_file(void)
{
gchar *xdg_config = xdg_get_config_home();
GString *prefs_file = g_string_new(xdg_config);
g_string_append(prefs_file, "/profanity/profrc");
gchar *result = strdup(prefs_file->str);
g_free(xdg_config);
g_string_free(prefs_file, TRUE);
return result;
}

View File

@ -63,25 +63,11 @@ void prefs_reset_login_search(void);
char * prefs_autocomplete_boolean_choice(char *prefix);
void prefs_reset_boolean_choice(void);
void prefs_set_beep(gboolean value);
void prefs_set_flash(gboolean value);
void prefs_set_chlog(gboolean value);
void prefs_set_history(gboolean value);
void prefs_set_splash(gboolean value);
void prefs_set_vercheck(gboolean value);
void prefs_set_titlebarversion(gboolean value);
void prefs_set_intype(gboolean value);
void prefs_set_states(gboolean value);
void prefs_set_outtype(gboolean value);
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_mouse(gboolean value);
void prefs_set_statuses(gboolean value);
void prefs_set_notify_message(gboolean value);
void prefs_set_notify_typing(gboolean value);
void prefs_set_notify_remind(gint period);
gint prefs_get_notify_remind(void);
void prefs_set_max_log_size(gint value);
@ -99,10 +85,10 @@ 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_set_autoaway_check(gboolean value);
void prefs_add_login(const char *jid);
gboolean prefs_get_boolean(preference_t pref);
void prefs_set_boolean(preference_t pref, gboolean value);
#endif