]>
vaikene.ee Git - evaf/blob - src/libs/Common/config.cpp
2 * @file Common/config.cpp
3 * @brief eVaf configuration interface implementation
6 * Copyright (c) 2011-2019 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
;
35 static Internal::Config
* singleton
= nullptr;
38 iConfig
* iConfig::instance()
40 if (nullptr == singleton
)
42 singleton
= new Internal::Config
;
44 return singleton
->_interface();
48 //-------------------------------------------------------------------
50 using namespace eVaf::Common::Internal
;
52 void Config::destroyInstance()
54 if (nullptr != singleton
)
64 setObjectName(QString("%1.iConfig").arg(VER_MODULE_NAME_STR
));
66 // Register the iConfig interface
67 iRegistry::instance()->registerInterface("iConfig", this);
69 EVAF_INFO("%s-Config created", VER_MODULE_NAME_STR
);
75 EVAF_INFO("%s-Config destroyed", VER_MODULE_NAME_STR
);
78 iConfig
* Config::_interface() const
80 return evafQueryInterface
<iConfig
>("iConfig");
85 // Finalize first in case this is not the first time init() is called
93 // Commit any queued parameters
96 // Clear the list of opened INI files
97 QHash
<QString
, IniFile
*>::iterator it
= mIniFiles
.begin();
98 while (it
!= mIniFiles
.end()) {
100 it
= mIniFiles
.erase(it
);
104 QVariant
Config::getValue(QString
const & paramName
, QVariant
const & defaultValue
) const
106 // Get the optional file part
107 int idx
= paramName
.indexOf('/');
109 EVAF_ERROR("Invalid parameter name '%s'", qPrintable(paramName
));
112 QString file
= paramName
.left(idx
);
113 QString name
= paramName
.mid(idx
+ 1);
115 // Ignore the optional backend identifier in the file name
116 idx
= file
.indexOf(':');
118 file
.remove(0, idx
+ 1);
120 // If the file name is empty or '*', use the application's name
121 if (file
.isEmpty() || file
== "*")
122 file
= iApp::instance()->name();
124 IniFile
* ini
= nullptr;
126 // Is this INI file already opened?
127 QHash
<QString
, IniFile
*>::const_iterator it
= mIniFiles
.constFind(file
);
129 // The file is opened and we can reuse it
130 if (it
!= mIniFiles
.constEnd()) {
134 // The file is not opened
136 ini
= new IniFile(QString("%1/%2.ini").arg(iApp::instance()->etcDir()).arg(file
));
137 if (!ini
->isValid()) {
138 EVAF_ERROR("Failed to open '%s' : %s", qPrintable(name
), qPrintable(ini
->errorString()));
142 mIniFiles
.insert(file
, ini
);
146 return ini
->getValue(name
.toLocal8Bit(), defaultValue
);
149 bool Config::setValue(QString
const & paramName
, QVariant
const & value
, bool commit
)
152 // Queue the write operation
153 mCommitQueue
.enqueue(NameValuePair(paramName
, value
));
156 // Commit any queued parameters
160 // Write the parameter
161 if (!writeValue(paramName
, value
))
168 bool Config::commitValues()
170 while (!mCommitQueue
.isEmpty()) {
171 if (!writeValue(mCommitQueue
.dequeue())) {
172 mCommitQueue
.clear();
179 bool Config::writeValue(QString
const & paramName
, QVariant
const & value
)
181 // Get the optional file part
182 int idx
= paramName
.indexOf('/');
184 EVAF_ERROR("Invalid parameter name '%s'", qPrintable(paramName
));
187 QString file
= paramName
.left(idx
);
188 QString name
= paramName
.mid(idx
+ 1);
190 // Ignore the optional backend identifier in the file name
191 idx
= file
.indexOf(':');
193 file
.remove(0, idx
+ 1);
195 // If the file name is empty or '*', use the application's name
196 if (file
.isEmpty() || file
== "*")
197 file
= iApp::instance()->name();
199 IniFile
* ini
= nullptr;
201 // Is this INI file already opened?
202 QHash
<QString
, IniFile
*>::const_iterator it
= mIniFiles
.constFind(file
);
204 // The file is opened and we can reuse it
205 if (it
!= mIniFiles
.constEnd()) {
209 // The file is not opened
211 ini
= new IniFile(QString("%1/%2.ini").arg(iApp::instance()->etcDir()).arg(file
));
212 if (!ini
->isValid()) {
213 EVAF_ERROR("Failed to open '%s' : %s", qPrintable(name
), qPrintable(ini
->errorString()));
217 mIniFiles
.insert(file
, ini
);
221 if (!ini
->setValue(name
.toLocal8Bit(), value
))