1 /*
2 * %kadu copyright begin%
3 * Copyright 2012 Wojciech Treter (juzefwt@gmail.com)
4 * Copyright 2011, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
5 * Copyright 2011, 2012, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
6 * %kadu copyright end%
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "services/jabber-vcard-downloader.h"
23 #include "services/jabber-vcard-service.h"
24 #include "services/jabber-vcard-uploader.h"
25 #include "jabber-protocol.h"
26
27 #include "jabber-personal-info-service.h"
28
29 #include "buddies/buddy-storage.h"
30
31 #include <qxmpp/QXmppVCardIq.h>
32
JabberPersonalInfoService(Account account,QObject * parent)33 JabberPersonalInfoService::JabberPersonalInfoService(Account account, QObject *parent) :
34 PersonalInfoService(account, parent)
35 {
36 }
37
~JabberPersonalInfoService()38 JabberPersonalInfoService::~JabberPersonalInfoService()
39 {
40 }
41
setBuddyStorage(BuddyStorage * buddyStorage)42 void JabberPersonalInfoService::setBuddyStorage(BuddyStorage *buddyStorage)
43 {
44 m_buddyStorage = buddyStorage;
45 }
46
setVCardService(JabberVCardService * vCardService)47 void JabberPersonalInfoService::setVCardService(JabberVCardService *vCardService)
48 {
49 VCardService = vCardService;
50 }
51
fetchPersonalInfo(const QString & id)52 void JabberPersonalInfoService::fetchPersonalInfo(const QString &id)
53 {
54 CurrentBuddy = m_buddyStorage->create();
55 if (!VCardService)
56 return;
57
58 JabberVCardDownloader *vCardDownloader = VCardService->createVCardDownloader();
59 if (!vCardDownloader)
60 return;
61
62 connect(vCardDownloader, SIGNAL(vCardDownloaded(bool,QXmppVCardIq)), this, SLOT(vCardDownloaded(bool,QXmppVCardIq)));
63 vCardDownloader->downloadVCard(id);
64 }
65
vCardDownloaded(bool ok,const QXmppVCardIq & vCard)66 void JabberPersonalInfoService::vCardDownloaded(bool ok, const QXmppVCardIq &vCard)
67 {
68 if (!ok)
69 return;
70
71 CurrentBuddy.setNickName(vCard.nickName());
72 CurrentBuddy.setFirstName(vCard.fullName());
73 CurrentBuddy.setFamilyName(vCard.middleName());
74 QDate bday = vCard.birthday();
75 if (bday.isValid() && !bday.isNull())
76 CurrentBuddy.setBirthYear(bday.year());
77
78 if (!vCard.addresses().isEmpty())
79 CurrentBuddy.setCity(vCard.addresses().at(0).locality());
80 if (!vCard.emails().isEmpty())
81 CurrentBuddy.setEmail(vCard.emails().at(0).address());
82 CurrentBuddy.setWebsite(vCard.url());
83
84 emit personalInfoAvailable(CurrentBuddy);
85 }
86
updatePersonalInfo(const QString & id,Buddy buddy)87 void JabberPersonalInfoService::updatePersonalInfo(const QString &id, Buddy buddy)
88 {
89 Q_UNUSED(id);
90
91 if (!VCardService)
92 {
93 emit personalInfoUpdated(false);
94 return;
95 }
96
97 CurrentBuddy = buddy;
98
99 auto vcard = QXmppVCardIq{};
100 vcard.setFullName(CurrentBuddy.firstName());
101 vcard.setNickName(CurrentBuddy.nickName());
102 vcard.setMiddleName(CurrentBuddy.familyName());
103 QDate birthday;
104 birthday.setDate(CurrentBuddy.birthYear(), 1, 1);
105 vcard.setBirthday(birthday);
106
107 auto addr = QXmppVCardAddress{};
108 addr.setLocality(CurrentBuddy.city());
109 vcard.setAddresses({addr});
110
111 auto email = QXmppVCardEmail{};
112 email.setAddress(CurrentBuddy.email());
113 vcard.setEmails({email});
114
115 vcard.setUrl(CurrentBuddy.website());
116
117 auto vCardUploader = VCardService->createVCardUploader();
118 if (!vCardUploader)
119 {
120 emit personalInfoUpdated(false);
121 return;
122 }
123
124 vCardUploader->uploadVCard(vcard);
125 emit personalInfoUpdated(true);
126 }
127
128 #include "moc_jabber-personal-info-service.cpp"
129