From aeefdf97706ab64ccb394891e89cca5c58edb9ae Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Sun, 18 Nov 2012 11:00:40 -0500 Subject: [PATCH] Do not re-save duplicated avatars, as jreen spams us with avatars and we constantly save them to disk --- src/libtomahawk/Source.cpp | 15 +++++++++++---- src/libtomahawk/Source.h | 1 + src/libtomahawk/sip/SipHandler.cpp | 15 +-------------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/libtomahawk/Source.cpp b/src/libtomahawk/Source.cpp index 4a5b79585..0a3482d74 100644 --- a/src/libtomahawk/Source.cpp +++ b/src/libtomahawk/Source.cpp @@ -132,15 +132,22 @@ Source::friendlyName() const void Source::setAvatar( const QPixmap& avatar ) { - delete m_avatar; - m_avatar = new QPixmap( avatar ); - m_fancyAvatar = 0; - QByteArray ba; QBuffer buffer( &ba ); buffer.open( QIODevice::WriteOnly ); avatar.save( &buffer, "PNG" ); + // Check if the avatar is different by comparing a hash of the first 4096 bytes + const QByteArray hash = QCryptographicHash::hash( ba.left( 4096 ), QCryptographicHash::Sha1 ); + if ( m_avatarHash == hash ) + return; + else + m_avatarHash = hash; + + delete m_avatar; + m_avatar = new QPixmap( avatar ); + m_fancyAvatar = 0; + TomahawkUtils::Cache::instance()->putData( "Sources", 7776000000 /* 90 days */, m_username, ba ); m_avatarUpdated = true; } diff --git a/src/libtomahawk/Source.h b/src/libtomahawk/Source.h index b7fbf7aba..1e57d4479 100644 --- a/src/libtomahawk/Source.h +++ b/src/libtomahawk/Source.h @@ -163,6 +163,7 @@ private: mutable QPixmap* m_avatar; mutable QPixmap* m_fancyAvatar; + mutable QByteArray m_avatarHash; mutable QHash< AvatarStyle, QHash< int, QPixmap > > m_coverCache; Tomahawk::playlistinterface_ptr m_playlistInterface; diff --git a/src/libtomahawk/sip/SipHandler.cpp b/src/libtomahawk/sip/SipHandler.cpp index fbc26255e..07457e26b 100644 --- a/src/libtomahawk/sip/SipHandler.cpp +++ b/src/libtomahawk/sip/SipHandler.cpp @@ -221,34 +221,21 @@ SipHandler::onAvatarReceived( const QString& from, const QPixmap& avatar ) // qDebug() << Q_FUNC_INFO << "setting avatar on source for" << from; if ( avatar.isNull() ) { -// qDebug() << Q_FUNC_INFO << "got null pixmap, not adding anything"; return; } m_usernameAvatars.insert( from, avatar ); - // - - //Tomahawk::source_ptr source = ->source(); ControlConnection *conn = Servent::instance()->lookupControlConnection( from ); if( conn ) { -// qDebug() << Q_FUNC_INFO << from << "got control connection"; Tomahawk::source_ptr source = conn->source(); if( source ) { -// qDebug() << Q_FUNC_INFO << from << "got source, setting avatar"; +// qDebug() << Q_FUNC_INFO << from << "got source, setting avatar on source:" << source->friendlyName(); source->setAvatar( avatar ); } - else - { -// qDebug() << Q_FUNC_INFO << from << "no source found, not setting avatar"; - } - } - else - { -// qDebug() << Q_FUNC_INFO << from << "no control connection setup yet"; } }