• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
contact.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2012 David Edmundson <kde@davidedmundson.co.uk>
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 St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "contact.h"
20 
21 #include <TelepathyQt/ContactManager>
22 #include <TelepathyQt/Connection>
23 #include <TelepathyQt/ContactCapabilities>
24 #include <TelepathyQt/AvatarData>
25 #include <TelepathyQt/Utils>
26 
27 #include <QBitmap>
28 #include <QPixmap>
29 #include <QPixmapCache>
30 
31 #include <KIconLoader>
32 #include <KConfigGroup>
33 #include <KConfig>
34 
35 #include "capabilities-hack-private.h"
36 
37 KTp::Contact::Contact(Tp::ContactManager *manager, const Tp::ReferencedHandles &handle, const Tp::Features &requestedFeatures, const QVariantMap &attributes)
38  : Tp::Contact(manager, handle, requestedFeatures, attributes)
39 {
40  connect(manager->connection().data(), SIGNAL(destroyed()), SIGNAL(invalidated()));
41  connect(manager->connection().data(), SIGNAL(invalidated(Tp::DBusProxy*,QString,QString)), SIGNAL(invalidated()));
42  connect(this, SIGNAL(avatarTokenChanged(QString)), SLOT(invalidateAvatarCache()));
43  connect(this, SIGNAL(avatarDataChanged(Tp::AvatarData)), SLOT(invalidateAvatarCache()));
44 }
45 
46 KTp::Presence KTp::Contact::presence() const
47 {
48  return KTp::Presence(Tp::Contact::presence());
49 }
50 
51 bool KTp::Contact::audioCallCapability() const
52 {
53  if (! manager()->connection()) {
54  return false;
55  }
56  Tp::ConnectionPtr connection = manager()->connection();
57  if (connection) {
58  bool contactCanStreamAudio = CapabilitiesHackPrivate::audioCalls(
59  capabilities(), connection->cmName());
60  bool selfCanStreamAudio = CapabilitiesHackPrivate::audioCalls(
61  connection->selfContact()->capabilities(), connection->cmName());
62  return contactCanStreamAudio && selfCanStreamAudio;
63  }
64  return false;
65 }
66 
67 bool KTp::Contact::videoCallCapability() const
68 {
69  if (! manager()->connection()) {
70  return false;
71  }
72  Tp::ConnectionPtr connection = manager()->connection();
73  if (connection) {
74  bool contactCanStreamVideo = CapabilitiesHackPrivate::videoCalls(
75  capabilities(), connection->cmName());
76  bool selfCanStreamVideo = CapabilitiesHackPrivate::videoCalls(
77  connection->selfContact()->capabilities(), connection->cmName());
78  return contactCanStreamVideo && selfCanStreamVideo;
79  }
80 
81  return false;
82 }
83 
84 bool KTp::Contact::fileTransferCapability() const
85 {
86  if (! manager()->connection()) {
87  return false;
88  }
89  if (manager()->connection()) {
90  bool contactCanHandleFiles = capabilities().fileTransfers();
91  bool selfCanHandleFiles = manager()->connection()->selfContact()->capabilities().fileTransfers();
92  return contactCanHandleFiles && selfCanHandleFiles;
93  }
94 
95  return false;
96 }
97 
98 QStringList KTp::Contact::clientTypes() const
99 {
100  /* Temporary workaround for upstream bug https://bugs.freedesktop.org/show_bug.cgi?id=55883)
101  * Close https://bugs.kde.org/show_bug.cgi?id=308217 when fixed upstream */
102  if (Tp::Contact::presence().type() == Tp::ConnectionPresenceTypeOffline) {
103  return QStringList();
104  }
105 
106  return Tp::Contact::clientTypes();
107 }
108 
109 QPixmap KTp::Contact::avatarPixmap()
110 {
111  QPixmap avatar;
112 
113  //check pixmap cache for the avatar, if not present, load the avatar
114  if (!QPixmapCache::find(keyCache(), avatar)){
115  QString file = avatarData().fileName;
116 
117  //if contact does not provide path, let's see if we have avatar for the stored token
118  if (file.isEmpty()) {
119  KConfig config(QLatin1String("ktelepathy-avatarsrc"));
120  KConfigGroup avatarTokenGroup = config.group(id());
121  QString avatarToken = avatarTokenGroup.readEntry(QLatin1String("avatarToken"));
122  //only bother loading the pixmap if the token is not empty
123  if (!avatarToken.isEmpty()) {
124  avatar.load(buildAvatarPath(avatarToken));
125  }
126  } else {
127  avatar.load(file);
128  }
129 
130  //if neither above succeeded, we need to load the icon
131  if (avatar.isNull()) {
132  avatar = KIconLoader::global()->loadIcon(QLatin1String("im-user"), KIconLoader::NoGroup, 96);
133  }
134 
135  //if the contact is offline, gray it out
136  if (presence().type() == Tp::ConnectionPresenceTypeOffline) {
137  avatarToGray(avatar);
138  }
139 
140  //insert the contact into pixmap cache for faster lookup
141  QPixmapCache::insert(keyCache(), avatar);
142  }
143 
144  return avatar;
145 }
146 
147 void KTp::Contact::avatarToGray(QPixmap &avatar)
148 {
149  QImage image = avatar.toImage();
150  QPixmap alpha= avatar.alphaChannel();
151  for (int i = 0; i < image.width(); ++i) {
152  for (int j = 0; j < image.height(); ++j) {
153  int colour = qGray(image.pixel(i, j));
154  image.setPixel(i, j, qRgb(colour, colour, colour));
155  }
156  }
157  avatar = avatar.fromImage(image);
158  avatar.setAlphaChannel(alpha);
159 }
160 
161 QString KTp::Contact::keyCache() const
162 {
163  return id() + (presence().type() == Tp::ConnectionPresenceTypeOffline ? QLatin1String("-offline") : QLatin1String("-online"));
164 }
165 
166 QString KTp::Contact::buildAvatarPath(const QString &avatarToken)
167 {
168  QString cacheDir = QString::fromLatin1(qgetenv("XDG_CACHE_HOME"));
169  if (cacheDir.isEmpty()) {
170  cacheDir = QString::fromLatin1("%1/.cache").arg(QLatin1String(qgetenv("HOME")));
171  }
172 
173  if (manager().isNull()) {
174  return QString();
175  }
176 
177  if (manager()->connection().isNull()) {
178  return QString();
179  }
180 
181  Tp::ConnectionPtr conn = manager()->connection();
182  QString path = QString::fromLatin1("%1/telepathy/avatars/%2/%3").
183  arg(cacheDir).arg(conn->cmName()).arg(conn->protocolName());
184 
185  QString avatarFileName = QString::fromLatin1("%1/%2").arg(path).arg(Tp::escapeAsIdentifier(avatarToken));
186 
187  return avatarFileName;
188 }
189 
190 void KTp::Contact::invalidateAvatarCache()
191 {
192  QPixmapCache::remove(id() + QLatin1String("-offline"));
193  QPixmapCache::remove(id() + QLatin1String("-online"));
194 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
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