/src/sip/jabber/avatarmanager.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 192 lines · 125 code · 37 blank · 30 comment · 12 complexity · 834f23217387ee30a6ec4e1e412482f9 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "avatarmanager.h"
  19. #include "utils/tomahawkutils.h"
  20. #include <jreen/vcard.h>
  21. #include <jreen/vcardupdate.h>
  22. #include <jreen/presence.h>
  23. #include <jreen/iqreply.h>
  24. #include <QDir>
  25. #include <QCryptographicHash>
  26. #include <QPixmap>
  27. #include "utils/logger.h"
  28. AvatarManager::AvatarManager(Jreen::Client *client) :
  29. m_cacheDir(TomahawkUtils::appDataDir().absolutePath().append("/jreen/"))
  30. {
  31. m_client = client;
  32. m_cachedAvatars = m_cacheDir.entryList();
  33. connect(m_client, SIGNAL(serverFeaturesReceived(QSet<QString>)), SLOT(onNewConnection()));
  34. connect(m_client, SIGNAL(presenceReceived(Jreen::Presence)), SLOT(onNewPresence(Jreen::Presence)));
  35. connect(m_client, SIGNAL(iqReceived(Jreen::IQ)), SLOT(onNewIq(Jreen::IQ)));
  36. connect(this, SIGNAL(newAvatar(QString)), SLOT(onNewAvatar(QString)));
  37. }
  38. AvatarManager::~AvatarManager()
  39. {
  40. }
  41. void AvatarManager::onNewConnection()
  42. {
  43. fetchVCard( m_client->jid().bare() );
  44. }
  45. void AvatarManager::fetchVCard(const QString &jid)
  46. {
  47. // qDebug() << Q_FUNC_INFO;
  48. Jreen::IQ iq(Jreen::IQ::Get, jid );
  49. iq.addExtension(new Jreen::VCard());
  50. Jreen::IQReply *reply = m_client->send(iq);
  51. connect(reply, SIGNAL(received(Jreen::IQ)), SLOT(onNewIq(Jreen::IQ)));
  52. }
  53. void AvatarManager::onNewPresence(const Jreen::Presence& presence)
  54. {
  55. Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
  56. if(update)
  57. {
  58. // qDebug() << "vcard: found update for" << presence.from().full();
  59. if(!isCached(update->photoHash()))
  60. {
  61. // qDebug() << presence.from().full() << "vcard: photo not cached, starting request..." << update->photoHash();
  62. fetchVCard( presence.from().bare() );
  63. }
  64. else
  65. {
  66. // qDebug() << presence.from().full() << "vcard: photo already cached no request necessary " << update->photoHash();
  67. m_JidsAvatarHashes.insert( update->photoHash(), presence.from().bare() );
  68. if ( !this->avatar( presence.from().bare() ).isNull() )
  69. emit newAvatar(presence.from().bare());
  70. }
  71. }
  72. else
  73. {
  74. // qDebug() << Q_FUNC_INFO << presence.from().full() << "got no statusupdateextension";
  75. //TODO: do we want this? might fetch avatars for broken clients
  76. fetchVCard( presence.from().bare() );
  77. }
  78. }
  79. void AvatarManager::onNewIq(const Jreen::IQ& iq)
  80. {
  81. Jreen::VCard::Ptr vcard = iq.payload<Jreen::VCard>();
  82. if(vcard)
  83. {
  84. iq.accept();
  85. // qDebug() << Q_FUNC_INFO << "Got vcard from " << iq.from().full();
  86. QString id = iq.from().full();
  87. QString avatarHash;
  88. const Jreen::VCard::Photo &photo = vcard->photo();
  89. if (!photo.data().isEmpty()) {
  90. // qDebug() << "vcard: got photo data" << id;
  91. avatarHash = QCryptographicHash::hash(photo.data(), QCryptographicHash::Sha1).toHex();
  92. if (!m_cacheDir.exists())
  93. m_cacheDir.mkpath( avatarDir( avatarHash ).absolutePath() );
  94. QFile file(avatarPath(avatarHash));
  95. if (file.open(QIODevice::WriteOnly)) {
  96. file.write(photo.data());
  97. file.close();
  98. }
  99. m_cachedAvatars.append(avatarHash);
  100. m_JidsAvatarHashes.insert( avatarHash, iq.from().bare() );
  101. Q_ASSERT(!this->avatar(iq.from().bare()).isNull());
  102. emit newAvatar(iq.from().bare());
  103. }
  104. else
  105. {
  106. // qDebug() << "vcard: got no photo data" << id;
  107. }
  108. // got own presence
  109. if ( m_client->jid().bare() == id )
  110. {
  111. // qDebug() << Q_FUNC_INFO << "got own vcard";
  112. Jreen::Presence presence = m_client->presence();
  113. Jreen::VCardUpdate::Ptr update = presence.payload<Jreen::VCardUpdate>();
  114. if (update->photoHash() != avatarHash)
  115. {
  116. qDebug() << Q_FUNC_INFO << "Updating own presence...";
  117. update->setPhotoHash(avatarHash);
  118. m_client->send(presence);
  119. }
  120. }
  121. }
  122. }
  123. QPixmap AvatarManager::avatar(const QString &jid) const
  124. {
  125. if( isCached( avatarHash( jid ) ) )
  126. {
  127. return QPixmap( avatarPath( avatarHash( jid ) ) );
  128. }
  129. else
  130. {
  131. return QPixmap();
  132. }
  133. }
  134. QString AvatarManager::avatarHash(const QString &jid) const
  135. {
  136. //qDebug() << Q_FUNC_INFO << jid << m_JidsAvatarHashes.key(jid);
  137. return m_JidsAvatarHashes.key(jid);
  138. }
  139. QDir AvatarManager::avatarDir(const QString&) const
  140. {
  141. return m_cacheDir;
  142. }
  143. QString AvatarManager::avatarPath(const QString &avatarHash) const
  144. {
  145. Q_ASSERT(!avatarHash.contains("@"));
  146. return avatarDir(avatarHash).absoluteFilePath(avatarHash);
  147. }
  148. bool AvatarManager::isCached(const QString &avatarHash) const
  149. {
  150. return m_cachedAvatars.contains( avatarHash );
  151. }
  152. void AvatarManager::onNewAvatar(const QString&)
  153. {
  154. // qDebug() << Q_FUNC_INFO << "Found new Avatar..." << jid;
  155. }