kaddressbook

distributionlistngwidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2007 Klaralvdalens Datakonsult AB <frank@kdab.net>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "distributionlistngwidget.h"
00024 #include "interfaces/core.h"
00025 #include "searchmanager.h"
00026 
00027 #include <libkdepim/distributionlist.h>
00028 #include <libkdepim/kvcarddrag.h>
00029 
00030 #include <kabc/vcardconverter.h>
00031 
00032 #include <kdialog.h>
00033 #include <kiconloader.h>
00034 #include <klistview.h>
00035 #include <klocale.h>
00036 #include <kpopupmenu.h>
00037 
00038 #include <qevent.h>
00039 #include <qguardedptr.h>
00040 #include <qlabel.h>
00041 #include <qlayout.h>
00042 #include <qpoint.h>
00043 #include <qtimer.h>
00044 #include <qpushbutton.h>
00045 #include <qtooltip.h>
00046 
00047 KAB::DistributionListNg::ListBox::ListBox( QWidget* parent ) : KListBox( parent )
00048 {
00049     setAcceptDrops( true );
00050 }
00051 
00052 void KAB::DistributionListNg::ListBox::dragMoveEvent( QDragMoveEvent *event )
00053 {
00054     QListBoxItem *item = itemAt( event->pos() );
00055     if ( !item ) {
00056         event->ignore();
00057     }
00058     else {
00059         event->accept( itemRect( item ) );
00060     }
00061 }
00062 
00063 void KAB::DistributionListNg::ListBox::dragEnterEvent( QDragEnterEvent *event )
00064 {
00065     KListBox::dragEnterEvent( event );
00066 }
00067 
00068 void KAB::DistributionListNg::ListBox::dropEvent( QDropEvent *event )
00069 {
00070     QListBoxItem *item = itemAt( event->pos() );
00071     if ( !item || index( item ) == 0 )
00072         return;
00073 
00074     QString vcards;
00075     if ( !KVCardDrag::decode( event, vcards ) )
00076         return;
00077 
00078     KABC::VCardConverter converter;
00079     emit dropped( item->text(), converter.parseVCards( vcards ) );
00080 }
00081 
00082 namespace KAB {
00083 namespace DistributionListNg {
00084 
00085 class Factory : public KAB::ExtensionFactory
00086 {
00087   public:
00088     KAB::ExtensionWidget *extension( KAB::Core *core, QWidget *parent, const char *name )
00089     {
00090       return new KAB::DistributionListNg::MainWidget( core, parent, name );
00091     }
00092 
00093     QString identifier() const
00094     {
00095       return "distribution_list_editor";
00096     }
00097 };
00098 
00099 }
00100 }
00101 
00102 extern "C" {
00103   void *init_libkaddrbk_distributionlistng()
00104   {
00105     return ( new KAB::DistributionListNg::Factory );
00106   }
00107 }
00108 
00109 QString KAB::DistributionListNg::MainWidget::title() const
00110 {
00111     return i18n( "Distribution List Editor NG" );
00112 }
00113 
00114 QString KAB::DistributionListNg::MainWidget::identifier() const
00115 {
00116     return "distribution_list_editor_ng";
00117 }
00118 
00119 KAB::DistributionListNg::MainWidget::MainWidget( KAB::Core *core, QWidget *parent, const char *name ) : KAB::ExtensionWidget( core, parent, name )
00120 {
00121     QVBoxLayout *layout = new QVBoxLayout( this );
00122     layout->setSpacing( KDialog::spacingHint() );
00123 
00124     QHBoxLayout *buttonLayout = new QHBoxLayout();
00125     layout->addLayout( buttonLayout );
00126 
00127     QLabel *label = new QLabel( this );
00128     label->setText( i18n( "Distribution Lists" ) );
00129     buttonLayout->addWidget( label );
00130     buttonLayout->addStretch( 1 );
00131 
00132     mAddButton = new QPushButton( this );
00133     mAddButton->setIconSet( SmallIconSet( "add" ) );
00134     QToolTip::add( mAddButton, i18n( "Add distribution list" ) );
00135     connect( mAddButton, SIGNAL(clicked()), core, SLOT(newDistributionList()) );
00136     buttonLayout->addWidget( mAddButton );
00137 
00138     mEditButton = new QPushButton( this );
00139     mEditButton->setIconSet( SmallIconSet( "edit" ) );
00140     QToolTip::add( mEditButton, i18n( "Edit distribution list" ) );
00141     connect( mEditButton, SIGNAL(clicked()), this, SLOT(editSelectedDistributionList()) );
00142     buttonLayout->addWidget( mEditButton );
00143 
00144     mRemoveButton = new QPushButton( this );
00145     mRemoveButton->setIconSet( SmallIconSet( "remove" ) );
00146     QToolTip::add( mRemoveButton, i18n( "Remove distribution list" ) );
00147     connect( mRemoveButton, SIGNAL(clicked()), this, SLOT(deleteSelectedDistributionList()) );
00148     buttonLayout->addWidget( mRemoveButton );
00149 
00150     mListBox = new ListBox( this );
00151     connect( mListBox, SIGNAL( contextMenuRequested( QListBoxItem*, const QPoint& ) ),
00152              this, SLOT( contextMenuRequested( QListBoxItem*, const QPoint& ) ) );
00153     connect( mListBox, SIGNAL( dropped( const QString &, const KABC::Addressee::List & ) ),
00154              this, SLOT( contactsDropped( const QString &, const KABC::Addressee::List & ) ) );
00155     connect( mListBox, SIGNAL( highlighted( int ) ),
00156              this, SLOT( itemSelected( int ) ) );
00157     connect( mListBox, SIGNAL(doubleClicked(QListBoxItem*)), SLOT(editSelectedDistributionList()) );
00158     layout->addWidget( mListBox );
00159 
00160     connect( core, SIGNAL( contactsUpdated() ),
00161              this, SLOT( updateEntries() ) );
00162     connect( core->addressBook(), SIGNAL( addressBookChanged( AddressBook* ) ),
00163              this, SLOT( updateEntries() ) );
00164 
00165     // When contacts are changed, update both distr list combo and contents of displayed distr list
00166     connect( core, SIGNAL( contactsUpdated() ),
00167              this, SLOT( updateEntries() ) );
00168 
00169     QTimer::singleShot( 0, this, SLOT( updateEntries() ) );
00170 }
00171 
00172 void KAB::DistributionListNg::MainWidget::contextMenuRequested( QListBoxItem *item, const QPoint &point )
00173 {
00174     QGuardedPtr<KPopupMenu> menu = new KPopupMenu( this );
00175     menu->insertItem( i18n( "New Distribution List..." ), core(), SLOT( newDistributionList() ) );
00176     if ( item )
00177     {
00178         menu->insertItem( i18n( "Edit..." ), this, SLOT( editSelectedDistributionList() ) );
00179         menu->insertItem( i18n( "Delete" ), this, SLOT( deleteSelectedDistributionList() ) );
00180     }
00181     menu->exec( point );
00182     delete menu;
00183 }
00184 
00185 void KAB::DistributionListNg::MainWidget::editSelectedDistributionList()
00186 {
00187     const QListBoxItem* const item = mListBox->selectedItem();
00188     if ( !item )
00189         return;
00190     core()->editDistributionList( item->text() );
00191 }
00192 
00193 void KAB::DistributionListNg::MainWidget::deleteSelectedDistributionList()
00194 {
00195     const QListBoxItem* const item = mListBox->selectedItem();
00196     const QString name = item ? item->text() : QString();
00197     if ( name.isNull() )
00198         return;
00199     const KPIM::DistributionList list = KPIM::DistributionList::findByName(
00200     core()->addressBook(), name );
00201     if ( list.isEmpty() )
00202         return;
00203     core()->deleteDistributionLists( name );
00204 }
00205 
00206 void KAB::DistributionListNg::MainWidget::contactsDropped( const QString &listName, const KABC::Addressee::List &addressees )
00207 {
00208     if ( addressees.isEmpty() )
00209         return;
00210 
00211     KPIM::DistributionList list = KPIM::DistributionList::findByName(
00212     core()->addressBook(), listName );
00213     if ( list.isEmpty() ) // not found [should be impossible]
00214         return;
00215 
00216     for ( KABC::Addressee::List::ConstIterator it = addressees.begin(); it != addressees.end(); ++it ) {
00217         list.insertEntry( *it );
00218     }
00219 
00220     core()->addressBook()->insertAddressee( list );
00221     changed( list );
00222 }
00223 
00224 void KAB::DistributionListNg::MainWidget::changed( const KABC::Addressee& dist )
00225 {
00226     emit modified( KABC::Addressee::List() << dist );
00227 }
00228 
00229 void KAB::DistributionListNg::MainWidget::updateEntries()
00230 {
00231     const bool hadSelection = mListBox->selectedItem() != 0;
00232     const QStringList newEntries = core()->distributionListNames();
00233     if ( !mCurrentEntries.isEmpty() && newEntries == mCurrentEntries )
00234         return;
00235     mCurrentEntries = newEntries;
00236     mListBox->clear();
00237     mListBox->insertItem( i18n( "All Contacts" ), 0 );
00238     mListBox->insertStringList( mCurrentEntries );
00239     if ( !hadSelection )
00240         mListBox->setSelected( 0, true );
00241 }
00242 
00243 void KAB::DistributionListNg::MainWidget::itemSelected( int index )
00244 {
00245     core()->setSelectedDistributionList( index == 0 ? QString() : mListBox->item( index )->text()  );
00246     mEditButton->setEnabled( index > 0 );
00247     mRemoveButton->setEnabled( index > 0 );
00248 }
00249 
00250 #include "distributionlistngwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys