]>
vaikene.ee Git - evaf/blob - src/libs/Common/config.cpp
610dd74d98b445ba766efad28cc0badf0df0e557
2 * @file Common/config.cpp
3 * @brief eVaf configuration interface implementation
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
22 #include "iregistry.h"
29 //-------------------------------------------------------------------
31 using namespace eVaf::Common
;
33 iConfig
* iConfig::instance()
35 static Internal::Config singleton
;
36 return singleton
.interface();
40 //-------------------------------------------------------------------
42 using namespace eVaf::Common::Internal
;
47 setObjectName(QString("%1.iConfig").arg(VER_MODULE_NAME_STR
));
49 // Register the iConfig interface
50 iRegistry::instance()->registerInterface("iConfig", this);
58 iConfig
* Config::interface() const
60 return evafQueryInterface
<iConfig
>("iConfig");
65 // Finalize first in case this is not the first time init() is called
73 // Commit any queued parameters
76 // Clear the list of opened INI files
77 QHash
<QString
, IniFile
*>::iterator it
= mIniFiles
.begin();
78 while (it
!= mIniFiles
.end()) {
80 it
= mIniFiles
.erase(it
);
84 QVariant
Config::getValue(QString
const & paramName
, QVariant
const & defaultValue
) const
86 // Get the optional file part
87 int idx
= paramName
.indexOf('/');
89 EVAF_ERROR("Invalid parameter name '%s'", qPrintable(paramName
));
92 QString file
= paramName
.left(idx
);
93 QString name
= paramName
.mid(idx
+ 1);
95 // Ignore the optional backend identifier in the file name
96 idx
= file
.indexOf(':');
98 file
.remove(0, idx
+ 1);
100 // If the file name is empty or '*', use the application's name
101 if (file
.isEmpty() || file
== "*")
102 file
= iApp::instance()->name();
106 // Is this INI file already opened?
107 QHash
<QString
, IniFile
*>::const_iterator it
= mIniFiles
.constFind(file
);
109 // The file is opened and we can reuse it
110 if (it
!= mIniFiles
.constEnd()) {
114 // The file is not opened
116 ini
= new IniFile(QString("%1/%2.ini").arg(iApp::instance()->etcDir()).arg(file
));
117 if (!ini
->isValid()) {
118 EVAF_ERROR("Failed to open '%s' : %s", qPrintable(name
), qPrintable(ini
->errorString()));
122 mIniFiles
.insert(file
, ini
);
126 return ini
->getValue(name
, defaultValue
);
129 bool Config::setValue(QString
const & paramName
, QVariant
const & value
, bool commit
)
132 // Queue the write operation
133 mCommitQueue
.enqueue(NameValuePair(paramName
, value
));
136 // Commit any queued parameters
140 // Write the parameter
141 if (!writeValue(paramName
, value
))
148 bool Config::commitValues()
150 while (!mCommitQueue
.isEmpty()) {
151 if (!writeValue(mCommitQueue
.dequeue())) {
152 mCommitQueue
.clear();
159 bool Config::writeValue(QString
const & paramName
, QVariant
const & value
)
161 // Get the optional file part
162 int idx
= paramName
.indexOf('/');
164 EVAF_ERROR("Invalid parameter name '%s'", qPrintable(paramName
));
167 QString file
= paramName
.left(idx
);
168 QString name
= paramName
.mid(idx
+ 1);
170 // Ignore the optional backend identifier in the file name
171 idx
= file
.indexOf(':');
173 file
.remove(0, idx
+ 1);
175 // If the file name is empty or '*', use the application's name
176 if (file
.isEmpty() || file
== "*")
177 file
= iApp::instance()->name();
181 // Is this INI file already opened?
182 QHash
<QString
, IniFile
*>::const_iterator it
= mIniFiles
.constFind(file
);
184 // The file is opened and we can reuse it
185 if (it
!= mIniFiles
.constEnd()) {
189 // The file is not opened
191 ini
= new IniFile(QString("%1/%2.ini").arg(iApp::instance()->etcDir()).arg(file
));
192 if (!ini
->isValid()) {
193 EVAF_ERROR("Failed to open '%s' : %s", qPrintable(name
), qPrintable(ini
->errorString()));
197 mIniFiles
.insert(file
, ini
);
201 if (!ini
->setValue(name
, value
))