kmail

templatesconfiguration.cpp
1/*
2 * kmail: KDE mail client
3 * This file: Copyright (C) 2006 Dmitry Morozhnikov
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include <config.h>
22
23#include <tdelocale.h>
24#include <tdeglobal.h>
25#include <tqpopupmenu.h>
26#include <tqpushbutton.h>
27#include <tqtextedit.h>
28#include <tqlineedit.h>
29#include <tqtoolbox.h>
30#include <kdebug.h>
31#include <tqfont.h>
32#include <kactivelabel.h>
33
34#include "templatesconfiguration_base.h"
35#include "templatesconfiguration_kfg.h"
36#include "globalsettings.h"
37#include "replyphrases.h"
38
39#include "templatesconfiguration.h"
40
41TemplatesConfiguration::TemplatesConfiguration( TQWidget *parent, const char *name )
42 :TemplatesConfigurationBase( parent, name )
43{
44 TQFont f = TDEGlobalSettings::fixedFont();
45 textEdit_new->setFont( f );
46 textEdit_reply->setFont( f );
47 textEdit_reply_all->setFont( f );
48 textEdit_forward->setFont( f );
49
50 setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding );
51 sizeHint();
52
53 connect( textEdit_new, TQ_SIGNAL( textChanged() ),
54 this, TQ_SLOT( slotTextChanged( void ) ) );
55 connect( textEdit_reply, TQ_SIGNAL( textChanged() ),
56 this, TQ_SLOT( slotTextChanged( void ) ) );
57 connect( textEdit_reply_all, TQ_SIGNAL( textChanged() ),
58 this, TQ_SLOT( slotTextChanged( void ) ) );
59 connect( textEdit_forward, TQ_SIGNAL( textChanged() ),
60 this, TQ_SLOT( slotTextChanged( void ) ) );
61 connect( lineEdit_quote, TQ_SIGNAL( textChanged( const TQString & ) ),
62 this, TQ_SLOT( slotTextChanged( void ) ) );
63
64 connect( mInsertCommand, TQ_SIGNAL( insertCommand(TQString, int) ),
65 this, TQ_SLOT( slotInsertCommand(TQString, int) ) );
66
67 TQString help;
68 if ( TQString( name ) == "folder-templates" ) {
69 help =
70 i18n( "<qt>"
71 "<p>Here you can create message templates to use when you "
72 "compose new messages or replies, or when you forward messages.</p>"
73 "<p>The message templates support substitution commands "
74 "by simple typing them or selecting them from menu "
75 "<i>Insert command</i>.</p>"
76 "<p>Templates specified here are folder-specific. "
77 "They override both global templates and per-identity "
78 "templates if they are specified.</p>"
79 "</qt>" );
80 } else if ( TQString( name ) == "identity-templates" ) {
81 help =
82 i18n( "<qt>"
83 "<p>Here you can create message templates to use when you "
84 "compose new messages or replies, or when you forward messages.</p>"
85 "<p>The message templates support substitution commands "
86 "by simple typing them or selecting them from menu "
87 "<i>Insert command</i>.</p>"
88 "<p>Templates specified here are mail identity-wide. "
89 "They override global templates and are being overridden by per-folder "
90 "templates if they are specified.</p>"
91 "</qt>" );
92 } else {
93 help =
94 i18n( "<qt>"
95 "<p>Here you can create message templates to use when you "
96 "compose new messages or replies, or when you forward messages.</p>"
97 "<p>The message templates support substitution commands "
98 "by simple typing them or selecting them from menu "
99 "<i>Insert command</i>.</p>"
100 "<p>This is a global (default) template. They can be overridden "
101 "by per-identity templates and by per-folder templates "
102 "if they are specified.</p>"
103 "</qt>" );
104 }
105 mHelp->setText( i18n( "<a href=\"whatsthis:%1\">How does this work?</a>" ).arg( help ) );
106}
107
108void TemplatesConfiguration::slotTextChanged()
109{
110 emit changed();
111}
112
113void TemplatesConfiguration::loadFromGlobal()
114{
115 if ( !GlobalSettings::self()->phrasesConverted() ) {
116 kdDebug() << "Phrases to templates conversion" << endl;
117 importFromPhrases();
118 }
119
120 TQString str;
121 str = GlobalSettings::self()->templateNewMessage();
122 if ( str.isEmpty() ) {
123 textEdit_new->setText( defaultNewMessage() );
124 } else {
125 textEdit_new->setText(str);
126 }
127 str = GlobalSettings::self()->templateReply();
128 if ( str.isEmpty() ) {
129 textEdit_reply->setText( defaultReply() );
130 } else {
131 textEdit_reply->setText( str );
132 }
133 str = GlobalSettings::self()->templateReplyAll();
134 if ( str.isEmpty() ) {
135 textEdit_reply_all->setText( defaultReplyAll() );
136 } else {
137 textEdit_reply_all->setText( str );
138 }
139 str = GlobalSettings::self()->templateForward();
140 if ( str.isEmpty() ) {
141 textEdit_forward->setText( defaultForward() );
142 } else {
143 textEdit_forward->setText( str );
144 }
145 str = GlobalSettings::self()->quoteString();
146 if ( str.isEmpty() ) {
147 lineEdit_quote->setText( defaultQuoteString() );
148 } else {
149 lineEdit_quote->setText( str );
150 }
151}
152
153void TemplatesConfiguration::saveToGlobal()
154{
155 GlobalSettings::self()->setTemplateNewMessage( strOrBlank( textEdit_new->text() ) );
156 GlobalSettings::self()->setTemplateReply( strOrBlank( textEdit_reply->text() ) );
157 GlobalSettings::self()->setTemplateReplyAll( strOrBlank( textEdit_reply_all->text() ) );
158 GlobalSettings::self()->setTemplateForward( strOrBlank( textEdit_forward->text() ) );
159 GlobalSettings::self()->setQuoteString( lineEdit_quote->text() );
160 GlobalSettings::self()->setPhrasesConverted( true );
161 GlobalSettings::self()->writeConfig();
162}
163
164void TemplatesConfiguration::loadFromIdentity( uint id )
165{
166 Templates t( TQString("IDENTITY_%1").arg( id ) );
167
168 TQString str;
169
170 str = t.templateNewMessage();
171 if ( str.isEmpty() ) {
172 str = GlobalSettings::self()->templateNewMessage();
173 }
174 if ( str.isEmpty() ) {
175 str = defaultNewMessage();
176 }
177 textEdit_new->setText( str );
178
179 str = t.templateReply();
180 if ( str.isEmpty() ) {
181 str = GlobalSettings::self()->templateReply();
182 }
183 if ( str.isEmpty() ) {
184 str = defaultReply();
185 }
186 textEdit_reply->setText( str );
187
188 str = t.templateReplyAll();
189 if ( str.isEmpty() ) {
190 str = GlobalSettings::self()->templateReplyAll();
191 }
192 if ( str.isEmpty() ) {
193 str = defaultReplyAll();
194 }
195 textEdit_reply_all->setText( str );
196
197 str = t.templateForward();
198 if ( str.isEmpty() ) {
199 str = GlobalSettings::self()->templateForward();
200 }
201 if ( str.isEmpty() ) {
202 str = defaultForward();
203 }
204 textEdit_forward->setText( str );
205
206 str = t.quoteString();
207 if ( str.isEmpty() ) {
208 str = GlobalSettings::self()->quoteString();
209 }
210 if ( str.isEmpty() ) {
211 str = defaultQuoteString();
212 }
213 lineEdit_quote->setText( str );
214}
215
216void TemplatesConfiguration::saveToIdentity( uint id )
217{
218 Templates t( TQString("IDENTITY_%1").arg( id ) );
219
220 t.setTemplateNewMessage( strOrBlank( textEdit_new->text() ) );
221 t.setTemplateReply( strOrBlank( textEdit_reply->text() ) );
222 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->text() ) );
223 t.setTemplateForward( strOrBlank( textEdit_forward->text() ) );
224 t.setQuoteString( lineEdit_quote->text() );
225 t.writeConfig();
226}
227
228void TemplatesConfiguration::loadFromFolder( TQString id, uint identity )
229{
230 Templates t( id );
231 Templates* tid = 0;
232
233 if ( identity ) {
234 tid = new Templates( TQString("IDENTITY_%1").arg( identity ) );
235 }
236
237 TQString str;
238
239 str = t.templateNewMessage();
240 if ( str.isEmpty() && tid ) {
241 str = tid->templateNewMessage();
242 }
243 if ( str.isEmpty() ) {
244 str = GlobalSettings::self()->templateNewMessage();
245 }
246 if ( str.isEmpty() ) {
247 str = defaultNewMessage();
248 }
249 textEdit_new->setText( str );
250
251 str = t.templateReply();
252 if ( str.isEmpty() && tid ) {
253 str = tid->templateReply();
254 }
255 if ( str.isEmpty() ) {
256 str = GlobalSettings::self()->templateReply();
257 }
258 if ( str.isEmpty() ) {
259 str = defaultReply();
260 }
261 textEdit_reply->setText( str );
262
263 str = t.templateReplyAll();
264 if ( str.isEmpty() && tid ) {
265 str = tid->templateReplyAll();
266 }
267 if ( str.isEmpty() ) {
268 str = GlobalSettings::self()->templateReplyAll();
269 }
270 if ( str.isEmpty() ) {
271 str = defaultReplyAll();
272 }
273 textEdit_reply_all->setText( str );
274
275 str = t.templateForward();
276 if ( str.isEmpty() && tid ) {
277 str = tid->templateForward();
278 }
279 if ( str.isEmpty() ) {
280 str = GlobalSettings::self()->templateForward();
281 }
282 if ( str.isEmpty() ) {
283 str = defaultForward();
284 }
285 textEdit_forward->setText( str );
286
287 str = t.quoteString();
288 if ( str.isEmpty() && tid ) {
289 str = tid->quoteString();
290 }
291 if ( str.isEmpty() ) {
292 str = GlobalSettings::self()->quoteString();
293 }
294 if ( str.isEmpty() ) {
295 str = defaultQuoteString();
296 }
297 lineEdit_quote->setText( str );
298
299 delete(tid);
300}
301
302void TemplatesConfiguration::saveToFolder( TQString id )
303{
304 Templates t( id );
305
306 t.setTemplateNewMessage( strOrBlank( textEdit_new->text() ) );
307 t.setTemplateReply( strOrBlank( textEdit_reply->text() ) );
308 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->text() ) );
309 t.setTemplateForward( strOrBlank( textEdit_forward->text() ) );
310 t.setQuoteString( lineEdit_quote->text() );
311 t.writeConfig();
312}
313
314void TemplatesConfiguration::loadFromPhrases()
315{
316 int currentNr = GlobalSettings::self()->replyCurrentLanguage();
317
318 ReplyPhrases replyPhrases( TQString::number( currentNr ) );
319
320 textEdit_new->setText( defaultNewMessage() );
321
322 TQString str;
323
324 str = replyPhrases.phraseReplySender();
325 if ( !str.isEmpty() ) {
326 textEdit_reply->setText( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" );
327 }
328 else {
329 textEdit_reply->setText( defaultReply() );
330 }
331
332 str = replyPhrases.phraseReplyAll();
333 if ( !str.isEmpty() ) {
334 textEdit_reply_all->setText( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" );
335 }
336 else {
337 textEdit_reply_all->setText( defaultReplyAll() );
338 }
339
340 str = replyPhrases.phraseForward();
341 if ( !str.isEmpty() ) {
342 textEdit_forward->setText( TQString( i18n(
343 "%REM=\"Default forward template\"%-\n"
344 "---------- %1 ----------\n"
345 "%TEXT\n"
346 "-------------------------------------------------------\n"
347 ) ).arg( convertPhrases( str ) ) );
348 }
349 else {
350 textEdit_forward->setText( defaultForward() );
351 }
352
353 str = replyPhrases.indentPrefix();
354 if ( !str.isEmpty() ) {
355 // no need to convert indentPrefix() because it is passed to KMMessage::asQuotedString() as is
356 lineEdit_quote->setText( str );
357 }
358 else {
359 lineEdit_quote->setText( defaultQuoteString() );
360 }
361}
362
363void TemplatesConfiguration::importFromPhrases()
364{
365 kdDebug() << "TemplatesConfiguration::importFromPhrases()" << endl;
366
367 int currentNr = GlobalSettings::self()->replyCurrentLanguage();
368
369 ReplyPhrases replyPhrases( TQString::number( currentNr ) );
370
371 TQString str;
372
373 str = replyPhrases.phraseReplySender();
374 if ( !str.isEmpty() ) {
375 GlobalSettings::self()->setTemplateReply( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" );
376 }
377 else {
378 GlobalSettings::self()->setTemplateReply( defaultReply() );
379 }
380
381 str = replyPhrases.phraseReplyAll();
382 if ( !str.isEmpty() ) {
383 GlobalSettings::self()->setTemplateReplyAll( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" );
384 }
385 else {
386 GlobalSettings::self()->setTemplateReplyAll( defaultReplyAll() );
387 }
388
389 str = replyPhrases.phraseForward();
390 if ( !str.isEmpty() ) {
391 GlobalSettings::self()->setTemplateForward( TQString( i18n(
392 "%REM=\"Default forward template\"%-\n"
393 "\n"
394 "---------- %1 ----------\n"
395 "\n"
396 "Subject: %OFULLSUBJECT\n"
397 "Date: %ODATE, %OTIMELONG\n"
398 "From: %OFROMADDR\n"
399 "%OADDRESSEESADDR\n"
400 "\n"
401 "%TEXT\n"
402 "-------------------------------------------------------\n"
403 ) ).arg( convertPhrases( str ) ) );
404 }
405 else {
406 GlobalSettings::self()->setTemplateForward( defaultForward() );
407 }
408
409 str = replyPhrases.indentPrefix();
410 if ( !str.isEmpty() ) {
411 // no need to convert indentPrefix() because it is passed to KMMessage::asQuotedString() as is
412 GlobalSettings::self()->setQuoteString( str );
413 }
414 else {
415 GlobalSettings::self()->setQuoteString( defaultQuoteString() );
416 }
417
418 GlobalSettings::self()->setPhrasesConverted( true );
419 GlobalSettings::self()->writeConfig();
420}
421
422TQString TemplatesConfiguration::convertPhrases( TQString &str )
423{
424 TQString result;
425 TQChar ch;
426
427 unsigned int strLength( str.length() );
428 for ( uint i = 0; i < strLength; ) {
429 ch = str[i++];
430 if ( ch == '%' ) {
431 ch = str[i++];
432 switch ( (char)ch ) {
433 case 'D':
434 result += "%ODATE";
435 break;
436 case 'e':
437 result += "%OFROMADDR";
438 break;
439 case 'F':
440 result += "%OFROMNAME";
441 break;
442 case 'f':
443 // is this used for something like FIDO quotes, like "AB>" ?
444 // not supported right now
445 break;
446 case 'T':
447 result += "%OTONAME";
448 break;
449 case 't':
450 result += "%OTOADDR";
451 break;
452 case 'C':
453 result += "%OCCNAME";
454 break;
455 case 'c':
456 result += "%OCCADDR";
457 break;
458 case 'S':
459 result += "%OFULLSUBJECT";
460 break;
461 case '_':
462 result += ' ';
463 break;
464 case 'L':
465 result += "\n";
466 break;
467 case '%':
468 result += "%%";
469 break;
470 default:
471 result += '%';
472 result += ch;
473 break;
474 }
475 } else
476 result += ch;
477 }
478 return result;
479}
480
481void TemplatesConfiguration::slotInsertCommand( TQString cmd, int adjustCursor )
482{
483 TQTextEdit* edit;
484
485 if( toolBox1->currentItem() == page_new ) {
486 edit = textEdit_new;
487 } else if( toolBox1->currentItem() == page_reply ) {
488 edit = textEdit_reply;
489 } else if( toolBox1->currentItem() == page_reply_all ) {
490 edit = textEdit_reply_all;
491 } else if( toolBox1->currentItem() == page_forward ) {
492 edit = textEdit_forward;
493 } else {
494 kdDebug() << "Unknown current page in TemplatesConfiguration!" << endl;
495 return;
496 }
497
498 // kdDebug() << "Insert command: " << cmd << endl;
499
500 int para, index;
501 edit->getCursorPosition( &para, &index );
502 edit->insertAt( cmd, para, index );
503
504 index += adjustCursor;
505
506 edit->setCursorPosition( para, index + cmd.length() );
507}
508
509TQString TemplatesConfiguration::defaultNewMessage() {
510 return i18n(
511 "%REM=\"Default new message template\"%-\n"
512 "%BLANK\n"
513 "%BLANK\n"
514 "%BLANK\n"
515 );
516}
517
518TQString TemplatesConfiguration::defaultReply() {
519 return i18n(
520 "%CURSOR\n"
521 "%BLANK\n"
522 "%REM=\"Default reply template\"%-\n"
523 "On %ODATEEN %OTIMELONGEN you wrote:\n"
524 "%QUOTE\n"
525 );
526}
527
528TQString TemplatesConfiguration::defaultReplyAll() {
529 return i18n(
530 "%CURSOR\n"
531 "%BLANK\n"
532 "%REM=\"Default reply all template\"%-\n"
533 "On %ODATEEN %OTIMELONGEN %OFROMNAME wrote:\n"
534 "%QUOTE\n"
535 );
536}
537
538TQString TemplatesConfiguration::defaultForward()
539{
540 return i18n(
541 "%REM=\"Default forward template\"%-\n"
542 "\n"
543 "---------- Forwarded Message ----------\n"
544 "\n"
545 "Subject: %OFULLSUBJECT\n"
546 "Date: %ODATE, %OTIMELONG\n"
547 "From: %OFROMADDR\n"
548 "%OADDRESSEESADDR\n"
549 "\n"
550 "%TEXT\n"
551 "-------------------------------------------------------\n"
552 );
553}
554
555TQString TemplatesConfiguration::defaultQuoteString() {
556 return "> ";
557}
558
559TQString TemplatesConfiguration::strOrBlank( TQString str ) {
560 if ( str.stripWhiteSpace().isEmpty() ) {
561 return TQString( "%BLANK" );
562 }
563 return str;
564}
565
566#include "templatesconfiguration.moc"