kabc Library API Documentation

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., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <qlistview.h>
00022 #include <qlayout.h>
00023 #include <qlabel.h>
00024 #include <qpushbutton.h>
00025 #include <qcombobox.h>
00026 #include <kinputdialog.h>
00027 #include <qbuttongroup.h>
00028 #include <qradiobutton.h>
00029 
00030 #include <klocale.h>
00031 #include <kdebug.h>
00032 #include <kmessagebox.h>
00033 
00034 #include "addressbook.h"
00035 #include "addresseedialog.h"
00036 #include "distributionlist.h"
00037 
00038 #include "distributionlistdialog.h"
00039 #include "distributionlistdialog.moc"
00040 
00041 using namespace KABC;
00042 
00043 DistributionListDialog::DistributionListDialog( AddressBook *addressBook, QWidget *parent)
00044     : KDialogBase( parent, "", true, i18n("Configure Distribution Lists"), Ok, Ok, true)
00045 {
00046   mEditor = new DistributionListEditorWidget( addressBook, this );
00047   setMainWidget( mEditor );
00048 
00049   connect( this, SIGNAL( okClicked() ), mEditor, SLOT( save() ) );
00050 }
00051 
00052 DistributionListDialog::~DistributionListDialog()
00053 {
00054 }
00055 
00056 
00057 EmailSelector::EmailSelector( const QStringList &emails, const QString &current,
00058                                       QWidget *parent ) :
00059   KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
00060                parent )
00061 {
00062   QFrame *topFrame = plainPage();
00063   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00064 
00065   mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
00066                                    topFrame );
00067   topLayout->addWidget( mButtonGroup );
00068 
00069   QStringList::ConstIterator it;
00070   for( it = emails.begin(); it != emails.end(); ++it ) {
00071     QRadioButton *button = new QRadioButton( *it, mButtonGroup );
00072     if ( (*it) == current ) {
00073       button->setDown( true );
00074     }
00075   }
00076 }
00077 
00078 QString EmailSelector::selected()
00079 {
00080   QButton *button = mButtonGroup->selected();
00081   if ( button ) return button->text();
00082   return QString::null;
00083 }
00084 
00085 QString EmailSelector::getEmail( const QStringList &emails, const QString &current,
00086                                      QWidget *parent )
00087 {
00088   EmailSelector *dlg = new EmailSelector( emails, current, parent );
00089   dlg->exec();
00090 
00091   QString result = dlg->selected();
00092 
00093   delete dlg;
00094 
00095   return result;
00096 }
00097 
00098 class EntryItem : public QListViewItem
00099 {
00100   public:
00101     EntryItem( QListView *parent, const Addressee &addressee,
00102                const QString &email=QString::null ) :
00103       QListViewItem( parent ),
00104       mAddressee( addressee ),
00105       mEmail( email )
00106     {
00107       setText( 0, addressee.realName() );
00108       if( email.isEmpty() ) {
00109         setText( 1, addressee.preferredEmail() );
00110         setText( 2, i18n("Yes") );
00111       } else {
00112         setText( 1, email );
00113         setText( 2, i18n("No") );
00114       }
00115     }
00116 
00117     Addressee addressee() const
00118     {
00119       return mAddressee;
00120     }
00121 
00122     QString email() const
00123     {
00124       return mEmail;
00125     }
00126 
00127   private:
00128     Addressee mAddressee;
00129     QString mEmail;
00130 };
00131 
00132 DistributionListEditorWidget::DistributionListEditorWidget( AddressBook *addressBook, QWidget *parent) :
00133   QWidget( parent ),
00134   mAddressBook( addressBook )
00135 {
00136   kdDebug(5700) << "DistributionListEditor()" << endl;
00137 
00138   QBoxLayout *topLayout = new QVBoxLayout( this );
00139   topLayout->setSpacing( KDialog::spacingHint() );
00140 
00141   QBoxLayout *nameLayout = new QHBoxLayout( topLayout) ;
00142 
00143   mNameCombo = new QComboBox( this );
00144   nameLayout->addWidget( mNameCombo );
00145   connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) );
00146 
00147   mNewButton = new QPushButton( i18n("New List..."), this );
00148   nameLayout->addWidget( mNewButton );
00149   connect( mNewButton, SIGNAL( clicked() ), SLOT( newList() ) );
00150 
00151   mEditButton = new QPushButton( i18n("Rename List..."), this );
00152   nameLayout->addWidget( mEditButton );
00153   connect( mEditButton, SIGNAL( clicked() ), SLOT( editList() ) );
00154 
00155   mRemoveButton = new QPushButton( i18n("Remove List"), this );
00156   nameLayout->addWidget( mRemoveButton );
00157   connect( mRemoveButton, SIGNAL( clicked() ), SLOT( removeList() ) );
00158 
00159   QGridLayout *gridLayout = new QGridLayout( topLayout, 3, 3 );
00160   gridLayout->setColStretch(1, 1);
00161 
00162   QLabel *listLabel = new QLabel( i18n("Available addresses:"), this );
00163   gridLayout->addWidget( listLabel, 0, 0 );
00164 
00165   mListLabel = new QLabel( this );
00166   gridLayout->addMultiCellWidget( mListLabel, 0, 0, 1, 2 );
00167 
00168   mAddresseeView = new QListView( this );
00169   mAddresseeView->addColumn( i18n("Name") );
00170   mAddresseeView->addColumn( i18n("Preferred Email") );
00171   mAddresseeView->setAllColumnsShowFocus( true );
00172   gridLayout->addWidget( mAddresseeView, 1, 0 );
00173   connect( mAddresseeView, SIGNAL( selectionChanged() ),
00174            SLOT( slotSelectionAddresseeViewChanged() ) );
00175   connect( mAddresseeView, SIGNAL( doubleClicked( QListViewItem * ) ),
00176            SLOT( addEntry() ) );
00177 
00178   mAddEntryButton = new QPushButton( i18n("Add Entry"), this );
00179   mAddEntryButton->setEnabled(false);
00180   gridLayout->addWidget( mAddEntryButton, 2, 0 );
00181   connect( mAddEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
00182 
00183   mEntryView = new QListView( this );
00184   mEntryView->addColumn( i18n("Name") );
00185   mEntryView->addColumn( i18n("Email") );
00186   mEntryView->addColumn( i18n("Use Preferred") );
00187   mEntryView->setEnabled(false);
00188   mEntryView->setAllColumnsShowFocus( true );
00189   gridLayout->addMultiCellWidget( mEntryView, 1, 1, 1, 2 );
00190   connect( mEntryView, SIGNAL( selectionChanged() ),
00191            SLOT( slotSelectionEntryViewChanged() ) );
00192 
00193   mChangeEmailButton = new QPushButton( i18n("Change Email..."), this );
00194   gridLayout->addWidget( mChangeEmailButton, 2, 1 );
00195   connect( mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00196 
00197   mRemoveEntryButton = new QPushButton( i18n("Remove Entry"), this );
00198   gridLayout->addWidget( mRemoveEntryButton, 2, 2 );
00199   connect( mRemoveEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
00200 
00201   mManager = new DistributionListManager( mAddressBook );
00202   mManager->load();
00203 
00204   updateAddresseeView();
00205   updateNameCombo();
00206 }
00207 
00208 DistributionListEditorWidget::~DistributionListEditorWidget()
00209 {
00210   kdDebug(5700) << "~DistributionListEditor()" << endl;
00211 
00212   delete mManager;
00213 }
00214 
00215 void DistributionListEditorWidget::save()
00216 {
00217   mManager->save();
00218 }
00219 
00220 void DistributionListEditorWidget::slotSelectionEntryViewChanged()
00221 {
00222   EntryItem *entryItem = static_cast<EntryItem *>( mEntryView->selectedItem() );
00223   bool state=entryItem;
00224 
00225   mChangeEmailButton->setEnabled(state);
00226   mRemoveEntryButton->setEnabled(state);
00227 }
00228 
00229 void DistributionListEditorWidget::newList()
00230 {
00231   bool ok;
00232   QString name = KInputDialog::getText( i18n( "New Distribution List" ),
00233     i18n( "Please enter &name:" ), QString::null, &ok );
00234   if (!ok) return;
00235 
00236   new DistributionList( mManager, name );
00237 
00238   mNameCombo->clear();
00239   mNameCombo->insertStringList( mManager->listNames() );
00240   mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
00241 
00242   updateEntryView();
00243   slotSelectionAddresseeViewChanged();
00244 }
00245 
00246 void DistributionListEditorWidget::editList()
00247 {
00248   QString oldName = mNameCombo->currentText();
00249   bool ok;
00250   QString name = KInputDialog::getText( i18n( "Distribution List" ),
00251     i18n( "Please change &name:" ), oldName, &ok );
00252   if (!ok) return;
00253 
00254   DistributionList *list = mManager->list( oldName );
00255   list->setName( name );
00256 
00257   mNameCombo->clear();
00258   mNameCombo->insertStringList( mManager->listNames() );
00259   mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
00260 
00261   updateEntryView();
00262   slotSelectionAddresseeViewChanged();
00263 }
00264 
00265 void DistributionListEditorWidget::removeList()
00266 {
00267   int result = KMessageBox::warningContinueCancel( this,
00268       i18n("Delete distribution list '%1'?") .arg( mNameCombo->currentText() ),
00269       QString::null, KStdGuiItem::del() );
00270 
00271   if ( result != KMessageBox::Continue ) return;
00272 
00273   mManager->remove( mManager->list( mNameCombo->currentText() ) );
00274   mNameCombo->removeItem( mNameCombo->currentItem() );
00275 
00276   updateEntryView();
00277   slotSelectionAddresseeViewChanged();
00278 }
00279 
00280 void DistributionListEditorWidget::addEntry()
00281 {
00282   AddresseeItem *addresseeItem =
00283       static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00284 
00285   if( !addresseeItem ) {
00286     kdDebug(5700) << "DLE::addEntry(): No addressee selected." << endl;
00287     return;
00288   }
00289 
00290   DistributionList *list = mManager->list( mNameCombo->currentText() );
00291   if ( !list ) {
00292     kdDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl;
00293     return;
00294   }
00295 
00296   list->insertEntry( addresseeItem->addressee() );
00297   updateEntryView();
00298   slotSelectionAddresseeViewChanged();
00299 }
00300 
00301 void DistributionListEditorWidget::removeEntry()
00302 {
00303   DistributionList *list = mManager->list( mNameCombo->currentText() );
00304   if ( !list ) return;
00305 
00306   EntryItem *entryItem =
00307       static_cast<EntryItem *>( mEntryView->selectedItem() );
00308   if ( !entryItem ) return;
00309 
00310   list->removeEntry( entryItem->addressee(), entryItem->email() );
00311   delete entryItem;
00312 }
00313 
00314 void DistributionListEditorWidget::changeEmail()
00315 {
00316   DistributionList *list = mManager->list( mNameCombo->currentText() );
00317   if ( !list ) return;
00318 
00319   EntryItem *entryItem =
00320       static_cast<EntryItem *>( mEntryView->selectedItem() );
00321   if ( !entryItem ) return;
00322 
00323   QString email = EmailSelector::getEmail( entryItem->addressee().emails(),
00324                                            entryItem->email(), this );
00325   list->removeEntry( entryItem->addressee(), entryItem->email() );
00326   list->insertEntry( entryItem->addressee(), email );
00327 
00328   updateEntryView();
00329 }
00330 
00331 void DistributionListEditorWidget::updateEntryView()
00332 {
00333   if ( mNameCombo->currentText().isEmpty() ) {
00334     mListLabel->setText( i18n("Selected addressees:") );
00335   } else {
00336     mListLabel->setText( i18n("Selected addresses in '%1':")
00337                          .arg( mNameCombo->currentText() ) );
00338   }
00339 
00340   mEntryView->clear();
00341 
00342   DistributionList *list = mManager->list( mNameCombo->currentText() );
00343   if ( !list ) {
00344     mEditButton->setEnabled(false);
00345     mRemoveButton->setEnabled(false);
00346     mChangeEmailButton->setEnabled(false);
00347     mRemoveEntryButton->setEnabled(false);
00348     mAddresseeView->setEnabled(false);
00349     mEntryView->setEnabled(false);
00350     return;
00351   } else {
00352     mEditButton->setEnabled(true);
00353     mRemoveButton->setEnabled(true);
00354     mAddresseeView->setEnabled(true);
00355     mEntryView->setEnabled(true);
00356   }
00357 
00358   DistributionList::Entry::List entries = list->entries();
00359   DistributionList::Entry::List::ConstIterator it;
00360   for( it = entries.begin(); it != entries.end(); ++it ) {
00361     new EntryItem( mEntryView, (*it).addressee, (*it).email );
00362   }
00363 
00364   EntryItem *entryItem = static_cast<EntryItem *>( mEntryView->selectedItem() );
00365   bool state=entryItem;
00366 
00367   mChangeEmailButton->setEnabled(state);
00368   mRemoveEntryButton->setEnabled(state);
00369 }
00370 
00371 void DistributionListEditorWidget::updateAddresseeView()
00372 {
00373   mAddresseeView->clear();
00374 
00375   AddressBook::Iterator it;
00376   for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00377     new AddresseeItem( mAddresseeView, *it );
00378   }
00379 }
00380 
00381 void DistributionListEditorWidget::updateNameCombo()
00382 {
00383   mNameCombo->insertStringList( mManager->listNames() );
00384 
00385   updateEntryView();
00386 }
00387 
00388 void DistributionListEditorWidget::slotSelectionAddresseeViewChanged()
00389 {
00390   AddresseeItem *addresseeItem =
00391       static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00392   bool state=addresseeItem;
00393   mAddEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty());
00394 }
KDE Logo
This file is part of the documentation for kabc Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 23 17:13:00 2004 by doxygen 1.3.8-20040913 written by Dimitri van Heesch, © 1997-2003