1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
6
* Tomahawk is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Tomahawk is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
20
#include "AvatarManager.h"
22
#include "utils/TomahawkUtils.h"
23
#include "utils/Logger.h"
25
#include <jreen/vcard.h>
26
#include <jreen/vcardupdate.h>
27
#include <jreen/presence.h>
28
#include <jreen/iqreply.h>
31
#include <QCryptographicHash>
35
AvatarManager::AvatarManager( Jreen::Client* client )
36
: m_cacheDir( TomahawkUtils::appDataDir().absolutePath().append( "/jreen/" ) )
39
m_cachedAvatars = m_cacheDir.entryList();
41
connect( m_client, SIGNAL( serverFeaturesReceived( QSet<QString> ) ), SLOT( onNewConnection() ) );
42
connect( m_client, SIGNAL( presenceReceived( Jreen::Presence ) ), SLOT( onNewPresence( Jreen::Presence ) ) );
43
connect( m_client, SIGNAL( iqReceived( Jreen::IQ ) ), SLOT( onNewIq( Jreen::IQ ) ) );
45
connect( this, SIGNAL( newAvatar( QString ) ), SLOT( onNewAvatar( QString ) ) );
49
AvatarManager::~AvatarManager()
55
AvatarManager::onNewConnection()
57
fetchVCard( m_client->jid().bare() );
62
AvatarManager::fetchVCard( const QString& jid )
64
Jreen::IQ iq( Jreen::IQ::Get, jid );
65
iq.addExtension( new Jreen::VCard() );
66
Jreen::IQReply *reply = m_client->send( iq );
68
connect( reply, SIGNAL( received( Jreen::IQ ) ), SLOT( onNewIq( Jreen::IQ ) ) );
73
AvatarManager::onNewPresence( const Jreen::Presence& presence )
75
if ( presence.error() )
80
Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
83
// qDebug() << "vcard: found update for" << presence.from().full();
84
if ( !isCached( update->photoHash() ) )
86
// qDebug() << presence.from().full() << "vcard: photo not cached, starting request..." << update->photoHash();
87
fetchVCard( presence.from().bare() );
91
// qDebug() << presence.from().full() << "vcard: photo already cached no request necessary " << update->photoHash();
92
m_JidsAvatarHashes.insert( update->photoHash(), presence.from().bare() );
94
if ( !this->avatar( presence.from().bare() ).isNull() )
95
emit newAvatar( presence.from().bare() );
100
//TODO: do we want this? might fetch avatars for broken clients
101
fetchVCard( presence.from().bare() );
107
AvatarManager::onNewIq( const Jreen::IQ& iq )
109
Jreen::VCard::Ptr vcard = iq.payload<Jreen::VCard>();
113
// qDebug() << Q_FUNC_INFO << "Got vcard from " << iq.from().full();
115
QString id = iq.from().full();
118
const Jreen::VCard::Photo &photo = vcard->photo();
119
if ( !photo.data().isEmpty() )
121
// qDebug() << "vcard: got photo data" << id;
123
avatarHash = QCryptographicHash::hash( photo.data(), QCryptographicHash::Sha1 ).toHex();
125
if ( !m_cacheDir.exists() )
126
m_cacheDir.mkpath( avatarDir( avatarHash ).absolutePath() );
128
QFile file( avatarPath( avatarHash ) );
129
if ( file.open( QIODevice::WriteOnly ) )
131
file.write( photo.data() );
135
m_cachedAvatars.append( avatarHash );
136
m_JidsAvatarHashes.insert( avatarHash, iq.from().bare() );
138
Q_ASSERT( !this->avatar( iq.from().bare() ).isNull() );
139
emit newAvatar( iq.from().bare() );
143
if ( m_client->jid().bare() == id )
145
Jreen::Presence presence = m_client->presence();
146
Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
147
if ( update->photoHash() != avatarHash )
149
update->setPhotoHash( avatarHash );
150
m_client->send( presence );
158
AvatarManager::avatar( const QString& jid ) const
160
if ( isCached( avatarHash( jid ) ) )
162
return QPixmap( avatarPath( avatarHash( jid ) ) );
172
AvatarManager::avatarHash( const QString& jid ) const
174
//qDebug() << Q_FUNC_INFO << jid << m_JidsAvatarHashes.key( jid );
175
return m_JidsAvatarHashes.key( jid );
180
AvatarManager::avatarDir( const QString& /* avatarHash */ ) const
187
AvatarManager::avatarPath( const QString& avatarHash ) const
189
Q_ASSERT( !avatarHash.contains( "@" ) );
190
return avatarDir( avatarHash ).absoluteFilePath( avatarHash );
195
AvatarManager::isCached( const QString& avatarHash ) const
197
return m_cachedAvatars.contains( avatarHash );
202
AvatarManager::onNewAvatar( const QString& /* jid */ )
204
// qDebug() << Q_FUNC_INFO << "Found new Avatar..." << jid;