From a6c3e00395d40b1daeccd6ca8335e6419c147ee0 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 3 Jan 2017 22:38:09 +0100 Subject: Move the mail between folders and update specialpurpose accordingly. --- common/specialpurposepreprocessor.cpp | 49 +++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'common/specialpurposepreprocessor.cpp') diff --git a/common/specialpurposepreprocessor.cpp b/common/specialpurposepreprocessor.cpp index e73e4ce..be5fa50 100644 --- a/common/specialpurposepreprocessor.cpp +++ b/common/specialpurposepreprocessor.cpp @@ -48,7 +48,7 @@ QByteArray getSpecialPurposeType(const QString &name) SpecialPurposeProcessor::SpecialPurposeProcessor(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) : mResourceType(resourceType), mResourceInstanceIdentifier(resourceInstanceIdentifier) {} -QByteArray SpecialPurposeProcessor::ensureFolder(const QByteArray &specialPurpose) +QByteArray SpecialPurposeProcessor::findFolder(const QByteArray &specialPurpose, bool createIfMissing) { if (!mSpecialPurposeFolders.contains(specialPurpose)) { //Try to find an existing drafts folder @@ -58,7 +58,7 @@ QByteArray SpecialPurposeProcessor::ensureFolder(const QByteArray &specialPurpos mSpecialPurposeFolders.insert(specialPurpose, r.entity.identifier()); }); - if (!mSpecialPurposeFolders.contains(specialPurpose)) { + if (!mSpecialPurposeFolders.contains(specialPurpose) && createIfMissing) { SinkTrace() << "Failed to find a " << specialPurpose << " folder, creating a new one"; auto folder = ApplicationDomain::Folder::create(mResourceInstanceIdentifier); folder.setSpecialPurpose(QByteArrayList() << specialPurpose); @@ -72,23 +72,33 @@ QByteArray SpecialPurposeProcessor::ensureFolder(const QByteArray &specialPurpos return mSpecialPurposeFolders.value(specialPurpose); } +bool SpecialPurposeProcessor::isSpecialPurposeFolder(const QByteArray &folder) const +{ + return mSpecialPurposeFolders.values().contains(folder); +} + void SpecialPurposeProcessor::moveToFolder(Sink::ApplicationDomain::ApplicationDomainType &newEntity) { + //If we remove the draft folder move back to inbox + //If we remove the trash property, move back to other specialpurpose folder or inbox + //If a folder is set explicitly, clear specialpurpose flags. using namespace Sink::ApplicationDomain; auto mail = newEntity.cast(); if (mail.getTrash()) { - auto f = ensureFolder(ApplicationDomain::SpecialPurpose::Mail::trash); + auto f = findFolder(ApplicationDomain::SpecialPurpose::Mail::trash, true); SinkTrace() << "Setting trash folder: " << f; mail.setFolder(f); - return; - } - if (mail.getDraft()) { - mail.setFolder(ensureFolder(ApplicationDomain::SpecialPurpose::Mail::drafts)); - return; - } - if (mail.getSent()) { - mail.setFolder(ensureFolder(ApplicationDomain::SpecialPurpose::Mail::sent)); - return; + } else if (mail.getDraft()) { + SinkTrace() << "Setting drafts folder: "; + mail.setFolder(findFolder(ApplicationDomain::SpecialPurpose::Mail::drafts, true)); + } else if (mail.getSent()) { + SinkTrace() << "Setting sent folder: "; + mail.setFolder(findFolder(ApplicationDomain::SpecialPurpose::Mail::sent, true)); + } else { + //No longer a specialpurpose mail, so move to inbox + if (isSpecialPurposeFolder(mail.getFolder()) || mail.getFolder().isEmpty()) { + mail.setFolder(findFolder(ApplicationDomain::SpecialPurpose::Mail::inbox, true)); + } } } @@ -99,5 +109,18 @@ void SpecialPurposeProcessor::newEntity(Sink::ApplicationDomain::ApplicationDoma void SpecialPurposeProcessor::modifiedEntity(const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, Sink::ApplicationDomain::ApplicationDomainType &newEntity) { - moveToFolder(newEntity); + using namespace Sink::ApplicationDomain; + auto mail = newEntity.cast(); + //If we moved the mail to a non-specialpurpose folder explicitly, also clear the flags. + if (mail.changedProperties().contains(Mail::Folder::name)) { + auto folder = mail.getFolder(); + bool isDraft = findFolder(ApplicationDomain::SpecialPurpose::Mail::drafts) == folder; + bool isSent = findFolder(ApplicationDomain::SpecialPurpose::Mail::sent) == folder; + bool isTrash = findFolder(ApplicationDomain::SpecialPurpose::Mail::trash) == folder; + mail.setDraft(isDraft); + mail.setTrash(isSent); + mail.setSent(isTrash); + } else { + moveToFolder(newEntity); + } } -- cgit v1.2.3