diff options
Diffstat (limited to 'examples/mailtransportresource/facade.cpp')
-rw-r--r-- | examples/mailtransportresource/facade.cpp | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/examples/mailtransportresource/facade.cpp b/examples/mailtransportresource/facade.cpp new file mode 100644 index 0000000..b90df16 --- /dev/null +++ b/examples/mailtransportresource/facade.cpp | |||
@@ -0,0 +1,142 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 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 "facade.h" | ||
21 | |||
22 | #include <QDir> | ||
23 | #include <QFileInfo> | ||
24 | #include <QSettings> | ||
25 | #include <QStandardPaths> | ||
26 | #include <QUuid> | ||
27 | #include <KMime/Message> | ||
28 | |||
29 | #include "resultprovider.h" | ||
30 | #include "mailtransport.h" | ||
31 | #include <log.h> | ||
32 | #include <resourceconfig.h> | ||
33 | |||
34 | static QString dataDirectory(const QByteArray &identifier) | ||
35 | { | ||
36 | return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sink/mailtransport/" + identifier; | ||
37 | } | ||
38 | |||
39 | class Outbox { | ||
40 | public: | ||
41 | Outbox(const QByteArray &identifier) : mIdentifier(identifier) | ||
42 | { | ||
43 | |||
44 | } | ||
45 | |||
46 | static QString fileName(const QByteArray &resourceId, const QByteArray &messageId) | ||
47 | { | ||
48 | return dataDirectory(resourceId)+"/" + messageId; | ||
49 | } | ||
50 | |||
51 | void add(const QByteArray &messageId, const QString &messagePath, QMap<QByteArray, QString> config) | ||
52 | { | ||
53 | QDir dir; | ||
54 | dir.mkpath(dataDirectory(mIdentifier)); | ||
55 | if (!QFile(messagePath).rename(fileName(mIdentifier, messageId))) { | ||
56 | ErrorMsg() << "Failed to move the file:"; | ||
57 | ErrorMsg() << messagePath << " to " << fileName(mIdentifier, messageId); | ||
58 | } | ||
59 | //TODO store settings | ||
60 | // QSettings settings(dataDirectory(mIdentifier) + "/messageId.ini", QSettings::IniFormat); | ||
61 | } | ||
62 | |||
63 | void dispatch(const QByteArray &messageId) | ||
64 | { | ||
65 | QFile mimeMessage(fileName(mIdentifier, messageId)); | ||
66 | if (!mimeMessage.open(QIODevice::ReadOnly)) { | ||
67 | ErrorMsg() << "Failed to open mime message: " << mimeMessage.errorString(); | ||
68 | ErrorMsg() << fileName(mIdentifier, messageId); | ||
69 | return; | ||
70 | } | ||
71 | |||
72 | auto msg = KMime::Message::Ptr::create(); | ||
73 | msg->setHead(KMime::CRLFtoLF(mimeMessage.readAll())); | ||
74 | msg->parse(); | ||
75 | MailTransport::sendMessage(msg, mServer, mUsername, mPassword, mCaCert); | ||
76 | Trace() << "Sent message: " << msg->subject(); | ||
77 | } | ||
78 | |||
79 | void setServer(const QByteArray &server, const QByteArray &username, const QByteArray &caCert) | ||
80 | { | ||
81 | mServer = server; | ||
82 | mUsername = username; | ||
83 | mCaCert = caCert; | ||
84 | } | ||
85 | |||
86 | void setPassword(const QByteArray &password) | ||
87 | { | ||
88 | mPassword = password; | ||
89 | } | ||
90 | |||
91 | private: | ||
92 | QByteArray mServer; | ||
93 | QByteArray mUsername; | ||
94 | QByteArray mPassword; | ||
95 | QByteArray mCaCert; | ||
96 | QByteArray mIdentifier; | ||
97 | }; | ||
98 | |||
99 | MailtransportFacade::MailtransportFacade(const QByteArray &identifier) : Sink::StoreFacade<Sink::ApplicationDomain::Mail>(), mIdentifier(identifier) | ||
100 | { | ||
101 | } | ||
102 | |||
103 | MailtransportFacade::~MailtransportFacade() | ||
104 | { | ||
105 | } | ||
106 | |||
107 | KAsync::Job<void> MailtransportFacade::create(const Sink::ApplicationDomain::Mail &mail) | ||
108 | { | ||
109 | Trace() << "Called create: "; | ||
110 | return KAsync::start<void>([mail, this]() { | ||
111 | auto config = ResourceConfig::getConfiguration(mIdentifier); | ||
112 | |||
113 | auto identifier = QUuid::createUuid().toByteArray(); | ||
114 | Trace() << "Sending new message: " << identifier; | ||
115 | Trace() << config.value("server").toByteArray() << config.value("username").toByteArray() << config.value("cacert").toByteArray(); | ||
116 | |||
117 | Outbox outbox(mIdentifier); | ||
118 | outbox.setServer(config.value("server").toByteArray(), config.value("username").toByteArray(), config.value("cacert").toByteArray()); | ||
119 | //FIXME remove and somehow retrieve the password on demand | ||
120 | outbox.setPassword(config.value("password").toByteArray()); | ||
121 | |||
122 | const QByteArray mimeMessage = mail.getProperty("mimeMessage").toByteArray(); | ||
123 | QMap<QByteArray, QString> configurationValues; | ||
124 | outbox.add(identifier, mimeMessage, configurationValues); | ||
125 | outbox.dispatch(identifier); | ||
126 | }); | ||
127 | } | ||
128 | |||
129 | KAsync::Job<void> MailtransportFacade::modify(const Sink::ApplicationDomain::Mail &mail) | ||
130 | { | ||
131 | return KAsync::error<void>(0, "Not implemented."); | ||
132 | } | ||
133 | |||
134 | KAsync::Job<void> MailtransportFacade::remove(const Sink::ApplicationDomain::Mail &mail) | ||
135 | { | ||
136 | return KAsync::error<void>(0, "Not implemented."); | ||
137 | } | ||
138 | |||
139 | QPair<KAsync::Job<void>, typename Sink::ResultEmitter<Sink::ApplicationDomain::Mail::Ptr>::Ptr> MailtransportFacade::load(const Sink::Query &query) | ||
140 | { | ||
141 | return qMakePair(KAsync::error<void>(0, "Not implemented."), Sink::ResultEmitter<Sink::ApplicationDomain::Mail::Ptr>::Ptr()); | ||
142 | } | ||