Messagelib

convertsnippetvariablesjob.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "convertsnippetvariablesjob.h"
8#include "composer/composerviewinterface.h"
9#include "snippet/convertsnippetvariablesutil.h"
10#include <KEmailAddress>
11#include <KMime/Types>
12#include <QDebug>
13#include <TemplateParser/TemplatesUtil>
14using namespace MessageComposer;
15ConvertSnippetVariablesJob::ConvertSnippetVariablesJob(QObject *parent)
16 : QObject(parent)
17{
18}
19
20ConvertSnippetVariablesJob::~ConvertSnippetVariablesJob()
21{
22 delete mComposerViewInterface;
23}
24
25void ConvertSnippetVariablesJob::setText(const QString &str)
26{
27 mText = str;
28}
29
30bool ConvertSnippetVariablesJob::canStart() const
31{
32 if (mText.isEmpty() || !mComposerViewInterface) {
33 return false;
34 }
35 return true;
36}
37
38void ConvertSnippetVariablesJob::start()
39{
40 if (!canStart()) {
41 Q_EMIT textConverted(QString());
43 return;
44 }
45 Q_EMIT textConverted(convertVariables(mComposerViewInterface, mText));
47}
48
49QString ConvertSnippetVariablesJob::text() const
50{
51 return mText;
52}
53
54MessageComposer::ComposerViewInterface *ConvertSnippetVariablesJob::composerViewInterface() const
55{
56 return mComposerViewInterface;
57}
58
59void ConvertSnippetVariablesJob::setComposerViewInterface(MessageComposer::ComposerViewInterface *composerViewInterface)
60{
61 mComposerViewInterface = composerViewInterface;
62}
63
64QString ConvertSnippetVariablesJob::convertVariables(const QString &cmd, int &i, QChar c)
65{
66 QString result;
67 if (cmd.startsWith(QLatin1StringView("LASTYEAR"))) {
68 i += strlen("LASTYEAR");
69 result.append(MessageComposer::ConvertSnippetVariablesUtil::lastYear());
70 } else if (cmd.startsWith(QLatin1StringView("NEXTYEAR"))) {
71 i += strlen("NEXTYEAR");
72 result.append(MessageComposer::ConvertSnippetVariablesUtil::nextYear());
73 } else if (cmd.startsWith(QLatin1StringView("MONTHNUMBER"))) {
74 i += strlen("MONTHNUMBER");
75 result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNumber());
76 } else if (cmd.startsWith(QLatin1StringView("DAYNUMBER"))) {
77 i += strlen("DAYNUMBER");
78 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayNumber());
79 } else if (cmd.startsWith(QLatin1StringView("CUSTOMDATE"))) {
80 i += strlen("CUSTOMDATE");
81 result.append(MessageComposer::ConvertSnippetVariablesUtil::customDate());
82 } else if (cmd.startsWith(QLatin1StringView("DAYOFMONTH"))) {
83 i += strlen("DAYOFMONTH");
84 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfMonth());
85 } else if (cmd.startsWith(QLatin1StringView("WEEKNUMBER"))) {
86 i += strlen("WEEKNUMBER");
87 result.append(MessageComposer::ConvertSnippetVariablesUtil::weekNumber());
88 } else if (cmd.startsWith(QLatin1StringView("MONTHNAMESHORT"))) {
89 i += strlen("MONTHNAMESHORT");
90 result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNameShort());
91 } else if (cmd.startsWith(QLatin1StringView("MONTHNAMELONG"))) {
92 i += strlen("MONTHNAMELONG");
93 result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNameLong());
94 } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEKNAMESHORT"))) {
95 i += strlen("DAYOFWEEKNAMESHORT");
96 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeekNameShort());
97 } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEKNAMELONG"))) {
98 i += strlen("DAYOFWEEKNAMELONG");
99 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeekNameLong());
100 } else if (cmd.startsWith(QLatin1StringView("YEARLASTMONTH"))) {
101 i += strlen("YEARLASTMONTH");
102 result.append(MessageComposer::ConvertSnippetVariablesUtil::yearLastMonth());
103 } else if (cmd.startsWith(QLatin1StringView("YEAR"))) {
104 i += strlen("YEAR");
105 result.append(MessageComposer::ConvertSnippetVariablesUtil::year());
106 } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEK"))) {
107 i += strlen("DAYOFWEEK");
108 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeek());
109 } else if (cmd.startsWith(QLatin1StringView("LASTMONTHNAMELONG"))) {
110 i += strlen("LASTMONTHNAMELONG");
111 result.append(MessageComposer::ConvertSnippetVariablesUtil::lastMonthNameLong());
112 } else {
113 result.append(c);
114 }
115 return result;
116}
117
118QString ConvertSnippetVariablesJob::convertVariables(MessageComposer::ComposerViewInterface *composerView, const QString &text)
119{
120 QString result;
121 const int tmpl_len = text.length();
122 for (int i = 0; i < tmpl_len; ++i) {
123 const QChar c = text[i];
124 if (c == QLatin1Char('%')) {
125 const QString cmd = text.mid(i + 1);
126 if (composerView) {
127 if (cmd.startsWith(QLatin1StringView("CCADDR"))) {
128 i += strlen("CCADDR");
129 const QString str = composerView->cc();
130 result.append(str);
131 } else if (cmd.startsWith(QLatin1StringView("CCFNAME"))) {
132 i += strlen("CCFNAME");
133 const QString str = getFirstNameFromEmail(composerView->cc());
134 result.append(str);
135 } else if (cmd.startsWith(QLatin1StringView("CCLNAME"))) {
136 i += strlen("CCLNAME");
137 const QString str = getLastNameFromEmail(composerView->cc());
138 result.append(str);
139 } else if (cmd.startsWith(QLatin1StringView("CCNAME"))) {
140 i += strlen("CCNAME");
141 const QString str = getNameFromEmail(composerView->cc());
142 result.append(str);
143 } else if (cmd.startsWith(QLatin1StringView("BCCADDR"))) {
144 i += strlen("BCCADDR");
145 const QString str = composerView->bcc();
146 result.append(str);
147 } else if (cmd.startsWith(QLatin1StringView("BCCFNAME"))) {
148 i += strlen("BCCFNAME");
149 const QString str = getFirstNameFromEmail(composerView->bcc());
150 result.append(str);
151 } else if (cmd.startsWith(QLatin1StringView("BCCLNAME"))) {
152 i += strlen("BCCLNAME");
153 const QString str = getLastNameFromEmail(composerView->bcc());
154 result.append(str);
155 } else if (cmd.startsWith(QLatin1StringView("BCCNAME"))) {
156 i += strlen("BCCNAME");
157 const QString str = getNameFromEmail(composerView->bcc());
158 result.append(str);
159 } else if (cmd.startsWith(QLatin1StringView("FULLSUBJECT"))) {
160 i += strlen("FULLSUBJECT");
161 const QString str = composerView->subject();
162 result.append(str);
163 } else if (cmd.startsWith(QLatin1StringView("TOADDR"))) {
164 i += strlen("TOADDR");
165 const QString str = composerView->to();
166 result.append(str);
167 } else if (cmd.startsWith(QLatin1StringView("TOFNAME"))) {
168 i += strlen("TOFNAME");
169 const QString str = getFirstNameFromEmail(composerView->to());
170 result.append(str);
171 } else if (cmd.startsWith(QLatin1StringView("TOLNAME"))) {
172 i += strlen("TOLNAME");
173 const QString str = getLastNameFromEmail(composerView->to());
174 result.append(str);
175 } else if (cmd.startsWith(QLatin1StringView("TONAME"))) {
176 i += strlen("TONAME");
177 const QString str = getNameFromEmail(composerView->to());
178 result.append(str);
179 } else if (cmd.startsWith(QLatin1StringView("FROMADDR"))) {
180 i += strlen("FROMADDR");
181 const QString str = composerView->from();
182 result.append(str);
183 } else if (cmd.startsWith(QLatin1StringView("FROMFNAME"))) {
184 i += strlen("FROMFNAME");
185 const QString str = getFirstNameFromEmail(composerView->from());
186 result.append(str);
187 } else if (cmd.startsWith(QLatin1StringView("FROMLNAME"))) {
188 i += strlen("FROMLNAME");
189 const QString str = getLastNameFromEmail(composerView->from());
190 result.append(str);
191 } else if (cmd.startsWith(QLatin1StringView("FROMNAME"))) {
192 i += strlen("FROMNAME");
193 const QString str = getNameFromEmail(composerView->from());
194 result.append(str);
195 } else if (cmd.startsWith(QLatin1StringView("DOW"))) {
196 i += strlen("DOW");
197 const QString str = composerView->insertDayOfWeek();
198 result.append(str);
199 } else if (cmd.startsWith(QLatin1StringView("DATE"))) {
200 i += strlen("DATE");
201 const QString str = composerView->longDate();
202 result.append(str);
203 } else if (cmd.startsWith(QLatin1StringView("SHORTDATE"))) {
204 i += strlen("SHORTDATE");
205 const QString str = composerView->shortDate();
206 result.append(str);
207 } else if (cmd.startsWith(QLatin1StringView("TIME"))) {
208 i += strlen("TIME");
209 const QString str = composerView->shortTime();
210 result.append(str);
211 } else if (cmd.startsWith(QLatin1StringView("TIMELONG"))) {
212 i += strlen("TIMELONG");
213 const QString str = composerView->longTime();
214 result.append(str);
215 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTCOUNT"))) {
216 i += strlen("ATTACHMENTCOUNT");
217 const QString str = QString::number(composerView->attachments().count());
218 result.append(str);
219 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTNAMES"))) {
220 i += strlen("ATTACHMENTNAMES");
221 const QString str = composerView->attachments().names().join(QLatin1Char(','));
222 result.append(str);
223 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTFILENAMES"))) {
224 i += strlen("ATTACHMENTFILENAMES");
225 const QString str = composerView->attachments().fileNames().join(QLatin1Char(','));
226 result.append(str);
227 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTNAMESANDSIZES"))) {
228 i += strlen("ATTACHMENTNAMESANDSIZES");
229 const QString str = composerView->attachments().namesAndSize().join(QLatin1Char(','));
230 result.append(str);
231 } else {
232 result.append(convertVariables(cmd, i, c));
233 }
234 } else {
235 result.append(convertVariables(cmd, i, c));
236 }
237 } else {
238 result.append(c);
239 }
240 }
241 return result;
242}
243
244QString ConvertSnippetVariablesJob::getNameFromEmail(const QString &address)
245{
246 const QStringList lst = address.split(QStringLiteral(", "));
247 QStringList resultName;
248 for (const QString &str : lst) {
249 KMime::Types::Mailbox mailBoxAddress;
251 const QString firstName = mailBoxAddress.name();
252 if (!firstName.isEmpty()) {
253 resultName << firstName;
254 }
255 }
256
257 const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
258 return str;
259}
260
261QString ConvertSnippetVariablesJob::getFirstNameFromEmail(const QString &address)
262{
263 const QStringList lst = KEmailAddress::splitAddressList(address);
264 QStringList resultName;
265 for (const QString &str : lst) {
266 KMime::Types::Mailbox mailBoxAddress;
268 const QString firstName = TemplateParser::Util::getFirstNameFromEmail(mailBoxAddress.name());
269 if (!firstName.isEmpty()) {
270 resultName << firstName;
271 }
272 }
273
274 const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
275 return str;
276}
277
278QString ConvertSnippetVariablesJob::getLastNameFromEmail(const QString &address)
279{
280 const QStringList lst = KEmailAddress::splitAddressList(address);
281 QStringList resultName;
282 for (const QString &str : lst) {
283 KMime::Types::Mailbox newAddress;
285 // qDebug() << "newAddress.name() " << newAddress.name();
286 const QString lastName = TemplateParser::Util::getLastNameFromEmail(newAddress.name());
287 if (!lastName.isEmpty()) {
288 resultName << lastName;
289 }
290 }
291
292 const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
293 return str;
294}
295
296#include "moc_convertsnippetvariablesjob.cpp"
QString name() const
void fromUnicodeString(QStringView s)
The ComposerViewInterface class.
KCODECS_EXPORT QString normalizeAddressesAndEncodeIdn(const QString &str)
KCODECS_EXPORT QStringList splitAddressList(const QString &aStr)
PostalAddress address(const QVariant &location)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
bool isEmpty() const const
Q_EMITQ_EMIT
void deleteLater()
QString & append(QChar ch)
bool isEmpty() const const
QString number(double n, char format, int precision)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.