]>
vaikene.ee Git - evaf/blob - src/libs/Common/prop.cpp
3 * @brief Implementation of the iProp interface
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.
21 #include "iregistry.h"
30 //-------------------------------------------------------------------
32 using namespace eVaf::Common
;
36 static Internal::Prop
* singleton
= nullptr;
39 iProp
* iProp::instance()
41 if (nullptr == singleton
)
43 singleton
= new Internal::Prop
;
45 return singleton
->_interface();
49 //-------------------------------------------------------------------
51 using namespace eVaf::Common::Internal
;
53 void Prop::destroyInstance()
55 if (nullptr != singleton
)
65 setObjectName(QString("%1.iProp").arg(VER_MODULE_NAME_STR
));
67 // Register the iProp interface
68 iRegistry::instance()->registerInterface("iProp", this);
70 EVAF_INFO("%s-Prop created", VER_MODULE_NAME_STR
);
76 EVAF_INFO("%s-Prop destroyed", VER_MODULE_NAME_STR
);
79 iProp
* Prop::_interface() const
81 return evafQueryInterface
<iProp
>("iProp");
86 // Set application name and language properties
87 setValue("applicationName", iApp::instance()->name());
88 setValue("applicationLanguage", iApp::instance()->language());
90 // Initialize properties defined in the application's XML file
91 QFile
xmlFile(iApp::instance()->etcDir() + iApp::instance()->xmlFileName());
92 if (xmlFile
.open(QFile::ReadOnly
)) {
94 QXmlStreamReader
xml(&xmlFile
);
95 while (!xml
.atEnd()) {
97 if (xml
.isStartElement()) {
98 if (xml
.name() == "properties") {
101 else if (isProp
&& xml
.name() == "property") {
102 #if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
103 if (isTrue(xml
.attributes().value("windowsonly").toString()))
107 if (isTrue(xml
.attributes().value("linuxonly").toString()))
110 // Get the name/value pair
111 QString name
= xml
.attributes().value("name").toString();
114 QString value
= xml
.attributes().value("value").toString();
116 // If value is empty, try the configuration
117 if (value
.isEmpty()) {
118 QString paramName
= xml
.attributes().value("config").toString();
119 if (!paramName
.isEmpty())
120 value
= iConfig::instance()->getValue(paramName
, "").toString();
123 setValue(name
, value
);
126 else if (xml
.isEndElement()) {
127 if (xml
.name() == "properties")
131 if (xml
.hasError()) {
132 EVAF_FATAL_ERROR("Error in the application's XML file %s : %s", qPrintable(xmlFile
.fileName()), qPrintable(xml
.errorString()));
137 // Initialize persistent properties
138 mPersistentProps
.reset(new QSettings(QString("%1/.%2.dat")
139 .arg(iApp::instance()->dataRootDir())
140 .arg(iApp::instance()->name()),
141 QSettings::IniFormat
));
142 QStringList keys
= mPersistentProps
->allKeys();
143 for (int i
= 0; i
< keys
.size(); ++i
) {
144 QString key
= keys
.at(i
);
145 setValue(key
, mPersistentProps
->value(key
));
153 mPersistentProps
.reset();
156 QVariant
Prop::getValue(QString
const & name
, QVariant
const & defaultValue
) const
158 QHash
<QString
, QVariant
>::const_iterator it
= mProps
.constFind(name
);
159 if (it
!= mProps
.constEnd()) {
160 QVariant value
= *it
;
161 if (value
.type() == defaultValue
.type())
164 return toVariant(value
.toString(), defaultValue
);
170 void Prop::setValue(QString
const & name
, QVariant
const & value
, bool persistent
)
172 bool isChanged
= true;
174 QHash
<QString
, QVariant
>::iterator it
= mProps
.find(name
);
175 if (it
!= mProps
.end()) {
176 isChanged
= *it
!= value
;
180 mProps
.insert(name
, value
);
182 if (persistent
&& mPersistentProps
&& mPersistentProps
->isWritable())
183 mPersistentProps
->setValue(name
, value
);
186 emit
valueChanged(name
, value
);