kmail Library API Documentation

signatureconfigurator.cpp

00001 /*  -*- c++ -*-
00002     signatureconfigurator.cpp
00003 
00004     KMail, the KDE mail client.
00005     Copyright (c) 2002 the KMail authors.
00006     See file AUTHORS for details
00007 
00008     This program is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU General Public License,
00010     version 2.0, as published by the Free Software Foundation.
00011     You should have received a copy of the GNU General Public License
00012     along with this program; if not, write to the Free Software Foundation,
00013     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
00014 */
00015 
00016 #ifdef HAVE_CONFIG_H
00017 #include <config.h>
00018 #endif
00019 
00020 #include "signatureconfigurator.h"
00021 
00022 #include <klocale.h>
00023 #include <kdialog.h>
00024 #include <klineedit.h>
00025 #include <kurlrequester.h>
00026 #include <kshellcompletion.h>
00027 #include <krun.h>
00028 
00029 #include <qlabel.h>
00030 #include <qlayout.h>
00031 #include <qcheckbox.h>
00032 #include <qcombobox.h>
00033 #include <qwidgetstack.h>
00034 #include <qtextedit.h>
00035 #include <qwhatsthis.h>
00036 
00037 #include <assert.h>
00038 
00039 using namespace KMail;
00040 
00041 namespace KMail {
00042 
00043   SignatureConfigurator::SignatureConfigurator( QWidget * parent, const char * name )
00044     : QWidget( parent, name )
00045   {
00046     // tmp. vars:
00047     QLabel * label;
00048     QWidget * page;
00049     QHBoxLayout * hlay;
00050     QVBoxLayout * vlay;
00051     QVBoxLayout * page_vlay;
00052 
00053     vlay = new QVBoxLayout( this, 0, KDialog::spacingHint(), "main layout" );
00054 
00055     // "enable signatue" checkbox:
00056     mEnableCheck = new QCheckBox( i18n("&Enable signature"), this );
00057     QWhatsThis::add(mEnableCheck, 
00058         i18n("Check this box if you want KMail to append a signature to mails "
00059              "written with this identity."));
00060     vlay->addWidget( mEnableCheck );
00061 
00062     // "obtain signature text from" combo and label:
00063     hlay = new QHBoxLayout( vlay ); // inherits spacing
00064     mSourceCombo = new QComboBox( false, this );
00065     QWhatsThis::add(mSourceCombo,
00066         i18n("Click on the widgets below to obtain help on the input methods."));
00067     mSourceCombo->setEnabled( false ); // since !mEnableCheck->isChecked()
00068     mSourceCombo->insertStringList( QStringList()
00069            << i18n("continuation of \"obtain signature text from\"",
00070                "Input Field Below")
00071            << i18n("continuation of \"obtain signature text from\"",
00072                "File")
00073                    << i18n("continuation of \"obtain signature text from\"",
00074                "Output of Command")
00075            );
00076     label = new QLabel( mSourceCombo,
00077             i18n("Obtain signature &text from:"), this );
00078     label->setEnabled( false ); // since !mEnableCheck->isChecked()
00079     hlay->addWidget( label );
00080     hlay->addWidget( mSourceCombo, 1 );
00081 
00082     // widget stack that is controlled by the source combo:
00083     QWidgetStack * widgetStack = new QWidgetStack( this );
00084     widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked()
00085     vlay->addWidget( widgetStack, 1 );
00086     connect( mSourceCombo, SIGNAL(highlighted(int)),
00087          widgetStack, SLOT(raiseWidget(int)) );
00088     // connects for the enabling of the widgets depending on
00089     // signatureEnabled:
00090     connect( mEnableCheck, SIGNAL(toggled(bool)),
00091          mSourceCombo, SLOT(setEnabled(bool)) );
00092     connect( mEnableCheck, SIGNAL(toggled(bool)),
00093          widgetStack, SLOT(setEnabled(bool)) );
00094     connect( mEnableCheck, SIGNAL(toggled(bool)),
00095          label, SLOT(setEnabled(bool)) );
00096     // The focus might be still in the widget that is disabled
00097     connect( mEnableCheck, SIGNAL(clicked()),
00098          mEnableCheck, SLOT(setFocus()) );
00099 
00100     int pageno = 0;
00101     // page 0: input field for direct entering:
00102     mTextEdit = new QTextEdit( widgetStack );
00103     QWhatsThis::add(mTextEdit, 
00104         i18n("Use this field to enter an arbitrary static signature."));
00105     widgetStack->addWidget( mTextEdit, pageno );
00106     mTextEdit->setFont( KGlobalSettings::fixedFont() );
00107     mTextEdit->setWordWrap( QTextEdit::NoWrap );
00108     mTextEdit->setTextFormat( Qt::PlainText );
00109 
00110     widgetStack->raiseWidget( 0 ); // since mSourceCombo->currentItem() == 0
00111 
00112     // page 1: "signature file" requester, label, "edit file" button:
00113     ++pageno;
00114     page = new QWidget( widgetStack );
00115     widgetStack->addWidget( page, pageno ); // force sequential numbers (play safe)
00116     page_vlay = new QVBoxLayout( page, 0, KDialog::spacingHint() );
00117     hlay = new QHBoxLayout( page_vlay ); // inherits spacing
00118     mFileRequester = new KURLRequester( page );
00119     QWhatsThis::add(mFileRequester, 
00120         i18n("Use this requester to specify a text file that contains your "
00121              "signature. It will be read every time you create a new mail or "
00122              "append a new signature."));
00123     hlay->addWidget( new QLabel( mFileRequester,
00124                  i18n("S&pecify file:"), page ) );
00125     hlay->addWidget( mFileRequester, 1 );
00126     mFileRequester->button()->setAutoDefault( false );
00127     connect( mFileRequester, SIGNAL(textChanged(const QString &)),
00128          this, SLOT(slotEnableEditButton(const QString &)) );
00129     mEditButton = new QPushButton( i18n("Edit &File"), page );
00130     QWhatsThis::add(mEditButton, i18n("Opens the specified file in a text editor."));
00131     connect( mEditButton, SIGNAL(clicked()), SLOT(slotEdit()) );
00132     mEditButton->setAutoDefault( false );
00133     mEditButton->setEnabled( false ); // initially nothing to edit
00134     hlay->addWidget( mEditButton );
00135     page_vlay->addStretch( 1 ); // spacer
00136 
00137     // page 2: "signature command" requester and label:
00138     ++pageno;
00139     page = new QWidget( widgetStack );
00140     widgetStack->addWidget( page, pageno );
00141     page_vlay = new QVBoxLayout( page, 0, KDialog::spacingHint() );
00142     hlay = new QHBoxLayout( page_vlay ); // inherits spacing
00143     mCommandEdit = new KLineEdit( page );
00144     mCommandEdit->setCompletionObject( new KShellCompletion() );
00145     mCommandEdit->setAutoDeleteCompletionObject( true );
00146     QWhatsThis::add(mCommandEdit, 
00147         i18n("You can add an arbitrary command here, either with or without path "
00148              "depending on whether or not the command is in your Path. For every "
00149              "new mail, KMail will execute the command and use what it outputs (to "
00150              "standard output) as a signature. Usual commands for use with this "
00151              "mechanism are \"fortune\" or \"ksig -random\"."));
00152     hlay->addWidget( new QLabel( mCommandEdit,
00153                  i18n("S&pecify command:"), page ) );
00154     hlay->addWidget( mCommandEdit, 1 );
00155     page_vlay->addStretch( 1 ); // spacer
00156 
00157   }
00158 
00159   SignatureConfigurator::~SignatureConfigurator() {
00160 
00161   }
00162 
00163   bool SignatureConfigurator::isSignatureEnabled() const {
00164     return mEnableCheck->isChecked();
00165   }
00166 
00167   void SignatureConfigurator::setSignatureEnabled( bool enable ) {
00168     mEnableCheck->setChecked( enable );
00169   }
00170 
00171   Signature::Type SignatureConfigurator::signatureType() const {
00172     if ( !isSignatureEnabled() ) return Signature::Disabled;
00173 
00174     switch ( mSourceCombo->currentItem() ) {
00175     case 0:  return Signature::Inlined;
00176     case 1:  return Signature::FromFile;
00177     case 2:  return Signature::FromCommand;
00178     default: return Signature::Disabled;
00179     }
00180   }
00181 
00182   void SignatureConfigurator::setSignatureType( Signature::Type type ) {
00183     setSignatureEnabled( type != Signature::Disabled );
00184 
00185     int idx = 0;
00186     switch( type ) {
00187     case Signature::Inlined:     idx = 0; break;
00188     case Signature::FromFile:    idx = 1; break;
00189     case Signature::FromCommand: idx = 2; break;
00190     default:                     idx = 0; break;
00191     };
00192 
00193     mSourceCombo->setCurrentItem( idx );
00194   }
00195 
00196   QString SignatureConfigurator::inlineText() const {
00197     return mTextEdit->text();
00198   }
00199 
00200   void SignatureConfigurator::setInlineText( const QString & text ) {
00201     mTextEdit->setText( text );
00202   }
00203 
00204   QString SignatureConfigurator::fileURL() const {
00205     return mFileRequester->url();
00206   }
00207 
00208   void SignatureConfigurator::setFileURL( const QString & url ) {
00209     mFileRequester->setURL( url );
00210   }
00211 
00212   QString SignatureConfigurator::commandURL() const {
00213     return mCommandEdit->text();
00214   }
00215 
00216   void SignatureConfigurator::setCommandURL( const QString & url ) {
00217     mCommandEdit->setText( url );
00218   }
00219 
00220 
00221   Signature SignatureConfigurator::signature() const {
00222     switch ( signatureType() ) {
00223     case Signature::Inlined:
00224       return Signature( inlineText() );
00225     case Signature::FromCommand:
00226       return Signature( commandURL(), true );
00227     case Signature::FromFile:
00228       return Signature( fileURL(), false );
00229     case Signature::Disabled:
00230     default:
00231       return Signature();
00232     };
00233   }
00234 
00235   void SignatureConfigurator::setSignature( const Signature & sig ) {
00236     setSignatureType( sig.type() );
00237     if ( sig.type() == Signature::Inlined )
00238       setInlineText( sig.text() );
00239     else
00240       setInlineText( QString::null );
00241     if ( sig.type() == Signature::FromFile )
00242       setFileURL( sig.url() );
00243     else
00244       setFileURL( QString::null );
00245     if ( sig.type() == Signature::FromCommand )
00246       setCommandURL( sig.url() );
00247     else
00248       setCommandURL( QString::null );
00249   }
00250 
00251   void SignatureConfigurator::slotEnableEditButton( const QString & url ) {
00252     mEditButton->setDisabled( url.stripWhiteSpace().isEmpty() );
00253   }
00254 
00255   void SignatureConfigurator::slotEdit() {
00256     QString url = mFileRequester->url().stripWhiteSpace();
00257     // slotEnableEditButton should prevent this assert from being hit:
00258     assert( !url.isEmpty() );
00259 
00260     (void)KRun::runURL( KURL( url ), QString::fromLatin1("text/plain") );
00261   }
00262 
00263 } // namespace KMail
00264 
00265 #include "signatureconfigurator.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:55:44 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003