summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-06-12 02:11:02 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-06-12 02:11:02 +0200
commit3fa1766af3fa85067d4b2d48c8410e6c201ae323 (patch)
tree5662b068088472ad7133d2b91bb0e0ffe1d9b10c
parentce56e7e0c973d31a900c9c467f639a344ea71bf1 (diff)
downloadsink-3fa1766af3fa85067d4b2d48c8410e6c201ae323.tar.gz
sink-3fa1766af3fa85067d4b2d48c8410e6c201ae323.zip
Moving of mails between folders
-rw-r--r--examples/imapresource/imapresource.cpp9
-rw-r--r--examples/imapresource/imapserverproxy.cpp16
-rw-r--r--examples/imapresource/imapserverproxy.h2
-rw-r--r--tests/mailtest.cpp55
-rw-r--r--tests/mailtest.h1
5 files changed, 80 insertions, 3 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp
index aca1c01..8cbb9fa 100644
--- a/examples/imapresource/imapresource.cpp
+++ b/examples/imapresource/imapresource.cpp
@@ -491,8 +491,11 @@ public:
491 flags << Imap::Flags::Flagged; 491 flags << Imap::Flags::Flagged;
492 } 492 }
493 493
494 const bool messageChanged = changedProperties.contains(Sink::ApplicationDomain::Mail::MimeMessage::name); 494 const bool messageMoved = changedProperties.contains(ApplicationDomain::Mail::Folder::name);
495 if (messageChanged) { 495 const bool messageChanged = changedProperties.contains(ApplicationDomain::Mail::MimeMessage::name);
496 if (messageChanged || messageMoved) {
497 const auto folderId = folderIdFromMailRid(oldRemoteId);
498 const QString oldMailbox = syncStore().resolveLocalId(ENTITY_TYPE_FOLDER, folderId);
496 QByteArray content = KMime::LFtoCRLF(mail.getMimeMessage()); 499 QByteArray content = KMime::LFtoCRLF(mail.getMimeMessage());
497 QDateTime internalDate = mail.getDate(); 500 QDateTime internalDate = mail.getDate();
498 auto rid = QSharedPointer<QByteArray>::create(); 501 auto rid = QSharedPointer<QByteArray>::create();
@@ -504,7 +507,7 @@ public:
504 Trace() << "Finished creating a modified mail: " << remoteId; 507 Trace() << "Finished creating a modified mail: " << remoteId;
505 *rid = remoteId; 508 *rid = remoteId;
506 }) 509 })
507 .then(imap->remove(mailbox, set)) 510 .then(imap->remove(oldMailbox, set))
508 .then<QByteArray>([rid, imap]() { 511 .then<QByteArray>([rid, imap]() {
509 return *rid; 512 return *rid;
510 }); 513 });
diff --git a/examples/imapresource/imapserverproxy.cpp b/examples/imapresource/imapserverproxy.cpp
index db68a53..49711a9 100644
--- a/examples/imapresource/imapserverproxy.cpp
+++ b/examples/imapresource/imapserverproxy.cpp
@@ -25,6 +25,7 @@
25#include <KIMAP/KIMAP/SelectJob> 25#include <KIMAP/KIMAP/SelectJob>
26#include <KIMAP/KIMAP/AppendJob> 26#include <KIMAP/KIMAP/AppendJob>
27#include <KIMAP/KIMAP/CreateJob> 27#include <KIMAP/KIMAP/CreateJob>
28#include <KIMAP/KIMAP/CopyJob>
28#include <KIMAP/KIMAP/RenameJob> 29#include <KIMAP/KIMAP/RenameJob>
29#include <KIMAP/KIMAP/DeleteJob> 30#include <KIMAP/KIMAP/DeleteJob>
30#include <KIMAP/KIMAP/StoreJob> 31#include <KIMAP/KIMAP/StoreJob>
@@ -219,6 +220,15 @@ KAsync::Job<void> ImapServerProxy::expunge(const KIMAP::ImapSet &set)
219 return runJob(job); 220 return runJob(job);
220} 221}
221 222
223KAsync::Job<void> ImapServerProxy::copy(const KIMAP::ImapSet &set, const QString &newMailbox)
224{
225 auto copy = new KIMAP::CopyJob(mSession);
226 copy->setSequenceSet(set);
227 copy->setUidBased(true);
228 copy->setMailBox(newMailbox);
229 return runJob(copy);
230}
231
222KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, FetchCallback callback) 232KAsync::Job<void> ImapServerProxy::fetch(const KIMAP::ImapSet &set, KIMAP::FetchJob::FetchScope scope, FetchCallback callback)
223{ 233{
224 auto fetch = new KIMAP::FetchJob(mSession); 234 auto fetch = new KIMAP::FetchJob(mSession);
@@ -304,6 +314,12 @@ KAsync::Job<void> ImapServerProxy::remove(const QString &mailbox, const QByteArr
304 return remove(mailbox, set); 314 return remove(mailbox, set);
305} 315}
306 316
317
318KAsync::Job<void> ImapServerProxy::move(const QString &mailbox, const KIMAP::ImapSet &set, const QString &newMailbox)
319{
320 return select(mailbox).then<void>(copy(set, newMailbox)).then<void>(store(set, QByteArrayList() << Flags::Deleted)).then<void>(expunge(set));
321}
322
307KAsync::Job<QString> ImapServerProxy::createSubfolder(const QString &parentMailbox, const QString &folderName) 323KAsync::Job<QString> ImapServerProxy::createSubfolder(const QString &parentMailbox, const QString &folderName)
308{ 324{
309 auto folder = QSharedPointer<QString>::create(); 325 auto folder = QSharedPointer<QString>::create();
diff --git a/examples/imapresource/imapserverproxy.h b/examples/imapresource/imapserverproxy.h
index 67ff000..95ed704 100644
--- a/examples/imapresource/imapserverproxy.h
+++ b/examples/imapresource/imapserverproxy.h
@@ -93,6 +93,7 @@ public:
93 KAsync::Job<void> remove(const QString &mailbox); 93 KAsync::Job<void> remove(const QString &mailbox);
94 KAsync::Job<void> expunge(); 94 KAsync::Job<void> expunge();
95 KAsync::Job<void> expunge(const KIMAP::ImapSet &set); 95 KAsync::Job<void> expunge(const KIMAP::ImapSet &set);
96 KAsync::Job<void> copy(const KIMAP::ImapSet &set, const QString &newMailbox);
96 97
97 typedef std::function<void(const QString &, 98 typedef std::function<void(const QString &,
98 const QMap<qint64,qint64> &, 99 const QMap<qint64,qint64> &,
@@ -109,6 +110,7 @@ public:
109 KAsync::Job<QList<qint64>> fetchHeaders(const QString &mailbox); 110 KAsync::Job<QList<qint64>> fetchHeaders(const QString &mailbox);
110 KAsync::Job<void> remove(const QString &mailbox, const KIMAP::ImapSet &set); 111 KAsync::Job<void> remove(const QString &mailbox, const KIMAP::ImapSet &set);
111 KAsync::Job<void> remove(const QString &mailbox, const QByteArray &imapSet); 112 KAsync::Job<void> remove(const QString &mailbox, const QByteArray &imapSet);
113 KAsync::Job<void> move(const QString &mailbox, const KIMAP::ImapSet &set, const QString &newMailbox);
112 KAsync::Job<QString> createSubfolder(const QString &parentMailbox, const QString &folderName); 114 KAsync::Job<QString> createSubfolder(const QString &parentMailbox, const QString &folderName);
113 KAsync::Job<QString> renameSubfolder(const QString &mailbox, const QString &newName); 115 KAsync::Job<QString> renameSubfolder(const QString &mailbox, const QString &newName);
114 116
diff --git a/tests/mailtest.cpp b/tests/mailtest.cpp
index 70d60bb..2fcad93 100644
--- a/tests/mailtest.cpp
+++ b/tests/mailtest.cpp
@@ -220,6 +220,61 @@ void MailTest::testCreateModifyDeleteMail()
220 VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Folder>(ResourceControl::Inspection::CacheIntegrityInspection(folder))); 220 VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Folder>(ResourceControl::Inspection::CacheIntegrityInspection(folder)));
221} 221}
222 222
223void MailTest::testMoveMail()
224{
225 const auto subject = QString::fromLatin1("Foobar");
226
227 auto folder = Folder::create(mResourceInstanceIdentifier);
228 folder.setName("folder2");
229 VERIFYEXEC(Store::create(folder));
230
231 auto folder1 = Folder::create(mResourceInstanceIdentifier);
232 folder1.setName("folder3");
233 VERIFYEXEC(Store::create(folder1));
234
235 auto message = KMime::Message::Ptr::create();
236 message->subject(true)->fromUnicodeString(subject, "utf8");
237 message->assemble();
238
239 auto mail = Mail::create(mResourceInstanceIdentifier);
240 mail.setMimeMessage(message->encodedContent());
241 mail.setFolder(folder);
242
243 VERIFYEXEC(Store::create(mail));
244 VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
245 {
246 auto job = Store::fetchAll<Mail>(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name << Mail::MimeMessage::name))
247 .then<void, QList<Mail::Ptr>>([=](const QList<Mail::Ptr> &mails) {
248 QCOMPARE(mails.size(), 1);
249 auto mail = *mails.first();
250 QCOMPARE(mail.getFolder(), folder.identifier());
251 Warning() << "path: " << mail.getMimeMessagePath();
252 QVERIFY(QFile(mail.getMimeMessagePath()).exists());
253 });
254 VERIFYEXEC(job);
255 }
256
257 VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Folder>(ResourceControl::Inspection::CacheIntegrityInspection(folder)));
258
259 mail.setFolder(folder1);
260
261 VERIFYEXEC(Store::modify(mail));
262 VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
263 {
264 auto job = Store::fetchAll<Mail>(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name << Mail::MimeMessage::name))
265 .then<void, QList<Mail::Ptr>>([=](const QList<Mail::Ptr> &mails) {
266 QCOMPARE(mails.size(), 1);
267 auto mail = *mails.first();
268 QCOMPARE(mail.getFolder(), folder1.identifier());
269 QVERIFY(QFile(mail.getMimeMessagePath()).exists());
270 Trace() << "Mime message path: " << mail.getMimeMessagePath();
271 });
272 VERIFYEXEC(job);
273 }
274 VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Folder>(ResourceControl::Inspection::CacheIntegrityInspection(folder)));
275 VERIFYEXEC(ResourceControl::inspect<ApplicationDomain::Folder>(ResourceControl::Inspection::CacheIntegrityInspection(folder1)));
276}
277
223void MailTest::testMarkMailAsRead() 278void MailTest::testMarkMailAsRead()
224{ 279{
225 auto folder = Folder::create(mResourceInstanceIdentifier); 280 auto folder = Folder::create(mResourceInstanceIdentifier);
diff --git a/tests/mailtest.h b/tests/mailtest.h
index 43d4f75..3ba7f63 100644
--- a/tests/mailtest.h
+++ b/tests/mailtest.h
@@ -64,6 +64,7 @@ private slots:
64 64
65 void testCreateModifyDeleteFolder(); 65 void testCreateModifyDeleteFolder();
66 void testCreateModifyDeleteMail(); 66 void testCreateModifyDeleteMail();
67 void testMoveMail();
67 void testMarkMailAsRead(); 68 void testMarkMailAsRead();
68 void testCreateDraft(); 69 void testCreateDraft();
69}; 70};