00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qcheckbox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qpainter.h>
00028 #include <qpushbutton.h>
00029 #include <qvalidator.h>
00030 #include <qstring.h>
00031 #include <qtoolbutton.h>
00032 #include <qtooltip.h>
00033
00034 #include <kaccelmanager.h>
00035 #include <kconfig.h>
00036 #include <kcombobox.h>
00037 #include <kdebug.h>
00038 #include <kdialog.h>
00039 #include <kiconloader.h>
00040 #include <kinputdialog.h>
00041 #include <klineedit.h>
00042 #include <klocale.h>
00043 #include <kmessagebox.h>
00044
00045 #include "emaileditwidget.h"
00046
00047 class EmailValidator : public QRegExpValidator
00048 {
00049 public:
00050 EmailValidator()
00051 : QRegExpValidator( 0, "EmailValidator" )
00052 {
00053 QRegExp rx( ".*@.*\\.[A-Za-z]+" );
00054 setRegExp( rx );
00055 }
00056 };
00057
00058 class EmailItem : public QListBoxText
00059 {
00060 public:
00061 EmailItem( QListBox *parent, const QString &text, bool preferred )
00062 : QListBoxText( parent, text ), mPreferred( preferred )
00063 {}
00064
00065 void setPreferred( bool preferred ) { mPreferred = preferred; }
00066 bool preferred() const { return mPreferred; }
00067
00068 void setText( const QString &text )
00069 {
00070 QListBoxText::setText( text );
00071 }
00072
00073 protected:
00074 virtual void paint( QPainter *p )
00075 {
00076 if ( mPreferred ) {
00077 QFont font = p->font();
00078 font.setBold( true );
00079 p->setFont( font );
00080 }
00081
00082 QListBoxText::paint( p );
00083 }
00084
00085 private:
00086 bool mPreferred;
00087 };
00088
00089 EmailEditWidget::EmailEditWidget( QWidget *parent, const char *name )
00090 : QWidget( parent, name )
00091 {
00092 QGridLayout *topLayout = new QGridLayout( this, 2, 2, KDialog::marginHint(),
00093 KDialog::spacingHint() );
00094
00095 QLabel *label = new QLabel( i18n( "Email:" ), this );
00096 topLayout->addWidget( label, 0, 0 );
00097
00098 mEmailEdit = new KLineEdit( this );
00099 mEmailEdit->setValidator( new EmailValidator );
00100 connect( mEmailEdit, SIGNAL( textChanged( const QString& ) ),
00101 SLOT( textChanged( const QString& ) ) );
00102 connect( mEmailEdit, SIGNAL( textChanged( const QString& ) ),
00103 SIGNAL( modified() ) );
00104 label->setBuddy( mEmailEdit );
00105 topLayout->addWidget( mEmailEdit, 0, 1 );
00106
00107 mEditButton = new QPushButton( i18n( "Edit Email Addresses..." ), this);
00108 connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00109 topLayout->addMultiCellWidget( mEditButton, 1, 1, 0, 1 );
00110
00111 topLayout->activate();
00112 }
00113
00114 EmailEditWidget::~EmailEditWidget()
00115 {
00116 }
00117
00118 void EmailEditWidget::setReadOnly( bool readOnly )
00119 {
00120 mEmailEdit->setReadOnly( readOnly );
00121 mEditButton->setEnabled( !readOnly );
00122 }
00123
00124 void EmailEditWidget::setEmails( const QStringList &list )
00125 {
00126 mEmailList = list;
00127
00128 bool blocked = mEmailEdit->signalsBlocked();
00129 mEmailEdit->blockSignals( true );
00130 if ( list.count() > 0 )
00131 mEmailEdit->setText( list[ 0 ] );
00132 else
00133 mEmailEdit->setText( "" );
00134 mEmailEdit->blockSignals( blocked );
00135 }
00136
00137 QStringList EmailEditWidget::emails()
00138 {
00139 if ( mEmailEdit->text().isEmpty() ) {
00140 if ( mEmailList.count() > 0 )
00141 mEmailList.remove( mEmailList.begin() );
00142 } else {
00143 if ( mEmailList.count() > 0 )
00144 mEmailList.remove( mEmailList.begin() );
00145
00146 mEmailList.prepend( mEmailEdit->text() );
00147 }
00148
00149 return mEmailList;
00150 }
00151
00152 void EmailEditWidget::edit()
00153 {
00154 EmailEditDialog dlg( mEmailList, this );
00155
00156 if ( dlg.exec() ) {
00157 if ( dlg.changed() ) {
00158 mEmailList = dlg.emails();
00159 mEmailEdit->setText( mEmailList[ 0 ] );
00160 emit modified();
00161 }
00162 }
00163 }
00164
00165 void EmailEditWidget::textChanged( const QString &text )
00166 {
00167 if ( mEmailList.count() > 0 )
00168 mEmailList.remove( mEmailList.begin() );
00169
00170 mEmailList.prepend( text );
00171 }
00172
00173
00174 EmailEditDialog::EmailEditDialog( const QStringList &list, QWidget *parent,
00175 const char *name )
00176 : KDialogBase( KDialogBase::Plain, i18n( "Edit Email Addresses" ),
00177 KDialogBase::Ok | KDialogBase::Cancel, KDialogBase::Help,
00178 parent, name, true )
00179 {
00180 QWidget *page = plainPage();
00181
00182 QGridLayout *topLayout = new QGridLayout( page, 4, 3, 0, spacingHint() );
00183
00184 mEmailListBox = new QListBox( page );
00185
00186
00187 mEmailListBox->setMinimumHeight( mEmailListBox->sizeHint().height() + 30 );
00188 connect( mEmailListBox, SIGNAL( highlighted( int ) ),
00189 SLOT( selectionChanged( int ) ) );
00190 connect( mEmailListBox, SIGNAL( selected( int ) ),
00191 SLOT( edit() ) );
00192 topLayout->addMultiCellWidget( mEmailListBox, 0, 3, 0, 1 );
00193
00194 mAddButton = new QPushButton( i18n( "Add..." ), page );
00195 connect( mAddButton, SIGNAL( clicked() ), SLOT( add() ) );
00196 topLayout->addWidget( mAddButton, 0, 2 );
00197
00198 mEditButton = new QPushButton( i18n( "Edit..." ), page );
00199 connect( mEditButton, SIGNAL( clicked() ), SLOT( edit() ) );
00200 topLayout->addWidget( mEditButton, 1, 2 );
00201
00202 mRemoveButton = new QPushButton( i18n( "Remove" ), page );
00203 connect( mRemoveButton, SIGNAL( clicked() ), SLOT( remove() ) );
00204 topLayout->addWidget( mRemoveButton, 2, 2 );
00205
00206 mStandardButton = new QPushButton( i18n( "Set Standard" ), page );
00207 connect( mStandardButton, SIGNAL( clicked() ), SLOT( standard() ) );
00208 topLayout->addWidget( mStandardButton, 3, 2 );
00209
00210 topLayout->activate();
00211
00212 QStringList items = list;
00213 if ( items.remove( "" ) > 0 )
00214 mChanged = true;
00215 else
00216 mChanged = false;
00217
00218 QStringList::ConstIterator it;
00219 bool preferred = true;
00220 for ( it = items.begin(); it != items.end(); ++it ) {
00221 new EmailItem( mEmailListBox, *it, preferred );
00222 preferred = false;
00223 }
00224
00225
00226 selectionChanged( -1 );
00227 KAcceleratorManager::manage( this );
00228
00229 setInitialSize( QSize( 400, 200 ) );
00230 }
00231
00232 EmailEditDialog::~EmailEditDialog()
00233 {
00234 }
00235
00236 QStringList EmailEditDialog::emails() const
00237 {
00238 QStringList emails;
00239
00240 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
00241 EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
00242 if ( item->preferred() )
00243 emails.prepend( item->text() );
00244 else
00245 emails.append( item->text() );
00246 }
00247
00248 return emails;
00249 }
00250
00251 void EmailEditDialog::add()
00252 {
00253 EmailValidator *validator = new EmailValidator;
00254 bool ok = false;
00255
00256 QString email = KInputDialog::getText( i18n( "Add Email" ), i18n( "New Email:" ),
00257 QString::null, &ok, this, "EmailEditDialog",
00258 validator );
00259
00260 if ( !ok )
00261 return;
00262
00263
00264 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
00265 if ( mEmailListBox->text( i ) == email )
00266 return;
00267 }
00268
00269 new EmailItem( mEmailListBox, email, (mEmailListBox->count() == 0) );
00270
00271 mChanged = true;
00272 }
00273
00274 void EmailEditDialog::edit()
00275 {
00276 EmailValidator *validator = new EmailValidator;
00277 bool ok = false;
00278
00279 int editPos = mEmailListBox->currentItem();
00280
00281 QString email = KInputDialog::getText( i18n( "Edit Email" ), i18n( "Email:" ),
00282 mEmailListBox->text( editPos ), &ok, this,
00283 "EmailEditDialog", validator );
00284
00285 if ( !ok )
00286 return;
00287
00288
00289 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
00290 if ( mEmailListBox->text( i ) == email )
00291 return;
00292 }
00293
00294 EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( editPos ) );
00295 item->setText( email );
00296 mEmailListBox->triggerUpdate( true );
00297
00298 mChanged = true;
00299 }
00300
00301 void EmailEditDialog::remove()
00302 {
00303 QString address = mEmailListBox->currentText();
00304
00305 QString text = i18n( "<qt>Are you sure that you want to remove the email address <b>%1</b>?</qt>" ).arg( address );
00306 QString caption = i18n( "Confirm Remove" );
00307
00308 if ( KMessageBox::warningContinueCancel( this, text, caption, KGuiItem( i18n("&Delete"), "editdelete") ) == KMessageBox::Continue) {
00309 EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( mEmailListBox->currentItem() ) );
00310
00311 bool preferred = item->preferred();
00312 mEmailListBox->removeItem( mEmailListBox->currentItem() );
00313 if ( preferred ) {
00314 item = dynamic_cast<EmailItem*>( mEmailListBox->item( 0 ) );
00315 if ( item )
00316 item->setPreferred( true );
00317 }
00318
00319 mChanged = true;
00320 }
00321 }
00322
00323 bool EmailEditDialog::changed() const
00324 {
00325 return mChanged;
00326 }
00327
00328 void EmailEditDialog::standard()
00329 {
00330 for ( uint i = 0; i < mEmailListBox->count(); ++i ) {
00331 EmailItem *item = static_cast<EmailItem*>( mEmailListBox->item( i ) );
00332 if ( (int)i == mEmailListBox->currentItem() )
00333 item->setPreferred( true );
00334 else
00335 item->setPreferred( false );
00336 }
00337
00338 mEmailListBox->triggerUpdate( true );
00339
00340 mChanged = true;
00341 }
00342
00343 void EmailEditDialog::selectionChanged( int index )
00344 {
00345 bool value = ( index >= 0 );
00346
00347 mRemoveButton->setEnabled( value );
00348 mEditButton->setEnabled( value );
00349 mStandardButton->setEnabled( value );
00350 }
00351
00352 #include "emaileditwidget.moc"