1 /*
2  * Copyright 2014-2015  Cristian Oneț <onet.cristian@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <config-kmymoney.h>
19 
20 #include "mymoneycontact.h"
21 
22 #ifdef ENABLE_ADDRESSBOOK
23 #include <KIdentityManagement/Identity>
24 #include <KIdentityManagement/IdentityManager>
25 #include <akonadi_version.h>
26 
27 #if AKONADI_VERSION >= QT_VERSION_CHECK(5, 18, 41)
28 #include <Akonadi/Collection>
29 #include <Akonadi/ItemFetchScope>
30 #include <Akonadi/RecursiveItemFetchJob>
31 #else
32 #include <AkonadiCore/RecursiveItemFetchJob>
33 #include <AkonadiCore/ItemFetchScope>
34 #include <AkonadiCore/Collection>
35 #endif
36 
37 #include <KContacts/Addressee>
38 #include <QRegularExpression>
39 #endif
40 
MyMoneyContact(QObject * parent)41 MyMoneyContact::MyMoneyContact(QObject *parent) : QObject(parent)
42 {
43 }
44 
ownerExists() const45 bool MyMoneyContact::ownerExists() const
46 {
47 #ifdef ENABLE_ADDRESSBOOK
48   KIdentityManagement::IdentityManager im;
49   KIdentityManagement::Identity id = im.defaultIdentity();
50   return !id.isNull();
51 #else
52   return false;
53 #endif
54 }
55 
ownerEmail() const56 QString MyMoneyContact::ownerEmail() const
57 {
58 #ifdef ENABLE_ADDRESSBOOK
59   KIdentityManagement::IdentityManager im;
60   KIdentityManagement::Identity id = im.defaultIdentity();
61   return id.primaryEmailAddress();
62 #else
63   return QString();
64 #endif
65 }
66 
ownerFullName() const67 QString MyMoneyContact::ownerFullName() const
68 {
69 #ifdef ENABLE_ADDRESSBOOK
70   KIdentityManagement::IdentityManager im;
71   KIdentityManagement::Identity id = im.defaultIdentity();
72   return id.fullName();
73 #else
74   return QString();
75 #endif
76 }
77 
fetchContact(const QString & email)78 void MyMoneyContact::fetchContact(const QString &email)
79 {
80 #ifdef ENABLE_ADDRESSBOOK
81   QRegularExpression re(".+@.+");
82   if (!re.match(email).hasMatch()) {
83     ContactData contact;
84     emit contactFetched(contact);
85   } else {
86     // fetch the contact data
87     Akonadi::RecursiveItemFetchJob *job = new Akonadi::RecursiveItemFetchJob(Akonadi::Collection::root(), QStringList() << KContacts::Addressee::mimeType());
88     job->fetchScope().fetchFullPayload();
89     job->fetchScope().setAncestorRetrieval(Akonadi::ItemFetchScope::Parent);
90     job->setProperty("MyMoneyContact_email", email);
91     connect(job, SIGNAL(result(KJob*)), this, SLOT(searchContactResult(KJob*)));
92     job->start();
93   }
94 #else
95   Q_UNUSED(email);
96   ContactData contact;
97   emit contactFetched(contact);
98 #endif
99 }
100 
searchContactResult(KJob * job)101 void MyMoneyContact::searchContactResult(KJob *job)
102 {
103 #ifdef ENABLE_ADDRESSBOOK
104   const Akonadi::RecursiveItemFetchJob *contactJob = qobject_cast<Akonadi::RecursiveItemFetchJob*>(job);
105   Akonadi::Item::List items;
106   if (contactJob)
107     items = contactJob->items();
108   ContactData contactData;
109   contactData.email = job->property("MyMoneyContact_email").toString();
110   foreach (const Akonadi::Item &item, items) {
111     const KContacts::Addressee &contact = item.payload<KContacts::Addressee>();
112     if (contact.emails().contains(contactData.email)) {
113       KContacts::PhoneNumber phone;
114       KContacts::PhoneNumber::List phones = contact.phoneNumbers();
115       if (phones.count() == 1)
116         phone = phones.first();
117       else {
118         QList<KContacts::PhoneNumber::Type> typesList = {KContacts::PhoneNumber::Work | KContacts::PhoneNumber::Pref,
119                                                          KContacts::PhoneNumber::Work,
120                                                          KContacts::PhoneNumber::Home | KContacts::PhoneNumber::Pref,
121                                                          KContacts::PhoneNumber::Home};
122         foreach (auto type,  typesList) {
123           foreach (auto phn, phones) {
124             if (phn.type() & type) {
125               phone = phn;
126               break;
127             }
128           }
129           if (!phone.isEmpty())
130             break;
131         }
132       }
133       if (phone.isEmpty() && !phones.isEmpty())
134         phone = phones.first();
135 
136       contactData.phoneNumber = phone.number();
137       KContacts::Address address;
138       KContacts::Address::List addresses = contact.addresses();
139       if (addresses.count() == 1)
140         address = addresses.first();
141       else {
142         QList<KContacts::Address::Type> typesList = {KContacts::Address::Work | KContacts::Address::Pref,
143                                                      KContacts::Address::Work,
144                                                      KContacts::Address::Home | KContacts::Address::Pref,
145                                                      KContacts::Address::Home};
146         foreach (auto type,  typesList) {
147           foreach (auto addr, addresses) {
148             if (addr.type() & type) {
149               address = addr;
150               break;
151             }
152           }
153           if (!address.isEmpty())
154             break;
155         }
156       }
157       if (address.isEmpty() && !addresses.isEmpty())
158         address = addresses.first();
159 
160       contactData.street = address.street();
161       contactData.locality = address.locality();
162       contactData.country = address.country();
163       contactData.region = address.region();
164       contactData.postalCode = address.postalCode();
165       break;
166     }
167   }
168   emit contactFetched(contactData);
169 #else
170   Q_UNUSED(job);
171 #endif
172 }
173