]> vaikene.ee Git - evaf/blob - src/libs/Common/prop.cpp
86d3b1e61558a034dbab336e8b821aa394699ceb
[evaf] / src / libs / Common / prop.cpp
1 /**
2 * @file Common/prop.h
3 * @brief Implementation of the iProp interface
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
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.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "prop.h"
21 #include "iregistry.h"
22 #include "ilogger.h"
23 #include "iapp.h"
24 #include "iconfig.h"
25 #include "util.h"
26 #include "version.h"
27
28 #include <QtCore>
29
30 //-------------------------------------------------------------------
31
32 using namespace eVaf::Common;
33
34 iProp * iProp::instance()
35 {
36 static Internal::Prop singleton;
37 return singleton.interface();
38 }
39
40
41 //-------------------------------------------------------------------
42
43 using namespace eVaf::Common::Internal;
44
45 Prop::Prop()
46 : iProp()
47 , mPersistentProps(0)
48 {
49 setObjectName(QString("%1.iProp").arg(VER_MODULE_NAME_STR));
50
51 // Register the iProp interface
52 iRegistry::instance()->registerInterface("iProp", this);
53 }
54
55 Prop::~Prop()
56 {
57 done();
58 }
59
60 iProp * Prop::interface() const
61 {
62 return evafQueryInterface<iProp>("iProp");
63 }
64
65 bool Prop::init()
66 {
67 // Set application name and language properties
68 setValue("applicationName", iApp::instance()->name());
69 setValue("applicationLanguage", iApp::instance()->language());
70
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)) {
74 bool isProp = false;
75 QXmlStreamReader xml(&xmlFile);
76 while (!xml.atEnd()) {
77 xml.readNext();
78 if (xml.isStartElement()) {
79 if (xml.name() == "properties") {
80 isProp = true;
81 }
82 else if (isProp && xml.name() == "property") {
83 #ifdef Q_OS_LINUX
84 if (isTrue(xml.attributes().value("windowsonly").toString()))
85 continue;
86 #endif
87 #ifdef Q_OS_WIN32
88 if (isTrue(xml.attributes().value("linuxonly").toString()))
89 continue;
90 #endif
91 // Get the name/value pair
92 QString name = xml.attributes().value("name").toString();
93 if (name.isEmpty())
94 continue;
95 QString value = xml.attributes().value("value").toString();
96
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();
102 }
103
104 setValue(name, value);
105 }
106 }
107 else if (xml.isEndElement()) {
108 if (xml.name() == "properties")
109 isProp = false;
110 }
111 }
112 if (xml.hasError()) {
113 EVAF_FATAL_ERROR("Error in the application's XML file %s : %s", qPrintable(xmlFile.fileName()), qPrintable(xml.errorString()));
114 return false;
115 }
116 }
117
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));
126 }
127
128 return true;
129 }
130
131 void Prop::done()
132 {
133 if (mPersistentProps) {
134 delete mPersistentProps;
135 mPersistentProps = 0;
136 }
137 }
138
139 QVariant Prop::getValue(QString const & name, QVariant const & defaultValue) const
140 {
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())
145 return value;
146 else
147 return toVariant(value.toString(), defaultValue);
148 }
149 else
150 return defaultValue;
151 }
152
153 void Prop::setValue(QString const & name, QVariant const & value, bool persistent)
154 {
155 bool isChanged = true;
156
157 QHash<QString, QVariant>::iterator it = mProps.find(name);
158 if (it != mProps.end()) {
159 isChanged = *it != value;
160 *it = value;
161 }
162 else
163 mProps.insert(name, value);
164
165 if (persistent && mPersistentProps && mPersistentProps->isWritable())
166 mPersistentProps->setValue(name, value);
167
168 if (isChanged)
169 emit valueChanged(name, value);
170 }