kmail

signatureconfigurator.cpp
1/*
2 signatureconfigurator.cpp
3
4 KMail, the KDE mail client.
5 Copyright (c) 2002 the KMail authors.
6 See file AUTHORS for details
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License,
10 version 2.0, as published by the Free Software Foundation.
11 You should have received a copy of the GNU General Public License
12 along with this program; if not, write to the Free Software Foundation,
13 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
14*/
15
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "signatureconfigurator.h"
21
22#include <tdelocale.h>
23#include <kdialog.h>
24#include <klineedit.h>
25#include <kurlrequester.h>
26#include <kshellcompletion.h>
27#include <krun.h>
28
29#include <tqcheckbox.h>
30#include <tqcombobox.h>
31#include <tqdir.h>
32#include <tqfileinfo.h>
33#include <tqlabel.h>
34#include <tqlayout.h>
35#include <tqtextedit.h>
36#include <tqwhatsthis.h>
37#include <tqwidgetstack.h>
38
39#include <assert.h>
40
41using namespace KMail;
42
43namespace KMail {
44
45 SignatureConfigurator::SignatureConfigurator( TQWidget * parent, const char * name )
46 : TQWidget( parent, name )
47 {
48 // tmp. vars:
49 TQLabel * label;
50 TQWidget * page;
51 TQHBoxLayout * hlay;
52 TQVBoxLayout * vlay;
53 TQVBoxLayout * page_vlay;
54
55 vlay = new TQVBoxLayout( this, 0, KDialog::spacingHint(), "main layout" );
56
57 // "enable signatue" checkbox:
58 mEnableCheck = new TQCheckBox( i18n("&Enable signature"), this );
59 TQWhatsThis::add(mEnableCheck,
60 i18n("Check this box if you want KMail to append a signature to mails "
61 "written with this identity."));
62 vlay->addWidget( mEnableCheck );
63
64 // "obtain signature text from" combo and label:
65 hlay = new TQHBoxLayout( vlay ); // inherits spacing
66 mSourceCombo = new TQComboBox( false, this );
67 TQWhatsThis::add(mSourceCombo,
68 i18n("Click on the widgets below to obtain help on the input methods."));
69 mSourceCombo->setEnabled( false ); // since !mEnableCheck->isChecked()
70 mSourceCombo->insertStringList( TQStringList()
71 << i18n("continuation of \"obtain signature text from\"",
72 "Input Field Below")
73 << i18n("continuation of \"obtain signature text from\"",
74 "File")
75 << i18n("continuation of \"obtain signature text from\"",
76 "Output of Command")
77 );
78 label = new TQLabel( mSourceCombo,
79 i18n("Obtain signature &text from:"), this );
80 label->setEnabled( false ); // since !mEnableCheck->isChecked()
81 hlay->addWidget( label );
82 hlay->addWidget( mSourceCombo, 1 );
83
84 // widget stack that is controlled by the source combo:
85 TQWidgetStack * widgetStack = new TQWidgetStack( this );
86 widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked()
87 vlay->addWidget( widgetStack, 1 );
88 connect( mSourceCombo, TQ_SIGNAL(highlighted(int)),
89 widgetStack, TQ_SLOT(raiseWidget(int)) );
90 // connects for the enabling of the widgets depending on
91 // signatureEnabled:
92 connect( mEnableCheck, TQ_SIGNAL(toggled(bool)),
93 mSourceCombo, TQ_SLOT(setEnabled(bool)) );
94 connect( mEnableCheck, TQ_SIGNAL(toggled(bool)),
95 widgetStack, TQ_SLOT(setEnabled(bool)) );
96 connect( mEnableCheck, TQ_SIGNAL(toggled(bool)),
97 label, TQ_SLOT(setEnabled(bool)) );
98 // The focus might be still in the widget that is disabled
99 connect( mEnableCheck, TQ_SIGNAL(clicked()),
100 mEnableCheck, TQ_SLOT(setFocus()) );
101
102 int pageno = 0;
103 // page 0: input field for direct entering:
104 mTextEdit = new TQTextEdit( widgetStack );
105 TQWhatsThis::add(mTextEdit,
106 i18n("Use this field to enter an arbitrary static signature."));
107 widgetStack->addWidget( mTextEdit, pageno );
108 mTextEdit->setFont( TDEGlobalSettings::fixedFont() );
109 mTextEdit->setWordWrap( TQTextEdit::NoWrap );
110 mTextEdit->setTextFormat( TQt::PlainText );
111
112 widgetStack->raiseWidget( 0 ); // since mSourceCombo->currentItem() == 0
113
114 // page 1: "signature file" requester, label, "edit file" button:
115 ++pageno;
116 page = new TQWidget( widgetStack );
117 widgetStack->addWidget( page, pageno ); // force sequential numbers (play safe)
118 page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() );
119 hlay = new TQHBoxLayout( page_vlay ); // inherits spacing
120 mFileRequester = new KURLRequester( page );
121 TQWhatsThis::add(mFileRequester,
122 i18n("Use this requester to specify a text file that contains your "
123 "signature. It will be read every time you create a new mail or "
124 "append a new signature."));
125 hlay->addWidget( new TQLabel( mFileRequester,
126 i18n("S&pecify file:"), page ) );
127 hlay->addWidget( mFileRequester, 1 );
128 mFileRequester->button()->setAutoDefault( false );
129 connect( mFileRequester, TQ_SIGNAL(textChanged(const TQString &)),
130 this, TQ_SLOT(slotEnableEditButton(const TQString &)) );
131 mEditButton = new TQPushButton( i18n("Edit &File"), page );
132 TQWhatsThis::add(mEditButton, i18n("Opens the specified file in a text editor."));
133 connect( mEditButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotEdit()) );
134 mEditButton->setAutoDefault( false );
135 mEditButton->setEnabled( false ); // initially nothing to edit
136 hlay->addWidget( mEditButton );
137 page_vlay->addStretch( 1 ); // spacer
138
139 // page 2: "signature command" requester and label:
140 ++pageno;
141 page = new TQWidget( widgetStack );
142 widgetStack->addWidget( page, pageno );
143 page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() );
144 hlay = new TQHBoxLayout( page_vlay ); // inherits spacing
145 mCommandEdit = new KLineEdit( page );
146 mCommandEdit->setCompletionObject( new KShellCompletion() );
147 mCommandEdit->setAutoDeleteCompletionObject( true );
148 TQWhatsThis::add(mCommandEdit,
149 i18n("You can add an arbitrary command here, either with or without path "
150 "depending on whether or not the command is in your Path. For every "
151 "new mail, KMail will execute the command and use what it outputs (to "
152 "standard output) as a signature. Usual commands for use with this "
153 "mechanism are \"fortune\" or \"ksig -random\"."));
154 hlay->addWidget( new TQLabel( mCommandEdit,
155 i18n("S&pecify command:"), page ) );
156 hlay->addWidget( mCommandEdit, 1 );
157 page_vlay->addStretch( 1 ); // spacer
158
159 }
160
161 SignatureConfigurator::~SignatureConfigurator() {
162
163 }
164
165 bool SignatureConfigurator::isSignatureEnabled() const {
166 return mEnableCheck->isChecked();
167 }
168
169 void SignatureConfigurator::setSignatureEnabled( bool enable ) {
170 mEnableCheck->setChecked( enable );
171 }
172
173 Signature::Type SignatureConfigurator::signatureType() const {
174 if ( !isSignatureEnabled() ) return Signature::Disabled;
175
176 switch ( mSourceCombo->currentItem() ) {
177 case 0: return Signature::Inlined;
178 case 1: return Signature::FromFile;
179 case 2: return Signature::FromCommand;
180 default: return Signature::Disabled;
181 }
182 }
183
184 void SignatureConfigurator::setSignatureType( Signature::Type type ) {
185 setSignatureEnabled( type != Signature::Disabled );
186
187 int idx = 0;
188 switch( type ) {
189 case Signature::Inlined: idx = 0; break;
190 case Signature::FromFile: idx = 1; break;
191 case Signature::FromCommand: idx = 2; break;
192 default: idx = 0; break;
193 };
194
195 mSourceCombo->setCurrentItem( idx );
196 }
197
198 TQString SignatureConfigurator::inlineText() const {
199 return mTextEdit->text();
200 }
201
202 void SignatureConfigurator::setInlineText( const TQString & text ) {
203 mTextEdit->setText( text );
204 }
205
206 TQString SignatureConfigurator::fileURL() const {
207 TQString file = mFileRequester->url().stripWhiteSpace();
208
209 // Force the filename to be relative to ~ instead of $PWD depending
210 // on the rest of the code (KRun::run in Edit and KFileItem on save)
211 if ( !file.isEmpty() && TQFileInfo( file ).isRelative() )
212 file = TQDir::home().absPath() + TQDir::separator() + file;
213
214 return file;
215 }
216
217 void SignatureConfigurator::setFileURL( const TQString & url ) {
218 mFileRequester->setURL( url );
219 }
220
221 TQString SignatureConfigurator::commandURL() const {
222 return mCommandEdit->text();
223 }
224
225 void SignatureConfigurator::setCommandURL( const TQString & url ) {
226 mCommandEdit->setText( url );
227 }
228
229
230 Signature SignatureConfigurator::signature() const {
231 Signature sig;
232 sig.setType( signatureType() );
233 sig.setText( inlineText() );
234 if ( signatureType() == Signature::FromCommand )
235 sig.setUrl( commandURL(), true );
236 if ( signatureType() == Signature::FromFile )
237 sig.setUrl( fileURL(), false );
238 return sig;
239 }
240
241 void SignatureConfigurator::setSignature( const Signature & sig ) {
242 setSignatureType( sig.type() );
243 setInlineText( sig.text() );
244 if ( sig.type() == Signature::FromFile )
245 setFileURL( sig.url() );
246 else
247 setFileURL( TQString() );
248 if ( sig.type() == Signature::FromCommand )
249 setCommandURL( sig.url() );
250 else
251 setCommandURL( TQString() );
252 }
253
254 void SignatureConfigurator::slotEnableEditButton( const TQString & url ) {
255 mEditButton->setDisabled( url.stripWhiteSpace().isEmpty() );
256 }
257
258 void SignatureConfigurator::slotEdit() {
259 TQString url = fileURL();
260 // slotEnableEditButton should prevent this assert from being hit:
261 assert( !url.isEmpty() );
262
263 (void)KRun::runURL( KURL( url ), TQString::fromLatin1("text/plain") );
264 }
265
266} // namespace KMail
267
268#include "signatureconfigurator.moc"
folderdiaquotatab.h
Definition: aboutdata.cpp:40