Messagelib

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

KDE's Doxygen guidelines are available online.