diff --git a/src/twc-config.c b/src/twc-config.c index 28a78dc..dc709eb 100644 --- a/src/twc-config.c +++ b/src/twc-config.c @@ -32,6 +32,9 @@ struct t_config_file *twc_config_file = NULL; struct t_config_section *twc_config_section_profile = NULL; +struct t_config_section *twc_config_section_profile_default = NULL; + +struct t_config_option *twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS]; char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] = { @@ -50,11 +53,22 @@ char *twc_profile_option_defaults[TWC_PROFILE_NUM_OPTIONS] = "off", "100", NULL, - "0", + NULL, "off", "on", }; +bool twc_profile_option_null_allowed[TWC_PROFILE_NUM_OPTIONS] = +{ + false, + false, + false, + true, // we allow proxy information to be null + true, // ------------------------------------- + false, + false +}; + /** * Get the index of a profile option name. */ @@ -188,6 +202,73 @@ twc_config_profile_change_callback(void *data, { } +/** + * Create a new option for a profile. + */ +struct t_config_option * +twc_config_init_option(struct t_config_section *section, + int option_index, const char *option_name, + bool is_default_profile) +{ + char *type; + char *description; + char *string_values = NULL; + int min = 0, max = 0; + + switch (option_index) + { + case TWC_PROFILE_OPTION_AUTOLOAD: + type = "boolean"; + description = "automatically load profile and connect to the Tox " + "network when WeeChat starts"; + break; + case TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS: + type = "integer"; + description = "maximum amount of friend requests to retain before " + "ignoring new ones"; + min = 0; max = INT_MAX; + break; + case TWC_PROFILE_OPTION_PROXY_ADDRESS: + type = "string"; + description = "proxy address"; + break; + case TWC_PROFILE_OPTION_PROXY_ENABLED: + type = "boolean"; + description = "use a proxy for communicating with the Tox " + "network; requries profile reload to take effect"; + break; + case TWC_PROFILE_OPTION_PROXY_PORT: + type = "integer"; + description = "proxy port"; + min = 0; max = UINT16_MAX; + break; + case TWC_PROFILE_OPTION_SAVEFILE: + type = "string"; + description = "path to Tox data file (\"%h\" will be replaced by " + "WeeChat home folder and \"%p\" by profile name"; + break; + case TWC_PROFILE_OPTION_UDP: + type = "boolean"; + description = "use UDP when communicating with the Tox network"; + break; + default: + return NULL; + } + + char *default_value = twc_profile_option_defaults[option_index]; + char *value = is_default_profile ? default_value : NULL; + bool null_allowed = !is_default_profile + || twc_profile_option_null_allowed[option_index]; + + return weechat_config_new_option( + twc_config_file, section, + option_name, type, description, string_values, min, max, + default_value, value, null_allowed, + twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, + twc_config_profile_change_callback, (void *)(intptr_t)option_index, + NULL, NULL); +} + /** * Initialize Tox-WeeChat config. Creates file and section objects. */ @@ -204,93 +285,21 @@ twc_config_init() NULL, NULL, NULL, NULL, NULL, NULL); -} -/** - * Create a new option for a profile. - */ -struct t_config_option * -twc_config_init_option(int option_index, const char *option_name) -{ - switch (option_index) + twc_config_section_profile_default = + weechat_config_new_section(twc_config_file, "profile_default", + 0, 0, + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL, + NULL, NULL); + + for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i) { - case TWC_PROFILE_OPTION_AUTOLOAD: - return weechat_config_new_option( - twc_config_file, twc_config_section_profile, - option_name, "boolean", - "automatically load a profile and connect to the Tox network " - "when WeeChat starts", - NULL, 0, 0, - twc_profile_option_defaults[option_index], NULL, 1, - twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, - twc_config_profile_change_callback, (void *)(intptr_t)option_index, - NULL, NULL); - case TWC_PROFILE_OPTION_MAX_FRIEND_REQUESTS: - return weechat_config_new_option( - twc_config_file, twc_config_section_profile, - option_name, "integer", - "maximum amount of friend requests to retain before dropping " - "new ones", - NULL, 0, INT_MAX, - twc_profile_option_defaults[option_index], NULL, 1, - twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, - twc_config_profile_change_callback, (void *)(intptr_t)option_index, - NULL, NULL); - case TWC_PROFILE_OPTION_PROXY_ADDRESS: - return weechat_config_new_option( - twc_config_file, twc_config_section_profile, - option_name, "string", - "proxy address ", - NULL, 0, 0, - twc_profile_option_defaults[option_index], NULL, 1, - twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, - twc_config_profile_change_callback, (void *)(intptr_t)option_index, - NULL, NULL); - case TWC_PROFILE_OPTION_PROXY_ENABLED: - return weechat_config_new_option( - twc_config_file, twc_config_section_profile, - option_name, "boolean", - "use a proxy for communicating with the Tox network; requires " - "profile reload to take effect", - NULL, 0, 0, - twc_profile_option_defaults[option_index], NULL, 1, - twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, - twc_config_profile_change_callback, (void *)(intptr_t)option_index, - NULL, NULL); - case TWC_PROFILE_OPTION_PROXY_PORT: - return weechat_config_new_option( - twc_config_file, twc_config_section_profile, - option_name, "integer", - "proxy address", - NULL, 1, UINT16_MAX , - twc_profile_option_defaults[option_index], NULL, 1, - twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, - twc_config_profile_change_callback, (void *)(intptr_t)option_index, - NULL, NULL); - case TWC_PROFILE_OPTION_SAVEFILE: - return weechat_config_new_option( - twc_config_file, twc_config_section_profile, - option_name, "string", - "path to Tox data file (\"%h\" will be replaced by WeeChat " - "home, \"%p\" by the profile name); will be created if it does " - "not exist.", - NULL, 0, 0, - twc_profile_option_defaults[option_index], NULL, 1, - twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, - twc_config_profile_change_callback, (void *)(intptr_t)option_index, - NULL, NULL); - case TWC_PROFILE_OPTION_UDP: - return weechat_config_new_option( - twc_config_file, twc_config_section_profile, - option_name, "boolean", - "use UDP when communicating with the Tox network", - NULL, 0, 0, - twc_profile_option_defaults[option_index], NULL, 1, - twc_config_profile_check_value_callback, (void *)(intptr_t)option_index, - twc_config_profile_change_callback, (void *)(intptr_t)option_index, - NULL, NULL); - default: - return NULL; + twc_config_profile_default[i] = + twc_config_init_option(twc_config_section_profile_default, + i, twc_profile_option_names[i], true); } } @@ -313,7 +322,8 @@ twc_config_init_profile(struct t_twc_profile *profile) profile->name, twc_profile_option_names[i]); - profile->options[i] = twc_config_init_option(i, option_name); + profile->options[i] = twc_config_init_option(twc_config_section_profile, + i, option_name, false); free(option_name); } } diff --git a/src/twc-config.h b/src/twc-config.h index 745075e..d99b89e 100644 --- a/src/twc-config.h +++ b/src/twc-config.h @@ -21,6 +21,7 @@ #define TOX_WEECHAT_CONFIG_H #include "twc-profile.h" +extern struct t_config_option *twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS]; void twc_config_init(); diff --git a/src/twc-profile.c b/src/twc-profile.c index f464244..1f4acf8 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -239,11 +239,12 @@ twc_profile_load(struct t_twc_profile *profile) "missing; aborting", weechat_prefix("error"), options->proxy_address, options->proxy_port); + return; } else { weechat_printf(profile->buffer, - "%sUsing proxy %s:%d", + "%susing proxy %s:%d", weechat_prefix("network"), options->proxy_address, options->proxy_port); } diff --git a/src/twc-profile.h b/src/twc-profile.h index 365e5cf..e4a1500 100644 --- a/src/twc-profile.h +++ b/src/twc-profile.h @@ -58,19 +58,31 @@ struct t_twc_profile extern struct t_twc_list *twc_profiles; #define TWC_PROFILE_OPTION_BOOLEAN(profile, index) \ - ((!weechat_config_option_is_null(profile->options[index])) ? \ - weechat_config_boolean(profile->options[index]) : \ - weechat_config_boolean_default(profile->options[index])) \ + (!weechat_config_option_is_null(profile->options[index]) \ + ? weechat_config_boolean(profile->options[index]) \ + : (!weechat_config_option_is_null(twc_config_profile_default[index]) \ + ? weechat_config_boolean(twc_config_profile_default[index]) \ + : (!weechat_config_option_default_is_null(twc_config_profile_default[index]) \ + ? weechat_config_boolean_default(twc_config_profile_default[index]) \ + : 0))) #define TWC_PROFILE_OPTION_INTEGER(profile, index) \ - ((!weechat_config_option_is_null(profile->options[index])) ? \ - weechat_config_integer(profile->options[index]) : \ - weechat_config_integer_default(profile->options[index])) \ + (!weechat_config_option_is_null(profile->options[index]) \ + ? weechat_config_integer(profile->options[index]) \ + : (!weechat_config_option_is_null(twc_config_profile_default[index]) \ + ? weechat_config_integer(twc_config_profile_default[index]) \ + : (!weechat_config_option_default_is_null(twc_config_profile_default[index]) \ + ? weechat_config_integer_default(twc_config_profile_default[index]) \ + : 0))) #define TWC_PROFILE_OPTION_STRING(profile, index) \ - ((!weechat_config_option_is_null(profile->options[index])) ? \ - weechat_config_string(profile->options[index]) : \ - weechat_config_string_default(profile->options[index])) \ + (!weechat_config_option_is_null(profile->options[index]) \ + ? weechat_config_string(profile->options[index]) \ + : (!weechat_config_option_is_null(twc_config_profile_default[index]) \ + ? weechat_config_string(twc_config_profile_default[index]) \ + : (!weechat_config_option_default_is_null(twc_config_profile_default[index]) \ + ? weechat_config_string_default(twc_config_profile_default[index]) \ + : NULL))) void twc_profile_init();