addresseeemailselection.cpp
00001 /* 00002 This file is part of libkdepim. 00003 00004 Copyright (c) 2004 Tobias Koenig <tokoe@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 <kglobal.h> 00023 #include <kiconloader.h> 00024 #include <klocale.h> 00025 00026 #include "recentaddresses.h" 00027 00028 #include "addresseeemailselection.h" 00029 00030 using namespace KPIM; 00031 using KRecentAddress::RecentAddresses; 00032 00033 AddresseeEmailSelection::AddresseeEmailSelection() 00034 : Selection() 00035 { 00036 } 00037 00038 uint AddresseeEmailSelection::fieldCount() const 00039 { 00040 return 3; 00041 } 00042 00043 TQString AddresseeEmailSelection::fieldTitle( uint index ) const 00044 { 00045 switch ( index ) { 00046 case 0: 00047 return i18n( "To" ); 00048 break; 00049 case 1: 00050 return i18n( "Cc" ); 00051 break; 00052 case 2: 00053 return i18n( "Bcc" ); 00054 break; 00055 default: 00056 return TQString(); 00057 } 00058 } 00059 00060 TQStringList AddresseeEmailSelection::to() const 00061 { 00062 return mToEmailList; 00063 } 00064 00065 TQStringList AddresseeEmailSelection::cc() const 00066 { 00067 return mCcEmailList; 00068 } 00069 00070 TQStringList AddresseeEmailSelection::bcc() const 00071 { 00072 return mBccEmailList; 00073 } 00074 00075 KABC::Addressee::List AddresseeEmailSelection::toAddresses() const 00076 { 00077 return mToAddresseeList; 00078 } 00079 00080 KABC::Addressee::List AddresseeEmailSelection::ccAddresses() const 00081 { 00082 return mCcAddresseeList; 00083 } 00084 00085 KABC::Addressee::List AddresseeEmailSelection::bccAddresses() const 00086 { 00087 return mBccAddresseeList; 00088 } 00089 00090 TQStringList AddresseeEmailSelection::toDistributionLists() const 00091 { 00092 return mToDistributionList; 00093 } 00094 00095 TQStringList AddresseeEmailSelection::ccDistributionLists() const 00096 { 00097 return mCcDistributionList; 00098 } 00099 00100 TQStringList AddresseeEmailSelection::bccDistributionLists() const 00101 { 00102 return mBccDistributionList; 00103 } 00104 00105 void AddresseeEmailSelection::setSelectedTo( const TQStringList &emails ) 00106 { 00107 setSelectedItem( 0, emails ); 00108 } 00109 00110 void AddresseeEmailSelection::setSelectedCC( const TQStringList &emails ) 00111 { 00112 setSelectedItem( 1, emails ); 00113 } 00114 00115 void AddresseeEmailSelection::setSelectedBCC( const TQStringList &emails ) 00116 { 00117 setSelectedItem( 2, emails ); 00118 } 00119 00120 00121 uint AddresseeEmailSelection::itemCount( const KABC::Addressee &addressee ) const 00122 { 00123 return addressee.emails().count(); 00124 } 00125 00126 TQString AddresseeEmailSelection::itemText( const KABC::Addressee &addressee, uint index ) const 00127 { 00128 return addressee.formattedName() + " " + email( addressee, index ); 00129 } 00130 00131 TQPixmap AddresseeEmailSelection::itemIcon( const KABC::Addressee &addressee, uint ) const 00132 { 00133 if ( !addressee.photo().data().isNull() ) 00134 return addressee.photo().data().smoothScale( 16, 16 ); 00135 else 00136 return KGlobal::iconLoader()->loadIcon( "personal", KIcon::Small ); 00137 } 00138 00139 bool AddresseeEmailSelection::itemEnabled( const KABC::Addressee &addressee, uint ) const 00140 { 00141 return addressee.emails().count() != 0; 00142 } 00143 00144 bool AddresseeEmailSelection::itemMatches( const KABC::Addressee &addressee, uint index, const TQString &pattern ) const 00145 { 00146 return addressee.formattedName().startsWith( pattern, false ) || 00147 email( addressee, index ).startsWith( pattern, false ); 00148 } 00149 00150 bool AddresseeEmailSelection::itemEquals( const KABC::Addressee &addressee, uint index, const TQString &pattern ) const 00151 { 00152 return (pattern == addressee.formattedName() + " " + email( addressee, index )) || 00153 (addressee.emails().contains( pattern )); 00154 } 00155 00156 TQString AddresseeEmailSelection::distributionListText( const KABC::DistributionList *distributionList ) const 00157 { 00158 return distributionList->name(); 00159 } 00160 00161 TQPixmap AddresseeEmailSelection::distributionListIcon( const KABC::DistributionList* ) const 00162 { 00163 return KGlobal::iconLoader()->loadIcon( "kdmconfig", KIcon::Small ); 00164 } 00165 00166 bool AddresseeEmailSelection::distributionListEnabled( const KABC::DistributionList* ) const 00167 { 00168 return true; 00169 } 00170 00171 bool AddresseeEmailSelection::distributionListMatches( const KABC::DistributionList *distributionList, 00172 const TQString &pattern ) const 00173 { 00174 // check whether the name of the distribution list matches the pattern or one of its entries. 00175 bool ok = distributionList->name().startsWith( pattern, false ); 00176 00177 KABC::DistributionList::Entry::List entries = distributionList->entries(); 00178 KABC::DistributionList::Entry::List::ConstIterator it; 00179 for ( it = entries.begin(); it != entries.end(); ++it ) { 00180 ok = ok || (*it).addressee.formattedName().startsWith( pattern, false ) || 00181 (*it).email.startsWith( pattern, false ); 00182 } 00183 00184 return ok; 00185 } 00186 00187 uint AddresseeEmailSelection::addressBookCount() const 00188 { 00189 // we provide the recent email addresses via the custom addressbooks 00190 return 1; 00191 } 00192 00193 TQString AddresseeEmailSelection::addressBookTitle( uint index ) const 00194 { 00195 if ( index == 0 ) 00196 return i18n( "Recent Addresses" ); 00197 else 00198 return TQString(); 00199 } 00200 00201 KABC::Addressee::List AddresseeEmailSelection::addressBookContent( uint index ) const 00202 { 00203 if ( index == 0 ) { 00204 KConfig config( "kmailrc" ); 00205 return RecentAddresses::self( &config )->kabcAddresses(); 00206 } else { 00207 return KABC::Addressee::List(); 00208 } 00209 } 00210 00211 TQString AddresseeEmailSelection::email( const KABC::Addressee &addressee, uint index ) const 00212 { 00213 return addressee.emails()[ index ]; 00214 } 00215 00216 void AddresseeEmailSelection::setSelectedItem( uint fieldIndex, const TQStringList &emails ) 00217 { 00218 TQStringList::ConstIterator it; 00219 for ( it = emails.begin(); it != emails.end(); ++it ) { 00220 KABC::Addressee addr; 00221 addr.insertEmail( *it, true ); 00222 00223 selector()->setItemSelected( fieldIndex, addr, 0, *it ); 00224 } 00225 } 00226 00227 void AddresseeEmailSelection::addSelectedAddressees( uint fieldIndex, const KABC::Addressee &addressee, uint itemIndex ) 00228 { 00229 switch ( fieldIndex ) { 00230 case 0: 00231 mToAddresseeList.append( addressee ); 00232 mToEmailList.append( email( addressee, itemIndex ) ); 00233 break; 00234 case 1: 00235 mCcAddresseeList.append( addressee ); 00236 mCcEmailList.append( email( addressee, itemIndex ) ); 00237 break; 00238 case 2: 00239 mBccAddresseeList.append( addressee ); 00240 mBccEmailList.append( email( addressee, itemIndex ) ); 00241 break; 00242 default: 00243 // oops 00244 break; 00245 } 00246 } 00247 00248 void AddresseeEmailSelection::addSelectedDistributionList( uint fieldIndex, const KABC::DistributionList *list ) 00249 { 00250 switch ( fieldIndex ) { 00251 case 0: 00252 mToDistributionList.append( list->name() ); 00253 break; 00254 case 1: 00255 mCcDistributionList.append( list->name() ); 00256 break; 00257 case 2: 00258 mBccDistributionList.append( list->name() ); 00259 break; 00260 default: 00261 // oops 00262 break; 00263 } 00264 }