Backport r950340 | aacid | 2009-04-06 23:21:18 +0200 (Mon, 06 Apr 2009) | 4 lines
[kdepim.git] / kmail / templatesconfiguration.cpp
blobd2baa61f26d0a0be15cdf8a8ad0ced7204279d3c
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 * kmail: KDE mail client
3 * Copyright (C) 2006 Dmitry Morozhnikov <dmiceman@mail.ru>
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "templatesconfiguration.h"
21 #include "ui_templatesconfiguration_base.h"
22 #include "templatesconfiguration_kfg.h"
23 #include "globalsettings.h"
24 #include "replyphrases.h"
26 #include <kdebug.h>
27 #include <klocale.h>
28 #include <kglobal.h>
30 #include <qpushbutton.h>
31 #include <qtextedit.h>
32 #include <qlineedit.h>
33 #include <QWhatsThis>
34 #include <qtoolbox.h>
35 #include <qfont.h>
37 TemplatesConfiguration::TemplatesConfiguration( QWidget *parent, const char *name )
38 : QWidget( parent )
40 setupUi(this);
41 setObjectName(name);
43 QFont f = KGlobalSettings::fixedFont();
44 textEdit_new->setFont( f );
45 textEdit_reply->setFont( f );
46 textEdit_reply_all->setFont( f );
47 textEdit_forward->setFont( f );
49 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
50 sizeHint();
52 connect( textEdit_new, SIGNAL( textChanged() ),
53 this, SLOT( slotTextChanged( void ) ) );
54 connect( textEdit_reply, SIGNAL( textChanged() ),
55 this, SLOT( slotTextChanged( void ) ) );
56 connect( textEdit_reply_all, SIGNAL( textChanged() ),
57 this, SLOT( slotTextChanged( void ) ) );
58 connect( textEdit_forward, SIGNAL( textChanged() ),
59 this, SLOT( slotTextChanged( void ) ) );
60 connect( lineEdit_quote, SIGNAL( textChanged( const QString & ) ),
61 this, SLOT( slotTextChanged( void ) ) );
63 connect( mInsertCommand, SIGNAL( insertCommand(const QString&, int) ),
64 this, SLOT( slotInsertCommand(const QString &, int) ) );
66 mHelpString =
67 i18n( "<p>Here you can create and manage templates to use when "
68 "composing new messages, replies or forwarded messages.</p>"
69 "<p>The message templates support substitution commands, "
70 "either simply type them or select them from "
71 "the <i>Insert command</i> menu.</p>" );
72 if ( QString( name ) == "folder-templates" ) {
73 mHelpString +=
74 i18n( "<p>Templates specified here are folder-specific. "
75 "They override both global templates and per-identity "
76 "templates.</p>" );
77 } else if ( QString( name ) == "identity-templates" ) {
78 mHelpString +=
79 i18n( "<p>Templates specified here are identity-specific. "
80 "They override global templates, but can be overridden by "
81 "per-folder templates if they are specified.</p>" );
82 } else {
83 mHelpString +=
84 i18n( "<p>These are global (default) templates. They can be overridden "
85 "by per-identity templates or per-folder templates "
86 "if they are specified.</p>" );
89 mHelp->setText( i18n( "<a href=\"whatsthis\">How does this work?</a>" ) );
90 connect( mHelp, SIGNAL( linkActivated ( const QString& ) ),
91 this, SLOT( slotHelpLinkClicked( const QString& ) ) );
95 void TemplatesConfiguration::slotHelpLinkClicked( const QString& )
97 QWhatsThis::showText( QCursor::pos(), mHelpString );
100 void TemplatesConfiguration::slotTextChanged()
102 emit changed();
105 void TemplatesConfiguration::loadFromGlobal()
107 if ( !GlobalSettings::self()->phrasesConverted() ) {
108 kDebug(5006) <<"Phrases to templates conversion";
109 importFromPhrases();
112 QString str;
113 str = GlobalSettings::self()->templateNewMessage();
114 if ( str.isEmpty() ) {
115 textEdit_new->setText( defaultNewMessage() );
116 } else {
117 textEdit_new->setText(str);
119 str = GlobalSettings::self()->templateReply();
120 if ( str.isEmpty() ) {
121 textEdit_reply->setText( defaultReply() );
122 } else {
123 textEdit_reply->setText( str );
125 str = GlobalSettings::self()->templateReplyAll();
126 if ( str.isEmpty() ) {
127 textEdit_reply_all->setText( defaultReplyAll() );
128 } else {
129 textEdit_reply_all->setText( str );
131 str = GlobalSettings::self()->templateForward();
132 if ( str.isEmpty() ) {
133 textEdit_forward->setText( defaultForward() );
134 } else {
135 textEdit_forward->setText( str );
137 str = GlobalSettings::self()->quoteString();
138 if ( str.isEmpty() ) {
139 lineEdit_quote->setText( defaultQuoteString() );
140 } else {
141 lineEdit_quote->setText( str );
145 void TemplatesConfiguration::saveToGlobal()
147 GlobalSettings::self()->setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
148 GlobalSettings::self()->setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
149 GlobalSettings::self()->setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
150 GlobalSettings::self()->setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
151 GlobalSettings::self()->setQuoteString( lineEdit_quote->text() );
152 GlobalSettings::self()->setPhrasesConverted( true );
153 GlobalSettings::self()->writeConfig();
156 void TemplatesConfiguration::loadFromIdentity( uint id )
158 Templates t( QString("IDENTITY_%1").arg( id ) );
160 QString str;
162 str = t.templateNewMessage();
163 if ( str.isEmpty() ) {
164 str = GlobalSettings::self()->templateNewMessage();
166 if ( str.isEmpty() ) {
167 str = defaultNewMessage();
169 textEdit_new->setText( str );
171 str = t.templateReply();
172 if ( str.isEmpty() ) {
173 str = GlobalSettings::self()->templateReply();
175 if ( str.isEmpty() ) {
176 str = defaultReply();
178 textEdit_reply->setText( str );
180 str = t.templateReplyAll();
181 if ( str.isEmpty() ) {
182 str = GlobalSettings::self()->templateReplyAll();
184 if ( str.isEmpty() ) {
185 str = defaultReplyAll();
187 textEdit_reply_all->setText( str );
189 str = t.templateForward();
190 if ( str.isEmpty() ) {
191 str = GlobalSettings::self()->templateForward();
193 if ( str.isEmpty() ) {
194 str = defaultForward();
196 textEdit_forward->setText( str );
198 str = t.quoteString();
199 if ( str.isEmpty() ) {
200 str = GlobalSettings::self()->quoteString();
202 if ( str.isEmpty() ) {
203 str = defaultQuoteString();
205 lineEdit_quote->setText( str );
208 void TemplatesConfiguration::saveToIdentity( uint id )
210 Templates t( QString("IDENTITY_%1").arg( id ) );
212 t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
213 t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
214 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
215 t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
216 t.setQuoteString( lineEdit_quote->text() );
217 t.writeConfig();
220 void TemplatesConfiguration::loadFromFolder( const QString &id, uint identity )
222 Templates t( id );
223 Templates* tid = 0;
225 if ( identity ) {
226 tid = new Templates( QString("IDENTITY_%1").arg( identity ) );
229 QString str;
231 str = t.templateNewMessage();
232 if ( str.isEmpty() && tid ) {
233 str = tid->templateNewMessage();
235 if ( str.isEmpty() ) {
236 str = GlobalSettings::self()->templateNewMessage();
238 if ( str.isEmpty() ) {
239 str = defaultNewMessage();
241 textEdit_new->setText( str );
243 str = t.templateReply();
244 if ( str.isEmpty() && tid ) {
245 str = tid->templateReply();
247 if ( str.isEmpty() ) {
248 str = GlobalSettings::self()->templateReply();
250 if ( str.isEmpty() ) {
251 str = defaultReply();
253 textEdit_reply->setText( str );
255 str = t.templateReplyAll();
256 if ( str.isEmpty() && tid ) {
257 str = tid->templateReplyAll();
259 if ( str.isEmpty() ) {
260 str = GlobalSettings::self()->templateReplyAll();
262 if ( str.isEmpty() ) {
263 str = defaultReplyAll();
265 textEdit_reply_all->setText( str );
267 str = t.templateForward();
268 if ( str.isEmpty() && tid ) {
269 str = tid->templateForward();
271 if ( str.isEmpty() ) {
272 str = GlobalSettings::self()->templateForward();
274 if ( str.isEmpty() ) {
275 str = defaultForward();
277 textEdit_forward->setText( str );
279 str = t.quoteString();
280 if ( str.isEmpty() && tid ) {
281 str = tid->quoteString();
283 if ( str.isEmpty() ) {
284 str = GlobalSettings::self()->quoteString();
286 if ( str.isEmpty() ) {
287 str = defaultQuoteString();
289 lineEdit_quote->setText( str );
291 delete(tid);
294 void TemplatesConfiguration::saveToFolder( const QString &id )
296 Templates t( id );
298 t.setTemplateNewMessage( strOrBlank( textEdit_new->toPlainText() ) );
299 t.setTemplateReply( strOrBlank( textEdit_reply->toPlainText() ) );
300 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->toPlainText() ) );
301 t.setTemplateForward( strOrBlank( textEdit_forward->toPlainText() ) );
302 t.setQuoteString( lineEdit_quote->text() );
303 t.writeConfig();
306 void TemplatesConfiguration::importFromPhrases()
308 kDebug(5006);
310 int currentNr = GlobalSettings::self()->replyCurrentLanguage();
312 ReplyPhrases replyPhrases( QString::number( currentNr ) );
314 QString str;
316 str = replyPhrases.phraseReplySender();
317 if ( !str.isEmpty() ) {
318 GlobalSettings::self()->setTemplateReply( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" );
320 else {
321 GlobalSettings::self()->setTemplateReply( defaultReply() );
324 str = replyPhrases.phraseReplyAll();
325 if ( !str.isEmpty() ) {
326 GlobalSettings::self()->setTemplateReplyAll( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" );
328 else {
329 GlobalSettings::self()->setTemplateReplyAll( defaultReplyAll() );
332 str = replyPhrases.phraseForward();
333 if ( !str.isEmpty() ) {
334 GlobalSettings::self()->setTemplateForward(
335 "%REM=\"" + i18n( "Default forward template" ) + "\"%-\n" +
336 i18nc( "Default template for forwarded messages."
337 "%1: forward phrase, e.g. \"Forwarded Message\", "
338 "%2: subject of original message, %3: date of original message, "
339 "%4: mail address of sender of original message, "
340 "%5: mail address of receiver of original message, "
341 "%6: text of original message",
342 "\n"
343 "---------- %1 ----------\n"
344 "\n"
345 "Subject: %2\n"
346 "Date: %3\n"
347 "From: %4\n"
348 "To: %5\n"
349 "\n"
350 "%6\n"
351 "-------------------------------------------------------\n",
352 convertPhrases( str ), "%OFULLSUBJECT", "%ODATE", "%OFROMADDR",
353 "%OTOADDR", "%TEXT" ) );
355 else {
356 GlobalSettings::self()->setTemplateForward( defaultForward() );
359 str = replyPhrases.indentPrefix();
360 if ( !str.isEmpty() ) {
361 // no need to convert indentPrefix() because it is passed to KMMessage::asQuotedString() as is
362 GlobalSettings::self()->setQuoteString( str );
364 else {
365 GlobalSettings::self()->setQuoteString( defaultQuoteString() );
368 GlobalSettings::self()->setPhrasesConverted( true );
369 GlobalSettings::self()->writeConfig();
372 QString TemplatesConfiguration::convertPhrases( const QString &str )
374 QString result;
375 QChar ch;
377 unsigned int strLength( str.length() );
378 for ( uint i = 0; i < strLength; ) {
379 ch = str[i++];
380 if ( ch == '%' ) {
381 ch = str[i++];
382 switch ( ch.toAscii() ) {
383 case 'D':
384 result += "%ODATE";
385 break;
386 case 'e':
387 result += "%OFROMADDR";
388 break;
389 case 'F':
390 result += "%OFROMNAME";
391 break;
392 case 'f':
393 // is this used for something like FIDO quotes, like "AB>" ?
394 // not supported right now
395 break;
396 case 'T':
397 result += "%OTONAME";
398 break;
399 case 't':
400 result += "%OTOADDR";
401 break;
402 case 'C':
403 result += "%OCCNAME";
404 break;
405 case 'c':
406 result += "%OCCADDR";
407 break;
408 case 'S':
409 result += "%OFULLSUBJECT";
410 break;
411 case '_':
412 result += ' ';
413 break;
414 case 'L':
415 result += '\n';
416 break;
417 case '%':
418 result += "%%";
419 break;
420 default:
421 result += '%';
422 result += ch;
423 break;
425 } else
426 result += ch;
428 return result;
431 void TemplatesConfiguration::slotInsertCommand( const QString &cmd, int adjustCursor )
433 KTextEdit* edit;
435 if( toolBox1->widget( toolBox1->currentIndex() ) == page_new ) {
436 edit = textEdit_new;
437 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_reply ) {
438 edit = textEdit_reply;
439 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_reply_all ) {
440 edit = textEdit_reply_all;
441 } else if( toolBox1->widget( toolBox1->currentIndex() ) == page_forward ) {
442 edit = textEdit_forward;
443 } else {
444 kDebug(5006) <<"Unknown current page in TemplatesConfiguration!";
445 return;
448 // kDebug(5006) <<"Insert command:" << cmd;
449 QTextCursor cursor = edit->textCursor();
450 cursor.insertText( cmd );
451 cursor.setPosition( cursor.position() + adjustCursor );
452 edit->setTextCursor( cursor );
453 edit->setFocus();
456 QString TemplatesConfiguration::defaultNewMessage() {
457 return "%REM=\"" + i18n( "Default new message template" ) + "\"%-\n"
458 "%BLANK";
461 QString TemplatesConfiguration::defaultReply() {
462 return
463 "%REM=\"" + i18n( "Default reply template" ) + "\"%-\n" +
464 i18nc( "Default reply template."
465 "%1: date of original message, %2: time of original message, "
466 "%3: quoted text of original message, %4: cursor Position",
467 "On %1 %2 you wrote:\n"
468 "%3\n"
469 "%4", "%ODATE", "%OTIMELONG", "%QUOTE", "%CURSOR" );
472 QString TemplatesConfiguration::defaultReplyAll() {
473 return
474 "%REM=\"" + i18n( "Default reply all template" ) + "\"%-\n" +
475 i18nc( "Default reply all template: %1: date, %2: time, %3: name of original sender, "
476 "%4: quoted text of original message, %5: cursor position",
477 "On %1 %2 %3 wrote:\n"
478 "%4\n"
479 "%5",
480 "%ODATE", "%OTIMELONG", "%OFROMNAME", "%QUOTE", "%CURSOR");
483 QString TemplatesConfiguration::defaultForward() {
484 return
485 "%REM=\"" + i18n( "Default forward template" ) + "\"%-\n" +
486 i18nc( "Default forward template: %1: subject of original message, "
487 "%2: date of original message, "
488 "%3: mail address of original sender, "
489 "%4: mail address of original receiver, "
490 "%5: original message text",
491 "\n"
492 "---------- Forwarded Message ----------\n"
493 "\n"
494 "Subject: %1\n"
495 "Date: %2\n"
496 "From: %3\n"
497 "To: %4\n"
498 "\n"
499 "%5\n"
500 "-------------------------------------------------------",
501 "%OFULLSUBJECT", "%ODATE", "%OFROMADDR", "%OTOADDR", "%TEXT" );
504 QString TemplatesConfiguration::defaultQuoteString() {
505 return "> ";
508 QString TemplatesConfiguration::strOrBlank( const QString &str ) {
509 if ( str.trimmed().isEmpty() ) {
510 return QString( "%BLANK" );
512 return str;
515 #include "templatesconfiguration.moc"