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