2 * Copyright (C) 2006 Dmitry Morozhnikov <dmiceman@mail.ru>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "templatesinsertcommand.h"
22 #include <KActionMenu>
27 #include <QSignalMapper>
30 #define I18N_NOOP(t) 0, t
32 #define I18N_NOOP2(c,t) c, t
34 using namespace TemplateParser
;
40 const TemplatesInsertCommand::Command command
;
42 QString
getLocalizedDisplayName() const
44 return i18nc( context
, name
);
48 static const InsertCommand originalCommands
[] =
50 { I18N_NOOP( "Quoted Message Text" ),
51 TemplatesInsertCommand::CQuote
54 { I18N_NOOP( "Message Text as Is" ),
55 TemplatesInsertCommand::CText
58 { I18N_NOOP( "Message Id" ),
59 TemplatesInsertCommand::COMsgId
62 { I18N_NOOP( "Date" ),
63 TemplatesInsertCommand::CODate
66 { I18N_NOOP( "Date in Short Format" ),
67 TemplatesInsertCommand::CODateShort
70 { I18N_NOOP( "Date in C Locale" ),
71 TemplatesInsertCommand::CODateEn
74 { I18N_NOOP( "Day of Week" ),
75 TemplatesInsertCommand::CODow
78 { I18N_NOOP( "Time" ),
79 TemplatesInsertCommand::COTime
82 { I18N_NOOP( "Time in Long Format" ),
83 TemplatesInsertCommand::COTimeLong
86 { I18N_NOOP( "Time in C Locale" ),
87 TemplatesInsertCommand::COTimeLongEn
90 { I18N_NOOP( "To Field Address" ),
91 TemplatesInsertCommand::COToAddr
94 { I18N_NOOP( "To Field Name" ),
95 TemplatesInsertCommand::COToName
98 { I18N_NOOP( "To Field First Name" ),
99 TemplatesInsertCommand::COToFName
102 { I18N_NOOP( "To Field Last Name" ),
103 TemplatesInsertCommand::COToLName
106 { I18N_NOOP( "CC Field Address" ),
107 TemplatesInsertCommand::COCCAddr
110 { I18N_NOOP( "CC Field Name" ),
111 TemplatesInsertCommand::COCCName
114 { I18N_NOOP( "CC Field First Name" ),
115 TemplatesInsertCommand::COCCFName
118 { I18N_NOOP( "CC Field Last Name" ),
119 TemplatesInsertCommand::COCCLName
122 { I18N_NOOP( "From Field Address" ),
123 TemplatesInsertCommand::COFromAddr
126 { I18N_NOOP( "From Field Name" ),
127 TemplatesInsertCommand::COFromName
130 { I18N_NOOP( "From Field First Name" ),
131 TemplatesInsertCommand::COFromFName
134 { I18N_NOOP( "From Field Last Name" ),
135 TemplatesInsertCommand::COFromLName
},
137 { I18N_NOOP( "Addresses of all recipients" ),
138 TemplatesInsertCommand::COAddresseesAddr
141 { I18N_NOOP2( "Template value for subject of the message", "Subject" ),
142 TemplatesInsertCommand::COFullSubject
145 { I18N_NOOP( "Quoted Headers" ),
146 TemplatesInsertCommand::CQHeaders
149 { I18N_NOOP( "Headers as Is" ),
150 TemplatesInsertCommand::CHeaders
153 { I18N_NOOP( "Header Content" ),
154 TemplatesInsertCommand::COHeader
157 { I18N_NOOP( "Reply as Quoted Plain Text" ),
158 TemplatesInsertCommand::CQuotePlain
161 { I18N_NOOP( "Reply as Quoted HTML Text" ),
162 TemplatesInsertCommand::CQuoteHtml
165 static const int originalCommandsCount
=
166 sizeof( originalCommands
) / sizeof( *originalCommands
);
168 static const InsertCommand currentCommands
[] =
170 { I18N_NOOP( "Date" ),
171 TemplatesInsertCommand::CDate
174 { I18N_NOOP( "Date in Short Format" ),
175 TemplatesInsertCommand::CDateShort
178 { I18N_NOOP( "Date in C Locale" ),
179 TemplatesInsertCommand::CDateEn
182 { I18N_NOOP( "Day of Week" ),
183 TemplatesInsertCommand::CDow
186 { I18N_NOOP( "Time" ),
187 TemplatesInsertCommand::CTime
190 { I18N_NOOP( "Time in Long Format" ),
191 TemplatesInsertCommand::CTimeLong
194 { I18N_NOOP( "Time in C Locale" ),
195 TemplatesInsertCommand::CTimeLongEn
},
196 { I18N_NOOP( "To Field Address" ),
197 TemplatesInsertCommand::CToAddr
200 { I18N_NOOP( "To Field Name" ),
201 TemplatesInsertCommand::CToName
204 { I18N_NOOP( "To Field First Name" ),
205 TemplatesInsertCommand::CToFName
208 { I18N_NOOP( "To Field Last Name" ),
209 TemplatesInsertCommand::CToLName
212 { I18N_NOOP( "CC Field Address" ),
213 TemplatesInsertCommand::CCCAddr
216 { I18N_NOOP( "CC Field Name" ),
217 TemplatesInsertCommand::CCCName
220 { I18N_NOOP( "CC Field First Name" ),
221 TemplatesInsertCommand::CCCFName
224 { I18N_NOOP( "CC Field Last Name" ),
225 TemplatesInsertCommand::CCCLName
228 { I18N_NOOP( "From Field Address" ),
229 TemplatesInsertCommand::CFromAddr
232 { I18N_NOOP( "From field Name" ),
233 TemplatesInsertCommand::CFromName
236 { I18N_NOOP( "From Field First Name" ),
237 TemplatesInsertCommand::CFromFName
240 { I18N_NOOP( "From Field Last Name" ),
241 TemplatesInsertCommand::CFromLName
244 { I18N_NOOP2( "Template subject command.", "Subject" ),
245 TemplatesInsertCommand::CFullSubject
248 { I18N_NOOP( "Header Content" ),
249 TemplatesInsertCommand::CHeader
252 static const int currentCommandsCount
= sizeof( currentCommands
) / sizeof( *currentCommands
);
254 static const InsertCommand extCommands
[] =
256 { I18N_NOOP( "Pipe Original Message Body and Insert Result as Quoted Text" ),
257 TemplatesInsertCommand::CQuotePipe
260 { I18N_NOOP( "Pipe Original Message Body and Insert Result as Is" ),
261 TemplatesInsertCommand::CTextPipe
264 { I18N_NOOP( "Pipe Original Message with Headers and Insert Result as Is" ),
265 TemplatesInsertCommand::CMsgPipe
268 { I18N_NOOP( "Pipe Current Message Body and Insert Result as Is" ),
269 TemplatesInsertCommand::CBodyPipe
272 { I18N_NOOP( "Pipe Current Message Body and Replace with Result" ),
273 TemplatesInsertCommand::CClearPipe
277 static const int extCommandsCount
=
278 sizeof( extCommands
) / sizeof( *extCommands
);
280 static const InsertCommand miscCommands
[] =
282 { I18N_NOOP2( "Inserts user signature, also known as footer, into message", "Signature" ),
283 TemplatesInsertCommand::CSignature
286 { I18N_NOOP( "Insert File Content" ),
287 TemplatesInsertCommand::CInsert
290 { I18N_NOOP2( "All characters, up to and including the next newline, "
291 "are discarded without performing any macro expansion",
292 "Discard to Next Line" ),
293 TemplatesInsertCommand::CDnl
296 { I18N_NOOP( "Template Comment" ),
297 TemplatesInsertCommand::CRem
300 { I18N_NOOP( "No Operation" ),
301 TemplatesInsertCommand::CNop
304 { I18N_NOOP( "Clear Generated Message" ),
305 TemplatesInsertCommand::CClear
308 { I18N_NOOP( "Turn Debug On" ),
309 TemplatesInsertCommand::CDebug
312 { I18N_NOOP( "Turn Debug Off" ),
313 TemplatesInsertCommand::CDebugOff
316 { I18N_NOOP( "Cursor position" ),
317 TemplatesInsertCommand::CCursor
320 { I18N_NOOP( "Blank text" ),
321 TemplatesInsertCommand::CBlank
324 static const int miscCommandsCount
= sizeof( miscCommands
) / sizeof( *miscCommands
);
326 static void fillMenuFromActionMap( const QMap
< QString
, TemplatesInsertCommand::Command
> &map
,
327 KActionMenu
*menu
, QSignalMapper
*mapper
)
329 QMap
< QString
, TemplatesInsertCommand::Command
>::const_iterator it
= map
.constBegin();
330 QMap
< QString
, TemplatesInsertCommand::Command
>::const_iterator end
= map
.constEnd();
332 while ( it
!= end
) {
333 KAction
*action
= new KAction( it
.key(), menu
); //krazy:exclude=tipsandthis
334 QObject::connect( action
, SIGNAL(triggered(bool)), mapper
, SLOT(map()) );
335 mapper
->setMapping( action
, it
.value() );
336 menu
->addAction( action
);
341 TemplatesInsertCommand::TemplatesInsertCommand( QWidget
*parent
, const QString
&name
)
342 : QPushButton( parent
)
344 setObjectName( name
);
345 setText( i18n( "&Insert Command" ) );
348 QMap
< QString
, Command
> commandMap
;
350 QSignalMapper
*mapper
= new QSignalMapper( this );
351 connect( mapper
, SIGNAL(mapped(int)),
352 this, SLOT(slotMapped(int)) );
354 mMenu
= new KActionMenu( i18n( "Insert Command" ), this );
356 i18nc( "@info:tooltip",
357 "Select a command to insert into the template" ) );
359 i18nc( "@info:whatsthis",
360 "Traverse this menu to find a command to insert into the current template "
361 "being edited. The command will be inserted at the cursor location, "
362 "so you want to move your cursor to the desired insertion point first." ) );
364 // ******************************************************
365 menu
= new KActionMenu( i18n( "Original Message" ), mMenu
);
366 mMenu
->addAction( menu
);
368 // Map sorts commands
369 for ( int i
= 0; i
< originalCommandsCount
; ++i
) {
370 commandMap
.insert( originalCommands
[i
].getLocalizedDisplayName(), originalCommands
[i
].command
);
373 fillMenuFromActionMap( commandMap
, menu
, mapper
);
376 // ******************************************************
377 menu
= new KActionMenu( i18n( "Current Message" ), mMenu
);
378 mMenu
->addAction( menu
);
380 for ( int i
= 0; i
< currentCommandsCount
; ++i
) {
381 commandMap
.insert( currentCommands
[i
].getLocalizedDisplayName(), currentCommands
[i
].command
);
384 fillMenuFromActionMap( commandMap
, menu
, mapper
);
387 // ******************************************************
388 menu
= new KActionMenu( i18n( "Process with External Programs" ), mMenu
);
389 mMenu
->addAction( menu
);
391 for ( int i
= 0; i
< extCommandsCount
; ++i
) {
392 commandMap
.insert( extCommands
[i
].getLocalizedDisplayName(), extCommands
[i
].command
);
395 fillMenuFromActionMap( commandMap
, menu
, mapper
);
398 // ******************************************************
399 menu
= new KActionMenu( i18nc( "Miscellaneous template commands menu", "Miscellaneous" ), mMenu
);
400 mMenu
->addAction( menu
);
402 for ( int i
= 0; i
< miscCommandsCount
; ++i
) {
403 commandMap
.insert( miscCommands
[i
].getLocalizedDisplayName(), miscCommands
[i
].command
);
406 fillMenuFromActionMap( commandMap
, menu
, mapper
);
408 setMenu( mMenu
->menu() );
411 TemplatesInsertCommand::~TemplatesInsertCommand()
415 void TemplatesInsertCommand::slotMapped( int cmd
)
417 emit
insertCommand( static_cast<Command
>( cmd
) );
420 case TemplatesInsertCommand::CQuote
:
421 emit
insertCommand( QLatin1String("%QUOTE") );
423 case TemplatesInsertCommand::CText
:
424 emit
insertCommand( QLatin1String("%TEXT") );
426 case TemplatesInsertCommand::COMsgId
:
427 emit
insertCommand( QLatin1String("%OMSGID") );
429 case TemplatesInsertCommand::CODate
:
430 emit
insertCommand( QLatin1String("%ODATE") );
432 case TemplatesInsertCommand::CODateShort
:
433 emit
insertCommand( QLatin1String("%ODATESHORT") );
435 case TemplatesInsertCommand::CODateEn
:
436 emit
insertCommand( QLatin1String("%ODATEEN") );
438 case TemplatesInsertCommand::CODow
:
439 emit
insertCommand( QLatin1String("%ODOW") );
441 case TemplatesInsertCommand::COTime
:
442 emit
insertCommand( QLatin1String("%OTIME") );
444 case TemplatesInsertCommand::COTimeLong
:
445 emit
insertCommand( QLatin1String("%OTIMELONG") );
447 case TemplatesInsertCommand::COTimeLongEn
:
448 emit
insertCommand( QLatin1String("%OTIMELONGEN") );
450 case TemplatesInsertCommand::COToAddr
:
451 emit
insertCommand( QLatin1String("%OTOADDR" ));
453 case TemplatesInsertCommand::COToName
:
454 emit
insertCommand( QLatin1String("%OTONAME") );
456 case TemplatesInsertCommand::COToFName
:
457 emit
insertCommand( QLatin1String("%OTOFNAME") );
459 case TemplatesInsertCommand::COToLName
:
460 emit
insertCommand( QLatin1String("%OTOLNAME") );
462 case TemplatesInsertCommand::COCCAddr
:
463 emit
insertCommand( QLatin1String("%OCCADDR") );
465 case TemplatesInsertCommand::COCCName
:
466 emit
insertCommand( QLatin1String("%OCCNAME") );
468 case TemplatesInsertCommand::COCCFName
:
469 emit
insertCommand( QLatin1String("%OCCFNAME") );
471 case TemplatesInsertCommand::COCCLName
:
472 emit
insertCommand( QLatin1String("%OCCLNAME") );
474 case TemplatesInsertCommand::COFromAddr
:
475 emit
insertCommand( QLatin1String("%OFROMADDR") );
477 case TemplatesInsertCommand::COFromName
:
478 emit
insertCommand( QLatin1String("%OFROMNAME") );
480 case TemplatesInsertCommand::COFromFName
:
481 emit
insertCommand( QLatin1String("%OFROMFNAME") );
483 case TemplatesInsertCommand::COFromLName
:
484 emit
insertCommand( QLatin1String("%OFROMLNAME") );
486 case TemplatesInsertCommand::COFullSubject
:
487 emit
insertCommand( QLatin1String("%OFULLSUBJECT") );
489 case TemplatesInsertCommand::CQHeaders
:
490 emit
insertCommand( QLatin1String("%QHEADERS") );
492 case TemplatesInsertCommand::CHeaders
:
493 emit
insertCommand( QLatin1String("%HEADERS") );
495 case TemplatesInsertCommand::COHeader
:
496 emit
insertCommand( QLatin1String("%OHEADER=\"\""), -1 );
498 case TemplatesInsertCommand::CMsgId
:
499 emit
insertCommand( QLatin1String("%MSGID") );
501 case TemplatesInsertCommand::CDate
:
502 emit
insertCommand( QLatin1String("%DATE") );
504 case TemplatesInsertCommand::CDateShort
:
505 emit
insertCommand( QLatin1String("%DATESHORT") );
507 case TemplatesInsertCommand::CDateEn
:
508 emit
insertCommand( QLatin1String("%DATEEN") );
510 case TemplatesInsertCommand::CDow
:
511 emit
insertCommand( QLatin1String("%DOW") );
513 case TemplatesInsertCommand::CTime
:
514 emit
insertCommand( QLatin1String("%TIME") );
516 case TemplatesInsertCommand::CTimeLong
:
517 emit
insertCommand( QLatin1String("%TIMELONG") );
519 case TemplatesInsertCommand::CTimeLongEn
:
520 emit
insertCommand( QLatin1String("%TIMELONGEN") );
522 case TemplatesInsertCommand::COAddresseesAddr
:
523 emit
insertCommand( QLatin1String("%OADDRESSEESADDR") );
525 case TemplatesInsertCommand::CToAddr
:
526 emit
insertCommand( QLatin1String("%TOADDR") );
528 case TemplatesInsertCommand::CToName
:
529 emit
insertCommand( QLatin1String("%TONAME") );
531 case TemplatesInsertCommand::CToFName
:
532 emit
insertCommand( QLatin1String("%TOFNAME") );
534 case TemplatesInsertCommand::CToLName
:
535 emit
insertCommand( QLatin1String("%TOLNAME") );
537 case TemplatesInsertCommand::CCCAddr
:
538 emit
insertCommand( QLatin1String("%CCADDR") );
540 case TemplatesInsertCommand::CCCName
:
541 emit
insertCommand( QLatin1String("%CCNAME") );
543 case TemplatesInsertCommand::CCCFName
:
544 emit
insertCommand( QLatin1String("%CCFNAME") );
546 case TemplatesInsertCommand::CCCLName
:
547 emit
insertCommand( QLatin1String("%CCLNAME") );
549 case TemplatesInsertCommand::CFromAddr
:
550 emit
insertCommand( QLatin1String("%FROMADDR") );
552 case TemplatesInsertCommand::CFromName
:
553 emit
insertCommand( QLatin1String("%FROMNAME") );
555 case TemplatesInsertCommand::CFromFName
:
556 emit
insertCommand( QLatin1String("%FROMFNAME") );
558 case TemplatesInsertCommand::CFromLName
:
559 emit
insertCommand( QLatin1String("%FROMLNAME") );
561 case TemplatesInsertCommand::CFullSubject
:
562 emit
insertCommand( QLatin1String("%FULLSUBJECT") );
564 case TemplatesInsertCommand::CHeader
:
565 emit
insertCommand( QLatin1String("%HEADER=\"\""), -1 );
567 case TemplatesInsertCommand::CSystem
:
568 emit
insertCommand( QLatin1String("%SYSTEM=\"\""), -1 );
570 case TemplatesInsertCommand::CQuotePipe
:
571 emit
insertCommand( QLatin1String("%QUOTEPIPE=\"\""), -1 );
573 case TemplatesInsertCommand::CTextPipe
:
574 emit
insertCommand( QLatin1String("%TEXTPIPE=\"\""), -1 );
576 case TemplatesInsertCommand::CMsgPipe
:
577 emit
insertCommand( QLatin1String("%MSGPIPE=\"\""), -1 );
579 case TemplatesInsertCommand::CBodyPipe
:
580 emit
insertCommand( QLatin1String("%BODYPIPE=\"\""), -1 );
582 case TemplatesInsertCommand::CClearPipe
:
583 emit
insertCommand( QLatin1String("%CLEARPIPE=\"\""), -1 );
585 case TemplatesInsertCommand::CCursor
:
586 emit
insertCommand( QLatin1String("%CURSOR") );
588 case TemplatesInsertCommand::CSignature
:
589 emit
insertCommand( QLatin1String("%SIGNATURE") );
591 case TemplatesInsertCommand::CInsert
:
592 emit
insertCommand( QLatin1String("%INSERT=\"\""), -1 );
594 case TemplatesInsertCommand::CDnl
:
595 emit
insertCommand( QLatin1String("%-") );
597 case TemplatesInsertCommand::CRem
:
598 emit
insertCommand( QLatin1String("%REM=\"\""), -1 );
600 case TemplatesInsertCommand::CNop
:
601 emit
insertCommand( QLatin1String("%NOP") );
603 case TemplatesInsertCommand::CClear
:
604 emit
insertCommand( QLatin1String("%CLEAR") );
606 case TemplatesInsertCommand::CDebug
:
607 emit
insertCommand( QLatin1String("%DEBUG") );
609 case TemplatesInsertCommand::CDebugOff
:
610 emit
insertCommand( QLatin1String("%DEBUGOFF") );
612 case TemplatesInsertCommand::CQuotePlain
:
613 emit
insertCommand( QLatin1String("%FORCEDPLAIN") );
615 case TemplatesInsertCommand::CQuoteHtml
:
616 emit
insertCommand( QLatin1String("%FORCEDHTML") );
619 kDebug() << "Unknown template command index:" << cmd
;
624 #include "templatesinsertcommand.moc"