summaryrefslogtreecommitdiffstats
path: root/examples/imapresource/imapresource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/imapresource/imapresource.cpp')
-rw-r--r--examples/imapresource/imapresource.cpp95
1 files changed, 2 insertions, 93 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp
index 7226fd5..f9168e4 100644
--- a/examples/imapresource/imapresource.cpp
+++ b/examples/imapresource/imapresource.cpp
@@ -47,6 +47,7 @@
47 47
48#include "imapserverproxy.h" 48#include "imapserverproxy.h"
49#include "entityreader.h" 49#include "entityreader.h"
50#include "mailpreprocessor.h"
50 51
51//This is the resources entity type, and not the domain type 52//This is the resources entity type, and not the domain type
52#define ENTITY_TYPE_MAIL "mail" 53#define ENTITY_TYPE_MAIL "mail"
@@ -136,98 +137,6 @@ public:
136 QByteArray mResourceInstanceIdentifier; 137 QByteArray mResourceInstanceIdentifier;
137}; 138};
138 139
139class MailPropertyExtractor : public Sink::EntityPreprocessor<ApplicationDomain::Mail>
140{
141public:
142
143 void updatedIndexedProperties(Sink::ApplicationDomain::Mail &mail)
144 {
145 const auto mimeMessagePath = mail.getMimeMessagePath();
146 Trace() << "Updating indexed properties " << mimeMessagePath;
147 QFile f(mimeMessagePath);
148 if (!f.open(QIODevice::ReadOnly)) {
149 Warning() << "Failed to open the file: " << mimeMessagePath;
150 return;
151 }
152 if (!f.size()) {
153 Warning() << "The file is empty.";
154 return;
155 }
156 const auto mappedSize = qMin((qint64)8000, f.size());
157 auto mapped = f.map(0, mappedSize);
158 if (!mapped) {
159 Warning() << "Failed to map the file: " << f.errorString();
160 return;
161 }
162
163 KMime::Message *msg = new KMime::Message;
164 msg->setHead(KMime::CRLFtoLF(QByteArray::fromRawData(reinterpret_cast<const char*>(mapped), mappedSize)));
165 msg->parse();
166
167 mail.setExtractedSubject(msg->subject(true)->asUnicodeString());
168 mail.setExtractedSender(msg->from(true)->asUnicodeString());
169 mail.setExtractedSenderName(msg->from(true)->asUnicodeString());
170 mail.setExtractedDate(msg->date(true)->dateTime());
171 }
172
173 void newEntity(Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
174 {
175 updatedIndexedProperties(mail);
176 }
177
178 void modifiedEntity(const Sink::ApplicationDomain::Mail &oldMail, Sink::ApplicationDomain::Mail &newMail,Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
179 {
180 updatedIndexedProperties(newMail);
181 }
182};
183
184class MimeMessageMover : public Sink::EntityPreprocessor<ApplicationDomain::Mail>
185{
186public:
187 MimeMessageMover(const QByteArray &resourceInstanceIdentifier) : Sink::EntityPreprocessor<ApplicationDomain::Mail>(), mResourceInstanceIdentifier(resourceInstanceIdentifier) {}
188
189 QString moveMessage(const QString &oldPath, const Sink::ApplicationDomain::Mail &mail)
190 {
191 const auto directory = Sink::resourceStorageLocation(mResourceInstanceIdentifier);
192 const auto filePath = directory + "/" + mail.identifier();
193 if (oldPath != filePath) {
194 if (!QDir().mkpath(directory)) {
195 Warning() << "Failed to create the directory: " << directory;
196 }
197 QFile::remove(filePath);
198 QFile origFile(oldPath);
199 if (!origFile.open(QIODevice::ReadWrite)) {
200 Warning() << "Failed to open the original file with write rights: " << origFile.errorString();
201 }
202 if (!origFile.rename(filePath)) {
203 Warning() << "Failed to move the file from: " << oldPath << " to " << filePath << ". " << origFile.errorString();
204 }
205 origFile.close();
206 return filePath;
207 }
208 return oldPath;
209 }
210
211 void newEntity(Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
212 {
213 if (!mail.getMimeMessagePath().isEmpty()) {
214 mail.setMimeMessagePath(moveMessage(mail.getMimeMessagePath(), mail));
215 }
216 }
217
218 void modifiedEntity(const Sink::ApplicationDomain::Mail &oldMail, Sink::ApplicationDomain::Mail &newMail, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
219 {
220 if (!newMail.getMimeMessagePath().isEmpty()) {
221 newMail.setMimeMessagePath(moveMessage(newMail.getMimeMessagePath(), newMail));
222 }
223 }
224
225 void deletedEntity(const Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction) Q_DECL_OVERRIDE
226 {
227 QFile::remove(mail.getMimeMessagePath());
228 }
229 QByteArray mResourceInstanceIdentifier;
230};
231 140
232static qint64 uidFromMailRid(const QByteArray &remoteId) 141static qint64 uidFromMailRid(const QByteArray &remoteId)
233{ 142{
@@ -651,7 +560,7 @@ ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPo
651 changereplay->mPassword = mPassword; 560 changereplay->mPassword = mPassword;
652 setupChangereplay(changereplay); 561 setupChangereplay(changereplay);
653 562
654 setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new SpecialPurposeProcessor(mResourceType, mResourceInstanceIdentifier) << new MimeMessageMover(mResourceInstanceIdentifier) << new MailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); 563 setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new SpecialPurposeProcessor(mResourceType, mResourceInstanceIdentifier) << new MimeMessageMover << new MailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>);
655 setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); 564 setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>);
656} 565}
657 566