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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <tqcheckbox.h> 00030 #include <tqcombobox.h> 00031 #include <tqdir.h> 00032 #include <tqfileinfo.h> 00033 #include <tqlabel.h> 00034 #include <tqlayout.h> 00035 #include <tqtextedit.h> 00036 #include <tqwhatsthis.h> 00037 #include <tqwidgetstack.h> 00038 00039 #include <assert.h> 00040 00041 using namespace KMail; 00042 00043 namespace KMail { 00044 00045 SignatureConfigurator::SignatureConfigurator( TQWidget * parent, const char * name ) 00046 : TQWidget( parent, name ) 00047 { 00048 // tmp. vars: 00049 TQLabel * label; 00050 TQWidget * page; 00051 TQHBoxLayout * hlay; 00052 TQVBoxLayout * vlay; 00053 TQVBoxLayout * page_vlay; 00054 00055 vlay = new TQVBoxLayout( this, 0, KDialog::spacingHint(), "main layout" ); 00056 00057 // "enable signatue" checkbox: 00058 mEnableCheck = new TQCheckBox( i18n("&Enable signature"), this ); 00059 TQWhatsThis::add(mEnableCheck, 00060 i18n("Check this box if you want KMail to append a signature to mails " 00061 "written with this identity.")); 00062 vlay->addWidget( mEnableCheck ); 00063 00064 // "obtain signature text from" combo and label: 00065 hlay = new TQHBoxLayout( vlay ); // inherits spacing 00066 mSourceCombo = new TQComboBox( false, this ); 00067 TQWhatsThis::add(mSourceCombo, 00068 i18n("Click on the widgets below to obtain help on the input methods.")); 00069 mSourceCombo->setEnabled( false ); // since !mEnableCheck->isChecked() 00070 mSourceCombo->insertStringList( TQStringList() 00071 << i18n("continuation of \"obtain signature text from\"", 00072 "Input Field Below") 00073 << i18n("continuation of \"obtain signature text from\"", 00074 "File") 00075 << i18n("continuation of \"obtain signature text from\"", 00076 "Output of Command") 00077 ); 00078 label = new TQLabel( mSourceCombo, 00079 i18n("Obtain signature &text from:"), this ); 00080 label->setEnabled( false ); // since !mEnableCheck->isChecked() 00081 hlay->addWidget( label ); 00082 hlay->addWidget( mSourceCombo, 1 ); 00083 00084 // widget stack that is controlled by the source combo: 00085 TQWidgetStack * widgetStack = new TQWidgetStack( this ); 00086 widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked() 00087 vlay->addWidget( widgetStack, 1 ); 00088 connect( mSourceCombo, TQT_SIGNAL(highlighted(int)), 00089 widgetStack, TQT_SLOT(raiseWidget(int)) ); 00090 // connects for the enabling of the widgets depending on 00091 // signatureEnabled: 00092 connect( mEnableCheck, TQT_SIGNAL(toggled(bool)), 00093 mSourceCombo, TQT_SLOT(setEnabled(bool)) ); 00094 connect( mEnableCheck, TQT_SIGNAL(toggled(bool)), 00095 widgetStack, TQT_SLOT(setEnabled(bool)) ); 00096 connect( mEnableCheck, TQT_SIGNAL(toggled(bool)), 00097 label, TQT_SLOT(setEnabled(bool)) ); 00098 // The focus might be still in the widget that is disabled 00099 connect( mEnableCheck, TQT_SIGNAL(clicked()), 00100 mEnableCheck, TQT_SLOT(setFocus()) ); 00101 00102 int pageno = 0; 00103 // page 0: input field for direct entering: 00104 mTextEdit = new TQTextEdit( widgetStack ); 00105 TQWhatsThis::add(mTextEdit, 00106 i18n("Use this field to enter an arbitrary static signature.")); 00107 widgetStack->addWidget( mTextEdit, pageno ); 00108 mTextEdit->setFont( KGlobalSettings::fixedFont() ); 00109 mTextEdit->setWordWrap( TQTextEdit::NoWrap ); 00110 mTextEdit->setTextFormat( TQt::PlainText ); 00111 00112 widgetStack->raiseWidget( 0 ); // since mSourceCombo->currentItem() == 0 00113 00114 // page 1: "signature file" requester, label, "edit file" button: 00115 ++pageno; 00116 page = new TQWidget( widgetStack ); 00117 widgetStack->addWidget( page, pageno ); // force sequential numbers (play safe) 00118 page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() ); 00119 hlay = new TQHBoxLayout( page_vlay ); // inherits spacing 00120 mFileRequester = new KURLRequester( page ); 00121 TQWhatsThis::add(mFileRequester, 00122 i18n("Use this requester to specify a text file that contains your " 00123 "signature. It will be read every time you create a new mail or " 00124 "append a new signature.")); 00125 hlay->addWidget( new TQLabel( mFileRequester, 00126 i18n("S&pecify file:"), page ) ); 00127 hlay->addWidget( mFileRequester, 1 ); 00128 mFileRequester->button()->setAutoDefault( false ); 00129 connect( mFileRequester, TQT_SIGNAL(textChanged(const TQString &)), 00130 this, TQT_SLOT(slotEnableEditButton(const TQString &)) ); 00131 mEditButton = new TQPushButton( i18n("Edit &File"), page ); 00132 TQWhatsThis::add(mEditButton, i18n("Opens the specified file in a text editor.")); 00133 connect( mEditButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotEdit()) ); 00134 mEditButton->setAutoDefault( false ); 00135 mEditButton->setEnabled( false ); // initially nothing to edit 00136 hlay->addWidget( mEditButton ); 00137 page_vlay->addStretch( 1 ); // spacer 00138 00139 // page 2: "signature command" requester and label: 00140 ++pageno; 00141 page = new TQWidget( widgetStack ); 00142 widgetStack->addWidget( page, pageno ); 00143 page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() ); 00144 hlay = new TQHBoxLayout( page_vlay ); // inherits spacing 00145 mCommandEdit = new KLineEdit( page ); 00146 mCommandEdit->setCompletionObject( new KShellCompletion() ); 00147 mCommandEdit->setAutoDeleteCompletionObject( true ); 00148 TQWhatsThis::add(mCommandEdit, 00149 i18n("You can add an arbitrary command here, either with or without path " 00150 "depending on whether or not the command is in your Path. For every " 00151 "new mail, KMail will execute the command and use what it outputs (to " 00152 "standard output) as a signature. Usual commands for use with this " 00153 "mechanism are \"fortune\" or \"ksig -random\".")); 00154 hlay->addWidget( new TQLabel( mCommandEdit, 00155 i18n("S&pecify command:"), page ) ); 00156 hlay->addWidget( mCommandEdit, 1 ); 00157 page_vlay->addStretch( 1 ); // spacer 00158 00159 } 00160 00161 SignatureConfigurator::~SignatureConfigurator() { 00162 00163 } 00164 00165 bool SignatureConfigurator::isSignatureEnabled() const { 00166 return mEnableCheck->isChecked(); 00167 } 00168 00169 void SignatureConfigurator::setSignatureEnabled( bool enable ) { 00170 mEnableCheck->setChecked( enable ); 00171 } 00172 00173 Signature::Type SignatureConfigurator::signatureType() const { 00174 if ( !isSignatureEnabled() ) return Signature::Disabled; 00175 00176 switch ( mSourceCombo->currentItem() ) { 00177 case 0: return Signature::Inlined; 00178 case 1: return Signature::FromFile; 00179 case 2: return Signature::FromCommand; 00180 default: return Signature::Disabled; 00181 } 00182 } 00183 00184 void SignatureConfigurator::setSignatureType( Signature::Type type ) { 00185 setSignatureEnabled( type != Signature::Disabled ); 00186 00187 int idx = 0; 00188 switch( type ) { 00189 case Signature::Inlined: idx = 0; break; 00190 case Signature::FromFile: idx = 1; break; 00191 case Signature::FromCommand: idx = 2; break; 00192 default: idx = 0; break; 00193 }; 00194 00195 mSourceCombo->setCurrentItem( idx ); 00196 } 00197 00198 TQString SignatureConfigurator::inlineText() const { 00199 return mTextEdit->text(); 00200 } 00201 00202 void SignatureConfigurator::setInlineText( const TQString & text ) { 00203 mTextEdit->setText( text ); 00204 } 00205 00206 TQString SignatureConfigurator::fileURL() const { 00207 TQString file = mFileRequester->url().stripWhiteSpace(); 00208 00209 // Force the filename to be relative to ~ instead of $PWD depending 00210 // on the rest of the code (KRun::run in Edit and KFileItem on save) 00211 if ( !file.isEmpty() && TQFileInfo( file ).isRelative() ) 00212 file = TQDir::home().absPath() + TQDir::separator() + file; 00213 00214 return file; 00215 } 00216 00217 void SignatureConfigurator::setFileURL( const TQString & url ) { 00218 mFileRequester->setURL( url ); 00219 } 00220 00221 TQString SignatureConfigurator::commandURL() const { 00222 return mCommandEdit->text(); 00223 } 00224 00225 void SignatureConfigurator::setCommandURL( const TQString & url ) { 00226 mCommandEdit->setText( url ); 00227 } 00228 00229 00230 Signature SignatureConfigurator::signature() const { 00231 Signature sig; 00232 sig.setType( signatureType() ); 00233 sig.setText( inlineText() ); 00234 if ( signatureType() == Signature::FromCommand ) 00235 sig.setUrl( commandURL(), true ); 00236 if ( signatureType() == Signature::FromFile ) 00237 sig.setUrl( fileURL(), false ); 00238 return sig; 00239 } 00240 00241 void SignatureConfigurator::setSignature( const Signature & sig ) { 00242 setSignatureType( sig.type() ); 00243 setInlineText( sig.text() ); 00244 if ( sig.type() == Signature::FromFile ) 00245 setFileURL( sig.url() ); 00246 else 00247 setFileURL( TQString() ); 00248 if ( sig.type() == Signature::FromCommand ) 00249 setCommandURL( sig.url() ); 00250 else 00251 setCommandURL( TQString() ); 00252 } 00253 00254 void SignatureConfigurator::slotEnableEditButton( const TQString & url ) { 00255 mEditButton->setDisabled( url.stripWhiteSpace().isEmpty() ); 00256 } 00257 00258 void SignatureConfigurator::slotEdit() { 00259 TQString url = fileURL(); 00260 // slotEnableEditButton should prevent this assert from being hit: 00261 assert( !url.isEmpty() ); 00262 00263 (void)KRun::runURL( KURL( url ), TQString::fromLatin1("text/plain") ); 00264 } 00265 00266 } // namespace KMail 00267 00268 #include "signatureconfigurator.moc"