diff options
Diffstat (limited to 'examples/client/main.cpp')
-rw-r--r-- | examples/client/main.cpp | 222 |
1 files changed, 0 insertions, 222 deletions
diff --git a/examples/client/main.cpp b/examples/client/main.cpp deleted file mode 100644 index f4b472f..0000000 --- a/examples/client/main.cpp +++ /dev/null | |||
@@ -1,222 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include <QApplication> | ||
21 | #include <QCommandLineParser> | ||
22 | #include <QCommandLineOption> | ||
23 | #include <QTime> | ||
24 | |||
25 | #include "common/store.h" | ||
26 | #include "common/log.h" | ||
27 | |||
28 | #include <QWidget> | ||
29 | #include <QTreeView> | ||
30 | #include <QVBoxLayout> | ||
31 | #include <QLabel> | ||
32 | #include <QPushButton> | ||
33 | #include <QItemSelectionModel> | ||
34 | #include <iostream> | ||
35 | |||
36 | /** | ||
37 | * A small abstraction layer to use the sink store with the type available as string. | ||
38 | */ | ||
39 | class StoreBase { | ||
40 | public: | ||
41 | virtual ~StoreBase(){}; | ||
42 | virtual Sink::ApplicationDomain::ApplicationDomainType::Ptr getObject() = 0; | ||
43 | virtual Sink::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) = 0; | ||
44 | virtual KAsync::Job<void> create(const Sink::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
45 | virtual KAsync::Job<void> modify(const Sink::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
46 | virtual KAsync::Job<void> remove(const Sink::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
47 | virtual QSharedPointer<QAbstractItemModel> loadModel(const Sink::Query &query) = 0; | ||
48 | }; | ||
49 | |||
50 | template <typename T> | ||
51 | class Store : public StoreBase { | ||
52 | public: | ||
53 | Sink::ApplicationDomain::ApplicationDomainType::Ptr getObject() Q_DECL_OVERRIDE { | ||
54 | return T::Ptr::create(); | ||
55 | } | ||
56 | |||
57 | Sink::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) Q_DECL_OVERRIDE { | ||
58 | return T::Ptr::create(resourceInstanceIdentifier, identifier, 0, QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
59 | } | ||
60 | |||
61 | KAsync::Job<void> create(const Sink::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
62 | return Sink::Store::create<T>(*static_cast<const T*>(&type)); | ||
63 | } | ||
64 | |||
65 | KAsync::Job<void> modify(const Sink::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
66 | return Sink::Store::modify<T>(*static_cast<const T*>(&type)); | ||
67 | } | ||
68 | |||
69 | KAsync::Job<void> remove(const Sink::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
70 | return Sink::Store::remove<T>(*static_cast<const T*>(&type)); | ||
71 | } | ||
72 | |||
73 | QSharedPointer<QAbstractItemModel> loadModel(const Sink::Query &query) Q_DECL_OVERRIDE { | ||
74 | return Sink::Store::loadModel<T>(query); | ||
75 | } | ||
76 | }; | ||
77 | |||
78 | StoreBase& getStore(const QString &type) | ||
79 | { | ||
80 | if (type == "folder") { | ||
81 | static Store<Sink::ApplicationDomain::Folder> store; | ||
82 | return store; | ||
83 | } else if (type == "mail") { | ||
84 | static Store<Sink::ApplicationDomain::Mail> store; | ||
85 | return store; | ||
86 | } else if (type == "event") { | ||
87 | static Store<Sink::ApplicationDomain::Event> store; | ||
88 | return store; | ||
89 | } else if (type == "resource") { | ||
90 | static Store<Sink::ApplicationDomain::SinkResource> store; | ||
91 | return store; | ||
92 | } | ||
93 | Q_ASSERT(false); | ||
94 | qWarning() << "Trying to get a store that doesn't exist, falling back to event"; | ||
95 | static Store<Sink::ApplicationDomain::Event> store; | ||
96 | return store; | ||
97 | } | ||
98 | |||
99 | template <typename T> | ||
100 | class View : public QWidget | ||
101 | { | ||
102 | public: | ||
103 | View(QAbstractItemModel *model) | ||
104 | : QWidget() | ||
105 | { | ||
106 | auto modelView = new QTreeView(this); | ||
107 | modelView->setModel(model); | ||
108 | resize(1000, 1500); | ||
109 | |||
110 | auto topLayout = new QVBoxLayout(this); | ||
111 | |||
112 | auto titleLabel = new QLabel(this); | ||
113 | titleLabel->setText("Demo"); | ||
114 | auto font = titleLabel->font(); | ||
115 | font.setWeight(QFont::Bold); | ||
116 | titleLabel->setFont(font); | ||
117 | titleLabel->setAlignment(Qt::AlignCenter); | ||
118 | |||
119 | auto syncButton = new QPushButton(this); | ||
120 | syncButton->setText("Synchronize!"); | ||
121 | QObject::connect(syncButton, &QPushButton::pressed, []() { | ||
122 | Sink::Query query; | ||
123 | query.resources << "sink.dummy.instance1"; | ||
124 | Sink::Store::synchronize(query).exec(); | ||
125 | }); | ||
126 | |||
127 | auto removeButton = new QPushButton(this); | ||
128 | removeButton->setText("Remove"); | ||
129 | QObject::connect(removeButton, &QPushButton::pressed, [modelView]() { | ||
130 | for (auto index : modelView->selectionModel()->selectedIndexes()) { | ||
131 | auto object = index.data(Sink::Store::DomainObjectRole).value<typename T::Ptr>(); | ||
132 | Sink::Store::remove(*object).exec(); | ||
133 | } | ||
134 | }); | ||
135 | |||
136 | topLayout->addWidget(titleLabel); | ||
137 | topLayout->addWidget(syncButton); | ||
138 | topLayout->addWidget(removeButton); | ||
139 | topLayout->addWidget(modelView, 10); | ||
140 | |||
141 | show(); | ||
142 | } | ||
143 | |||
144 | }; | ||
145 | |||
146 | static QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Sink::Query query) | ||
147 | { | ||
148 | QTime time; | ||
149 | time.start(); | ||
150 | if (type == "folder") { | ||
151 | query.requestedProperties << "name" << "parent"; | ||
152 | } else if (type == "mail") { | ||
153 | query.requestedProperties << "subject" << "folder" << "date"; | ||
154 | } else if (type == "event") { | ||
155 | query.requestedProperties << "summary"; | ||
156 | } else if (type == "resource") { | ||
157 | query.requestedProperties << "type"; | ||
158 | } | ||
159 | auto model = getStore(type).loadModel(query); | ||
160 | qDebug() << "Folder type " << type; | ||
161 | qDebug() << "Loaded model in " << time.elapsed() << " ms"; | ||
162 | Q_ASSERT(model); | ||
163 | return model; | ||
164 | } | ||
165 | |||
166 | QMap<QString, QString> consumeMap(QList<QString> &list) | ||
167 | { | ||
168 | QMap<QString, QString> map; | ||
169 | while(list.size() >= 2) { | ||
170 | map.insert(list.at(0), list.at(1)); | ||
171 | list = list.mid(2); | ||
172 | } | ||
173 | return map; | ||
174 | } | ||
175 | |||
176 | int main(int argc, char *argv[]) | ||
177 | { | ||
178 | QApplication app(argc, argv); | ||
179 | |||
180 | QCommandLineParser cliOptions; | ||
181 | cliOptions.addPositionalArgument(QObject::tr("[type]"), | ||
182 | QObject::tr("A type to work with")); | ||
183 | cliOptions.addPositionalArgument(QObject::tr("[resource]"), | ||
184 | QObject::tr("A resource to connect to")); | ||
185 | cliOptions.addOption(QCommandLineOption("debuglevel", "A debuglevel from 0-6", "debuglevel")); | ||
186 | cliOptions.addHelpOption(); | ||
187 | cliOptions.process(app); | ||
188 | QStringList args = cliOptions.positionalArguments(); | ||
189 | |||
190 | if (cliOptions.isSet("debuglevel")) { | ||
191 | Sink::Log::setDebugOutputLevel(static_cast<Sink::Log::DebugLevel>(cliOptions.value("debuglevel").toInt())); | ||
192 | } | ||
193 | |||
194 | auto type = !args.isEmpty() ? args.takeFirst() : QByteArray(); | ||
195 | auto resources = args; | ||
196 | |||
197 | Sink::Query query; | ||
198 | for (const auto &res : resources) { | ||
199 | query.resources << res.toLatin1(); | ||
200 | } | ||
201 | query.liveQuery = true; | ||
202 | if (type == "folder") { | ||
203 | query.parentProperty = "parent"; | ||
204 | } | ||
205 | auto model = loadModel(type, query); | ||
206 | if (type == "folder") { | ||
207 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) { | ||
208 | for (int i = start; i <= end; i++) { | ||
209 | model->fetchMore(model->index(i, 0, index)); | ||
210 | } | ||
211 | }); | ||
212 | auto view = QSharedPointer<View<Sink::ApplicationDomain::Folder> >::create(model.data()); | ||
213 | app.exec(); | ||
214 | } else if (type == "mail") { | ||
215 | auto view = QSharedPointer<View<Sink::ApplicationDomain::Mail> >::create(model.data()); | ||
216 | app.exec(); | ||
217 | } else if (type == "event") { | ||
218 | auto view = QSharedPointer<View<Sink::ApplicationDomain::Event> >::create(model.data()); | ||
219 | app.exec(); | ||
220 | } | ||
221 | return 0; | ||
222 | } | ||