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"
30 #include <qpushbutton.h>
31 #include <qtextedit.h>
32 #include <qlineedit.h>
37 TemplatesConfiguration::TemplatesConfiguration( QWidget
*parent
, const char *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
);
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) ) );
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" ) {
74 i18n( "<p>Templates specified here are folder-specific. "
75 "They override both global templates and per-identity "
77 } else if ( QString( name
) == "identity-templates" ) {
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>" );
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()
105 void TemplatesConfiguration::loadFromGlobal()
107 if ( !GlobalSettings::self()->phrasesConverted() ) {
108 kDebug(5006) <<"Phrases to templates conversion";
113 str
= GlobalSettings::self()->templateNewMessage();
114 if ( str
.isEmpty() ) {
115 textEdit_new
->setText( defaultNewMessage() );
117 textEdit_new
->setText(str
);
119 str
= GlobalSettings::self()->templateReply();
120 if ( str
.isEmpty() ) {
121 textEdit_reply
->setText( defaultReply() );
123 textEdit_reply
->setText( str
);
125 str
= GlobalSettings::self()->templateReplyAll();
126 if ( str
.isEmpty() ) {
127 textEdit_reply_all
->setText( defaultReplyAll() );
129 textEdit_reply_all
->setText( str
);
131 str
= GlobalSettings::self()->templateForward();
132 if ( str
.isEmpty() ) {
133 textEdit_forward
->setText( defaultForward() );
135 textEdit_forward
->setText( str
);
137 str
= GlobalSettings::self()->quoteString();
138 if ( str
.isEmpty() ) {
139 lineEdit_quote
->setText( defaultQuoteString() );
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
) );
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() );
220 void TemplatesConfiguration::loadFromFolder( const QString
&id
, uint identity
)
226 tid
= new Templates( QString("IDENTITY_%1").arg( identity
) );
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
);
294 void TemplatesConfiguration::saveToFolder( const QString
&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() );
306 void TemplatesConfiguration::importFromPhrases()
310 int currentNr
= GlobalSettings::self()->replyCurrentLanguage();
312 ReplyPhrases
replyPhrases( QString::number( currentNr
) );
316 str
= replyPhrases
.phraseReplySender();
317 if ( !str
.isEmpty() ) {
318 GlobalSettings::self()->setTemplateReply( convertPhrases( str
) + "\n%QUOTE\n%CURSOR\n" );
321 GlobalSettings::self()->setTemplateReply( defaultReply() );
324 str
= replyPhrases
.phraseReplyAll();
325 if ( !str
.isEmpty() ) {
326 GlobalSettings::self()->setTemplateReplyAll( convertPhrases( str
) + "\n%QUOTE\n%CURSOR\n" );
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",
343 "---------- %1 ----------\n"
351 "-------------------------------------------------------\n",
352 convertPhrases( str
), "%OFULLSUBJECT", "%ODATE", "%OFROMADDR",
353 "%OTOADDR", "%TEXT" ) );
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
);
365 GlobalSettings::self()->setQuoteString( defaultQuoteString() );
368 GlobalSettings::self()->setPhrasesConverted( true );
369 GlobalSettings::self()->writeConfig();
372 QString
TemplatesConfiguration::convertPhrases( const QString
&str
)
377 unsigned int strLength( str
.length() );
378 for ( uint i
= 0; i
< strLength
; ) {
382 switch ( ch
.toAscii() ) {
387 result
+= "%OFROMADDR";
390 result
+= "%OFROMNAME";
393 // is this used for something like FIDO quotes, like "AB>" ?
394 // not supported right now
397 result
+= "%OTONAME";
400 result
+= "%OTOADDR";
403 result
+= "%OCCNAME";
406 result
+= "%OCCADDR";
409 result
+= "%OFULLSUBJECT";
431 void TemplatesConfiguration::slotInsertCommand( const QString
&cmd
, int adjustCursor
)
435 if( toolBox1
->widget( toolBox1
->currentIndex() ) == page_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
;
444 kDebug(5006) <<"Unknown current page in TemplatesConfiguration!";
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
);
456 QString
TemplatesConfiguration::defaultNewMessage() {
457 return "%REM=\"" + i18n( "Default new message template" ) + "\"%-\n"
461 QString
TemplatesConfiguration::defaultReply() {
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"
469 "%4", "%ODATE", "%OTIMELONG", "%QUOTE", "%CURSOR" );
472 QString
TemplatesConfiguration::defaultReplyAll() {
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"
480 "%ODATE", "%OTIMELONG", "%OFROMNAME", "%QUOTE", "%CURSOR");
483 QString
TemplatesConfiguration::defaultForward() {
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",
492 "---------- Forwarded Message ----------\n"
500 "-------------------------------------------------------",
501 "%OFULLSUBJECT", "%ODATE", "%OFROMADDR", "%OTOADDR", "%TEXT" );
504 QString
TemplatesConfiguration::defaultQuoteString() {
508 QString
TemplatesConfiguration::strOrBlank( const QString
&str
) {
509 if ( str
.trimmed().isEmpty() ) {
510 return QString( "%BLANK" );
515 #include "templatesconfiguration.moc"