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