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 <klineeditdlg.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   KLineEditDlg dlg(i18n("Please enter name:"), QString::null, this);
00232   dlg.setCaption(i18n("New Distribution List"));
00233   if (!dlg.exec()) return;
00234 
00235   new DistributionList( mManager, dlg.text() );
00236 
00237   mNameCombo->clear();
00238   mNameCombo->insertStringList( mManager->listNames() );
00239   mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
00240 
00241   updateEntryView();
00242   slotSelectionAddresseeViewChanged();
00243 }
00244 
00245 void DistributionListEditorWidget::editList()
00246 {
00247   QString oldName = mNameCombo->currentText();
00248   
00249   KLineEditDlg dlg(i18n("Please change name:"), oldName, this);
00250   dlg.setCaption(i18n("Distribution List"));
00251   if (!dlg.exec()) return;
00252 
00253   DistributionList *list = mManager->list( oldName );
00254   list->setName( dlg.text() );
00255 
00256   mNameCombo->clear();
00257   mNameCombo->insertStringList( mManager->listNames() );
00258   mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
00259 
00260   updateEntryView();
00261   slotSelectionAddresseeViewChanged();
00262 }
00263 
00264 void DistributionListEditorWidget::removeList()
00265 {
00266   int result = KMessageBox::warningContinueCancel( this,
00267       i18n("Delete distibution list '%1'?") .arg( mNameCombo->currentText() ),
00268       QString::null, i18n("Delete") );
00269 
00270   if ( result != KMessageBox::Continue ) return;
00271 
00272   delete mManager->list( mNameCombo->currentText() );
00273   mNameCombo->removeItem( mNameCombo->currentItem() );
00274 
00275   updateEntryView();
00276   slotSelectionAddresseeViewChanged();
00277 }
00278 
00279 void DistributionListEditorWidget::addEntry()
00280 {
00281   AddresseeItem *addresseeItem =
00282       static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00283 
00284   if( !addresseeItem ) {
00285     kdDebug(5700) << "DLE::addEntry(): No addressee selected." << endl;
00286     return;
00287   }
00288 
00289   DistributionList *list = mManager->list( mNameCombo->currentText() );
00290   if ( !list ) {
00291     kdDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl;
00292     return;
00293   }
00294 
00295   list->insertEntry( addresseeItem->addressee() );
00296   updateEntryView();
00297   slotSelectionAddresseeViewChanged();
00298 }
00299 
00300 void DistributionListEditorWidget::removeEntry()
00301 {
00302   DistributionList *list = mManager->list( mNameCombo->currentText() );
00303   if ( !list ) return;
00304 
00305   EntryItem *entryItem =
00306       static_cast<EntryItem *>( mEntryView->selectedItem() );
00307   if ( !entryItem ) return;
00308 
00309   list->removeEntry( entryItem->addressee(), entryItem->email() );
00310   delete entryItem;
00311 }
00312 
00313 void DistributionListEditorWidget::changeEmail()
00314 {
00315   DistributionList *list = mManager->list( mNameCombo->currentText() );
00316   if ( !list ) return;
00317 
00318   EntryItem *entryItem =
00319       static_cast<EntryItem *>( mEntryView->selectedItem() );
00320   if ( !entryItem ) return;
00321 
00322   QString email = EmailSelector::getEmail( entryItem->addressee().emails(),
00323                                            entryItem->email(), this );
00324   list->removeEntry( entryItem->addressee(), entryItem->email() );
00325   list->insertEntry( entryItem->addressee(), email );
00326 
00327   updateEntryView();
00328 }
00329 
00330 void DistributionListEditorWidget::updateEntryView()
00331 {
00332   if ( mNameCombo->currentText().isEmpty() ) {
00333     mListLabel->setText( i18n("Selected addressees:") );
00334   } else {
00335     mListLabel->setText( i18n("Selected addresses in '%1':")
00336                          .arg( mNameCombo->currentText() ) );
00337   }
00338 
00339   mEntryView->clear();
00340 
00341   DistributionList *list = mManager->list( mNameCombo->currentText() );
00342   if ( !list ) {
00343     mEditButton->setEnabled(false);
00344     mRemoveButton->setEnabled(false);
00345     mChangeEmailButton->setEnabled(false);
00346     mRemoveEntryButton->setEnabled(false);
00347     mAddresseeView->setEnabled(false);
00348     mEntryView->setEnabled(false);
00349     return;
00350   } else {
00351     mEditButton->setEnabled(true);
00352     mRemoveButton->setEnabled(true);
00353     mAddresseeView->setEnabled(true);
00354     mEntryView->setEnabled(true);
00355   }
00356 
00357   DistributionList::Entry::List entries = list->entries();
00358   DistributionList::Entry::List::ConstIterator it;
00359   for( it = entries.begin(); it != entries.end(); ++it ) {
00360     new EntryItem( mEntryView, (*it).addressee, (*it).email );
00361   }
00362 
00363   EntryItem *entryItem = static_cast<EntryItem *>( mEntryView->selectedItem() );
00364   bool state=entryItem;
00365 
00366   mChangeEmailButton->setEnabled(state);
00367   mRemoveEntryButton->setEnabled(state);
00368 }
00369 
00370 void DistributionListEditorWidget::updateAddresseeView()
00371 {
00372   mAddresseeView->clear();
00373 
00374   AddressBook::Iterator it;
00375   for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00376     new AddresseeItem( mAddresseeView, *it );
00377   }
00378 }
00379 
00380 void DistributionListEditorWidget::updateNameCombo()
00381 {
00382   mNameCombo->insertStringList( mManager->listNames() );
00383 
00384   updateEntryView();
00385 }
00386 
00387 void DistributionListEditorWidget::slotSelectionAddresseeViewChanged()
00388 {
00389   AddresseeItem *addresseeItem =
00390       static_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00391   bool state=addresseeItem;
00392   mAddEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty());
00393 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:16:07 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001