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 | |
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')
-rw-r--r-- | framework/settings/CMakeLists.txt | 13 | ||||
-rw-r--r-- | framework/settings/qmldir | 2 | ||||
-rw-r--r-- | framework/settings/settings.cpp | 147 | ||||
-rw-r--r-- | framework/settings/settings.h | 89 | ||||
-rw-r--r-- | framework/settings/settingsplugin.cpp | 16 |
5 files changed, 253 insertions, 14 deletions
diff --git a/framework/settings/CMakeLists.txt b/framework/settings/CMakeLists.txt index 77268ab9..15ab7584 100644 --- a/framework/settings/CMakeLists.txt +++ b/framework/settings/CMakeLists.txt | |||
@@ -1,15 +1,16 @@ | |||
1 | set(settingsplugin_SRCS | 1 | set(settingsplugin_SRCS |
2 | settingsplugin.cpp | 2 | settingsplugin.cpp |
3 | maildir_resource.cpp | 3 | # maildir_resource.cpp |
4 | resourcelistmodel.cpp | 4 | # resourcelistmodel.cpp |
5 | resourcescontroller.cpp | 5 | # resourcescontroller.cpp |
6 | settings.cpp | ||
6 | ) | 7 | ) |
7 | 8 | ||
8 | add_library(settingsplugin SHARED ${settingsplugin_SRCS}) | 9 | add_library(settingsplugin SHARED ${settingsplugin_SRCS}) |
9 | 10 | ||
10 | qt5_use_modules(settingsplugin Core Quick Qml) | 11 | qt5_use_modules(settingsplugin Core Quick Qml) |
11 | 12 | ||
12 | target_link_libraries(settingsplugin sink) | 13 | target_link_libraries(settingsplugin) |
13 | 14 | ||
14 | install(TARGETS settingsplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/sink/settings) | 15 | install(TARGETS settingsplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/settings) |
15 | install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/sink/settings) | 16 | install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/settings) |
diff --git a/framework/settings/qmldir b/framework/settings/qmldir index 1740f29a..7b756aea 100644 --- a/framework/settings/qmldir +++ b/framework/settings/qmldir | |||
@@ -1,3 +1,3 @@ | |||
1 | module org.kde.sink.settings | 1 | module org.kde.kube.settings |
2 | 2 | ||
3 | plugin settingsplugin | 3 | plugin settingsplugin |
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 | } | ||
diff --git a/framework/settings/settings.h b/framework/settings/settings.h new file mode 100644 index 00000000..bfee55cb --- /dev/null +++ b/framework/settings/settings.h | |||
@@ -0,0 +1,89 @@ | |||
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 | #pragma once | ||
20 | |||
21 | #include <QObject> | ||
22 | #include <QByteArray> | ||
23 | #include <QSettings> | ||
24 | #include <QSharedPointer> | ||
25 | |||
26 | namespace Kube { | ||
27 | |||
28 | class Settings : public QObject { | ||
29 | Q_OBJECT | ||
30 | Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier) | ||
31 | public: | ||
32 | Settings(QObject *parent = 0); | ||
33 | Settings(const QByteArray &id, QObject *parent = 0); | ||
34 | virtual ~Settings(); | ||
35 | Settings(const Settings&); | ||
36 | |||
37 | void setIdentifier(const QByteArray &id); | ||
38 | QByteArray identifier() const; | ||
39 | |||
40 | Q_INVOKABLE void save(); | ||
41 | private: | ||
42 | void load(); | ||
43 | QSharedPointer<QSettings> getSettings(); | ||
44 | QByteArray mIdentifier; | ||
45 | }; | ||
46 | |||
47 | class Account; | ||
48 | class Identity; | ||
49 | class Transport; | ||
50 | |||
51 | class ApplicationContext : public Settings | ||
52 | { | ||
53 | Q_OBJECT | ||
54 | public: | ||
55 | ApplicationContext(); | ||
56 | Account currentAccount() const; | ||
57 | |||
58 | }; | ||
59 | |||
60 | class Account : public Settings | ||
61 | { | ||
62 | Q_OBJECT | ||
63 | public: | ||
64 | Account(const QByteArray &identifier); | ||
65 | Identity primaryIdentity() const; | ||
66 | }; | ||
67 | |||
68 | class Identity : public Settings | ||
69 | { | ||
70 | Q_OBJECT | ||
71 | public: | ||
72 | Identity(const QByteArray &identifier); | ||
73 | Transport transport() const; | ||
74 | }; | ||
75 | |||
76 | class Transport : public Settings | ||
77 | { | ||
78 | Q_OBJECT | ||
79 | public: | ||
80 | Transport(const QByteArray &identifier); | ||
81 | QByteArray username() const; | ||
82 | QByteArray password() const; | ||
83 | QByteArray server() const; | ||
84 | }; | ||
85 | |||
86 | } | ||
87 | |||
88 | Q_DECLARE_METATYPE(Kube::Settings*); | ||
89 | |||
diff --git a/framework/settings/settingsplugin.cpp b/framework/settings/settingsplugin.cpp index ca670583..a1888669 100644 --- a/framework/settings/settingsplugin.cpp +++ b/framework/settings/settingsplugin.cpp | |||
@@ -1,16 +1,18 @@ | |||
1 | #include "settingsplugin.h" | 1 | #include "settingsplugin.h" |
2 | 2 | ||
3 | #include "resourcescontroller.h" | 3 | // #include "resourcescontroller.h" |
4 | #include "resourcelistmodel.h" | 4 | // #include "resourcelistmodel.h" |
5 | #include "maildir_resource.h" | 5 | // #include "maildir_resource.h" |
6 | #include "settings.h" | ||
6 | 7 | ||
7 | #include <QtQml> | 8 | #include <QtQml> |
8 | 9 | ||
9 | void SettingsPlugin::registerTypes (const char *uri) | 10 | void SettingsPlugin::registerTypes (const char *uri) |
10 | { | 11 | { |
11 | Q_ASSERT(uri == QLatin1String("org.kde.sink.settings")); | 12 | Q_ASSERT(uri == QLatin1String("org.kde.kube.settings")); |
12 | 13 | ||
13 | qmlRegisterType<ResourceListModel>(); | 14 | // qmlRegisterType<ResourceListModel>(); |
14 | qmlRegisterType<ResourcesController>(uri, 1, 0, "Resources"); | 15 | // qmlRegisterType<ResourcesController>(uri, 1, 0, "Resources"); |
15 | qmlRegisterType<MaildirResouceController>(uri, 1, 0, "Maildir"); | 16 | // qmlRegisterType<MaildirResouceController>(uri, 1, 0, "Maildir"); |
17 | qmlRegisterType<Kube::Settings>(uri, 1, 0, "Settings"); | ||
16 | } | 18 | } |