summaryrefslogtreecommitdiffstats
path: root/examples/maildirresource/maildirresource.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-07-07 22:23:49 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-07-07 22:23:49 +0200
commitda2b049e248c1ad7efeb53685158a205335e4e36 (patch)
tree1e7e5e940e9b760b2108081b1d2f3879cebdb0ff /examples/maildirresource/maildirresource.cpp
parent9bcb822963fc96c94dbe7dcc4134dcd2dac454ff (diff)
downloadsink-da2b049e248c1ad7efeb53685158a205335e4e36.tar.gz
sink-da2b049e248c1ad7efeb53685158a205335e4e36.zip
A new debug system.
Instead of a single #define as debug area the new system allows for an identifier for each debug message with the structure component.area. The component is a dot separated identifier of the runtime component, such as the process or the plugin. The area is the code component, and can be as such defined at compiletime. The idea of this system is that it becomes possible to i.e. look at the output of all messages in the query subsystem of a specific resource (something that happens in the client process, but in the resource-specific subcomponent). The new macros are supposed to be less likely to clash with other names, hence the new names.
Diffstat (limited to 'examples/maildirresource/maildirresource.cpp')
-rw-r--r--examples/maildirresource/maildirresource.cpp57
1 files changed, 28 insertions, 29 deletions
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp
index e1fcfdf..d7a6fff 100644
--- a/examples/maildirresource/maildirresource.cpp
+++ b/examples/maildirresource/maildirresource.cpp
@@ -51,8 +51,7 @@
51#define ENTITY_TYPE_MAIL "mail" 51#define ENTITY_TYPE_MAIL "mail"
52#define ENTITY_TYPE_FOLDER "folder" 52#define ENTITY_TYPE_FOLDER "folder"
53 53
54#undef DEBUG_AREA 54SINK_DEBUG_AREA("maildirresource")
55#define DEBUG_AREA "resource.maildir"
56 55
57using namespace Sink; 56using namespace Sink;
58 57
@@ -65,8 +64,8 @@ static QString getFilePathFromMimeMessagePath(const QString &mimeMessagePath)
65 QDir dir(path); 64 QDir dir(path);
66 const QFileInfoList list = dir.entryInfoList(QStringList() << (key+"*"), QDir::Files); 65 const QFileInfoList list = dir.entryInfoList(QStringList() << (key+"*"), QDir::Files);
67 if (list.size() != 1) { 66 if (list.size() != 1) {
68 Warning() << "Failed to find message " << mimeMessagePath; 67 SinkWarning() << "Failed to find message " << mimeMessagePath;
69 Warning() << "Failed to find message " << path; 68 SinkWarning() << "Failed to find message " << path;
70 return QString(); 69 return QString();
71 } 70 }
72 return list.first().filePath(); 71 return list.first().filePath();
@@ -115,7 +114,7 @@ public:
115 const auto path = getPath(folder, transaction); 114 const auto path = getPath(folder, transaction);
116 KPIM::Maildir maildir(path, false); 115 KPIM::Maildir maildir(path, false);
117 if (!maildir.isValid(true)) { 116 if (!maildir.isValid(true)) {
118 Warning() << "Maildir is not existing: " << path; 117 SinkWarning() << "Maildir is not existing: " << path;
119 } 118 }
120 auto identifier = maildir.addEntryFromPath(oldPath); 119 auto identifier = maildir.addEntryFromPath(oldPath);
121 return path + "/" + identifier; 120 return path + "/" + identifier;
@@ -124,7 +123,7 @@ public:
124 const auto path = getPath(folder, transaction); 123 const auto path = getPath(folder, transaction);
125 KPIM::Maildir maildir(path, false); 124 KPIM::Maildir maildir(path, false);
126 if (!maildir.isValid(true)) { 125 if (!maildir.isValid(true)) {
127 Warning() << "Maildir is not existing: " << path; 126 SinkWarning() << "Maildir is not existing: " << path;
128 } 127 }
129 auto oldIdentifier = KPIM::Maildir::getKeyFromFile(oldPath); 128 auto oldIdentifier = KPIM::Maildir::getKeyFromFile(oldPath);
130 auto pathParts = oldPath.split('/'); 129 auto pathParts = oldPath.split('/');
@@ -135,7 +134,7 @@ public:
135 } 134 }
136 KPIM::Maildir oldMaildir(oldDirectory, false); 135 KPIM::Maildir oldMaildir(oldDirectory, false);
137 if (!oldMaildir.isValid(false)) { 136 if (!oldMaildir.isValid(false)) {
138 Warning() << "Maildir is not existing: " << path; 137 SinkWarning() << "Maildir is not existing: " << path;
139 } 138 }
140 auto identifier = oldMaildir.moveEntryTo(oldIdentifier, maildir); 139 auto identifier = oldMaildir.moveEntryTo(oldIdentifier, maildir);
141 return path + "/" + identifier; 140 return path + "/" + identifier;
@@ -158,7 +157,7 @@ public:
158 const bool mimeMessageChanged = mimeMessage.isValid() && mimeMessage.toString() != oldEntity.getProperty("mimeMessage").toString(); 157 const bool mimeMessageChanged = mimeMessage.isValid() && mimeMessage.toString() != oldEntity.getProperty("mimeMessage").toString();
159 const bool folderChanged = newFolder.isValid() && newFolder.toString() != oldEntity.getProperty("mimeMessage").toString(); 158 const bool folderChanged = newFolder.isValid() && newFolder.toString() != oldEntity.getProperty("mimeMessage").toString();
160 if (mimeMessageChanged || folderChanged) { 159 if (mimeMessageChanged || folderChanged) {
161 Trace() << "Moving mime message: " << mimeMessageChanged << folderChanged; 160 SinkTrace() << "Moving mime message: " << mimeMessageChanged << folderChanged;
162 auto newPath = moveMessage(mimeMessage.toString(), newEntity.getProperty("folder").toByteArray(), transaction); 161 auto newPath = moveMessage(mimeMessage.toString(), newEntity.getProperty("folder").toByteArray(), transaction);
163 if (newPath != oldEntity.getProperty("mimeMessage").toString()) { 162 if (newPath != oldEntity.getProperty("mimeMessage").toString()) {
164 const auto oldPath = getFilePathFromMimeMessagePath(oldEntity.getProperty("mimeMessage").toString()); 163 const auto oldPath = getFilePathFromMimeMessagePath(oldEntity.getProperty("mimeMessage").toString());
@@ -278,7 +277,7 @@ public:
278 { 277 {
279 const QByteArray bufferType = ENTITY_TYPE_FOLDER; 278 const QByteArray bufferType = ENTITY_TYPE_FOLDER;
280 QStringList folderList = listAvailableFolders(); 279 QStringList folderList = listAvailableFolders();
281 Trace() << "Found folders " << folderList; 280 SinkTrace() << "Found folders " << folderList;
282 281
283 scanForRemovals(bufferType, 282 scanForRemovals(bufferType,
284 [this, &bufferType](const std::function<void(const QByteArray &)> &callback) { 283 [this, &bufferType](const std::function<void(const QByteArray &)> &callback) {
@@ -304,23 +303,23 @@ public:
304 303
305 void synchronizeMails(const QString &path) 304 void synchronizeMails(const QString &path)
306 { 305 {
307 Trace() << "Synchronizing mails" << path; 306 SinkTrace() << "Synchronizing mails" << path;
308 auto time = QSharedPointer<QTime>::create(); 307 auto time = QSharedPointer<QTime>::create();
309 time->start(); 308 time->start();
310 const QByteArray bufferType = ENTITY_TYPE_MAIL; 309 const QByteArray bufferType = ENTITY_TYPE_MAIL;
311 310
312 KPIM::Maildir maildir(path, true); 311 KPIM::Maildir maildir(path, true);
313 if (!maildir.isValid()) { 312 if (!maildir.isValid()) {
314 Warning() << "Failed to sync folder."; 313 SinkWarning() << "Failed to sync folder.";
315 return; 314 return;
316 } 315 }
317 316
318 Trace() << "Importing new mail."; 317 SinkTrace() << "Importing new mail.";
319 maildir.importNewMails(); 318 maildir.importNewMails();
320 319
321 auto listingPath = maildir.pathToCurrent(); 320 auto listingPath = maildir.pathToCurrent();
322 auto entryIterator = QSharedPointer<QDirIterator>::create(listingPath, QDir::Files); 321 auto entryIterator = QSharedPointer<QDirIterator>::create(listingPath, QDir::Files);
323 Trace() << "Looking into " << listingPath; 322 SinkTrace() << "Looking into " << listingPath;
324 323
325 const auto folderLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, path.toUtf8()); 324 const auto folderLocalId = syncStore().resolveRemoteId(ENTITY_TYPE_FOLDER, path.toUtf8());
326 325
@@ -332,7 +331,7 @@ public:
332 callback(sinkId); 331 callback(sinkId);
333 }, 332 },
334 [&](const Index::Error &error) { 333 [&](const Index::Error &error) {
335 Warning() << "Error in index: " << error.message << property; 334 SinkWarning() << "Error in index: " << error.message << property;
336 }); 335 });
337 }, 336 },
338 [](const QByteArray &remoteId) -> bool { 337 [](const QByteArray &remoteId) -> bool {
@@ -350,7 +349,7 @@ public:
350 const auto flags = maildir.readEntryFlags(fileName); 349 const auto flags = maildir.readEntryFlags(fileName);
351 const auto maildirKey = maildir.getKeyFromFile(fileName); 350 const auto maildirKey = maildir.getKeyFromFile(fileName);
352 351
353 Trace() << "Found a mail " << filePath << " : " << fileName; 352 SinkTrace() << "Found a mail " << filePath << " : " << fileName;
354 353
355 Sink::ApplicationDomain::Mail mail; 354 Sink::ApplicationDomain::Mail mail;
356 mail.setProperty("folder", folderLocalId); 355 mail.setProperty("folder", folderLocalId);
@@ -362,12 +361,12 @@ public:
362 createOrModify(bufferType, remoteId, mail); 361 createOrModify(bufferType, remoteId, mail);
363 } 362 }
364 const auto elapsed = time->elapsed(); 363 const auto elapsed = time->elapsed();
365 Log() << "Synchronized " << count << " mails in " << listingPath << Sink::Log::TraceTime(elapsed) << " " << elapsed/qMax(count, 1) << " [ms/mail]"; 364 SinkLog() << "Synchronized " << count << " mails in " << listingPath << Sink::Log::TraceTime(elapsed) << " " << elapsed/qMax(count, 1) << " [ms/mail]";
366 } 365 }
367 366
368 KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE 367 KAsync::Job<void> synchronizeWithSource() Q_DECL_OVERRIDE
369 { 368 {
370 Log() << " Synchronizing"; 369 SinkLog() << " Synchronizing";
371 return KAsync::start<void, KAsync::Job<void> >([this]() { 370 return KAsync::start<void, KAsync::Job<void> >([this]() {
372 KPIM::Maildir maildir(mMaildirPath, true); 371 KPIM::Maildir maildir(mMaildirPath, true);
373 if (!maildir.isValid(false)) { 372 if (!maildir.isValid(false)) {
@@ -381,7 +380,7 @@ public:
381 //Don't let the transaction grow too much 380 //Don't let the transaction grow too much
382 commit(); 381 commit();
383 } 382 }
384 Log() << "Done Synchronizing"; 383 SinkLog() << "Done Synchronizing";
385 return KAsync::null<void>(); 384 return KAsync::null<void>();
386 }); 385 });
387 } 386 }
@@ -402,15 +401,15 @@ public:
402 { 401 {
403 if (operation == Sink::Operation_Creation) { 402 if (operation == Sink::Operation_Creation) {
404 const auto remoteId = getFilePathFromMimeMessagePath(mail.getMimeMessagePath()); 403 const auto remoteId = getFilePathFromMimeMessagePath(mail.getMimeMessagePath());
405 Trace() << "Mail created: " << remoteId; 404 SinkTrace() << "Mail created: " << remoteId;
406 return KAsync::start<QByteArray>([=]() -> QByteArray { 405 return KAsync::start<QByteArray>([=]() -> QByteArray {
407 return remoteId.toUtf8(); 406 return remoteId.toUtf8();
408 }); 407 });
409 } else if (operation == Sink::Operation_Removal) { 408 } else if (operation == Sink::Operation_Removal) {
410 Trace() << "Removing a mail: " << oldRemoteId; 409 SinkTrace() << "Removing a mail: " << oldRemoteId;
411 return KAsync::null<QByteArray>(); 410 return KAsync::null<QByteArray>();
412 } else if (operation == Sink::Operation_Modification) { 411 } else if (operation == Sink::Operation_Modification) {
413 Trace() << "Modifying a mail: " << oldRemoteId; 412 SinkTrace() << "Modifying a mail: " << oldRemoteId;
414 const auto remoteId = getFilePathFromMimeMessagePath(mail.getMimeMessagePath()); 413 const auto remoteId = getFilePathFromMimeMessagePath(mail.getMimeMessagePath());
415 return KAsync::start<QByteArray>([=]() -> QByteArray { 414 return KAsync::start<QByteArray>([=]() -> QByteArray {
416 return remoteId.toUtf8(); 415 return remoteId.toUtf8();
@@ -425,7 +424,7 @@ public:
425 auto folderName = folder.getName(); 424 auto folderName = folder.getName();
426 //FIXME handle non toplevel folders 425 //FIXME handle non toplevel folders
427 auto path = mMaildirPath + "/" + folderName; 426 auto path = mMaildirPath + "/" + folderName;
428 Trace() << "Creating a new folder: " << path; 427 SinkTrace() << "Creating a new folder: " << path;
429 KPIM::Maildir maildir(path, false); 428 KPIM::Maildir maildir(path, false);
430 maildir.create(); 429 maildir.create();
431 return KAsync::start<QByteArray>([=]() -> QByteArray { 430 return KAsync::start<QByteArray>([=]() -> QByteArray {
@@ -433,12 +432,12 @@ public:
433 }); 432 });
434 } else if (operation == Sink::Operation_Removal) { 433 } else if (operation == Sink::Operation_Removal) {
435 const auto path = oldRemoteId; 434 const auto path = oldRemoteId;
436 Trace() << "Removing a folder: " << path; 435 SinkTrace() << "Removing a folder: " << path;
437 KPIM::Maildir maildir(path, false); 436 KPIM::Maildir maildir(path, false);
438 maildir.remove(); 437 maildir.remove();
439 return KAsync::null<QByteArray>(); 438 return KAsync::null<QByteArray>();
440 } else if (operation == Sink::Operation_Modification) { 439 } else if (operation == Sink::Operation_Modification) {
441 Warning() << "Folder modifications are not implemented"; 440 SinkWarning() << "Folder modifications are not implemented";
442 return KAsync::start<QByteArray>([=]() -> QByteArray { 441 return KAsync::start<QByteArray>([=]() -> QByteArray {
443 return oldRemoteId; 442 return oldRemoteId;
444 }); 443 });
@@ -472,7 +471,7 @@ MaildirResource::MaildirResource(const QByteArray &instanceIdentifier, const QSh
472 setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new FolderPreprocessor(mMaildirPath) << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); 471 setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new FolderPreprocessor(mMaildirPath) << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>);
473 472
474 KPIM::Maildir dir(mMaildirPath, true); 473 KPIM::Maildir dir(mMaildirPath, true);
475 Trace() << "Started maildir resource for maildir: " << mMaildirPath; 474 SinkTrace() << "Started maildir resource for maildir: " << mMaildirPath;
476 { 475 {
477 auto draftsFolder = dir.addSubFolder("Drafts"); 476 auto draftsFolder = dir.addSubFolder("Drafts");
478 auto remoteId = synchronizer->createFolder(draftsFolder, "folder", QByteArrayList() << "drafts"); 477 auto remoteId = synchronizer->createFolder(draftsFolder, "folder", QByteArrayList() << "drafts");
@@ -503,7 +502,7 @@ KAsync::Job<void> MaildirResource::inspect(int inspectionType, const QByteArray
503 auto entityStore = QSharedPointer<EntityStore>::create(mResourceType, mResourceInstanceIdentifier, transaction); 502 auto entityStore = QSharedPointer<EntityStore>::create(mResourceType, mResourceInstanceIdentifier, transaction);
504 auto syncStore = QSharedPointer<RemoteIdMap>::create(synchronizationTransaction); 503 auto syncStore = QSharedPointer<RemoteIdMap>::create(synchronizationTransaction);
505 504
506 Trace() << "Inspecting " << inspectionType << domainType << entityId << property << expectedValue; 505 SinkTrace() << "Inspecting " << inspectionType << domainType << entityId << property << expectedValue;
507 506
508 if (domainType == ENTITY_TYPE_MAIL) { 507 if (domainType == ENTITY_TYPE_MAIL) {
509 auto mail = entityStore->read<Sink::ApplicationDomain::Mail>(entityId); 508 auto mail = entityStore->read<Sink::ApplicationDomain::Mail>(entityId);
@@ -542,7 +541,7 @@ KAsync::Job<void> MaildirResource::inspect(int inspectionType, const QByteArray
542 auto folder = entityStore->read<Sink::ApplicationDomain::Folder>(entityId); 541 auto folder = entityStore->read<Sink::ApplicationDomain::Folder>(entityId);
543 542
544 if (inspectionType == Sink::ResourceControl::Inspection::CacheIntegrityInspectionType) { 543 if (inspectionType == Sink::ResourceControl::Inspection::CacheIntegrityInspectionType) {
545 Trace() << "Inspecting cache integrity" << remoteId; 544 SinkTrace() << "Inspecting cache integrity" << remoteId;
546 if (!QDir(remoteId).exists()) { 545 if (!QDir(remoteId).exists()) {
547 return KAsync::error<void>(1, "The directory is not existing: " + remoteId); 546 return KAsync::error<void>(1, "The directory is not existing: " + remoteId);
548 } 547 }
@@ -553,14 +552,14 @@ KAsync::Job<void> MaildirResource::inspect(int inspectionType, const QByteArray
553 expectedCount++; 552 expectedCount++;
554 }, 553 },
555 [&](const Index::Error &error) { 554 [&](const Index::Error &error) {
556 Warning() << "Error in index: " << error.message << property; 555 SinkWarning() << "Error in index: " << error.message << property;
557 }); 556 });
558 557
559 QDir dir(remoteId + "/cur"); 558 QDir dir(remoteId + "/cur");
560 const QFileInfoList list = dir.entryInfoList(QDir::Files); 559 const QFileInfoList list = dir.entryInfoList(QDir::Files);
561 if (list.size() != expectedCount) { 560 if (list.size() != expectedCount) {
562 for (const auto &fileInfo : list) { 561 for (const auto &fileInfo : list) {
563 Warning() << "Found in cache: " << fileInfo.fileName(); 562 SinkWarning() << "Found in cache: " << fileInfo.fileName();
564 } 563 }
565 return KAsync::error<void>(1, QString("Wrong number of files; found %1 instead of %2.").arg(list.size()).arg(expectedCount)); 564 return KAsync::error<void>(1, QString("Wrong number of files; found %1 instead of %2.").arg(list.size()).arg(expectedCount));
566 } 565 }