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 2012-08-23 00:30:11 +01:00
parent 0f7f0a259d
commit b3f6023278
5 changed files with 72 additions and 94 deletions

View File

@ -55,6 +55,10 @@ static gboolean _handle_command(const char * const command,
const char * const inp); const char * const inp);
static void _update_presence(const jabber_presence_t presence, static void _update_presence(const jabber_presence_t presence,
const char * const show, const char * const inp); const char * const show, const char * const inp);
static gboolean
_cmd_set_boolean_preference(const char * const inp, struct cmd_help_t help,
const char * const cmd_str, const char * const display,
void (*set_func)(gboolean));
// command prototypes // command prototypes
static gboolean _cmd_quit(const char * const inp, struct cmd_help_t help); static gboolean _cmd_quit(const char * const inp, struct cmd_help_t help);
@ -376,7 +380,7 @@ process_input(char *inp)
* Initialise command autocompleter and history * Initialise command autocompleter and history
*/ */
void void
command_init(void) cmd_init(void)
{ {
log_msg(PROF_LEVEL_INFO, "prof", "Initialising commands"); log_msg(PROF_LEVEL_INFO, "prof", "Initialising commands");
commands_ac = p_autocomplete_new(); commands_ac = p_autocomplete_new();
@ -407,13 +411,13 @@ cmd_complete(char *inp)
} }
void void
reset_command_completer(void) cmd_reset_completer(void)
{ {
p_autocomplete_reset(commands_ac); p_autocomplete_reset(commands_ac);
} }
GSList * GSList *
cmd_get_help_list_basic(void) cmd_get_basic_help(void)
{ {
GSList *result = NULL; GSList *result = NULL;
@ -426,7 +430,7 @@ cmd_get_help_list_basic(void)
} }
GSList * GSList *
cmd_get_help_list_settings(void) cmd_get_settings_help(void)
{ {
GSList *result = NULL; GSList *result = NULL;
@ -439,7 +443,7 @@ cmd_get_help_list_settings(void)
} }
GSList * GSList *
cmd_get_help_list_status(void) cmd_get_status_help(void)
{ {
GSList *result = NULL; GSList *result = NULL;
@ -705,109 +709,43 @@ _cmd_close(const char * const inp, struct cmd_help_t help)
static gboolean static gboolean
_cmd_set_beep(const char * const inp, struct cmd_help_t help) _cmd_set_beep(const char * const inp, struct cmd_help_t help)
{ {
if (strcmp(inp, "/beep on") == 0) { return _cmd_set_boolean_preference(inp, help, "/beep",
cons_show("Sound enabled."); "Sound", prefs_set_beep);
prefs_set_beep(TRUE);
} else if (strcmp(inp, "/beep off") == 0) {
cons_show("Sound disabled.");
prefs_set_beep(FALSE);
} else {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
cons_show(usage);
}
return TRUE;
} }
static gboolean static gboolean
_cmd_set_notify(const char * const inp, struct cmd_help_t help) _cmd_set_notify(const char * const inp, struct cmd_help_t help)
{ {
if (strcmp(inp, "/notify on") == 0) { return _cmd_set_boolean_preference(inp, help, "/notify",
cons_show("Desktop notifications enabled."); "Desktop notifications", prefs_set_notify);
prefs_set_notify(TRUE);
} else if (strcmp(inp, "/notify off") == 0) {
cons_show("Desktop notifications disabled.");
prefs_set_notify(FALSE);
} else {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
cons_show(usage);
}
return TRUE;
} }
static gboolean static gboolean
_cmd_set_typing(const char * const inp, struct cmd_help_t help) _cmd_set_typing(const char * const inp, struct cmd_help_t help)
{ {
if (strcmp(inp, "/typing on") == 0) { return _cmd_set_boolean_preference(inp, help, "/typing",
cons_show("Incoming typing notifications enabled."); "Incoming typing notifications", prefs_set_typing);
prefs_set_typing(TRUE);
} else if (strcmp(inp, "/typing off") == 0) {
cons_show("Incoming typing notifications disabled.");
prefs_set_typing(FALSE);
} else {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
cons_show(usage);
}
return TRUE;
} }
static gboolean static gboolean
_cmd_set_flash(const char * const inp, struct cmd_help_t help) _cmd_set_flash(const char * const inp, struct cmd_help_t help)
{ {
if (strcmp(inp, "/flash on") == 0) { return _cmd_set_boolean_preference(inp, help, "/flash",
cons_show("Screen flash enabled."); "Screen flash", prefs_set_flash);
prefs_set_flash(TRUE);
} else if (strcmp(inp, "/flash off") == 0) {
cons_show("Screen flash disabled.");
prefs_set_flash(FALSE);
} else {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
cons_show(usage);
}
return TRUE;
} }
static gboolean static gboolean
_cmd_set_showsplash(const char * const inp, struct cmd_help_t help) _cmd_set_showsplash(const char * const inp, struct cmd_help_t help)
{ {
if (strcmp(inp, "/showsplash on") == 0) { return _cmd_set_boolean_preference(inp, help, "/showsplash",
cons_show("Splash screen enabled."); "Splash screen", prefs_set_showsplash);
prefs_set_showsplash(TRUE);
} else if (strcmp(inp, "/showsplash off") == 0) {
cons_show("Splash screen disabled.");
prefs_set_showsplash(FALSE);
} else {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
cons_show(usage);
}
return TRUE;
} }
static gboolean static gboolean
_cmd_set_chlog(const char * const inp, struct cmd_help_t help) _cmd_set_chlog(const char * const inp, struct cmd_help_t help)
{ {
if (strcmp(inp, "/chlog on") == 0) { return _cmd_set_boolean_preference(inp, help, "/chlog",
cons_show("Chat logging enabled."); "Chat logging", prefs_set_chlog);
prefs_set_chlog(TRUE);
} else if (strcmp(inp, "/chlog off") == 0) {
cons_show("Chat logging disabled.");
prefs_set_chlog(FALSE);
} else {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
cons_show(usage);
}
return TRUE;
} }
static gboolean static gboolean
@ -891,3 +829,41 @@ _update_presence(const jabber_presence_t presence,
} }
} }
static gboolean
_cmd_set_boolean_preference(const char * const inp, struct cmd_help_t help,
const char * const cmd_str, const char * const display,
void (*set_func)(gboolean))
{
GString *on = g_string_new(cmd_str);
g_string_append(on, " on");
GString *off = g_string_new(cmd_str);
g_string_append(off, " off");
GString *enabled = g_string_new(display);
g_string_append(enabled, " enabled.");
GString *disabled = g_string_new(display);
g_string_append(disabled, " disabled.");
if (strcmp(inp, on->str) == 0) {
cons_show(enabled->str);
set_func(TRUE);
} else if (strcmp(inp, off->str) == 0) {
cons_show(disabled->str);
set_func(FALSE);
} else {
char usage[strlen(help.usage + 8)];
sprintf(usage, "Usage: %s", help.usage);
cons_show(usage);
}
g_string_free(on, TRUE);
g_string_free(off, TRUE);
g_string_free(enabled, TRUE);
g_string_free(disabled, TRUE);
return TRUE;
}

View File

@ -23,19 +23,21 @@
#ifndef COMMAND_H #ifndef COMMAND_H
#define COMMAND_H #define COMMAND_H
// command help strings // Command help strings
struct cmd_help_t { struct cmd_help_t {
const gchar *usage; const gchar *usage;
const gchar *short_help; const gchar *short_help;
const gchar *long_help[50]; const gchar *long_help[50];
}; };
void command_init(void); void cmd_init(void);
gboolean process_input(char *inp); gboolean process_input(char *inp);
char * cmd_complete(char *inp); char * cmd_complete(char *inp);
void reset_command_completer(void); void cmd_reset_completer(void);
GSList * cmd_get_help_list_basic(void);
GSList * cmd_get_help_list_settings(void); // command help
GSList * cmd_get_help_list_status(void); GSList * cmd_get_basic_help(void);
GSList * cmd_get_settings_help(void);
GSList * cmd_get_status_help(void);
#endif #endif

View File

@ -154,7 +154,7 @@ inp_get_char(int *ch, char *input, int *size)
reset_search_attempts(); reset_search_attempts();
reset_login_search(); reset_login_search();
reset_command_completer(); cmd_reset_completer();
} }
} }

View File

@ -83,7 +83,7 @@ profanity_init(const int disable_tls, char *log_level)
prefs_load(); prefs_load();
gui_init(); gui_init();
jabber_init(disable_tls); jabber_init(disable_tls);
command_init(); cmd_init();
contact_list_init(); contact_list_init();
atexit(_profanity_shutdown); atexit(_profanity_shutdown);
} }

View File

@ -462,7 +462,7 @@ _cons_show_basic_help(void)
{ {
cons_show(""); cons_show("");
GSList *basic_helpers = cmd_get_help_list_basic(); GSList *basic_helpers = cmd_get_basic_help();
while (basic_helpers != NULL) { while (basic_helpers != NULL) {
struct cmd_help_t *help = (struct cmd_help_t *)basic_helpers->data; struct cmd_help_t *help = (struct cmd_help_t *)basic_helpers->data;
char line[25 + 2 + strlen(help->short_help)]; char line[25 + 2 + strlen(help->short_help)];
@ -484,7 +484,7 @@ cons_help(void)
cons_show("Settings:"); cons_show("Settings:");
cons_show(""); cons_show("");
GSList *settings_helpers = cmd_get_help_list_settings(); GSList *settings_helpers = cmd_get_settings_help();
while (settings_helpers != NULL) { while (settings_helpers != NULL) {
struct cmd_help_t *help = (struct cmd_help_t *)settings_helpers->data; struct cmd_help_t *help = (struct cmd_help_t *)settings_helpers->data;
char line[25 + 2 + strlen(help->short_help)]; char line[25 + 2 + strlen(help->short_help)];
@ -497,7 +497,7 @@ cons_help(void)
cons_show("Status changes:"); cons_show("Status changes:");
cons_show(""); cons_show("");
GSList *status_helpers = cmd_get_help_list_status(); GSList *status_helpers = cmd_get_status_help();
while (status_helpers != NULL) { while (status_helpers != NULL) {
struct cmd_help_t *help = (struct cmd_help_t *)status_helpers->data; struct cmd_help_t *help = (struct cmd_help_t *)status_helpers->data;
char line[25 + 2 + strlen(help->short_help)]; char line[25 + 2 + strlen(help->short_help)];