diff options
-rw-r--r-- | examples/imapresource/imapresource.cpp | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 1bcbd24..aca1c01 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -169,6 +169,46 @@ public: | |||
169 | } | 169 | } |
170 | }; | 170 | }; |
171 | 171 | ||
172 | class MimeMessageMover : public Sink::EntityPreprocessor<ApplicationDomain::Mail> | ||
173 | { | ||
174 | public: | ||
175 | MimeMessageMover(const QByteArray &resourceInstanceIdentifier) : Sink::EntityPreprocessor<ApplicationDomain::Mail>(), mResourceInstanceIdentifier(resourceInstanceIdentifier) {} | ||
176 | |||
177 | QString moveMessage(const QString &oldPath, const Sink::ApplicationDomain::Mail &mail) | ||
178 | { | ||
179 | const auto directory = Sink::resourceStorageLocation(mResourceInstanceIdentifier) + "/" + mail.getFolder(); | ||
180 | const auto filePath = directory + "/" + mail.identifier(); | ||
181 | if (oldPath != filePath) { | ||
182 | QDir().mkpath(directory); | ||
183 | QFile::remove(filePath); | ||
184 | QFile::rename(oldPath, filePath); | ||
185 | return filePath; | ||
186 | } | ||
187 | return oldPath; | ||
188 | } | ||
189 | |||
190 | void newEntity(Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
191 | { | ||
192 | if (!mail.getMimeMessagePath().isEmpty()) { | ||
193 | mail.setMimeMessagePath(moveMessage(mail.getMimeMessagePath(), mail)); | ||
194 | } | ||
195 | } | ||
196 | |||
197 | void modifiedEntity(const Sink::ApplicationDomain::Mail &oldMail, Sink::ApplicationDomain::Mail &newMail, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
198 | { | ||
199 | if (!newMail.getMimeMessagePath().isEmpty()) { | ||
200 | newMail.setMimeMessagePath(moveMessage(newMail.getMimeMessagePath(), newMail)); | ||
201 | } | ||
202 | if (oldMail.getFolder() != newMail.getFolder()) { | ||
203 | newMail.setMimeMessagePath(moveMessage(oldMail.getMimeMessagePath(), newMail)); | ||
204 | } | ||
205 | } | ||
206 | |||
207 | void deletedEntity(const Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
208 | { | ||
209 | QFile::remove(mail.getMimeMessagePath()); | ||
210 | } | ||
211 | QByteArray mResourceInstanceIdentifier; | ||
172 | }; | 212 | }; |
173 | 213 | ||
174 | static qint64 uidFromMailRid(const QByteArray &remoteId) | 214 | static qint64 uidFromMailRid(const QByteArray &remoteId) |
@@ -276,22 +316,9 @@ public: | |||
276 | 316 | ||
277 | Trace() << "Found a mail " << remoteId << message.msg->subject(true)->asUnicodeString() << message.flags; | 317 | Trace() << "Found a mail " << remoteId << message.msg->subject(true)->asUnicodeString() << message.flags; |
278 | 318 | ||
279 | Sink::ApplicationDomain::Mail mail; | 319 | auto mail = Sink::ApplicationDomain::Mail::create(mResourceInstanceIdentifier); |
280 | mail.setFolder(folderLocalId); | 320 | mail.setFolder(folderLocalId); |
281 | 321 | mail.setMimeMessage(message.msg->encodedContent()); | |
282 | const auto directory = Sink::resourceStorageLocation(mResourceInstanceIdentifier) + "/" + path.toUtf8(); | ||
283 | QDir().mkpath(directory); | ||
284 | auto filePath = directory + "/" + QByteArray::number(message.uid); | ||
285 | QFile file(filePath); | ||
286 | if (!file.open(QIODevice::WriteOnly)) { | ||
287 | Warning() << "Failed to open file for writing: " << file.errorString(); | ||
288 | } | ||
289 | const auto content = message.msg->encodedContent(); | ||
290 | file.write(content); | ||
291 | //Close before calling createOrModify, to ensure the file is available | ||
292 | file.close(); | ||
293 | |||
294 | mail.setMimeMessagePath(filePath); | ||
295 | mail.setUnread(!message.flags.contains(Imap::Flags::Seen)); | 322 | mail.setUnread(!message.flags.contains(Imap::Flags::Seen)); |
296 | mail.setImportant(message.flags.contains(Imap::Flags::Flagged)); | 323 | mail.setImportant(message.flags.contains(Imap::Flags::Flagged)); |
297 | 324 | ||
@@ -603,7 +630,7 @@ ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPo | |||
603 | changereplay->mPassword = mPassword; | 630 | changereplay->mPassword = mPassword; |
604 | setupChangereplay(changereplay); | 631 | setupChangereplay(changereplay); |
605 | 632 | ||
606 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new DraftsProcessor << new MailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); | 633 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new DraftsProcessor << new MimeMessageMover(mResourceInstanceIdentifier) << new MailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); |
607 | setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); | 634 | setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); |
608 | } | 635 | } |
609 | 636 | ||