File indexing completed on 2024-12-22 04:55:32

0001 /*
0002   This file is part of KAddressBook.
0003 
0004   SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
0005 
0006   SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "contactfields.h"
0010 
0011 #include <KLocalizedString>
0012 #include <QUrl>
0013 
0014 using namespace KAddressBookImportExport;
0015 
0016 QString ContactFields::label(Field field)
0017 {
0018     switch (field) {
0019     case Undefined:
0020         return i18nc("@item Undefined import field type", "Undefined");
0021         break;
0022     case FormattedName:
0023         return KContacts::Addressee::formattedNameLabel();
0024     case Prefix:
0025         return KContacts::Addressee::prefixLabel();
0026     case GivenName:
0027         return KContacts::Addressee::givenNameLabel();
0028     case AdditionalName:
0029         return KContacts::Addressee::additionalNameLabel();
0030     case FamilyName:
0031         return KContacts::Addressee::familyNameLabel();
0032     case Suffix:
0033         return KContacts::Addressee::suffixLabel();
0034     case NickName:
0035         return KContacts::Addressee::nickNameLabel();
0036     case Birthday:
0037         return KContacts::Addressee::birthdayLabel();
0038     case Anniversary:
0039         return i18nc("The wedding anniversary of a contact", "Anniversary");
0040     case HomeAddressStreet:
0041         return KContacts::Addressee::homeAddressStreetLabel();
0042     case HomeAddressPostOfficeBox:
0043         return KContacts::Addressee::homeAddressPostOfficeBoxLabel();
0044     case HomeAddressLocality:
0045         return KContacts::Addressee::homeAddressLocalityLabel();
0046     case HomeAddressRegion:
0047         return KContacts::Addressee::homeAddressRegionLabel();
0048     case HomeAddressPostalCode:
0049         return KContacts::Addressee::homeAddressPostalCodeLabel();
0050     case HomeAddressCountry:
0051         return KContacts::Addressee::homeAddressCountryLabel();
0052     case HomeAddressLabel:
0053         return KContacts::Addressee::homeAddressLabelLabel();
0054     case BusinessAddressStreet:
0055         return KContacts::Addressee::businessAddressStreetLabel();
0056     case BusinessAddressPostOfficeBox:
0057         return KContacts::Addressee::businessAddressPostOfficeBoxLabel();
0058     case BusinessAddressLocality:
0059         return KContacts::Addressee::businessAddressLocalityLabel();
0060     case BusinessAddressRegion:
0061         return KContacts::Addressee::businessAddressRegionLabel();
0062     case BusinessAddressPostalCode:
0063         return KContacts::Addressee::businessAddressPostalCodeLabel();
0064     case BusinessAddressCountry:
0065         return KContacts::Addressee::businessAddressCountryLabel();
0066     case BusinessAddressLabel:
0067         return KContacts::Addressee::businessAddressLabelLabel();
0068     case HomePhone:
0069         return KContacts::Addressee::homePhoneLabel();
0070     case BusinessPhone:
0071         return KContacts::Addressee::businessPhoneLabel();
0072     case MobilePhone:
0073         return KContacts::Addressee::mobilePhoneLabel();
0074     case HomeFax:
0075         return KContacts::Addressee::homeFaxLabel();
0076     case BusinessFax:
0077         return KContacts::Addressee::businessFaxLabel();
0078     case CarPhone:
0079         return KContacts::Addressee::carPhoneLabel();
0080     case Isdn:
0081         return KContacts::Addressee::isdnLabel();
0082     case Pager:
0083         return KContacts::Addressee::pagerLabel();
0084     case PreferredEmail:
0085         return i18nc("Preferred email address", "EMail (preferred)");
0086     case Email2:
0087         return i18nc("Second email address", "EMail (2)");
0088     case Email3:
0089         return i18nc("Third email address", "EMail (3)");
0090     case Email4:
0091         return i18nc("Fourth email address", "EMail (4)");
0092     case Mailer:
0093         return KContacts::Addressee::mailerLabel();
0094     case Title:
0095         return KContacts::Addressee::titleLabel();
0096     case Role:
0097         return KContacts::Addressee::roleLabel();
0098     case Organization:
0099         return KContacts::Addressee::organizationLabel();
0100     case Note:
0101         return KContacts::Addressee::noteLabel();
0102     case Homepage:
0103         return KContacts::Addressee::urlLabel();
0104     case BlogFeed:
0105         return i18n("Blog Feed");
0106     case Profession:
0107         return i18n("Profession");
0108     case Office:
0109         return i18n("Office");
0110     case Manager:
0111         return i18n("Manager");
0112     case Assistant:
0113         return i18n("Assistant");
0114     case Spouse:
0115         return i18n("Spouse");
0116     }
0117 
0118     return {};
0119 }
0120 
0121 ContactFields::Fields ContactFields::allFields()
0122 {
0123     Fields fields;
0124 
0125     fields << Undefined << FormattedName << Prefix << GivenName << AdditionalName << FamilyName << Suffix << NickName << Birthday << Anniversary
0126            << PreferredEmail << Email2 << Email3 << Email4 << HomeAddressStreet << HomeAddressPostOfficeBox << HomeAddressLocality << HomeAddressRegion
0127            << HomeAddressPostalCode << HomeAddressCountry << HomeAddressLabel << BusinessAddressStreet << BusinessAddressPostOfficeBox
0128            << BusinessAddressLocality << BusinessAddressRegion << BusinessAddressPostalCode << BusinessAddressCountry << BusinessAddressLabel << HomePhone
0129            << BusinessPhone << MobilePhone << HomeFax << BusinessFax << CarPhone << Isdn << Pager << Mailer << Title << Role << Organization << Note << Homepage
0130            << BlogFeed << Profession << Office << Manager << Assistant << Spouse;
0131 
0132     return fields;
0133 }
0134 
0135 void ContactFields::setValue(Field field, const QString &value, KContacts::Addressee &contact)
0136 {
0137     switch (field) {
0138     case ContactFields::Undefined:
0139         break;
0140     case ContactFields::FormattedName:
0141         contact.setFormattedName(value);
0142         break;
0143     case ContactFields::GivenName:
0144         contact.setGivenName(value);
0145         break;
0146     case ContactFields::FamilyName:
0147         contact.setFamilyName(value);
0148         break;
0149     case ContactFields::AdditionalName:
0150         contact.setAdditionalName(value);
0151         break;
0152     case ContactFields::Prefix:
0153         contact.setPrefix(value);
0154         break;
0155     case ContactFields::Suffix:
0156         contact.setSuffix(value);
0157         break;
0158     case ContactFields::NickName:
0159         contact.setNickName(value);
0160         break;
0161     case ContactFields::Birthday:
0162         contact.setBirthday(QDate::fromString(value, Qt::ISODate));
0163         break;
0164     case ContactFields::Anniversary:
0165         contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Anniversary"), value);
0166         break;
0167     case ContactFields::PreferredEmail: {
0168         KContacts::Email email(value);
0169         email.setPreferred(true);
0170         contact.addEmail(email);
0171         break;
0172     }
0173     case ContactFields::Email2:
0174     case ContactFields::Email3:
0175     case ContactFields::Email4:
0176         contact.addEmail(KContacts::Email(value));
0177         break;
0178     case ContactFields::Role:
0179         contact.setRole(value);
0180         break;
0181     case ContactFields::Title:
0182         contact.setTitle(value);
0183         break;
0184     case ContactFields::Mailer:
0185         contact.setMailer(value);
0186         break;
0187     case ContactFields::Homepage: {
0188         KContacts::ResourceLocatorUrl url;
0189         url.setUrl(QUrl(value));
0190         contact.setUrl(url);
0191         break;
0192     }
0193     case ContactFields::Organization:
0194         contact.setOrganization(value);
0195         break;
0196     case ContactFields::Note:
0197         contact.setNote(value);
0198         break;
0199     case ContactFields::HomePhone: {
0200         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Home);
0201         number.setNumber(value);
0202         contact.insertPhoneNumber(number);
0203         break;
0204     }
0205     case ContactFields::BusinessPhone: {
0206         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Work);
0207         number.setNumber(value);
0208         contact.insertPhoneNumber(number);
0209         break;
0210     }
0211     case ContactFields::MobilePhone: {
0212         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Cell);
0213         number.setNumber(value);
0214         contact.insertPhoneNumber(number);
0215         break;
0216     }
0217     case ContactFields::HomeFax: {
0218         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Home | KContacts::PhoneNumber::Fax);
0219         number.setNumber(value);
0220         contact.insertPhoneNumber(number);
0221         break;
0222     }
0223     case ContactFields::BusinessFax: {
0224         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Fax);
0225         number.setNumber(value);
0226         contact.insertPhoneNumber(number);
0227         break;
0228     }
0229     case ContactFields::CarPhone: {
0230         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Car);
0231         number.setNumber(value);
0232         contact.insertPhoneNumber(number);
0233         break;
0234     }
0235     case ContactFields::Isdn: {
0236         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Isdn);
0237         number.setNumber(value);
0238         contact.insertPhoneNumber(number);
0239         break;
0240     }
0241     case ContactFields::Pager: {
0242         KContacts::PhoneNumber number = contact.phoneNumber(KContacts::PhoneNumber::Pager);
0243         number.setNumber(value);
0244         contact.insertPhoneNumber(number);
0245         break;
0246     }
0247 
0248     case ContactFields::HomeAddressStreet: {
0249         KContacts::Address address = contact.address(KContacts::Address::Home);
0250         address.setStreet(value);
0251         contact.insertAddress(address);
0252         break;
0253     }
0254     case ContactFields::HomeAddressPostOfficeBox: {
0255         KContacts::Address address = contact.address(KContacts::Address::Home);
0256         address.setPostOfficeBox(value);
0257         contact.insertAddress(address);
0258         break;
0259     }
0260     case ContactFields::HomeAddressLocality: {
0261         KContacts::Address address = contact.address(KContacts::Address::Home);
0262         address.setLocality(value);
0263         contact.insertAddress(address);
0264         break;
0265     }
0266     case ContactFields::HomeAddressRegion: {
0267         KContacts::Address address = contact.address(KContacts::Address::Home);
0268         address.setRegion(value);
0269         contact.insertAddress(address);
0270         break;
0271     }
0272     case ContactFields::HomeAddressPostalCode: {
0273         KContacts::Address address = contact.address(KContacts::Address::Home);
0274         address.setPostalCode(value);
0275         contact.insertAddress(address);
0276         break;
0277     }
0278     case ContactFields::HomeAddressCountry: {
0279         KContacts::Address address = contact.address(KContacts::Address::Home);
0280         address.setCountry(value);
0281         contact.insertAddress(address);
0282         break;
0283     }
0284     case ContactFields::HomeAddressLabel: {
0285         KContacts::Address address = contact.address(KContacts::Address::Home);
0286         address.setLabel(value);
0287         contact.insertAddress(address);
0288         break;
0289     }
0290     case ContactFields::BusinessAddressStreet: {
0291         KContacts::Address address = contact.address(KContacts::Address::Work);
0292         address.setStreet(value);
0293         contact.insertAddress(address);
0294         break;
0295     }
0296     case ContactFields::BusinessAddressPostOfficeBox: {
0297         KContacts::Address address = contact.address(KContacts::Address::Work);
0298         address.setPostOfficeBox(value);
0299         contact.insertAddress(address);
0300         break;
0301     }
0302     case ContactFields::BusinessAddressLocality: {
0303         KContacts::Address address = contact.address(KContacts::Address::Work);
0304         address.setLocality(value);
0305         contact.insertAddress(address);
0306         break;
0307     }
0308     case ContactFields::BusinessAddressRegion: {
0309         KContacts::Address address = contact.address(KContacts::Address::Work);
0310         address.setRegion(value);
0311         contact.insertAddress(address);
0312         break;
0313     }
0314     case ContactFields::BusinessAddressPostalCode: {
0315         KContacts::Address address = contact.address(KContacts::Address::Work);
0316         address.setPostalCode(value);
0317         contact.insertAddress(address);
0318         break;
0319     }
0320     case ContactFields::BusinessAddressCountry: {
0321         KContacts::Address address = contact.address(KContacts::Address::Work);
0322         address.setCountry(value);
0323         contact.insertAddress(address);
0324         break;
0325     }
0326     case ContactFields::BusinessAddressLabel: {
0327         KContacts::Address address = contact.address(KContacts::Address::Work);
0328         address.setLabel(value);
0329         contact.insertAddress(address);
0330         break;
0331     }
0332     case BlogFeed:
0333         contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("BlogFeed"), value);
0334         break;
0335     case Profession:
0336         contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession"), value);
0337         break;
0338     case Office:
0339         contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office"), value);
0340         break;
0341     case Manager:
0342         contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ManagersName"), value);
0343         break;
0344     case Assistant:
0345         contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-AssistantsName"), value);
0346         break;
0347     case Spouse:
0348         contact.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"), value);
0349         break;
0350     }
0351 }
0352 
0353 QString ContactFields::value(Field field, const KContacts::Addressee &contact)
0354 {
0355     switch (field) {
0356     case Undefined:
0357         return {};
0358     case FormattedName:
0359         return contact.formattedName();
0360     case Prefix:
0361         return contact.prefix();
0362     case GivenName:
0363         return contact.givenName();
0364     case AdditionalName:
0365         return contact.additionalName();
0366     case FamilyName:
0367         return contact.familyName();
0368     case Suffix:
0369         return contact.suffix();
0370     case NickName:
0371         return contact.nickName();
0372     case Birthday: {
0373         const QDateTime birthday = contact.birthday();
0374         if (birthday.date().isValid()) {
0375             return birthday.date().toString(Qt::ISODate);
0376         } else {
0377             return {};
0378         }
0379     }
0380     case Anniversary:
0381         return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Anniversary"));
0382     case HomeAddressStreet: {
0383         const KContacts::Address address = contact.address(KContacts::Address::Home);
0384         return address.street();
0385     }
0386     case HomeAddressPostOfficeBox: {
0387         const KContacts::Address address = contact.address(KContacts::Address::Home);
0388         return address.postOfficeBox();
0389     }
0390     case HomeAddressLocality: {
0391         const KContacts::Address address = contact.address(KContacts::Address::Home);
0392         return address.locality();
0393     }
0394     case HomeAddressRegion: {
0395         const KContacts::Address address = contact.address(KContacts::Address::Home);
0396         return address.region();
0397     }
0398     case HomeAddressPostalCode: {
0399         const KContacts::Address address = contact.address(KContacts::Address::Home);
0400         return address.postalCode();
0401     }
0402     case HomeAddressCountry: {
0403         const KContacts::Address address = contact.address(KContacts::Address::Home);
0404         return address.country();
0405     }
0406     case HomeAddressLabel: {
0407         const KContacts::Address address = contact.address(KContacts::Address::Home);
0408         return address.label();
0409     }
0410     case BusinessAddressStreet: {
0411         const KContacts::Address address = contact.address(KContacts::Address::Work);
0412         return address.street();
0413     }
0414     case BusinessAddressPostOfficeBox: {
0415         const KContacts::Address address = contact.address(KContacts::Address::Work);
0416         return address.postOfficeBox();
0417     }
0418     case BusinessAddressLocality: {
0419         const KContacts::Address address = contact.address(KContacts::Address::Work);
0420         return address.locality();
0421     }
0422     case BusinessAddressRegion: {
0423         const KContacts::Address address = contact.address(KContacts::Address::Work);
0424         return address.region();
0425     }
0426     case BusinessAddressPostalCode: {
0427         const KContacts::Address address = contact.address(KContacts::Address::Work);
0428         return address.postalCode();
0429     }
0430     case BusinessAddressCountry: {
0431         const KContacts::Address address = contact.address(KContacts::Address::Work);
0432         return address.country();
0433     }
0434     case BusinessAddressLabel: {
0435         const KContacts::Address address = contact.address(KContacts::Address::Work);
0436         return address.label();
0437     }
0438     case HomePhone:
0439         return contact.phoneNumber(KContacts::PhoneNumber::Home).number();
0440     case BusinessPhone:
0441         return contact.phoneNumber(KContacts::PhoneNumber::Work).number();
0442     case MobilePhone:
0443         return contact.phoneNumber(KContacts::PhoneNumber::Cell).number();
0444     case HomeFax:
0445         return contact.phoneNumber(KContacts::PhoneNumber::Home | KContacts::PhoneNumber::Fax).number();
0446     case BusinessFax:
0447         return contact.phoneNumber(KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Fax).number();
0448     case CarPhone:
0449         return contact.phoneNumber(KContacts::PhoneNumber::Car).number();
0450     case Isdn:
0451         return contact.phoneNumber(KContacts::PhoneNumber::Isdn).number();
0452     case Pager:
0453         return contact.phoneNumber(KContacts::PhoneNumber::Pager).number();
0454     case PreferredEmail: {
0455         const QStringList emails = contact.emails();
0456         if (!emails.isEmpty()) {
0457             return emails.at(0);
0458         } else {
0459             return {};
0460         }
0461     }
0462     case Email2: {
0463         const QStringList emails = contact.emails();
0464         if (emails.count() > 1) {
0465             return emails.at(1);
0466         } else {
0467             return {};
0468         }
0469     }
0470     case Email3: {
0471         const QStringList emails = contact.emails();
0472         if (emails.count() > 2) {
0473             return emails.at(2);
0474         } else {
0475             return {};
0476         }
0477     }
0478     case Email4: {
0479         const QStringList emails = contact.emails();
0480         if (emails.count() > 3) {
0481             return emails.at(3);
0482         } else {
0483             return {};
0484         }
0485     }
0486     case Mailer:
0487         return contact.mailer();
0488     case Title:
0489         return contact.title();
0490     case Role:
0491         return contact.role();
0492     case Organization:
0493         return contact.organization();
0494     case Note:
0495         return contact.note();
0496     case Homepage:
0497         return contact.url().url().url();
0498     case BlogFeed:
0499         return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("BlogFeed"));
0500     case Profession:
0501         return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession"));
0502     case Office:
0503         return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office"));
0504     case Manager:
0505         return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ManagersName"));
0506     case Assistant:
0507         return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-AssistantsName"));
0508     case Spouse:
0509         return contact.custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"));
0510     }
0511 
0512     return {};
0513 }