diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-11 16:14:24 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-11 16:14:24 +0200 |
commit | dc33cea58f4b920f60648e81379c58bd62861b9b (patch) | |
tree | 6d47ce491bd2eb0f3b4bede993ef9952140b26a0 /examples | |
parent | 5bf7ded65ef517fac6b088342d195392bc09be4c (diff) | |
download | sink-dc33cea58f4b920f60648e81379c58bd62861b9b.tar.gz sink-dc33cea58f4b920f60648e81379c58bd62861b9b.zip |
Copy mail back from mailtransport
Diffstat (limited to 'examples')
-rw-r--r-- | examples/mailtransportresource/mailtransportresource.cpp | 46 | ||||
-rw-r--r-- | examples/mailtransportresource/tests/mailtransporttest.cpp | 34 |
2 files changed, 76 insertions, 4 deletions
diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp index 75d9898..8e5b22e 100644 --- a/examples/mailtransportresource/mailtransportresource.cpp +++ b/examples/mailtransportresource/mailtransportresource.cpp | |||
@@ -43,6 +43,44 @@ SINK_DEBUG_AREA("mailtransportresource") | |||
43 | 43 | ||
44 | using namespace Sink; | 44 | using namespace Sink; |
45 | 45 | ||
46 | class MailtransportPreprocessor : public Sink::Preprocessor | ||
47 | { | ||
48 | public: | ||
49 | MailtransportPreprocessor() : Sink::Preprocessor() {} | ||
50 | |||
51 | QByteArray getTargetResource() | ||
52 | { | ||
53 | using namespace Sink::ApplicationDomain; | ||
54 | |||
55 | auto resource = Store::readOne<ApplicationDomain::SinkResource>(Query{}.filter(resourceInstanceIdentifier()).request<ApplicationDomain::SinkResource::Account>()); | ||
56 | if (resource.identifier().isEmpty()) { | ||
57 | SinkWarning() << "Failed to retrieve this resource: " << resourceInstanceIdentifier(); | ||
58 | } | ||
59 | Query query; | ||
60 | query.containsFilter<ApplicationDomain::SinkResource::Capabilities>(ApplicationDomain::ResourceCapabilities::Mail::sent); | ||
61 | query.filter<ApplicationDomain::SinkResource::Account>(resource.getAccount()); | ||
62 | auto targetResource = Store::readOne<ApplicationDomain::SinkResource>(query); | ||
63 | if (targetResource.identifier().isEmpty()) { | ||
64 | SinkWarning() << "Failed to find target resource: " << targetResource.identifier(); | ||
65 | } | ||
66 | return targetResource.identifier(); | ||
67 | } | ||
68 | |||
69 | void modifiedEntity(const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, Sink::ApplicationDomain::ApplicationDomainType &newEntity) Q_DECL_OVERRIDE | ||
70 | { | ||
71 | using namespace Sink::ApplicationDomain; | ||
72 | auto mail = newEntity.cast<Mail>(); | ||
73 | if (mail.changedProperties().contains(Mail::Trash::name)) { | ||
74 | //Move back to regular resource | ||
75 | newEntity.setResource(getTargetResource()); | ||
76 | } else if (mail.changedProperties().contains(Mail::Draft::name)) { | ||
77 | //Move back to regular resource | ||
78 | newEntity.setResource(getTargetResource()); | ||
79 | } | ||
80 | //TODO this will only copy it back, but not yet move | ||
81 | } | ||
82 | }; | ||
83 | |||
46 | class MailtransportSynchronizer : public Sink::Synchronizer { | 84 | class MailtransportSynchronizer : public Sink::Synchronizer { |
47 | public: | 85 | public: |
48 | MailtransportSynchronizer(const Sink::ResourceContext &resourceContext) | 86 | MailtransportSynchronizer(const Sink::ResourceContext &resourceContext) |
@@ -65,7 +103,11 @@ public: | |||
65 | msg->setHead(KMime::CRLFtoLF(data)); | 103 | msg->setHead(KMime::CRLFtoLF(data)); |
66 | msg->parse(); | 104 | msg->parse(); |
67 | if (settings.testMode) { | 105 | if (settings.testMode) { |
68 | SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier(); | 106 | auto subject = msg->subject(true)->asUnicodeString(); |
107 | SinkLog() << "I would totally send that mail, but I'm in test mode." << mail.identifier() << subject; | ||
108 | if (!subject.contains("send")) { | ||
109 | return KAsync::error("Failed to send the message."); | ||
110 | } | ||
69 | auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; | 111 | auto path = resourceStorageLocation(mResourceInstanceIdentifier) + "/test/"; |
70 | SinkTrace() << path; | 112 | SinkTrace() << path; |
71 | QDir dir; | 113 | QDir dir; |
@@ -194,7 +236,7 @@ MailtransportResource::MailtransportResource(const Sink::ResourceContext &resour | |||
194 | setupSynchronizer(synchronizer); | 236 | setupSynchronizer(synchronizer); |
195 | setupInspector(QSharedPointer<MailtransportInspector>::create(resourceContext)); | 237 | setupInspector(QSharedPointer<MailtransportInspector>::create(resourceContext)); |
196 | 238 | ||
197 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new MailPropertyExtractor); | 239 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new MailPropertyExtractor << new MailtransportPreprocessor); |
198 | } | 240 | } |
199 | 241 | ||
200 | MailtransportResourceFactory::MailtransportResourceFactory(QObject *parent) | 242 | MailtransportResourceFactory::MailtransportResourceFactory(QObject *parent) |
diff --git a/examples/mailtransportresource/tests/mailtransporttest.cpp b/examples/mailtransportresource/tests/mailtransporttest.cpp index 23a61b8..a30fc20 100644 --- a/examples/mailtransportresource/tests/mailtransporttest.cpp +++ b/examples/mailtransportresource/tests/mailtransporttest.cpp | |||
@@ -59,7 +59,7 @@ private slots: | |||
59 | { | 59 | { |
60 | auto message = KMime::Message::Ptr::create(); | 60 | auto message = KMime::Message::Ptr::create(); |
61 | message->messageID(true)->generate("foo.com"); | 61 | message->messageID(true)->generate("foo.com"); |
62 | message->subject(true)->fromUnicodeString(QString::fromLatin1("Foobar"), "utf8"); | 62 | message->subject(true)->fromUnicodeString(QString::fromLatin1("send: Foobar"), "utf8"); |
63 | message->assemble(); | 63 | message->assemble(); |
64 | 64 | ||
65 | auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier); | 65 | auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier); |
@@ -83,7 +83,37 @@ private slots: | |||
83 | QVERIFY(!mailInSentMailFolder.getSubject().isEmpty()); | 83 | QVERIFY(!mailInSentMailFolder.getSubject().isEmpty()); |
84 | } | 84 | } |
85 | 85 | ||
86 | //TODO test mail that fails to be sent. add a special header to the mail and have the resource fail sending. Ensure we can modify the mail to fix sending of the message. | 86 | void testSendFailure() |
87 | { | ||
88 | auto message = KMime::Message::Ptr::create(); | ||
89 | message->messageID(true)->generate("foo.com"); | ||
90 | message->subject(true)->fromUnicodeString(QString::fromLatin1("error: Foobar"), "utf8"); | ||
91 | message->assemble(); | ||
92 | |||
93 | auto mail = ApplicationDomain::Mail::create(mResourceInstanceIdentifier); | ||
94 | mail.setMimeMessage(message->encodedContent()); | ||
95 | |||
96 | VERIFYEXEC(Store::create(mail)); | ||
97 | VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); | ||
98 | |||
99 | //Ensure the mail is queryable in the outbox | ||
100 | auto mailInOutbox = Store::readOne<ApplicationDomain::Mail>(Query().resourceFilter(mResourceInstanceIdentifier).filter<Mail::Sent>(false)); | ||
101 | QVERIFY(!mailInOutbox.identifier().isEmpty()); | ||
102 | |||
103 | //Modify back to drafts | ||
104 | auto modifiedMail = mailInOutbox; | ||
105 | modifiedMail.setDraft(true); | ||
106 | VERIFYEXEC(Store::modify(modifiedMail)); | ||
107 | VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier)); | ||
108 | |||
109 | QTest::qWait(100); | ||
110 | // auto mailsInOutbox = Store::read<ApplicationDomain::Mail>(Query().resourceFilter(mResourceInstanceIdentifier)); | ||
111 | // QCOMPARE(mailsInOutbox.size(), 0); | ||
112 | |||
113 | auto mailsInDrafts = Store::read<ApplicationDomain::Mail>(Query().resourceFilter(mStorageResource)); | ||
114 | QCOMPARE(mailsInDrafts.size(), 1); | ||
115 | |||
116 | } | ||
87 | 117 | ||
88 | }; | 118 | }; |
89 | 119 | ||