kmail

recipientspicker.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of KMail.
00003 
00004     Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "recipientspicker.h"
00023 
00024 #include "globalsettings.h"
00025 
00026 #include <libkdepim/recentaddresses.h>
00027 #include <libkdepim/ldapsearchdialog.h>
00028 
00029 #include <libemailfunctions/email.h>
00030 
00031 #ifndef KDEPIM_NEW_DISTRLISTS
00032 #include <kabc/distributionlist.h>
00033 #endif
00034 
00035 #include <klistview.h>
00036 #include <klocale.h>
00037 #include <kabc/resource.h>
00038 #include <kiconloader.h>
00039 #include <kdialog.h>
00040 #include <kwin.h>
00041 #include <kmessagebox.h>
00042 
00043 #include <qlayout.h>
00044 #include <qcombobox.h>
00045 #include <qpushbutton.h>
00046 #include <qtoolbutton.h>
00047 #include <qlabel.h>
00048 
00049 #ifdef KDEPIM_NEW_DISTRLISTS
00050 RecipientItem::RecipientItem( KABC::AddressBook *ab )
00051   : mAddressBook( ab )
00052 {
00053 }
00054 #else
00055 RecipientItem::RecipientItem()
00056   : mDistributionList( 0 )
00057 {
00058 }
00059 #endif
00060 
00061 #ifdef KDEPIM_NEW_DISTRLISTS
00062 void RecipientItem::setDistributionList( KPIM::DistributionList &list )
00063 {
00064   mDistributionList = list;
00065 
00066   mIcon = KGlobal::iconLoader()->loadIcon( "kdmconfig", KIcon::Small );
00067 
00068   mName = list.name();
00069   mKey = list.name();
00070 
00071   int count = list.entries( mAddressBook ).count();
00072   mEmail = i18n( "1 email address", "%n email addresses", count );
00073 
00074   mRecipient = mName;
00075 
00076   mTooltip = createTooltip( list );
00077 }
00078 #else
00079 void RecipientItem::setDistributionList( KABC::DistributionList *list )
00080 {
00081   mDistributionList = list;
00082 
00083   mIcon = KGlobal::iconLoader()->loadIcon( "kdmconfig", KIcon::Small );
00084 
00085   mName = list->name();
00086   mKey = list->name();
00087 
00088   int count = list->entries().count();
00089   mEmail = i18n( "1 email address", "%n email addresses", count );
00090 
00091   mRecipient = mName;
00092 
00093   mTooltip = createTooltip( list );
00094 }
00095 #endif
00096 
00097 void RecipientItem::setAddressee( const KABC::Addressee &a,
00098   const QString &email )
00099 {
00100   mAddressee = a;
00101   mEmail = email;
00102   mRecipient = mAddressee.fullEmail( mEmail );
00103 
00104   QImage img = a.photo().data();
00105   if ( !img.isNull() )
00106     mIcon = img.smoothScale( 20, 20, QImage::ScaleMin );
00107   else
00108     mIcon = KGlobal::iconLoader()->loadIcon( "personal", KIcon::Small );
00109 
00110   mName = mAddressee.realName();
00111   mKey = mAddressee.realName() + '|' + mEmail;
00112 
00113   mTooltip = "<qt>";
00114   if ( !mAddressee.realName().isEmpty() ) {
00115     mTooltip += mAddressee.realName() + "<br/>";
00116   }
00117   mTooltip += "<b>" + mEmail + "</b>";
00118 }
00119 
00120 QPixmap RecipientItem::icon() const
00121 {
00122   return mIcon;
00123 }
00124 
00125 QString RecipientItem::name() const
00126 {
00127   return mName;
00128 }
00129 
00130 QString RecipientItem::email() const
00131 {
00132   return mEmail;
00133 }
00134 
00135 QString RecipientItem::recipient() const
00136 {
00137   return mRecipient;
00138 }
00139 
00140 QString RecipientItem::tooltip() const
00141 {
00142   return mTooltip;
00143 }
00144 
00145 #ifdef KDEPIM_NEW_DISTRLISTS
00146 KPIM::DistributionList& RecipientItem::distributionList() {
00147   return mDistributionList;
00148 }
00149 #else
00150 KABC::DistributionList * RecipientItem::distributionList() {
00151   return mDistributionList;
00152 }
00153 #endif
00154 
00155 #ifdef KDEPIM_NEW_DISTRLISTS
00156 QString RecipientItem::createTooltip( KPIM::DistributionList &distributionList ) const
00157 {
00158   QString txt = "<qt>";
00159 
00160   txt += "<b>" + i18n( "Distribution List %1" ).arg ( distributionList.name() ) + "</b>";
00161   txt += "<ul>";
00162   KPIM::DistributionList::Entry::List entries = distributionList.entries( mAddressBook );
00163   KPIM::DistributionList::Entry::List::ConstIterator it;
00164   for( it = entries.begin(); it != entries.end(); ++it ) {
00165     txt += "<li>";
00166     txt += (*it).addressee.realName() + ' ';
00167     txt += "<em>";
00168     if ( (*it).email.isEmpty() ) txt += (*it).addressee.preferredEmail();
00169     else txt += (*it).email;
00170     txt += "</em>";
00171     txt += "<li/>";
00172   }
00173   txt += "</ul>";
00174   txt += "</qt>";
00175 
00176   return txt;
00177 }
00178 #else
00179 QString RecipientItem::createTooltip( KABC::DistributionList *distributionList ) const
00180 {
00181   QString txt = "<qt>";
00182 
00183   txt += "<b>" + i18n("Distribution List %1" ).arg ( distributionList->name() ) + "</b>";
00184   txt += "<ul>";
00185   KABC::DistributionList::Entry::List entries = distributionList->entries();
00186   KABC::DistributionList::Entry::List::ConstIterator it;
00187   for( it = entries.begin(); it != entries.end(); ++it ) {
00188     txt += "<li>";
00189     txt += (*it).addressee.realName() + ' ';
00190     txt += "<em>";
00191     if ( (*it).email.isEmpty() ) txt += (*it).addressee.preferredEmail();
00192     else txt += (*it).email;
00193     txt += "</em>";
00194     txt += "</li>";
00195   }
00196   txt += "</ul>";
00197   txt += "</qt>";
00198 
00199   return txt;
00200 }
00201 #endif
00202 
00203 void RecipientItem::setRecipientType( const QString &type )
00204 {
00205   mType = type;
00206 }
00207 
00208 QString RecipientItem::recipientType() const
00209 {
00210   return mType;
00211 }
00212 
00213 
00214 RecipientViewItem::RecipientViewItem( RecipientItem *item, KListView *listView )
00215   : KListViewItem( listView ), mRecipientItem( item )
00216 {
00217   setText( 0, item->recipientType() );
00218   setText( 1, item->name() );
00219   setText( 2, item->email() );
00220 
00221   setPixmap( 1, item->icon() );
00222 }
00223 
00224 RecipientItem *RecipientViewItem::recipientItem() const
00225 {
00226   return mRecipientItem;
00227 }
00228 
00229 
00230 RecipientsListToolTip::RecipientsListToolTip( QWidget *parent,
00231   KListView *listView )
00232   : QToolTip( parent )
00233 {
00234   mListView = listView;
00235 }
00236 
00237 void RecipientsListToolTip::maybeTip( const QPoint & pos )
00238 {
00239   QRect r;
00240   QListViewItem *item = mListView->itemAt( pos );
00241   RecipientViewItem *i = static_cast<RecipientViewItem *>( item );
00242 
00243   if( item ) {
00244     r = mListView->itemRect( item );
00245     QString tipText( i->recipientItem()->tooltip() );
00246     if ( !tipText.isEmpty() ) {
00247       tip( r, tipText );
00248     }
00249   }
00250 }
00251 
00252 
00253 RecipientsCollection::RecipientsCollection( const QString &id )
00254 {
00255   mId = id;
00256   mTitle = id;
00257   mIsReferenceContainer = false;
00258 }
00259 
00260 RecipientsCollection::~RecipientsCollection()
00261 {
00262   deleteAll();
00263 }
00264 
00265 void RecipientsCollection::setReferenceContainer( bool isReferenceContainer )
00266 {
00267   mIsReferenceContainer = isReferenceContainer;
00268 }
00269 
00270 bool RecipientsCollection::isReferenceContainer() const
00271 {
00272   return mIsReferenceContainer;
00273 }
00274 
00275 void RecipientsCollection::setTitle( const QString &title )
00276 {
00277   mTitle = title;
00278 }
00279 
00280 QString RecipientsCollection::title() const
00281 {
00282   return mTitle;
00283 }
00284 
00285 void RecipientsCollection::addItem( RecipientItem *item )
00286 {
00287   mKeyMap.insert( item->key(), item );
00288 }
00289 
00290 RecipientItem::List RecipientsCollection::items() const
00291 {
00292   return mKeyMap.values();
00293 }
00294 
00295 bool RecipientsCollection::hasEquivalentItem( RecipientItem *item ) const
00296 {
00297   return mKeyMap.find( item->key() ) != mKeyMap.end();
00298 }
00299 
00300 RecipientItem * RecipientsCollection::getEquivalentItem( RecipientItem *item) const
00301 {
00302   QMap<QString, RecipientItem *>::ConstIterator it;
00303   it = mKeyMap.find( item->key() );
00304   if ( it == mKeyMap.end() )
00305     return 0;
00306   return (*it);
00307 }
00308 
00309 void RecipientsCollection::clear()
00310 {
00311   mKeyMap.clear();
00312 }
00313 
00314 void RecipientsCollection::deleteAll()
00315 {
00316   if ( !isReferenceContainer() ) {
00317     QMap<QString, RecipientItem *>::ConstIterator it;
00318     for( it = mKeyMap.begin(); it != mKeyMap.end(); ++it ) {
00319       delete *it;
00320     }
00321   }
00322   clear();
00323 }
00324 
00325 QString RecipientsCollection::id() const
00326 {
00327   return mId;
00328 }
00329 
00330 SearchLine::SearchLine( QWidget *parent, KListView *listView )
00331   : KListViewSearchLine( parent, listView )
00332 {
00333 }
00334 
00335 void SearchLine::keyPressEvent( QKeyEvent *ev )
00336 {
00337   if ( ev->key() == Key_Down ) emit downPressed();
00338 
00339   KListViewSearchLine::keyPressEvent( ev );
00340 }
00341 
00342 
00343 RecipientsPicker::RecipientsPicker( QWidget *parent )
00344   : QDialog( parent, "RecipientsPicker" )
00345 #ifndef KDEPIM_NEW_DISTRLISTS
00346     , mDistributionListManager( 0 )
00347 #endif
00348     ,mLdapSearchDialog( 0 )
00349 {
00350 //  KWin::setType( winId(), NET::Dock );
00351 
00352   setCaption( i18n("Select Recipient") );
00353 
00354   QBoxLayout *topLayout = new QVBoxLayout( this );
00355   topLayout->setSpacing( KDialog::spacingHint() );
00356   topLayout->setMargin( KDialog::marginHint() );
00357 
00358   QBoxLayout *resLayout = new QHBoxLayout( topLayout );
00359 
00360   QLabel *label = new QLabel( i18n("Address book:"), this );
00361   resLayout->addWidget( label );
00362 
00363   mCollectionCombo = new QComboBox( this );
00364   resLayout->addWidget( mCollectionCombo );
00365   resLayout->addItem(new QSpacerItem(1, 1, QSizePolicy::Expanding));
00366 
00367 //  connect( mCollectionCombo, SIGNAL( highlighted( int ) ),
00368 //    SLOT( updateList() ) );
00369   connect( mCollectionCombo, SIGNAL( activated( int ) ),
00370     SLOT( updateList() ) );
00371 
00372   QBoxLayout *searchLayout = new QHBoxLayout( topLayout );
00373 
00374   QToolButton *button = new QToolButton( this );
00375   button->setIconSet( KGlobal::iconLoader()->loadIconSet(
00376               KApplication::reverseLayout() ? "clear_left":"locationbar_erase", KIcon::Small, 0 ) );
00377   searchLayout->addWidget( button );
00378   connect( button, SIGNAL( clicked() ), SLOT( resetSearch() ) );
00379 
00380   label = new QLabel( i18n("&Search:"), this );
00381   searchLayout->addWidget( label );
00382 
00383   mRecipientList = new KListView( this );
00384   mRecipientList->setSelectionMode( QListView::Extended );
00385   mRecipientList->setAllColumnsShowFocus( true );
00386   mRecipientList->setFullWidth( true );
00387   topLayout->addWidget( mRecipientList );
00388   mRecipientList->addColumn( i18n("->") );
00389   mRecipientList->addColumn( i18n("Name") );
00390   mRecipientList->addColumn( i18n("Email") );
00391   connect( mRecipientList, SIGNAL( doubleClicked( QListViewItem *,
00392     const QPoint &, int ) ), SLOT( slotPicked() ) );
00393   connect( mRecipientList, SIGNAL( returnPressed( QListViewItem * ) ),
00394     SLOT( slotPicked() ) );
00395 
00396   new RecipientsListToolTip( mRecipientList->viewport(), mRecipientList );
00397 
00398   mSearchLine = new SearchLine( this, mRecipientList );
00399   searchLayout->addWidget( mSearchLine );
00400   label->setBuddy( label );
00401   connect( mSearchLine, SIGNAL( downPressed() ), SLOT( setFocusList() ) );
00402 
00403   mSearchLDAPButton = new QPushButton( i18n("Search &Directory Service"), this );
00404   searchLayout->addWidget( mSearchLDAPButton );
00405   connect( mSearchLDAPButton, SIGNAL( clicked() ), SLOT( slotSearchLDAP() ) );
00406 
00407   QBoxLayout *buttonLayout = new QHBoxLayout( topLayout );
00408 
00409   buttonLayout->addStretch( 1 );
00410 
00411   mToButton = new QPushButton( i18n("Add as To"), this );
00412   buttonLayout->addWidget( mToButton );
00413   connect( mToButton, SIGNAL( clicked() ), SLOT( slotToClicked() ) );
00414 
00415   mCcButton = new QPushButton( i18n("Add as CC"), this );
00416   buttonLayout->addWidget( mCcButton );
00417   connect( mCcButton, SIGNAL( clicked() ), SLOT( slotCcClicked() ) );
00418 
00419   mBccButton = new QPushButton( i18n("Add as BCC"), this );
00420   buttonLayout->addWidget( mBccButton );
00421   connect( mBccButton, SIGNAL( clicked() ), SLOT( slotBccClicked() ) );
00422   // BCC isn't commonly used, so hide it for now
00423   //mBccButton->hide();
00424 
00425   QPushButton *closeButton = new QPushButton( i18n("&Cancel"), this );
00426   buttonLayout->addWidget( closeButton );
00427   connect( closeButton, SIGNAL( clicked() ), SLOT( close() ) );
00428 
00429   {
00430     using namespace KABC;
00431     mAddressBook = KABC::StdAddressBook::self( true );
00432     connect( mAddressBook, SIGNAL( addressBookChanged( AddressBook * ) ),
00433              this, SLOT( insertAddressBook( AddressBook * ) ) );
00434   }
00435 
00436   initCollections();
00437 
00438   mCollectionCombo->setCurrentItem( 0 );
00439 
00440   updateList();
00441 
00442   mSearchLine->setFocus();
00443 
00444   readConfig();
00445 
00446   setTabOrder( mCollectionCombo, mSearchLine );
00447   setTabOrder( mSearchLine, mRecipientList );
00448   setTabOrder( closeButton, mCollectionCombo );
00449 }
00450 
00451 RecipientsPicker::~RecipientsPicker()
00452 {
00453   writeConfig();
00454 
00455 #ifndef KDEPIM_NEW_DISTRLISTS
00456   delete mDistributionListManager;
00457 #endif
00458 
00459   QMap<int,RecipientsCollection *>::ConstIterator it;
00460   for( it = mCollectionMap.begin(); it != mCollectionMap.end(); ++it ) {
00461     delete *it;
00462   }
00463 }
00464 
00465 void RecipientsPicker::initCollections()
00466 {
00467   mAllRecipients = new RecipientsCollection( i18n("All") );
00468   mAllRecipients->setReferenceContainer( true );
00469   mDistributionLists = new RecipientsCollection( i18n("Distribution Lists") );
00470   mSelectedRecipients = new RecipientsCollection( i18n("Selected Recipients") );
00471 
00472   insertCollection( mAllRecipients );
00473   insertAddressBook( mAddressBook );
00474   insertCollection( mDistributionLists );
00475   insertRecentAddresses();
00476   insertCollection( mSelectedRecipients );
00477 
00478   rebuildAllRecipientsList();
00479 }
00480 
00481 void RecipientsPicker::insertAddressBook( KABC::AddressBook *addressbook )
00482 {
00483   QMap<KABC::Resource *,RecipientsCollection *> collectionMap;
00484 
00485   QPtrList<KABC::Resource> resources = addressbook->resources();
00486   KABC::Resource *res;
00487   for( res = resources.first(); res; res = resources.next() ) {
00488     RecipientsCollection *collection = new RecipientsCollection( res->identifier() );
00489     collectionMap.insert( res, collection );
00490     collection->setTitle( res->resourceName() );
00491   }
00492 
00493   QMap<QString,RecipientsCollection *> categoryMap;
00494 
00495   KABC::AddressBook::Iterator it;
00496   for( it = addressbook->begin(); it != addressbook->end(); ++it ) {
00497     QStringList emails = (*it).emails();
00498     QStringList::ConstIterator it3;
00499     for( it3 = emails.begin(); it3 != emails.end(); ++it3 ) {
00500 #ifdef KDEPIM_NEW_DISTRLISTS
00501       RecipientItem *item = new RecipientItem( mAddressBook );
00502 #else
00503       RecipientItem *item = new RecipientItem;
00504 #endif
00505       item->setAddressee( *it, *it3 );
00506 
00507       QMap<KABC::Resource *,RecipientsCollection *>::ConstIterator collIt;
00508       collIt = collectionMap.find( it->resource() );
00509       if ( collIt != collectionMap.end() ) {
00510         (*collIt)->addItem( item );
00511       }
00512 
00513       QStringList categories = (*it).categories();
00514       QStringList::ConstIterator catIt;
00515       for( catIt = categories.begin(); catIt != categories.end(); ++catIt ) {
00516         QMap<QString, RecipientsCollection *>::ConstIterator catMapIt;
00517         catMapIt = categoryMap.find( *catIt );
00518         RecipientsCollection *collection;
00519         if ( catMapIt == categoryMap.end() ) {
00520           collection = new RecipientsCollection( *catIt );
00521           collection->setReferenceContainer( true );
00522           categoryMap.insert( *catIt, collection );
00523         } else {
00524           collection = *catMapIt;
00525         }
00526         collection->addItem( item );
00527       }
00528     }
00529   }
00530 
00531   QMap<KABC::Resource *,RecipientsCollection *>::ConstIterator it2;
00532   for( it2 = collectionMap.begin(); it2 != collectionMap.end(); ++it2 ) {
00533     insertCollection( *it2 );
00534   }
00535 
00536   QMap<QString, RecipientsCollection *>::ConstIterator it3;
00537   for( it3 = categoryMap.begin(); it3 != categoryMap.end(); ++it3 ) {
00538     insertCollection( *it3 );
00539   }
00540 
00541   insertDistributionLists();
00542   rebuildAllRecipientsList();
00543   updateList();
00544 }
00545 
00546 void RecipientsPicker::insertDistributionLists()
00547 {
00548   mDistributionLists->deleteAll();
00549 
00550 #ifdef KDEPIM_NEW_DISTRLISTS
00551   QValueList<KPIM::DistributionList> lists = KPIM::DistributionList::allDistributionLists( mAddressBook );
00552   for ( uint i = 0; i < lists.count(); ++i ) {
00553     RecipientItem *item = new RecipientItem( mAddressBook );
00554     item->setDistributionList( lists[ i ] );
00555     mDistributionLists->addItem( item );
00556   }
00557 #else
00558   delete mDistributionListManager;
00559   mDistributionListManager =
00560     new KABC::DistributionListManager( KABC::StdAddressBook::self( true ) );
00561 
00562   mDistributionListManager->load();
00563 
00564   QStringList lists = mDistributionListManager->listNames();
00565 
00566   QStringList::Iterator listIt;
00567   for ( listIt = lists.begin(); listIt != lists.end(); ++listIt ) {
00568     KABC::DistributionList *list = mDistributionListManager->list( *listIt );
00569     RecipientItem *item = new RecipientItem;
00570     item->setDistributionList( list );
00571     mDistributionLists->addItem( item );
00572   }
00573 #endif
00574 }
00575 
00576 void RecipientsPicker::insertRecentAddresses()
00577 {
00578   RecipientsCollection *collection = new RecipientsCollection( i18n("Recent Addresses") );
00579 
00580   KConfig config( "kmailrc" );
00581   KABC::Addressee::List recents =
00582     KRecentAddress::RecentAddresses::self( &config )->kabcAddresses();
00583 
00584   KABC::Addressee::List::ConstIterator it;
00585   for( it = recents.begin(); it != recents.end(); ++it ) {
00586 #ifdef KDEPIM_NEW_DISTRLISTS
00587     RecipientItem *item = new RecipientItem( mAddressBook );
00588 #else
00589     RecipientItem *item = new RecipientItem;
00590 #endif
00591     item->setAddressee( *it, (*it).preferredEmail() );
00592     collection->addItem( item );
00593   }
00594 
00595   insertCollection( collection );
00596 }
00597 
00598 void RecipientsPicker::insertCollection( RecipientsCollection *coll )
00599 {
00600   int index = 0;
00601   QMap<int,RecipientsCollection *>::ConstIterator it;
00602   for ( it = mCollectionMap.begin(); it != mCollectionMap.end(); ++it ) {
00603     if ( (*it)->id() == coll->id() ) {
00604       delete *it;
00605       mCollectionMap.remove( index );
00606       mCollectionMap.insert( index, coll );
00607       return;
00608     }
00609     index++;
00610   }
00611 
00612   mCollectionCombo->insertItem( coll->title(), index );
00613   mCollectionMap.insert( index, coll );
00614 }
00615 
00616 void RecipientsPicker::updateRecipient( const Recipient &recipient )
00617 {
00618   RecipientItem::List allRecipients = mAllRecipients->items();
00619   RecipientItem::List::ConstIterator itAll;
00620   for( itAll = allRecipients.begin(); itAll != allRecipients.end(); ++itAll ) {
00621     if ( (*itAll)->recipient() == recipient.email() ) {
00622       (*itAll)->setRecipientType( recipient.typeLabel() );
00623     }
00624   }
00625   updateList();
00626 }
00627 
00628 void RecipientsPicker::setRecipients( const Recipient::List &recipients )
00629 {
00630   mSelectedRecipients->deleteAll();
00631 
00632   Recipient::List::ConstIterator it;
00633   for( it = recipients.begin(); it != recipients.end(); ++it ) {
00634     RecipientItem *item = 0;
00635 
00636     // if recipient is a distribution list, create
00637     // a detached copy.
00638     RecipientItem::List items = mDistributionLists->items();
00639     RecipientItem::List::ConstIterator distIt;
00640 #ifdef KDEPIM_NEW_DISTRLISTS
00641     for ( distIt = items.begin(); distIt != items.end(); ++distIt ) {
00642       if ( (*it).email() == (*distIt)->name() ) {
00643         item = new RecipientItem( mAddressBook );
00644         item->setDistributionList( (*distIt)->distributionList() );
00645       }
00646     }
00647 #else
00648     for ( distIt = items.begin(); distIt != items.end(); ++distIt ) {
00649       if ( (*it).email() == (*distIt)->name() ) {
00650         item = new RecipientItem();
00651         item->setDistributionList( (*distIt)->distributionList() );
00652       }
00653     }
00654 #endif
00655 
00656     if ( !item ) {
00657       KABC::Addressee a;
00658       QString name;
00659       QString email;
00660       KABC::Addressee::parseEmailAddress( (*it).email(), name, email );
00661       a.setNameFromString( name );
00662       a.insertEmail( email );
00663 
00664 #ifdef KDEPIM_NEW_DISTRLISTS
00665       item = new RecipientItem( mAddressBook );
00666 #else
00667       item = new RecipientItem;
00668 #endif
00669       item->setAddressee( a, a.preferredEmail() );
00670     }
00671 
00672     item->setRecipientType( (*it).typeLabel() );
00673     mSelectedRecipients->addItem( item );
00674   }
00675 
00676   updateList();
00677 }
00678 
00679 void RecipientsPicker::setDefaultButton( QPushButton *button )
00680 {
00681 //  button->setText( "<qt><b>" + button->text() + "</b></qt>" );
00682   button->setDefault( true );
00683 }
00684 
00685 void RecipientsPicker::setDefaultType( Recipient::Type type )
00686 {
00687   mDefaultType = type;
00688 
00689   if ( type == Recipient::To ) {
00690     setDefaultButton( mToButton );
00691   } else if ( type == Recipient::Cc ) {
00692     setDefaultButton( mCcButton );
00693   } else if ( type == Recipient::Bcc ) {
00694     setDefaultButton( mBccButton );
00695   }
00696 }
00697 
00698 void RecipientsPicker::rebuildAllRecipientsList()
00699 {
00700   mAllRecipients->clear();
00701 
00702   QMap<int,RecipientsCollection *>::ConstIterator it;
00703   for( it = mCollectionMap.begin(); it != mCollectionMap.end(); ++it ) {
00704     // skip self
00705     if ( (*it) == mAllRecipients )
00706       continue;
00707 
00708     RecipientItem::List coll = (*it)->items();
00709 
00710     RecipientItem::List::ConstIterator rcptIt;
00711     for ( rcptIt = coll.begin(); rcptIt != coll.end(); ++rcptIt ) {
00712       mAllRecipients->addItem( *rcptIt );
00713     }
00714   }
00715 }
00716 
00717 void RecipientsPicker::updateList()
00718 {
00719   mRecipientList->clear();
00720 
00721   RecipientsCollection *coll = mCollectionMap[ mCollectionCombo->currentItem() ];
00722 
00723   RecipientItem::List items = coll->items();
00724   RecipientItem::List::ConstIterator it;
00725   for( it = items.begin(); it != items.end(); ++it ) {
00726     if ( coll != mSelectedRecipients ) {
00727       RecipientItem *selItem = mSelectedRecipients->getEquivalentItem( *it );
00728       if ( selItem ) {
00729         (*it)->setRecipientType( selItem->recipientType() );
00730       } else {
00731         (*it)->setRecipientType( QString() );
00732       }
00733     }
00734     new RecipientViewItem( *it, mRecipientList );
00735   }
00736 
00737   mSearchLine->updateSearch();
00738 }
00739 
00740 void RecipientsPicker::slotToClicked()
00741 {
00742   pick( Recipient::To );
00743 }
00744 
00745 void RecipientsPicker::slotCcClicked()
00746 {
00747   pick( Recipient::Cc );
00748 }
00749 
00750 void RecipientsPicker::slotBccClicked()
00751 {
00752   pick( Recipient::Bcc );
00753 }
00754 
00755 void RecipientsPicker::slotPicked( QListViewItem *viewItem )
00756 {
00757   RecipientViewItem *item = static_cast<RecipientViewItem *>( viewItem );
00758   if ( item ) {
00759     RecipientItem *i = item->recipientItem();
00760     emit pickedRecipient( Recipient( i->recipient(), Recipient::Undefined ) );
00761   }
00762   close();
00763 }
00764 
00765 void RecipientsPicker::slotPicked()
00766 {
00767   pick( mDefaultType );
00768 }
00769 
00770 void RecipientsPicker::pick( Recipient::Type type )
00771 {
00772   kdDebug() << "RecipientsPicker::pick " << int( type ) << endl;
00773 
00774   int count = 0;
00775   QListViewItemIterator it( mRecipientList , 
00776             QListViewItemIterator::Visible | QListViewItemIterator::Selected );
00777   for ( ; it.current(); ++it )
00778       ++count;
00779 
00780   if ( count > GlobalSettings::self()->maximumRecipients() ) {
00781     KMessageBox::sorry( this,
00782         i18n("You selected 1 recipient. The maximum supported number of "
00783              "recipients is %1. Please adapt the selection.",
00784              "You selected %n recipients. The maximum supported number of "
00785              "recipients is %1. Please adapt the selection.", count)
00786       .arg( GlobalSettings::self()->maximumRecipients() ) );
00787     return;
00788   }
00789 
00790   it = QListViewItemIterator( mRecipientList , 
00791             QListViewItemIterator::Visible | QListViewItemIterator::Selected );
00792   for ( ; it.current(); ++it ) {
00793     RecipientViewItem *item = static_cast<RecipientViewItem *>( it.current() );
00794     if ( item ) {
00795       RecipientItem *i = item->recipientItem();
00796       Recipient r = i->recipient();
00797       r.setType( type );
00798       emit pickedRecipient( r );
00799     }
00800   }
00801   close();
00802 }
00803 
00804 void RecipientsPicker::keyPressEvent( QKeyEvent *ev )
00805 {
00806   if ( ev->key() == Key_Escape ) close();
00807 
00808   QWidget::keyPressEvent( ev );
00809 }
00810 
00811 void RecipientsPicker::readConfig()
00812 {
00813   KConfig *cfg = KGlobal::config();
00814   cfg->setGroup( "RecipientsPicker" );
00815   QSize size = cfg->readSizeEntry( "Size" );
00816   if ( !size.isEmpty() ) {
00817     resize( size );
00818   }
00819   int currentCollection = cfg->readNumEntry( "CurrentCollection", -1 );
00820   if ( currentCollection >= 0 &&
00821        currentCollection < mCollectionCombo->count() ) {
00822     mCollectionCombo->setCurrentItem( currentCollection );
00823   }
00824 }
00825 
00826 void RecipientsPicker::writeConfig()
00827 {
00828   KConfig *cfg = KGlobal::config();
00829   cfg->setGroup( "RecipientsPicker" );
00830   cfg->writeEntry( "Size", size() );
00831   cfg->writeEntry( "CurrentCollection", mCollectionCombo->currentItem() );
00832 }
00833 
00834 void RecipientsPicker::setFocusList()
00835 {
00836   mRecipientList->setFocus();
00837 }
00838 
00839 void RecipientsPicker::resetSearch()
00840 {
00841   mSearchLine->setText( QString::null );
00842 }
00843 
00844 void RecipientsPicker::slotSearchLDAP()
00845 {
00846     if ( !mLdapSearchDialog ) {
00847         mLdapSearchDialog = new KPIM::LDAPSearchDialog( this );
00848         connect( mLdapSearchDialog, SIGNAL( addresseesAdded() ),
00849                  SLOT(ldapSearchResult() ) );
00850     }
00851     mLdapSearchDialog->setSearchText( mSearchLine->text() );
00852     mLdapSearchDialog->show();
00853 
00854 }
00855 
00856 void RecipientsPicker::ldapSearchResult()
00857 {
00858     QStringList emails = QStringList::split(',', mLdapSearchDialog->selectedEMails() );
00859     QStringList::iterator it( emails.begin() );
00860     QStringList::iterator end( emails.end() );
00861     for ( ; it != end; ++it ){
00862         QString name;
00863         QString email;
00864         KPIM::getNameAndMail( (*it), name, email );
00865         KABC::Addressee ad;
00866         ad.setNameFromString( name );
00867         ad.insertEmail( email );
00868 #ifdef KDEPIM_NEW_DISTRLISTS
00869         RecipientItem *item = new RecipientItem( mAddressBook );
00870 #else
00871         RecipientItem *item = new RecipientItem;
00872 #endif
00873         item->setAddressee( ad, ad.preferredEmail() );
00874         emit pickedRecipient( Recipient( item->recipient(), Recipient::Undefined ) );
00875     }
00876 }
00877 
00878 #include "recipientspicker.moc"