]> vaikene.ee Git - evaf/blob - src/libs/Common/config.h
6abdda51a5007876ea21e5702e01f638cf5f076e
[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-2019 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 /**
62 * Destroys the iConfig interface instance.
63 */
64 static void destroyInstance();
65
66 Config();
67
68 virtual ~Config();
69
70 /**
71 * Returns the current implementation of the iConfig interface
72 */
73 iConfig * _interface() const;
74
75 /**
76 * Initializes the iConfig interface implementation.
77 * @return True if succeeded; false if not
78 */
79 bool init();
80
81 /**
82 * Finalizes the iConfig interface implementation.
83 */
84 void done();
85
86 /*
87 iConfig interface
88 */
89
90 virtual QVariant getValue(QString const & paramName, QVariant const & defaultValue) const;
91
92 virtual bool setValue(QString const & paramName, QVariant const & value, bool commit);
93
94
95 private: // Members
96
97 /// List of already opened INI files
98 mutable QHash<QString, IniFile *> mIniFiles;
99
100 /// Commit queue
101 QQueue<NameValuePair> mCommitQueue;
102
103
104 private: // Methods
105
106 /// Commits queued parameters
107 bool commitValues();
108
109 /// Writes a parameter value to the INI file
110 bool writeValue(QString const & paramName, QVariant const & value);
111
112 /// Writes a name/value pair to the INI file
113 inline bool writeValue(NameValuePair const & param)
114 {
115 return writeValue(param.name, param.value);
116 }
117
118 };
119
120 } // namespace eVaf::Common::Internal
121 } // namespace eVaf::Common
122 } // namespace eVaf
123
124 #endif // config.h