AppOpts Pile
version 0.0.1
The pile provides the application with easy loading
|
Application options. More...
#include <appopts.h>
Public Member Functions | |
AppOpts () | |
Default constructor. More... | |
virtual | ~AppOpts () |
Destructor. More... | |
bool | loadFromAll (UserMsg &um, const QString &s_app_name=QString()) |
Load options from files. More... | |
bool | loadFile (const QString &s_file, PerSt **out_pers, UserMsg &um) |
Load options from a file. More... | |
bool | readValueFromCfgs (const OneOpt &opt, UserMsg &um) |
Looks into existing files for requested variable. More... | |
bool | readMultipleFromCfgs (const OneOptList &optlist, UserMsg &um) |
Looks into existing files for requested variables. More... | |
void | setValue (const QString &s_key, const QString &s_value) |
Set a value. More... | |
void | setValue (const QString &s_key, const QStringList &sl_value) |
Set a value. More... | |
void | appendValue (const QString &s_key, const QString &s_value) |
Append a value. More... | |
void | appendValues (const QString &s_key, const QStringList &sl_values) |
Append values. More... | |
bool | setCurrentConfig (const QString &s_file, UserMsg &um) |
Set current file. More... | |
bool | valueB (const QString &s_name, bool b_default=false) const |
Get a boolean value based on option's name. More... | |
QString | valueS (const QString &s_name, const QString &s_default=QString()) const |
Get a string value. More... | |
QStringList | valueSL (const QString &s_name, const QStringList &sl_default=QStringList()) const |
Get a list of strings. More... | |
int | valueI (const QString &s_name, int i_default=0) const |
Get an integer value. More... | |
double | valueD (const QString &s_name, double d_default=0.0) const |
Get an integer value. More... | |
Static Public Member Functions | |
static QString | cfgFileName (const QString &s_app_name) |
Get a proper name starting from a template. More... | |
Application options.
This class holds a list of key-value pairs, where values are ultimately list of strings.
AppOpts::AppOpts | ( | ) |
Default constructor.
Creates a valid instance.
|
virtual |
Destructor.
Releases all resources associated with this instance.
void AppOpts::appendValue | ( | const QString & | s_key, |
const QString & | s_value | ||
) |
Append a value.
The class represents values for options as a list of strings. This overload will either create a list with a single member and use that as a value if the option is not found or append s_value to internal list associated with this option.
s_key | the name of the variable to change |
s_value | a string value to use |
void AppOpts::appendValues | ( | const QString & | s_key, |
const QStringList & | sl_values | ||
) |
Append values.
The class represents values for options as a list of strings. If the option is already present inside t6he two lists will be merged.
s_key | the name of the variable to change |
s_value | a string value to use |
|
static |
Get a proper name starting from a template.
The method will replace spaces by _
and will make all letters lower-case. If input template is empty then the result is config
.
s_app_name | Input template. |
bool AppOpts::loadFile | ( | const QString & | s_file, |
PerSt ** | out_pers, | ||
UserMsg & | um | ||
) |
Load options from a file.
Creates a PerSt instance from our file and checks that a general
section exists and it contains a proper perst_version
version string that we are safe to interpret.
s_file | Input file's path. |
out_pers | Resulted PerSt object, if any. |
um | communication device. |
bool AppOpts::loadFromAll | ( | UserMsg & | um, |
const QString & | s_app_name = QString() |
||
) |
Load options from files.
The function will either get the name of the application or use provided string. The spaces are replaced by _
and all letters are lower-cased. See cfgFileName()
for details.
The name that results is then used to search for a configuration file in three locations: system data directory, user home directory and current directory. If found, the file is immediately loaded in this order.
The reverse order (current dir, user home, system data) is used to decide where to save changed settings.
um | structure used to show messages. |
s_app_name | the name to use for file name. |
Get the name of the config file
bool AppOpts::readMultipleFromCfgs | ( | const OneOptList & | list, |
UserMsg & | um | ||
) |
Looks into existing files for requested variables.
The method simply iterates the list of options and uses readValueFromCfgs()
to get their values and insert them into its own option tree.
This is useful if the application defines at startup a list of options that it expects, then initializes the AppOpts instance and makes sure that all required options are present.
list | the list of variables to search |
um | communication object |
bool AppOpts::readValueFromCfgs | ( | const OneOpt & | opt, |
UserMsg & | um | ||
) |
Looks into existing files for requested variable.
The method will honor variable's group and try to locate the value in system, user and local config files (represented as persistent storage inside). If found the variable and the value are inserted into local storage, replacing any value that was previously there.
If a variable is not required and is also not found in the configuration files the default value is used.
opt | definition of the variable to search |
um | communication object |
bool AppOpts::setCurrentConfig | ( | const QString & | s_file, |
UserMsg & | um | ||
) |
Set current file.
Current config file is meaningful if the values (maybe adjusted by the application) need to be saved back to a file.
The s_file may be one of the following keywords:
If s_file is an actual file that is different from the files used as system, user and local files the file is loaded and made current.
A previous current file that is not one of the three types (loaded by a previous call to setCurrentConfig()
) will be discarded by this method.
s_file | keyword or the path and name of the file to make current |
um | communication object |
void AppOpts::setValue | ( | const QString & | s_key, |
const QString & | s_value | ||
) |
Set a value.
The class represents values for options as a list of strings. This overload will create a list with a single member and use that as a value.
Any value that was previously assigned to this option will be overwritten.
s_key | the name of the variable to change |
s_value | a string value to use |
void AppOpts::setValue | ( | const QString & | s_key, |
const QStringList & | sl_value | ||
) |
Set a value.
The class represents values for options as a list of strings. The sl_value argument is directly used in internal map.
Any value that was previously assigned to this option will be overwritten.
s_key | the name of the variable to change |
s_value | a string value to use |
bool AppOpts::valueB | ( | const QString & | s_name, |
bool | b_default = false |
||
) | const |
Get a boolean value based on option's name.
The class represents values for options as a list of strings. For boolean values first entry in the list is used and the string is compared against FALSE
, false
and 0
; if the string matches the value is false, otherwise it is true.
s_name | name of the option to retreive |
b_default | default value if the option is not found |
double AppOpts::valueD | ( | const QString & | s_name, |
double | d_default = 0.0 |
||
) | const |
Get an integer value.
The class represents values for options as a list of strings. For double values first entry in the list is used and converted into double. If the conversion fails the default value is returned.
s_name | name of the option to retreive |
d_default | default value if the option is not found or can't be converted |
int AppOpts::valueI | ( | const QString & | s_name, |
int | i_default = 0 |
||
) | const |
Get an integer value.
The class represents values for options as a list of strings. For integer values first entry in the list is used and converted into integer. If the conversion fails the default value is returned.
s_name | name of the option to retreive |
i_default | default value if the option is not found or can't be converted |
QString AppOpts::valueS | ( | const QString & | s_name, |
const QString & | s_default = QString() |
||
) | const |
Get a string value.
The class represents values for options as a list of strings. For string values first entry in the list is used.
s_name | name of the option to retreive |
s_default | default value if the option is not found |
QStringList AppOpts::valueSL | ( | const QString & | s_name, |
const QStringList & | sl_default = QStringList() |
||
) | const |
Get a list of strings.
The class represents values for options as a list of strings. Entire list is returned if found.
An empty list is interpreted as a missing value and default list is returned.
s_name | name of the option to retreive |
s_default | default value if the option is not found |