File indexing completed on 2024-05-19 05:07:15

0001 /*
0002     SPDX-FileCopyrightText: 2014-2015 Cristian OneČ› <onet.cristian@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include <config-kmymoney.h>
0007 
0008 #include "mymoneycontact.h"
0009 
0010 #ifdef ENABLE_ADDRESSBOOK
0011 #include <KIdentityManagement/Identity>
0012 #include <KIdentityManagement/IdentityManager>
0013 #include <akonadi_version.h>
0014 
0015 #if AKONADI_VERSION >= QT_VERSION_CHECK(5, 18, 41)
0016 #include <Akonadi/Collection>
0017 #include <Akonadi/ItemFetchScope>
0018 #include <Akonadi/RecursiveItemFetchJob>
0019 #else
0020 #include <AkonadiCore/RecursiveItemFetchJob>
0021 #include <AkonadiCore/ItemFetchScope>
0022 #include <AkonadiCore/Collection>
0023 #endif
0024 
0025 #include <KContacts/Addressee>
0026 #include <QRegularExpression>
0027 #endif
0028 
0029 MyMoneyContact::MyMoneyContact(QObject *parent) : QObject(parent)
0030 {
0031 }
0032 
0033 bool MyMoneyContact::ownerExists() const
0034 {
0035 #ifdef ENABLE_ADDRESSBOOK
0036     KIdentityManagement::IdentityManager im;
0037     KIdentityManagement::Identity id = im.defaultIdentity();
0038     return !id.isNull();
0039 #else
0040     return false;
0041 #endif
0042 }
0043 
0044 QString MyMoneyContact::ownerEmail() const
0045 {
0046 #ifdef ENABLE_ADDRESSBOOK
0047     KIdentityManagement::IdentityManager im;
0048     KIdentityManagement::Identity id = im.defaultIdentity();
0049     return id.primaryEmailAddress();
0050 #else
0051     return QString();
0052 #endif
0053 }
0054 
0055 QString MyMoneyContact::ownerFullName() const
0056 {
0057 #ifdef ENABLE_ADDRESSBOOK
0058     KIdentityManagement::IdentityManager im;
0059     KIdentityManagement::Identity id = im.defaultIdentity();
0060     return id.fullName();
0061 #else
0062     return QString();
0063 #endif
0064 }
0065 
0066 void MyMoneyContact::fetchContact(const QString &email)
0067 {
0068 #ifdef ENABLE_ADDRESSBOOK
0069     static const QRegularExpression re(".+@.+");
0070     if (!re.match(email).hasMatch()) {
0071         ContactData contact;
0072         Q_EMIT contactFetched(contact);
0073     } else {
0074         // fetch the contact data
0075         Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob(Akonadi::Collection::root(), QStringList{KContacts::Addressee::mimeType()});
0076         job->fetchScope().fetchFullPayload();
0077         job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
0078         job->setProperty("MyMoneyContact_email", email);
0079         connect(job, &Akonadi::RecursiveItemFetchJob::result, this, &MyMoneyContact::searchContactResult);
0080         job->start();
0081     }
0082 #else
0083     Q_UNUSED(email);
0084     ContactData contact;
0085     Q_EMIT contactFetched(contact);
0086 #endif
0087 }
0088 
0089 void MyMoneyContact::searchContactResult(KJob *job)
0090 {
0091 #ifdef ENABLE_ADDRESSBOOK
0092     const Akonadi::RecursiveItemFetchJob *contactJob = qobject_cast<Akonadi::RecursiveItemFetchJob*>(job);
0093     Akonadi::Item::List items;
0094     if (contactJob)
0095         items = contactJob->items();
0096     ContactData contactData;
0097     contactData.email = job->property("MyMoneyContact_email").toString();
0098     for (const Akonadi::Item& item : qAsConst(items)) {
0099         const KContacts::Addressee &contact = item.payload<KContacts::Addressee>();
0100         if (contact.emails().contains(contactData.email)) {
0101             KContacts::PhoneNumber phone;
0102             const KContacts::PhoneNumber::List phones = contact.phoneNumbers();
0103             if (phones.count() == 1)
0104                 phone = phones.first();
0105             else {
0106                 const QList<KContacts::PhoneNumber::Type> typesList = {
0107                     KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Pref,
0108                     KContacts::PhoneNumber::Work,
0109                     KContacts::PhoneNumber::Home | KContacts::PhoneNumber::Pref,
0110                     KContacts::PhoneNumber::Home,
0111                 };
0112                 for (const auto& type : typesList) {
0113                     for (const auto& phn : phones) {
0114                         if (phn.type() & type) {
0115                             phone = phn;
0116                             break;
0117                         }
0118                     }
0119                     if (!phone.isEmpty())
0120                         break;
0121                 }
0122             }
0123             if (phone.isEmpty() && !phones.isEmpty())
0124                 phone = phones.first();
0125 
0126             contactData.phoneNumber = phone.number();
0127             KContacts::Address address;
0128             const KContacts::Address::List addresses = contact.addresses();
0129             if (addresses.count() == 1)
0130                 address = addresses.first();
0131             else {
0132                 const QList<KContacts::Address::Type> typesList = {
0133                     KContacts::Address::Work | KContacts::Address::Pref,
0134                     KContacts::Address::Work,
0135                     KContacts::Address::Home | KContacts::Address::Pref,
0136                     KContacts::Address::Home,
0137                 };
0138                 for (const auto& type : typesList) {
0139                     for (const auto& addr : addresses) {
0140                         if (addr.type() & type) {
0141                             address = addr;
0142                             break;
0143                         }
0144                     }
0145                     if (!address.isEmpty())
0146                         break;
0147                 }
0148             }
0149             if (address.isEmpty() && !addresses.isEmpty())
0150                 address = addresses.first();
0151 
0152             contactData.street = address.street();
0153             contactData.locality = address.locality();
0154             contactData.country = address.country();
0155             contactData.region = address.region();
0156             contactData.postalCode = address.postalCode();
0157             break;
0158         }
0159     }
0160     Q_EMIT contactFetched(contactData);
0161 #else
0162     Q_UNUSED(job);
0163 #endif
0164 }