kaddressbook Library API Documentation

featuredistributionlist.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2002 Mirko Boehm <mirko@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program 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
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qlayout.h>
00025 #include <qcombobox.h>
00026 #include <qpushbutton.h>
00027 #include <qlistview.h>
00028 #include <qdragobject.h>
00029 #include <qtooltip.h>
00030 
00031 #include <kdialog.h>
00032 #include <klocale.h>
00033 #include <kabc/distributionlist.h>
00034 #include <kabc/distributionlisteditor.h>
00035 #include <kabc/vcardconverter.h>
00036 #include <klineeditdlg.h>
00037 #include <kmessagebox.h>
00038 #include <kdebug.h>
00039 #include <kiconloader.h>
00040 #include <libkdepim/kvcarddrag.h>
00041 
00042 #include "addresseeutil.h"
00043 #include "featuredistributionlist.h"
00044 #include "featuredistributionlistview.h"
00045 
00046 namespace KABC
00047 {
00048 
00049 // MOSTLY A COPY FROM kdelibs/kabc:
00050 class EntryItem : public QListViewItem
00051 {
00052   protected:
00053     FeatureDistributionList *list;
00054 
00055   public:
00056     EntryItem( FeatureDistributionList *l, QListView *parent,
00057                const Addressee &addressee, const QString &email = QString::null )
00058       : QListViewItem( parent ), list( l ), mAddressee( addressee ), mEmail( email )
00059     {
00060       setDropEnabled( true );
00061       setText( 0, addressee.realName() );
00062       if ( email.isEmpty() ) {
00063         setText( 1, addressee.preferredEmail() );
00064         setText( 2, i18n( "Yes" ) );
00065       } else {
00066         setText( 1, email );
00067         setText( 2, i18n( "No" ) );
00068       }
00069     }
00070 
00071     Addressee addressee() const
00072     {
00073       return mAddressee;
00074     }
00075 
00076     QString email() const
00077     {
00078       return mEmail;
00079     }
00080 
00081   protected:
00082     bool acceptDrop( const QMimeSource* )
00083     {
00084       // WORK_TO_DO: check data type
00085       return true;
00086     }
00087 
00088     void dropped( QDropEvent *e )
00089     {
00090       list->slotDropped( e );
00091     }
00092 
00093   private:
00094     Addressee mAddressee;
00095     QString mEmail;
00096 };
00097 
00098 }
00099 
00100 FeatureDistributionList::FeatureDistributionList( KABC::AddressBook *doc,
00101                                             QWidget *parent, const char* name )
00102   : QWidget( parent, name ),
00103     mDoc( doc ),
00104     mManager( new KABC::DistributionListManager( doc ) )
00105 {
00106   initGUI();
00107 
00108   connect( mLvAddressees, SIGNAL(selectionChanged()),
00109            SLOT(slotAddresseeSelectionChanged()));
00110   connect( mLvAddressees, SIGNAL(dropped(QDropEvent*)),
00111            SLOT(slotDropped(QDropEvent*)));
00112 
00113   mLvAddressees->addColumn( i18n( "Name" ) );
00114   mLvAddressees->addColumn( i18n( "Email" ) );
00115   mLvAddressees->addColumn( i18n( "Use Preferred" ) );
00116 
00117   mManager->load();
00118 }
00119 
00120 FeatureDistributionList::~FeatureDistributionList()
00121 {
00122   delete mManager;
00123 }
00124 
00125 void FeatureDistributionList::update()
00126 {
00127   int index = mCbListSelect->currentItem();
00128 
00129   mLvAddressees->clear();
00130   mCbListSelect->clear();
00131   mCbListSelect->insertStringList(mManager->listNames());
00132 
00133   if ( index < mCbListSelect->count() ) {
00134     mCbListSelect->setCurrentItem( index );
00135   }
00136 
00137   updateGUI();
00138 }
00139 
00140 void FeatureDistributionList::updateGUI()
00141 {
00142   KABC::DistributionList *list = mManager->list( mCbListSelect->currentText() );
00143   if( !list ) {
00144     mPbListRename->setEnabled( false );
00145     mPbListRemove->setEnabled( false );
00146     mPbChangeEmail->setEnabled( false );
00147     mPbEntryRemove->setEnabled( false );
00148     mLvAddressees->setEnabled( false );
00149     mLvAddressees->clear();
00150     mCbListSelect->setEnabled( false );
00151     return;
00152   } else {
00153     mPbListRename->setEnabled( true );
00154     mPbListRemove->setEnabled( true );
00155     mLvAddressees->setEnabled( true );
00156     mLvAddressees->clear();
00157     KABC::DistributionList::Entry::List entries = list->entries();
00158     KABC::DistributionList::Entry::List::ConstIterator it;
00159     for( it = entries.begin(); it != entries.end(); ++it ) {
00160       new KABC::EntryItem( this, mLvAddressees, (*it).addressee, (*it).email );
00161     }
00162     mCbListSelect->setEnabled( true );
00163   }
00164 
00165   KABC::EntryItem *entryItem = static_cast<KABC::EntryItem *>( mLvAddressees->selectedItem() );
00166 
00167   bool state = entryItem;
00168   mPbChangeEmail->setEnabled( state );
00169   mPbEntryRemove->setEnabled( state );
00170 }
00171 
00172 void FeatureDistributionList::showEvent( QShowEvent* )
00173 {
00174   update();
00175 }
00176 
00177 void FeatureDistributionList::slotListNew()
00178 {
00179   KLineEditDlg dlg( i18n( "Please enter name:" ), QString::null, this );
00180   dlg.setCaption( i18n("New Distribution List") );
00181 
00182   if ( !dlg.exec() )
00183     return;
00184 
00185   new KABC::DistributionList( mManager, dlg.text() );
00186 
00187   mCbListSelect->clear();
00188   mCbListSelect->insertStringList( mManager->listNames() );
00189   mCbListSelect->setCurrentItem( mCbListSelect->count() - 1 );
00190 
00191   commit();
00192   update();
00193 }
00194 
00195 void FeatureDistributionList::slotListRename()
00196 {
00197   QString oldName = mCbListSelect->currentText();
00198 
00199   KLineEditDlg dlg( i18n( "Please change name:" ), oldName, this );
00200   dlg.setCaption( i18n( "Distribution List" ) );
00201 
00202   if ( !dlg.exec() )
00203     return;
00204 
00205   KABC::DistributionList *list = mManager->list( oldName );
00206   list->setName( dlg.text() );
00207 
00208   mCbListSelect->clear();
00209   mCbListSelect->insertStringList( mManager->listNames() );
00210   mCbListSelect->setCurrentItem( mCbListSelect->count() - 1 );
00211 
00212   commit();
00213   update();
00214 }
00215 
00216 void FeatureDistributionList::slotListRemove()
00217 {
00218   int result = KMessageBox::warningContinueCancel( this,
00219                   i18n( "Delete distibution list '%1'?" ).arg( mCbListSelect->currentText() ),
00220                   QString::null, i18n( "Delete" ) );
00221 
00222   if ( result != KMessageBox::Continue)
00223     return;
00224 
00225   delete mManager->list( mCbListSelect->currentText() );
00226   mCbListSelect->removeItem( mCbListSelect->currentItem() );
00227 
00228   commit();
00229   updateGUI();
00230 }
00231 
00232 void FeatureDistributionList::slotEntryChangeEmail()
00233 {
00234   KABC::DistributionList *list = mManager->list( mCbListSelect->currentText() );
00235   if ( !list )
00236     return;
00237 
00238   KABC::EntryItem *entryItem = static_cast<KABC::EntryItem *>( mLvAddressees->selectedItem() );
00239   if ( !entryItem )
00240     return;
00241 
00242   QString email = KABC::EmailSelectDialog::getEmail( entryItem->addressee().emails(),
00243                                                      entryItem->email(), this );
00244   list->removeEntry( entryItem->addressee(), entryItem->email() );
00245   list->insertEntry( entryItem->addressee(), email );
00246 
00247   commit();
00248   update();
00249 }
00250 
00251 void FeatureDistributionList::slotEntryRemove()
00252 {
00253   KABC::DistributionList *list = mManager->list( mCbListSelect->currentText() );
00254   if ( !list )
00255     return;
00256 
00257   KABC::EntryItem *entryItem = static_cast<KABC::EntryItem *>( mLvAddressees->selectedItem() );
00258   if ( !entryItem )
00259     return;
00260 
00261   list->removeEntry( entryItem->addressee(), entryItem->email() );
00262   delete entryItem;
00263 
00264   commit();
00265 }
00266 
00267 void FeatureDistributionList::slotListSelected( int )
00268 {
00269   update();
00270 }
00271 
00272 void FeatureDistributionList::slotAddresseeSelectionChanged()
00273 {
00274   KABC::EntryItem *entryItem = static_cast<KABC::EntryItem *>( mLvAddressees->selectedItem() );
00275   bool state = entryItem;
00276 
00277   mPbChangeEmail->setEnabled( state );
00278   mPbEntryRemove->setEnabled( state );
00279 }
00280 
00281 void FeatureDistributionList::commit()
00282 {
00283   mManager->save();
00284   emit modified();
00285 }
00286 
00287 void FeatureDistributionList::dropEvent( QDropEvent *e )
00288 {
00289   KABC::DistributionList *distributionList = mManager->list( mCbListSelect->currentText() );
00290   if ( !distributionList ) {
00291     kdDebug(5700) << "FeatureDistributionList::dropEvent: No dist list '"
00292                   << mCbListSelect->currentText() << "'" << endl;
00293     return;
00294   }
00295 
00296   QString vcards;
00297   if ( KVCardDrag::decode( e, vcards ) ) {
00298     QStringList list = QStringList::split( "\r\n\r\n", vcards );
00299     QStringList::Iterator it;
00300     KABC::VCardConverter converter;
00301     for ( it = list.begin(); it != list.end(); ++it ) {
00302       KABC::Addressee addr;
00303       if ( converter.vCardToAddressee( (*it).stripWhiteSpace(), addr ) )
00304         distributionList->insertEntry( addr );
00305     }
00306 
00307     commit();
00308     update();
00309   }
00310 }
00311 
00312 void FeatureDistributionList::slotDropped( QDropEvent *e )
00313 {
00314   dropEvent( e );
00315 }
00316 
00317 void FeatureDistributionList::initGUI()
00318 {
00319   QGridLayout *layout = new QGridLayout( this, 1, 1, KDialog::marginHint(), KDialog::spacingHint() );
00320   QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
00321   layout->addMultiCell( spacer, 3, 4, 2, 2 );
00322 
00323   mCbListSelect = new QComboBox( false, this );
00324   layout->addWidget( mCbListSelect, 0, 0 );
00325 
00326   mPbListRename = new QPushButton( i18n( "Rename List..." ), this );
00327   layout->addWidget( mPbListRename, 2, 0 );
00328 
00329   mPbListRemove = new QPushButton( i18n( "Remove List" ), this );
00330   layout->addWidget( mPbListRemove, 3, 0 );
00331 
00332   QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
00333   layout->addItem( spacer_2, 4, 0 );
00334 
00335   mPbChangeEmail = new QPushButton( i18n( "Change Email..." ), this );
00336   layout->addWidget( mPbChangeEmail, 0, 2 );
00337 
00338   mPbEntryRemove = new QPushButton( i18n( "Remove Entry" ), this );
00339   layout->addWidget( mPbEntryRemove, 1, 2 );
00340 
00341   mPbListNew = new QPushButton( i18n( "New List..." ), this );
00342   layout->addWidget( mPbListNew, 1, 0 );
00343 
00344   mLvAddressees = new FeatureDistributionListView( this );
00345   layout->addMultiCellWidget( mLvAddressees, 0, 4, 1, 1 );
00346   QToolTip::add(mLvAddressees, i18n("Drag addressees here to add them to the distribution list."));
00347 
00348   // signals and slots connections
00349   connect( mPbListNew, SIGNAL( clicked() ), this, SLOT( slotListNew() ) );
00350   connect( mPbListRename, SIGNAL( clicked() ), this, SLOT( slotListRename() ) );
00351   connect( mPbListRemove, SIGNAL( clicked() ), this, SLOT( slotListRemove() ) );
00352   connect( mPbChangeEmail, SIGNAL( clicked() ), this, SLOT( slotEntryChangeEmail() ) );
00353   connect( mPbEntryRemove, SIGNAL( clicked() ), this, SLOT( slotEntryRemove() ) );
00354   connect( mCbListSelect, SIGNAL( activated(int) ), this, SLOT( slotListSelected(int) ) );
00355 }
00356 
00357 #include "featuredistributionlist.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:09 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001