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     layout->addWidget( mListBox );
00158 
00159     connect( core, SIGNAL( contactsUpdated() ),
00160              this, SLOT( updateEntries() ) );
00161     connect( core->addressBook(), SIGNAL( addressBookChanged( AddressBook* ) ),
00162              this, SLOT( updateEntries() ) );
00163 
00164     // When contacts are changed, update both distr list combo and contents of displayed distr list
00165     connect( core, SIGNAL( contactsUpdated() ),
00166              this, SLOT( updateEntries() ) );
00167 
00168     QTimer::singleShot( 0, this, SLOT( updateEntries() ) );
00169 }
00170 
00171 void KAB::DistributionListNg::MainWidget::contextMenuRequested( QListBoxItem *item, const QPoint &point )
00172 {
00173     QGuardedPtr<KPopupMenu> menu = new KPopupMenu( this );
00174     menu->insertItem( i18n( "New Distribution List..." ), core(), SLOT( newDistributionList() ) );
00175     if ( item )
00176     {
00177         menu->insertItem( i18n( "Edit..." ), this, SLOT( editSelectedDistributionList() ) );
00178         menu->insertItem( i18n( "Delete" ), this, SLOT( deleteSelectedDistributionList() ) );
00179     }
00180     menu->exec( point );
00181     delete menu;
00182 }
00183 
00184 void KAB::DistributionListNg::MainWidget::editSelectedDistributionList()
00185 {
00186     const QListBoxItem* const item = mListBox->selectedItem();
00187     if ( !item )
00188         return;
00189     core()->editDistributionList( item->text() );
00190 }
00191 
00192 void KAB::DistributionListNg::MainWidget::deleteSelectedDistributionList()
00193 {
00194     const QListBoxItem* const item = mListBox->selectedItem();
00195     const QString name = item ? item->text() : QString();
00196     if ( name.isNull() )
00197         return;
00198     const KPIM::DistributionList list = KPIM::DistributionList::findByName(
00199     core()->addressBook(), name );
00200     if ( list.isEmpty() )
00201         return;
00202     core()->deleteDistributionLists( name );
00203 }
00204 
00205 void KAB::DistributionListNg::MainWidget::contactsDropped( const QString &listName, const KABC::Addressee::List &addressees )
00206 {
00207     if ( addressees.isEmpty() )
00208         return;
00209 
00210     KPIM::DistributionList list = KPIM::DistributionList::findByName(
00211     core()->addressBook(), listName );
00212     if ( list.isEmpty() ) // not found [should be impossible]
00213         return;
00214 
00215     for ( KABC::Addressee::List::ConstIterator it = addressees.begin(); it != addressees.end(); ++it ) {
00216         list.insertEntry( *it );
00217     }
00218 
00219     core()->addressBook()->insertAddressee( list );
00220     changed( list );
00221 }
00222 
00223 void KAB::DistributionListNg::MainWidget::changed( const KABC::Addressee& dist )
00224 {
00225     emit modified( KABC::Addressee::List() << dist );
00226 }
00227 
00228 void KAB::DistributionListNg::MainWidget::updateEntries()
00229 {
00230     const bool hadSelection = mListBox->selectedItem() != 0;
00231     const QStringList newEntries = core()->distributionListNames();
00232     if ( !mCurrentEntries.isEmpty() && newEntries == mCurrentEntries )
00233         return;
00234     mCurrentEntries = newEntries;
00235     mListBox->clear();
00236     mListBox->insertItem( i18n( "All Contacts" ), 0 );
00237     mListBox->insertStringList( mCurrentEntries );
00238     if ( !hadSelection )
00239         mListBox->setSelected( 0, true );
00240 }
00241 
00242 void KAB::DistributionListNg::MainWidget::itemSelected( int index )
00243 {
00244     core()->setSelectedDistributionList( index == 0 ? QString() : mListBox->item( index )->text()  );
00245     mEditButton->setEnabled( index > 0 );
00246     mRemoveButton->setEnabled( index > 0 );
00247 }
00248 
00249 #include "distributionlistngwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys