]> vaikene.ee Git - evaf/blob - src/libs/Common/config.h
Merge branch 'master' of ssh://vaikene.net/var/gitpub/evaf
[evaf] / src / libs / Common / config.h
1 /**
2 * @file Common/config.h
3 * @brief eVaf configuration interface implementation
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 #ifndef __COMMON_CONFIG_H
21 # define __COMMON_CONFIG_H
22
23 #include "iconfig.h"
24
25 #include <QObject>
26 #include <QString>
27 #include <QVariant>
28 #include <QHash>
29 #include <QQueue>
30
31 namespace eVaf {
32 namespace Common {
33 class IniFile;
34 namespace Internal {
35
36 /**
37 * Structure for queued write operations
38 */
39 struct NameValuePair
40 {
41 NameValuePair(QString const & n, QVariant const & v)
42 : name(n)
43 , value(v)
44 {}
45
46 QString name;
47 QVariant value;
48 };
49
50 /**
51 * Default implementation of the iConfig interface.
52 *
53 * This class implements the iConfig interface using INI files in the eVaf::Common::iApp::instance()->etcDir() directory.
54 */
55 class Config : public iConfig
56 {
57 Q_OBJECT
58
59 public:
60
61 Config();
62
63 virtual ~Config();
64
65 /**
66 * Returns the current implementation of the iConfig interface
67 */
68 iConfig * interface() const;
69
70 /**
71 * Initializes the iConfig interface implementation.
72 * @return True if succeeded; false if not
73 */
74 bool init();
75
76 /**
77 * Finalizes the iConfig interface implementation.
78 */
79 void done();
80
81 /*
82 iConfig interface
83 */
84
85 virtual QVariant getValue(QString const & paramName, QVariant const & defaultValue) const;
86
87 virtual bool setValue(QString const & paramName, QVariant const & value, bool commit);
88
89
90 private: // Members
91
92 /// List of already opened INI files
93 mutable QHash<QString, IniFile *> mIniFiles;
94
95 /// Commit queue
96 QQueue<NameValuePair> mCommitQueue;
97
98
99 private: // Methods
100
101 /// Commits queued parameters
102 bool commitValues();
103
104 /// Writes a parameter value to the INI file
105 bool writeValue(QString const & paramName, QVariant const & value);
106
107 /// Writes a name/value pair to the INI file
108 inline bool writeValue(NameValuePair const & param)
109 {
110 return writeValue(param.name, param.value);
111 }
112
113 };
114
115 } // namespace eVaf::Common::Internal
116 } // namespace eVaf::Common
117 } // namespace eVaf
118
119 #endif // config.h