kabc Library API Documentation

distributionlisteditor.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 <qpushbutton.h>
00024 #include <qcombobox.h>
00025 #include <qinputdialog.h>
00026 #include <qbuttongroup.h>
00027 #include <qradiobutton.h>
00028 
00029 #include <klocale.h>
00030 #include <kdebug.h>
00031 
00032 #include "addressbook.h"
00033 #include "addresseedialog.h"
00034 #include "distributionlist.h"
00035 
00036 #include "distributionlisteditor.h"
00037 #include "distributionlisteditor.moc"
00038 
00039 using namespace KABC;
00040 
00041 EmailSelectDialog::EmailSelectDialog( const QStringList &emails, const QString &current,
00042                                       QWidget *parent ) :
00043   KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
00044                parent )
00045 {
00046   QFrame *topFrame = plainPage();
00047   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00048 
00049   mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
00050                                    topFrame );
00051   topLayout->addWidget( mButtonGroup );
00052 
00053   QStringList::ConstIterator it;
00054   for( it = emails.begin(); it != emails.end(); ++it ) {
00055     QRadioButton *button = new QRadioButton( *it, mButtonGroup );
00056     if ( (*it) == current ) {
00057       button->setDown( true );
00058     }
00059   }
00060 }
00061 
00062 QString EmailSelectDialog::selected()
00063 {
00064   QButton *button = mButtonGroup->selected();
00065   if ( button ) return button->text();
00066   return QString::null;
00067 }
00068 
00069 QString EmailSelectDialog::getEmail( const QStringList &emails, const QString &current,
00070                                      QWidget *parent )
00071 {
00072   EmailSelectDialog *dlg = new EmailSelectDialog( emails, current, parent );
00073   dlg->exec();
00074 
00075   QString result = dlg->selected();
00076 
00077   delete dlg;
00078 
00079   return result;
00080 }
00081 
00082 class EditEntryItem : public QListViewItem
00083 {
00084   public:
00085     EditEntryItem( QListView *parent, const Addressee &addressee,
00086                const QString &email=QString::null ) :
00087       QListViewItem( parent ),
00088       mAddressee( addressee ),
00089       mEmail( email )
00090     {
00091       setText( 0, addressee.realName() );
00092       if( email.isEmpty() ) {
00093         setText( 1, addressee.preferredEmail() );
00094         setText( 2, i18n("Yes") );
00095       } else {
00096         setText( 1, email );
00097         setText( 2, i18n("No") );
00098       }
00099     }
00100 
00101     Addressee addressee() const
00102     {
00103       return mAddressee;
00104     }
00105 
00106     QString email() const
00107     {
00108       return mEmail;
00109     }
00110 
00111   private:
00112     Addressee mAddressee;
00113     QString mEmail;
00114 };
00115 
00116 DistributionListEditor::DistributionListEditor( AddressBook *addressBook, QWidget *parent) :
00117   QWidget( parent ),
00118   mAddressBook( addressBook )
00119 {
00120   kdDebug(5700) << "DistributionListEditor()" << endl;
00121 
00122   QBoxLayout *topLayout = new QVBoxLayout( this );
00123   topLayout->setMargin( KDialog::marginHint() );
00124   topLayout->setSpacing( KDialog::spacingHint() );
00125 
00126   QBoxLayout *nameLayout = new QHBoxLayout( topLayout) ;
00127 
00128   mNameCombo = new QComboBox( this );
00129   nameLayout->addWidget( mNameCombo );
00130   connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) );
00131 
00132   newButton = new QPushButton( i18n("New List"), this );
00133   nameLayout->addWidget( newButton );
00134   connect( newButton, SIGNAL( clicked() ), SLOT( newList() ) );
00135 
00136   removeButton = new QPushButton( i18n("Remove List"), this );
00137   nameLayout->addWidget( removeButton );
00138   connect( removeButton, SIGNAL( clicked() ), SLOT( removeList() ) );
00139 
00140   mEntryView = new QListView( this );
00141   mEntryView->addColumn( i18n("Name") );
00142   mEntryView->addColumn( i18n("Email") );
00143   mEntryView->addColumn( i18n("Use preferred") );
00144   topLayout->addWidget( mEntryView );
00145   connect(mEntryView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionEntryViewChanged()));
00146 
00147   changeEmailButton = new QPushButton( i18n("Change Email"), this );
00148   topLayout->addWidget( changeEmailButton );
00149   connect( changeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00150 
00151   removeEntryButton = new QPushButton( i18n("Remove Entry"), this );
00152   topLayout->addWidget( removeEntryButton );
00153   connect( removeEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
00154 
00155   addEntryButton = new QPushButton( i18n("Add Entry"), this );
00156   topLayout->addWidget( addEntryButton );
00157   connect( addEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
00158 
00159   mAddresseeView = new QListView( this );
00160   mAddresseeView->addColumn( i18n("Name") );
00161   mAddresseeView->addColumn( i18n("Preferred Email") );
00162   topLayout->addWidget( mAddresseeView );
00163 
00164 
00165   connect(mAddresseeView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionAddresseeViewChanged()));
00166 
00167   mManager = new DistributionListManager( mAddressBook );
00168   mManager->load();
00169 
00170   updateAddresseeView();
00171   updateNameCombo();
00172   removeButton->setEnabled(!mManager->listNames().isEmpty());
00173 }
00174 
00175 DistributionListEditor::~DistributionListEditor()
00176 {
00177   kdDebug(5700) << "~DistributionListEditor()" << endl;
00178 
00179   mManager->save();
00180   delete mManager;
00181 }
00182 
00183 void DistributionListEditor::slotSelectionEntryViewChanged()
00184 {
00185     EditEntryItem *entryItem =dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00186     bool state=entryItem;
00187 
00188     changeEmailButton->setEnabled(state);
00189     removeEntryButton->setEnabled(state);
00190 }
00191 
00192 void DistributionListEditor::newList()
00193 {
00194   bool ok = false;
00195   QString name = QInputDialog::getText( i18n("New Distribution List"),
00196                                         i18n("Please enter name."),
00197                                         QLineEdit::Normal, QString::null, &ok,
00198                                         this );
00199   if ( !ok || name.isEmpty() ) return;
00200 
00201   new DistributionList( mManager, name );
00202 
00203   mNameCombo->insertItem( name );
00204   removeButton->setEnabled(true);
00205   updateEntryView();
00206 }
00207 
00208 void DistributionListEditor::removeList()
00209 {
00210   delete mManager->list( mNameCombo->currentText() );
00211   mNameCombo->removeItem( mNameCombo->currentItem() );
00212   removeButton->setEnabled(!mManager->listNames().isEmpty());
00213   addEntryButton->setEnabled( !mNameCombo->currentText().isEmpty());
00214   updateEntryView();
00215 }
00216 
00217 void DistributionListEditor::addEntry()
00218 {
00219   AddresseeItem *addresseeItem =
00220       dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00221 
00222   if( !addresseeItem ) {
00223     kdDebug(5700) << "DLE::addEntry(): No addressee selected." << endl;
00224     return;
00225   }
00226 
00227   DistributionList *list = mManager->list( mNameCombo->currentText() );
00228   if ( !list ) {
00229     kdDebug(5700) << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl;
00230     return;
00231   }
00232 
00233   list->insertEntry( addresseeItem->addressee() );
00234   updateEntryView();
00235   slotSelectionAddresseeViewChanged();
00236 }
00237 
00238 void DistributionListEditor::removeEntry()
00239 {
00240   DistributionList *list = mManager->list( mNameCombo->currentText() );
00241   if ( !list ) return;
00242 
00243   EditEntryItem *entryItem =
00244       dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00245   if ( !entryItem ) return;
00246 
00247   list->removeEntry( entryItem->addressee(), entryItem->email() );
00248   delete entryItem;
00249 }
00250 
00251 void DistributionListEditor::changeEmail()
00252 {
00253   DistributionList *list = mManager->list( mNameCombo->currentText() );
00254   if ( !list ) return;
00255 
00256   EditEntryItem *entryItem =
00257       dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00258   if ( !entryItem ) return;
00259 
00260   QString email = EmailSelectDialog::getEmail( entryItem->addressee().emails(),
00261                                                entryItem->email(), this );
00262   list->removeEntry( entryItem->addressee(), entryItem->email() );
00263   list->insertEntry( entryItem->addressee(), email );
00264 
00265   updateEntryView();
00266 }
00267 
00268 void DistributionListEditor::updateEntryView()
00269 {
00270   DistributionList *list = mManager->list( mNameCombo->currentText() );
00271   if ( !list ) return;
00272 
00273   mEntryView->clear();
00274   DistributionList::Entry::List entries = list->entries();
00275   DistributionList::Entry::List::ConstIterator it;
00276   for( it = entries.begin(); it != entries.end(); ++it ) {
00277     new EditEntryItem( mEntryView, (*it).addressee, (*it).email );
00278   }
00279    EditEntryItem *entryItem =dynamic_cast<EditEntryItem *>( mEntryView->selectedItem() );
00280    bool state=entryItem;
00281 
00282    changeEmailButton->setEnabled(state);
00283    removeEntryButton->setEnabled(state);
00284 }
00285 
00286 void DistributionListEditor::updateAddresseeView()
00287 {
00288   mAddresseeView->clear();
00289 
00290   AddressBook::Iterator it;
00291   for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
00292     new AddresseeItem( mAddresseeView, *it );
00293   }
00294 }
00295 
00296 void DistributionListEditor::updateNameCombo()
00297 {
00298   mNameCombo->insertStringList( mManager->listNames() );
00299 
00300   updateEntryView();
00301 }
00302 
00303 void DistributionListEditor::slotSelectionAddresseeViewChanged()
00304 {
00305     AddresseeItem *addresseeItem =
00306         dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
00307     bool state=addresseeItem;
00308     addEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty());
00309 }
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