kaddressbook Library API Documentation

distributionlistwidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2002 Tobias Koenig <tokoe@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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018     As a special exception, permission is given to link this program
00019     with any edition of Qt, and distribute the resulting executable,
00020     without including the source code for Qt in the source distribution.
00021 */
00022 
00023 #include <qbuttongroup.h>
00024 #include <qcombobox.h>
00025 #include <qlabel.h>
00026 #include <qlayout.h>
00027 #include <qlistview.h>
00028 #include <qpushbutton.h>
00029 #include <qradiobutton.h>
00030 
00031 #include <kaccelmanager.h>
00032 #include <kdebug.h>
00033 #include <kinputdialog.h>
00034 #include <klocale.h>
00035 #include <kmessagebox.h>
00036 
00037 #include <kabc/addresseedialog.h>
00038 #include <kabc/distributionlist.h>
00039 #include <kabc/stdaddressbook.h>
00040 #include <kabc/vcardconverter.h>
00041 #include <libkdepim/kvcarddrag.h>
00042 
00043 #include "core.h"
00044 
00045 #include "distributionlistwidget.h"
00046 
00047 class DistributionListFactory : public KAB::ExtensionFactory
00048 {
00049   public:
00050     KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00051     {
00052       return new DistributionListWidget( core, parent, name );
00053     }
00054 
00055     QString identifier() const
00056     {
00057       return "distribution_list_editor";
00058     }
00059 };
00060 
00061 extern "C" {
00062   void *init_libkaddrbk_distributionlist()
00063   {
00064     return ( new DistributionListFactory );
00065   }
00066 }
00067 
00068 class ContactItem : public QListViewItem
00069 {
00070   public:
00071     ContactItem( DistributionListView *parent, const KABC::Addressee &addressee,
00072                const QString &email = QString::null ) :
00073       QListViewItem( parent ),
00074       mAddressee( addressee ),
00075       mEmail( email )
00076     {
00077       setText( 0, addressee.realName() );
00078       if( email.isEmpty() ) {
00079         setText( 1, addressee.preferredEmail() );
00080         setText( 2, i18n( "Yes" ) );
00081       } else {
00082         setText( 1, email );
00083         setText( 2, i18n( "No" ) );
00084       }
00085     }
00086 
00087     KABC::Addressee addressee() const
00088     {
00089       return mAddressee;
00090     }
00091 
00092     QString email() const
00093     {
00094       return mEmail;
00095     }
00096 
00097   protected:
00098     bool acceptDrop( const QMimeSource* )
00099     {
00100       return true;
00101     }
00102 
00103   private:
00104     KABC::Addressee mAddressee;
00105     QString mEmail;
00106 };
00107 
00108 DistributionListWidget::DistributionListWidget( KAB::Core *core, QWidget *parent,
00109                                                 const char *name )
00110   : KAB::ExtensionWidget( core, parent, name ), mManager( 0 )
00111 {
00112   QGridLayout *topLayout = new QGridLayout( this, 3, 4, KDialog::marginHint(),
00113                                             KDialog::spacingHint() );
00114 
00115   mNameCombo = new QComboBox( this );
00116   topLayout->addWidget( mNameCombo, 0, 0 );
00117   connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateContactView() ) );
00118 
00119   mCreateListButton = new QPushButton( i18n( "New List..." ), this );
00120   topLayout->addWidget( mCreateListButton, 0, 1 );
00121   connect( mCreateListButton, SIGNAL( clicked() ), SLOT( createList() ) );
00122 
00123   mEditListButton = new QPushButton( i18n( "Rename List..." ), this );
00124   topLayout->addWidget( mEditListButton, 0, 2 );
00125   connect( mEditListButton, SIGNAL( clicked() ), SLOT( editList() ) );
00126 
00127   mRemoveListButton = new QPushButton( i18n( "Remove List" ), this );
00128   topLayout->addWidget( mRemoveListButton, 0, 3 );
00129   connect( mRemoveListButton, SIGNAL( clicked() ), SLOT( removeList() ) );
00130 
00131   mContactView = new DistributionListView( this );
00132   mContactView->addColumn( i18n( "Name" ) );
00133   mContactView->addColumn( i18n( "Email" ) );
00134   mContactView->addColumn( i18n( "Use Preferred" ) );
00135   mContactView->setEnabled( false );
00136   mContactView->setAllColumnsShowFocus( true );
00137   mContactView->setFullWidth( true );
00138   topLayout->addMultiCellWidget( mContactView, 1, 1, 0, 3 );
00139   connect( mContactView, SIGNAL( selectionChanged() ),
00140            SLOT( selectionContactViewChanged() ) );
00141   connect( mContactView, SIGNAL( dropped( QDropEvent*, QListViewItem* ) ),
00142            SLOT( dropped( QDropEvent*, QListViewItem* ) ) );
00143 
00144   mAddContactButton = new QPushButton( i18n( "Add Contact" ), this );
00145   mAddContactButton->setEnabled( false );
00146   topLayout->addWidget( mAddContactButton, 2, 0 );
00147   connect( mAddContactButton, SIGNAL( clicked() ), SLOT( addContact() ) );
00148 
00149   mChangeEmailButton = new QPushButton( i18n( "Change Email..." ), this );
00150   topLayout->addWidget( mChangeEmailButton, 2, 2 );
00151   connect( mChangeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
00152 
00153   mRemoveContactButton = new QPushButton( i18n( "Remove Contact" ), this );
00154   topLayout->addWidget( mRemoveContactButton, 2, 3 );
00155   connect( mRemoveContactButton, SIGNAL( clicked() ), SLOT( removeContact() ) );
00156 
00157   mManager = new KABC::DistributionListManager( core->addressBook() );
00158 
00159   connect( KABC::DistributionListWatcher::self(), SIGNAL( changed() ),
00160            this, SLOT( updateNameCombo() ) );
00161   connect( core->addressBook(), SIGNAL( addressBookChanged( AddressBook* ) ),
00162            this, SLOT( updateNameCombo() ) );
00163 
00164   updateNameCombo();
00165 
00166   KAcceleratorManager::manage( this );
00167 }
00168 
00169 DistributionListWidget::~DistributionListWidget()
00170 {
00171   delete mManager;
00172 }
00173 
00174 void DistributionListWidget::save()
00175 {
00176   mManager->save();
00177 }
00178 
00179 void DistributionListWidget::selectionContactViewChanged()
00180 {
00181   ContactItem *contactItem =
00182                   static_cast<ContactItem *>( mContactView->selectedItem() );
00183   bool state = contactItem;
00184 
00185   mChangeEmailButton->setEnabled( state );
00186   mRemoveContactButton->setEnabled( state );
00187 }
00188 
00189 void DistributionListWidget::createList()
00190 {
00191   QString newName = KInputDialog::getText( i18n( "New Distribution List" ),
00192                                            i18n( "Please enter name:" ),
00193                                            QString::null, 0, this );
00194 
00195   if ( newName.isEmpty() ) return;
00196 
00197   if ( mManager->listNames().contains( newName ) ) {
00198     KMessageBox::sorry( this, i18n( "The name already exists" ) );
00199     return;
00200   }
00201   new KABC::DistributionList( mManager, newName );
00202 
00203   mNameCombo->clear();
00204   mNameCombo->insertStringList( mManager->listNames() );
00205   mNameCombo->setCurrentItem( mNameCombo->count() - 1 );
00206 
00207   updateContactView();
00208 
00209   changed();
00210 }
00211 
00212 void DistributionListWidget::editList()
00213 {
00214   QString oldName = mNameCombo->currentText();
00215 
00216   QString newName = KInputDialog::getText( i18n( "New Distribution List" ),
00217                                            i18n( "Please enter name:" ),
00218                                            oldName, 0, this );
00219 
00220   if ( newName.isEmpty() ) return;
00221 
00222   if ( mManager->listNames().contains( newName ) ) {
00223     KMessageBox::sorry( this, i18n( "The name already exists" ) );
00224     return;
00225   }
00226   KABC::DistributionList *list = mManager->list( oldName );
00227   list->setName( newName );
00228 
00229   int pos = mNameCombo->currentItem();
00230   mNameCombo->clear();
00231   mNameCombo->insertStringList( mManager->listNames() );
00232   mNameCombo->setCurrentItem( pos );
00233 
00234   updateContactView();
00235 
00236   changed();
00237 }
00238 
00239 void DistributionListWidget::removeList()
00240 {
00241   int result = KMessageBox::warningContinueCancel( this,
00242       i18n( "<qt>Delete distribution list <b>%1</b>?</qt>" ) .arg( mNameCombo->currentText() ),
00243       QString::null, KGuiItem( i18n("Delete"), "editdelete") );
00244 
00245   if ( result != KMessageBox::Continue )
00246     return;
00247 
00248   mManager->remove( mManager->list( mNameCombo->currentText() ) );
00249   mNameCombo->removeItem( mNameCombo->currentItem() );
00250 
00251   updateContactView();
00252 
00253   changed();
00254 }
00255 
00256 void DistributionListWidget::addContact()
00257 {
00258   KABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00259   if ( !list )
00260     return;
00261 
00262   KABC::Addressee::List addrList = selectedContacts();
00263   KABC::Addressee::List::Iterator it;
00264   for ( it = addrList.begin(); it != addrList.end(); ++it )
00265     list->insertEntry( *it );
00266 
00267   updateContactView();
00268 
00269   changed();
00270 }
00271 
00272 void DistributionListWidget::removeContact()
00273 {
00274   KABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00275   if ( !list )
00276     return;
00277 
00278   ContactItem *contactItem =
00279                     static_cast<ContactItem *>( mContactView->selectedItem() );
00280   if ( !contactItem )
00281     return;
00282 
00283   list->removeEntry( contactItem->addressee(), contactItem->email() );
00284   delete contactItem;
00285 
00286   changed();
00287 }
00288 
00289 void DistributionListWidget::changeEmail()
00290 {
00291   KABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00292   if ( !list )
00293     return;
00294 
00295   ContactItem *contactItem =
00296                     static_cast<ContactItem *>( mContactView->selectedItem() );
00297   if ( !contactItem )
00298     return;
00299 
00300   QString email = EmailSelector::getEmail( contactItem->addressee().emails(),
00301                                            contactItem->email(), this );
00302   list->removeEntry( contactItem->addressee(), contactItem->email() );
00303   list->insertEntry( contactItem->addressee(), email );
00304 
00305   updateContactView();
00306 
00307   changed();
00308 }
00309 
00310 void DistributionListWidget::updateContactView()
00311 {
00312   mContactView->clear();
00313 
00314   KABC::DistributionList *list = mManager->list( mNameCombo->currentText() );
00315   if ( !list ) {
00316     mEditListButton->setEnabled( false );
00317     mRemoveListButton->setEnabled( false );
00318     mChangeEmailButton->setEnabled( false );
00319     mRemoveContactButton->setEnabled( false );
00320     mContactView->setEnabled( false );
00321     return;
00322   } else {
00323     mEditListButton->setEnabled( true );
00324     mRemoveListButton->setEnabled( true );
00325     mContactView->setEnabled( true );
00326   }
00327 
00328   KABC::DistributionList::Entry::List entries = list->entries();
00329   KABC::DistributionList::Entry::List::ConstIterator it;
00330   for( it = entries.begin(); it != entries.end(); ++it )
00331     new ContactItem( mContactView, (*it).addressee, (*it).email );
00332 
00333   ContactItem *contactItem =
00334                     static_cast<ContactItem *>( mContactView->selectedItem() );
00335   bool state = contactItem;
00336 
00337   mChangeEmailButton->setEnabled( state );
00338   mRemoveContactButton->setEnabled( state );
00339 }
00340 
00341 void DistributionListWidget::updateNameCombo()
00342 {
00343   mManager->load();
00344 
00345   int pos = mNameCombo->currentItem();
00346   mNameCombo->clear();
00347   mNameCombo->insertStringList( mManager->listNames() );
00348   mNameCombo->setCurrentItem( pos );
00349 
00350   updateContactView();
00351 }
00352 
00353 void DistributionListWidget::dropEvent( QDropEvent *e )
00354 {
00355   KABC::DistributionList *distributionList = mManager->list( mNameCombo->currentText() );
00356   if ( !distributionList )
00357     return;
00358 
00359   QString vcards;
00360   if ( KVCardDrag::decode( e, vcards ) ) {
00361     KABC::VCardConverter converter;
00362     KABC::Addressee::List list = converter.parseVCards( vcards );
00363     KABC::Addressee::List::Iterator it;
00364     for ( it = list.begin(); it != list.end(); ++it )
00365       distributionList->insertEntry( *it );
00366 
00367     changed();
00368     updateContactView();
00369   }
00370 }
00371 
00372 void DistributionListWidget::contactsSelectionChanged()
00373 {
00374   mAddContactButton->setEnabled( contactsSelected() && mNameCombo->count() > 0 );
00375 }
00376 
00377 QString DistributionListWidget::title() const
00378 {
00379   return i18n( "Distribution List Editor" );
00380 }
00381 
00382 QString DistributionListWidget::identifier() const
00383 {
00384   return "distribution_list_editor";
00385 }
00386 
00387 void DistributionListWidget::dropped( QDropEvent *e, QListViewItem* )
00388 {
00389   dropEvent( e );
00390 }
00391 
00392 void DistributionListWidget::changed()
00393 {
00394   save();
00395 }
00396 
00397 
00398 DistributionListView::DistributionListView( QWidget *parent, const char* name )
00399   : KListView( parent, name )
00400 {
00401   setDragEnabled( true );
00402   setAcceptDrops( true );
00403   setAllColumnsShowFocus( true );
00404 }
00405 
00406 void DistributionListView::dragEnterEvent( QDragEnterEvent* e )
00407 {
00408   bool canDecode = QTextDrag::canDecode( e );
00409   e->accept( canDecode );
00410 }
00411 
00412 void DistributionListView::viewportDragMoveEvent( QDragMoveEvent *e )
00413 {
00414   bool canDecode = QTextDrag::canDecode( e );
00415   e->accept( canDecode );
00416 }
00417 
00418 void DistributionListView::viewportDropEvent( QDropEvent *e )
00419 {
00420   emit dropped( e, 0 );
00421 }
00422 
00423 void DistributionListView::dropEvent( QDropEvent *e )
00424 {
00425   emit dropped( e, 0 );
00426 }
00427 
00428 
00429 EmailSelector::EmailSelector( const QStringList &emails,
00430                               const QString &current, QWidget *parent )
00431   : KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
00432                parent )
00433 {
00434   QFrame *topFrame = plainPage();
00435   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00436 
00437   mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
00438                                    topFrame );
00439   topLayout->addWidget( mButtonGroup );
00440 
00441   QRadioButton *button = new QRadioButton( i18n("Preferred address"), mButtonGroup );
00442   button->setDown( true );
00443   mEmailMap.insert( mButtonGroup->id( button ), "" );
00444 
00445   QStringList::ConstIterator it;
00446   for ( it = emails.begin(); it != emails.end(); ++it ) {
00447     button = new QRadioButton( *it, mButtonGroup );
00448     mEmailMap.insert( mButtonGroup->id( button ), *it );
00449     if ( (*it) == current )
00450       button->setDown( true );
00451   }
00452 }
00453 
00454 QString EmailSelector::selected() const
00455 {
00456   QButton *button = mButtonGroup->selected();
00457   if ( button )
00458     return mEmailMap[ mButtonGroup->id( button ) ];
00459 
00460   return QString::null;
00461 }
00462 
00463 QString EmailSelector::getEmail( const QStringList &emails,
00464                                  const QString &current, QWidget *parent )
00465 {
00466   EmailSelector dlg( emails, current, parent );
00467   dlg.exec();
00468 
00469   return dlg.selected();
00470 }
00471 
00472 
00473 #include "distributionlistwidget.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:42:46 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003