templatesconfiguration.cpp
00001 /* -*- mode: C++; c-file-style: "gnu" -*- 00002 * kmail: KDE mail client 00003 * This file: Copyright (C) 2006 Dmitry Morozhnikov 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 * 00019 */ 00020 00021 #include <config.h> 00022 00023 #include <tdelocale.h> 00024 #include <tdeglobal.h> 00025 #include <tqpopupmenu.h> 00026 #include <tqpushbutton.h> 00027 #include <tqtextedit.h> 00028 #include <tqlineedit.h> 00029 #include <tqtoolbox.h> 00030 #include <kdebug.h> 00031 #include <tqfont.h> 00032 #include <kactivelabel.h> 00033 00034 #include "templatesconfiguration_base.h" 00035 #include "templatesconfiguration_kfg.h" 00036 #include "globalsettings.h" 00037 #include "replyphrases.h" 00038 00039 #include "templatesconfiguration.h" 00040 00041 TemplatesConfiguration::TemplatesConfiguration( TQWidget *parent, const char *name ) 00042 :TemplatesConfigurationBase( parent, name ) 00043 { 00044 TQFont f = TDEGlobalSettings::fixedFont(); 00045 textEdit_new->setFont( f ); 00046 textEdit_reply->setFont( f ); 00047 textEdit_reply_all->setFont( f ); 00048 textEdit_forward->setFont( f ); 00049 00050 setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ); 00051 sizeHint(); 00052 00053 connect( textEdit_new, TQT_SIGNAL( textChanged() ), 00054 this, TQT_SLOT( slotTextChanged( void ) ) ); 00055 connect( textEdit_reply, TQT_SIGNAL( textChanged() ), 00056 this, TQT_SLOT( slotTextChanged( void ) ) ); 00057 connect( textEdit_reply_all, TQT_SIGNAL( textChanged() ), 00058 this, TQT_SLOT( slotTextChanged( void ) ) ); 00059 connect( textEdit_forward, TQT_SIGNAL( textChanged() ), 00060 this, TQT_SLOT( slotTextChanged( void ) ) ); 00061 connect( lineEdit_quote, TQT_SIGNAL( textChanged( const TQString & ) ), 00062 this, TQT_SLOT( slotTextChanged( void ) ) ); 00063 00064 connect( mInsertCommand, TQT_SIGNAL( insertCommand(TQString, int) ), 00065 this, TQT_SLOT( slotInsertCommand(TQString, int) ) ); 00066 00067 TQString help; 00068 if ( TQString( name ) == "folder-templates" ) { 00069 help = 00070 i18n( "<qt>" 00071 "<p>Here you can create message templates to use when you " 00072 "compose new messages or replies, or when you forward messages.</p>" 00073 "<p>The message templates support substitution commands " 00074 "by simple typing them or selecting them from menu " 00075 "<i>Insert command</i>.</p>" 00076 "<p>Templates specified here are folder-specific. " 00077 "They override both global templates and per-identity " 00078 "templates if they are specified.</p>" 00079 "</qt>" ); 00080 } else if ( TQString( name ) == "identity-templates" ) { 00081 help = 00082 i18n( "<qt>" 00083 "<p>Here you can create message templates to use when you " 00084 "compose new messages or replies, or when you forward messages.</p>" 00085 "<p>The message templates support substitution commands " 00086 "by simple typing them or selecting them from menu " 00087 "<i>Insert command</i>.</p>" 00088 "<p>Templates specified here are mail identity-wide. " 00089 "They override global templates and are being overridden by per-folder " 00090 "templates if they are specified.</p>" 00091 "</qt>" ); 00092 } else { 00093 help = 00094 i18n( "<qt>" 00095 "<p>Here you can create message templates to use when you " 00096 "compose new messages or replies, or when you forward messages.</p>" 00097 "<p>The message templates support substitution commands " 00098 "by simple typing them or selecting them from menu " 00099 "<i>Insert command</i>.</p>" 00100 "<p>This is a global (default) template. They can be overridden " 00101 "by per-identity templates and by per-folder templates " 00102 "if they are specified.</p>" 00103 "</qt>" ); 00104 } 00105 mHelp->setText( i18n( "<a href=\"whatsthis:%1\">How does this work?</a>" ).arg( help ) ); 00106 } 00107 00108 void TemplatesConfiguration::slotTextChanged() 00109 { 00110 emit changed(); 00111 } 00112 00113 void TemplatesConfiguration::loadFromGlobal() 00114 { 00115 if ( !GlobalSettings::self()->phrasesConverted() ) { 00116 kdDebug() << "Phrases to templates conversion" << endl; 00117 importFromPhrases(); 00118 } 00119 00120 TQString str; 00121 str = GlobalSettings::self()->templateNewMessage(); 00122 if ( str.isEmpty() ) { 00123 textEdit_new->setText( defaultNewMessage() ); 00124 } else { 00125 textEdit_new->setText(str); 00126 } 00127 str = GlobalSettings::self()->templateReply(); 00128 if ( str.isEmpty() ) { 00129 textEdit_reply->setText( defaultReply() ); 00130 } else { 00131 textEdit_reply->setText( str ); 00132 } 00133 str = GlobalSettings::self()->templateReplyAll(); 00134 if ( str.isEmpty() ) { 00135 textEdit_reply_all->setText( defaultReplyAll() ); 00136 } else { 00137 textEdit_reply_all->setText( str ); 00138 } 00139 str = GlobalSettings::self()->templateForward(); 00140 if ( str.isEmpty() ) { 00141 textEdit_forward->setText( defaultForward() ); 00142 } else { 00143 textEdit_forward->setText( str ); 00144 } 00145 str = GlobalSettings::self()->quoteString(); 00146 if ( str.isEmpty() ) { 00147 lineEdit_quote->setText( defaultQuoteString() ); 00148 } else { 00149 lineEdit_quote->setText( str ); 00150 } 00151 } 00152 00153 void TemplatesConfiguration::saveToGlobal() 00154 { 00155 GlobalSettings::self()->setTemplateNewMessage( strOrBlank( textEdit_new->text() ) ); 00156 GlobalSettings::self()->setTemplateReply( strOrBlank( textEdit_reply->text() ) ); 00157 GlobalSettings::self()->setTemplateReplyAll( strOrBlank( textEdit_reply_all->text() ) ); 00158 GlobalSettings::self()->setTemplateForward( strOrBlank( textEdit_forward->text() ) ); 00159 GlobalSettings::self()->setQuoteString( lineEdit_quote->text() ); 00160 GlobalSettings::self()->setPhrasesConverted( true ); 00161 GlobalSettings::self()->writeConfig(); 00162 } 00163 00164 void TemplatesConfiguration::loadFromIdentity( uint id ) 00165 { 00166 Templates t( TQString("IDENTITY_%1").arg( id ) ); 00167 00168 TQString str; 00169 00170 str = t.templateNewMessage(); 00171 if ( str.isEmpty() ) { 00172 str = GlobalSettings::self()->templateNewMessage(); 00173 } 00174 if ( str.isEmpty() ) { 00175 str = defaultNewMessage(); 00176 } 00177 textEdit_new->setText( str ); 00178 00179 str = t.templateReply(); 00180 if ( str.isEmpty() ) { 00181 str = GlobalSettings::self()->templateReply(); 00182 } 00183 if ( str.isEmpty() ) { 00184 str = defaultReply(); 00185 } 00186 textEdit_reply->setText( str ); 00187 00188 str = t.templateReplyAll(); 00189 if ( str.isEmpty() ) { 00190 str = GlobalSettings::self()->templateReplyAll(); 00191 } 00192 if ( str.isEmpty() ) { 00193 str = defaultReplyAll(); 00194 } 00195 textEdit_reply_all->setText( str ); 00196 00197 str = t.templateForward(); 00198 if ( str.isEmpty() ) { 00199 str = GlobalSettings::self()->templateForward(); 00200 } 00201 if ( str.isEmpty() ) { 00202 str = defaultForward(); 00203 } 00204 textEdit_forward->setText( str ); 00205 00206 str = t.quoteString(); 00207 if ( str.isEmpty() ) { 00208 str = GlobalSettings::self()->quoteString(); 00209 } 00210 if ( str.isEmpty() ) { 00211 str = defaultQuoteString(); 00212 } 00213 lineEdit_quote->setText( str ); 00214 } 00215 00216 void TemplatesConfiguration::saveToIdentity( uint id ) 00217 { 00218 Templates t( TQString("IDENTITY_%1").arg( id ) ); 00219 00220 t.setTemplateNewMessage( strOrBlank( textEdit_new->text() ) ); 00221 t.setTemplateReply( strOrBlank( textEdit_reply->text() ) ); 00222 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->text() ) ); 00223 t.setTemplateForward( strOrBlank( textEdit_forward->text() ) ); 00224 t.setQuoteString( lineEdit_quote->text() ); 00225 t.writeConfig(); 00226 } 00227 00228 void TemplatesConfiguration::loadFromFolder( TQString id, uint identity ) 00229 { 00230 Templates t( id ); 00231 Templates* tid = 0; 00232 00233 if ( identity ) { 00234 tid = new Templates( TQString("IDENTITY_%1").arg( identity ) ); 00235 } 00236 00237 TQString str; 00238 00239 str = t.templateNewMessage(); 00240 if ( str.isEmpty() && tid ) { 00241 str = tid->templateNewMessage(); 00242 } 00243 if ( str.isEmpty() ) { 00244 str = GlobalSettings::self()->templateNewMessage(); 00245 } 00246 if ( str.isEmpty() ) { 00247 str = defaultNewMessage(); 00248 } 00249 textEdit_new->setText( str ); 00250 00251 str = t.templateReply(); 00252 if ( str.isEmpty() && tid ) { 00253 str = tid->templateReply(); 00254 } 00255 if ( str.isEmpty() ) { 00256 str = GlobalSettings::self()->templateReply(); 00257 } 00258 if ( str.isEmpty() ) { 00259 str = defaultReply(); 00260 } 00261 textEdit_reply->setText( str ); 00262 00263 str = t.templateReplyAll(); 00264 if ( str.isEmpty() && tid ) { 00265 str = tid->templateReplyAll(); 00266 } 00267 if ( str.isEmpty() ) { 00268 str = GlobalSettings::self()->templateReplyAll(); 00269 } 00270 if ( str.isEmpty() ) { 00271 str = defaultReplyAll(); 00272 } 00273 textEdit_reply_all->setText( str ); 00274 00275 str = t.templateForward(); 00276 if ( str.isEmpty() && tid ) { 00277 str = tid->templateForward(); 00278 } 00279 if ( str.isEmpty() ) { 00280 str = GlobalSettings::self()->templateForward(); 00281 } 00282 if ( str.isEmpty() ) { 00283 str = defaultForward(); 00284 } 00285 textEdit_forward->setText( str ); 00286 00287 str = t.quoteString(); 00288 if ( str.isEmpty() && tid ) { 00289 str = tid->quoteString(); 00290 } 00291 if ( str.isEmpty() ) { 00292 str = GlobalSettings::self()->quoteString(); 00293 } 00294 if ( str.isEmpty() ) { 00295 str = defaultQuoteString(); 00296 } 00297 lineEdit_quote->setText( str ); 00298 00299 delete(tid); 00300 } 00301 00302 void TemplatesConfiguration::saveToFolder( TQString id ) 00303 { 00304 Templates t( id ); 00305 00306 t.setTemplateNewMessage( strOrBlank( textEdit_new->text() ) ); 00307 t.setTemplateReply( strOrBlank( textEdit_reply->text() ) ); 00308 t.setTemplateReplyAll( strOrBlank( textEdit_reply_all->text() ) ); 00309 t.setTemplateForward( strOrBlank( textEdit_forward->text() ) ); 00310 t.setQuoteString( lineEdit_quote->text() ); 00311 t.writeConfig(); 00312 } 00313 00314 void TemplatesConfiguration::loadFromPhrases() 00315 { 00316 int currentNr = GlobalSettings::self()->replyCurrentLanguage(); 00317 00318 ReplyPhrases replyPhrases( TQString::number( currentNr ) ); 00319 00320 textEdit_new->setText( defaultNewMessage() ); 00321 00322 TQString str; 00323 00324 str = replyPhrases.phraseReplySender(); 00325 if ( !str.isEmpty() ) { 00326 textEdit_reply->setText( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" ); 00327 } 00328 else { 00329 textEdit_reply->setText( defaultReply() ); 00330 } 00331 00332 str = replyPhrases.phraseReplyAll(); 00333 if ( !str.isEmpty() ) { 00334 textEdit_reply_all->setText( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" ); 00335 } 00336 else { 00337 textEdit_reply_all->setText( defaultReplyAll() ); 00338 } 00339 00340 str = replyPhrases.phraseForward(); 00341 if ( !str.isEmpty() ) { 00342 textEdit_forward->setText( TQString( i18n( 00343 "%REM=\"Default forward template\"%-\n" 00344 "---------- %1 ----------\n" 00345 "%TEXT\n" 00346 "-------------------------------------------------------\n" 00347 ) ).arg( convertPhrases( str ) ) ); 00348 } 00349 else { 00350 textEdit_forward->setText( defaultForward() ); 00351 } 00352 00353 str = replyPhrases.indentPrefix(); 00354 if ( !str.isEmpty() ) { 00355 // no need to convert indentPrefix() because it is passed to KMMessage::asQuotedString() as is 00356 lineEdit_quote->setText( str ); 00357 } 00358 else { 00359 lineEdit_quote->setText( defaultQuoteString() ); 00360 } 00361 } 00362 00363 void TemplatesConfiguration::importFromPhrases() 00364 { 00365 kdDebug() << "TemplatesConfiguration::importFromPhrases()" << endl; 00366 00367 int currentNr = GlobalSettings::self()->replyCurrentLanguage(); 00368 00369 ReplyPhrases replyPhrases( TQString::number( currentNr ) ); 00370 00371 TQString str; 00372 00373 str = replyPhrases.phraseReplySender(); 00374 if ( !str.isEmpty() ) { 00375 GlobalSettings::self()->setTemplateReply( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" ); 00376 } 00377 else { 00378 GlobalSettings::self()->setTemplateReply( defaultReply() ); 00379 } 00380 00381 str = replyPhrases.phraseReplyAll(); 00382 if ( !str.isEmpty() ) { 00383 GlobalSettings::self()->setTemplateReplyAll( convertPhrases( str ) + "\n%QUOTE\n%CURSOR\n" ); 00384 } 00385 else { 00386 GlobalSettings::self()->setTemplateReplyAll( defaultReplyAll() ); 00387 } 00388 00389 str = replyPhrases.phraseForward(); 00390 if ( !str.isEmpty() ) { 00391 GlobalSettings::self()->setTemplateForward( TQString( i18n( 00392 "%REM=\"Default forward template\"%-\n" 00393 "\n" 00394 "---------- %1 ----------\n" 00395 "\n" 00396 "Subject: %OFULLSUBJECT\n" 00397 "Date: %ODATE, %OTIMELONG\n" 00398 "From: %OFROMADDR\n" 00399 "%OADDRESSEESADDR\n" 00400 "\n" 00401 "%TEXT\n" 00402 "-------------------------------------------------------\n" 00403 ) ).arg( convertPhrases( str ) ) ); 00404 } 00405 else { 00406 GlobalSettings::self()->setTemplateForward( defaultForward() ); 00407 } 00408 00409 str = replyPhrases.indentPrefix(); 00410 if ( !str.isEmpty() ) { 00411 // no need to convert indentPrefix() because it is passed to KMMessage::asQuotedString() as is 00412 GlobalSettings::self()->setQuoteString( str ); 00413 } 00414 else { 00415 GlobalSettings::self()->setQuoteString( defaultQuoteString() ); 00416 } 00417 00418 GlobalSettings::self()->setPhrasesConverted( true ); 00419 GlobalSettings::self()->writeConfig(); 00420 } 00421 00422 TQString TemplatesConfiguration::convertPhrases( TQString &str ) 00423 { 00424 TQString result; 00425 TQChar ch; 00426 00427 unsigned int strLength( str.length() ); 00428 for ( uint i = 0; i < strLength; ) { 00429 ch = str[i++]; 00430 if ( ch == '%' ) { 00431 ch = str[i++]; 00432 switch ( (char)ch ) { 00433 case 'D': 00434 result += "%ODATE"; 00435 break; 00436 case 'e': 00437 result += "%OFROMADDR"; 00438 break; 00439 case 'F': 00440 result += "%OFROMNAME"; 00441 break; 00442 case 'f': 00443 // is this used for something like FIDO quotes, like "AB>" ? 00444 // not supported right now 00445 break; 00446 case 'T': 00447 result += "%OTONAME"; 00448 break; 00449 case 't': 00450 result += "%OTOADDR"; 00451 break; 00452 case 'C': 00453 result += "%OCCNAME"; 00454 break; 00455 case 'c': 00456 result += "%OCCADDR"; 00457 break; 00458 case 'S': 00459 result += "%OFULLSUBJECT"; 00460 break; 00461 case '_': 00462 result += ' '; 00463 break; 00464 case 'L': 00465 result += "\n"; 00466 break; 00467 case '%': 00468 result += "%%"; 00469 break; 00470 default: 00471 result += '%'; 00472 result += ch; 00473 break; 00474 } 00475 } else 00476 result += ch; 00477 } 00478 return result; 00479 } 00480 00481 void TemplatesConfiguration::slotInsertCommand( TQString cmd, int adjustCursor ) 00482 { 00483 TQTextEdit* edit; 00484 00485 if( toolBox1->currentItem() == page_new ) { 00486 edit = textEdit_new; 00487 } else if( toolBox1->currentItem() == page_reply ) { 00488 edit = textEdit_reply; 00489 } else if( toolBox1->currentItem() == page_reply_all ) { 00490 edit = textEdit_reply_all; 00491 } else if( toolBox1->currentItem() == page_forward ) { 00492 edit = textEdit_forward; 00493 } else { 00494 kdDebug() << "Unknown current page in TemplatesConfiguration!" << endl; 00495 return; 00496 } 00497 00498 // kdDebug() << "Insert command: " << cmd << endl; 00499 00500 int para, index; 00501 edit->getCursorPosition( ¶, &index ); 00502 edit->insertAt( cmd, para, index ); 00503 00504 index += adjustCursor; 00505 00506 edit->setCursorPosition( para, index + cmd.length() ); 00507 } 00508 00509 TQString TemplatesConfiguration::defaultNewMessage() { 00510 return i18n( 00511 "%REM=\"Default new message template\"%-\n" 00512 "%BLANK\n" 00513 "%BLANK\n" 00514 "%BLANK\n" 00515 ); 00516 } 00517 00518 TQString TemplatesConfiguration::defaultReply() { 00519 return i18n( 00520 "%CURSOR\n" 00521 "%BLANK\n" 00522 "%REM=\"Default reply template\"%-\n" 00523 "On %ODATEEN %OTIMELONGEN you wrote:\n" 00524 "%QUOTE\n" 00525 ); 00526 } 00527 00528 TQString TemplatesConfiguration::defaultReplyAll() { 00529 return i18n( 00530 "%CURSOR\n" 00531 "%BLANK\n" 00532 "%REM=\"Default reply all template\"%-\n" 00533 "On %ODATEEN %OTIMELONGEN %OFROMNAME wrote:\n" 00534 "%QUOTE\n" 00535 ); 00536 } 00537 00538 TQString TemplatesConfiguration::defaultForward() 00539 { 00540 return i18n( 00541 "%REM=\"Default forward template\"%-\n" 00542 "\n" 00543 "---------- Forwarded Message ----------\n" 00544 "\n" 00545 "Subject: %OFULLSUBJECT\n" 00546 "Date: %ODATE, %OTIMELONG\n" 00547 "From: %OFROMADDR\n" 00548 "%OADDRESSEESADDR\n" 00549 "\n" 00550 "%TEXT\n" 00551 "-------------------------------------------------------\n" 00552 ); 00553 } 00554 00555 TQString TemplatesConfiguration::defaultQuoteString() { 00556 return "> "; 00557 } 00558 00559 TQString TemplatesConfiguration::strOrBlank( TQString str ) { 00560 if ( str.stripWhiteSpace().isEmpty() ) { 00561 return TQString( "%BLANK" ); 00562 } 00563 return str; 00564 } 00565 00566 #include "templatesconfiguration.moc"