diff options
Diffstat (limited to 'framework/domain/composer.cpp')
-rw-r--r-- | framework/domain/composer.cpp | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/framework/domain/composer.cpp b/framework/domain/composer.cpp new file mode 100644 index 00000000..2f4fe2e9 --- /dev/null +++ b/framework/domain/composer.cpp | |||
@@ -0,0 +1,199 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Michael Bohlender <michael.bohlender@kdemail.net> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | |||
21 | #include "composer.h" | ||
22 | #include <actions/context.h> | ||
23 | #include <actions/action.h> | ||
24 | #include <settings/settings.h> | ||
25 | #include <KMime/Message> | ||
26 | #include <KCodecs/KEmailAddress> | ||
27 | #include <QVariant> | ||
28 | #include <QDebug> | ||
29 | |||
30 | #include "mailtemplates.h" | ||
31 | |||
32 | Composer::Composer(QObject *parent) : QObject(parent) | ||
33 | { | ||
34 | m_identityModel << "Kuberich <kuberich@kolabnow.com>" << "Uni <kuberich@university.edu>" << "Spam <hello.spam@spam.to>"; | ||
35 | } | ||
36 | |||
37 | QString Composer::to() const | ||
38 | { | ||
39 | return m_to; | ||
40 | } | ||
41 | |||
42 | void Composer::setTo(const QString &to) | ||
43 | { | ||
44 | if(m_to != to) { | ||
45 | m_to = to; | ||
46 | emit toChanged(); | ||
47 | } | ||
48 | } | ||
49 | |||
50 | QString Composer::cc() const | ||
51 | { | ||
52 | return m_cc; | ||
53 | } | ||
54 | |||
55 | void Composer::setCc(const QString &cc) | ||
56 | { | ||
57 | if(m_cc != cc) { | ||
58 | m_cc = cc; | ||
59 | emit ccChanged(); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | QString Composer::bcc() const | ||
64 | { | ||
65 | return m_bcc; | ||
66 | } | ||
67 | |||
68 | void Composer::setBcc(const QString &bcc) | ||
69 | { | ||
70 | if(m_bcc != bcc) { | ||
71 | m_bcc = bcc; | ||
72 | emit bccChanged(); | ||
73 | } | ||
74 | } | ||
75 | |||
76 | QString Composer::subject() const | ||
77 | { | ||
78 | return m_subject; | ||
79 | } | ||
80 | |||
81 | void Composer::setSubject(const QString &subject) | ||
82 | { | ||
83 | if(m_subject != subject) { | ||
84 | m_subject = subject; | ||
85 | emit subjectChanged(); | ||
86 | } | ||
87 | } | ||
88 | |||
89 | QString Composer::body() const | ||
90 | { | ||
91 | return m_body; | ||
92 | } | ||
93 | |||
94 | void Composer::setBody(const QString &body) | ||
95 | { | ||
96 | if(m_body != body) { | ||
97 | m_body = body; | ||
98 | emit bodyChanged(); | ||
99 | } | ||
100 | } | ||
101 | |||
102 | QStringList Composer::identityModel() const | ||
103 | { | ||
104 | return m_identityModel; | ||
105 | } | ||
106 | |||
107 | int Composer::fromIndex() const | ||
108 | { | ||
109 | return m_fromIndex; | ||
110 | } | ||
111 | |||
112 | void Composer::setFromIndex(int fromIndex) | ||
113 | { | ||
114 | if(m_fromIndex != fromIndex) { | ||
115 | m_fromIndex = fromIndex; | ||
116 | emit fromIndexChanged(); | ||
117 | } | ||
118 | } | ||
119 | |||
120 | QVariant Composer::originalMessage() const | ||
121 | { | ||
122 | return m_originalMessage; | ||
123 | } | ||
124 | |||
125 | void Composer::setOriginalMessage(const QVariant &originalMessage) | ||
126 | { | ||
127 | const auto mailData = KMime::CRLFtoLF(originalMessage.toByteArray()); | ||
128 | if (!mailData.isEmpty()) { | ||
129 | KMime::Message::Ptr mail(new KMime::Message); | ||
130 | mail->setContent(mailData); | ||
131 | mail->parse(); | ||
132 | auto reply = MailTemplates::reply(mail); | ||
133 | //We assume reply | ||
134 | setTo(reply->to(true)->asUnicodeString()); | ||
135 | setCc(reply->cc(true)->asUnicodeString()); | ||
136 | setSubject(reply->subject(true)->asUnicodeString()); | ||
137 | setBody(reply->body()); | ||
138 | m_msg = QVariant::fromValue(reply); | ||
139 | } else { | ||
140 | m_msg = QVariant(); | ||
141 | } | ||
142 | } | ||
143 | |||
144 | KMime::Message::Ptr Composer::assembleMessage() | ||
145 | { | ||
146 | auto mail = m_msg.value<KMime::Message::Ptr>(); | ||
147 | if (!mail) { | ||
148 | mail = KMime::Message::Ptr::create(); | ||
149 | } | ||
150 | for (const auto &to : KEmailAddress::splitAddressList(m_to)) { | ||
151 | QByteArray displayName; | ||
152 | QByteArray addrSpec; | ||
153 | QByteArray comment; | ||
154 | KEmailAddress::splitAddress(to.toUtf8(), displayName, addrSpec, comment); | ||
155 | mail->to(true)->addAddress(addrSpec, displayName); | ||
156 | } | ||
157 | mail->subject(true)->fromUnicodeString(m_subject, "utf-8"); | ||
158 | mail->setBody(m_body.toUtf8()); | ||
159 | mail->assemble(); | ||
160 | return mail; | ||
161 | } | ||
162 | |||
163 | void Composer::send() | ||
164 | { | ||
165 | auto mail = assembleMessage(); | ||
166 | Kube::ApplicationContext settings; | ||
167 | auto account = settings.currentAccount(); | ||
168 | auto identity = account.primaryIdentity(); | ||
169 | auto transport = identity.transport(); | ||
170 | |||
171 | Kube::Context context; | ||
172 | context.setProperty("message", QVariant::fromValue(mail)); | ||
173 | |||
174 | context.setProperty("username", transport.username()); | ||
175 | context.setProperty("password", transport.password()); | ||
176 | context.setProperty("server", transport.server()); | ||
177 | |||
178 | Kube::Action("org.kde.kube.actions.sendmail", context).execute(); | ||
179 | clear(); | ||
180 | } | ||
181 | |||
182 | void Composer::saveAsDraft() | ||
183 | { | ||
184 | auto mail = assembleMessage(); | ||
185 | Kube::Context context; | ||
186 | context.setProperty("message", QVariant::fromValue(mail)); | ||
187 | Kube::Action("org.kde.kube.actions.saveasdraft", context).execute(); | ||
188 | clear(); | ||
189 | } | ||
190 | |||
191 | void Composer::clear() | ||
192 | { | ||
193 | setSubject(""); | ||
194 | setBody(""); | ||
195 | setTo(""); | ||
196 | setCc(""); | ||
197 | setBcc(""); | ||
198 | setFromIndex(-1); | ||
199 | } | ||