summaryrefslogtreecommitdiffstats
path: root/framework/src/settings/settings.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/settings/settings.cpp')
-rw-r--r--framework/src/settings/settings.cpp174
1 files changed, 174 insertions, 0 deletions
diff --git a/framework/src/settings/settings.cpp b/framework/src/settings/settings.cpp
new file mode 100644
index 00000000..00d1b9c5
--- /dev/null
+++ b/framework/src/settings/settings.cpp
@@ -0,0 +1,174 @@
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#include <QFile>
26
27using namespace Kube;
28
29Settings::Settings(QObject *parent)
30 : QObject(parent),
31 mLoaded(false)
32{
33
34}
35
36Settings::Settings(const QByteArray &id, QObject *parent)
37 : QObject(parent),
38 mIdentifier(id),
39 mLoaded(false)
40{
41 load();
42}
43
44Settings::Settings(const Settings &other)
45 : QObject(other.parent()),
46 mIdentifier(other.mIdentifier),
47 mLoaded(false)
48{
49 load();
50}
51
52Settings::~Settings()
53{
54}
55
56QSharedPointer<QSettings> Settings::getSettings()
57{
58 return QSharedPointer<QSettings>::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString("/kube/%1.ini").arg(QString::fromLatin1(mIdentifier)), QSettings::IniFormat);
59}
60
61void Settings::save()
62{
63 qWarning() << "Saving" << mIdentifier;
64 auto settings = getSettings();
65
66 for (const auto &p : dynamicPropertyNames()) {
67 qWarning() << "setting " << p << property(p);
68 if (p == "identifier" || p == "name") {
69 continue;
70 }
71 settings->setValue(p, property(p));
72 }
73 for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) {
74 const auto p = metaObject()->property(i).name();
75 if (p == QByteArray("identifier") || p == QByteArray("name")) {
76 continue;
77 }
78 qWarning() << "setting " << p << property(p);
79 settings->setValue(p, property(p));
80 }
81 settings->sync();
82}
83
84void Settings::remove()
85{
86 QFile::remove(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString("/kube/%1.ini").arg(QString::fromLatin1(mIdentifier)));
87}
88
89void Settings::load()
90{
91 if (mLoaded || mIdentifier.isEmpty() || mIdentifier.endsWith(".")) {
92 return;
93 }
94 mLoaded = true;
95 for (int i = metaObject()->propertyOffset(); i < metaObject()->propertyCount(); i++) {
96 auto p = metaObject()->property(i).name();
97 setProperty(p, QVariant());
98 }
99 auto settings = getSettings();
100 for (const auto &p : settings->allKeys()) {
101 qWarning() << "loading " << p << settings->value(p);
102 setProperty(p.toLatin1(), settings->value(p));
103 }
104}
105
106void Settings::setIdentifier(const QByteArray &id)
107{
108 mIdentifier = id;
109 load();
110}
111
112QByteArray Settings::identifier() const
113{
114 return mIdentifier;
115}
116
117ApplicationContext::ApplicationContext()
118 : Settings("applicationcontext")
119{
120
121}
122
123Account ApplicationContext::currentAccount() const
124{
125 return Account(property("currentAccountId").toByteArray());
126}
127
128Account::Account(const QByteArray &identifier)
129 : Settings("account." + identifier)
130{
131
132}
133
134Identity Account::primaryIdentity() const
135{
136 return Identity(property("primaryIdentityId").toByteArray());
137}
138
139QByteArray Account::type() const
140{
141 return property("type").toByteArray();
142}
143
144Identity::Identity(const QByteArray &identifier)
145 : Settings("identity." + identifier)
146{
147
148}
149
150Transport Identity::transport() const
151{
152 return Transport(property("transportId").toByteArray());
153}
154
155Transport::Transport(const QByteArray &identifier)
156 : Settings("transport." + identifier)
157{
158
159}
160
161QByteArray Transport::username() const
162{
163 return property("username").toByteArray();
164}
165
166QByteArray Transport::password() const
167{
168 return property("password").toByteArray();
169}
170
171QByteArray Transport::server() const
172{
173 return property("server").toByteArray();
174}