Tweaked config code.

This commit is contained in:
Håvard Pettersson 2014-10-11 11:23:26 +02:00
parent 9510732626
commit 38df57f07c
4 changed files with 121 additions and 97 deletions

View File

@ -32,6 +32,9 @@
struct t_config_file *twc_config_file = NULL; 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 = 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] = char *twc_profile_option_names[TWC_PROFILE_NUM_OPTIONS] =
{ {
@ -50,11 +53,22 @@ char *twc_profile_option_defaults[TWC_PROFILE_NUM_OPTIONS] =
"off", "off",
"100", "100",
NULL, NULL,
"0", NULL,
"off", "off",
"on", "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. * 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. * Initialize Tox-WeeChat config. Creates file and section objects.
*/ */
@ -204,93 +285,21 @@ twc_config_init()
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,
NULL, NULL); NULL, NULL);
}
/** twc_config_section_profile_default =
* Create a new option for a profile. weechat_config_new_section(twc_config_file, "profile_default",
*/ 0, 0,
struct t_config_option * NULL, NULL,
twc_config_init_option(int option_index, const char *option_name) NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL);
for (int i = 0; i < TWC_PROFILE_NUM_OPTIONS; ++i)
{ {
switch (option_index) twc_config_profile_default[i] =
{ twc_config_init_option(twc_config_section_profile_default,
case TWC_PROFILE_OPTION_AUTOLOAD: i, twc_profile_option_names[i], true);
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;
} }
} }
@ -313,7 +322,8 @@ twc_config_init_profile(struct t_twc_profile *profile)
profile->name, profile->name,
twc_profile_option_names[i]); 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); free(option_name);
} }
} }

View File

@ -21,6 +21,7 @@
#define TOX_WEECHAT_CONFIG_H #define TOX_WEECHAT_CONFIG_H
#include "twc-profile.h" #include "twc-profile.h"
extern struct t_config_option *twc_config_profile_default[TWC_PROFILE_NUM_OPTIONS];
void void
twc_config_init(); twc_config_init();

View File

@ -239,11 +239,12 @@ twc_profile_load(struct t_twc_profile *profile)
"missing; aborting", "missing; aborting",
weechat_prefix("error"), weechat_prefix("error"),
options->proxy_address, options->proxy_port); options->proxy_address, options->proxy_port);
return;
} }
else else
{ {
weechat_printf(profile->buffer, weechat_printf(profile->buffer,
"%sUsing proxy %s:%d", "%susing proxy %s:%d",
weechat_prefix("network"), weechat_prefix("network"),
options->proxy_address, options->proxy_port); options->proxy_address, options->proxy_port);
} }

View File

@ -58,19 +58,31 @@ struct t_twc_profile
extern struct t_twc_list *twc_profiles; extern struct t_twc_list *twc_profiles;
#define TWC_PROFILE_OPTION_BOOLEAN(profile, index) \ #define TWC_PROFILE_OPTION_BOOLEAN(profile, index) \
((!weechat_config_option_is_null(profile->options[index])) ? \ (!weechat_config_option_is_null(profile->options[index]) \
weechat_config_boolean(profile->options[index]) : \ ? weechat_config_boolean(profile->options[index]) \
weechat_config_boolean_default(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) \ #define TWC_PROFILE_OPTION_INTEGER(profile, index) \
((!weechat_config_option_is_null(profile->options[index])) ? \ (!weechat_config_option_is_null(profile->options[index]) \
weechat_config_integer(profile->options[index]) : \ ? weechat_config_integer(profile->options[index]) \
weechat_config_integer_default(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) \ #define TWC_PROFILE_OPTION_STRING(profile, index) \
((!weechat_config_option_is_null(profile->options[index])) ? \ (!weechat_config_option_is_null(profile->options[index]) \
weechat_config_string(profile->options[index]) : \ ? weechat_config_string(profile->options[index]) \
weechat_config_string_default(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 void
twc_profile_init(); twc_profile_init();