kmail

recipientspicker.cpp
1/*
2 This file is part of KMail.
3
4 Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include "recipientspicker.h"
23
24#include "globalsettings.h"
25
26#include <libtdepim/recentaddresses.h>
27#include <libtdepim/ldapsearchdialog.h>
28
29#include <libemailfunctions/email.h>
30
31#ifndef TDEPIM_NEW_DISTRLISTS
32#include <tdeabc/distributionlist.h>
33#endif
34
35#include <tdelistview.h>
36#include <tdelocale.h>
37#include <tdeabc/resource.h>
38#include <kiconloader.h>
39#include <kdialog.h>
40#include <twin.h>
41#include <tdemessagebox.h>
42
43#include <tqlayout.h>
44#include <tqcombobox.h>
45#include <tqpushbutton.h>
46#include <tqtoolbutton.h>
47#include <tqlabel.h>
48
49#ifdef TDEPIM_NEW_DISTRLISTS
50RecipientItem::RecipientItem( TDEABC::AddressBook *ab )
51 : mAddressBook( ab )
52{
53}
54#else
55RecipientItem::RecipientItem()
56 : mDistributionList( 0 )
57{
58}
59#endif
60
61#ifdef TDEPIM_NEW_DISTRLISTS
62void RecipientItem::setDistributionList( KPIM::DistributionList &list )
63{
64 mDistributionList = list;
65
66 mIcon = TDEGlobal::iconLoader()->loadIcon( "tdmconfig", TDEIcon::Small );
67
68 mName = list.name();
69 mKey = list.name();
70
71 int count = list.entries( mAddressBook ).count();
72 mEmail = i18n( "1 email address", "%n email addresses", count );
73
74 mRecipient = mName;
75
76 mTooltip = createTooltip( list );
77}
78#else
79void RecipientItem::setDistributionList( TDEABC::DistributionList *list )
80{
81 mDistributionList = list;
82
83 mIcon = TDEGlobal::iconLoader()->loadIcon( "tdmconfig", TDEIcon::Small );
84
85 mName = list->name();
86 mKey = list->name();
87
88 int count = list->entries().count();
89 mEmail = i18n( "1 email address", "%n email addresses", count );
90
91 mRecipient = mName;
92
93 mTooltip = createTooltip( list );
94}
95#endif
96
97void RecipientItem::setAddressee( const TDEABC::Addressee &a,
98 const TQString &email )
99{
100 mAddressee = a;
101 mEmail = email;
102 mRecipient = mAddressee.fullEmail( mEmail );
103
104 TQImage img = a.photo().data();
105 if ( !img.isNull() )
106 mIcon = img.smoothScale( 20, 20, TQImage::ScaleMin );
107 else
108 mIcon = TDEGlobal::iconLoader()->loadIcon( "preferences-desktop-personal", TDEIcon::Small );
109
110 mName = mAddressee.realName();
111 mKey = mAddressee.realName() + '|' + mEmail;
112
113 mTooltip = "<qt>";
114 if ( !mAddressee.realName().isEmpty() ) {
115 mTooltip += mAddressee.realName() + "<br/>";
116 }
117 mTooltip += "<b>" + mEmail + "</b>";
118}
119
120TQPixmap RecipientItem::icon() const
121{
122 return mIcon;
123}
124
125TQString RecipientItem::name() const
126{
127 return mName;
128}
129
130TQString RecipientItem::email() const
131{
132 return mEmail;
133}
134
135TQString RecipientItem::recipient() const
136{
137 return mRecipient;
138}
139
140TQString RecipientItem::tooltip() const
141{
142 return mTooltip;
143}
144
145#ifdef TDEPIM_NEW_DISTRLISTS
146KPIM::DistributionList& RecipientItem::distributionList() {
147 return mDistributionList;
148}
149#else
150TDEABC::DistributionList * RecipientItem::distributionList() {
151 return mDistributionList;
152}
153#endif
154
155#ifdef TDEPIM_NEW_DISTRLISTS
156TQString RecipientItem::createTooltip( KPIM::DistributionList &distributionList ) const
157{
158 TQString txt = "<qt>";
159
160 txt += "<b>" + i18n( "Distribution List %1" ).arg ( distributionList.name() ) + "</b>";
161 txt += "<ul>";
162 KPIM::DistributionList::Entry::List entries = distributionList.entries( mAddressBook );
163 KPIM::DistributionList::Entry::List::ConstIterator it;
164 for( it = entries.begin(); it != entries.end(); ++it ) {
165 txt += "<li>";
166 txt += (*it).addressee.realName() + ' ';
167 txt += "<em>";
168 if ( (*it).email.isEmpty() ) txt += (*it).addressee.preferredEmail();
169 else txt += (*it).email;
170 txt += "</em>";
171 txt += "<li/>";
172 }
173 txt += "</ul>";
174 txt += "</qt>";
175
176 return txt;
177}
178#else
179TQString RecipientItem::createTooltip( TDEABC::DistributionList *distributionList ) const
180{
181 TQString txt = "<qt>";
182
183 txt += "<b>" + i18n("Distribution List %1" ).arg ( distributionList->name() ) + "</b>";
184 txt += "<ul>";
185 TDEABC::DistributionList::Entry::List entries = distributionList->entries();
186 TDEABC::DistributionList::Entry::List::ConstIterator it;
187 for( it = entries.begin(); it != entries.end(); ++it ) {
188 txt += "<li>";
189 txt += (*it).addressee.realName() + ' ';
190 txt += "<em>";
191 if ( (*it).email.isEmpty() ) txt += (*it).addressee.preferredEmail();
192 else txt += (*it).email;
193 txt += "</em>";
194 txt += "</li>";
195 }
196 txt += "</ul>";
197 txt += "</qt>";
198
199 return txt;
200}
201#endif
202
203void RecipientItem::setRecipientType( const TQString &type )
204{
205 mType = type;
206}
207
208TQString RecipientItem::recipientType() const
209{
210 return mType;
211}
212
213
214RecipientViewItem::RecipientViewItem( RecipientItem *item, TDEListView *listView )
215 : TDEListViewItem( listView ), mRecipientItem( item )
216{
217 setText( 0, item->recipientType() );
218 setText( 1, item->name() );
219 setText( 2, item->email() );
220
221 setPixmap( 1, item->icon() );
222}
223
224RecipientItem *RecipientViewItem::recipientItem() const
225{
226 return mRecipientItem;
227}
228
229
230RecipientsListToolTip::RecipientsListToolTip( TQWidget *parent,
231 TDEListView *listView )
232 : TQToolTip( parent )
233{
234 mListView = listView;
235}
236
237void RecipientsListToolTip::maybeTip( const TQPoint & pos )
238{
239 TQRect r;
240 TQListViewItem *item = mListView->itemAt( pos );
241 RecipientViewItem *i = static_cast<RecipientViewItem *>( item );
242
243 if( item ) {
244 r = mListView->itemRect( item );
245 TQString tipText( i->recipientItem()->tooltip() );
246 if ( !tipText.isEmpty() ) {
247 tip( r, tipText );
248 }
249 }
250}
251
252
253RecipientsCollection::RecipientsCollection( const TQString &id )
254{
255 mId = id;
256 mTitle = id;
257 mIsReferenceContainer = false;
258}
259
260RecipientsCollection::~RecipientsCollection()
261{
262 deleteAll();
263}
264
265void RecipientsCollection::setReferenceContainer( bool isReferenceContainer )
266{
267 mIsReferenceContainer = isReferenceContainer;
268}
269
270bool RecipientsCollection::isReferenceContainer() const
271{
272 return mIsReferenceContainer;
273}
274
275void RecipientsCollection::setTitle( const TQString &title )
276{
277 mTitle = title;
278}
279
280TQString RecipientsCollection::title() const
281{
282 return mTitle;
283}
284
285void RecipientsCollection::addItem( RecipientItem *item )
286{
287 mKeyMap.insert( item->key(), item );
288}
289
290RecipientItem::List RecipientsCollection::items() const
291{
292 return mKeyMap.values();
293}
294
295bool RecipientsCollection::hasEquivalentItem( RecipientItem *item ) const
296{
297 return mKeyMap.find( item->key() ) != mKeyMap.end();
298}
299
300RecipientItem * RecipientsCollection::getEquivalentItem( RecipientItem *item) const
301{
302 TQMap<TQString, RecipientItem *>::ConstIterator it;
303 it = mKeyMap.find( item->key() );
304 if ( it == mKeyMap.end() )
305 return 0;
306 return (*it);
307}
308
309void RecipientsCollection::clear()
310{
311 mKeyMap.clear();
312}
313
314void RecipientsCollection::deleteAll()
315{
316 if ( !isReferenceContainer() ) {
317 TQMap<TQString, RecipientItem *>::ConstIterator it;
318 for( it = mKeyMap.begin(); it != mKeyMap.end(); ++it ) {
319 delete *it;
320 }
321 }
322 clear();
323}
324
325TQString RecipientsCollection::id() const
326{
327 return mId;
328}
329
330SearchLine::SearchLine( TQWidget *parent, TDEListView *listView )
331 : TDEListViewSearchLine( parent, listView )
332{
333}
334
335void SearchLine::keyPressEvent( TQKeyEvent *ev )
336{
337 if ( ev->key() == Key_Down ) emit downPressed();
338
339 TDEListViewSearchLine::keyPressEvent( ev );
340}
341
342
343RecipientsPicker::RecipientsPicker( TQWidget *parent )
344 : TQDialog( parent, "RecipientsPicker" )
345#ifndef TDEPIM_NEW_DISTRLISTS
346 , mDistributionListManager( 0 )
347#endif
348 ,mLdapSearchDialog( 0 )
349{
350// KWin::setType( winId(), NET::TQt::Dock );
351
352 setCaption( i18n("Select Recipient") );
353
354 TQBoxLayout *topLayout = new TQVBoxLayout( this );
355 topLayout->setSpacing( KDialog::spacingHint() );
356 topLayout->setMargin( KDialog::marginHint() );
357
358 TQBoxLayout *resLayout = new TQHBoxLayout( topLayout );
359
360 TQLabel *label = new TQLabel( i18n("Address book:"), this );
361 resLayout->addWidget( label );
362
363 mCollectionCombo = new TQComboBox( this );
364 resLayout->addWidget( mCollectionCombo );
365 resLayout->addItem(new TQSpacerItem(1, 1, TQSizePolicy::Expanding));
366
367// connect( mCollectionCombo, TQ_SIGNAL( highlighted( int ) ),
368// TQ_SLOT( updateList() ) );
369 connect( mCollectionCombo, TQ_SIGNAL( activated( int ) ),
370 TQ_SLOT( updateList() ) );
371
372 TQBoxLayout *searchLayout = new TQHBoxLayout( topLayout );
373
374 TQToolButton *button = new TQToolButton( this );
375 button->setIconSet( TDEGlobal::iconLoader()->loadIconSet(
376 TDEApplication::reverseLayout() ? "clear_left":"locationbar_erase", TDEIcon::Small, 0 ) );
377 searchLayout->addWidget( button );
378 connect( button, TQ_SIGNAL( clicked() ), TQ_SLOT( resetSearch() ) );
379
380 label = new TQLabel( i18n("&Search:"), this );
381 searchLayout->addWidget( label );
382
383 mRecipientList = new TDEListView( this );
384 mRecipientList->setSelectionMode( TQListView::Extended );
385 mRecipientList->setAllColumnsShowFocus( true );
386 mRecipientList->setFullWidth( true );
387 topLayout->addWidget( mRecipientList );
388 mRecipientList->addColumn( i18n("->") );
389 mRecipientList->addColumn( i18n("Name") );
390 mRecipientList->addColumn( i18n("Email") );
391 connect( mRecipientList, TQ_SIGNAL( doubleClicked( TQListViewItem *,
392 const TQPoint &, int ) ), TQ_SLOT( slotPicked() ) );
393 connect( mRecipientList, TQ_SIGNAL( returnPressed( TQListViewItem * ) ),
394 TQ_SLOT( slotPicked() ) );
395
396 new RecipientsListToolTip( mRecipientList->viewport(), mRecipientList );
397
398 mSearchLine = new SearchLine( this, mRecipientList );
399 searchLayout->addWidget( mSearchLine );
400 label->setBuddy( label );
401 connect( mSearchLine, TQ_SIGNAL( downPressed() ), TQ_SLOT( setFocusList() ) );
402
403 mSearchLDAPButton = new TQPushButton( i18n("Search &Directory Service"), this );
404 searchLayout->addWidget( mSearchLDAPButton );
405 connect( mSearchLDAPButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotSearchLDAP() ) );
406
407 TQBoxLayout *buttonLayout = new TQHBoxLayout( topLayout );
408
409 buttonLayout->addStretch( 1 );
410
411 mToButton = new TQPushButton( i18n("Add as To"), this );
412 buttonLayout->addWidget( mToButton );
413 connect( mToButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotToClicked() ) );
414
415 mCcButton = new TQPushButton( i18n("Add as CC"), this );
416 buttonLayout->addWidget( mCcButton );
417 connect( mCcButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotCcClicked() ) );
418
419 mBccButton = new TQPushButton( i18n("Add as BCC"), this );
420 buttonLayout->addWidget( mBccButton );
421 connect( mBccButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotBccClicked() ) );
422 // BCC isn't commonly used, so hide it for now
423 //mBccButton->hide();
424
425 TQPushButton *closeButton = new TQPushButton( i18n("&Cancel"), this );
426 buttonLayout->addWidget( closeButton );
427 connect( closeButton, TQ_SIGNAL( clicked() ), TQ_SLOT( close() ) );
428
429 {
430 using namespace TDEABC;
431 mAddressBook = TDEABC::StdAddressBook::self( true );
432 connect( mAddressBook, TQ_SIGNAL( addressBookChanged( AddressBook * ) ),
433 this, TQ_SLOT( insertAddressBook( AddressBook * ) ) );
434 }
435
436 initCollections();
437
438 mCollectionCombo->setCurrentItem( 0 );
439
440 updateList();
441
442 mSearchLine->setFocus();
443
444 readConfig();
445
446 setTabOrder( mCollectionCombo, mSearchLine );
447 setTabOrder( mSearchLine, mRecipientList );
448 setTabOrder( closeButton, mCollectionCombo );
449}
450
451RecipientsPicker::~RecipientsPicker()
452{
453 writeConfig();
454
455#ifndef TDEPIM_NEW_DISTRLISTS
456 delete mDistributionListManager;
457#endif
458
459 TQMap<int,RecipientsCollection *>::ConstIterator it;
460 for( it = mCollectionMap.begin(); it != mCollectionMap.end(); ++it ) {
461 delete *it;
462 }
463}
464
465void RecipientsPicker::initCollections()
466{
467 mAllRecipients = new RecipientsCollection( i18n("All") );
468 mAllRecipients->setReferenceContainer( true );
469 mDistributionLists = new RecipientsCollection( i18n("Distribution Lists") );
470 mSelectedRecipients = new RecipientsCollection( i18n("Selected Recipients") );
471
472 insertCollection( mAllRecipients );
473 insertAddressBook( mAddressBook );
474 insertCollection( mDistributionLists );
475 insertRecentAddresses();
476 insertCollection( mSelectedRecipients );
477
478 rebuildAllRecipientsList();
479}
480
481void RecipientsPicker::insertAddressBook( TDEABC::AddressBook *addressbook )
482{
483 TQMap<TDEABC::Resource *,RecipientsCollection *> collectionMap;
484
485 TQPtrList<TDEABC::Resource> resources = addressbook->resources();
486 TDEABC::Resource *res;
487 for( res = resources.first(); res; res = resources.next() ) {
488 RecipientsCollection *collection = new RecipientsCollection( res->identifier() );
489 collectionMap.insert( res, collection );
490 collection->setTitle( res->resourceName() );
491 }
492
493 TQMap<TQString,RecipientsCollection *> categoryMap;
494
495 TDEABC::AddressBook::Iterator it;
496 for( it = addressbook->begin(); it != addressbook->end(); ++it ) {
497 TQStringList emails = (*it).emails();
498 TQStringList::ConstIterator it3;
499 for( it3 = emails.begin(); it3 != emails.end(); ++it3 ) {
500#ifdef TDEPIM_NEW_DISTRLISTS
501 RecipientItem *item = new RecipientItem( mAddressBook );
502#else
503 RecipientItem *item = new RecipientItem;
504#endif
505 item->setAddressee( *it, *it3 );
506
507 TQMap<TDEABC::Resource *,RecipientsCollection *>::ConstIterator collIt;
508 collIt = collectionMap.find( it->resource() );
509 if ( collIt != collectionMap.end() ) {
510 (*collIt)->addItem( item );
511 }
512
513 TQStringList categories = (*it).categories();
514 TQStringList::ConstIterator catIt;
515 for( catIt = categories.begin(); catIt != categories.end(); ++catIt ) {
516 TQMap<TQString, RecipientsCollection *>::ConstIterator catMapIt;
517 catMapIt = categoryMap.find( *catIt );
518 RecipientsCollection *collection;
519 if ( catMapIt == categoryMap.end() ) {
520 collection = new RecipientsCollection( *catIt );
521 collection->setReferenceContainer( true );
522 categoryMap.insert( *catIt, collection );
523 } else {
524 collection = *catMapIt;
525 }
526 collection->addItem( item );
527 }
528 }
529 }
530
531 TQMap<TDEABC::Resource *,RecipientsCollection *>::ConstIterator it2;
532 for( it2 = collectionMap.begin(); it2 != collectionMap.end(); ++it2 ) {
533 insertCollection( *it2 );
534 }
535
536 TQMap<TQString, RecipientsCollection *>::ConstIterator it3;
537 for( it3 = categoryMap.begin(); it3 != categoryMap.end(); ++it3 ) {
538 insertCollection( *it3 );
539 }
540
541 insertDistributionLists();
542 rebuildAllRecipientsList();
543 updateList();
544}
545
546void RecipientsPicker::insertDistributionLists()
547{
548 mDistributionLists->deleteAll();
549
550#ifdef TDEPIM_NEW_DISTRLISTS
551 TQValueList<KPIM::DistributionList> lists = KPIM::DistributionList::allDistributionLists( mAddressBook );
552 for ( uint i = 0; i < lists.count(); ++i ) {
553 RecipientItem *item = new RecipientItem( mAddressBook );
554 item->setDistributionList( lists[ i ] );
555 mDistributionLists->addItem( item );
556 }
557#else
558 delete mDistributionListManager;
559 mDistributionListManager =
560 new TDEABC::DistributionListManager( TDEABC::StdAddressBook::self( true ) );
561
562 mDistributionListManager->load();
563
564 TQStringList lists = mDistributionListManager->listNames();
565
566 TQStringList::Iterator listIt;
567 for ( listIt = lists.begin(); listIt != lists.end(); ++listIt ) {
568 TDEABC::DistributionList *list = mDistributionListManager->list( *listIt );
569 RecipientItem *item = new RecipientItem;
570 item->setDistributionList( list );
571 mDistributionLists->addItem( item );
572 }
573#endif
574}
575
576void RecipientsPicker::insertRecentAddresses()
577{
578 RecipientsCollection *collection = new RecipientsCollection( i18n("Recent Addresses") );
579
580 TDEConfig config( "kmailrc" );
581 TDEABC::Addressee::List recents =
582 TDERecentAddress::RecentAddresses::self( &config )->tdeabcAddresses();
583
584 TDEABC::Addressee::List::ConstIterator it;
585 for( it = recents.begin(); it != recents.end(); ++it ) {
586#ifdef TDEPIM_NEW_DISTRLISTS
587 RecipientItem *item = new RecipientItem( mAddressBook );
588#else
589 RecipientItem *item = new RecipientItem;
590#endif
591 item->setAddressee( *it, (*it).preferredEmail() );
592 collection->addItem( item );
593 }
594
595 insertCollection( collection );
596}
597
598void RecipientsPicker::insertCollection( RecipientsCollection *coll )
599{
600 int index = 0;
601 TQMap<int,RecipientsCollection *>::ConstIterator it;
602 for ( it = mCollectionMap.begin(); it != mCollectionMap.end(); ++it ) {
603 if ( (*it)->id() == coll->id() ) {
604 delete *it;
605 mCollectionMap.remove( index );
606 mCollectionMap.insert( index, coll );
607 return;
608 }
609 index++;
610 }
611
612 mCollectionCombo->insertItem( coll->title(), index );
613 mCollectionMap.insert( index, coll );
614}
615
616void RecipientsPicker::updateRecipient( const Recipient &recipient )
617{
618 RecipientItem::List allRecipients = mAllRecipients->items();
619 RecipientItem::List::ConstIterator itAll;
620 for( itAll = allRecipients.begin(); itAll != allRecipients.end(); ++itAll ) {
621 if ( (*itAll)->recipient() == recipient.email() ) {
622 (*itAll)->setRecipientType( recipient.typeLabel() );
623 }
624 }
625 updateList();
626}
627
628void RecipientsPicker::setRecipients( const Recipient::List &recipients )
629{
630 mSelectedRecipients->deleteAll();
631
632 Recipient::List::ConstIterator it;
633 for( it = recipients.begin(); it != recipients.end(); ++it ) {
634 RecipientItem *item = 0;
635
636 // if recipient is a distribution list, create
637 // a detached copy.
638 RecipientItem::List items = mDistributionLists->items();
639 RecipientItem::List::ConstIterator distIt;
640#ifdef TDEPIM_NEW_DISTRLISTS
641 for ( distIt = items.begin(); distIt != items.end(); ++distIt ) {
642 if ( (*it).email() == (*distIt)->name() ) {
643 item = new RecipientItem( mAddressBook );
644 item->setDistributionList( (*distIt)->distributionList() );
645 }
646 }
647#else
648 for ( distIt = items.begin(); distIt != items.end(); ++distIt ) {
649 if ( (*it).email() == (*distIt)->name() ) {
650 item = new RecipientItem();
651 item->setDistributionList( (*distIt)->distributionList() );
652 }
653 }
654#endif
655
656 if ( !item ) {
657 TDEABC::Addressee a;
658 TQString name;
659 TQString email;
660 TDEABC::Addressee::parseEmailAddress( (*it).email(), name, email );
661 a.setNameFromString( name );
662 a.insertEmail( email );
663
664#ifdef TDEPIM_NEW_DISTRLISTS
665 item = new RecipientItem( mAddressBook );
666#else
667 item = new RecipientItem;
668#endif
669 item->setAddressee( a, a.preferredEmail() );
670 }
671
672 item->setRecipientType( (*it).typeLabel() );
673 mSelectedRecipients->addItem( item );
674 }
675
676 updateList();
677}
678
679void RecipientsPicker::setDefaultButton( TQPushButton *button )
680{
681// button->setText( "<qt><b>" + button->text() + "</b></qt>" );
682 button->setDefault( true );
683}
684
685void RecipientsPicker::setDefaultType( Recipient::Type type )
686{
687 mDefaultType = type;
688
689 if ( type == Recipient::To ) {
690 setDefaultButton( mToButton );
691 } else if ( type == Recipient::Cc ) {
692 setDefaultButton( mCcButton );
693 } else if ( type == Recipient::Bcc ) {
694 setDefaultButton( mBccButton );
695 }
696}
697
698void RecipientsPicker::rebuildAllRecipientsList()
699{
700 mAllRecipients->clear();
701
702 TQMap<int,RecipientsCollection *>::ConstIterator it;
703 for( it = mCollectionMap.begin(); it != mCollectionMap.end(); ++it ) {
704 // skip self
705 if ( (*it) == mAllRecipients )
706 continue;
707
708 RecipientItem::List coll = (*it)->items();
709
710 RecipientItem::List::ConstIterator rcptIt;
711 for ( rcptIt = coll.begin(); rcptIt != coll.end(); ++rcptIt ) {
712 mAllRecipients->addItem( *rcptIt );
713 }
714 }
715}
716
717void RecipientsPicker::updateList()
718{
719 mRecipientList->clear();
720
721 RecipientsCollection *coll = mCollectionMap[ mCollectionCombo->currentItem() ];
722
723 RecipientItem::List items = coll->items();
724 RecipientItem::List::ConstIterator it;
725 for( it = items.begin(); it != items.end(); ++it ) {
726 if ( coll != mSelectedRecipients ) {
727 RecipientItem *selItem = mSelectedRecipients->getEquivalentItem( *it );
728 if ( selItem ) {
729 (*it)->setRecipientType( selItem->recipientType() );
730 } else {
731 (*it)->setRecipientType( TQString() );
732 }
733 }
734 new RecipientViewItem( *it, mRecipientList );
735 }
736
737 mSearchLine->updateSearch();
738}
739
740void RecipientsPicker::slotToClicked()
741{
742 pick( Recipient::To );
743}
744
745void RecipientsPicker::slotCcClicked()
746{
747 pick( Recipient::Cc );
748}
749
750void RecipientsPicker::slotBccClicked()
751{
752 pick( Recipient::Bcc );
753}
754
755void RecipientsPicker::slotPicked( TQListViewItem *viewItem )
756{
757 RecipientViewItem *item = static_cast<RecipientViewItem *>( viewItem );
758 if ( item ) {
759 RecipientItem *i = item->recipientItem();
760 emit pickedRecipient( Recipient( i->recipient(), Recipient::Undefined ) );
761 }
762 close();
763}
764
765void RecipientsPicker::slotPicked()
766{
767 pick( mDefaultType );
768}
769
770void RecipientsPicker::pick( Recipient::Type type )
771{
772 kdDebug() << "RecipientsPicker::pick " << int( type ) << endl;
773
774 int count = 0;
775 TQListViewItemIterator it( mRecipientList ,
776 TQListViewItemIterator::Visible | TQListViewItemIterator::Selected );
777 for ( ; it.current(); ++it )
778 ++count;
779
780 if ( count > GlobalSettings::self()->maximumRecipients() ) {
781 KMessageBox::sorry( this,
782 i18n("You selected 1 recipient. The maximum supported number of "
783 "recipients is %1. Please adapt the selection.",
784 "You selected %n recipients. The maximum supported number of "
785 "recipients is %1. Please adapt the selection.", count)
786 .arg( GlobalSettings::self()->maximumRecipients() ) );
787 return;
788 }
789
790 it = TQListViewItemIterator( mRecipientList ,
791 TQListViewItemIterator::Visible | TQListViewItemIterator::Selected );
792 for ( ; it.current(); ++it ) {
793 RecipientViewItem *item = static_cast<RecipientViewItem *>( it.current() );
794 if ( item ) {
795 RecipientItem *i = item->recipientItem();
796 Recipient r = i->recipient();
797 r.setType( type );
798 emit pickedRecipient( r );
799 }
800 }
801 close();
802}
803
804void RecipientsPicker::keyPressEvent( TQKeyEvent *ev )
805{
806 if ( ev->key() == Key_Escape ) close();
807
808 TQWidget::keyPressEvent( ev );
809}
810
811void RecipientsPicker::readConfig()
812{
813 TDEConfig *cfg = TDEGlobal::config();
814 cfg->setGroup( "RecipientsPicker" );
815 TQSize size = cfg->readSizeEntry( "Size" );
816 if ( !size.isEmpty() ) {
817 resize( size );
818 }
819 int currentCollection = cfg->readNumEntry( "CurrentCollection", -1 );
820 if ( currentCollection >= 0 &&
821 currentCollection < mCollectionCombo->count() ) {
822 mCollectionCombo->setCurrentItem( currentCollection );
823 }
824}
825
826void RecipientsPicker::writeConfig()
827{
828 TDEConfig *cfg = TDEGlobal::config();
829 cfg->setGroup( "RecipientsPicker" );
830 cfg->writeEntry( "Size", size() );
831 cfg->writeEntry( "CurrentCollection", mCollectionCombo->currentItem() );
832}
833
834void RecipientsPicker::setFocusList()
835{
836 mRecipientList->setFocus();
837}
838
839void RecipientsPicker::resetSearch()
840{
841 mSearchLine->setText( TQString() );
842}
843
844void RecipientsPicker::slotSearchLDAP()
845{
846 if ( !mLdapSearchDialog ) {
847 mLdapSearchDialog = new KPIM::LDAPSearchDialog( this );
848 connect( mLdapSearchDialog, TQ_SIGNAL( addresseesAdded() ),
849 TQ_SLOT(ldapSearchResult() ) );
850 }
851 mLdapSearchDialog->setSearchText( mSearchLine->text() );
852 mLdapSearchDialog->show();
853
854}
855
856void RecipientsPicker::ldapSearchResult()
857{
858 TQStringList emails = KPIM::splitEmailAddrList( mLdapSearchDialog->selectedEMails() );
859 TQStringList::iterator it( emails.begin() );
860 TQStringList::iterator end( emails.end() );
861 for ( ; it != end; ++it ){
862 TQString name;
863 TQString email;
864 KPIM::getNameAndMail( (*it), name, email );
865 TDEABC::Addressee ad;
866 ad.setNameFromString( name );
867 ad.insertEmail( email );
868#ifdef TDEPIM_NEW_DISTRLISTS
869 RecipientItem *item = new RecipientItem( mAddressBook );
870#else
871 RecipientItem *item = new RecipientItem;
872#endif
873 item->setAddressee( ad, ad.preferredEmail() );
874 emit pickedRecipient( Recipient( item->recipient(), Recipient::Undefined ) );
875 }
876}
877
878#include "recipientspicker.moc"