Remove double margin
[kdepim.git] / kaddressbook / contactfields.cpp
blob4a4c51da1ea11e06f439173ea941d58e1263bb17
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 <KLocalizedString>
24 #include <QUrl>
26 QString ContactFields::label(Field field)
28 switch (field) {
29 case Undefined:
30 return i18nc("@item Undefined import field type", "Undefined");
31 break;
32 case FormattedName:
33 return KContacts::Addressee::formattedNameLabel();
34 case Prefix:
35 return KContacts::Addressee::prefixLabel();
36 case GivenName:
37 return KContacts::Addressee::givenNameLabel();
38 case AdditionalName:
39 return KContacts::Addressee::additionalNameLabel();
40 case FamilyName:
41 return KContacts::Addressee::familyNameLabel();
42 case Suffix:
43 return KContacts::Addressee::suffixLabel();
44 case NickName:
45 return KContacts::Addressee::nickNameLabel();
46 case Birthday:
47 return KContacts::Addressee::birthdayLabel();
48 case Anniversary:
49 return i18nc("The wedding anniversary of a contact", "Anniversary");
50 case HomeAddressStreet:
51 return KContacts::Addressee::homeAddressStreetLabel();
52 case HomeAddressPostOfficeBox:
53 return KContacts::Addressee::homeAddressPostOfficeBoxLabel();
54 case HomeAddressLocality:
55 return KContacts::Addressee::homeAddressLocalityLabel();
56 case HomeAddressRegion:
57 return KContacts::Addressee::homeAddressRegionLabel();
58 case HomeAddressPostalCode:
59 return KContacts::Addressee::homeAddressPostalCodeLabel();
60 case HomeAddressCountry:
61 return KContacts::Addressee::homeAddressCountryLabel();
62 case HomeAddressLabel:
63 return KContacts::Addressee::homeAddressLabelLabel();
64 case BusinessAddressStreet:
65 return KContacts::Addressee::businessAddressStreetLabel();
66 case BusinessAddressPostOfficeBox:
67 return KContacts::Addressee::businessAddressPostOfficeBoxLabel();
68 case BusinessAddressLocality:
69 return KContacts::Addressee::businessAddressLocalityLabel();
70 case BusinessAddressRegion:
71 return KContacts::Addressee::businessAddressRegionLabel();
72 case BusinessAddressPostalCode:
73 return KContacts::Addressee::businessAddressPostalCodeLabel();
74 case BusinessAddressCountry:
75 return KContacts::Addressee::businessAddressCountryLabel();
76 case BusinessAddressLabel:
77 return KContacts::Addressee::businessAddressLabelLabel();
78 case HomePhone:
79 return KContacts::Addressee::homePhoneLabel();
80 case BusinessPhone:
81 return KContacts::Addressee::businessPhoneLabel();
82 case MobilePhone:
83 return KContacts::Addressee::mobilePhoneLabel();
84 case HomeFax:
85 return KContacts::Addressee::homeFaxLabel();
86 case BusinessFax:
87 return KContacts::Addressee::businessFaxLabel();
88 case CarPhone:
89 return KContacts::Addressee::carPhoneLabel();
90 case Isdn:
91 return KContacts::Addressee::isdnLabel();
92 case Pager:
93 return KContacts::Addressee::pagerLabel();
94 case PreferredEmail:
95 return i18nc("Preferred email address", "EMail (preferred)");
96 case Email2:
97 return i18nc("Second email address", "EMail (2)");
98 case Email3:
99 return i18nc("Third email address", "EMail (3)");
100 case Email4:
101 return i18nc("Fourth email address", "EMail (4)");
102 case Mailer:
103 return KContacts::Addressee::mailerLabel();
104 case Title:
105 return KContacts::Addressee::titleLabel();
106 case Role:
107 return KContacts::Addressee::roleLabel();
108 case Organization:
109 return KContacts::Addressee::organizationLabel();
110 case Note:
111 return KContacts::Addressee::noteLabel();
112 case Homepage:
113 return KContacts::Addressee::urlLabel();
114 case BlogFeed:
115 return i18n("Blog Feed");
116 case Profession:
117 return i18n("Profession");
118 case Office:
119 return i18n("Office");
120 case Manager:
121 return i18n("Manager");
122 case Assistant:
123 return i18n("Assistant");
124 case Spouse:
125 return i18n("Spouse");
128 return QString();
131 ContactFields::Fields ContactFields::allFields()
133 Fields fields;
135 fields << Undefined
136 << FormattedName
137 << Prefix
138 << GivenName
139 << AdditionalName
140 << FamilyName
141 << Suffix
142 << NickName
143 << Birthday
144 << Anniversary
145 << PreferredEmail
146 << Email2
147 << Email3
148 << Email4
149 << HomeAddressStreet
150 << HomeAddressPostOfficeBox
151 << HomeAddressLocality
152 << HomeAddressRegion
153 << HomeAddressPostalCode
154 << HomeAddressCountry
155 << HomeAddressLabel
156 << BusinessAddressStreet
157 << BusinessAddressPostOfficeBox
158 << BusinessAddressLocality
159 << BusinessAddressRegion
160 << BusinessAddressPostalCode
161 << BusinessAddressCountry
162 << BusinessAddressLabel
163 << HomePhone
164 << BusinessPhone
165 << MobilePhone
166 << HomeFax
167 << BusinessFax
168 << CarPhone
169 << Isdn
170 << Pager
171 << Mailer
172 << Title
173 << Role
174 << Organization
175 << Note
176 << Homepage
177 << BlogFeed
178 << Profession
179 << Office
180 << Manager
181 << Assistant
182 << Spouse;
184 return fields;
187 void ContactFields::setValue(Field field, const QString &value, KContacts::Addressee &contact)
189 switch (field) {
190 case ContactFields::Undefined:
191 break;
192 case ContactFields::FormattedName:
193 contact.setFormattedName(value);
194 break;
195 case ContactFields::GivenName:
196 contact.setGivenName(value);
197 break;
198 case ContactFields::FamilyName:
199 contact.setFamilyName(value);
200 break;
201 case ContactFields::AdditionalName:
202 contact.setAdditionalName(value);
203 break;
204 case ContactFields::Prefix:
205 contact.setPrefix(value);
206 break;
207 case ContactFields::Suffix:
208 contact.setSuffix(value);
209 break;
210 case ContactFields::NickName:
211 contact.setNickName(value);
212 break;
213 case ContactFields::Birthday:
214 contact.setBirthday(QDateTime::fromString(value, Qt::ISODate));
215 break;
216 case ContactFields::Anniversary:
217 contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
218 QStringLiteral("X-Anniversary"), value);
219 break;
220 case ContactFields::PreferredEmail:
221 contact.insertEmail(value, true);
222 break;
223 case ContactFields::Email2:
224 contact.insertEmail(value, false);
225 break;
226 case ContactFields::Email3:
227 contact.insertEmail(value, false);
228 break;
229 case ContactFields::Email4:
230 contact.insertEmail(value, false);
231 break;
232 case ContactFields::Role:
233 contact.setRole(value);
234 break;
235 case ContactFields::Title:
236 contact.setTitle(value);
237 break;
238 case ContactFields::Mailer:
239 contact.setMailer(value);
240 break;
241 case ContactFields::Homepage: {
242 KContacts::ResourceLocatorUrl url;
243 url.setUrl(QUrl(value));
244 contact.setUrl(url);
246 break;
247 case ContactFields::Organization:
248 contact.setOrganization(value);
249 break;
250 case ContactFields::Note:
251 contact.setNote(value);
252 break;
253 case ContactFields::HomePhone: {
254 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Home);
255 number.setNumber(value);
256 contact.insertPhoneNumber(number);
258 break;
259 case ContactFields::BusinessPhone: {
260 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Work);
261 number.setNumber(value);
262 contact.insertPhoneNumber(number);
264 break;
265 case ContactFields::MobilePhone: {
266 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Cell);
267 number.setNumber(value);
268 contact.insertPhoneNumber(number);
270 break;
271 case ContactFields::HomeFax: {
272 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Home |
273 KContacts::PhoneNumber::Fax);
274 number.setNumber(value);
275 contact.insertPhoneNumber(number);
277 break;
278 case ContactFields::BusinessFax: {
279 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Work |
280 KContacts::PhoneNumber::Fax);
281 number.setNumber(value);
282 contact.insertPhoneNumber(number);
284 break;
285 case ContactFields::CarPhone: {
286 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Car);
287 number.setNumber(value);
288 contact.insertPhoneNumber(number);
290 break;
291 case ContactFields::Isdn: {
292 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Isdn);
293 number.setNumber(value);
294 contact.insertPhoneNumber(number);
296 break;
297 case ContactFields::Pager: {
298 KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Pager);
299 number.setNumber(value);
300 contact.insertPhoneNumber(number);
302 break;
304 case ContactFields::HomeAddressStreet: {
305 KContacts::Address address = contact.address(KContacts::Address::Home);
306 address.setStreet(value);
307 contact.insertAddress(address);
309 break;
310 case ContactFields::HomeAddressPostOfficeBox: {
311 KContacts::Address address = contact.address(KContacts::Address::Home);
312 address.setPostOfficeBox(value);
313 contact.insertAddress(address);
315 break;
316 case ContactFields::HomeAddressLocality: {
317 KContacts::Address address = contact.address(KContacts::Address::Home);
318 address.setLocality(value);
319 contact.insertAddress(address);
321 break;
322 case ContactFields::HomeAddressRegion: {
323 KContacts::Address address = contact.address(KContacts::Address::Home);
324 address.setRegion(value);
325 contact.insertAddress(address);
327 break;
328 case ContactFields::HomeAddressPostalCode: {
329 KContacts::Address address = contact.address(KContacts::Address::Home);
330 address.setPostalCode(value);
331 contact.insertAddress(address);
333 break;
334 case ContactFields::HomeAddressCountry: {
335 KContacts::Address address = contact.address(KContacts::Address::Home);
336 address.setCountry(value);
337 contact.insertAddress(address);
339 break;
340 case ContactFields::HomeAddressLabel: {
341 KContacts::Address address = contact.address(KContacts::Address::Home);
342 address.setLabel(value);
343 contact.insertAddress(address);
345 break;
346 case ContactFields::BusinessAddressStreet: {
347 KContacts::Address address = contact.address(KContacts::Address::Work);
348 address.setStreet(value);
349 contact.insertAddress(address);
351 break;
352 case ContactFields::BusinessAddressPostOfficeBox: {
353 KContacts::Address address = contact.address(KContacts::Address::Work);
354 address.setPostOfficeBox(value);
355 contact.insertAddress(address);
357 break;
358 case ContactFields::BusinessAddressLocality: {
359 KContacts::Address address = contact.address(KContacts::Address::Work);
360 address.setLocality(value);
361 contact.insertAddress(address);
363 break;
364 case ContactFields::BusinessAddressRegion: {
365 KContacts::Address address = contact.address(KContacts::Address::Work);
366 address.setRegion(value);
367 contact.insertAddress(address);
369 break;
370 case ContactFields::BusinessAddressPostalCode: {
371 KContacts::Address address = contact.address(KContacts::Address::Work);
372 address.setPostalCode(value);
373 contact.insertAddress(address);
375 break;
376 case ContactFields::BusinessAddressCountry: {
377 KContacts::Address address = contact.address(KContacts::Address::Work);
378 address.setCountry(value);
379 contact.insertAddress(address);
381 break;
382 case ContactFields::BusinessAddressLabel: {
383 KContacts::Address address = contact.address(KContacts::Address::Work);
384 address.setLabel(value);
385 contact.insertAddress(address);
387 break;
388 case BlogFeed:
389 contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
390 QStringLiteral("BlogFeed"), value);
391 break;
392 case Profession:
393 contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
394 QStringLiteral("X-Profession"), value);
395 break;
396 case Office:
397 contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
398 QStringLiteral("X-Office"), value);
399 break;
400 case Manager:
401 contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
402 QStringLiteral("X-ManagersName"), value);
403 break;
404 case Assistant:
405 contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
406 QStringLiteral("X-AssistantsName"), value);
407 break;
408 case Spouse:
409 contact.insertCustom(QStringLiteral("KADDRESSBOOK"),
410 QStringLiteral("X-SpousesName"), value);
411 break;
415 QString ContactFields::value(Field field, const KContacts::Addressee &contact)
417 switch (field) {
418 case Undefined:
419 return QString();
420 break;
421 case FormattedName:
422 return contact.formattedName();
423 break;
424 case Prefix:
425 return contact.prefix();
426 break;
427 case GivenName:
428 return contact.givenName();
429 break;
430 case AdditionalName:
431 return contact.additionalName();
432 break;
433 case FamilyName:
434 return contact.familyName();
435 break;
436 case Suffix:
437 return contact.suffix();
438 break;
439 case NickName:
440 return contact.nickName();
441 break;
442 case Birthday: {
443 const QDateTime birthday = contact.birthday();
444 if (birthday.date().isValid()) {
445 return birthday.date().toString(Qt::ISODate);
446 } else {
447 return QString();
450 break;
451 case Anniversary:
452 return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Anniversary"));
453 break;
454 case HomeAddressStreet: {
455 const KContacts::Address address = contact.address(KContacts::Address::Home);
456 return address.street();
458 break;
459 case HomeAddressPostOfficeBox: {
460 const KContacts::Address address = contact.address(KContacts::Address::Home);
461 return address.postOfficeBox();
463 break;
464 case HomeAddressLocality: {
465 const KContacts::Address address = contact.address(KContacts::Address::Home);
466 return address.locality();
468 break;
469 case HomeAddressRegion: {
470 const KContacts::Address address = contact.address(KContacts::Address::Home);
471 return address.region();
473 break;
474 case HomeAddressPostalCode: {
475 const KContacts::Address address = contact.address(KContacts::Address::Home);
476 return address.postalCode();
478 break;
479 case HomeAddressCountry: {
480 const KContacts::Address address = contact.address(KContacts::Address::Home);
481 return address.country();
483 break;
484 case HomeAddressLabel: {
485 const KContacts::Address address = contact.address(KContacts::Address::Home);
486 return address.label();
488 break;
489 case BusinessAddressStreet: {
490 const KContacts::Address address = contact.address(KContacts::Address::Work);
491 return address.street();
493 break;
494 case BusinessAddressPostOfficeBox: {
495 const KContacts::Address address = contact.address(KContacts::Address::Work);
496 return address.postOfficeBox();
498 break;
499 case BusinessAddressLocality: {
500 const KContacts::Address address = contact.address(KContacts::Address::Work);
501 return address.locality();
503 break;
504 case BusinessAddressRegion: {
505 const KContacts::Address address = contact.address(KContacts::Address::Work);
506 return address.region();
508 break;
509 case BusinessAddressPostalCode: {
510 const KContacts::Address address = contact.address(KContacts::Address::Work);
511 return address.postalCode();
513 break;
514 case BusinessAddressCountry: {
515 const KContacts::Address address = contact.address(KContacts::Address::Work);
516 return address.country();
518 break;
519 case BusinessAddressLabel: {
520 const KContacts::Address address = contact.address(KContacts::Address::Work);
521 return address.label();
523 break;
524 case HomePhone:
525 return contact.phoneNumber(KContacts::PhoneNumber::Home).number();
526 break;
527 case BusinessPhone:
528 return contact.phoneNumber(KContacts::PhoneNumber::Work).number();
529 break;
530 case MobilePhone:
531 return contact.phoneNumber(KContacts::PhoneNumber::Cell).number();
532 break;
533 case HomeFax:
534 return contact.phoneNumber(KContacts::PhoneNumber::Home | KContacts::PhoneNumber::Fax).number();
535 break;
536 case BusinessFax:
537 return contact.phoneNumber(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Fax).number();
538 break;
539 case CarPhone:
540 return contact.phoneNumber(KContacts::PhoneNumber::Car).number();
541 break;
542 case Isdn:
543 return contact.phoneNumber(KContacts::PhoneNumber::Isdn).number();
544 break;
545 case Pager:
546 return contact.phoneNumber(KContacts::PhoneNumber::Pager).number();
547 break;
548 case PreferredEmail: {
549 const QStringList emails = contact.emails();
550 if (emails.count() > 0) {
551 return emails.at(0);
552 } else {
553 return QString();
556 break;
557 case Email2: {
558 const QStringList emails = contact.emails();
559 if (emails.count() > 1) {
560 return emails.at(1);
561 } else {
562 return QString();
565 break;
566 case Email3: {
567 const QStringList emails = contact.emails();
568 if (emails.count() > 2) {
569 return emails.at(2);
570 } else {
571 return QString();
574 break;
575 case Email4: {
576 const QStringList emails = contact.emails();
577 if (emails.count() > 3) {
578 return emails.at(3);
579 } else {
580 return QString();
583 break;
584 case Mailer:
585 return contact.mailer();
586 break;
587 case Title:
588 return contact.title();
589 break;
590 case Role:
591 return contact.role();
592 break;
593 case Organization:
594 return contact.organization();
595 break;
596 case Note:
597 return contact.note();
598 break;
599 case Homepage:
600 return contact.url().url().url();
601 break;
602 case BlogFeed:
603 return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("BlogFeed"));
604 break;
605 case Profession:
606 return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession"));
607 break;
608 case Office:
609 return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office"));
610 break;
611 case Manager:
612 return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ManagersName"));
613 break;
614 case Assistant:
615 return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-AssistantsName"));
616 break;
617 case Spouse:
618 return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"));
619 break;
622 return QString();