mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-04 05:07:27 +02:00
* Style cleanup for AvatarManager.
This commit is contained in:
@@ -33,47 +33,51 @@
|
|||||||
#include "utils/Logger.h"
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
|
|
||||||
AvatarManager::AvatarManager(Jreen::Client *client) :
|
AvatarManager::AvatarManager( Jreen::Client* client )
|
||||||
m_cacheDir(TomahawkUtils::appDataDir().absolutePath().append("/jreen/"))
|
: m_cacheDir( TomahawkUtils::appDataDir().absolutePath().append( "/jreen/" ) )
|
||||||
{
|
{
|
||||||
m_client = client;
|
m_client = client;
|
||||||
|
|
||||||
m_cachedAvatars = m_cacheDir.entryList();
|
m_cachedAvatars = m_cacheDir.entryList();
|
||||||
|
|
||||||
connect(m_client, SIGNAL(serverFeaturesReceived(QSet<QString>)), SLOT(onNewConnection()));
|
connect( m_client, SIGNAL( serverFeaturesReceived( QSet<QString> ) ), SLOT( onNewConnection() ) );
|
||||||
connect(m_client, SIGNAL(presenceReceived(Jreen::Presence)), SLOT(onNewPresence(Jreen::Presence)));
|
connect( m_client, SIGNAL( presenceReceived( Jreen::Presence ) ), SLOT( onNewPresence( Jreen::Presence ) ) );
|
||||||
connect(m_client, SIGNAL(iqReceived(Jreen::IQ)), SLOT(onNewIq(Jreen::IQ)));
|
connect( m_client, SIGNAL( iqReceived( Jreen::IQ ) ), SLOT( onNewIq( Jreen::IQ ) ) );
|
||||||
|
|
||||||
connect(this, SIGNAL(newAvatar(QString)), SLOT(onNewAvatar(QString)));
|
connect( this, SIGNAL( newAvatar( QString ) ), SLOT( onNewAvatar( QString ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
AvatarManager::~AvatarManager()
|
AvatarManager::~AvatarManager()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void AvatarManager::onNewConnection()
|
|
||||||
|
void
|
||||||
|
AvatarManager::onNewConnection()
|
||||||
{
|
{
|
||||||
fetchVCard( m_client->jid().bare() );
|
fetchVCard( m_client->jid().bare() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AvatarManager::fetchVCard(const QString &jid)
|
void
|
||||||
|
AvatarManager::fetchVCard( const QString& jid )
|
||||||
{
|
{
|
||||||
// qDebug() << Q_FUNC_INFO;
|
Jreen::IQ iq( Jreen::IQ::Get, jid );
|
||||||
|
iq.addExtension( new Jreen::VCard() );
|
||||||
|
Jreen::IQReply *reply = m_client->send( iq );
|
||||||
|
|
||||||
Jreen::IQ iq(Jreen::IQ::Get, jid );
|
connect( reply, SIGNAL( received( Jreen::IQ ) ), SLOT( onNewIq( Jreen::IQ ) ) );
|
||||||
iq.addExtension(new Jreen::VCard());
|
|
||||||
Jreen::IQReply *reply = m_client->send(iq);
|
|
||||||
connect(reply, SIGNAL(received(Jreen::IQ)), SLOT(onNewIq(Jreen::IQ)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AvatarManager::onNewPresence(const Jreen::Presence& presence)
|
|
||||||
|
void
|
||||||
|
AvatarManager::onNewPresence( const Jreen::Presence& presence )
|
||||||
{
|
{
|
||||||
Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
|
Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
|
||||||
if(update)
|
if ( update )
|
||||||
{
|
{
|
||||||
// qDebug() << "vcard: found update for" << presence.from().full();
|
// qDebug() << "vcard: found update for" << presence.from().full();
|
||||||
if(!isCached(update->photoHash()))
|
if ( !isCached( update->photoHash() ) )
|
||||||
{
|
{
|
||||||
// qDebug() << presence.from().full() << "vcard: photo not cached, starting request..." << update->photoHash();
|
// qDebug() << presence.from().full() << "vcard: photo not cached, starting request..." << update->photoHash();
|
||||||
fetchVCard( presence.from().bare() );
|
fetchVCard( presence.from().bare() );
|
||||||
@@ -84,78 +88,72 @@ void AvatarManager::onNewPresence(const Jreen::Presence& presence)
|
|||||||
m_JidsAvatarHashes.insert( update->photoHash(), presence.from().bare() );
|
m_JidsAvatarHashes.insert( update->photoHash(), presence.from().bare() );
|
||||||
|
|
||||||
if ( !this->avatar( presence.from().bare() ).isNull() )
|
if ( !this->avatar( presence.from().bare() ).isNull() )
|
||||||
emit newAvatar(presence.from().bare());
|
emit newAvatar( presence.from().bare() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// qDebug() << Q_FUNC_INFO << presence.from().full() << "got no statusupdateextension";
|
|
||||||
|
|
||||||
//TODO: do we want this? might fetch avatars for broken clients
|
//TODO: do we want this? might fetch avatars for broken clients
|
||||||
fetchVCard( presence.from().bare() );
|
fetchVCard( presence.from().bare() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AvatarManager::onNewIq(const Jreen::IQ& iq)
|
|
||||||
|
void
|
||||||
|
AvatarManager::onNewIq( const Jreen::IQ& iq )
|
||||||
{
|
{
|
||||||
Jreen::VCard::Ptr vcard = iq.payload<Jreen::VCard>();
|
Jreen::VCard::Ptr vcard = iq.payload<Jreen::VCard>();
|
||||||
if(vcard)
|
if ( vcard )
|
||||||
{
|
{
|
||||||
iq.accept();
|
iq.accept();
|
||||||
|
|
||||||
// qDebug() << Q_FUNC_INFO << "Got vcard from " << iq.from().full();
|
// qDebug() << Q_FUNC_INFO << "Got vcard from " << iq.from().full();
|
||||||
|
|
||||||
QString id = iq.from().full();
|
QString id = iq.from().full();
|
||||||
QString avatarHash;
|
QString avatarHash;
|
||||||
|
|
||||||
const Jreen::VCard::Photo &photo = vcard->photo();
|
const Jreen::VCard::Photo &photo = vcard->photo();
|
||||||
if (!photo.data().isEmpty()) {
|
if ( !photo.data().isEmpty() )
|
||||||
|
{
|
||||||
// qDebug() << "vcard: got photo data" << id;
|
// qDebug() << "vcard: got photo data" << id;
|
||||||
|
|
||||||
avatarHash = QCryptographicHash::hash(photo.data(), QCryptographicHash::Sha1).toHex();
|
avatarHash = QCryptographicHash::hash( photo.data(), QCryptographicHash::Sha1 ).toHex();
|
||||||
|
|
||||||
if (!m_cacheDir.exists())
|
if ( !m_cacheDir.exists() )
|
||||||
m_cacheDir.mkpath( avatarDir( avatarHash ).absolutePath() );
|
m_cacheDir.mkpath( avatarDir( avatarHash ).absolutePath() );
|
||||||
|
|
||||||
|
QFile file( avatarPath( avatarHash ) );
|
||||||
QFile file(avatarPath(avatarHash));
|
if ( file.open( QIODevice::WriteOnly ) )
|
||||||
if (file.open(QIODevice::WriteOnly)) {
|
{
|
||||||
file.write(photo.data());
|
file.write( photo.data() );
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_cachedAvatars.append(avatarHash);
|
m_cachedAvatars.append( avatarHash );
|
||||||
m_JidsAvatarHashes.insert( avatarHash, iq.from().bare() );
|
m_JidsAvatarHashes.insert( avatarHash, iq.from().bare() );
|
||||||
|
|
||||||
Q_ASSERT(!this->avatar(iq.from().bare()).isNull());
|
Q_ASSERT( !this->avatar( iq.from().bare() ).isNull() );
|
||||||
emit newAvatar(iq.from().bare());
|
emit newAvatar( iq.from().bare() );
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// qDebug() << "vcard: got no photo data" << id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// got own presence
|
// got own presence
|
||||||
if ( m_client->jid().bare() == id )
|
if ( m_client->jid().bare() == id )
|
||||||
{
|
{
|
||||||
// qDebug() << Q_FUNC_INFO << "got own vcard";
|
|
||||||
|
|
||||||
Jreen::Presence presence = m_client->presence();
|
Jreen::Presence presence = m_client->presence();
|
||||||
Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
|
Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
|
||||||
if (update->photoHash() != avatarHash)
|
if ( update->photoHash() != avatarHash )
|
||||||
{
|
{
|
||||||
qDebug() << Q_FUNC_INFO << "Updating own presence...";
|
update->setPhotoHash( avatarHash );
|
||||||
|
m_client->send( presence );
|
||||||
update->setPhotoHash(avatarHash);
|
|
||||||
m_client->send(presence);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QPixmap AvatarManager::avatar(const QString &jid) const
|
|
||||||
|
QPixmap
|
||||||
|
AvatarManager::avatar( const QString& jid ) const
|
||||||
{
|
{
|
||||||
if( isCached( avatarHash( jid ) ) )
|
if ( isCached( avatarHash( jid ) ) )
|
||||||
{
|
{
|
||||||
return QPixmap( avatarPath( avatarHash( jid ) ) );
|
return QPixmap( avatarPath( avatarHash( jid ) ) );
|
||||||
}
|
}
|
||||||
@@ -165,29 +163,39 @@ QPixmap AvatarManager::avatar(const QString &jid) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AvatarManager::avatarHash(const QString &jid) const
|
|
||||||
|
QString
|
||||||
|
AvatarManager::avatarHash( const QString& jid ) const
|
||||||
{
|
{
|
||||||
//qDebug() << Q_FUNC_INFO << jid << m_JidsAvatarHashes.key(jid);
|
//qDebug() << Q_FUNC_INFO << jid << m_JidsAvatarHashes.key( jid );
|
||||||
return m_JidsAvatarHashes.key(jid);
|
return m_JidsAvatarHashes.key( jid );
|
||||||
}
|
}
|
||||||
|
|
||||||
QDir AvatarManager::avatarDir(const QString&) const
|
|
||||||
|
QDir
|
||||||
|
AvatarManager::avatarDir( const QString& /* avatarHash */ ) const
|
||||||
{
|
{
|
||||||
return m_cacheDir;
|
return m_cacheDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString AvatarManager::avatarPath(const QString &avatarHash) const
|
|
||||||
|
QString
|
||||||
|
AvatarManager::avatarPath( const QString& avatarHash ) const
|
||||||
{
|
{
|
||||||
Q_ASSERT(!avatarHash.contains("@"));
|
Q_ASSERT( !avatarHash.contains( "@" ) );
|
||||||
return avatarDir(avatarHash).absoluteFilePath(avatarHash);
|
return avatarDir( avatarHash ).absoluteFilePath( avatarHash );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AvatarManager::isCached(const QString &avatarHash) const
|
|
||||||
|
bool
|
||||||
|
AvatarManager::isCached( const QString& avatarHash ) const
|
||||||
{
|
{
|
||||||
return m_cachedAvatars.contains( avatarHash );
|
return m_cachedAvatars.contains( avatarHash );
|
||||||
}
|
}
|
||||||
|
|
||||||
void AvatarManager::onNewAvatar(const QString&)
|
|
||||||
|
void
|
||||||
|
AvatarManager::onNewAvatar( const QString& /* jid */ )
|
||||||
{
|
{
|
||||||
// qDebug() << Q_FUNC_INFO << "Found new Avatar..." << jid;
|
// qDebug() << Q_FUNC_INFO << "Found new Avatar..." << jid;
|
||||||
}
|
}
|
||||||
|
@@ -32,32 +32,32 @@ class ACCOUNTDLLEXPORT AvatarManager : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AvatarManager(Jreen::Client *client);
|
AvatarManager(Jreen::Client* client);
|
||||||
virtual ~AvatarManager();
|
virtual ~AvatarManager();
|
||||||
|
|
||||||
QPixmap avatar(const QString &jid) const;
|
QPixmap avatar(const QString& jid) const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void newAvatar( const QString &jid );
|
void newAvatar( const QString& jid );
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onNewPresence( const Jreen::Presence& presence );
|
void onNewPresence( const Jreen::Presence& presence );
|
||||||
void onNewIq(const Jreen::IQ &iq);
|
void onNewIq(const Jreen::IQ& iq);
|
||||||
void onNewConnection();
|
void onNewConnection();
|
||||||
void onNewAvatar( const QString &jid );
|
void onNewAvatar( const QString& jid );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void fetchVCard( const QString &jid);
|
void fetchVCard( const QString& jid );
|
||||||
QString avatarHash(const QString &jid) const;
|
QString avatarHash( const QString& jid ) const;
|
||||||
QString avatarPath(const QString &avatarHash) const;
|
QString avatarPath( const QString& avatarHash ) const;
|
||||||
|
|
||||||
QDir avatarDir(const QString &avatarHash) const;
|
QDir avatarDir( const QString& avatarHash ) const;
|
||||||
bool isCached(const QString &avatarHash) const;
|
bool isCached( const QString& avatarHash ) const;
|
||||||
|
|
||||||
Jreen::Client *m_client;
|
Jreen::Client* m_client;
|
||||||
QStringList m_cachedAvatars;
|
QStringList m_cachedAvatars;
|
||||||
QDir m_cacheDir;
|
QDir m_cacheDir;
|
||||||
QMap<QString, QString> m_JidsAvatarHashes;
|
QMap< QString, QString > m_JidsAvatarHashes;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // AVATARMANAGER_H
|
#endif // AVATARMANAGER_H
|
||||||
|
Reference in New Issue
Block a user