kmail

recipientspicker.cpp

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