TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

recipientspicker.cpp 24KB

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