diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-05 10:37:28 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-05 10:37:28 +0200 |
commit | ca354ec4e2b9bc35b7d97c41bec68cccf6a6c8c9 (patch) | |
tree | 416c7c3462cd0df4d8681fe0414cf543ef4c094e | |
parent | c03743546aa97d697d99eaac854516b1aa1f072b (diff) | |
download | sink-ca354ec4e2b9bc35b7d97c41bec68cccf6a6c8c9.tar.gz sink-ca354ec4e2b9bc35b7d97c41bec68cccf6a6c8c9.zip |
Create actual messages in maildir
-rw-r--r-- | examples/maildirresource/libmaildir/maildir.cpp | 34 | ||||
-rw-r--r-- | examples/maildirresource/libmaildir/maildir.h | 1 | ||||
-rw-r--r-- | examples/maildirresource/maildirresource.cpp | 2 | ||||
-rw-r--r-- | tests/maildirresourcetest.cpp | 14 |
4 files changed, 48 insertions, 3 deletions
diff --git a/examples/maildirresource/libmaildir/maildir.cpp b/examples/maildirresource/libmaildir/maildir.cpp index 8d6de3e..670d4ca 100644 --- a/examples/maildirresource/libmaildir/maildir.cpp +++ b/examples/maildirresource/libmaildir/maildir.cpp | |||
@@ -706,6 +706,40 @@ QString Maildir::addEntry(const QByteArray& data) | |||
706 | return uniqueKey; | 706 | return uniqueKey; |
707 | } | 707 | } |
708 | 708 | ||
709 | QString Maildir::addEntryFromPath(const QString& path) | ||
710 | { | ||
711 | QString uniqueKey; | ||
712 | QString key; | ||
713 | QString finalKey; | ||
714 | QString curKey; | ||
715 | |||
716 | // QUuid doesn't return globally unique identifiers, therefor we query until we | ||
717 | // get one that doesn't exists yet | ||
718 | do { | ||
719 | uniqueKey = createUniqueFileName() + d->hostName; | ||
720 | key = d->path + QLatin1String("/tmp/") + uniqueKey; | ||
721 | finalKey = d->path + QLatin1String("/new/") + uniqueKey; | ||
722 | curKey = d->path + QLatin1String("/cur/") + uniqueKey; | ||
723 | } while (QFile::exists(key) || QFile::exists(finalKey) || QFile::exists(curKey)); | ||
724 | |||
725 | QFile f(path); | ||
726 | if (!f.open(QIODevice::ReadWrite)) { | ||
727 | qWarning() << f.errorString(); | ||
728 | qWarning() << "Cannot open mail file: " << key; | ||
729 | return QString(); | ||
730 | } | ||
731 | |||
732 | if (!f.rename(finalKey)) { | ||
733 | qWarning() << "Maildir: Failed to add entry: " << finalKey << "! Error: " << f.errorString(); | ||
734 | // d->lastError = i18n("Failed to create mail file %1. The error was: %2" , finalKey, f.errorString()); | ||
735 | return QString(); | ||
736 | } | ||
737 | // KeyCache *keyCache = KeyCache::self(); | ||
738 | // keyCache->removeKey(d->path, key); //remove all keys, be it "cur" or "new" first | ||
739 | // keyCache->addNewKey(d->path, key); //and add a key for "new", as the mail was moved there | ||
740 | return uniqueKey; | ||
741 | } | ||
742 | |||
709 | bool Maildir::removeEntry(const QString& key) | 743 | bool Maildir::removeEntry(const QString& key) |
710 | { | 744 | { |
711 | QString realKey(d->findRealKey(key)); | 745 | QString realKey(d->findRealKey(key)); |
diff --git a/examples/maildirresource/libmaildir/maildir.h b/examples/maildirresource/libmaildir/maildir.h index 31778ac..8c622c7 100644 --- a/examples/maildirresource/libmaildir/maildir.h +++ b/examples/maildirresource/libmaildir/maildir.h | |||
@@ -207,6 +207,7 @@ public: | |||
207 | * Adds the given @p data to the maildir. Returns the key of the entry. | 207 | * Adds the given @p data to the maildir. Returns the key of the entry. |
208 | */ | 208 | */ |
209 | QString addEntry( const QByteArray& data ); | 209 | QString addEntry( const QByteArray& data ); |
210 | QString addEntryFromPath(const QString& path); | ||
210 | 211 | ||
211 | /** | 212 | /** |
212 | * Removes the entry with the given @p key. Returns success or failure. | 213 | * Removes the entry with the given @p key. Returns success or failure. |
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp index 839b427..2ad45cd 100644 --- a/examples/maildirresource/maildirresource.cpp +++ b/examples/maildirresource/maildirresource.cpp | |||
@@ -290,7 +290,7 @@ KAsync::Job<void> MaildirResource::replay(Sink::Storage &synchronizationStore, c | |||
290 | } | 290 | } |
291 | //FIXME move the mime message from the mimeMessage property to the proper place. | 291 | //FIXME move the mime message from the mimeMessage property to the proper place. |
292 | Trace() << "Creating a new mail."; | 292 | Trace() << "Creating a new mail."; |
293 | const auto remoteId = maildir.addEntry("foobar"); | 293 | const auto remoteId = maildir.addEntryFromPath(mail.getProperty("mimeMessage").toString()); |
294 | if (remoteId.isEmpty()) { | 294 | if (remoteId.isEmpty()) { |
295 | Warning() << "Failed to create mail: " << remoteId; | 295 | Warning() << "Failed to create mail: " << remoteId; |
296 | return KAsync::error<void>(1, "Failed to create mail."); | 296 | return KAsync::error<void>(1, "Failed to create mail."); |
diff --git a/tests/maildirresourcetest.cpp b/tests/maildirresourcetest.cpp index 7b7f819..b91aef2 100644 --- a/tests/maildirresourcetest.cpp +++ b/tests/maildirresourcetest.cpp | |||
@@ -1,6 +1,7 @@ | |||
1 | #include <QtTest> | 1 | #include <QtTest> |
2 | 2 | ||
3 | #include <QString> | 3 | #include <QString> |
4 | #include <KMime/Message> | ||
4 | 5 | ||
5 | #include "maildirresource/maildirresource.h" | 6 | #include "maildirresource/maildirresource.h" |
6 | #include "store.h" | 7 | #include "store.h" |
@@ -296,9 +297,12 @@ private slots: | |||
296 | // Ensure all local data is processed | 297 | // Ensure all local data is processed |
297 | Sink::ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | 298 | Sink::ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); |
298 | 299 | ||
300 | auto message = KMime::Message::Ptr::create(); | ||
301 | message->subject(true)->fromUnicodeString(QString::fromLatin1("Foobar"), "utf8"); | ||
302 | message->assemble(); | ||
303 | |||
299 | Sink::ApplicationDomain::Mail mail("org.kde.maildir.instance1"); | 304 | Sink::ApplicationDomain::Mail mail("org.kde.maildir.instance1"); |
300 | mail.setProperty("name", "testCreateMail"); | 305 | mail.setBlobProperty("mimeMessage", message->encodedContent()); |
301 | // FIXME instead of properties, ensure the mimeMessage property is used and the file is moved as expected | ||
302 | 306 | ||
303 | Sink::Store::create(mail).exec().waitForFinished(); | 307 | Sink::Store::create(mail).exec().waitForFinished(); |
304 | 308 | ||
@@ -309,6 +313,12 @@ private slots: | |||
309 | QDir dir(targetPath); | 313 | QDir dir(targetPath); |
310 | dir.setFilter(QDir::Files); | 314 | dir.setFilter(QDir::Files); |
311 | QTRY_COMPARE(dir.count(), static_cast<unsigned int>(1)); | 315 | QTRY_COMPARE(dir.count(), static_cast<unsigned int>(1)); |
316 | QFile file(targetPath + "/" + dir.entryList().first()); | ||
317 | QVERIFY(file.open(QIODevice::ReadOnly)); | ||
318 | KMime::Message m; | ||
319 | m.setContent(file.readAll()); | ||
320 | m.parse(); | ||
321 | QCOMPARE(m.subject(true)->asUnicodeString(), QString::fromLatin1("Foobar")); | ||
312 | } | 322 | } |
313 | 323 | ||
314 | void testRemoveMail() | 324 | void testRemoveMail() |