25#include <tqpopupmenu.h> 
   26#include <tqpushbutton.h> 
   27#include <tqtextedit.h> 
   28#include <tqlineedit.h> 
   32#include <kactivelabel.h> 
   34#include "templatesconfiguration_base.h" 
   35#include "templatesconfiguration_kfg.h" 
   36#include "globalsettings.h" 
   37#include "replyphrases.h" 
   39#include "templatesconfiguration.h" 
   41TemplatesConfiguration::TemplatesConfiguration( TQWidget *parent, 
const char *name )
 
   42  :TemplatesConfigurationBase( parent, name )
 
   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 );
 
   50  setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding );
 
   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 ) ) );
 
   64  connect( mInsertCommand, TQ_SIGNAL( insertCommand(TQString, 
int) ),
 
   65           this, TQ_SLOT( slotInsertCommand(TQString, 
int) ) );
 
   68  if ( TQString( name ) == 
"folder-templates" ) {
 
   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>" 
   80  } 
else if ( TQString( name ) == 
"identity-templates" ) {
 
   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>" 
   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>" 
  105  mHelp->setText( i18n( 
"<a href=\"whatsthis:%1\">How does this work?</a>" ).arg( help ) );
 
  108void TemplatesConfiguration::slotTextChanged()
 
  113void TemplatesConfiguration::loadFromGlobal()
 
  115  if ( !GlobalSettings::self()->phrasesConverted() ) {
 
  116    kdDebug() << 
"Phrases to templates conversion" << endl;
 
  121  str = GlobalSettings::self()->templateNewMessage();
 
  122  if ( str.isEmpty() ) {
 
  123    textEdit_new->setText( defaultNewMessage() );
 
  125    textEdit_new->setText(str);
 
  127  str = GlobalSettings::self()->templateReply();
 
  128  if ( str.isEmpty() ) {
 
  129    textEdit_reply->setText( defaultReply() );
 
  131    textEdit_reply->setText( str );
 
  133  str = GlobalSettings::self()->templateReplyAll();
 
  134  if ( str.isEmpty() ) {
 
  135    textEdit_reply_all->setText( defaultReplyAll() );
 
  137    textEdit_reply_all->setText( str );
 
  139  str = GlobalSettings::self()->templateForward();
 
  140  if ( str.isEmpty() ) {
 
  141    textEdit_forward->setText( defaultForward() );
 
  143    textEdit_forward->setText( str );
 
  145  str = GlobalSettings::self()->quoteString();
 
  146  if ( str.isEmpty() ) {
 
  147    lineEdit_quote->setText( defaultQuoteString() );
 
  149    lineEdit_quote->setText( str );
 
  153void TemplatesConfiguration::saveToGlobal()
 
  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();
 
  164void TemplatesConfiguration::loadFromIdentity( uint 
id )
 
  166  Templates t( TQString(
"IDENTITY_%1").arg( 
id ) );
 
  170  str = t.templateNewMessage();
 
  171  if ( str.isEmpty() ) {
 
  172    str = GlobalSettings::self()->templateNewMessage();
 
  174  if ( str.isEmpty() ) {
 
  175    str = defaultNewMessage();
 
  177  textEdit_new->setText( str );
 
  179  str = t.templateReply();
 
  180  if ( str.isEmpty() ) {
 
  181    str = GlobalSettings::self()->templateReply();
 
  183  if ( str.isEmpty() ) {
 
  184    str = defaultReply();
 
  186  textEdit_reply->setText( str );
 
  188  str = t.templateReplyAll();
 
  189  if ( str.isEmpty() ) {
 
  190    str = GlobalSettings::self()->templateReplyAll();
 
  192  if ( str.isEmpty() ) {
 
  193    str = defaultReplyAll();
 
  195  textEdit_reply_all->setText( str );
 
  197  str = t.templateForward();
 
  198  if ( str.isEmpty() ) {
 
  199    str = GlobalSettings::self()->templateForward();
 
  201  if ( str.isEmpty() ) {
 
  202    str = defaultForward();
 
  204  textEdit_forward->setText( str );
 
  206  str = t.quoteString();
 
  207  if ( str.isEmpty() ) {
 
  208    str = GlobalSettings::self()->quoteString();
 
  210  if ( str.isEmpty() ) {
 
  211    str = defaultQuoteString();
 
  213  lineEdit_quote->setText( str );
 
  216void TemplatesConfiguration::saveToIdentity( uint 
id )
 
  218  Templates t( TQString(
"IDENTITY_%1").arg( 
id ) );
 
  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() );
 
  228void TemplatesConfiguration::loadFromFolder( TQString 
id, uint identity )
 
  234    tid = 
new Templates( TQString(
"IDENTITY_%1").arg( identity ) );
 
  239  str = t.templateNewMessage();
 
  240  if ( str.isEmpty() && tid ) {
 
  241    str = tid->templateNewMessage();
 
  243  if ( str.isEmpty() ) {
 
  244    str = GlobalSettings::self()->templateNewMessage();
 
  246  if ( str.isEmpty() ) {
 
  247    str = defaultNewMessage();
 
  249  textEdit_new->setText( str );
 
  251  str = t.templateReply();
 
  252  if ( str.isEmpty() && tid ) {
 
  253    str = tid->templateReply();
 
  255  if ( str.isEmpty() ) {
 
  256    str = GlobalSettings::self()->templateReply();
 
  258  if ( str.isEmpty() ) {
 
  259    str = defaultReply();
 
  261  textEdit_reply->setText( str );
 
  263  str = t.templateReplyAll();
 
  264  if ( str.isEmpty() && tid ) {
 
  265    str = tid->templateReplyAll();
 
  267  if ( str.isEmpty() ) {
 
  268    str = GlobalSettings::self()->templateReplyAll();
 
  270  if ( str.isEmpty() ) {
 
  271    str = defaultReplyAll();
 
  273  textEdit_reply_all->setText( str );
 
  275  str = t.templateForward();
 
  276  if ( str.isEmpty() && tid ) {
 
  277    str = tid->templateForward();
 
  279  if ( str.isEmpty() ) {
 
  280    str = GlobalSettings::self()->templateForward();
 
  282  if ( str.isEmpty() ) {
 
  283    str = defaultForward();
 
  285  textEdit_forward->setText( str );
 
  287  str = t.quoteString();
 
  288  if ( str.isEmpty() && tid ) {
 
  289    str = tid->quoteString();
 
  291  if ( str.isEmpty() ) {
 
  292    str = GlobalSettings::self()->quoteString();
 
  294  if ( str.isEmpty() ) {
 
  295      str = defaultQuoteString();
 
  297  lineEdit_quote->setText( str );
 
  302void TemplatesConfiguration::saveToFolder( TQString 
id )
 
  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() );
 
  314void TemplatesConfiguration::loadFromPhrases()
 
  316  int currentNr = GlobalSettings::self()->replyCurrentLanguage();
 
  318  ReplyPhrases replyPhrases( TQString::number( currentNr ) );
 
  320  textEdit_new->setText( defaultNewMessage() );
 
  324  str = replyPhrases.phraseReplySender();
 
  325  if ( !str.isEmpty() ) {
 
  326    textEdit_reply->setText( convertPhrases( str ) + 
"\n%QUOTE\n%CURSOR\n" );
 
  329    textEdit_reply->setText( defaultReply() );
 
  332  str = replyPhrases.phraseReplyAll();
 
  333  if ( !str.isEmpty() ) {
 
  334    textEdit_reply_all->setText( convertPhrases( str ) + 
"\n%QUOTE\n%CURSOR\n" );
 
  337    textEdit_reply_all->setText( defaultReplyAll() );
 
  340  str = replyPhrases.phraseForward();
 
  341  if ( !str.isEmpty() ) {
 
  342    textEdit_forward->setText( TQString( i18n(
 
  343    "%REM=\"Default forward template\"%-\n" 
  344    "----------  %1  ----------\n" 
  346    "-------------------------------------------------------\n" 
  347    ) ).arg( convertPhrases( str ) ) );
 
  350    textEdit_forward->setText( defaultForward() );
 
  353  str = replyPhrases.indentPrefix();
 
  354  if ( !str.isEmpty() ) {
 
  356    lineEdit_quote->setText( str );
 
  359    lineEdit_quote->setText( defaultQuoteString() );
 
  363void TemplatesConfiguration::importFromPhrases()
 
  365  kdDebug() << 
"TemplatesConfiguration::importFromPhrases()" << endl;
 
  367  int currentNr = GlobalSettings::self()->replyCurrentLanguage();
 
  369  ReplyPhrases replyPhrases( TQString::number( currentNr ) );
 
  373  str = replyPhrases.phraseReplySender();
 
  374  if ( !str.isEmpty() ) {
 
  375    GlobalSettings::self()->setTemplateReply( convertPhrases( str ) + 
"\n%QUOTE\n%CURSOR\n" );
 
  378    GlobalSettings::self()->setTemplateReply( defaultReply() );
 
  381  str = replyPhrases.phraseReplyAll();
 
  382  if ( !str.isEmpty() ) {
 
  383    GlobalSettings::self()->setTemplateReplyAll( convertPhrases( str ) + 
"\n%QUOTE\n%CURSOR\n" );
 
  386    GlobalSettings::self()->setTemplateReplyAll( defaultReplyAll() );
 
  389  str = replyPhrases.phraseForward();
 
  390  if ( !str.isEmpty() ) {
 
  391    GlobalSettings::self()->setTemplateForward( TQString( i18n(
 
  392    "%REM=\"Default forward template\"%-\n" 
  394    "----------  %1  ----------\n" 
  396    "Subject: %OFULLSUBJECT\n" 
  397    "Date: %ODATE, %OTIMELONG\n" 
  402    "-------------------------------------------------------\n" 
  403    ) ).arg( convertPhrases( str ) ) );
 
  406    GlobalSettings::self()->setTemplateForward( defaultForward() );
 
  409  str = replyPhrases.indentPrefix();
 
  410  if ( !str.isEmpty() ) {
 
  412    GlobalSettings::self()->setQuoteString( str );
 
  415    GlobalSettings::self()->setQuoteString( defaultQuoteString() );
 
  418  GlobalSettings::self()->setPhrasesConverted( 
true );
 
  419  GlobalSettings::self()->writeConfig();
 
  422TQString TemplatesConfiguration::convertPhrases( TQString &str )
 
  427  unsigned int strLength( str.length() );
 
  428  for ( uint i = 0; i < strLength; ) {
 
  432      switch ( (
char)ch ) {
 
  437        result += 
"%OFROMADDR";
 
  440        result += 
"%OFROMNAME";
 
  447        result += 
"%OTONAME";
 
  450        result += 
"%OTOADDR";
 
  453        result += 
"%OCCNAME";
 
  456        result += 
"%OCCADDR";
 
  459        result += 
"%OFULLSUBJECT";
 
  481void TemplatesConfiguration::slotInsertCommand( TQString cmd, 
int adjustCursor )
 
  485  if( toolBox1->currentItem() == page_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;
 
  494    kdDebug() << 
"Unknown current page in TemplatesConfiguration!" << endl;
 
  501  edit->getCursorPosition( ¶, &index );
 
  502  edit->insertAt( cmd, para, index );
 
  504  index += adjustCursor;
 
  506  edit->setCursorPosition( para, index + cmd.length() );
 
  509TQString TemplatesConfiguration::defaultNewMessage() {
 
  511    "%REM=\"Default new message template\"%-\n" 
  518TQString TemplatesConfiguration::defaultReply() {
 
  522    "%REM=\"Default reply template\"%-\n" 
  523    "On %ODATEEN %OTIMELONGEN you wrote:\n" 
  528TQString TemplatesConfiguration::defaultReplyAll() {
 
  532    "%REM=\"Default reply all template\"%-\n" 
  533    "On %ODATEEN %OTIMELONGEN %OFROMNAME wrote:\n" 
  538TQString TemplatesConfiguration::defaultForward()
 
  541    "%REM=\"Default forward template\"%-\n" 
  543    "----------  Forwarded Message  ----------\n" 
  545    "Subject: %OFULLSUBJECT\n" 
  546    "Date: %ODATE, %OTIMELONG\n" 
  551    "-------------------------------------------------------\n" 
  555TQString TemplatesConfiguration::defaultQuoteString() {
 
  559TQString TemplatesConfiguration::strOrBlank( TQString str ) {
 
  560  if ( str.stripWhiteSpace().isEmpty() ) {
 
  561    return TQString( 
"%BLANK" );
 
  566#include "templatesconfiguration.moc"