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 : 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 
87 PersonData::~PersonData()
88 {
89  delete d_ptr;
90 }
91 
92 bool PersonData::isValid() const
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 
110 void PersonData::onContactChanged()
111 {
112  Q_D(PersonData);
113 
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 
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 {
153  Q_D(PersonData);
154  auto contact = dynamic_cast<AbstractEditableContact *>(d->metaContact.personAddressee().data());
155 
156  return contact && contact->setCustomProperty(key, value);
157 }
158 
160 {
161  QString contactPresence = contactCustomProperty(QStringLiteral("telepathy-presence")).toString();
162  return KPeople::iconNameForPresenceString(contactPresence);
163 }
164 
166 {
168 }
169 
171 {
173 }
174 
176 {
178 }
179 
181 {
183 }
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 {
201  const QVariantList emails = contactCustomProperty(AbstractContact::AllEmailsProperty).toList();
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 }
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:185
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:151
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:145
QString presence() const
Returns the contact&#39;s online presence.
Definition: persondata.cpp:170
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:98
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:92
bool isEditable() const
Q_D(Todo)
QString email() const
Returns the contact&#39;s preferred email address.
Definition: persondata.cpp:180
QStringList allEmails() const
Returns all e-mail addresses from the person.
Definition: persondata.cpp:199
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:175
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:104
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jan 24 2022 22:42:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.