21 #include <TelepathyQt/ContactManager>
22 #include <TelepathyQt/Connection>
23 #include <TelepathyQt/ContactCapabilities>
24 #include <TelepathyQt/AvatarData>
25 #include <TelepathyQt/Utils>
29 #include <QPixmapCache>
31 #include <KIconLoader>
32 #include <KConfigGroup>
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)
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()));
53 if (! manager()->connection()) {
56 Tp::ConnectionPtr connection = manager()->connection();
59 capabilities(), connection->cmName());
61 connection->selfContact()->capabilities(), connection->cmName());
62 return contactCanStreamAudio && selfCanStreamAudio;
69 if (! manager()->connection()) {
72 Tp::ConnectionPtr connection = manager()->connection();
75 capabilities(), connection->cmName());
77 connection->selfContact()->capabilities(), connection->cmName());
78 return contactCanStreamVideo && selfCanStreamVideo;
86 if (! manager()->connection()) {
89 if (manager()->connection()) {
90 bool contactCanHandleFiles = capabilities().fileTransfers();
91 bool selfCanHandleFiles = manager()->connection()->selfContact()->capabilities().fileTransfers();
92 return contactCanHandleFiles && selfCanHandleFiles;
102 if (Tp::Contact::presence().type() == Tp::ConnectionPresenceTypeOffline) {
103 return QStringList();
106 return Tp::Contact::clientTypes();
114 if (!QPixmapCache::find(keyCache(), avatar)){
115 QString file = avatarData().fileName;
118 if (file.isEmpty()) {
119 KConfig config(QLatin1String(
"ktelepathy-avatarsrc"));
120 KConfigGroup avatarTokenGroup = config.group(
id());
121 QString avatarToken = avatarTokenGroup.readEntry(QLatin1String(
"avatarToken"));
123 if (!avatarToken.isEmpty()) {
124 avatar.load(buildAvatarPath(avatarToken));
131 if (avatar.isNull()) {
132 avatar = KIconLoader::global()->loadIcon(QLatin1String(
"im-user"), KIconLoader::NoGroup, 96);
136 if (presence().type() == Tp::ConnectionPresenceTypeOffline) {
137 avatarToGray(avatar);
141 QPixmapCache::insert(keyCache(), avatar);
147 void KTp::Contact::avatarToGray(QPixmap &avatar)
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));
157 avatar = avatar.fromImage(image);
158 avatar.setAlphaChannel(alpha);
161 QString KTp::Contact::keyCache()
const
163 return id() + (presence().type() == Tp::ConnectionPresenceTypeOffline ? QLatin1String(
"-offline") : QLatin1String(
"-online"));
166 QString KTp::Contact::buildAvatarPath(
const QString &avatarToken)
168 QString cacheDir = QString::fromLatin1(qgetenv(
"XDG_CACHE_HOME"));
169 if (cacheDir.isEmpty()) {
170 cacheDir = QString::fromLatin1(
"%1/.cache").arg(QLatin1String(qgetenv(
"HOME")));
173 if (manager().isNull()) {
177 if (manager()->connection().isNull()) {
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());
185 QString avatarFileName = QString::fromLatin1(
"%1/%2").arg(path).arg(Tp::escapeAsIdentifier(avatarToken));
187 return avatarFileName;
190 void KTp::Contact::invalidateAvatarCache()
192 QPixmapCache::remove(
id() + QLatin1String(
"-offline"));
193 QPixmapCache::remove(
id() + QLatin1String(
"-online"));