]>
vaikene.ee Git - evaf/blob - src/libs/Common/prop.cpp
86d3b1e61558a034dbab336e8b821aa394699ceb
3 * @brief Implementation of the iProp interface
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.
21 #include "iregistry.h"
30 //-------------------------------------------------------------------
32 using namespace eVaf::Common
;
34 iProp
* iProp::instance()
36 static Internal::Prop singleton
;
37 return singleton
.interface();
41 //-------------------------------------------------------------------
43 using namespace eVaf::Common::Internal
;
49 setObjectName(QString("%1.iProp").arg(VER_MODULE_NAME_STR
));
51 // Register the iProp interface
52 iRegistry::instance()->registerInterface("iProp", this);
60 iProp
* Prop::interface() const
62 return evafQueryInterface
<iProp
>("iProp");
67 // Set application name and language properties
68 setValue("applicationName", iApp::instance()->name());
69 setValue("applicationLanguage", iApp::instance()->language());
71 // Initialize properties defined in the application's XML file
72 QFile
xmlFile(iApp::instance()->etcDir() + iApp::instance()->xmlFileName());
73 if (xmlFile
.open(QFile::ReadOnly
)) {
75 QXmlStreamReader
xml(&xmlFile
);
76 while (!xml
.atEnd()) {
78 if (xml
.isStartElement()) {
79 if (xml
.name() == "properties") {
82 else if (isProp
&& xml
.name() == "property") {
84 if (isTrue(xml
.attributes().value("windowsonly").toString()))
88 if (isTrue(xml
.attributes().value("linuxonly").toString()))
91 // Get the name/value pair
92 QString name
= xml
.attributes().value("name").toString();
95 QString value
= xml
.attributes().value("value").toString();
97 // If value is empty, try the configuration
98 if (value
.isEmpty()) {
99 QString paramName
= xml
.attributes().value("config").toString();
100 if (!paramName
.isEmpty())
101 value
= iConfig::instance()->getValue(paramName
, "").toString();
104 setValue(name
, value
);
107 else if (xml
.isEndElement()) {
108 if (xml
.name() == "properties")
112 if (xml
.hasError()) {
113 EVAF_FATAL_ERROR("Error in the application's XML file %s : %s", qPrintable(xmlFile
.fileName()), qPrintable(xml
.errorString()));
118 // Initialize persistent properties
119 if (mPersistentProps
)
120 delete mPersistentProps
;
121 mPersistentProps
= new QSettings(QString("%1/.%2.dat").arg(iApp::instance()->dataRootDir()).arg(iApp::instance()->name()), QSettings::IniFormat
);
122 QStringList keys
= mPersistentProps
->allKeys();
123 for (int i
= 0; i
< keys
.size(); ++i
) {
124 QString key
= keys
.at(i
);
125 setValue(key
, mPersistentProps
->value(key
));
133 if (mPersistentProps
) {
134 delete mPersistentProps
;
135 mPersistentProps
= 0;
139 QVariant
Prop::getValue(QString
const & name
, QVariant
const & defaultValue
) const
141 QHash
<QString
, QVariant
>::const_iterator it
= mProps
.constFind(name
);
142 if (it
!= mProps
.constEnd()) {
143 QVariant value
= *it
;
144 if (value
.type() == defaultValue
.type())
147 return toVariant(value
.toString(), defaultValue
);
153 void Prop::setValue(QString
const & name
, QVariant
const & value
, bool persistent
)
155 bool isChanged
= true;
157 QHash
<QString
, QVariant
>::iterator it
= mProps
.find(name
);
158 if (it
!= mProps
.end()) {
159 isChanged
= *it
!= value
;
163 mProps
.insert(name
, value
);
165 if (persistent
&& mPersistentProps
&& mPersistentProps
->isWritable())
166 mPersistentProps
->setValue(name
, value
);
169 emit
valueChanged(name
, value
);