diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-25 16:40:54 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-25 16:40:54 +0100 |
commit | fed67ae13d4b9c109449f6077cea328913a8548e (patch) | |
tree | 057d19f9412b2120df749259d393b18c1d5cfb24 /framework/settings/settings.cpp | |
parent | 0aba0c3fc68712383774263d0906f8e996e1e9c0 (diff) | |
download | kube-fed67ae13d4b9c109449f6077cea328913a8548e.tar.gz kube-fed67ae13d4b9c109449f6077cea328913a8548e.zip |
An overly basic settings framework.
and a settings view to mess around.
Diffstat (limited to 'framework/settings/settings.cpp')
-rw-r--r-- | framework/settings/settings.cpp | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/framework/settings/settings.cpp b/framework/settings/settings.cpp new file mode 100644 index 00000000..a4e28190 --- /dev/null +++ b/framework/settings/settings.cpp | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #include "settings.h" | ||
20 | |||
21 | #include <QDebug> | ||
22 | #include <QStandardPaths> | ||
23 | #include <QMetaObject> | ||
24 | #include <QMetaProperty> | ||
25 | |||
26 | using namespace Kube; | ||
27 | |||
28 | Settings::Settings(QObject *parent) | ||
29 | : QObject(parent) | ||
30 | { | ||
31 | |||
32 | } | ||
33 | |||
34 | Settings::Settings(const QByteArray &id, QObject *parent) | ||
35 | : QObject(parent), | ||
36 | mIdentifier(id) | ||
37 | { | ||
38 | load(); | ||
39 | } | ||
40 | |||
41 | Settings::Settings(const Settings &other) | ||
42 | : QObject(other.parent()), | ||
43 | mIdentifier(other.mIdentifier) | ||
44 | { | ||
45 | load(); | ||
46 | } | ||
47 | |||
48 | Settings::~Settings() | ||
49 | { | ||
50 | // save(); | ||
51 | } | ||
52 | |||
53 | QSharedPointer<QSettings> Settings::getSettings() | ||
54 | { | ||
55 | return QSharedPointer<QSettings>::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString("/kube/%1.ini").arg(QString::fromLatin1(mIdentifier)), QSettings::IniFormat); | ||
56 | } | ||
57 | |||
58 | void Settings::save() | ||
59 | { | ||
60 | qWarning() << "Saving" << mIdentifier; | ||
61 | auto settings = getSettings(); | ||
62 | for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) { | ||
63 | const auto p = metaObject()->property(i).name(); | ||
64 | qWarning() << "setting " << p << property(p); | ||
65 | settings->setValue(p, property(p)); | ||
66 | } | ||
67 | settings->sync(); | ||
68 | } | ||
69 | |||
70 | void Settings::load() | ||
71 | { | ||
72 | qWarning() << "loading" << mIdentifier; | ||
73 | for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) { | ||
74 | auto p = metaObject()->property(i).name(); | ||
75 | setProperty(p, QVariant()); | ||
76 | } | ||
77 | auto settings = getSettings(); | ||
78 | for (const auto &p : settings->allKeys()) { | ||
79 | qWarning() << "loading " << p << settings->value(p); | ||
80 | setProperty(p.toLatin1(), settings->value(p)); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | void Settings::setIdentifier(const QByteArray &id) | ||
85 | { | ||
86 | mIdentifier = id; | ||
87 | load(); | ||
88 | } | ||
89 | |||
90 | QByteArray Settings::identifier() const | ||
91 | { | ||
92 | return mIdentifier; | ||
93 | } | ||
94 | |||
95 | ApplicationContext::ApplicationContext() | ||
96 | : Settings("applicationcontext") | ||
97 | { | ||
98 | |||
99 | } | ||
100 | |||
101 | Account ApplicationContext::currentAccount() const | ||
102 | { | ||
103 | return Account(property("currentAccountId").toByteArray()); | ||
104 | } | ||
105 | |||
106 | Account::Account(const QByteArray &identifier) | ||
107 | : Settings("account." + identifier) | ||
108 | { | ||
109 | |||
110 | } | ||
111 | |||
112 | Identity Account::primaryIdentity() const | ||
113 | { | ||
114 | return Identity(property("primaryIdentityId").toByteArray()); | ||
115 | } | ||
116 | |||
117 | Identity::Identity(const QByteArray &identifier) | ||
118 | : Settings("identity." + identifier) | ||
119 | { | ||
120 | |||
121 | } | ||
122 | |||
123 | Transport Identity::transport() const | ||
124 | { | ||
125 | return Transport(property("transportId").toByteArray()); | ||
126 | } | ||
127 | |||
128 | Transport::Transport(const QByteArray &identifier) | ||
129 | : Settings("transport." + identifier) | ||
130 | { | ||
131 | |||
132 | } | ||
133 | |||
134 | QByteArray Transport::username() const | ||
135 | { | ||
136 | return property("username").toByteArray(); | ||
137 | } | ||
138 | |||
139 | QByteArray Transport::password() const | ||
140 | { | ||
141 | return property("password").toByteArray(); | ||
142 | } | ||
143 | |||
144 | QByteArray Transport::server() const | ||
145 | { | ||
146 | return property("server").toByteArray(); | ||
147 | } | ||