• Skip to content
  • Skip to link menu
KDE API Documentation - persondata.cpp Source File (KPeople)
  • KDE Home
  • Contact Us
 

KPeople

  • frameworks
  • kpeople
  • src
persondata.cpp
1 /*
2  Copyright (C) 2013 David Edmundson (davidedmundson@kde.org)
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "persondata.h"
20 
21 #include "metacontact_p.h"
22 #include "personmanager_p.h"
23 #include "personpluginmanager_p.h"
24 #include "backends/basepersonsdatasource.h"
25 #include "backends/contactmonitor.h"
26 #include "backends/abstractcontact.h"
27 
28 #include <QUrl>
29 #include <QDebug>
30 #include <QStandardPaths>
31 
32 namespace KPeople
33 {
34 class PersonDataPrivate
35 {
36 public:
37  QStringList contactUris;
38  MetaContact metaContact;
39  QList<ContactMonitorPtr> watchers;
40 };
41 }
42 
43 using namespace KPeople;
44 
45 KPeople::PersonData::PersonData(const QString &id, QObject *parent):
46  QObject(parent),
47  d_ptr(new PersonDataPrivate)
48 {
49  Q_D(PersonData);
50 
51  QString personUri;
52  //query DB
53  if (id.startsWith(QLatin1String("kpeople://"))) {
54  personUri = id;
55  } else {
56  personUri = PersonManager::instance()->personUriForContact(id);
57  }
58 
59  if (personUri.isEmpty()) {
60  d->contactUris = QStringList() << id;
61  } else {
62  d->contactUris = PersonManager::instance()->contactsForPersonUri(personUri);
63  }
64 
65  QMap<QString, AbstractContact::Ptr> contacts;
66  Q_FOREACH (const QString &contactUri, d->contactUris) {
67  //load the correct data source for this contact ID
68  const QString sourceId = contactUri.left(contactUri.indexOf(QStringLiteral("://")));
69  BasePersonsDataSource *dataSource = PersonPluginManager::dataSource(sourceId);
70  if (dataSource) {
71  ContactMonitorPtr cw = dataSource->contactMonitor(contactUri);
72  d->watchers << cw;
73 
74  //if the data source already has the contact set it already
75  //if not it will be loaded when the contactChanged signal is emitted
76  if (cw->contact()) {
77  contacts[contactUri] = cw->contact();
78  }
79  connect(cw.data(), SIGNAL(contactChanged()), SLOT(onContactChanged()));
80  } else
81  qWarning() << "error: creating PersonData for unknown contact" << contactUri << id;
82  }
83 
84  d->metaContact = MetaContact(personUri, contacts);
85 }
86 
87 PersonData::~PersonData()
88 {
89  delete d_ptr;
90 }
91 
92 QString PersonData::personUri() const
93 {
94  Q_D(const PersonData);
95  return d->metaContact.id();
96 }
97 
98 QStringList PersonData::contactUris() const
99 {
100  Q_D(const PersonData);
101  return d->metaContact.contactUris();
102 }
103 
104 void PersonData::onContactChanged()
105 {
106  Q_D(PersonData);
107 
108  ContactMonitor *watcher = qobject_cast<ContactMonitor *>(sender());
109  if (d->metaContact.contactUris().contains(watcher->contactUri())) {
110 #warning probably not needed anymore
111  d->metaContact.updateContact(watcher->contactUri(), watcher->contact());
112  } else {
113  d->metaContact.insertContact(watcher->contactUri(), watcher->contact());
114  }
115  Q_EMIT dataChanged();
116 }
117 
118 QPixmap PersonData::photo() const
119 {
120  Q_D(const PersonData);
121  QPixmap avatar;
122 
123  QVariant pic = contactCustomProperty(AbstractContact::PictureProperty);
124  if (pic.canConvert<QImage>()) {
125  avatar = QPixmap::fromImage(pic.value<QImage>());
126  } else if (pic.canConvert<QUrl>()) {
127  avatar = QPixmap(pic.toUrl().toLocalFile());
128  }
129 
130  if (avatar.isNull()) {
131  static QString defaultAvatar = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kpeople/dummy_avatar.png"));
132  avatar = QPixmap(defaultAvatar);
133  }
134  return avatar;
135 }
136 QVariant PersonData::contactCustomProperty(const QString &key) const
137 {
138  Q_D(const PersonData);
139  return d->metaContact.personAddressee()->customProperty(key);
140 }
141 
142 QString PersonData::presenceIconName() const
143 {
144  Q_D(const PersonData);
145  QString contactPresence = contactCustomProperty(QStringLiteral("telepathy-presence")).toString();
146  return KPeople::iconNameForPresenceString(contactPresence);
147 }
148 
149 QString PersonData::name() const
150 {
151  return contactCustomProperty(AbstractContact::NameProperty).toString();
152 }
153 
154 QString PersonData::presence() const
155 {
156  return contactCustomProperty(AbstractContact::PresenceProperty).toString();
157 }
158 
159 QUrl PersonData::pictureUrl() const
160 {
161  return contactCustomProperty(AbstractContact::PictureProperty).toUrl();
162 }
163 
164 QString PersonData::email() const
165 {
166  return contactCustomProperty(AbstractContact::EmailProperty).toString();
167 }
168 
169 QStringList PersonData::groups() const
170 {
171 // We might want to cache it eventually?
172 
173  QVariantList groups = contactCustomProperty(AbstractContact::GroupsProperty).toList();
174  QStringList ret;
175  Q_FOREACH (const QVariant &g, groups) {
176  Q_ASSERT(g.canConvert<QString>());
177  ret += g.toString();
178  }
179  ret.removeDuplicates();
180  return ret;
181 }
182 
183 QStringList PersonData::allEmails() const
184 {
185  QVariantList emails = contactCustomProperty(AbstractContact::AllEmailsProperty).toList();
186  QStringList ret;
187  Q_FOREACH (const QVariant &e, emails) {
188  Q_ASSERT(e.canConvert<QString>());
189  ret += e.toString();
190  }
191  ret.removeDuplicates();
192  return ret;
193 }
194 
KPeople::PersonData::presenceIconName
QString presenceIconName() const
KPeople::AbstractContact::PictureProperty
static const QString PictureProperty
QUrl or QPixmap property representing the contacts' avatar.
Definition: abstractcontact.h:62
KPeople::PersonData::groups
QStringList groups() const
Returns all groups the person is in.
Definition: persondata.cpp:169
KPeople::ContactMonitor::contactUri
QString contactUri() const
The ID of the contact being loaded.
Definition: contactmonitor.cpp:59
KPeople::PersonData::name
QString name() const
KPeople::AbstractContact::AllEmailsProperty
static const QString AllEmailsProperty
QVariantList property that lists the emails the contact has.
Definition: abstractcontact.h:68
KPeople::ContactMonitor
This class loads data for a single contact from a datasource.
Definition: contactmonitor.h:42
KPeople::PersonData
Allows to query the information about a given person.
Definition: persondata.h:44
KPeople::AbstractContact::NameProperty
static const QString NameProperty
String property representing the display name of the contact.
Definition: abstractcontact.h:48
KPeople::ContactMonitor::contact
AbstractContact::Ptr contact() const
The currently loaded information on this contact.
Definition: contactmonitor.cpp:52
KPeople
The KPeople namespace contains all the classes for Libkpeople.
KPeople::PersonData::photo
QPixmap photo() const
KPeople::PersonData::contactCustomProperty
QVariant contactCustomProperty(const QString &key) const
Definition: persondata.cpp:136
KPeople::PersonData::presence
QString presence() const
Returns the contact's online presence.
Definition: persondata.cpp:154
KPeople::AbstractContact::PresenceProperty
static const QString PresenceProperty
String property representing the IM presence of the contact.
Definition: abstractcontact.h:57
KPeople::iconNameForPresenceString
KPEOPLE_EXPORT QString iconNameForPresenceString(const QString &presenceName)
Return a QPixmap for a TP presence string.
Definition: global.cpp:38
KPeople::PersonData::personUri
QString personUri() const
Returns the person's id.
Definition: persondata.cpp:92
KPeople::PersonData::email
QString email() const
Returns the contact's preferred email address.
Definition: persondata.cpp:164
KPeople::PersonData::allEmails
QStringList allEmails() const
Returns all e-mail addresses from the person.
Definition: persondata.cpp:183
KPeople::PersonData::dataChanged
void dataChanged()
One of the contact sources has changed.
KPeople::AbstractContact::EmailProperty
static const QString EmailProperty
String property representing the preferred name of the contact.
Definition: abstractcontact.h:51
KPeople::AbstractContact::GroupsProperty
static const QString GroupsProperty
QVariantList property that lists the groups the contacts belongs to.
Definition: abstractcontact.h:65
KPeople::PersonData::pictureUrl
QUrl pictureUrl() const
Returns a the url of the picture that represents the contact.
Definition: persondata.cpp:159
KPeople::PersonData::PersonData
PersonData(const QString &id, QObject *parent=0)
Creates a Person object from a given ID.
Definition: persondata.cpp:45
KPeople::PersonData::contactUris
QStringList contactUris() const
Returns a list of contact ids that identify the PersonData instance.
Definition: persondata.cpp:98
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Fri Feb 13 2015 15:16:39 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPeople

Skip menu "KPeople"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal