kmail

templatesconfiguration.cpp

Go to the documentation of this file.
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 <klocale.h>
00024 #include <kglobal.h>
00025 #include <qpopupmenu.h>
00026 #include <qpushbutton.h>
00027 #include <qtextedit.h>
00028 #include <qlineedit.h>
00029 #include <qtoolbox.h>
00030 #include <kdebug.h>
00031 #include <qfont.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( QWidget *parent, const char *name )
00042   :TemplatesConfigurationBase( parent, name )
00043 {
00044   QFont f = KGlobalSettings::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( QSizePolicy::Expanding, QSizePolicy::Expanding );
00051   sizeHint();
00052 
00053   connect( textEdit_new, SIGNAL( textChanged() ),
00054            this, SLOT( slotTextChanged( void ) ) );
00055   connect( textEdit_reply, SIGNAL( textChanged() ),
00056            this, SLOT( slotTextChanged( void ) ) );
00057   connect( textEdit_reply_all, SIGNAL( textChanged() ),
00058            this, SLOT( slotTextChanged( void ) ) );
00059   connect( textEdit_forward, SIGNAL( textChanged() ),
00060            this, SLOT( slotTextChanged( void ) ) );
00061   connect( lineEdit_quote, SIGNAL( textChanged( const QString & ) ),
00062            this, SLOT( slotTextChanged( void ) ) );
00063 
00064   connect( mInsertCommand, SIGNAL( insertCommand(QString, int) ),
00065            this, SLOT( slotInsertCommand(QString, int) ) );
00066 
00067   QString help;
00068   if ( QString( 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 ( QString( 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   QString 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( QString("IDENTITY_%1").arg( id ) );
00167 
00168   QString 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( QString("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( QString id, uint identity )
00229 {
00230   Templates t( id );
00231   Templates* tid = 0;
00232 
00233   if ( identity ) {
00234     tid = new Templates( QString("IDENTITY_%1").arg( identity ) );
00235   }
00236 
00237   QString 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( QString 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( QString::number( currentNr ) );
00319 
00320   textEdit_new->setText( defaultNewMessage() );
00321 
00322   QString 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( QString( 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( QString::number( currentNr ) );
00370 
00371   QString 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( QString( i18n(
00392     "%REM=\"Default forward template\"%-\n"
00393     "\n"
00394     "----------  %1  ----------\n"
00395     "\n"
00396     "Subject: %OFULLSUBJECT\n"
00397     "Date: %ODATE\n"
00398     "From: %OFROMADDR\n"
00399     "To: %OTOADDR\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 QString TemplatesConfiguration::convertPhrases( QString &str )
00423 {
00424   QString result;
00425   QChar 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( QString cmd, int adjustCursor )
00482 {
00483   QTextEdit* 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( &para, &index );
00502   edit->insertAt( cmd, para, index );
00503 
00504   index += adjustCursor;
00505 
00506   edit->setCursorPosition( para, index + cmd.length() );
00507 }
00508 
00509 QString TemplatesConfiguration::defaultNewMessage() {
00510   return i18n(
00511     "%REM=\"Default new message template\"%-\n"
00512     "%BLANK"
00513     );
00514 }
00515 
00516 QString TemplatesConfiguration::defaultReply() {
00517   return i18n(
00518     "%REM=\"Default reply template\"%-\n"
00519     "On %ODATEEN %OTIMELONGEN you wrote:\n"
00520     "%QUOTE\n"
00521     "%CURSOR\n"
00522     );
00523 }
00524 
00525 QString TemplatesConfiguration::defaultReplyAll() {
00526   return i18n(
00527     "%REM=\"Default reply all template\"%-\n"
00528     "On %ODATEEN %OTIMELONGEN %OFROMNAME wrote:\n"
00529     "%QUOTE\n"
00530     "%CURSOR\n"
00531     );
00532 }
00533 
00534 QString TemplatesConfiguration::defaultForward() {
00535   return i18n(
00536     "%REM=\"Default forward template\"%-\n"
00537     "\n"
00538     "----------  Forwarded Message  ----------\n"
00539     "\n"
00540     "Subject: %OFULLSUBJECT\n"
00541     "Date: %ODATE\n"
00542     "From: %OFROMADDR\n"
00543     "To: %OTOADDR\n"
00544     "\n"
00545     "%TEXT\n"
00546     "-------------------------------------------------------\n"
00547     );
00548 }
00549 
00550 QString TemplatesConfiguration::defaultQuoteString() {
00551   return "> ";
00552 }
00553 
00554 QString TemplatesConfiguration::strOrBlank( QString str ) {
00555   if ( str.stripWhiteSpace().isEmpty() ) {
00556     return QString( "%BLANK" );
00557   }
00558   return str;
00559 }
00560 
00561 #include "templatesconfiguration.moc"