summaryrefslogtreecommitdiffstats
path: root/libtdepim/addresseeemailselection.cpp
blob: 11e9af2c6b4b501540fced9cc940fc8260fad7ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
    This file is part of libtdepim.

    Copyright (c) 2004 Tobias Koenig <[email protected]>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelocale.h>

#include "recentaddresses.h"

#include "addresseeemailselection.h"

using namespace KPIM;
using TDERecentAddress::RecentAddresses;

AddresseeEmailSelection::AddresseeEmailSelection()
  : Selection()
{
}

uint AddresseeEmailSelection::fieldCount() const
{
  return 3;
}

TQString AddresseeEmailSelection::fieldTitle( uint index ) const
{
  switch ( index ) {
    case 0:
      return i18n( "To" );
      break;
    case 1:
      return i18n( "Cc" );
      break;
    case 2:
      return i18n( "Bcc" );
      break;
    default:
      return TQString();
  }
}

TQStringList AddresseeEmailSelection::to() const
{
  return mToEmailList;
}

TQStringList AddresseeEmailSelection::cc() const
{
  return mCcEmailList;
}

TQStringList AddresseeEmailSelection::bcc() const
{
  return mBccEmailList;
}

KABC::Addressee::List AddresseeEmailSelection::toAddresses() const
{
  return mToAddresseeList;
}

KABC::Addressee::List AddresseeEmailSelection::ccAddresses() const
{
  return mCcAddresseeList;
}

KABC::Addressee::List AddresseeEmailSelection::bccAddresses() const
{
  return mBccAddresseeList;
}

TQStringList AddresseeEmailSelection::toDistributionLists() const
{
  return mToDistributionList;
}

TQStringList AddresseeEmailSelection::ccDistributionLists() const
{
  return mCcDistributionList;
}

TQStringList AddresseeEmailSelection::bccDistributionLists() const
{
  return mBccDistributionList;
}

void AddresseeEmailSelection::setSelectedTo( const TQStringList &emails )
{
  setSelectedItem( 0, emails );
}

void AddresseeEmailSelection::setSelectedCC( const TQStringList &emails )
{
  setSelectedItem( 1, emails );
}

void AddresseeEmailSelection::setSelectedBCC( const TQStringList &emails )
{
  setSelectedItem( 2, emails );
}


uint AddresseeEmailSelection::itemCount( const KABC::Addressee &addressee ) const
{
  return addressee.emails().count();
}

TQString AddresseeEmailSelection::itemText( const KABC::Addressee &addressee, uint index ) const
{
  return addressee.formattedName() + " " + email( addressee, index );
}

TQPixmap AddresseeEmailSelection::itemIcon( const KABC::Addressee &addressee, uint ) const
{
  if ( !addressee.photo().data().isNull() )
    return addressee.photo().data().smoothScale( 16, 16 );
  else
    return TDEGlobal::iconLoader()->loadIcon( "personal", TDEIcon::Small );
}

bool AddresseeEmailSelection::itemEnabled( const KABC::Addressee &addressee, uint ) const
{
  return addressee.emails().count() != 0;
}

bool AddresseeEmailSelection::itemMatches( const KABC::Addressee &addressee, uint index, const TQString &pattern ) const
{
  return addressee.formattedName().startsWith( pattern, false ) ||
         email( addressee, index ).startsWith( pattern, false );
}

bool AddresseeEmailSelection::itemEquals( const KABC::Addressee &addressee, uint index, const TQString &pattern ) const
{
  return (pattern == addressee.formattedName() + " " + email( addressee, index )) ||
         (addressee.emails().contains( pattern ));
}

TQString AddresseeEmailSelection::distributionListText( const KABC::DistributionList *distributionList ) const
{
  return distributionList->name();
}

TQPixmap AddresseeEmailSelection::distributionListIcon( const KABC::DistributionList* ) const
{
  return TDEGlobal::iconLoader()->loadIcon( "tdmconfig", TDEIcon::Small );
}

bool AddresseeEmailSelection::distributionListEnabled( const KABC::DistributionList* ) const
{
  return true;
}

bool AddresseeEmailSelection::distributionListMatches( const KABC::DistributionList *distributionList,
                                                       const TQString &pattern ) const
{
  // check whether the name of the distribution list matches the pattern or one of its entries.
  bool ok = distributionList->name().startsWith( pattern, false );

  KABC::DistributionList::Entry::List entries = distributionList->entries();
  KABC::DistributionList::Entry::List::ConstIterator it;
  for ( it = entries.begin(); it != entries.end(); ++it ) {
    ok = ok || (*it).addressee.formattedName().startsWith( pattern, false ) ||
               (*it).email.startsWith( pattern, false );
  }

  return ok;
}

uint AddresseeEmailSelection::addressBookCount() const
{
  // we provide the recent email addresses via the custom addressbooks
  return 1;
}

TQString AddresseeEmailSelection::addressBookTitle( uint index ) const
{
  if ( index == 0 )
    return i18n( "Recent Addresses" );
  else
    return TQString();
}

KABC::Addressee::List AddresseeEmailSelection::addressBookContent( uint index ) const
{
  if ( index == 0 ) {
    TDEConfig config( "kmailrc" );
    return RecentAddresses::self( &config )->kabcAddresses();
  } else {
    return KABC::Addressee::List();
  }
}

TQString AddresseeEmailSelection::email( const KABC::Addressee &addressee, uint index ) const
{
  return addressee.emails()[ index ];
}

void AddresseeEmailSelection::setSelectedItem( uint fieldIndex, const TQStringList &emails )
{
  TQStringList::ConstIterator it;
  for ( it = emails.begin(); it != emails.end(); ++it ) {
    KABC::Addressee addr;
    addr.insertEmail( *it, true );

    selector()->setItemSelected( fieldIndex, addr, 0, *it );
  }
}

void AddresseeEmailSelection::addSelectedAddressees( uint fieldIndex, const KABC::Addressee &addressee, uint itemIndex )
{
  switch ( fieldIndex ) {
    case 0:
      mToAddresseeList.append( addressee );
      mToEmailList.append( email( addressee, itemIndex ) );
      break;
    case 1:
      mCcAddresseeList.append( addressee );
      mCcEmailList.append( email( addressee, itemIndex ) );
      break;
    case 2:
      mBccAddresseeList.append( addressee );
      mBccEmailList.append( email( addressee, itemIndex ) );
      break;
    default:
      // oops
      break;
  }
}

void AddresseeEmailSelection::addSelectedDistributionList( uint fieldIndex, const KABC::DistributionList *list )
{
  switch ( fieldIndex ) {
    case 0:
      mToDistributionList.append( list->name() );
      break;
    case 1:
      mCcDistributionList.append( list->name() );
      break;
    case 2:
      mBccDistributionList.append( list->name() );
      break;
    default:
      // oops
      break;
  }
}