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