Messagelib

templatescommandmenu.cpp
1 /*
2  SPDX-FileCopyrightText: 2018-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "templatescommandmenu.h"
8 
9 #include "templateparser_debug.h"
10 #include <KActionMenu>
11 #include <KLazyLocalizedString>
12 #include <KLocalizedString>
13 #include <QAction>
14 
15 using namespace TemplateParser;
16 
17 struct InsertCommand {
19  const TemplatesCommandMenu::Command command;
20 
21  Q_REQUIRED_RESULT QString getLocalizedDisplayName() const
22  {
23  return KLocalizedString(name).toString();
24  }
25 };
26 
27 static const InsertCommand originalCommands[] = {{kli18n("Quoted Message Text"), TemplatesCommandMenu::CQuote},
28 
29  {kli18n("Message Text as Is"), TemplatesCommandMenu::CText},
30 
31  {kli18n("Message Id"), TemplatesCommandMenu::COMsgId},
32 
33  {kli18n("Date"), TemplatesCommandMenu::CODate},
34 
35  {kli18n("Date in Short Format"), TemplatesCommandMenu::CODateShort},
36 
37  {kli18n("Date in C Locale"), TemplatesCommandMenu::CODateEn},
38 
39  {kli18n("Day of Week"), TemplatesCommandMenu::CODow},
40 
41  {kli18n("Time"), TemplatesCommandMenu::COTime},
42 
43  {kli18n("Time in Long Format"), TemplatesCommandMenu::COTimeLong},
44 
45  {kli18n("Time in C Locale"), TemplatesCommandMenu::COTimeLongEn},
46 
47  {kli18n("To Field Address"), TemplatesCommandMenu::COToAddr},
48 
49  {kli18n("To Field Name"), TemplatesCommandMenu::COToName},
50 
51  {kli18n("To Field First Name"), TemplatesCommandMenu::COToFName},
52 
53  {kli18n("To Field Last Name"), TemplatesCommandMenu::COToLName},
54 
55  {kli18n("CC Field Address"), TemplatesCommandMenu::COCCAddr},
56 
57  {kli18n("CC Field Name"), TemplatesCommandMenu::COCCName},
58 
59  {kli18n("CC Field First Name"), TemplatesCommandMenu::COCCFName},
60 
61  {kli18n("CC Field Last Name"), TemplatesCommandMenu::COCCLName},
62 
63  {kli18n("From Field Address"), TemplatesCommandMenu::COFromAddr},
64 
65  {kli18n("From Field Name"), TemplatesCommandMenu::COFromName},
66 
67  {kli18n("From Field First Name"), TemplatesCommandMenu::COFromFName},
68 
69  {kli18n("From Field Last Name"), TemplatesCommandMenu::COFromLName},
70 
71  {kli18n("Addresses of all recipients"), TemplatesCommandMenu::COAddresseesAddr},
72 
73  {kli18nc("Template value for subject of the message", "Subject"), TemplatesCommandMenu::COFullSubject},
74 
75  {kli18n("Quoted Headers"), TemplatesCommandMenu::CQHeaders},
76 
77  {kli18n("Headers as Is"), TemplatesCommandMenu::CHeaders},
78 
79  {kli18n("Header Content"), TemplatesCommandMenu::COHeader},
80 
81  {kli18n("Reply as Quoted Plain Text"), TemplatesCommandMenu::CQuotePlain},
82 
83  {kli18n("Reply as Quoted HTML Text"), TemplatesCommandMenu::CQuoteHtml}};
84 static const int originalCommandsCount = sizeof(originalCommands) / sizeof(*originalCommands);
85 
86 static const InsertCommand currentCommands[] = {{kli18n("Date"), TemplatesCommandMenu::CDate},
87 
88  {kli18n("Date in Short Format"), TemplatesCommandMenu::CDateShort},
89 
90  {kli18n("Date in C Locale"), TemplatesCommandMenu::CDateEn},
91 
92  {kli18n("Day of Week"), TemplatesCommandMenu::CDow},
93 
94  {kli18n("Time"), TemplatesCommandMenu::CTime},
95 
96  {kli18n("Time in Long Format"), TemplatesCommandMenu::CTimeLong},
97 
98  {kli18n("Time in C Locale"), TemplatesCommandMenu::CTimeLongEn},
99  {kli18n("To Field Address"), TemplatesCommandMenu::CToAddr},
100 
101  {kli18n("To Field Name"), TemplatesCommandMenu::CToName},
102 
103  {kli18n("To Field First Name"), TemplatesCommandMenu::CToFName},
104 
105  {kli18n("To Field Last Name"), TemplatesCommandMenu::CToLName},
106 
107  {kli18n("CC Field Address"), TemplatesCommandMenu::CCCAddr},
108 
109  {kli18n("CC Field Name"), TemplatesCommandMenu::CCCName},
110 
111  {kli18n("CC Field First Name"), TemplatesCommandMenu::CCCFName},
112 
113  {kli18n("CC Field Last Name"), TemplatesCommandMenu::CCCLName},
114 
115  {kli18n("From Field Address"), TemplatesCommandMenu::CFromAddr},
116 
117  {kli18n("From field Name"), TemplatesCommandMenu::CFromName},
118 
119  {kli18n("From Field First Name"), TemplatesCommandMenu::CFromFName},
120 
121  {kli18n("From Field Last Name"), TemplatesCommandMenu::CFromLName},
122 
123  {kli18nc("Template subject command.", "Subject"), TemplatesCommandMenu::CFullSubject},
124 
125  {kli18n("Header Content"), TemplatesCommandMenu::CHeader}};
126 static const int currentCommandsCount = sizeof(currentCommands) / sizeof(*currentCommands);
127 
128 static const InsertCommand extCommands[] = {{kli18n("Pipe Original Message Body and Insert Result as Quoted Text"), TemplatesCommandMenu::CQuotePipe},
129 
130  {kli18n("Pipe Original Message Body and Insert Result as Is"), TemplatesCommandMenu::CTextPipe},
131 
132  {kli18n("Pipe Original Message with Headers and Insert Result as Is"), TemplatesCommandMenu::CMsgPipe},
133 
134  {kli18n("Pipe Current Message Body and Insert Result as Is"), TemplatesCommandMenu::CBodyPipe},
135 
136  {kli18n("Pipe Current Message Body and Replace with Result"), TemplatesCommandMenu::CClearPipe}};
137 
138 static const int extCommandsCount = sizeof(extCommands) / sizeof(*extCommands);
139 
140 static const InsertCommand miscCommands[] = {
141  {kli18nc("Inserts user signature, also known as footer, into message", "Signature"), TemplatesCommandMenu::CSignature},
142 
143  {kli18n("Insert File Content"), TemplatesCommandMenu::CInsert},
144 
145  {kli18nc("All characters, up to and including the next newline, "
146  "are discarded without performing any macro expansion",
147  "Discard to Next Line"),
148  TemplatesCommandMenu::CDnl},
149 
150  {kli18n("Template Comment"), TemplatesCommandMenu::CRem},
151 
152  {kli18n("No Operation"), TemplatesCommandMenu::CNop},
153 
154  {kli18n("Clear Generated Message"), TemplatesCommandMenu::CClear},
155  {kli18n("Cursor position"), TemplatesCommandMenu::CCursor},
156 
157  {kli18n("Blank text"), TemplatesCommandMenu::CBlank},
158 
159  {kli18n("Dictionary Language"), TemplatesCommandMenu::CDictionaryLanguage},
160  {kli18n("Language"), TemplatesCommandMenu::CLanguage},
161  // TODO add support for custom variable. %CUSTOM="???" ?
162 };
163 static const int miscCommandsCount = sizeof(miscCommands) / sizeof(*miscCommands);
164 
165 static const InsertCommand debugCommands[] = {{kli18n("Turn Debug On"), TemplatesCommandMenu::CDebug},
166 
167  {kli18n("Turn Debug Off"), TemplatesCommandMenu::CDebugOff}};
168 static const int debugCommandsCount = sizeof(debugCommands) / sizeof(*debugCommands);
169 
170 void TemplatesCommandMenu::fillMenuFromActionMap(const QMap<QString, TemplatesCommandMenu::Command> &map, KActionMenu *menu)
171 {
174 
175  while (it != end) {
176  auto action = new QAction(it.key(), menu); // krazy:exclude=tipsandthis
177  const TemplatesCommandMenu::Command cmd = it.value();
178  connect(action, &QAction::triggered, this, [this, cmd] {
179  slotInsertCommand(cmd);
180  });
181  menu->addAction(action);
182  ++it;
183  }
184 }
185 
186 TemplatesCommandMenu::TemplatesCommandMenu(QObject *parent)
187  : QObject(parent)
188 {
189 }
190 
191 TemplatesCommandMenu::~TemplatesCommandMenu() = default;
192 
193 QMenu *TemplatesCommandMenu::menu() const
194 {
195  return mMenu->menu();
196 }
197 
198 void TemplatesCommandMenu::fillMenu()
199 {
200  mMenu = new KActionMenu(i18n("Insert Command"), this);
201 }
202 
203 void TemplatesCommandMenu::fillSubMenus()
204 {
205  if (mWasInitialized) {
206  return;
207  }
208  mWasInitialized = true;
209  QMap<QString, Command> commandMap;
210  KActionMenu *menu = nullptr;
211  // ******************************************************
212  if (mType & ReplyForwardMessage) {
213  menu = new KActionMenu(i18n("Original Message"), mMenu);
214  mMenu->addAction(menu);
215 
216  // Map sorts commands
217  for (int i = 0; i < originalCommandsCount; ++i) {
218  commandMap.insert(originalCommands[i].getLocalizedDisplayName(), originalCommands[i].command);
219  }
220 
221  fillMenuFromActionMap(commandMap, menu);
222  commandMap.clear();
223  }
224 
225  // ******************************************************
226  if (mType & CurrentMessage) {
227  menu = new KActionMenu(i18n("Current Message"), mMenu);
228  mMenu->addAction(menu);
229 
230  for (int i = 0; i < currentCommandsCount; ++i) {
231  commandMap.insert(currentCommands[i].getLocalizedDisplayName(), currentCommands[i].command);
232  }
233 
234  fillMenuFromActionMap(commandMap, menu);
235  commandMap.clear();
236  }
237 
238  // ******************************************************
239  if (mType & External) {
240  menu = new KActionMenu(i18n("Process with External Programs"), mMenu);
241  mMenu->addAction(menu);
242 
243  for (int i = 0; i < extCommandsCount; ++i) {
244  commandMap.insert(extCommands[i].getLocalizedDisplayName(), extCommands[i].command);
245  }
246 
247  fillMenuFromActionMap(commandMap, menu);
248  commandMap.clear();
249  }
250 
251  // ******************************************************
252  if (mType & Misc) {
253  menu = new KActionMenu(i18nc("Miscellaneous template commands menu", "Miscellaneous"), mMenu);
254  mMenu->addAction(menu);
255 
256  for (int i = 0; i < miscCommandsCount; ++i) {
257  commandMap.insert(miscCommands[i].getLocalizedDisplayName(), miscCommands[i].command);
258  }
259 
260  fillMenuFromActionMap(commandMap, menu);
261  commandMap.clear();
262  }
263 
264  // ******************************************************
265  if (!qEnvironmentVariableIsEmpty("KDEPIM_DEBUGGING")) {
266  if (mType & Debug) {
267  menu = new KActionMenu(i18nc("Debug template commands menu", "Debug"), mMenu);
268  mMenu->addAction(menu);
269 
270  for (int i = 0; i < debugCommandsCount; ++i) {
271  commandMap.insert(debugCommands[i].getLocalizedDisplayName(), debugCommands[i].command);
272  }
273 
274  fillMenuFromActionMap(commandMap, menu);
275  }
276  }
277 }
278 
279 TemplatesCommandMenu::MenuTypes TemplatesCommandMenu::type() const
280 {
281  return mType;
282 }
283 
284 void TemplatesCommandMenu::setType(MenuTypes type)
285 {
286  mType = type;
287  delete mMenu;
288  mWasInitialized = false;
289  fillMenu();
290  fillSubMenus();
291 }
292 
293 void TemplatesCommandMenu::slotInsertCommand(TemplatesCommandMenu::Command cmd)
294 {
295  Q_EMIT insertCommand(cmd);
296 
297  switch (cmd) {
298  case TemplatesCommandMenu::CBlank:
299  Q_EMIT insertCommand(QStringLiteral("%BLANK"));
300  return;
301  case TemplatesCommandMenu::CQuote:
302  Q_EMIT insertCommand(QStringLiteral("%QUOTE"));
303  return;
304  case TemplatesCommandMenu::CText:
305  Q_EMIT insertCommand(QStringLiteral("%TEXT"));
306  return;
307  case TemplatesCommandMenu::COMsgId:
308  Q_EMIT insertCommand(QStringLiteral("%OMSGID"));
309  return;
310  case TemplatesCommandMenu::CODate:
311  Q_EMIT insertCommand(QStringLiteral("%ODATE"));
312  return;
313  case TemplatesCommandMenu::CODateShort:
314  Q_EMIT insertCommand(QStringLiteral("%ODATESHORT"));
315  return;
316  case TemplatesCommandMenu::CODateEn:
317  Q_EMIT insertCommand(QStringLiteral("%ODATEEN"));
318  return;
319  case TemplatesCommandMenu::CODow:
320  Q_EMIT insertCommand(QStringLiteral("%ODOW"));
321  return;
322  case TemplatesCommandMenu::COTime:
323  Q_EMIT insertCommand(QStringLiteral("%OTIME"));
324  return;
325  case TemplatesCommandMenu::COTimeLong:
326  Q_EMIT insertCommand(QStringLiteral("%OTIMELONG"));
327  return;
328  case TemplatesCommandMenu::COTimeLongEn:
329  Q_EMIT insertCommand(QStringLiteral("%OTIMELONGEN"));
330  return;
331  case TemplatesCommandMenu::COToAddr:
332  Q_EMIT insertCommand(QStringLiteral("%OTOADDR"));
333  return;
334  case TemplatesCommandMenu::COToName:
335  Q_EMIT insertCommand(QStringLiteral("%OTONAME"));
336  return;
337  case TemplatesCommandMenu::COToFName:
338  Q_EMIT insertCommand(QStringLiteral("%OTOFNAME"));
339  return;
340  case TemplatesCommandMenu::COToLName:
341  Q_EMIT insertCommand(QStringLiteral("%OTOLNAME"));
342  return;
343  case TemplatesCommandMenu::COCCAddr:
344  Q_EMIT insertCommand(QStringLiteral("%OCCADDR"));
345  return;
346  case TemplatesCommandMenu::COCCName:
347  Q_EMIT insertCommand(QStringLiteral("%OCCNAME"));
348  return;
349  case TemplatesCommandMenu::COCCFName:
350  Q_EMIT insertCommand(QStringLiteral("%OCCFNAME"));
351  return;
352  case TemplatesCommandMenu::COCCLName:
353  Q_EMIT insertCommand(QStringLiteral("%OCCLNAME"));
354  return;
355  case TemplatesCommandMenu::COFromAddr:
356  Q_EMIT insertCommand(QStringLiteral("%OFROMADDR"));
357  return;
358  case TemplatesCommandMenu::COFromName:
359  Q_EMIT insertCommand(QStringLiteral("%OFROMNAME"));
360  return;
361  case TemplatesCommandMenu::COFromFName:
362  Q_EMIT insertCommand(QStringLiteral("%OFROMFNAME"));
363  return;
364  case TemplatesCommandMenu::COFromLName:
365  Q_EMIT insertCommand(QStringLiteral("%OFROMLNAME"));
366  return;
367  case TemplatesCommandMenu::COFullSubject:
368  Q_EMIT insertCommand(QStringLiteral("%OFULLSUBJECT"));
369  return;
370  case TemplatesCommandMenu::CQHeaders:
371  Q_EMIT insertCommand(QStringLiteral("%QHEADERS"));
372  return;
373  case TemplatesCommandMenu::CHeaders:
374  Q_EMIT insertCommand(QStringLiteral("%HEADERS"));
375  return;
376  case TemplatesCommandMenu::COHeader:
377  Q_EMIT insertCommand(QStringLiteral("%OHEADER=\"\""), -1);
378  return;
379  case TemplatesCommandMenu::CMsgId:
380  Q_EMIT insertCommand(QStringLiteral("%MSGID"));
381  return;
382  case TemplatesCommandMenu::CDate:
383  Q_EMIT insertCommand(QStringLiteral("%DATE"));
384  return;
385  case TemplatesCommandMenu::CDateShort:
386  Q_EMIT insertCommand(QStringLiteral("%DATESHORT"));
387  return;
388  case TemplatesCommandMenu::CDateEn:
389  Q_EMIT insertCommand(QStringLiteral("%DATEEN"));
390  return;
391  case TemplatesCommandMenu::CDow:
392  Q_EMIT insertCommand(QStringLiteral("%DOW"));
393  return;
394  case TemplatesCommandMenu::CTime:
395  Q_EMIT insertCommand(QStringLiteral("%TIME"));
396  return;
397  case TemplatesCommandMenu::CTimeLong:
398  Q_EMIT insertCommand(QStringLiteral("%TIMELONG"));
399  return;
400  case TemplatesCommandMenu::CTimeLongEn:
401  Q_EMIT insertCommand(QStringLiteral("%TIMELONGEN"));
402  return;
403  case TemplatesCommandMenu::COAddresseesAddr:
404  Q_EMIT insertCommand(QStringLiteral("%OADDRESSEESADDR"));
405  return;
406  case TemplatesCommandMenu::CToAddr:
407  Q_EMIT insertCommand(QStringLiteral("%TOADDR"));
408  return;
409  case TemplatesCommandMenu::CToName:
410  Q_EMIT insertCommand(QStringLiteral("%TONAME"));
411  return;
412  case TemplatesCommandMenu::CToFName:
413  Q_EMIT insertCommand(QStringLiteral("%TOFNAME"));
414  return;
415  case TemplatesCommandMenu::CToLName:
416  Q_EMIT insertCommand(QStringLiteral("%TOLNAME"));
417  return;
418  case TemplatesCommandMenu::CCCAddr:
419  Q_EMIT insertCommand(QStringLiteral("%CCADDR"));
420  return;
421  case TemplatesCommandMenu::CCCName:
422  Q_EMIT insertCommand(QStringLiteral("%CCNAME"));
423  return;
424  case TemplatesCommandMenu::CCCFName:
425  Q_EMIT insertCommand(QStringLiteral("%CCFNAME"));
426  return;
427  case TemplatesCommandMenu::CCCLName:
428  Q_EMIT insertCommand(QStringLiteral("%CCLNAME"));
429  return;
430  case TemplatesCommandMenu::CFromAddr:
431  Q_EMIT insertCommand(QStringLiteral("%FROMADDR"));
432  return;
433  case TemplatesCommandMenu::CFromName:
434  Q_EMIT insertCommand(QStringLiteral("%FROMNAME"));
435  return;
436  case TemplatesCommandMenu::CFromFName:
437  Q_EMIT insertCommand(QStringLiteral("%FROMFNAME"));
438  return;
439  case TemplatesCommandMenu::CFromLName:
440  Q_EMIT insertCommand(QStringLiteral("%FROMLNAME"));
441  return;
442  case TemplatesCommandMenu::CFullSubject:
443  Q_EMIT insertCommand(QStringLiteral("%FULLSUBJECT"));
444  return;
445  case TemplatesCommandMenu::CHeader:
446  Q_EMIT insertCommand(QStringLiteral("%HEADER=\"\""), -1);
447  return;
448  case TemplatesCommandMenu::CSystem:
449  Q_EMIT insertCommand(QStringLiteral("%SYSTEM=\"\""), -1);
450  return;
451  case TemplatesCommandMenu::CQuotePipe:
452  Q_EMIT insertCommand(QStringLiteral("%QUOTEPIPE=\"\""), -1);
453  return;
454  case TemplatesCommandMenu::CTextPipe:
455  Q_EMIT insertCommand(QStringLiteral("%TEXTPIPE=\"\""), -1);
456  return;
457  case TemplatesCommandMenu::CMsgPipe:
458  Q_EMIT insertCommand(QStringLiteral("%MSGPIPE=\"\""), -1);
459  return;
460  case TemplatesCommandMenu::CBodyPipe:
461  Q_EMIT insertCommand(QStringLiteral("%BODYPIPE=\"\""), -1);
462  return;
463  case TemplatesCommandMenu::CClearPipe:
464  Q_EMIT insertCommand(QStringLiteral("%CLEARPIPE=\"\""), -1);
465  return;
466  case TemplatesCommandMenu::CCursor:
467  Q_EMIT insertCommand(QStringLiteral("%CURSOR"));
468  return;
469  case TemplatesCommandMenu::CSignature:
470  Q_EMIT insertCommand(QStringLiteral("%SIGNATURE"));
471  return;
472  case TemplatesCommandMenu::CInsert:
473  Q_EMIT insertCommand(QStringLiteral("%INSERT=\"\""), -1);
474  return;
475  case TemplatesCommandMenu::CDnl:
476  Q_EMIT insertCommand(QStringLiteral("%-"));
477  return;
478  case TemplatesCommandMenu::CRem:
479  Q_EMIT insertCommand(QStringLiteral("%REM=\"\""), -1);
480  return;
481  case TemplatesCommandMenu::CNop:
482  Q_EMIT insertCommand(QStringLiteral("%NOP"));
483  return;
484  case TemplatesCommandMenu::CClear:
485  Q_EMIT insertCommand(QStringLiteral("%CLEAR"));
486  return;
487  case TemplatesCommandMenu::CDebug:
488  Q_EMIT insertCommand(QStringLiteral("%DEBUG"));
489  return;
490  case TemplatesCommandMenu::CDebugOff:
491  Q_EMIT insertCommand(QStringLiteral("%DEBUGOFF"));
492  return;
493  case TemplatesCommandMenu::CQuotePlain:
494  Q_EMIT insertCommand(QStringLiteral("%FORCEDPLAIN"));
495  return;
496  case TemplatesCommandMenu::CQuoteHtml:
497  Q_EMIT insertCommand(QStringLiteral("%FORCEDHTML"));
498  return;
499  case TemplatesCommandMenu::CDictionaryLanguage:
500  Q_EMIT insertCommand(QStringLiteral("%DICTIONARYLANGUAGE=\"\""), -1);
501  return;
502  case TemplatesCommandMenu::CLanguage:
503  Q_EMIT insertCommand(QStringLiteral("%LANGUAGE=\"\""), -1);
504  return;
505  }
506  qCDebug(TEMPLATEPARSER_LOG) << "Unknown template command index:" << cmd;
507 }
void addAction(QAction *action)
Q_EMITQ_EMIT
void clear()
QString toString() const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QMap::iterator insert(const Key &key, const T &value)
QMenu * menu() const const
QString i18n(const char *text, const TYPE &arg...)
void triggered(bool checked)
QString name(StandardShortcut id)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QFuture< void > map(Sequence &sequence, MapFunctor function)
const QList< QKeySequence > & end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue May 17 2022 04:00:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.