25 #include <tqsignalmapper.h>
26 #include <tqtoolbutton.h>
28 #include <tdeabc/stdaddressbook.h>
29 #include <kcombobox.h>
31 #include <tdeglobal.h>
32 #include <kiconloader.h>
33 #include <klineedit.h>
34 #include <tdelistview.h>
35 #include <tdelocale.h>
37 #include "addresseeselector.h"
41 class AddresseeSelector::AddressBookManager
44 TQStringList titles()
const;
46 void addResource( TDEABC::Resource* );
47 void addAddressBook(
const TQString &title, SelectionItem::List &list );
53 struct AddressBookEntry {
55 SelectionItem::List list;
58 TQValueList<TDEABC::Resource*> mResources;
59 TQValueList<AddressBookEntry> mAddressBooks;
62 TQStringList AddresseeSelector::AddressBookManager::titles()
const
67 titles.append( i18n(
"All" ) );
69 TQValueList<TDEABC::Resource*>::ConstIterator resIt;
70 for ( resIt = mResources.begin(); resIt != mResources.end(); ++resIt )
71 titles.append( (*resIt)->resourceName() );
73 TQValueList<AddressBookEntry>::ConstIterator abIt;
74 for ( abIt = mAddressBooks.begin(); abIt != mAddressBooks.end(); ++abIt )
75 titles.append( (*abIt).title );
80 void AddresseeSelector::AddressBookManager::addResource( TDEABC::Resource *resource )
82 if ( mResources.find( resource ) == mResources.end() )
83 mResources.append( resource );
86 void AddresseeSelector::AddressBookManager::addAddressBook(
const TQString &title,
87 SelectionItem::List &list )
89 AddressBookEntry entry;
95 mAddressBooks.append( entry );
98 void AddresseeSelector::AddressBookManager::clear()
101 mAddressBooks.clear();
104 bool AddresseeSelector::AddressBookManager::contains( uint index,
const SelectionItem &item )
109 if ( mResources.count() > 0 ) {
110 if ( index <= mResources.count() ) {
112 if ( item.addressee().resource() == mResources[ index ] )
119 index = index - mResources.count();
121 if ( mAddressBooks.count() > 0 ) {
122 if ( index <= mAddressBooks.count() ) {
124 AddressBookEntry entry = mAddressBooks[ index ];
125 SelectionItem::List::ConstIterator it;
126 for ( it = entry.list.begin(); it != entry.list.end(); ++it )
127 if ( (*it).addressee() == item.addressee() )
138 SelectionItem::SelectionItem(
const TDEABC::Addressee &addressee, uint index )
139 : mAddressee( addressee ), mDistributionList( 0 ), mIndex( index )
141 mField.fill(
false, 10 );
144 SelectionItem::SelectionItem( TDEABC::DistributionList *list, uint index )
145 : mDistributionList( list ), mIndex( index )
147 mField.fill(
false, 10 );
150 SelectionItem::SelectionItem()
151 : mDistributionList( 0 ), mIndex( 0 )
153 mField.fill(
false, 10 );
156 void SelectionItem::addToField(
int index )
158 mField.setBit( index );
161 void SelectionItem::removeFromField(
int index )
163 mField.clearBit( index );
166 bool SelectionItem::isInField(
int index )
168 return mField.testBit( index );
171 TDEABC::Addressee SelectionItem::addressee()
const
176 TDEABC::DistributionList* SelectionItem::distributionList()
const
178 return mDistributionList;
181 uint SelectionItem::index()
const
187 class SelectionViewItem :
public TQListViewItem
190 SelectionViewItem( TQListView *parent, Selection *selection,
192 : TQListViewItem( parent,
"" ), mSelection( selection ), mItem( item )
194 if ( mItem->distributionList() == 0 )
195 mIcon = mSelection->itemIcon( mItem->addressee(), mItem->index() );
197 mIcon = mSelection->distributionListIcon( mItem->distributionList() );
200 TQString text(
int column )
const
203 if ( mItem->distributionList() == 0 )
204 return mSelection->itemText( mItem->addressee(), mItem->index() );
206 return mSelection->distributionListText( mItem->distributionList() );
211 const TQPixmap* pixmap(
int column )
const
222 Selection *mSelection;
227 AddresseeSelector::AddresseeSelector( Selection *selection, TQWidget *parent,
const char *name )
228 : TQWidget( parent, name ), mSelection( selection ), mManager( 0 )
230 mMoveMapper =
new TQSignalMapper(
this );
231 mRemoveMapper =
new TQSignalMapper(
this );
233 mAddressBookManager =
new AddressBookManager();
239 mSelection->setSelector(
this );
242 AddresseeSelector::~AddresseeSelector()
247 delete mAddressBookManager;
248 mAddressBookManager = 0;
251 void AddresseeSelector::init()
253 connect( TDEABC::StdAddressBook::self(
true ), TQ_SIGNAL( addressBookChanged( AddressBook* ) ),
254 this, TQ_SLOT( reloadAddressBook() ) );
255 connect( mAddresseeFilter, TQ_SIGNAL( textChanged(
const TQString& ) ),
256 this, TQ_SLOT( updateAddresseeView() ) );
257 connect( mAddressBookCombo, TQ_SIGNAL( activated(
int ) ),
258 this, TQ_SLOT( updateAddresseeView() ) );
260 connect( mMoveMapper, TQ_SIGNAL( mapped(
int ) ),
261 this, TQ_SLOT( move(
int ) ) );
262 connect( mRemoveMapper, TQ_SIGNAL( mapped(
int ) ),
263 this, TQ_SLOT( remove(
int ) ) );
268 void AddresseeSelector::initGUI()
270 TQGridLayout *layout =
new TQGridLayout(
this, 2, 3, KDialog::marginHint(), KDialog::spacingHint() );
271 TQGridLayout *topLayout =
new TQGridLayout(
this, 2, 2, KDialog::marginHint() );
273 TQLabel *label =
new TQLabel( i18n(
"Address book:" ),
this );
274 mAddressBookCombo =
new KComboBox(
false,
this );
276 topLayout->addWidget( label, 0, 0 );
277 topLayout->addWidget( mAddressBookCombo, 0, 1 );
279 label =
new TQLabel( i18n(
"Search:" ),
this );
280 mAddresseeFilter =
new KLineEdit(
this );
282 topLayout->addWidget( label, 1, 0 );
283 topLayout->addWidget( mAddresseeFilter, 1, 1 );
285 topLayout->setColStretch( 1, 1 );
287 layout->addMultiCellLayout( topLayout, 0, 0, 0, 2 );
291 TQIconSet moveSet = TDEGlobal::iconLoader()->loadIconSet(
"go-next", TDEIcon::Small );
292 TQIconSet removeSet = TDEGlobal::iconLoader()->loadIconSet(
"go-previous", TDEIcon::Small );
294 uint count = mSelection->fieldCount();
295 for ( uint i = 0; i < count; ++i, ++row ) {
296 TDEListView *listView =
new TDEListView(
this );
297 listView->addColumn( mSelection->fieldTitle( i ) );
298 listView->setFullWidth(
true );
299 mSelectionViews.append( listView );
301 connect( listView, TQ_SIGNAL( doubleClicked( TQListViewItem*,
const TQPoint&,
int ) ),
302 mRemoveMapper, TQ_SLOT( map() ) );
303 mRemoveMapper->setMapping( listView, i );
305 TQVBoxLayout *buttonLayout =
new TQVBoxLayout(
this );
306 buttonLayout->setAlignment( TQt::AlignBottom );
307 layout->addLayout( buttonLayout, row, 1 );
310 TQToolButton *moveButton =
new TQToolButton(
this );
311 moveButton->setIconSet( moveSet );
312 moveButton->setFixedSize( 30, 30 );
314 connect( moveButton, TQ_SIGNAL( clicked() ),
315 mMoveMapper, TQ_SLOT( map() ) );
316 mMoveMapper->setMapping( moveButton, i );
319 TQToolButton *removeButton =
new TQToolButton(
this );
320 removeButton->setIconSet( removeSet );
321 removeButton->setFixedSize( 30, 30 );
323 connect( removeButton, TQ_SIGNAL( clicked() ),
324 mRemoveMapper, TQ_SLOT( map() ) );
325 mRemoveMapper->setMapping( removeButton, i );
327 buttonLayout->addWidget( moveButton );
328 buttonLayout->addWidget( removeButton );
330 layout->addWidget( listView, row, 2 );
333 mAddresseeView =
new TDEListView(
this );
334 mAddresseeView->addColumn(
"" );
335 mAddresseeView->header()->hide();
336 mAddresseeView->setFullWidth(
true );
338 layout->addMultiCellWidget( mAddresseeView, 1, row, 0, 0 );
341 void AddresseeSelector::finish()
343 SelectionItem::List::Iterator it;
345 for ( uint field = 0; field < mSelection->fieldCount(); ++field ) {
346 for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) {
347 if ( (*it).isInField( field ) ) {
348 if ( (*it).distributionList() == 0 )
349 mSelection->addSelectedAddressees( field, (*it).addressee(), (*it).index() );
351 mSelection->addSelectedDistributionList( field, (*it).distributionList() );
357 void AddresseeSelector::updateAddresseeView()
359 mAddresseeView->clear();
361 int addressBookIndex = mAddressBookCombo->currentItem();
363 SelectionItem::List::Iterator it;
364 for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) {
365 if ( mAddressBookManager->contains( addressBookIndex, *it ) ) {
366 if ( (*it).distributionList() == 0 ) {
367 if ( mAddresseeFilter->text().isEmpty() ||
368 mSelection->itemMatches( (*it).addressee(), (*it).index(),
369 mAddresseeFilter->text() ) )
370 new SelectionViewItem( mAddresseeView, mSelection, &(*it) );
372 if ( mAddresseeFilter->text().isEmpty() ||
373 mSelection->distributionListMatches( (*it).distributionList(),
374 mAddresseeFilter->text() ) )
375 new SelectionViewItem( mAddresseeView, mSelection, &(*it) );
380 updateSelectionViews();
383 void AddresseeSelector::move(
int index )
385 SelectionViewItem *item =
dynamic_cast<SelectionViewItem*
>( mAddresseeView->selectedItem() );
387 item->item()->addToField( index );
388 updateSelectionView( index );
392 void AddresseeSelector::remove(
int index )
394 TDEListView *view = mSelectionViews[ index ];
396 SelectionViewItem *item =
dynamic_cast<SelectionViewItem*
>( view->selectedItem() );
398 item->item()->removeFromField( index );
399 updateSelectionView( index );
403 void AddresseeSelector::setItemSelected( uint fieldIndex,
const TDEABC::Addressee &addr, uint itemIndex )
407 SelectionItem::List::Iterator it;
408 for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) {
409 if ( (*it).addressee() == addr && (*it).index() == itemIndex ) {
410 (*it).addToField( fieldIndex );
417 item.addToField( fieldIndex );
419 mSelectionItems.append( item );
422 updateSelectionView( fieldIndex );
425 void AddresseeSelector::setItemSelected( uint fieldIndex,
const TDEABC::Addressee &addr,
426 uint itemIndex,
const TQString &text )
430 SelectionItem::List::Iterator it;
431 for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) {
432 if ( mSelection->itemEquals( (*it).addressee(), (*it).index(), text ) ) {
433 (*it).addToField( fieldIndex );
440 item.addToField( fieldIndex );
442 mSelectionItems.append( item );
445 updateSelectionView( fieldIndex );
448 void AddresseeSelector::updateSelectionView(
int index )
450 TDEListView *view = mSelectionViews[ index ];
453 SelectionItem::List::Iterator it;
454 for ( it = mSelectionItems.begin(); it != mSelectionItems.end(); ++it ) {
455 if ( (*it).isInField( index ) )
456 new SelectionViewItem( view, mSelection, &(*it) );
460 void AddresseeSelector::updateSelectionViews()
462 for ( uint i = 0; i < mSelection->fieldCount(); ++i )
463 updateSelectionView( i );
466 void AddresseeSelector::reloadAddressBook()
469 TDEABC::Addressee::List list = TDEABC::StdAddressBook::self(
true )->allAddressees();
470 TDEABC::Addressee::List::Iterator it;
472 SelectionItem::List selectedItems;
474 SelectionItem::List::Iterator itemIt;
475 for ( itemIt = mSelectionItems.begin(); itemIt != mSelectionItems.end(); ++itemIt ) {
476 bool isSelected =
false;
477 for ( uint i = 0; i < mSelection->fieldCount(); ++i ) {
478 if ( (*itemIt).isInField( i ) ) {
485 if ( isSelected && (*itemIt).distributionList() == 0 ) {
486 selectedItems.append( *itemIt );
490 mSelectionItems.clear();
491 mSelectionItems = selectedItems;
493 for ( it = list.begin(); it != list.end(); ++it ) {
494 uint itemCount = mSelection->itemCount( *it );
495 for ( uint index = 0; index < itemCount; ++index ) {
496 bool available =
false;
497 for ( itemIt = mSelectionItems.begin(); itemIt != mSelectionItems.end(); ++itemIt ) {
498 if ( (*itemIt).addressee() == (*it) && (*itemIt).index() == index ) {
506 mSelectionItems.append( item );
513 mManager =
new TDEABC::DistributionListManager( TDEABC::StdAddressBook::self(
true ) );
517 TQStringList lists = mManager->listNames();
519 TQStringList::Iterator listIt;
520 for ( listIt = lists.begin(); listIt != lists.end(); ++listIt ) {
521 TDEABC::DistributionList *list = mManager->list( *listIt );
523 mSelectionItems.append( item );
526 mAddressBookManager->clear();
529 mAddressBookCombo->clear();
531 TQPtrList<TDEABC::Resource> resources = TDEABC::StdAddressBook::self(
true )->resources();
532 TQPtrListIterator<TDEABC::Resource> resIt( resources );
533 while ( resIt.current() ) {
534 if ( resIt.current()->isActive() )
535 mAddressBookManager->addResource( resIt );
540 for ( uint i = 0; i < mSelection->addressBookCount(); ++i ) {
541 SelectionItem::List itemList;
543 TDEABC::Addressee::List addrList = mSelection->addressBookContent( i );
544 for ( it = addrList.begin(); it != addrList.end(); ++it ) {
545 uint itemCount = mSelection->itemCount( *it );
546 for ( uint index = 0; index < itemCount; ++index ) {
548 mSelectionItems.append( item );
549 itemList.append( item );
553 mAddressBookManager->addAddressBook( mSelection->addressBookTitle( i ),
557 mAddressBookCombo->insertStringList( mAddressBookManager->titles() );
559 updateAddresseeView();
563 AddresseeSelectorDialog::AddresseeSelectorDialog( Selection *selection,
564 TQWidget *parent,
const char *name )
565 : KDialogBase( Plain,
"", Ok | Cancel, Ok, parent, name, true )
567 TQFrame *frame = plainPage();
568 TQVBoxLayout *layout =
new TQVBoxLayout( frame );
569 mSelector =
new KPIM::AddresseeSelector( selection, frame );
570 layout->addWidget( mSelector );
575 void AddresseeSelectorDialog::accept()
581 #include "addresseeselector.moc"
TDEPIM classes for drag and drop of mails.