summaryrefslogtreecommitdiffstats
path: root/common/mailpreprocessor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/mailpreprocessor.cpp')
-rw-r--r--common/mailpreprocessor.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/common/mailpreprocessor.cpp b/common/mailpreprocessor.cpp
new file mode 100644
index 0000000..64cb3d9
--- /dev/null
+++ b/common/mailpreprocessor.cpp
@@ -0,0 +1,117 @@
1/*
2 * Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "mailpreprocessor.h"
21
22#include <QFile>
23#include <QDir>
24#include <KMime/KMime/KMimeMessage>
25
26#include "pipeline.h"
27#include "definitions.h"
28#include "applicationdomaintype.h"
29
30using namespace Sink;
31
32void MailPropertyExtractor::updatedIndexedProperties(Sink::ApplicationDomain::Mail &mail)
33{
34 const auto mimeMessagePath = mail.getMimeMessagePath();
35 Trace() << "Updating indexed properties " << mimeMessagePath;
36 QFile f(mimeMessagePath);
37 if (!f.open(QIODevice::ReadOnly)) {
38 Warning() << "Failed to open the file: " << mimeMessagePath;
39 return;
40 }
41 if (!f.size()) {
42 Warning() << "The file is empty.";
43 return;
44 }
45 const auto mappedSize = qMin((qint64)8000, f.size());
46 auto mapped = f.map(0, mappedSize);
47 if (!mapped) {
48 Warning() << "Failed to map the file: " << f.errorString();
49 return;
50 }
51
52 KMime::Message *msg = new KMime::Message;
53 msg->setHead(KMime::CRLFtoLF(QByteArray::fromRawData(reinterpret_cast<const char*>(mapped), mappedSize)));
54 msg->parse();
55
56 mail.setExtractedSubject(msg->subject(true)->asUnicodeString());
57 mail.setExtractedSender(msg->from(true)->asUnicodeString());
58 mail.setExtractedSenderName(msg->from(true)->asUnicodeString());
59 mail.setExtractedDate(msg->date(true)->dateTime());
60}
61
62void MailPropertyExtractor::newEntity(Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction)
63{
64 updatedIndexedProperties(mail);
65}
66
67void MailPropertyExtractor::modifiedEntity(const Sink::ApplicationDomain::Mail &oldMail, Sink::ApplicationDomain::Mail &newMail,Sink::Storage::Transaction &transaction)
68{
69 updatedIndexedProperties(newMail);
70}
71
72
73MimeMessageMover::MimeMessageMover() : Sink::EntityPreprocessor<ApplicationDomain::Mail>()
74{
75}
76
77QString MimeMessageMover::moveMessage(const QString &oldPath, const Sink::ApplicationDomain::Mail &mail)
78{
79 const auto directory = Sink::resourceStorageLocation(resourceInstanceIdentifier());
80 const auto filePath = directory + "/" + mail.identifier();
81 if (oldPath != filePath) {
82 if (!QDir().mkpath(directory)) {
83 Warning() << "Failed to create the directory: " << directory;
84 }
85 QFile::remove(filePath);
86 QFile origFile(oldPath);
87 if (!origFile.open(QIODevice::ReadWrite)) {
88 Warning() << "Failed to open the original file with write rights: " << origFile.errorString();
89 }
90 if (!origFile.rename(filePath)) {
91 Warning() << "Failed to move the file from: " << oldPath << " to " << filePath << ". " << origFile.errorString();
92 }
93 origFile.close();
94 return filePath;
95 }
96 return oldPath;
97}
98
99void MimeMessageMover::newEntity(Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction)
100{
101 if (!mail.getMimeMessagePath().isEmpty()) {
102 mail.setMimeMessagePath(moveMessage(mail.getMimeMessagePath(), mail));
103 }
104}
105
106void MimeMessageMover::modifiedEntity(const Sink::ApplicationDomain::Mail &oldMail, Sink::ApplicationDomain::Mail &newMail, Sink::Storage::Transaction &transaction)
107{
108 if (!newMail.getMimeMessagePath().isEmpty()) {
109 newMail.setMimeMessagePath(moveMessage(newMail.getMimeMessagePath(), newMail));
110 }
111}
112
113void MimeMessageMover::deletedEntity(const Sink::ApplicationDomain::Mail &mail, Sink::Storage::Transaction &transaction)
114{
115 QFile::remove(mail.getMimeMessagePath());
116}
117