Messagelib

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

KDE's Doxygen guidelines are available online.