KPeople

persondata.cpp
1 /*
2  SPDX-FileCopyrightText: 2013 David Edmundson ([email protected])
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 
22 namespace KPeople
23 {
24 class PersonDataPrivate
25 {
26 public:
27  QStringList contactUris;
28  MetaContact metaContact;
29  QList<ContactMonitorPtr> watchers;
30 };
31 }
32 
33 using namespace KPeople;
34 
36  : QObject(parent)
37  , d_ptr(new PersonDataPrivate)
38 {
39  Q_D(PersonData);
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 : qAsConst(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  if (personUri.isEmpty() && contacts.size() == 1) {
80  d->metaContact = MetaContact(id, contacts.first());
81  } else {
82  d->metaContact = MetaContact(personUri, contacts);
83  }
84 }
85 
86 PersonData::~PersonData()
87 {
88  delete d_ptr;
89 }
90 
91 bool PersonData::isValid() const
92 {
93  Q_D(const PersonData);
94  return !d->metaContact.id().isEmpty();
95 }
96 
98 {
99  Q_D(const PersonData);
100  return d->metaContact.id();
101 }
102 
104 {
105  Q_D(const PersonData);
106  return d->metaContact.contactUris();
107 }
108 
109 void PersonData::onContactChanged()
110 {
111  Q_D(PersonData);
112 
114  if (!watcher->contact()) {
115  d->metaContact.removeContact(watcher->contactUri());
116  } else if (d->metaContact.contactUris().contains(watcher->contactUri())) {
117 #ifdef __GNUC__
118 #warning probably not needed anymore
119 #endif
120  d->metaContact.updateContact(watcher->contactUri(), watcher->contact());
121  } else {
122  d->metaContact.insertContact(watcher->contactUri(), watcher->contact());
123  }
125 }
126 
128 {
129  QPixmap avatar;
130 
132  if (pic.canConvert<QImage>()) {
133  avatar = QPixmap::fromImage(pic.value<QImage>());
134  } else if (pic.canConvert<QUrl>()) {
135  avatar = QPixmap(pic.toUrl().toLocalFile());
136  }
137 
138  if (avatar.isNull()) {
139  static QString defaultAvatar = QStringLiteral(":/org.kde.kpeople/pixmaps/dummy_avatar.png");
140  avatar = QPixmap(defaultAvatar);
141  }
142  return avatar;
143 }
145 {
146  Q_D(const PersonData);
147  return d->metaContact.personAddressee()->customProperty(key);
148 }
149 
151 {
152  Q_D(PersonData);
153  auto contact = dynamic_cast<AbstractEditableContact *>(d->metaContact.personAddressee().data());
154 
155  return contact && contact->setCustomProperty(key, value);
156 }
157 
159 {
160  QString contactPresence = contactCustomProperty(QStringLiteral("telepathy-presence")).toString();
161  return KPeople::iconNameForPresenceString(contactPresence);
162 }
163 
165 {
167 }
168 
170 {
172 }
173 
175 {
177 }
178 
180 {
182 }
183 
185 {
186  // We might want to cache it eventually?
187 
189  QStringList ret;
190  for (const QVariant &g : groups) {
191  Q_ASSERT(g.canConvert<QString>());
192  ret += g.toString();
193  }
194  ret.removeDuplicates();
195  return ret;
196 }
197 
199 {
200  const QVariantList emails = contactCustomProperty(AbstractContact::AllEmailsProperty).toList();
201  QStringList ret;
202  for (const QVariant &e : emails) {
203  Q_ASSERT(e.canConvert<QString>());
204  ret += e.toString();
205  }
206  ret.removeDuplicates();
207  return ret;
208 }
209 
211 {
212  Q_D(const PersonData);
213  return dynamic_cast<const AbstractEditableContact *>(d->metaContact.personAddressee().constData());
214 }
bool canConvert(int targetTypeId) const const
QUrl toUrl() const const
QString presenceIconName() const
static const QString PictureProperty
QUrl or QPixmap property representing the contacts&#39; avatar.
QStringList groups() const
Returns all groups the person is in.
Definition: persondata.cpp:184
QList< QVariant > toList() const const
QObject * sender() const const
PersonData(const QString &id, QObject *parent=nullptr)
Creates a Person object from a given ID.
Definition: persondata.cpp:35
int removeDuplicates()
QString contactUri() const
The ID of the contact being loaded.
QString name() const
Q_SCRIPTABLE bool setContactCustomProperty(const QString &key, const QVariant &value)
Sends a desired value for the contact according to the key.
Definition: persondata.cpp:150
T value() const const
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
static const QString AllEmailsProperty
QVariantList property that lists the emails the contact has.
This class loads data for a single contact from a datasource.
T * data() const const
Allows to query the information about a given person.
Definition: persondata.h:34
static const QString NameProperty
String property representing the display name of the contact.
AbstractContact::Ptr contact() const
The currently loaded information on this contact.
QPixmap photo() const
bool isEmpty() const const
Q_SCRIPTABLE QVariant contactCustomProperty(const QString &key) const
Definition: persondata.cpp:144
QString presence() const
Returns the contact&#39;s online presence.
Definition: persondata.cpp:169
static const QString PresenceProperty
String property representing the IM presence of the contact.
QString personUri() const
Returns the person&#39;s id.
Definition: persondata.cpp:97
QString scheme() const const
QString toLocalFile() const const
bool isNull() const const
bool isValid() const
Returns true if this PersonData is mapped to some existing contact.
Definition: persondata.cpp:91
bool isEditable() const
QString email() const
Returns the contact&#39;s preferred email address.
Definition: persondata.cpp:179
QStringList allEmails() const
Returns all e-mail addresses from the person.
Definition: persondata.cpp:198
void dataChanged()
One of the contact sources has changed.
static const QString EmailProperty
String property representing the preferred name of the contact.
static const QString GroupsProperty
QVariantList property that lists the groups the contacts belongs to.
T & first()
QUrl pictureUrl() const
Returns a the url of the picture that represents the contact.
Definition: persondata.cpp:174
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
QString toString() const const
int size() const const
Q_EMITQ_EMIT
QStringList contactUris() const
Returns a list of contact ids that identify the PersonData instance.
Definition: persondata.cpp:103
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Jun 20 2021 22:45:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.