00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "distributionlistdialog.h"
00022 #include "distributionlist.h"
00023 #include "addressbook.h"
00024 #include "addresseedialog.h"
00025
00026 #include <kinputdialog.h>
00027 #include <klocale.h>
00028 #include <kdebug.h>
00029 #include <kmessagebox.h>
00030 #include <kcombobox.h>
00031
00032 #include <QtGui/QTreeWidget>
00033 #include <QtGui/QLayout>
00034 #include <QtGui/QLabel>
00035 #include <QtGui/QPushButton>
00036 #include <QtGui/QGroupBox>
00037 #include <QtGui/QButtonGroup>
00038 #include <QtGui/QRadioButton>
00039
00040 using namespace KABC;
00041
00042 DistributionListDialog::DistributionListDialog( AddressBook *addressBook, QWidget *parent )
00043 : KDialog( parent ), d( 0 )
00044 {
00045 setModal( true );
00046 setCaption( i18n( "Configure Distribution Lists" ) );
00047 setButtons( Ok );
00048 setDefaultButton( Ok );
00049 showButtonSeparator( true );
00050
00051 DistributionListEditorWidget *editor = new DistributionListEditorWidget( addressBook, this );
00052 setMainWidget( editor );
00053
00054 connect( this, SIGNAL( okClicked() ), editor, SLOT( save() ) );
00055 }
00056
00057 DistributionListDialog::~DistributionListDialog()
00058 {
00059 }
00060
00061 class EmailSelector::Private
00062 {
00063 public:
00064 QButtonGroup *mButtonGroup;
00065 QMap<QWidget *, QString> mEmailMap;
00066 };
00067
00068 EmailSelector::EmailSelector( const QStringList &emails, const QString ¤t, QWidget *parent )
00069 : KDialog( parent ), d( new Private )
00070 {
00071 setCaption( i18n( "Select Email Address" ) );
00072 setButtons( Ok );
00073 setDefaultButton( Ok );
00074
00075 QFrame *topFrame = new QFrame( this );
00076 setMainWidget( topFrame );
00077
00078 QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00079
00080 QGroupBox *box = new QGroupBox( i18n( "Email Addresses" ) );
00081 d->mButtonGroup = new QButtonGroup( box );
00082 topLayout->addWidget( box );
00083
00084 QStringList::ConstIterator it;
00085 for ( it = emails.begin(); it != emails.end(); ++it ) {
00086 QRadioButton *button = new QRadioButton( *it, box );
00087 d->mButtonGroup->addButton( button );
00088 d->mEmailMap.insert( button, *it );
00089 if ( (*it) == current ) {
00090 button->setChecked( true );
00091 }
00092 }
00093 }
00094
00095 EmailSelector::~EmailSelector()
00096 {
00097 delete d;
00098 }
00099
00100 QString EmailSelector::selected() const
00101 {
00102 QAbstractButton *button = d->mButtonGroup->checkedButton();
00103 if ( !button ) {
00104 return QString();
00105 }
00106
00107 return d->mEmailMap[button];
00108 }
00109
00110 QString EmailSelector::getEmail( const QStringList &emails, const QString ¤t,
00111 QWidget *parent )
00112 {
00113 EmailSelector dlg( emails, current, parent );
00114 dlg.exec();
00115
00116 return dlg.selected();
00117 }
00118
00119 class EntryItem : public QTreeWidgetItem
00120 {
00121 public:
00122 EntryItem( QTreeWidget *parent, const Addressee &addressee,
00123 const QString &email=QString() ) :
00124 QTreeWidgetItem( parent ),
00125 mAddressee( addressee ),
00126 mEmail( email )
00127 {
00128 setText( 0, addressee.realName() );
00129 if ( email.isEmpty() ) {
00130 setText( 1, addressee.preferredEmail() );
00131 setText( 2, i18nc( "this the preferred email address", "Yes" ) );
00132 } else {
00133 setText( 1, email );
00134 setText( 2, i18nc( "this is not the preferred email address", "No" ) );
00135 }
00136 }
00137
00138 Addressee addressee() const
00139 {
00140 return mAddressee;
00141 }
00142
00143 QString email() const
00144 {
00145 return mEmail;
00146 }
00147
00148 private:
00149 Addressee mAddressee;
00150 QString mEmail;
00151 };
00152
00153 class DistributionListEditorWidget::Private
00154 {
00155 public:
00156 Private( AddressBook *addressBook, DistributionListEditorWidget *parent )
00157 : mParent( parent ), mAddressBook( addressBook )
00158 {
00159 }
00160
00161 ~Private()
00162 {
00163 }
00164
00165 void newList();
00166 void editList();
00167 void removeList();
00168 void addEntry();
00169 void removeEntry();
00170 void changeEmail();
00171 void updateEntryView();
00172 void updateAddresseeView();
00173 void updateNameCombo();
00174 void slotSelectionEntryViewChanged();
00175 void slotSelectionAddresseeViewChanged();
00176 void save();
00177
00178 DistributionListEditorWidget *mParent;
00179 KComboBox *mNameCombo;
00180 QLabel *mListLabel;
00181 QTreeWidget *mEntryView;
00182 QTreeWidget *mAddresseeView;
00183
00184 AddressBook *mAddressBook;
00185 QPushButton *mNewButton, *mEditButton, *mRemoveButton;
00186 QPushButton *mChangeEmailButton, *mRemoveEntryButton, *mAddEntryButton;
00187 };
00188
00189 DistributionListEditorWidget::DistributionListEditorWidget( AddressBook *addressBook,
00190 QWidget *parent )
00191 : QWidget( parent ), d( new Private( addressBook, this ) )
00192 {
00193 kDebug();
00194
00195 QBoxLayout *topLayout = new QVBoxLayout( this );
00196 topLayout->setSpacing( KDialog::spacingHint() );
00197
00198 QBoxLayout *nameLayout = new QHBoxLayout();
00199 topLayout->addLayout( topLayout );
00200
00201 d->mNameCombo = new KComboBox( this );
00202 nameLayout->addWidget( d->mNameCombo );
00203 connect( d->mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) );
00204
00205 d->mNewButton = new QPushButton( i18n( "New List..." ), this );
00206 nameLayout->addWidget( d->mNewButton );
00207 connect( d->mNewButton, SIGNAL( clicked() ), SLOT( newList() ) );
00208
00209 d->mEditButton = new QPushButton( i18n( "Rename List..." ), this );
00210 nameLayout->addWidget( d->mEditButton );
00211 connect( d->mEditButton, SIGNAL( clicked() ), SLOT( editList() ) );
00212
00213 d->mRemoveButton = new QPushButton( i18n( "Remove List" ), this );
00214 nameLayout->addWidget( d->mRemoveButton );
00215 connect( d->mRemoveButton, SIGNAL( clicked() ), SLOT( removeList() ) );
00216
00217 QGridLayout *gridLayout = new QGridLayout();
00218 topLayout->addLayout( gridLayout );
00219 gridLayout->setColumnStretch( 1, 1 );
00220
00221 QLabel *listLabel = new QLabel( i18n( "Available addresses:" ), this );
00222 gridLayout->addWidget( listLabel, 0, 0 );
00223
00224 d->mListLabel = new QLabel( this );
00225 gridLayout->addWidget( d->mListLabel, 0, 0, 1, 2 );
00226
00227 d->mAddresseeView = new QTreeWidget( this );
00228 d->mAddresseeView->setColumnCount( 2 );
00229 QStringList labels;
00230 labels << i18nc( "@title:column addressee name", "Name" )
00231 << i18nc( "@title:column addressee preferred email", "Preferred Email" );
00232 d->mAddresseeView->setHeaderLabels( labels );
00233 gridLayout->addWidget( d->mAddresseeView, 1, 0 );
00234 connect( d->mAddresseeView, SIGNAL( itemSelectionChanged() ),
00235 SLOT( slotSelectionAddresseeViewChanged() ) );
00236 connect( d->mAddresseeView, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00237 SLOT( addEntry() ) );
00238
00239 d->mAddEntryButton = new QPushButton( i18n( "Add Entry" ), this );
00240 d->mAddEntryButton->setEnabled( false );
00241 gridLayout->addWidget( d->mAddEntryButton, 2, 0 );
00242 connect( d->mAddEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
00243
00244 d->mEntryView = new QTreeWidget( this );
00245 QStringList entryLabels;
00246 entryLabels << i18nc( "@title:column addressee name", "Name" )
00247 << i18nc( "@title:column addressee preferred email", "Email" )
00248 << i18nc( "@title:column use preferred email", "Use Preferred" );
00249 d->mEntryView->setEnabled( false );
00250 gridLayout->addWidget( d->mEntryView, 1, 1, 1, 2 );
00251 connect( d->mEntryView, SIGNAL( itemSelectionChanged() ),
00252 SLOT( slotSelectionEntryViewChanged() ) );
00253
00254 d->mChangeEmailButton = new QPushButton( i18n( "Change Email..." ), this );
00255 gridLayout->addWidget( d->mChangeEmailButton, 2, 1 );
00256 connect( d->mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00257
00258 d->mRemoveEntryButton = new QPushButton( i18n( "Remove Entry" ), this );
00259 gridLayout->addWidget( d->mRemoveEntryButton, 2, 2 );
00260 connect( d->mRemoveEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
00261
00262 d->updateAddresseeView();
00263 d->updateNameCombo();
00264 }
00265
00266 DistributionListEditorWidget::~DistributionListEditorWidget()
00267 {
00268 delete d;
00269 }
00270
00271 void DistributionListEditorWidget::Private::save()
00272 {
00273
00274
00275
00276 }
00277
00278 void DistributionListEditorWidget::Private::slotSelectionEntryViewChanged()
00279 {
00280 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00281 bool state = selected.count() > 0;
00282 mChangeEmailButton->setEnabled( state );
00283 mRemoveEntryButton->setEnabled( state );
00284 }
00285
00286 void DistributionListEditorWidget::Private::newList()
00287 {
00288 bool ok;
00289 QString name = KInputDialog::getText( i18n( "New Distribution List" ),
00290 i18n( "Please enter &name:" ), QString(), &ok );
00291 if ( !ok ) {
00292 return;
00293 }
00294
00295 mAddressBook->createDistributionList( name );
00296
00297 mNameCombo->clear();
00298 mNameCombo->addItems( mAddressBook->allDistributionListNames() );
00299 mNameCombo->setCurrentIndex( mNameCombo->count() - 1 );
00300
00301 updateEntryView();
00302 slotSelectionAddresseeViewChanged();
00303 }
00304
00305 void DistributionListEditorWidget::Private::editList()
00306 {
00307 QString oldName = mNameCombo->currentText();
00308 bool ok;
00309 QString name = KInputDialog::getText( i18n( "Distribution List" ),
00310 i18n( "Please change &name:" ), oldName, &ok );
00311 if ( !ok ) {
00312 return;
00313 }
00314
00315 DistributionList *list = mAddressBook->findDistributionListByName( oldName );
00316 if ( list ) {
00317 list->setName( name );
00318 }
00319
00320 mNameCombo->clear();
00321 mNameCombo->addItems( mAddressBook->allDistributionListNames() );
00322 mNameCombo->setCurrentIndex( mNameCombo->count() - 1 );
00323
00324 updateEntryView();
00325 slotSelectionAddresseeViewChanged();
00326 }
00327
00328 void DistributionListEditorWidget::Private::removeList()
00329 {
00330 int result = KMessageBox::warningContinueCancel( mParent,
00331 i18n( "Delete distribution list '%1'?", mNameCombo->currentText() ),
00332 QString(), KStandardGuiItem::del() );
00333
00334 if ( result != KMessageBox::Continue ) {
00335 return;
00336 }
00337
00338 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00339 if ( list ) {
00340
00341
00342 mAddressBook->removeDistributionList( list );
00343 mNameCombo->removeItem( mNameCombo->currentIndex() );
00344 }
00345
00346 updateEntryView();
00347 slotSelectionAddresseeViewChanged();
00348 }
00349
00350 void DistributionListEditorWidget::Private::addEntry()
00351 {
00352 QList<QTreeWidgetItem*> selected = mAddresseeView->selectedItems();
00353 if ( selected.count() == 0 ) {
00354 kDebug() << "No addressee selected.";
00355 return;
00356 }
00357 AddresseeItem *addresseeItem =
00358 static_cast<AddresseeItem *>( selected.at( 0 ) );
00359
00360 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00361 if ( !list ) {
00362 kDebug() << "No dist list '" << mNameCombo->currentText() << "'";
00363 return;
00364 }
00365
00366 list->insertEntry( addresseeItem->addressee() );
00367 updateEntryView();
00368 slotSelectionAddresseeViewChanged();
00369 }
00370
00371 void DistributionListEditorWidget::Private::removeEntry()
00372 {
00373 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00374 if ( !list ) {
00375 return;
00376 }
00377
00378 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00379 if ( selected.count() == 0 ) {
00380 return;
00381 }
00382
00383 EntryItem *entryItem =
00384 static_cast<EntryItem *>( selected.at( 0 ) );
00385
00386 list->removeEntry( entryItem->addressee(), entryItem->email() );
00387 delete entryItem;
00388 }
00389
00390 void DistributionListEditorWidget::Private::changeEmail()
00391 {
00392 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00393 if ( !list ) {
00394 return;
00395 }
00396
00397 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00398 if ( selected.count() == 0 ) {
00399 return;
00400 }
00401
00402 EntryItem *entryItem =
00403 static_cast<EntryItem *>( selected.at( 0 ) );
00404
00405 QString email = EmailSelector::getEmail( entryItem->addressee().emails(),
00406 entryItem->email(), mParent );
00407 list->removeEntry( entryItem->addressee(), entryItem->email() );
00408 list->insertEntry( entryItem->addressee(), email );
00409
00410 updateEntryView();
00411 }
00412
00413 void DistributionListEditorWidget::Private::updateEntryView()
00414 {
00415 if ( mNameCombo->currentText().isEmpty() ) {
00416 mListLabel->setText( i18n( "Selected addressees:" ) );
00417 } else {
00418 mListLabel->setText( i18n( "Selected addresses in '%1':",
00419 mNameCombo->currentText() ) );
00420 }
00421
00422 mEntryView->clear();
00423
00424 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00425 if ( !list ) {
00426 mEditButton->setEnabled( false );
00427 mRemoveButton->setEnabled( false );
00428 mChangeEmailButton->setEnabled( false );
00429 mRemoveEntryButton->setEnabled( false );
00430 mAddresseeView->setEnabled( false );
00431 mEntryView->setEnabled( false );
00432 return;
00433 } else {
00434 mEditButton->setEnabled( true );
00435 mRemoveButton->setEnabled( true );
00436 mAddresseeView->setEnabled( true );
00437 mEntryView->setEnabled( true );
00438 }
00439
00440 DistributionList::Entry::List entries = list->entries();
00441 DistributionList::Entry::List::ConstIterator it;
00442 for ( it = entries.begin(); it != entries.end(); ++it ) {
00443 new EntryItem( mEntryView, (*it).addressee(), (*it).email() );
00444 }
00445
00446 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00447 bool state = ( selected.count() != 0 );
00448
00449 mChangeEmailButton->setEnabled( state );
00450 mRemoveEntryButton->setEnabled( state );
00451 }
00452
00453 void DistributionListEditorWidget::Private::updateAddresseeView()
00454 {
00455 mAddresseeView->clear();
00456
00457 AddressBook::Iterator it;
00458 for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00459 new AddresseeItem( mAddresseeView, *it );
00460 }
00461 }
00462
00463 void DistributionListEditorWidget::Private::updateNameCombo()
00464 {
00465 mNameCombo->addItems( mAddressBook->allDistributionListNames() );
00466
00467 updateEntryView();
00468 }
00469
00470 void DistributionListEditorWidget::Private::slotSelectionAddresseeViewChanged()
00471 {
00472 QList<QTreeWidgetItem*> selected = mAddresseeView->selectedItems();
00473 bool state = ( selected.count() != 0 );
00474 mAddEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty() );
00475 }
00476
00477 #include "distributionlistdialog.moc"