7#include "convertsnippetvariablesjob.h"
8#include "composer/composerviewinterface.h"
9#include "snippet/convertsnippetvariablesutil.h"
10#include <KEmailAddress>
13#include <TemplateParser/TemplatesUtil>
15ConvertSnippetVariablesJob::ConvertSnippetVariablesJob(
QObject *parent)
20ConvertSnippetVariablesJob::~ConvertSnippetVariablesJob()
22 delete mComposerViewInterface;
25void ConvertSnippetVariablesJob::setText(
const QString &str)
30bool ConvertSnippetVariablesJob::canStart()
const
32 if (mText.isEmpty() || !mComposerViewInterface) {
38void ConvertSnippetVariablesJob::start()
41 Q_EMIT textConverted(QString());
45 Q_EMIT textConverted(convertVariables(mComposerViewInterface, mText));
49QString ConvertSnippetVariablesJob::text()
const
54MessageComposer::ComposerViewInterface *ConvertSnippetVariablesJob::composerViewInterface()
const
56 return mComposerViewInterface;
59void ConvertSnippetVariablesJob::setComposerViewInterface(MessageComposer::ComposerViewInterface *composerViewInterface)
61 mComposerViewInterface = composerViewInterface;
64QString ConvertSnippetVariablesJob::convertVariables(
const QString &cmd,
int &i, QChar c)
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"))) {
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());
118QString ConvertSnippetVariablesJob::convertVariables(MessageComposer::ComposerViewInterface *composerView,
const QString &text)
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);
127 if (cmd.
startsWith(QLatin1StringView(
"CCADDR"))) {
128 i += strlen(
"CCADDR");
129 const QString str = composerView->cc();
131 }
else if (cmd.
startsWith(QLatin1StringView(
"CCFNAME"))) {
132 i += strlen(
"CCFNAME");
133 const QString str = getFirstNameFromEmail(composerView->cc());
135 }
else if (cmd.
startsWith(QLatin1StringView(
"CCLNAME"))) {
136 i += strlen(
"CCLNAME");
137 const QString str = getLastNameFromEmail(composerView->cc());
139 }
else if (cmd.
startsWith(QLatin1StringView(
"CCNAME"))) {
140 i += strlen(
"CCNAME");
141 const QString str = getNameFromEmail(composerView->cc());
143 }
else if (cmd.
startsWith(QLatin1StringView(
"BCCADDR"))) {
144 i += strlen(
"BCCADDR");
145 const QString str = composerView->bcc();
147 }
else if (cmd.
startsWith(QLatin1StringView(
"BCCFNAME"))) {
148 i += strlen(
"BCCFNAME");
149 const QString str = getFirstNameFromEmail(composerView->bcc());
151 }
else if (cmd.
startsWith(QLatin1StringView(
"BCCLNAME"))) {
152 i += strlen(
"BCCLNAME");
153 const QString str = getLastNameFromEmail(composerView->bcc());
155 }
else if (cmd.
startsWith(QLatin1StringView(
"BCCNAME"))) {
156 i += strlen(
"BCCNAME");
157 const QString str = getNameFromEmail(composerView->bcc());
159 }
else if (cmd.
startsWith(QLatin1StringView(
"FULLSUBJECT"))) {
160 i += strlen(
"FULLSUBJECT");
161 const QString str = composerView->subject();
163 }
else if (cmd.
startsWith(QLatin1StringView(
"TOADDR"))) {
164 i += strlen(
"TOADDR");
165 const QString str = composerView->to();
167 }
else if (cmd.
startsWith(QLatin1StringView(
"TOFNAME"))) {
168 i += strlen(
"TOFNAME");
169 const QString str = getFirstNameFromEmail(composerView->to());
171 }
else if (cmd.
startsWith(QLatin1StringView(
"TOLNAME"))) {
172 i += strlen(
"TOLNAME");
173 const QString str = getLastNameFromEmail(composerView->to());
175 }
else if (cmd.
startsWith(QLatin1StringView(
"TONAME"))) {
176 i += strlen(
"TONAME");
177 const QString str = getNameFromEmail(composerView->to());
179 }
else if (cmd.
startsWith(QLatin1StringView(
"FROMADDR"))) {
180 i += strlen(
"FROMADDR");
181 const QString str = composerView->from();
183 }
else if (cmd.
startsWith(QLatin1StringView(
"FROMFNAME"))) {
184 i += strlen(
"FROMFNAME");
185 const QString str = getFirstNameFromEmail(composerView->from());
187 }
else if (cmd.
startsWith(QLatin1StringView(
"FROMLNAME"))) {
188 i += strlen(
"FROMLNAME");
189 const QString str = getLastNameFromEmail(composerView->from());
191 }
else if (cmd.
startsWith(QLatin1StringView(
"FROMNAME"))) {
192 i += strlen(
"FROMNAME");
193 const QString str = getNameFromEmail(composerView->from());
195 }
else if (cmd.
startsWith(QLatin1StringView(
"DOW"))) {
197 const QString str = composerView->insertDayOfWeek();
199 }
else if (cmd.
startsWith(QLatin1StringView(
"DATE"))) {
201 const QString str = composerView->longDate();
203 }
else if (cmd.
startsWith(QLatin1StringView(
"SHORTDATE"))) {
204 i += strlen(
"SHORTDATE");
205 const QString str = composerView->shortDate();
207 }
else if (cmd.
startsWith(QLatin1StringView(
"TIME"))) {
209 const QString str = composerView->shortTime();
211 }
else if (cmd.
startsWith(QLatin1StringView(
"TIMELONG"))) {
212 i += strlen(
"TIMELONG");
213 const QString str = composerView->longTime();
215 }
else if (cmd.
startsWith(QLatin1StringView(
"ATTACHMENTCOUNT"))) {
216 i += strlen(
"ATTACHMENTCOUNT");
217 const QString str =
QString::number(composerView->attachments().count());
219 }
else if (cmd.
startsWith(QLatin1StringView(
"ATTACHMENTNAMES"))) {
220 i += strlen(
"ATTACHMENTNAMES");
221 const QString str = composerView->attachments().names().
join(QLatin1Char(
','));
223 }
else if (cmd.
startsWith(QLatin1StringView(
"ATTACHMENTFILENAMES"))) {
224 i += strlen(
"ATTACHMENTFILENAMES");
225 const QString str = composerView->attachments().fileNames().
join(QLatin1Char(
','));
227 }
else if (cmd.
startsWith(QLatin1StringView(
"ATTACHMENTNAMESANDSIZES"))) {
228 i += strlen(
"ATTACHMENTNAMESANDSIZES");
229 const QString str = composerView->attachments().namesAndSize().
join(QLatin1Char(
','));
232 result.
append(convertVariables(cmd, i, c));
235 result.
append(convertVariables(cmd, i, c));
244QString ConvertSnippetVariablesJob::getNameFromEmail(
const QString &address)
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();
253 resultName << firstName;
257 const QString str = resultName.
isEmpty() ? QString() : resultName.join(QStringLiteral(
", "));
261QString ConvertSnippetVariablesJob::getFirstNameFromEmail(
const QString &address)
264 QStringList resultName;
265 for (
const QString &str : lst) {
266 KMime::Types::Mailbox mailBoxAddress;
268 const QString firstName = TemplateParser::Util::getFirstNameFromEmail(mailBoxAddress.
name());
270 resultName << firstName;
274 const QString str = resultName.
isEmpty() ? QString() : resultName.join(QStringLiteral(
", "));
278QString ConvertSnippetVariablesJob::getLastNameFromEmail(
const QString &address)
281 QStringList resultName;
282 for (
const QString &str : lst) {
283 KMime::Types::Mailbox newAddress;
286 const QString lastName = TemplateParser::Util::getLastNameFromEmail(newAddress.
name());
288 resultName << lastName;
292 const QString str = resultName.
isEmpty() ? QString() : resultName.join(QStringLiteral(
", "));
296#include "moc_convertsnippetvariablesjob.cpp"
void fromUnicodeString(QStringView s)
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
QString & append(QChar ch)
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) 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