kabc
distributionlistdialog.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 <QtCore/QPointer> 00033 #include <QtGui/QTreeWidget> 00034 #include <QtGui/QLayout> 00035 #include <QtGui/QLabel> 00036 #include <QtGui/QPushButton> 00037 #include <QtGui/QGroupBox> 00038 #include <QtGui/QButtonGroup> 00039 #include <QtGui/QRadioButton> 00040 00041 using namespace KABC; 00042 00043 DistributionListDialog::DistributionListDialog( AddressBook *addressBook, QWidget *parent ) 00044 : KDialog( parent ), d( 0 ) 00045 { 00046 setModal( true ); 00047 setCaption( i18n( "Configure Distribution Lists" ) ); 00048 setButtons( Ok ); 00049 setDefaultButton( Ok ); 00050 showButtonSeparator( true ); 00051 00052 DistributionListEditorWidget *editor = new DistributionListEditorWidget( addressBook, this ); 00053 setMainWidget( editor ); 00054 00055 connect( this, SIGNAL(okClicked()), editor, SLOT(save()) ); 00056 } 00057 00058 DistributionListDialog::~DistributionListDialog() 00059 { 00060 } 00061 00062 class EmailSelector::Private 00063 { 00064 public: 00065 QButtonGroup *mButtonGroup; 00066 QMap<QWidget *, QString> mEmailMap; 00067 }; 00068 00069 EmailSelector::EmailSelector( const QStringList &emails, const QString ¤t, QWidget *parent ) 00070 : KDialog( parent ), d( new Private ) 00071 { 00072 setCaption( i18n( "Select Email Address" ) ); 00073 setButtons( Ok ); 00074 setDefaultButton( Ok ); 00075 00076 QFrame *topFrame = new QFrame( this ); 00077 setMainWidget( topFrame ); 00078 00079 QBoxLayout *topLayout = new QVBoxLayout( topFrame ); 00080 00081 QGroupBox *box = new QGroupBox( i18n( "Email Addresses" ) ); 00082 d->mButtonGroup = new QButtonGroup( box ); 00083 topLayout->addWidget( box ); 00084 00085 QVBoxLayout *layout = new QVBoxLayout; 00086 00087 QStringList::ConstIterator it; 00088 for ( it = emails.begin(); it != emails.end(); ++it ) { 00089 QRadioButton *button = new QRadioButton( *it, box ); 00090 d->mButtonGroup->addButton( button ); 00091 d->mEmailMap.insert( button, *it ); 00092 layout->addWidget( button ); 00093 if ( (*it) == current ) { 00094 button->setChecked( true ); 00095 } 00096 } 00097 layout->addStretch( 1 ); 00098 box->setLayout( layout ); 00099 } 00100 00101 EmailSelector::~EmailSelector() 00102 { 00103 delete d; 00104 } 00105 00106 QString EmailSelector::selected() const 00107 { 00108 QAbstractButton *button = d->mButtonGroup->checkedButton(); 00109 if ( !button ) { 00110 return QString(); 00111 } 00112 00113 return d->mEmailMap[button]; 00114 } 00115 00116 QString EmailSelector::getEmail( const QStringList &emails, const QString ¤t, 00117 QWidget *parent ) 00118 { 00119 QString email; 00120 00121 QPointer<EmailSelector> dlg = new EmailSelector( emails, current, parent ); 00122 if ( dlg->exec() && dlg ) { 00123 email = dlg->selected(); 00124 } 00125 00126 delete dlg; 00127 00128 return email; 00129 } 00130 00131 class EntryItem : public QTreeWidgetItem 00132 { 00133 public: 00134 EntryItem( QTreeWidget *parent, const Addressee &addressee, 00135 const QString &email=QString() ) : 00136 QTreeWidgetItem( parent ), 00137 mAddressee( addressee ), 00138 mEmail( email ) 00139 { 00140 setText( 0, addressee.realName() ); 00141 if ( email.isEmpty() ) { 00142 setText( 1, addressee.preferredEmail() ); 00143 setText( 2, i18nc( "this the preferred email address", "Yes" ) ); 00144 } else { 00145 setText( 1, email ); 00146 setText( 2, i18nc( "this is not the preferred email address", "No" ) ); 00147 } 00148 } 00149 00150 Addressee addressee() const 00151 { 00152 return mAddressee; 00153 } 00154 00155 QString email() const 00156 { 00157 return mEmail; 00158 } 00159 00160 private: 00161 Addressee mAddressee; 00162 QString mEmail; 00163 }; 00164 00165 class DistributionListEditorWidget::Private 00166 { 00167 public: 00168 Private( AddressBook *addressBook, DistributionListEditorWidget *parent ) 00169 : mParent( parent ), mAddressBook( addressBook ) 00170 { 00171 } 00172 00173 ~Private() 00174 { 00175 } 00176 00177 void newList(); 00178 void editList(); 00179 void removeList(); 00180 void addEntry(); 00181 void removeEntry(); 00182 void changeEmail(); 00183 void updateEntryView(); 00184 void updateAddresseeView(); 00185 void updateNameCombo(); 00186 void slotSelectionEntryViewChanged(); 00187 void slotSelectionAddresseeViewChanged(); 00188 void save(); 00189 00190 DistributionListEditorWidget *mParent; 00191 KComboBox *mNameCombo; 00192 QLabel *mListLabel; 00193 QTreeWidget *mEntryView; 00194 QTreeWidget *mAddresseeView; 00195 00196 AddressBook *mAddressBook; 00197 QPushButton *mNewButton, *mEditButton, *mRemoveButton; 00198 QPushButton *mChangeEmailButton, *mRemoveEntryButton, *mAddEntryButton; 00199 }; 00200 00201 DistributionListEditorWidget::DistributionListEditorWidget( AddressBook *addressBook, 00202 QWidget *parent ) 00203 : QWidget( parent ), d( new Private( addressBook, this ) ) 00204 { 00205 kDebug(); 00206 00207 QBoxLayout *topLayout = new QVBoxLayout( this ); 00208 00209 QBoxLayout *nameLayout = new QHBoxLayout(); 00210 topLayout->addLayout( topLayout ); 00211 00212 d->mNameCombo = new KComboBox( this ); 00213 nameLayout->addWidget( d->mNameCombo ); 00214 connect( d->mNameCombo, SIGNAL(activated(int)), SLOT(updateEntryView()) ); 00215 00216 d->mNewButton = new QPushButton( i18n( "New List..." ), this ); 00217 nameLayout->addWidget( d->mNewButton ); 00218 connect( d->mNewButton, SIGNAL(clicked()), SLOT(newList()) ); 00219 00220 d->mEditButton = new QPushButton( i18n( "Rename List..." ), this ); 00221 nameLayout->addWidget( d->mEditButton ); 00222 connect( d->mEditButton, SIGNAL(clicked()), SLOT(editList()) ); 00223 00224 d->mRemoveButton = new QPushButton( i18n( "Remove List" ), this ); 00225 nameLayout->addWidget( d->mRemoveButton ); 00226 connect( d->mRemoveButton, SIGNAL(clicked()), SLOT(removeList()) ); 00227 00228 QGridLayout *gridLayout = new QGridLayout(); 00229 topLayout->addLayout( gridLayout ); 00230 gridLayout->setColumnStretch( 1, 1 ); 00231 00232 QLabel *listLabel = new QLabel( i18n( "Available addresses:" ), this ); 00233 gridLayout->addWidget( listLabel, 0, 0 ); 00234 00235 d->mListLabel = new QLabel( this ); 00236 gridLayout->addWidget( d->mListLabel, 0, 0, 1, 2 ); 00237 00238 d->mAddresseeView = new QTreeWidget( this ); 00239 d->mAddresseeView->setColumnCount( 2 ); 00240 QStringList labels; 00241 labels << i18nc( "@title:column addressee name", "Name" ) 00242 << i18nc( "@title:column addressee preferred email", "Preferred Email" ); 00243 d->mAddresseeView->setHeaderLabels( labels ); 00244 gridLayout->addWidget( d->mAddresseeView, 1, 0 ); 00245 connect( d->mAddresseeView, SIGNAL(itemSelectionChanged()), 00246 SLOT(slotSelectionAddresseeViewChanged()) ); 00247 connect( d->mAddresseeView, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), 00248 SLOT(addEntry()) ); 00249 00250 d->mAddEntryButton = new QPushButton( i18n( "Add Entry" ), this ); 00251 d->mAddEntryButton->setEnabled( false ); 00252 gridLayout->addWidget( d->mAddEntryButton, 2, 0 ); 00253 connect( d->mAddEntryButton, SIGNAL(clicked()), SLOT(addEntry()) ); 00254 00255 d->mEntryView = new QTreeWidget( this ); 00256 QStringList entryLabels; 00257 entryLabels << i18nc( "@title:column addressee name", "Name" ) 00258 << i18nc( "@title:column addressee preferred email", "Email" ) 00259 << i18nc( "@title:column use preferred email", "Use Preferred" ); 00260 d->mEntryView->setEnabled( false ); 00261 gridLayout->addWidget( d->mEntryView, 1, 1, 1, 2 ); 00262 connect( d->mEntryView, SIGNAL(itemSelectionChanged()), 00263 SLOT(slotSelectionEntryViewChanged()) ); 00264 00265 d->mChangeEmailButton = new QPushButton( i18n( "Change Email..." ), this ); 00266 gridLayout->addWidget( d->mChangeEmailButton, 2, 1 ); 00267 connect( d->mChangeEmailButton, SIGNAL(clicked()), SLOT(changeEmail()) ); 00268 00269 d->mRemoveEntryButton = new QPushButton( i18n( "Remove Entry" ), this ); 00270 gridLayout->addWidget( d->mRemoveEntryButton, 2, 2 ); 00271 connect( d->mRemoveEntryButton, SIGNAL(clicked()), SLOT(removeEntry()) ); 00272 00273 d->updateAddresseeView(); 00274 d->updateNameCombo(); 00275 } 00276 00277 DistributionListEditorWidget::~DistributionListEditorWidget() 00278 { 00279 delete d; 00280 } 00281 00282 void DistributionListEditorWidget::Private::save() 00283 { 00284 // FIXME new distribution list handling 00285 // do we need extra save? 00286 //mManager->save(); 00287 } 00288 00289 void DistributionListEditorWidget::Private::slotSelectionEntryViewChanged() 00290 { 00291 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems(); 00292 bool state = selected.count() > 0; 00293 mChangeEmailButton->setEnabled( state ); 00294 mRemoveEntryButton->setEnabled( state ); 00295 } 00296 00297 void DistributionListEditorWidget::Private::newList() 00298 { 00299 bool ok; 00300 QString name = KInputDialog::getText( i18n( "New Distribution List" ), 00301 i18n( "Please enter &name:" ), QString(), &ok ); 00302 if ( !ok ) { 00303 return; 00304 } 00305 00306 mAddressBook->createDistributionList( name ); 00307 00308 mNameCombo->clear(); 00309 mNameCombo->addItems( mAddressBook->allDistributionListNames() ); 00310 mNameCombo->setCurrentIndex( mNameCombo->count() - 1 ); 00311 00312 updateEntryView(); 00313 slotSelectionAddresseeViewChanged(); 00314 } 00315 00316 void DistributionListEditorWidget::Private::editList() 00317 { 00318 QString oldName = mNameCombo->currentText(); 00319 bool ok; 00320 QString name = KInputDialog::getText( i18n( "Distribution List" ), 00321 i18n( "Please change &name:" ), oldName, &ok ); 00322 if ( !ok ) { 00323 return; 00324 } 00325 00326 DistributionList *list = mAddressBook->findDistributionListByName( oldName ); 00327 if ( list ) { 00328 list->setName( name ); 00329 } 00330 00331 mNameCombo->clear(); 00332 mNameCombo->addItems( mAddressBook->allDistributionListNames() ); 00333 mNameCombo->setCurrentIndex( mNameCombo->count() - 1 ); 00334 00335 updateEntryView(); 00336 slotSelectionAddresseeViewChanged(); 00337 } 00338 00339 void DistributionListEditorWidget::Private::removeList() 00340 { 00341 int result = KMessageBox::warningContinueCancel( mParent, 00342 i18n( "Delete distribution list '%1'?", mNameCombo->currentText() ), 00343 QString(), KStandardGuiItem::del() ); 00344 00345 if ( result != KMessageBox::Continue ) { 00346 return; 00347 } 00348 00349 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() ); 00350 if ( list ) { 00351 // FIXME new distribution list handling 00352 // list should be deleted, no? 00353 mAddressBook->removeDistributionList( list ); 00354 mNameCombo->removeItem( mNameCombo->currentIndex() ); 00355 } 00356 00357 updateEntryView(); 00358 slotSelectionAddresseeViewChanged(); 00359 } 00360 00361 void DistributionListEditorWidget::Private::addEntry() 00362 { 00363 QList<QTreeWidgetItem*> selected = mAddresseeView->selectedItems(); 00364 if ( selected.count() == 0 ) { 00365 kDebug() << "No addressee selected."; 00366 return; 00367 } 00368 AddresseeItem *addresseeItem = 00369 static_cast<AddresseeItem *>( selected.at( 0 ) ); 00370 00371 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() ); 00372 if ( !list ) { 00373 kDebug() << "No dist list '" << mNameCombo->currentText() << "'"; 00374 return; 00375 } 00376 00377 list->insertEntry( addresseeItem->addressee() ); 00378 updateEntryView(); 00379 slotSelectionAddresseeViewChanged(); 00380 } 00381 00382 void DistributionListEditorWidget::Private::removeEntry() 00383 { 00384 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() ); 00385 if ( !list ) { 00386 return; 00387 } 00388 00389 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems(); 00390 if ( selected.count() == 0 ) { 00391 return; 00392 } 00393 00394 EntryItem *entryItem = 00395 static_cast<EntryItem *>( selected.at( 0 ) ); 00396 00397 list->removeEntry( entryItem->addressee(), entryItem->email() ); 00398 delete entryItem; 00399 } 00400 00401 void DistributionListEditorWidget::Private::changeEmail() 00402 { 00403 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() ); 00404 if ( !list ) { 00405 return; 00406 } 00407 00408 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems(); 00409 if ( selected.count() == 0 ) { 00410 return; 00411 } 00412 00413 EntryItem *entryItem = 00414 static_cast<EntryItem *>( selected.at( 0 ) ); 00415 00416 QString email = EmailSelector::getEmail( entryItem->addressee().emails(), 00417 entryItem->email(), mParent ); 00418 list->removeEntry( entryItem->addressee(), entryItem->email() ); 00419 list->insertEntry( entryItem->addressee(), email ); 00420 00421 updateEntryView(); 00422 } 00423 00424 void DistributionListEditorWidget::Private::updateEntryView() 00425 { 00426 if ( mNameCombo->currentText().isEmpty() ) { 00427 mListLabel->setText( i18n( "Selected addressees:" ) ); 00428 } else { 00429 mListLabel->setText( i18n( "Selected addresses in '%1':", 00430 mNameCombo->currentText() ) ); 00431 } 00432 00433 mEntryView->clear(); 00434 00435 DistributionList *list = mAddressBook->findDistributionListByName( mNameCombo->currentText() ); 00436 if ( !list ) { 00437 mEditButton->setEnabled( false ); 00438 mRemoveButton->setEnabled( false ); 00439 mChangeEmailButton->setEnabled( false ); 00440 mRemoveEntryButton->setEnabled( false ); 00441 mAddresseeView->setEnabled( false ); 00442 mEntryView->setEnabled( false ); 00443 return; 00444 } else { 00445 mEditButton->setEnabled( true ); 00446 mRemoveButton->setEnabled( true ); 00447 mAddresseeView->setEnabled( true ); 00448 mEntryView->setEnabled( true ); 00449 } 00450 00451 DistributionList::Entry::List entries = list->entries(); 00452 DistributionList::Entry::List::ConstIterator it; 00453 for ( it = entries.constBegin(); it != entries.constEnd(); ++it ) { 00454 new EntryItem( mEntryView, (*it).addressee(), (*it).email() ); 00455 } 00456 00457 QList<QTreeWidgetItem*> selected = mEntryView->selectedItems(); 00458 bool state = ( selected.count() != 0 ); 00459 00460 mChangeEmailButton->setEnabled( state ); 00461 mRemoveEntryButton->setEnabled( state ); 00462 } 00463 00464 void DistributionListEditorWidget::Private::updateAddresseeView() 00465 { 00466 mAddresseeView->clear(); 00467 00468 AddressBook::Iterator it; 00469 for ( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) { 00470 new AddresseeItem( mAddresseeView, *it ); 00471 } 00472 } 00473 00474 void DistributionListEditorWidget::Private::updateNameCombo() 00475 { 00476 mNameCombo->addItems( mAddressBook->allDistributionListNames() ); 00477 00478 updateEntryView(); 00479 } 00480 00481 void DistributionListEditorWidget::Private::slotSelectionAddresseeViewChanged() 00482 { 00483 QList<QTreeWidgetItem*> selected = mAddresseeView->selectedItems(); 00484 bool state = ( selected.count() != 0 ); 00485 mAddEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty() ); 00486 } 00487 00488 #include "distributionlistdialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.