24 #include <tqcheckbox.h>
27 #include <tqpainter.h>
28 #include <tqpushbutton.h>
29 #include <tqvalidator.h>
31 #include <tqtoolbutton.h>
32 #include <tqtooltip.h>
34 #include <tdeaccelmanager.h>
35 #include <tdeconfig.h>
36 #include <kcombobox.h>
39 #include <kiconloader.h>
40 #include <kinputdialog.h>
41 #include <klineedit.h>
42 #include <tdelocale.h>
43 #include <tdemessagebox.h>
45 #include "emaileditwidget.h"
47 class EmailValidator :
public TQRegExpValidator
51 : TQRegExpValidator( 0,
"EmailValidator" )
53 TQRegExp rx(
".*@.*\\.[A-Za-z]+" );
58 class EmailItem :
public TQListBoxText
61 EmailItem( TQListBox *parent,
const TQString &text,
bool preferred )
62 : TQListBoxText( parent, text ), mPreferred( preferred )
65 void setPreferred(
bool preferred ) { mPreferred = preferred; }
66 bool preferred()
const {
return mPreferred; }
68 void setText(
const TQString &text )
70 TQListBoxText::setText( text );
74 virtual void paint( TQPainter *p )
77 TQFont font = p->font();
82 TQListBoxText::paint( p );
89 EmailEditWidget::EmailEditWidget( TQWidget *parent,
const char *name )
90 : TQWidget( parent, name )
92 TQGridLayout *topLayout =
new TQGridLayout(
this, 2, 2, KDialog::marginHint(),
93 KDialog::spacingHint() );
95 TQLabel *label =
new TQLabel( i18n(
"Email:" ),
this );
96 topLayout->addWidget( label, 0, 0 );
98 mEmailEdit =
new KLineEdit(
this );
99 mEmailEdit->setValidator(
new EmailValidator );
100 connect( mEmailEdit, TQ_SIGNAL( textChanged(
const TQString& ) ),
101 TQ_SLOT( textChanged(
const TQString& ) ) );
102 connect( mEmailEdit, TQ_SIGNAL( textChanged(
const TQString& ) ),
103 TQ_SIGNAL( modified() ) );
104 label->setBuddy( mEmailEdit );
105 topLayout->addWidget( mEmailEdit, 0, 1 );
107 mEditButton =
new TQPushButton( i18n(
"Edit Email Addresses..." ),
this);
108 connect( mEditButton, TQ_SIGNAL( clicked() ), TQ_SLOT( edit() ) );
109 topLayout->addMultiCellWidget( mEditButton, 1, 1, 0, 1 );
111 topLayout->activate();
114 EmailEditWidget::~EmailEditWidget()
118 void EmailEditWidget::setReadOnly(
bool readOnly )
120 mEmailEdit->setReadOnly( readOnly );
121 mEditButton->setEnabled( !readOnly );
124 void EmailEditWidget::setEmails(
const TQStringList &list )
128 bool blocked = mEmailEdit->signalsBlocked();
129 mEmailEdit->blockSignals(
true );
130 if ( list.count() > 0 )
131 mEmailEdit->setText( list[ 0 ] );
133 mEmailEdit->setText(
"" );
134 mEmailEdit->blockSignals( blocked );
137 TQStringList EmailEditWidget::emails()
139 if ( mEmailEdit->text().isEmpty() ) {
140 if ( mEmailList.count() > 0 )
141 mEmailList.remove( mEmailList.begin() );
143 if ( mEmailList.count() > 0 )
144 mEmailList.remove( mEmailList.begin() );
146 mEmailList.prepend( mEmailEdit->text() );
152 void EmailEditWidget::edit()
154 EmailEditDialog dlg( mEmailList,
this );
157 if ( dlg.changed() ) {
158 mEmailList = dlg.emails();
159 mEmailEdit->setText( mEmailList[ 0 ] );
165 void EmailEditWidget::textChanged(
const TQString &text )
167 if ( mEmailList.count() > 0 )
168 mEmailList.remove( mEmailList.begin() );
170 mEmailList.prepend( text );
174 EmailEditDialog::EmailEditDialog(
const TQStringList &list, TQWidget *parent,
176 : KDialogBase( KDialogBase::Plain, i18n(
"Edit Email Addresses" ),
177 KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Help,
180 TQWidget *page = plainPage();
182 TQGridLayout *topLayout =
new TQGridLayout( page, 4, 3, 0, spacingHint() );
184 mEmailListBox =
new TQListBox( page );
187 mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
188 connect( mEmailListBox, TQ_SIGNAL( highlighted(
int ) ),
189 TQ_SLOT( selectionChanged(
int ) ) );
190 connect( mEmailListBox, TQ_SIGNAL( selected(
int ) ),
192 topLayout->addMultiCellWidget( mEmailListBox, 0, 3, 0, 1 );
194 mAddButton =
new TQPushButton( i18n(
"Add..." ), page );
195 connect( mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( add() ) );
196 topLayout->addWidget( mAddButton, 0, 2 );
198 mEditButton =
new TQPushButton( i18n(
"Edit..." ), page );
199 connect( mEditButton, TQ_SIGNAL( clicked() ), TQ_SLOT( edit() ) );
200 topLayout->addWidget( mEditButton, 1, 2 );
202 mRemoveButton =
new TQPushButton( i18n(
"Remove" ), page );
203 connect( mRemoveButton, TQ_SIGNAL( clicked() ), TQ_SLOT( remove() ) );
204 topLayout->addWidget( mRemoveButton, 2, 2 );
206 mStandardButton =
new TQPushButton( i18n(
"Set Standard" ), page );
207 connect( mStandardButton, TQ_SIGNAL( clicked() ), TQ_SLOT( standard() ) );
208 topLayout->addWidget( mStandardButton, 3, 2 );
210 topLayout->activate();
212 TQStringList items = list;
213 if ( items.remove(
"" ) > 0 )
218 TQStringList::ConstIterator it;
219 bool preferred =
true;
220 for ( it = items.begin(); it != items.end(); ++it ) {
221 new EmailItem( mEmailListBox, *it, preferred );
226 selectionChanged( -1 );
227 TDEAcceleratorManager::manage(
this );
229 setInitialSize( TQSize( 400, 200 ) );
232 EmailEditDialog::~EmailEditDialog()
236 TQStringList EmailEditDialog::emails()
const
240 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
241 EmailItem *item =
static_cast<EmailItem*
>( mEmailListBox->item( i ) );
242 if ( item->preferred() )
243 emails.prepend( item->text() );
245 emails.append( item->text() );
251 void EmailEditDialog::add()
253 EmailValidator *validator =
new EmailValidator;
256 TQString email = KInputDialog::getText( i18n(
"Add Email" ), i18n(
"New Email:" ),
257 TQString(), &ok,
this,
"EmailEditDialog",
264 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
265 if ( mEmailListBox->text( i ) == email )
269 new EmailItem( mEmailListBox, email, (mEmailListBox->count() == 0) );
274 void EmailEditDialog::edit()
276 EmailValidator *validator =
new EmailValidator;
279 int editPos = mEmailListBox->currentItem();
281 TQString email = KInputDialog::getText( i18n(
"Edit Email" ), i18n(
"Email:" ),
282 mEmailListBox->text( editPos ), &ok,
this,
283 "EmailEditDialog", validator );
289 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
290 if ( mEmailListBox->text( i ) == email )
294 EmailItem *item =
static_cast<EmailItem*
>( mEmailListBox->item( editPos ) );
295 item->setText( email );
296 mEmailListBox->triggerUpdate(
true );
301 void EmailEditDialog::remove()
303 TQString address = mEmailListBox->currentText();
305 TQString text = i18n(
"<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>" ).arg( address );
306 TQString caption = i18n(
"Confirm Remove" );
308 if ( KMessageBox::warningContinueCancel(
this, text, caption, KGuiItem( i18n(
"&Delete"),
"edit-delete") ) == KMessageBox::Continue) {
309 EmailItem *item =
static_cast<EmailItem*
>( mEmailListBox->item( mEmailListBox->currentItem() ) );
311 bool preferred = item->preferred();
312 mEmailListBox->removeItem( mEmailListBox->currentItem() );
314 item =
dynamic_cast<EmailItem*
>( mEmailListBox->item( 0 ) );
316 item->setPreferred(
true );
323 bool EmailEditDialog::changed()
const
328 void EmailEditDialog::standard()
330 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
331 EmailItem *item =
static_cast<EmailItem*
>( mEmailListBox->item( i ) );
332 if ( (
int)i == mEmailListBox->currentItem() )
333 item->setPreferred(
true );
335 item->setPreferred(
false );
338 mEmailListBox->triggerUpdate(
true );
343 void EmailEditDialog::selectionChanged(
int index )
345 bool value = ( index >= 0 );
347 mRemoveButton->setEnabled( value );
348 mEditButton->setEnabled( value );
349 mStandardButton->setEnabled( value );
352 #include "emaileditwidget.moc"