Messagelib

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

KDE's Doxygen guidelines are available online.