summaryrefslogtreecommitdiffstats
path: root/examples/client
diff options
context:
space:
mode:
Diffstat (limited to 'examples/client')
-rw-r--r--examples/client/main.cpp248
1 files changed, 25 insertions, 223 deletions
diff --git a/examples/client/main.cpp b/examples/client/main.cpp
index ad65e44..127fd2f 100644
--- a/examples/client/main.cpp
+++ b/examples/client/main.cpp
@@ -20,19 +20,10 @@
20#include <QApplication> 20#include <QApplication>
21#include <QCommandLineParser> 21#include <QCommandLineParser>
22#include <QCommandLineOption> 22#include <QCommandLineOption>
23#include <QElapsedTimer> 23#include <QTime>
24#include <QDir>
25 24
26#include "common/clientapi.h" 25#include "common/clientapi.h"
27#include "common/resource.h"
28#include "common/storage.h"
29#include "common/domain/event.h"
30#include "common/domain/folder.h"
31#include "common/resourceconfig.h"
32#include "common/log.h" 26#include "common/log.h"
33#include "common/storage.h"
34#include "common/definitions.h"
35#include "console.h"
36 27
37#include <QWidget> 28#include <QWidget>
38#include <QTreeView> 29#include <QTreeView>
@@ -152,26 +143,6 @@ public:
152 143
153}; 144};
154 145
155class MyApplication : public QApplication
156{
157 QElapsedTimer t;
158public:
159 MyApplication(int& argc, char ** argv) : QApplication(argc, argv) { }
160 virtual ~MyApplication() { }
161
162 virtual bool notify(QObject* receiver, QEvent* event)
163 {
164 t.start();
165 bool ret = QApplication::notify(receiver, event);
166 if(t.elapsed() > 3)
167 qDebug("processing event type %d for object %s took %dms",
168 (int)event->type(), receiver->objectName().toLocal8Bit().data(),
169 (int)t.elapsed());
170 return ret;
171 }
172};
173
174
175static QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query) 146static QSharedPointer<QAbstractItemModel> loadModel(const QString &type, Akonadi2::Query query)
176{ 147{
177 QTime time; 148 QTime time;
@@ -204,11 +175,9 @@ QMap<QString, QString> consumeMap(QList<QString> &list)
204 175
205int main(int argc, char *argv[]) 176int main(int argc, char *argv[])
206{ 177{
207 MyApplication app(argc, argv); 178 QApplication app(argc, argv);
208 179
209 QCommandLineParser cliOptions; 180 QCommandLineParser cliOptions;
210 cliOptions.addPositionalArgument(QObject::tr("[command]"),
211 QObject::tr("A command"));
212 cliOptions.addPositionalArgument(QObject::tr("[type]"), 181 cliOptions.addPositionalArgument(QObject::tr("[type]"),
213 QObject::tr("A type to work with")); 182 QObject::tr("A type to work with"));
214 cliOptions.addPositionalArgument(QObject::tr("[resource]"), 183 cliOptions.addPositionalArgument(QObject::tr("[resource]"),
@@ -217,206 +186,39 @@ int main(int argc, char *argv[])
217 cliOptions.addHelpOption(); 186 cliOptions.addHelpOption();
218 cliOptions.process(app); 187 cliOptions.process(app);
219 QStringList args = cliOptions.positionalArguments(); 188 QStringList args = cliOptions.positionalArguments();
220 auto command = args.takeFirst();
221 189
222 if (cliOptions.isSet("debuglevel")) { 190 if (cliOptions.isSet("debuglevel")) {
223 Akonadi2::Log::setDebugOutputLevel(static_cast<Akonadi2::Log::DebugLevel>(cliOptions.value("debuglevel").toInt())); 191 Akonadi2::Log::setDebugOutputLevel(static_cast<Akonadi2::Log::DebugLevel>(cliOptions.value("debuglevel").toInt()));
224 } 192 }
225 193
226 if (command == "list") { 194 auto type = !args.isEmpty() ? args.takeFirst() : QByteArray();
227 auto type = !args.isEmpty() ? args.takeFirst() : QByteArray(); 195 auto resources = args;
228 auto resources = args;
229
230 Akonadi2::Query query;
231 for (const auto &res : resources) {
232 query.resources << res.toLatin1();
233 }
234 query.syncOnDemand = false;
235 query.processAll = false;
236 query.liveQuery = false;
237 196
238 auto model = loadModel(type, query); 197 Akonadi2::Query query;
239 qDebug() << "Listing"; 198 for (const auto &res : resources) {
240 int colSize = 38; //Necessary to display a complete UUID 199 query.resources << res.toLatin1();
241 std::cout << " Column "; 200 }
242 std::cout << QString("Resource").leftJustified(colSize, ' ', true).toStdString(); 201 query.syncOnDemand = false;
243 std::cout << QString("Identifier").leftJustified(colSize, ' ', true).toStdString(); 202 query.processAll = false;
244 for (int i = 0; i < model->columnCount(QModelIndex()); i++) { 203 query.liveQuery = true;
245 std::cout << " | " << model->headerData(i, Qt::Horizontal).toString().leftJustified(colSize, ' ', true).toStdString(); 204 if (type == "folder") {
246 } 205 query.parentProperty = "parent";
247 std::cout << std::endl; 206 }
248 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model, colSize](const QModelIndex &index, int start, int end) { 207 auto model = loadModel(type, query);
208 if (type == "folder") {
209 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) {
249 for (int i = start; i <= end; i++) { 210 for (int i = start; i <= end; i++) {
250 std::cout << " Row " << QString::number(model->rowCount()).rightJustified(4, ' ').toStdString() << ": "; 211 model->fetchMore(model->index(i, 0, index));
251 auto object = model->data(model->index(i, 0, index), Akonadi2::Store::DomainObjectBaseRole).value<Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr>();
252 std::cout << " " << object->resourceInstanceIdentifier().leftJustified(colSize, ' ', true).toStdString();
253 std::cout << object->identifier().leftJustified(colSize, ' ', true).toStdString();
254 for (int col = 0; col < model->columnCount(QModelIndex()); col++) {
255 std::cout << " | " << model->data(model->index(i, col, index)).toString().leftJustified(colSize, ' ', true).toStdString();
256 }
257 std::cout << std::endl;
258 } 212 }
259 }); 213 });
260 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, &app](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) { 214 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Folder> >::create(model.data());
261 if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) { 215 app.exec();
262 app.quit(); 216 } else if (type == "mail") {
263 } 217 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Mail> >::create(model.data());
264 }); 218 app.exec();
265 if (!model->data(QModelIndex(), Akonadi2::Store::ChildrenFetchedRole).toBool()) { 219 } else if (type == "event") {
266 return app.exec(); 220 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Event> >::create(model.data());
267 }
268 } else if (command == "count") {
269 auto type = !args.isEmpty() ? args.takeFirst() : QByteArray();
270 auto resources = args;
271
272 Akonadi2::Query query;
273 for (const auto &res : resources) {
274 query.resources << res.toLatin1();
275 }
276 query.syncOnDemand = false;
277 query.processAll = false;
278 query.liveQuery = false;
279 auto model = loadModel(type, query);
280 QObject::connect(model.data(), &QAbstractItemModel::dataChanged, [model, &app](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
281 if (roles.contains(Akonadi2::Store::ChildrenFetchedRole)) {
282 std::cout << "\tCounted results " << model->rowCount(QModelIndex()) << std::endl;
283 app.quit();
284 }
285 });
286 return app.exec();
287 } else if (command == "synchronize") {
288 auto resources = args;
289 Akonadi2::Query query;
290 for (const auto &res : resources) {
291 query.resources << res.toLatin1();
292 }
293 query.syncOnDemand = true;
294 query.processAll = true;
295 Akonadi2::Store::synchronize(query).then<void>([&app]() {
296 app.quit();
297 }).exec();
298 app.exec(); 221 app.exec();
299 } else if (command == "show") {
300 auto type = !args.isEmpty() ? args.takeFirst() : QByteArray();
301 auto resources = args;
302
303 Akonadi2::Query query;
304 for (const auto &res : resources) {
305 query.resources << res.toLatin1();
306 }
307 query.syncOnDemand = false;
308 query.processAll = false;
309 query.liveQuery = true;
310 if (type == "folder") {
311 query.parentProperty = "parent";
312 }
313 auto model = loadModel(type, query);
314 if (type == "folder") {
315 QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, [model](const QModelIndex &index, int start, int end) {
316 for (int i = start; i <= end; i++) {
317 model->fetchMore(model->index(i, 0, index));
318 }
319 });
320 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Folder> >::create(model.data());
321 app.exec();
322 } else if (type == "mail") {
323 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Mail> >::create(model.data());
324 app.exec();
325 } else if (type == "event") {
326 auto view = QSharedPointer<View<Akonadi2::ApplicationDomain::Event> >::create(model.data());
327 app.exec();
328 }
329 } else if (command == "clear") {
330 auto resources = args;
331
332 qDebug() << "Clearing";
333 for (const auto &resource : resources) {
334 Akonadi2::Store::removeFromDisk(resource.toLatin1());
335 }
336 } else if (command == "create") {
337 auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
338 auto &store = getStore(type);
339 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object;
340 if (type == "resource") {
341 auto resourceType = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
342 object = store.getObject("");
343 object->setProperty("type", resourceType);
344 } else {
345 auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
346 object = store.getObject(resource);
347 }
348 auto map = consumeMap(args);
349 for (auto i = map.begin(); i != map.end(); ++i) {
350 object->setProperty(i.key().toLatin1(), i.value());
351 }
352 auto result = store.create(*object).exec();
353 result.waitForFinished();
354 if (result.errorCode()) {
355 std::cout << "An error occurred while creating the entity: " << result.errorMessage().toStdString();
356 }
357 } else if (command == "modify") {
358 auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
359 auto &store = getStore(type);
360 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object;
361 if (type == "resource") {
362 auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
363 object = store.getObject("", identifier);
364 } else {
365 auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
366 auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
367 object = store.getObject(resource, identifier);
368 }
369 auto map = consumeMap(args);
370 for (auto i = map.begin(); i != map.end(); ++i) {
371 object->setProperty(i.key().toLatin1(), i.value());
372 }
373 auto result = store.modify(*object).exec();
374 result.waitForFinished();
375 if (result.errorCode()) {
376 std::cout << "An error occurred while modifying the entity: " << result.errorMessage().toStdString();
377 }
378 } else if (command == "remove") {
379 auto type = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
380 auto &store = getStore(type);
381 Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr object;
382 if (type == "resource") {
383 auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
384 object = store.getObject("", identifier);
385 } else {
386 auto resource = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
387 auto identifier = !args.isEmpty() ? args.takeFirst().toLatin1() : QByteArray();
388 object = store.getObject(resource, identifier);
389 }
390 auto result = store.remove(*object).exec();
391 result.waitForFinished();
392 if (result.errorCode()) {
393 std::cout << "An error occurred while removing the entity: " << result.errorMessage().toStdString();
394 }
395 } else if (command == "stat") {
396 auto resources = args;
397 for (const auto &resource : resources) {
398 Akonadi2::Storage storage(Akonadi2::storageLocation(), resource, Akonadi2::Storage::ReadOnly);
399 auto transaction = storage.createTransaction(Akonadi2::Storage::ReadOnly);
400
401 QList<QByteArray> databases = transaction.getDatabaseNames();
402 qint64 total = 0;
403 for (const auto &databaseName : databases) {
404 std::cout << "Database: " << databaseName.toStdString() << std::endl;
405 auto db = transaction.openDatabase(databaseName);
406 auto size = db.getSize();
407 std::cout << "\tSize [kb]: " << size / 1024 << std::endl;
408 total += size;
409 }
410 std::cout << "Total [kb]: " << total / 1024 << std::endl;
411 int diskUsage = 0;
412 QDir dir(Akonadi2::storageLocation());
413 for (const auto &folder : dir.entryList(QStringList() << resource + "*")) {
414 diskUsage += Akonadi2::Storage(Akonadi2::storageLocation(), folder, Akonadi2::Storage::ReadOnly).diskUsage();
415 }
416 std::cout << "Disk usage [kb]: " << diskUsage / 1024 << std::endl;
417 }
418 } else {
419 qWarning() << "Unknown command " << command;
420 } 222 }
421 return 0; 223 return 0;
422} 224}