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("DAYOFMONTH"))) {
77 i += strlen("DAYOFMONTH");
78 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfMonth());
79 } else if (cmd.startsWith(QLatin1StringView("WEEKNUMBER"))) {
80 i += strlen("WEEKNUMBER");
81 result.append(MessageComposer::ConvertSnippetVariablesUtil::weekNumber());
82 } else if (cmd.startsWith(QLatin1StringView("MONTHNAMESHORT"))) {
83 i += strlen("MONTHNAMESHORT");
84 result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNameShort());
85 } else if (cmd.startsWith(QLatin1StringView("MONTHNAMELONG"))) {
86 i += strlen("MONTHNAMELONG");
87 result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNameLong());
88 } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEKNAMESHORT"))) {
89 i += strlen("DAYOFWEEKNAMESHORT");
90 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeekNameShort());
91 } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEKNAMELONG"))) {
92 i += strlen("DAYOFWEEKNAMELONG");
93 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeekNameLong());
94 } else if (cmd.startsWith(QLatin1StringView("YEARLASTMONTH"))) {
95 i += strlen("YEARLASTMONTH");
96 result.append(MessageComposer::ConvertSnippetVariablesUtil::yearLastMonth());
97 } else if (cmd.startsWith(QLatin1StringView("YEAR"))) {
98 i += strlen("YEAR");
99 result.append(MessageComposer::ConvertSnippetVariablesUtil::year());
100 } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEK"))) {
101 i += strlen("DAYOFWEEK");
102 result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeek());
103 } else if (cmd.startsWith(QLatin1StringView("LASTMONTHNAMELONG"))) {
104 i += strlen("LASTMONTHNAMELONG");
105 result.append(MessageComposer::ConvertSnippetVariablesUtil::lastMonthNameLong());
106 } else {
107 result.append(c);
108 }
109 return result;
110}
111
112QString ConvertSnippetVariablesJob::convertVariables(MessageComposer::ComposerViewInterface *composerView, const QString &text)
113{
114 QString result;
115 const int tmpl_len = text.length();
116 for (int i = 0; i < tmpl_len; ++i) {
117 const QChar c = text[i];
118 if (c == QLatin1Char('%')) {
119 const QString cmd = text.mid(i + 1);
120 if (composerView) {
121 if (cmd.startsWith(QLatin1StringView("CCADDR"))) {
122 i += strlen("CCADDR");
123 const QString str = composerView->cc();
124 result.append(str);
125 } else if (cmd.startsWith(QLatin1StringView("CCFNAME"))) {
126 i += strlen("CCFNAME");
127 const QString str = getFirstNameFromEmail(composerView->cc());
128 result.append(str);
129 } else if (cmd.startsWith(QLatin1StringView("CCLNAME"))) {
130 i += strlen("CCLNAME");
131 const QString str = getLastNameFromEmail(composerView->cc());
132 result.append(str);
133 } else if (cmd.startsWith(QLatin1StringView("CCNAME"))) {
134 i += strlen("CCNAME");
135 const QString str = getNameFromEmail(composerView->cc());
136 result.append(str);
137 } else if (cmd.startsWith(QLatin1StringView("BCCADDR"))) {
138 i += strlen("BCCADDR");
139 const QString str = composerView->bcc();
140 result.append(str);
141 } else if (cmd.startsWith(QLatin1StringView("BCCFNAME"))) {
142 i += strlen("BCCFNAME");
143 const QString str = getFirstNameFromEmail(composerView->bcc());
144 result.append(str);
145 } else if (cmd.startsWith(QLatin1StringView("BCCLNAME"))) {
146 i += strlen("BCCLNAME");
147 const QString str = getLastNameFromEmail(composerView->bcc());
148 result.append(str);
149 } else if (cmd.startsWith(QLatin1StringView("BCCNAME"))) {
150 i += strlen("BCCNAME");
151 const QString str = getNameFromEmail(composerView->bcc());
152 result.append(str);
153 } else if (cmd.startsWith(QLatin1StringView("FULLSUBJECT"))) {
154 i += strlen("FULLSUBJECT");
155 const QString str = composerView->subject();
156 result.append(str);
157 } else if (cmd.startsWith(QLatin1StringView("TOADDR"))) {
158 i += strlen("TOADDR");
159 const QString str = composerView->to();
160 result.append(str);
161 } else if (cmd.startsWith(QLatin1StringView("TOFNAME"))) {
162 i += strlen("TOFNAME");
163 const QString str = getFirstNameFromEmail(composerView->to());
164 result.append(str);
165 } else if (cmd.startsWith(QLatin1StringView("TOLNAME"))) {
166 i += strlen("TOLNAME");
167 const QString str = getLastNameFromEmail(composerView->to());
168 result.append(str);
169 } else if (cmd.startsWith(QLatin1StringView("TONAME"))) {
170 i += strlen("TONAME");
171 const QString str = getNameFromEmail(composerView->to());
172 result.append(str);
173 } else if (cmd.startsWith(QLatin1StringView("FROMADDR"))) {
174 i += strlen("FROMADDR");
175 const QString str = composerView->from();
176 result.append(str);
177 } else if (cmd.startsWith(QLatin1StringView("FROMFNAME"))) {
178 i += strlen("FROMFNAME");
179 const QString str = getFirstNameFromEmail(composerView->from());
180 result.append(str);
181 } else if (cmd.startsWith(QLatin1StringView("FROMLNAME"))) {
182 i += strlen("FROMLNAME");
183 const QString str = getLastNameFromEmail(composerView->from());
184 result.append(str);
185 } else if (cmd.startsWith(QLatin1StringView("FROMNAME"))) {
186 i += strlen("FROMNAME");
187 const QString str = getNameFromEmail(composerView->from());
188 result.append(str);
189 } else if (cmd.startsWith(QLatin1StringView("DOW"))) {
190 i += strlen("DOW");
191 const QString str = composerView->insertDayOfWeek();
192 result.append(str);
193 } else if (cmd.startsWith(QLatin1StringView("DATE"))) {
194 i += strlen("DATE");
195 const QString str = composerView->longDate();
196 result.append(str);
197 } else if (cmd.startsWith(QLatin1StringView("SHORTDATE"))) {
198 i += strlen("SHORTDATE");
199 const QString str = composerView->shortDate();
200 result.append(str);
201 } else if (cmd.startsWith(QLatin1StringView("TIME"))) {
202 i += strlen("TIME");
203 const QString str = composerView->shortTime();
204 result.append(str);
205 } else if (cmd.startsWith(QLatin1StringView("TIMELONG"))) {
206 i += strlen("TIMELONG");
207 const QString str = composerView->longTime();
208 result.append(str);
209 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTCOUNT"))) {
210 i += strlen("ATTACHMENTCOUNT");
211 const QString str = QString::number(composerView->attachments().count());
212 result.append(str);
213 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTNAMES"))) {
214 i += strlen("ATTACHMENTNAMES");
215 const QString str = composerView->attachments().names().join(QLatin1Char(','));
216 result.append(str);
217 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTFILENAMES"))) {
218 i += strlen("ATTACHMENTFILENAMES");
219 const QString str = composerView->attachments().fileNames().join(QLatin1Char(','));
220 result.append(str);
221 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTNAMESANDSIZES"))) {
222 i += strlen("ATTACHMENTNAMESANDSIZES");
223 const QString str = composerView->attachments().namesAndSize().join(QLatin1Char(','));
224 result.append(str);
225 } else {
226 result.append(convertVariables(cmd, i, c));
227 }
228 } else {
229 result.append(convertVariables(cmd, i, c));
230 }
231 } else {
232 result.append(c);
233 }
234 }
235 return result;
236}
237
238QString ConvertSnippetVariablesJob::getNameFromEmail(const QString &address)
239{
240 const QStringList lst = address.split(QStringLiteral(", "));
242 for (const QString &str : lst) {
245 const QString firstName = mailBoxAddress.name();
246 if (!firstName.isEmpty()) {
248 }
249 }
250
251 const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
252 return str;
253}
254
255QString ConvertSnippetVariablesJob::getFirstNameFromEmail(const QString &address)
256{
257 const QStringList lst = KEmailAddress::splitAddressList(address);
259 for (const QString &str : lst) {
262 const QString firstName = TemplateParser::Util::getFirstNameFromEmail(mailBoxAddress.name());
263 if (!firstName.isEmpty()) {
265 }
266 }
267
268 const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
269 return str;
270}
271
272QString ConvertSnippetVariablesJob::getLastNameFromEmail(const QString &address)
273{
274 const QStringList lst = KEmailAddress::splitAddressList(address);
276 for (const QString &str : lst) {
279 qDebug() << "newAddress.name() " << newAddress.name();
280 const QString lastName = TemplateParser::Util::getLastNameFromEmail(newAddress.name());
281 if (!lastName.isEmpty()) {
283 }
284 }
285
286 const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
287 return str;
288}
289
290#include "moc_convertsnippetvariablesjob.cpp"
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...
Q_EMITQ_EMIT
void deleteLater()
T qobject_cast(QObject *object)
QString & append(QChar ch)
bool isEmpty() const const
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 14 2024 11:49:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.