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 TQt, and distribute the resulting executable, 00020 without including the source code for TQt 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 <tqevent.h> 00039 #include <tqguardedptr.h> 00040 #include <tqlabel.h> 00041 #include <tqlayout.h> 00042 #include <tqpoint.h> 00043 #include <tqtimer.h> 00044 #include <tqpushbutton.h> 00045 #include <tqtooltip.h> 00046 00047 KAB::DistributionListNg::ListBox::ListBox( TQWidget* parent ) : KListBox( parent ) 00048 { 00049 setAcceptDrops( true ); 00050 } 00051 00052 void KAB::DistributionListNg::ListBox::dragMoveEvent( TQDragMoveEvent *event ) 00053 { 00054 TQListBoxItem *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( TQDragEnterEvent *event ) 00064 { 00065 KListBox::dragEnterEvent( event ); 00066 } 00067 00068 void KAB::DistributionListNg::ListBox::dropEvent( TQDropEvent *event ) 00069 { 00070 TQListBoxItem *item = itemAt( event->pos() ); 00071 if ( !item || index( item ) == 0 ) 00072 return; 00073 00074 KABC::Addressee::List list; 00075 if ( !KVCardDrag::decode( event, list ) ) 00076 return; 00077 00078 emit dropped( item->text(), list ); 00079 } 00080 00081 namespace KAB { 00082 namespace DistributionListNg { 00083 00084 class Factory : public KAB::ExtensionFactory 00085 { 00086 public: 00087 KAB::ExtensionWidget *extension( KAB::Core *core, TQWidget *parent, const char *name ) 00088 { 00089 return new KAB::DistributionListNg::MainWidget( core, parent, name ); 00090 } 00091 00092 TQString identifier() const 00093 { 00094 return "distribution_list_editor"; 00095 } 00096 }; 00097 00098 } 00099 } 00100 00101 extern "C" { 00102 void *init_libkaddrbk_distributionlistng() 00103 { 00104 return ( new KAB::DistributionListNg::Factory ); 00105 } 00106 } 00107 00108 TQString KAB::DistributionListNg::MainWidget::title() const 00109 { 00110 return i18n( "Distribution List Editor NG" ); 00111 } 00112 00113 TQString KAB::DistributionListNg::MainWidget::identifier() const 00114 { 00115 return "distribution_list_editor_ng"; 00116 } 00117 00118 KAB::DistributionListNg::MainWidget::MainWidget( KAB::Core *core, TQWidget *parent, const char *name ) : KAB::ExtensionWidget( core, parent, name ) 00119 { 00120 TQVBoxLayout *layout = new TQVBoxLayout( this ); 00121 layout->setSpacing( KDialog::spacingHint() ); 00122 00123 TQHBoxLayout *buttonLayout = new TQHBoxLayout(); 00124 layout->addLayout( buttonLayout ); 00125 00126 TQLabel *label = new TQLabel( this ); 00127 label->setText( i18n( "Distribution Lists" ) ); 00128 buttonLayout->addWidget( label ); 00129 buttonLayout->addStretch( 1 ); 00130 00131 mAddButton = new TQPushButton( this ); 00132 mAddButton->setIconSet( SmallIconSet( "add" ) ); 00133 TQToolTip::add( mAddButton, i18n( "Add distribution list" ) ); 00134 connect( mAddButton, TQT_SIGNAL(clicked()), core, TQT_SLOT(newDistributionList()) ); 00135 buttonLayout->addWidget( mAddButton ); 00136 00137 mEditButton = new TQPushButton( this ); 00138 mEditButton->setIconSet( SmallIconSet( "edit" ) ); 00139 TQToolTip::add( mEditButton, i18n( "Edit distribution list" ) ); 00140 connect( mEditButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(editSelectedDistributionList()) ); 00141 buttonLayout->addWidget( mEditButton ); 00142 00143 mRemoveButton = new TQPushButton( this ); 00144 mRemoveButton->setIconSet( SmallIconSet( "remove" ) ); 00145 TQToolTip::add( mRemoveButton, i18n( "Remove distribution list" ) ); 00146 connect( mRemoveButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteSelectedDistributionList()) ); 00147 buttonLayout->addWidget( mRemoveButton ); 00148 00149 mListBox = new ListBox( this ); 00150 connect( mListBox, TQT_SIGNAL( contextMenuRequested( TQListBoxItem*, const TQPoint& ) ), 00151 this, TQT_SLOT( contextMenuRequested( TQListBoxItem*, const TQPoint& ) ) ); 00152 connect( mListBox, TQT_SIGNAL( dropped( const TQString &, const KABC::Addressee::List & ) ), 00153 this, TQT_SLOT( contactsDropped( const TQString &, const KABC::Addressee::List & ) ) ); 00154 connect( mListBox, TQT_SIGNAL( highlighted( int ) ), 00155 this, TQT_SLOT( itemSelected( int ) ) ); 00156 connect( mListBox, TQT_SIGNAL(doubleClicked(TQListBoxItem*)), TQT_SLOT(editSelectedDistributionList()) ); 00157 layout->addWidget( mListBox ); 00158 00159 connect( core, TQT_SIGNAL( contactsUpdated() ), 00160 this, TQT_SLOT( updateEntries() ) ); 00161 connect( core->addressBook(), TQT_SIGNAL( addressBookChanged( AddressBook* ) ), 00162 this, TQT_SLOT( updateEntries() ) ); 00163 00164 // When contacts are changed, update both distr list combo and contents of displayed distr list 00165 connect( core, TQT_SIGNAL( contactsUpdated() ), 00166 this, TQT_SLOT( updateEntries() ) ); 00167 00168 TQTimer::singleShot( 0, this, TQT_SLOT( updateEntries() ) ); 00169 } 00170 00171 void KAB::DistributionListNg::MainWidget::contextMenuRequested( TQListBoxItem *item, const TQPoint &point ) 00172 { 00173 TQGuardedPtr<KPopupMenu> menu = new KPopupMenu( this ); 00174 menu->insertItem( i18n( "New Distribution List..." ), core(), TQT_SLOT( newDistributionList() ) ); 00175 if ( item && ( item->text() !=i18n( "All Contacts" ) ) ) 00176 { 00177 menu->insertItem( i18n( "Edit..." ), this, TQT_SLOT( editSelectedDistributionList() ) ); 00178 menu->insertItem( i18n( "Delete" ), this, TQT_SLOT( deleteSelectedDistributionList() ) ); 00179 } 00180 menu->exec( point ); 00181 delete menu; 00182 } 00183 00184 void KAB::DistributionListNg::MainWidget::editSelectedDistributionList() 00185 { 00186 const TQListBoxItem* 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 TQListBoxItem* const item = mListBox->selectedItem(); 00195 const TQString name = item ? item->text() : TQString(); 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 TQString &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 TQStringList 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 ? TQString() : mListBox->item( index )->text() ); 00245 mEditButton->setEnabled( index > 0 ); 00246 mRemoveButton->setEnabled( index > 0 ); 00247 } 00248 00249 #include "distributionlistngwidget.moc"