Make it possible to use a distinct selection model in the foldertreewidget.
[kdepim.git] / kaddressbook / contactfields.cpp
blob4c00f19517cbdd0d4ea76fb79ca5f71962a6906a
1 /*
2 This file is part of KAddressBook.
4 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "contactfields.h"
23 #include <klocale.h>
25 QString ContactFields::label( Field field )
27 switch ( field ) {
28 case Undefined: return i18nc( "@item Undefined import field type", "Undefined" ); break;
29 case FormattedName: return KABC::Addressee::formattedNameLabel(); break;
30 case Prefix: return KABC::Addressee::prefixLabel(); break;
31 case GivenName: return KABC::Addressee::givenNameLabel(); break;
32 case AdditionalName: return KABC::Addressee::additionalNameLabel(); break;
33 case FamilyName: return KABC::Addressee::familyNameLabel(); break;
34 case Suffix: return KABC::Addressee::suffixLabel(); break;
35 case NickName: return KABC::Addressee::nickNameLabel(); break;
36 case Birthday: return KABC::Addressee::birthdayLabel(); break;
37 case Anniversary: return i18n( "Anniversary" ); break;
38 case HomeAddressStreet: return KABC::Addressee::homeAddressStreetLabel(); break;
39 case HomeAddressLocality: return KABC::Addressee::homeAddressLocalityLabel(); break;
40 case HomeAddressRegion: return KABC::Addressee::homeAddressRegionLabel(); break;
41 case HomeAddressPostalCode: return KABC::Addressee::homeAddressPostalCodeLabel(); break;
42 case HomeAddressCountry: return KABC::Addressee::homeAddressCountryLabel(); break;
43 case HomeAddressLabel: return KABC::Addressee::homeAddressLabelLabel(); break;
44 case BusinessAddressStreet: return KABC::Addressee::businessAddressStreetLabel(); break;
45 case BusinessAddressLocality: return KABC::Addressee::businessAddressLocalityLabel(); break;
46 case BusinessAddressRegion: return KABC::Addressee::businessAddressRegionLabel(); break;
47 case BusinessAddressPostalCode: return KABC::Addressee::businessAddressPostalCodeLabel(); break;
48 case BusinessAddressCountry: return KABC::Addressee::businessAddressCountryLabel(); break;
49 case BusinessAddressLabel: return KABC::Addressee::businessAddressLabelLabel(); break;
50 case HomePhone: return KABC::Addressee::homePhoneLabel(); break;
51 case BusinessPhone: return KABC::Addressee::businessPhoneLabel(); break;
52 case MobilePhone: return KABC::Addressee::mobilePhoneLabel(); break;
53 case HomeFax: return KABC::Addressee::homeFaxLabel(); break;
54 case BusinessFax: return KABC::Addressee::businessFaxLabel(); break;
55 case CarPhone: return KABC::Addressee::carPhoneLabel(); break;
56 case Isdn: return KABC::Addressee::isdnLabel(); break;
57 case Pager: return KABC::Addressee::pagerLabel(); break;
58 case PreferredEmail: return i18nc( "Preferred email address", "EMail (preferred)" ); break;
59 case Email2: return i18nc( "Second email address", "EMail (2)" ); break;
60 case Email3: return i18nc( "Third email address", "EMail (3)" ); break;
61 case Email4: return i18nc( "Fourth email address", "EMail (4)" ); break;
62 case Mailer: return KABC::Addressee::mailerLabel(); break;
63 case Title: return KABC::Addressee::titleLabel(); break;
64 case Role: return KABC::Addressee::roleLabel(); break;
65 case Organization: return KABC::Addressee::organizationLabel(); break;
66 case Note: return KABC::Addressee::noteLabel(); break;
67 case Homepage: return KABC::Addressee::urlLabel(); break;
70 return QString();
73 ContactFields::Fields ContactFields::allFields()
75 Fields fields;
77 fields << Undefined
78 << FormattedName
79 << Prefix
80 << GivenName
81 << AdditionalName
82 << FamilyName
83 << Suffix
84 << NickName
85 << Birthday
86 << Anniversary
87 << PreferredEmail
88 << Email2
89 << Email3
90 << Email4
91 << HomeAddressStreet
92 << HomeAddressLocality
93 << HomeAddressRegion
94 << HomeAddressPostalCode
95 << HomeAddressCountry
96 << HomeAddressLabel
97 << BusinessAddressStreet
98 << BusinessAddressLocality
99 << BusinessAddressRegion
100 << BusinessAddressPostalCode
101 << BusinessAddressCountry
102 << BusinessAddressLabel
103 << HomePhone
104 << BusinessPhone
105 << MobilePhone
106 << HomeFax
107 << BusinessFax
108 << CarPhone
109 << Isdn
110 << Pager
111 << Mailer
112 << Title
113 << Role
114 << Organization
115 << Note
116 << Homepage;
118 return fields;
121 void ContactFields::setValue( Field field, const QString &value, KABC::Addressee &contact )
123 switch ( field ) {
124 case ContactFields::Undefined:
125 break;
126 case ContactFields::FormattedName:
127 contact.setFormattedName( value );
128 break;
129 case ContactFields::GivenName:
130 contact.setGivenName( value );
131 break;
132 case ContactFields::FamilyName:
133 contact.setFamilyName( value );
134 break;
135 case ContactFields::AdditionalName:
136 contact.setAdditionalName( value );
137 break;
138 case ContactFields::Prefix:
139 contact.setPrefix( value );
140 break;
141 case ContactFields::Suffix:
142 contact.setSuffix( value );
143 break;
144 case ContactFields::NickName:
145 contact.setNickName( value );
146 break;
147 case ContactFields::Birthday:
148 contact.setBirthday( QDateTime::fromString( value, Qt::ISODate ) );
149 break;
150 case ContactFields::Anniversary:
151 contact.insertCustom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-Anniversary" ), value );
152 break;
153 case ContactFields::PreferredEmail:
154 contact.insertEmail( value, true );
155 break;
156 case ContactFields::Email2:
157 contact.insertEmail( value, false );
158 break;
159 case ContactFields::Email3:
160 contact.insertEmail( value, false );
161 break;
162 case ContactFields::Email4:
163 contact.insertEmail( value, false );
164 break;
165 case ContactFields::Role:
166 contact.setRole( value );
167 break;
168 case ContactFields::Title:
169 contact.setTitle( value );
170 break;
171 case ContactFields::Mailer:
172 contact.setMailer( value );
173 break;
174 case ContactFields::Homepage:
175 contact.setUrl( KUrl( value ) );
176 break;
177 case ContactFields::Organization:
178 contact.setOrganization( value );
179 break;
180 case ContactFields::Note:
181 contact.setNote( value );
182 break;
183 case ContactFields::HomePhone:
185 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Home );
186 number.setNumber( value );
187 contact.insertPhoneNumber( number );
189 break;
190 case ContactFields::BusinessPhone:
192 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Work );
193 number.setNumber( value );
194 contact.insertPhoneNumber( number );
196 break;
197 case ContactFields::MobilePhone:
199 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Cell );
200 number.setNumber( value );
201 contact.insertPhoneNumber( number );
203 break;
204 case ContactFields::HomeFax:
206 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Home |
207 KABC::PhoneNumber::Fax );
208 number.setNumber( value );
209 contact.insertPhoneNumber( number );
211 break;
212 case ContactFields::BusinessFax:
214 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Work |
215 KABC::PhoneNumber::Fax );
216 number.setNumber( value );
217 contact.insertPhoneNumber( number );
219 break;
220 case ContactFields::CarPhone:
222 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Car );
223 number.setNumber( value );
224 contact.insertPhoneNumber( number );
226 break;
227 case ContactFields::Isdn:
229 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Isdn );
230 number.setNumber( value );
231 contact.insertPhoneNumber( number );
233 break;
234 case ContactFields::Pager:
236 KABC::PhoneNumber number = contact.phoneNumber( KABC::PhoneNumber::Pager );
237 number.setNumber( value );
238 contact.insertPhoneNumber( number );
240 break;
242 case ContactFields::HomeAddressStreet:
244 KABC::Address address = contact.address( KABC::Address::Home );
245 address.setStreet( value );
246 contact.insertAddress( address );
248 break;
249 case ContactFields::HomeAddressLocality:
251 KABC::Address address = contact.address( KABC::Address::Home );
252 address.setLocality( value );
253 contact.insertAddress( address );
255 break;
256 case ContactFields::HomeAddressRegion:
258 KABC::Address address = contact.address( KABC::Address::Home );
259 address.setRegion( value );
260 contact.insertAddress( address );
262 break;
263 case ContactFields::HomeAddressPostalCode:
265 KABC::Address address = contact.address( KABC::Address::Home );
266 address.setPostalCode( value );
267 contact.insertAddress( address );
269 break;
270 case ContactFields::HomeAddressCountry:
272 KABC::Address address = contact.address( KABC::Address::Home );
273 address.setCountry( value );
274 contact.insertAddress( address );
276 break;
277 case ContactFields::HomeAddressLabel:
279 KABC::Address address = contact.address( KABC::Address::Home );
280 address.setLabel( value );
281 contact.insertAddress( address );
283 break;
284 case ContactFields::BusinessAddressStreet:
286 KABC::Address address = contact.address( KABC::Address::Work );
287 address.setStreet( value );
288 contact.insertAddress( address );
290 break;
291 case ContactFields::BusinessAddressLocality:
293 KABC::Address address = contact.address( KABC::Address::Work );
294 address.setLocality( value );
295 contact.insertAddress( address );
297 break;
298 case ContactFields::BusinessAddressRegion:
300 KABC::Address address = contact.address( KABC::Address::Work );
301 address.setRegion( value );
302 contact.insertAddress( address );
304 break;
305 case ContactFields::BusinessAddressPostalCode:
307 KABC::Address address = contact.address( KABC::Address::Work );
308 address.setPostalCode( value );
309 contact.insertAddress( address );
311 break;
312 case ContactFields::BusinessAddressCountry:
314 KABC::Address address = contact.address( KABC::Address::Work );
315 address.setCountry( value );
316 contact.insertAddress( address );
318 break;
319 case ContactFields::BusinessAddressLabel:
321 KABC::Address address = contact.address( KABC::Address::Work );
322 address.setLabel( value );
323 contact.insertAddress( address );
325 break;
329 QString ContactFields::value( Field field, const KABC::Addressee &contact )
331 switch ( field ) {
332 case Undefined: return QString(); break;
333 case FormattedName: return contact.formattedName(); break;
334 case Prefix: return contact.prefix(); break;
335 case GivenName: return contact.givenName(); break;
336 case AdditionalName: return contact.additionalName(); break;
337 case FamilyName: return contact.familyName(); break;
338 case Suffix: return contact.suffix(); break;
339 case NickName: return contact.nickName(); break;
340 case Birthday:
342 const QDateTime birthday = contact.birthday();
343 if ( birthday.isValid() )
344 return birthday.toString( Qt::ISODate );
345 else
346 return QString();
348 break;
349 case Anniversary: return contact.custom( QLatin1String( "KADDRESSBOOK" ), QLatin1String( "X-Anniversary" ) ); break;
350 case HomeAddressStreet:
352 const KABC::Address address = contact.address( KABC::Address::Home );
353 return address.street();
355 break;
356 case HomeAddressLocality:
358 const KABC::Address address = contact.address( KABC::Address::Home );
359 return address.locality();
361 break;
362 case HomeAddressRegion:
364 const KABC::Address address = contact.address( KABC::Address::Home );
365 return address.region();
367 break;
368 case HomeAddressPostalCode:
370 const KABC::Address address = contact.address( KABC::Address::Home );
371 return address.postalCode();
373 break;
374 case HomeAddressCountry:
376 const KABC::Address address = contact.address( KABC::Address::Home );
377 return address.country();
379 break;
380 case HomeAddressLabel:
382 const KABC::Address address = contact.address( KABC::Address::Home );
383 return address.label();
385 break;
386 case BusinessAddressStreet:
388 const KABC::Address address = contact.address( KABC::Address::Work );
389 return address.street();
391 break;
392 case BusinessAddressLocality:
394 const KABC::Address address = contact.address( KABC::Address::Work );
395 return address.locality();
397 break;
398 case BusinessAddressRegion:
400 const KABC::Address address = contact.address( KABC::Address::Work );
401 return address.region();
403 break;
404 case BusinessAddressPostalCode:
406 const KABC::Address address = contact.address( KABC::Address::Work );
407 return address.postalCode();
409 break;
410 case BusinessAddressCountry:
412 const KABC::Address address = contact.address( KABC::Address::Work );
413 return address.country();
415 break;
416 case BusinessAddressLabel:
418 const KABC::Address address = contact.address( KABC::Address::Work );
419 return address.label();
421 break;
422 case HomePhone: return contact.phoneNumber( KABC::PhoneNumber::Home ).number(); break;
423 case BusinessPhone: return contact.phoneNumber( KABC::PhoneNumber::Work ).number(); break;
424 case MobilePhone: return contact.phoneNumber( KABC::PhoneNumber::Cell ).number(); break;
425 case HomeFax: return contact.phoneNumber( KABC::PhoneNumber::Home | KABC::PhoneNumber::Fax ).number(); break;
426 case BusinessFax: return contact.phoneNumber( KABC::PhoneNumber::Work | KABC::PhoneNumber::Fax ).number(); break;
427 case CarPhone: return contact.phoneNumber( KABC::PhoneNumber::Car ).number(); break;
428 case Isdn: return contact.phoneNumber( KABC::PhoneNumber::Isdn ).number(); break;
429 case Pager: return contact.phoneNumber( KABC::PhoneNumber::Pager ).number(); break;
430 case PreferredEmail:
432 const QStringList emails = contact.emails();
433 if ( emails.count() > 0 )
434 return emails.at( 0 );
435 else
436 return QString();
438 case Email2:
440 const QStringList emails = contact.emails();
441 if ( emails.count() > 1 )
442 return emails.at( 1 );
443 else
444 return QString();
446 case Email3:
448 const QStringList emails = contact.emails();
449 if ( emails.count() > 2 )
450 return emails.at( 2 );
451 else
452 return QString();
454 case Email4:
456 const QStringList emails = contact.emails();
457 if ( emails.count() > 3 )
458 return emails.at( 3 );
459 else
460 return QString();
462 case Mailer: return contact.mailer(); break;
463 case Title: return contact.title(); break;
464 case Role: return contact.role(); break;
465 case Organization: return contact.organization(); break;
466 case Note: return contact.note(); break;
467 case Homepage: return contact.url().url(); break;
470 return QString();