AppOpts Pile   version 0.0.1
The pile provides the application with easy loading
AppOpts Class Reference

Application options. More...

#include <appopts.h>

+ Inheritance diagram for AppOpts:

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...
 

Detailed Description

Application options.

This class holds a list of key-value pairs, where values are ultimately list of strings.

Constructor & Destructor Documentation

AppOpts::AppOpts ( )

Default constructor.

Creates a valid instance.

AppOpts::~AppOpts ( )
virtual

Destructor.

Releases all resources associated with this instance.

Member Function Documentation

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.

Parameters
s_keythe name of the variable to change
s_valuea 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.

Parameters
s_keythe name of the variable to change
s_valuea string value to use
QString AppOpts::cfgFileName ( const QString &  s_app_name)
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.

Parameters
s_app_nameInput template.
Returns
the name of the config file without extension.
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.

Parameters
s_fileInput file's path.
out_persResulted PerSt object, if any.
umcommunication device.
Returns
true if everything went fine.
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.

Parameters
umstructure used to show messages.
s_app_namethe name to use for file name.
Returns
true if everything went fine.

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.

Parameters
listthe list of variables to search
umcommunication object
Returns
true if all required variables were found
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.

Parameters
optdefinition of the variable to search
umcommunication object
Returns
true if the variable was found in at least one file
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:

  • system
  • user
  • local In this case, if the coresponding persistent object is valid, the file is used as current file.

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.

Parameters
s_filekeyword or the path and name of the file to make current
umcommunication object
Returns
true if everything went well
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.

Parameters
s_keythe name of the variable to change
s_valuea 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.

Parameters
s_keythe name of the variable to change
s_valuea 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.

Parameters
s_namename of the option to retreive
b_defaultdefault value if the option is not found
Returns
the result
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.

Parameters
s_namename of the option to retreive
d_defaultdefault value if the option is not found or can't be converted
Returns
the number
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.

Parameters
s_namename of the option to retreive
i_defaultdefault value if the option is not found or can't be converted
Returns
the integer
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.

Parameters
s_namename of the option to retreive
s_defaultdefault value if the option is not found
Returns
the string that was 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.

Parameters
s_namename of the option to retreive
s_defaultdefault value if the option is not found
Returns
the list that was found

The documentation for this class was generated from the following files: