KPeople

persondata.cpp
1/*
2 SPDX-FileCopyrightText: 2013 David Edmundson (davidedmundson@kde.org)
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "persondata.h"
8
9#include "backends/abstractcontact.h"
10#include "backends/abstracteditablecontact.h"
11#include "backends/basepersonsdatasource.h"
12#include "backends/contactmonitor.h"
13#include "metacontact_p.h"
14#include "personmanager_p.h"
15#include "personpluginmanager.h"
16
17#include "kpeople_debug.h"
18
19#include <QStandardPaths>
20#include <QUrl>
21
22namespace KPeople
23{
24class PersonDataPrivate
25{
26public:
27 QStringList contactUris;
28 MetaContact metaContact;
30};
31}
32
33using namespace KPeople;
34
36 : QObject(parent)
37 , d_ptr(new PersonDataPrivate)
38{
40
41 if (id.isEmpty()) {
42 return;
43 }
44
46 // query DB
47 if (id.startsWith(QLatin1String("kpeople://"))) {
48 personUri = id;
49 } else {
50 personUri = PersonManager::instance()->personUriForContact(id);
51 }
52
53 if (personUri.isEmpty()) {
54 d->contactUris = QStringList() << id;
55 } else {
56 d->contactUris = PersonManager::instance()->contactsForPersonUri(personUri);
57 }
58
60 for (const QString &contactUri : std::as_const(d->contactUris)) {
61 // load the correct data source for this contact ID
62 const QString sourceId = QUrl(contactUri).scheme();
63 Q_ASSERT(!sourceId.isEmpty());
64 BasePersonsDataSource *dataSource = PersonPluginManager::dataSource(sourceId);
65 if (dataSource) {
66 ContactMonitorPtr cw = dataSource->contactMonitor(contactUri);
67 d->watchers << cw;
68
69 // if the data source already has the contact set it already
70 // if not it will be loaded when the contactChanged signal is emitted
71 if (cw->contact()) {
72 contacts[contactUri] = cw->contact();
73 }
74 connect(cw.data(), SIGNAL(contactChanged()), SLOT(onContactChanged()));
75 } else {
76 qCWarning(KPEOPLE_LOG) << "error: creating PersonData for unknown contact" << contactUri << id;
77 }
78 }
79
80 if (personUri.isEmpty() && contacts.size() == 1) {
81 d->metaContact = MetaContact(id, contacts.first());
82 } else {
83 d->metaContact = MetaContact(personUri, contacts);
84 }
85}
86
87PersonData::~PersonData()
88{
89 delete d_ptr;
90}
91
93{
94 Q_D(const PersonData);
95 return !d->metaContact.id().isEmpty();
96}
97
99{
100 Q_D(const PersonData);
101 return d->metaContact.id();
102}
103
105{
106 Q_D(const PersonData);
107 return d->metaContact.contactUris();
108}
109
110void PersonData::onContactChanged()
111{
113
114 ContactMonitor *watcher = qobject_cast<ContactMonitor *>(sender());
115 if (!watcher->contact()) {
116 d->metaContact.removeContact(watcher->contactUri());
117 } else if (d->metaContact.contactUris().contains(watcher->contactUri())) {
118#ifdef __GNUC__
119#warning probably not needed anymore
120#endif
121 d->metaContact.updateContact(watcher->contactUri(), watcher->contact());
122 } else {
123 d->metaContact.insertContact(watcher->contactUri(), watcher->contact());
124 }
126}
127
128QPixmap PersonData::photo() const
129{
130 QPixmap avatar;
131
133 if (pic.canConvert<QImage>()) {
134 avatar = QPixmap::fromImage(pic.value<QImage>());
135 } else if (pic.canConvert<QUrl>()) {
136 avatar = QPixmap(pic.toUrl().toLocalFile());
137 }
138
139 if (avatar.isNull()) {
140 static QString defaultAvatar = QStringLiteral(":/org.kde.kpeople/pixmaps/dummy_avatar.png");
141 avatar = QPixmap(defaultAvatar);
142 }
143 return avatar;
144}
146{
147 Q_D(const PersonData);
148 return d->metaContact.personAddressee()->customProperty(key);
149}
150
152{
154 auto contact = dynamic_cast<AbstractEditableContact *>(d->metaContact.personAddressee().data());
155
156 return contact && contact->setCustomProperty(key, value);
157}
158
159QString PersonData::presenceIconName() const
160{
161 QString contactPresence = contactCustomProperty(QStringLiteral("telepathy-presence")).toString();
162 return KPeople::iconNameForPresenceString(contactPresence);
163}
164
165QString PersonData::name() const
166{
168}
169
174
179
184
186{
187 // We might want to cache it eventually?
188
190 QStringList ret;
191 for (const QVariant &g : groups) {
192 Q_ASSERT(g.canConvert<QString>());
193 ret += g.toString();
194 }
195 ret.removeDuplicates();
196 return ret;
197}
198
200{
202 QStringList ret;
203 for (const QVariant &e : emails) {
204 Q_ASSERT(e.canConvert<QString>());
205 ret += e.toString();
206 }
207 ret.removeDuplicates();
208 return ret;
209}
210
212{
213 Q_D(const PersonData);
214 return dynamic_cast<const AbstractEditableContact *>(d->metaContact.personAddressee().constData());
215}
216
217#include "moc_persondata.cpp"
static const QString NameProperty
String property representing the display name of the contact.
static const QString EmailProperty
String property representing the preferred name of the contact.
static const QString AllEmailsProperty
QVariantList property that lists the emails the contact has.
static const QString PresenceProperty
String property representing the IM presence of the contact.
static const QString GroupsProperty
QVariantList property that lists the groups the contacts belongs to.
static const QString PictureProperty
QUrl or QPixmap property representing the contacts' avatar.
This class loads data for a single contact from a datasource.
QString contactUri() const
The ID of the contact being loaded.
AbstractContact::Ptr contact() const
The currently loaded information on this contact.
Allows to query the information about a given person.
Definition persondata.h:35
QStringList allEmails() const
Returns all e-mail addresses from the person.
Q_SCRIPTABLE bool setContactCustomProperty(const QString &key, const QVariant &value)
Sends a desired value for the contact according to the key.
void dataChanged()
One of the contact sources has changed.
Q_SCRIPTABLE QVariant contactCustomProperty(const QString &key) const
QString personUri() const
Returns the person's id.
PersonData(const QString &id, QObject *parent=nullptr)
Creates a Person object from a given ID.
QString presence() const
Returns the contact's online presence.
QStringList groups() const
Returns all groups the person is in.
bool isValid() const
Returns true if this PersonData is mapped to some existing contact.
QUrl pictureUrl() const
Returns a the url of the picture that represents the contact.
QStringList contactUris() const
Returns a list of contact ids that identify the PersonData instance.
QString email() const
Returns the contact's preferred email address.
T & first()
size_type size() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * sender() const const
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
bool isNull() const const
T * data() const const
bool isEmpty() const const
qsizetype removeDuplicates()
QString scheme() const const
QString toLocalFile() const const
bool canConvert() const const
QList< QVariant > toList() const const
QString toString() const const
QUrl toUrl() const const
T value() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:12:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.