diff options
Diffstat (limited to 'examples/client')
-rw-r--r-- | examples/client/main.cpp | 158 |
1 files changed, 122 insertions, 36 deletions
diff --git a/examples/client/main.cpp b/examples/client/main.cpp index 04ced55..ce29b95 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp | |||
@@ -41,6 +41,68 @@ | |||
41 | #include <QItemSelectionModel> | 41 | #include <QItemSelectionModel> |
42 | #include <iostream> | 42 | #include <iostream> |
43 | 43 | ||
44 | /** | ||
45 | * A small abstraction layer to use the akonadi store with the type available as string. | ||
46 | */ | ||
47 | class StoreBase { | ||
48 | public: | ||
49 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() = 0; | ||
50 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) = 0; | ||
51 | virtual KAsync::Job<void> create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
52 | virtual KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
53 | virtual KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) = 0; | ||
54 | virtual QSharedPointer<QAbstractItemModel> loadModel(const Akonadi2::Query &query) = 0; | ||
55 | }; | ||
56 | |||
57 | template <typename T> | ||
58 | class Store : public StoreBase { | ||
59 | public: | ||
60 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject() Q_DECL_OVERRIDE { | ||
61 | return T::Ptr::create(); | ||
62 | } | ||
63 | |||
64 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr getObject(const QByteArray &resourceInstanceIdentifier, const QByteArray &identifier = QByteArray()) Q_DECL_OVERRIDE { | ||
65 | return T::Ptr::create(resourceInstanceIdentifier, identifier, 0, QSharedPointer<Akonadi2::ApplicationDomain::MemoryBufferAdaptor>::create()); | ||
66 | } | ||
67 | |||
68 | KAsync::Job<void> create(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
69 | return Akonadi2::Store::create<T>(*static_cast<const T*>(&type)); | ||
70 | } | ||
71 | |||
72 | KAsync::Job<void> modify(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
73 | return Akonadi2::Store::modify<T>(*static_cast<const T*>(&type)); | ||
74 | } | ||
75 | |||
76 | KAsync::Job<void> remove(const Akonadi2::ApplicationDomain::ApplicationDomainType &type) Q_DECL_OVERRIDE { | ||
77 | return Akonadi2::Store::remove<T>(*static_cast<const T*>(&type)); | ||
78 | } | ||
79 | |||
80 | QSharedPointer<QAbstractItemModel> loadModel(const Akonadi2::Query &query) Q_DECL_OVERRIDE { | ||
81 | return Akonadi2::Store::loadModel<T>(query); | ||
82 | } | ||
83 | }; | ||
84 | |||
85 | StoreBase& getStore(const QString &type) | ||
86 | { | ||
87 | if (type == "folder") { | ||
88 | static Store<Akonadi2::ApplicationDomain::Folder> store; | ||
89 | return store; | ||
90 | } else if (type == "mail") { | ||
91 | static Store<Akonadi2::ApplicationDomain::Mail> store; | ||
92 | return store; | ||
93 | } else if (type == "event") { | ||
94 | static Store<Akonadi2::ApplicationDomain::Event> store; | ||
95 | return store; | ||
96 | } else if (type == "resource") { | ||
97 | static Store<Akonadi2::ApplicationDomain::AkonadiResource> store; | ||
98 | return store; | ||
99 | } | ||
100 | Q_ASSERT(false); | ||
101 | qWarning() << "Trying to get a store that doesn't exist, falling back to event"; | ||
102 | static Store<Akonadi2::ApplicationDomain::Event> store; | ||
103 | return store; | ||
104 | } | ||
105 | |||
44 | template <typename T> | 106 | template <typename T> |
45 | class View : public QWidget | 107 | class View : public QWidget |
46 | { | 108 | { |
@@ -108,31 +170,37 @@ public: | |||
108 | } | 170 | } |
109 | }; | 171 | }; |
110 | 172 | ||
173 | |||
111 | static QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query) | 174 | static QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query) |
112 | { | 175 | { |
113 | QTime time; | 176 | QTime time; |
114 | time.start(); | 177 | time.start(); |
115 | |||
116 | QSharedPointer<QAbstractItemModel> model; | ||
117 | if (type == "folder") { | 178 | if (type == "folder") { |
118 | query.requestedProperties << "name" << "parent"; | 179 | query.requestedProperties << "name" << "parent"; |
119 | model = Akonadi2::Store::loadModel<Akonadi2::ApplicationDomain::Folder>(query); | ||
120 | } else if (type == "mail") { | 180 | } else if (type == "mail") { |
121 | query.requestedProperties << "subject" << "folder" << "date"; | 181 | query.requestedProperties << "subject" << "folder" << "date"; |
122 | model = Akonadi2::Store::loadModel<Akonadi2::ApplicationDomain::Mail>(query); | ||
123 | } else if (type == "event") { | 182 | } else if (type == "event") { |
124 | query.requestedProperties << "summary"; | 183 | query.requestedProperties << "summary"; |
125 | model = Akonadi2::Store::loadModel<Akonadi2::ApplicationDomain::Event>(query); | ||
126 | } else if (type == "resource") { | 184 | } else if (type == "resource") { |
127 | query.requestedProperties << "identifier" << "type"; | 185 | query.requestedProperties << "type"; |
128 | model = Akonadi2::Store::loadModel<Akonadi2::ApplicationDomain::AkonadiResource>(query); | ||
129 | } | 186 | } |
187 | auto model = getStore(type).loadModel(query); | ||
130 | qDebug() << "Folder type " << type; | 188 | qDebug() << "Folder type " << type; |
131 | qDebug() << "Loaded model in " << time.elapsed() << " ms"; | 189 | qDebug() << "Loaded model in " << time.elapsed() << " ms"; |
132 | Q_ASSERT(model); | 190 | Q_ASSERT(model); |
133 | return model; | 191 | return model; |
134 | } | 192 | } |
135 | 193 | ||
194 | QMap<QString, QString> consumeMap(QList<QString> &list) | ||
195 | { | ||
196 | QMap<QString, QString> map; | ||
197 | while(list.size() >= 2) { | ||
198 | map.insert(list.at(0), list.at(1)); | ||
199 | list = list.mid(2); | ||
200 | } | ||
201 | return map; | ||
202 | } | ||
203 | |||
136 | int main(int argc, char *argv[]) | 204 | int main(int argc, char *argv[]) |
137 | { | 205 | { |
138 | MyApplication app(argc, argv); | 206 | MyApplication app(argc, argv); |
@@ -154,12 +222,6 @@ int main(int argc, char *argv[]) | |||
154 | Akonadi2::Log::setDebugOutputLevel(static_cast<Akonadi2::Log::DebugLevel>(cliOptions.value("debuglevel").toInt())); | 222 | Akonadi2::Log::setDebugOutputLevel(static_cast<Akonadi2::Log::DebugLevel>(cliOptions.value("debuglevel").toInt())); |
155 | } | 223 | } |
156 | 224 | ||
157 | //Ensure resource is ready | ||
158 | // for (const auto &resource : resources) { | ||
159 | // Akonadi2::ResourceFactory::load(Akonadi2::Store::resourceName(resource.toLatin1())); | ||
160 | // ResourceConfig::addResource(resource.toLatin1(), Akonadi2::Store::resourceName(resource.toLatin1())); | ||
161 | // } | ||
162 | |||
163 | if (command == "list") { | 225 | if (command == "list") { |
164 | auto type = !args.isEmpty() ? args.takeFirst() : QByteArray(); | 226 | auto type = !args.isEmpty() ? args.takeFirst() : QByteArray(); |
165 | auto resources = args; | 227 | auto resources = args; |
@@ -174,17 +236,22 @@ int main(int argc, char *argv[]) | |||
174 | 236 | ||
175 | auto model = loadModel(type, query); | 237 | auto model = loadModel(type, query); |
176 | qDebug() << "Listing"; | 238 | qDebug() << "Listing"; |
177 | std::cout << "\tColumn\t\t Identifier\t\t\t\t"; | 239 | int colSize = 38; //Necessary to display a complete UUID |
240 | std::cout << " Column "; | ||
241 | std::cout << QString("Resource").leftJustified(colSize, ' ', true).toStdString(); | ||
242 | std::cout << QString("Identifier").leftJustified(colSize, ' ', true).toStdString(); | ||
178 | for (int i = 0; i < model->columnCount(QModelIndex()); i++) { | 243 | for (int i = 0; i < model->columnCount(QModelIndex()); i++) { |
179 | std::cout << "\t|" << model->headerData(i, Qt::Horizontal).toString().toStdString(); | 244 | std::cout << " | " << model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true).toStdString(); |
180 | } | 245 | } |
181 | std::cout << std::endl; | 246 | std::cout << std::endl; |
182 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) { | 247 | QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize](const QModelIndex &index, int start, int end) { |
183 | for (int i = start; i <= end; i++) { | 248 | for (int i = start; i <= end; i++) { |
184 | std::cout << "\tRow " << model->rowCount() << ":\t "; | 249 | std::cout << " Row " << model->rowCount() << ": "; |
185 | std::cout << "\t" << model->data(model->index(i, 0, index), Akonadi2::Store::DomainObjectBaseRole).value<Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr>()->identifier().toStdString() << "\t"; | 250 | auto object = model->data(model->index(i, 0, index), Akonadi2::Store::DomainObjectBaseRole).value<Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr>(); |
251 | std::cout << " " << object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true).toStdString(); | ||
252 | std::cout << object->identifier().leftJustified(colSize, ' ', true).toStdString(); | ||
186 | for (int col = 0; col < model->columnCount(QModelIndex()); col++) { | 253 | for (int col = 0; col < model->columnCount(QModelIndex()); col++) { |
187 | std::cout << "\t|" << model->data(model->index(i, col, index)).toString().toStdString(); | 254 | std::cout << " | " << model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true).toStdString(); |
188 | } | 255 | } |
189 | std::cout << std::endl; | 256 | std::cout << std::endl; |
190 | } | 257 | } |
@@ -266,25 +333,44 @@ int main(int argc, char *argv[]) | |||
266 | Akonadi2::Store::removeFromDisk(resource.toLatin1()); | 333 | Akonadi2::Store::removeFromDisk(resource.toLatin1()); |
267 | } | 334 | } |
268 | } else if (command == "create") { | 335 | } else if (command == "create") { |
269 | auto type = !args.isEmpty() ? args.takeFirst() : QByteArray(); | 336 | auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); |
270 | if (type == "resource") { | 337 | auto &store = getStore(type); |
271 | Akonadi2::ApplicationDomain::AkonadiResource resource; | 338 | auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); |
272 | resource.setProperty("identifier", args.at(0)); | 339 | auto object = store.getObject(resource); |
273 | resource.setProperty("type", args.at(1)); | 340 | auto map = consumeMap(args); |
274 | //TODO turn this into a bunch of json | 341 | for (auto i = map.begin(); i != map.end(); ++i) { |
275 | if (args.size() >= 4) { | 342 | object->setProperty(i.key().toLatin1(), i.value()); |
276 | resource.setProperty(args.at(2).toLatin1(), args.at(3)); | 343 | } |
277 | } | 344 | auto result = store.create(*object).exec(); |
278 | Akonadi2::Store::create<Akonadi2::ApplicationDomain::AkonadiResource>(resource).exec().waitForFinished(); | 345 | result.waitForFinished(); |
279 | qDebug() << "Created resource " << args; | 346 | if (result.errorCode()) { |
347 | std::cout << "An error occurred while creating the entity: " << result.errorMessage().toStdString(); | ||
348 | } | ||
349 | } else if (command == "modify") { | ||
350 | auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); | ||
351 | auto &store = getStore(type); | ||
352 | auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); | ||
353 | auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); | ||
354 | auto object = store.getObject(resource, identifier); | ||
355 | auto map = consumeMap(args); | ||
356 | for (auto i = map.begin(); i != map.end(); ++i) { | ||
357 | object->setProperty(i.key().toLatin1(), i.value()); | ||
358 | } | ||
359 | auto result = store.modify(*object).exec(); | ||
360 | result.waitForFinished(); | ||
361 | if (result.errorCode()) { | ||
362 | std::cout << "An error occurred while modifying the entity: " << result.errorMessage().toStdString(); | ||
280 | } | 363 | } |
281 | } else if (command == "remove") { | 364 | } else if (command == "remove") { |
282 | auto type = !args.isEmpty() ? args.takeFirst() : QByteArray(); | 365 | auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); |
283 | if (type == "resource") { | 366 | auto &store = getStore(type); |
284 | Akonadi2::ApplicationDomain::AkonadiResource resource; | 367 | auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); |
285 | resource.setProperty("identifier", args.at(0)); | 368 | auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray(); |
286 | Akonadi2::Store::remove<Akonadi2::ApplicationDomain::AkonadiResource>(resource).exec().waitForFinished(); | 369 | auto object = store.getObject(resource, identifier); |
287 | qDebug() << "Created resource " << args; | 370 | auto result = store.remove(*object).exec(); |
371 | result.waitForFinished(); | ||
372 | if (result.errorCode()) { | ||
373 | std::cout << "An error occurred while removing the entity: " << result.errorMessage().toStdString(); | ||
288 | } | 374 | } |
289 | } else if (command == "stat") { | 375 | } else if (command == "stat") { |
290 | auto resources = args; | 376 | auto resources = args; |