TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

distributionlistngwidget.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. This file is part of KAddressBook.
  3. Copyright (c) 2007 Klaralvdalens Datakonsult AB <frank@kdab.net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. As a special exception, permission is given to link this program
  16. with any edition of TQt, and distribute the resulting executable,
  17. without including the source code for TQt in the source distribution.
  18. */
  19. #include "distributionlistngwidget.h"
  20. #include "interfaces/core.h"
  21. #include "searchmanager.h"
  22. #include <libtdepim/distributionlist.h>
  23. #include <libtdepim/kvcarddrag.h>
  24. #include <kabc/vcardconverter.h>
  25. #include <kdialog.h>
  26. #include <kiconloader.h>
  27. #include <klistview.h>
  28. #include <klocale.h>
  29. #include <kpopupmenu.h>
  30. #include <tqevent.h>
  31. #include <tqguardedptr.h>
  32. #include <tqlabel.h>
  33. #include <tqlayout.h>
  34. #include <tqpoint.h>
  35. #include <tqtimer.h>
  36. #include <tqpushbutton.h>
  37. #include <tqtooltip.h>
  38. KAB::DistributionListNg::ListBox::ListBox( TQWidget* parent ) : KListBox( parent )
  39. {
  40. setAcceptDrops( true );
  41. }
  42. void KAB::DistributionListNg::ListBox::dragMoveEvent( TQDragMoveEvent *event )
  43. {
  44. TQListBoxItem *item = itemAt( event->pos() );
  45. if ( !item ) {
  46. event->ignore();
  47. }
  48. else {
  49. event->accept( tqitemRect( item ) );
  50. }
  51. }
  52. void KAB::DistributionListNg::ListBox::dragEnterEvent( TQDragEnterEvent *event )
  53. {
  54. KListBox::dragEnterEvent( event );
  55. }
  56. void KAB::DistributionListNg::ListBox::dropEvent( TQDropEvent *event )
  57. {
  58. TQListBoxItem *item = itemAt( event->pos() );
  59. if ( !item || index( item ) == 0 )
  60. return;
  61. KABC::Addressee::List list;
  62. if ( !KVCardDrag::decode( event, list ) )
  63. return;
  64. emit dropped( item->text(), list );
  65. }
  66. namespace KAB {
  67. namespace DistributionListNg {
  68. class Factory : public KAB::ExtensionFactory
  69. {
  70. public:
  71. KAB::ExtensionWidget *extension( KAB::Core *core, TQWidget *parent, const char *name )
  72. {
  73. return new KAB::DistributionListNg::MainWidget( core, parent, name );
  74. }
  75. TQString identifier() const
  76. {
  77. return "distribution_list_editor";
  78. }
  79. };
  80. }
  81. }
  82. extern "C" {
  83. void *init_libkaddrbk_distributionlistng()
  84. {
  85. return ( new KAB::DistributionListNg::Factory );
  86. }
  87. }
  88. TQString KAB::DistributionListNg::MainWidget::title() const
  89. {
  90. return i18n( "Distribution List Editor NG" );
  91. }
  92. TQString KAB::DistributionListNg::MainWidget::identifier() const
  93. {
  94. return "distribution_list_editor_ng";
  95. }
  96. KAB::DistributionListNg::MainWidget::MainWidget( KAB::Core *core, TQWidget *parent, const char *name ) : KAB::ExtensionWidget( core, parent, name )
  97. {
  98. TQVBoxLayout *tqlayout = new TQVBoxLayout( this );
  99. tqlayout->setSpacing( KDialog::spacingHint() );
  100. TQHBoxLayout *buttonLayout = new TQHBoxLayout();
  101. tqlayout->addLayout( buttonLayout );
  102. TQLabel *label = new TQLabel( this );
  103. label->setText( i18n( "Distribution Lists" ) );
  104. buttonLayout->addWidget( label );
  105. buttonLayout->addStretch( 1 );
  106. mAddButton = new TQPushButton( this );
  107. mAddButton->setIconSet( SmallIconSet( "add" ) );
  108. TQToolTip::add( mAddButton, i18n( "Add distribution list" ) );
  109. connect( mAddButton, TQT_SIGNAL(clicked()), core, TQT_SLOT(newDistributionList()) );
  110. buttonLayout->addWidget( mAddButton );
  111. mEditButton = new TQPushButton( this );
  112. mEditButton->setIconSet( SmallIconSet( "edit" ) );
  113. TQToolTip::add( mEditButton, i18n( "Edit distribution list" ) );
  114. connect( mEditButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(editSelectedDistributionList()) );
  115. buttonLayout->addWidget( mEditButton );
  116. mRemoveButton = new TQPushButton( this );
  117. mRemoveButton->setIconSet( SmallIconSet( "remove" ) );
  118. TQToolTip::add( mRemoveButton, i18n( "Remove distribution list" ) );
  119. connect( mRemoveButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(deleteSelectedDistributionList()) );
  120. buttonLayout->addWidget( mRemoveButton );
  121. mListBox = new ListBox( this );
  122. connect( mListBox, TQT_SIGNAL( contextMenuRequested( TQListBoxItem*, const TQPoint& ) ),
  123. this, TQT_SLOT( contextMenuRequested( TQListBoxItem*, const TQPoint& ) ) );
  124. connect( mListBox, TQT_SIGNAL( dropped( const TQString &, const KABC::Addressee::List & ) ),
  125. this, TQT_SLOT( contactsDropped( const TQString &, const KABC::Addressee::List & ) ) );
  126. connect( mListBox, TQT_SIGNAL( highlighted( int ) ),
  127. this, TQT_SLOT( itemSelected( int ) ) );
  128. connect( mListBox, TQT_SIGNAL(doubleClicked(TQListBoxItem*)), TQT_SLOT(editSelectedDistributionList()) );
  129. tqlayout->addWidget( mListBox );
  130. connect( core, TQT_SIGNAL( contactsUpdated() ),
  131. this, TQT_SLOT( updateEntries() ) );
  132. connect( core->addressBook(), TQT_SIGNAL( addressBookChanged( AddressBook* ) ),
  133. this, TQT_SLOT( updateEntries() ) );
  134. // When contacts are changed, update both distr list combo and contents of displayed distr list
  135. connect( core, TQT_SIGNAL( contactsUpdated() ),
  136. this, TQT_SLOT( updateEntries() ) );
  137. TQTimer::singleShot( 0, this, TQT_SLOT( updateEntries() ) );
  138. }
  139. void KAB::DistributionListNg::MainWidget::contextMenuRequested( TQListBoxItem *item, const TQPoint &point )
  140. {
  141. TQGuardedPtr<KPopupMenu> menu = new KPopupMenu( this );
  142. menu->insertItem( i18n( "New Distribution List..." ), core(), TQT_SLOT( newDistributionList() ) );
  143. if ( item && ( item->text() !=i18n( "All Contacts" ) ) )
  144. {
  145. menu->insertItem( i18n( "Edit..." ), this, TQT_SLOT( editSelectedDistributionList() ) );
  146. menu->insertItem( i18n( "Delete" ), this, TQT_SLOT( deleteSelectedDistributionList() ) );
  147. }
  148. menu->exec( point );
  149. delete menu;
  150. }
  151. void KAB::DistributionListNg::MainWidget::editSelectedDistributionList()
  152. {
  153. const TQListBoxItem* const item = mListBox->selectedItem();
  154. if ( !item )
  155. return;
  156. core()->editDistributionList( item->text() );
  157. }
  158. void KAB::DistributionListNg::MainWidget::deleteSelectedDistributionList()
  159. {
  160. const TQListBoxItem* const item = mListBox->selectedItem();
  161. const TQString name = item ? item->text() : TQString();
  162. if ( name.isNull() )
  163. return;
  164. const KPIM::DistributionList list = KPIM::DistributionList::findByName(
  165. core()->addressBook(), name );
  166. if ( list.isEmpty() )
  167. return;
  168. core()->deleteDistributionLists( name );
  169. }
  170. void KAB::DistributionListNg::MainWidget::contactsDropped( const TQString &listName, const KABC::Addressee::List &addressees )
  171. {
  172. if ( addressees.isEmpty() )
  173. return;
  174. KPIM::DistributionList list = KPIM::DistributionList::findByName(
  175. core()->addressBook(), listName );
  176. if ( list.isEmpty() ) // not found [should be impossible]
  177. return;
  178. for ( KABC::Addressee::List::ConstIterator it = addressees.begin(); it != addressees.end(); ++it ) {
  179. list.insertEntry( *it );
  180. }
  181. core()->addressBook()->insertAddressee( list );
  182. changed( list );
  183. }
  184. void KAB::DistributionListNg::MainWidget::changed( const KABC::Addressee& dist )
  185. {
  186. emit modified( KABC::Addressee::List() << dist );
  187. }
  188. void KAB::DistributionListNg::MainWidget::updateEntries()
  189. {
  190. const bool hadSelection = mListBox->selectedItem() != 0;
  191. const TQStringList newEntries = core()->distributionListNames();
  192. if ( !mCurrentEntries.isEmpty() && newEntries == mCurrentEntries )
  193. return;
  194. mCurrentEntries = newEntries;
  195. mListBox->clear();
  196. mListBox->insertItem( i18n( "All Contacts" ), 0 );
  197. mListBox->insertStringList( mCurrentEntries );
  198. if ( !hadSelection )
  199. mListBox->setSelected( 0, true );
  200. }
  201. void KAB::DistributionListNg::MainWidget::itemSelected( int index )
  202. {
  203. core()->setSelectedDistributionList( index == 0 ? TQString() : mListBox->item( index )->text() );
  204. mEditButton->setEnabled( index > 0 );
  205. mRemoveButton->setEnabled( index > 0 );
  206. }
  207. #include "distributionlistngwidget.moc"