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
00031 #include <QtGui/QTreeWidget>
00032 #include <QtGui/QLayout>
00033 #include <QtGui/QLabel>
00034 #include <QtGui/QPushButton>
00035 #include <QtGui/QComboBox>
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, i18n( "Yes" ) );
00132 } else {
00133 setText( 1, email );
00134 setText( 2, i18n( "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 QComboBox *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(5700) << "DistributionListEditor()";
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 QComboBox( 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 << i18n( "Name" ) << i18n( "Preferred Email" );
00231 d->mAddresseeView->setHeaderLabels( labels );
00232 gridLayout->addWidget( d->mAddresseeView, 1, 0 );
00233 connect( d->mAddresseeView, SIGNAL( itemSelectionChanged() ),
00234 SLOT( slotSelectionAddresseeViewChanged() ) );
00235 connect( d->mAddresseeView, SIGNAL( itemDoubleClicked( QTreeWidgetItem *, int ) ),
00236 SLOT( addEntry() ) );
00237
00238 d->mAddEntryButton = new QPushButton( i18n( "Add Entry" ), this );
00239 d->mAddEntryButton->setEnabled( false );
00240 gridLayout->addWidget( d->mAddEntryButton, 2, 0 );
00241 connect( d->mAddEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
00242
00243 d->mEntryView = new QTreeWidget( this );
00244 QStringList entryLabels;
00245 entryLabels << i18n( "Name" ) << i18n( "Email" ) << i18n( "Use Preferred" );
00246 d->mEntryView->setEnabled( false );
00247 gridLayout->addWidget( d->mEntryView, 1, 1, 1, 2 );
00248 connect( d->mEntryView, SIGNAL( itemSelectionChanged() ),
00249 SLOT( slotSelectionEntryViewChanged() ) );
00250
00251 d->mChangeEmailButton = new QPushButton( i18n( "Change Email..." ), this );
00252 gridLayout->addWidget( d->mChangeEmailButton, 2, 1 );
00253 connect( d->mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00254
00255 d->mRemoveEntryButton = new QPushButton( i18n( "Remove Entry" ), this );
00256 gridLayout->addWidget( d->mRemoveEntryButton, 2, 2 );
00257 connect( d->mRemoveEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
00258
00259 d->updateAddresseeView();
00260 d->updateNameCombo();
00261 }
00262
00263 DistributionListEditorWidget::~DistributionListEditorWidget()
00264 {
00265 delete d;
00266 }
00267
00268 void DistributionListEditorWidget::Private::save()
00269 {
00270
00271
00272
00273 }
00274
00275 void DistributionListEditorWidget::Private::slotSelectionEntryViewChanged()
00276 {
00277 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00278 bool state = selected.count() > 0;
00279 mChangeEmailButton->setEnabled( state );
00280 mRemoveEntryButton->setEnabled( state );
00281 }
00282
00283 void DistributionListEditorWidget::Private::newList()
00284 {
00285 bool ok;
00286 QString name = KInputDialog::getText( i18n( "New Distribution List" ),
00287 i18n( "Please enter &name:" ), QString(), &ok );
00288 if ( !ok ) {
00289 return;
00290 }
00291
00292 mAddressBook->createDistributionList( name );
00293
00294 mNameCombo->clear();
00295 mNameCombo->addItems( mAddressBook->allDistributionListNames() );
00296 mNameCombo->setCurrentIndex( mNameCombo->count() - 1 );
00297
00298 updateEntryView();
00299 slotSelectionAddresseeViewChanged();
00300 }
00301
00302 void DistributionListEditorWidget::Private::editList()
00303 {
00304 QString oldName = mNameCombo->currentText();
00305 bool ok;
00306 QString name = KInputDialog::getText( i18n( "Distribution List" ),
00307 i18n( "Please change &name:" ), oldName, &ok );
00308 if ( !ok ) {
00309 return;
00310 }
00311
00312 DistributionList *list = mAddressBook->findDistributionListByName( oldName );
00313 if ( list )
00314 list->setName( name );
00315
00316 mNameCombo->clear();
00317 mNameCombo->addItems( mAddressBook->allDistributionListNames() );
00318 mNameCombo->setCurrentIndex( mNameCombo->count() - 1 );
00319
00320 updateEntryView();
00321 slotSelectionAddresseeViewChanged();
00322 }
00323
00324 void DistributionListEditorWidget::Private::removeList()
00325 {
00326 int result = KMessageBox::warningContinueCancel( mParent,
00327 i18n( "Delete distribution list '%1'?", mNameCombo->currentText() ),
00328 QString(), KStandardGuiItem::del() );
00329
00330 if ( result != KMessageBox::Continue ) {
00331 return;
00332 }
00333
00334 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00335 if ( list ) {
00336
00337
00338 mAddressBook->removeDistributionList( list );
00339 mNameCombo->removeItem( mNameCombo->currentIndex() );
00340 }
00341
00342 updateEntryView();
00343 slotSelectionAddresseeViewChanged();
00344 }
00345
00346 void DistributionListEditorWidget::Private::addEntry()
00347 {
00348 QList<QTreeWidgetItem*> selected = mAddresseeView->selectedItems();
00349 if ( selected.count() == 0 ) {
00350 kDebug(5700) << "DLE::addEntry(): No addressee selected.";
00351 return;
00352 }
00353 AddresseeItem *addresseeItem =
00354 static_cast<AddresseeItem *>( selected.at( 0 ) );
00355
00356 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00357 if ( !list ) {
00358 kDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'";
00359 return;
00360 }
00361
00362 list->insertEntry( addresseeItem->addressee() );
00363 updateEntryView();
00364 slotSelectionAddresseeViewChanged();
00365 }
00366
00367 void DistributionListEditorWidget::Private::removeEntry()
00368 {
00369 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00370 if ( !list ) {
00371 return;
00372 }
00373
00374 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00375 if ( selected.count() == 0 ) {
00376 return;
00377 }
00378
00379 EntryItem *entryItem =
00380 static_cast<EntryItem *>( selected.at( 0 ) );
00381
00382 list->removeEntry( entryItem->addressee(), entryItem->email() );
00383 delete entryItem;
00384 }
00385
00386 void DistributionListEditorWidget::Private::changeEmail()
00387 {
00388 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00389 if ( !list ) {
00390 return;
00391 }
00392
00393 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00394 if ( selected.count() == 0 ) {
00395 return;
00396 }
00397
00398 EntryItem *entryItem =
00399 static_cast<EntryItem *>( selected.at( 0 ) );
00400
00401 QString email = EmailSelector::getEmail( entryItem->addressee().emails(),
00402 entryItem->email(), mParent );
00403 list->removeEntry( entryItem->addressee(), entryItem->email() );
00404 list->insertEntry( entryItem->addressee(), email );
00405
00406 updateEntryView();
00407 }
00408
00409 void DistributionListEditorWidget::Private::updateEntryView()
00410 {
00411 if ( mNameCombo->currentText().isEmpty() ) {
00412 mListLabel->setText( i18n( "Selected addressees:" ) );
00413 } else {
00414 mListLabel->setText( i18n( "Selected addresses in '%1':",
00415 mNameCombo->currentText() ) );
00416 }
00417
00418 mEntryView->clear();
00419
00420 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() );
00421 if ( !list ) {
00422 mEditButton->setEnabled( false );
00423 mRemoveButton->setEnabled( false );
00424 mChangeEmailButton->setEnabled( false );
00425 mRemoveEntryButton->setEnabled( false );
00426 mAddresseeView->setEnabled( false );
00427 mEntryView->setEnabled( false );
00428 return;
00429 } else {
00430 mEditButton->setEnabled( true );
00431 mRemoveButton->setEnabled( true );
00432 mAddresseeView->setEnabled( true );
00433 mEntryView->setEnabled( true );
00434 }
00435
00436 DistributionList::Entry::List entries = list->entries();
00437 DistributionList::Entry::List::ConstIterator it;
00438 for ( it = entries.begin(); it != entries.end(); ++it ) {
00439 new EntryItem( mEntryView, (*it).addressee(), (*it).email() );
00440 }
00441
00442 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems();
00443 bool state = ( selected.count() != 0 );
00444
00445 mChangeEmailButton->setEnabled( state );
00446 mRemoveEntryButton->setEnabled( state );
00447 }
00448
00449 void DistributionListEditorWidget::Private::updateAddresseeView()
00450 {
00451 mAddresseeView->clear();
00452
00453 AddressBook::Iterator it;
00454 for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00455 new AddresseeItem( mAddresseeView, *it );
00456 }
00457 }
00458
00459 void DistributionListEditorWidget::Private::updateNameCombo()
00460 {
00461 mNameCombo->addItems( mAddressBook->allDistributionListNames() );
00462
00463 updateEntryView();
00464 }
00465
00466 void DistributionListEditorWidget::Private::slotSelectionAddresseeViewChanged()
00467 {
00468 QList<QTreeWidgetItem*> selected = mAddresseeView->selectedItems();
00469 bool state = ( selected.count() != 0 );
00470 mAddEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty() );
00471 }
00472
00473 #include "distributionlistdialog.moc"