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