libtdepim

addresseeemailselection.cpp
1 /*
2  This file is part of libtdepim.
3 
4  Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <tdeglobal.h>
23 #include <kiconloader.h>
24 #include <tdelocale.h>
25 
26 #include "recentaddresses.h"
27 
28 #include "addresseeemailselection.h"
29 
30 using namespace KPIM;
32 
33 AddresseeEmailSelection::AddresseeEmailSelection()
34  : Selection()
35 {
36 }
37 
38 uint AddresseeEmailSelection::fieldCount() const
39 {
40  return 3;
41 }
42 
43 TQString AddresseeEmailSelection::fieldTitle( uint index ) const
44 {
45  switch ( index ) {
46  case 0:
47  return i18n( "To" );
48  break;
49  case 1:
50  return i18n( "Cc" );
51  break;
52  case 2:
53  return i18n( "Bcc" );
54  break;
55  default:
56  return TQString();
57  }
58 }
59 
60 TQStringList AddresseeEmailSelection::to() const
61 {
62  return mToEmailList;
63 }
64 
65 TQStringList AddresseeEmailSelection::cc() const
66 {
67  return mCcEmailList;
68 }
69 
70 TQStringList AddresseeEmailSelection::bcc() const
71 {
72  return mBccEmailList;
73 }
74 
75 TDEABC::Addressee::List AddresseeEmailSelection::toAddresses() const
76 {
77  return mToAddresseeList;
78 }
79 
80 TDEABC::Addressee::List AddresseeEmailSelection::ccAddresses() const
81 {
82  return mCcAddresseeList;
83 }
84 
85 TDEABC::Addressee::List AddresseeEmailSelection::bccAddresses() const
86 {
87  return mBccAddresseeList;
88 }
89 
90 TQStringList AddresseeEmailSelection::toDistributionLists() const
91 {
92  return mToDistributionList;
93 }
94 
95 TQStringList AddresseeEmailSelection::ccDistributionLists() const
96 {
97  return mCcDistributionList;
98 }
99 
100 TQStringList AddresseeEmailSelection::bccDistributionLists() const
101 {
102  return mBccDistributionList;
103 }
104 
105 void AddresseeEmailSelection::setSelectedTo( const TQStringList &emails )
106 {
107  setSelectedItem( 0, emails );
108 }
109 
110 void AddresseeEmailSelection::setSelectedCC( const TQStringList &emails )
111 {
112  setSelectedItem( 1, emails );
113 }
114 
115 void AddresseeEmailSelection::setSelectedBCC( const TQStringList &emails )
116 {
117  setSelectedItem( 2, emails );
118 }
119 
120 
121 uint AddresseeEmailSelection::itemCount( const TDEABC::Addressee &addressee ) const
122 {
123  return addressee.emails().count();
124 }
125 
126 TQString AddresseeEmailSelection::itemText( const TDEABC::Addressee &addressee, uint index ) const
127 {
128  return addressee.formattedName() + " " + email( addressee, index );
129 }
130 
131 TQPixmap AddresseeEmailSelection::itemIcon( const TDEABC::Addressee &addressee, uint ) const
132 {
133  if ( !addressee.photo().data().isNull() )
134  return addressee.photo().data().smoothScale( 16, 16 );
135  else
136  return TDEGlobal::iconLoader()->loadIcon( "preferences-desktop-personal", TDEIcon::Small );
137 }
138 
139 bool AddresseeEmailSelection::itemEnabled( const TDEABC::Addressee &addressee, uint ) const
140 {
141  return addressee.emails().count() != 0;
142 }
143 
144 bool AddresseeEmailSelection::itemMatches( const TDEABC::Addressee &addressee, uint index, const TQString &pattern ) const
145 {
146  return addressee.formattedName().startsWith( pattern, false ) ||
147  email( addressee, index ).startsWith( pattern, false );
148 }
149 
150 bool AddresseeEmailSelection::itemEquals( const TDEABC::Addressee &addressee, uint index, const TQString &pattern ) const
151 {
152  return (pattern == addressee.formattedName() + " " + email( addressee, index )) ||
153  (addressee.emails().contains( pattern ));
154 }
155 
156 TQString AddresseeEmailSelection::distributionListText( const TDEABC::DistributionList *distributionList ) const
157 {
158  return distributionList->name();
159 }
160 
161 TQPixmap AddresseeEmailSelection::distributionListIcon( const TDEABC::DistributionList* ) const
162 {
163  return TDEGlobal::iconLoader()->loadIcon( "tdmconfig", TDEIcon::Small );
164 }
165 
166 bool AddresseeEmailSelection::distributionListEnabled( const TDEABC::DistributionList* ) const
167 {
168  return true;
169 }
170 
171 bool AddresseeEmailSelection::distributionListMatches( const TDEABC::DistributionList *distributionList,
172  const TQString &pattern ) const
173 {
174  // check whether the name of the distribution list matches the pattern or one of its entries.
175  bool ok = distributionList->name().startsWith( pattern, false );
176 
177  TDEABC::DistributionList::Entry::List entries = distributionList->entries();
178  TDEABC::DistributionList::Entry::List::ConstIterator it;
179  for ( it = entries.begin(); it != entries.end(); ++it ) {
180  ok = ok || (*it).addressee.formattedName().startsWith( pattern, false ) ||
181  (*it).email.startsWith( pattern, false );
182  }
183 
184  return ok;
185 }
186 
187 uint AddresseeEmailSelection::addressBookCount() const
188 {
189  // we provide the recent email addresses via the custom addressbooks
190  return 1;
191 }
192 
193 TQString AddresseeEmailSelection::addressBookTitle( uint index ) const
194 {
195  if ( index == 0 )
196  return i18n( "Recent Addresses" );
197  else
198  return TQString();
199 }
200 
201 TDEABC::Addressee::List AddresseeEmailSelection::addressBookContent( uint index ) const
202 {
203  if ( index == 0 ) {
204  TDEConfig config( "kmailrc" );
205  return RecentAddresses::self( &config )->tdeabcAddresses();
206  } else {
207  return TDEABC::Addressee::List();
208  }
209 }
210 
211 TQString AddresseeEmailSelection::email( const TDEABC::Addressee &addressee, uint index ) const
212 {
213  return addressee.emails()[ index ];
214 }
215 
216 void AddresseeEmailSelection::setSelectedItem( uint fieldIndex, const TQStringList &emails )
217 {
218  TQStringList::ConstIterator it;
219  for ( it = emails.begin(); it != emails.end(); ++it ) {
220  TDEABC::Addressee addr;
221  addr.insertEmail( *it, true );
222 
223  selector()->setItemSelected( fieldIndex, addr, 0, *it );
224  }
225 }
226 
227 void AddresseeEmailSelection::addSelectedAddressees( uint fieldIndex, const TDEABC::Addressee &addressee, uint itemIndex )
228 {
229  switch ( fieldIndex ) {
230  case 0:
231  mToAddresseeList.append( addressee );
232  mToEmailList.append( email( addressee, itemIndex ) );
233  break;
234  case 1:
235  mCcAddresseeList.append( addressee );
236  mCcEmailList.append( email( addressee, itemIndex ) );
237  break;
238  case 2:
239  mBccAddresseeList.append( addressee );
240  mBccEmailList.append( email( addressee, itemIndex ) );
241  break;
242  default:
243  // oops
244  break;
245  }
246 }
247 
248 void AddresseeEmailSelection::addSelectedDistributionList( uint fieldIndex, const TDEABC::DistributionList *list )
249 {
250  switch ( fieldIndex ) {
251  case 0:
252  mToDistributionList.append( list->name() );
253  break;
254  case 1:
255  mCcDistributionList.append( list->name() );
256  break;
257  case 2:
258  mBccDistributionList.append( list->name() );
259  break;
260  default:
261  // oops
262  break;
263  }
264 }
Handles a list of "recent email-addresses".
TDEPIM classes for drag and drop of mails.