kaddressbook

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