1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-25 10:19:41 +01:00

Merge branch 'jreen-avatars'

Conflicts:
	src/libtomahawk/source.h
This commit is contained in:
Dominik Schmidt 2011-04-20 21:31:44 +02:00
commit 26be5c954d
16 changed files with 388 additions and 13 deletions

View File

@ -32,8 +32,6 @@ ENDIF()
#ENDFOREACH( moddir )
SET( tomahawkSources ${tomahawkSources}
sip/SipHandler.cpp
web/api_v1.cpp
resolvers/scriptresolver.cpp
@ -71,7 +69,6 @@ SET( tomahawkSourcesGui ${tomahawkSourcesGui}
SET( tomahawkHeaders ${tomahawkHeaders}
"${TOMAHAWK_INC_DIR}/tomahawk/tomahawkapp.h"
sip/SipHandler.h
web/api_v1.h

View File

@ -29,6 +29,7 @@ set( libSources
viewpage.cpp
sip/SipPlugin.cpp
sip/SipHandler.cpp
audio/madtranscode.cpp
audio/dummytranscode.cpp
@ -187,6 +188,7 @@ set( libHeaders
playlist.h
sip/SipPlugin.h
sip/SipHandler.h
audio/transcodeinterface.h
audio/madtranscode.h

View File

@ -23,6 +23,7 @@
#include "database/databasecommand_collectionstats.h"
#include "dbsyncconnection.h"
#include "sourcelist.h"
#include <sip/SipHandler.h>
#define TCP_TIMEOUT 600
@ -120,6 +121,10 @@ ControlConnection::registerSource()
Source* source = (Source*) sender();
Q_UNUSED( source )
Q_ASSERT( source == m_source.data() );
qDebug() << Q_FUNC_INFO << "Setting avatar ... " << name() << !SipHandler::instance()->avatar( name() ).isNull();
source->setAvatar( SipHandler::instance()->avatar( name() ) );
// .. but we'll use the shared pointer we've already made:
m_registered = true;

View File

@ -44,6 +44,8 @@ public:
DBSyncConnection* dbSyncConnection();
Tomahawk::source_ptr source() const { return m_source; }
protected:
virtual void setup();

View File

@ -1,5 +1,5 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
@ -24,19 +24,33 @@
#include <QPluginLoader>
#include <QMessageBox>
#include "functimeout.h"
#include "database/database.h"
#include "network/controlconnection.h"
#include "sourcelist.h"
#include "tomahawksettings.h"
#include "tomahawk/tomahawkapp.h"
//#include "tomahawk/tomahawkapp.h"
#include "config.h"
//remove
#include <QLabel>
SipHandler* SipHandler::s_instance = 0;
SipHandler* SipHandler::instance()
{
return s_instance;
}
SipHandler::SipHandler( QObject* parent )
: QObject( parent )
, m_connected( false )
{
s_instance = this;
loadPlugins( findPlugins() );
connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) );
@ -55,6 +69,23 @@ SipHandler::plugins() const
return m_plugins;
}
const QPixmap SipHandler::avatar( const QString& name ) const
{
qDebug() << Q_FUNC_INFO << "Getting avatar" << name << m_usernameAvatars.keys();
if( m_usernameAvatars.keys().contains( name ) )
{
qDebug() << Q_FUNC_INFO << "Getting avatar and avatar != null ";
Q_ASSERT(!m_usernameAvatars.value( name ).isNull());
return m_usernameAvatars.value( name );
}
else
{
qDebug() << Q_FUNC_INFO << "Getting avatar and avatar == null, GAAAAAH ";
return QPixmap();
}
}
void
SipHandler::onSettingsChanged()
@ -147,6 +178,7 @@ SipHandler::loadPlugin( const QString& path )
QObject::connect( sip, SIGNAL( disconnected() ), SIGNAL( disconnected() ) );
QObject::connect( sip, SIGNAL( error( int, QString ) ), SLOT( onError( int, QString ) ) );
QObject::connect( sip, SIGNAL( avatarReceived( QString, QPixmap ) ), SLOT( onAvatarReceived( QString, QPixmap ) ) );
m_plugins << sip;
}
}
@ -182,7 +214,8 @@ SipHandler::connectPlugins( bool startup, const QString &pluginName )
if ( !TomahawkSettings::instance()->acceptedLegalWarning() )
{
int result = QMessageBox::question(
TomahawkApp::instance()->mainWindow(), tr( "Legal Warning" ),
//TomahawkApp::instance()->mainWindow(),
0, tr( "Legal Warning" ),
tr( "By pressing OK below, you agree that your use of Tomahawk will be in accordance with any applicable laws, including copyright and intellectual property laws, in effect in your country of residence, and indemnify the Tomahawk developers and project from liability should you choose to break those laws.\n\nFor more information, please see http://gettomahawk.com/legal" ),
tr( "I Do Not Agree" ), tr( "I Agree" )
);
@ -346,3 +379,35 @@ SipHandler::onError( int code, const QString& msg )
QTimer::singleShot( 10000, sip, SLOT( connectPlugin() ) );
}
}
void SipHandler::onAvatarReceived( const QString& from, const QPixmap& avatar )
{
qDebug() << Q_FUNC_INFO << "Set avatar on source for " << from;
Q_ASSERT(!avatar.isNull());
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";
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";
}
}

View File

@ -1,5 +1,5 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
*
* Tomahawk is free software: you can redistribute it and/or modify
@ -23,19 +23,24 @@
#include <QDebug>
#include <QObject>
#include <QHash>
#include <QPixmap>
#include <QString>
class SipHandler : public QObject
{
Q_OBJECT
public:
// static SipHandler* instance() { return s_instance ? s_instance : new SipHandler(); }
static SipHandler* instance();
SipHandler( QObject* parent );
~SipHandler();
QList< SipPlugin* > plugins() const;
const QPixmap avatar( const QString& name ) const;
public slots:
void addContact( const QString& id ) { qDebug() << Q_FUNC_INFO << id; }
@ -57,7 +62,11 @@ private slots:
void onSettingsChanged();
void onAvatarReceived( const QString& from, const QPixmap& avatar = QPixmap());
private:
static SipHandler *s_instance;
QStringList findPlugins();
bool pluginLoaded( const QString& name ) const;
@ -66,6 +75,9 @@ private:
QList< SipPlugin* > m_plugins;
bool m_connected;
QHash<QString, QPixmap> m_usernameAvatars;
};
#endif

View File

@ -57,7 +57,9 @@ signals:
void peerOnline( const QString& );
void peerOffline( const QString& );
void msgReceived( const QString& from, const QString& msg );
void avatarReceived ( const QString& from, const QPixmap& avatar);
void addMenu( QMenu* menu );
void removeMenu( QMenu* menu );
};

View File

@ -111,6 +111,16 @@ Source::friendlyName() const
return m_friendlyname;
}
void Source::setAvatar(const QPixmap& avatar)
{
m_avatar = avatar;
}
const QPixmap Source::avatar() const
{
return m_avatar;
}
void
Source::setFriendlyName( const QString& fname )

View File

@ -54,6 +54,8 @@ public:
QString userName() const { return m_username; }
QString friendlyName() const;
void setFriendlyName( const QString& fname );
void setAvatar(const QPixmap &avatar);
const QPixmap avatar() const;
collection_ptr collection() const;
void addCollection( const Tomahawk::collection_ptr& c );
@ -117,6 +119,8 @@ private:
QString m_textStatus;
ControlConnection* m_cc;
QPixmap m_avatar;
};
};

View File

@ -11,6 +11,7 @@ set( jabberSources
jabber_p.cpp
tomahawksipmessage.cpp
tomahawksipmessagefactory.cpp
avatarmanager.cpp
)
set( jabberHeaders
@ -18,6 +19,7 @@ set( jabberHeaders
jabber_p.h
tomahawksipmessage.h
tomahawksipmessagefactory.h
avatarmanager.h
)
include_directories( . ${CMAKE_CURRENT_BINARY_DIR} ..

View File

@ -0,0 +1,171 @@
#include "avatarmanager.h"
#include "utils/tomahawkutils.h"
#include <jreen/vcard.h>
#include <jreen/vcardupdate.h>
#include <jreen/presence.h>
#include <QDir>
#include <QDebug>
#include <QCryptographicHash>
#include <QPixmap>
AvatarManager::AvatarManager(Jreen::Client *client) :
m_cacheDir(TomahawkUtils::appDataDir().absolutePath().append("/jreen/"))
{
m_client = client;
m_cachedAvatars = m_cacheDir.entryList();
connect(m_client, SIGNAL(serverFeaturesReceived(QSet<QString>)), SLOT(onNewConnection()));
connect(m_client, SIGNAL(newPresence(Jreen::Presence)), SLOT(onNewPresence(Jreen::Presence)));
connect(m_client, SIGNAL(newIQ(Jreen::IQ)), SLOT(onNewIq(Jreen::IQ)));
connect(this, SIGNAL(newAvatar(QString)), SLOT(onNewAvatar(QString)));
}
AvatarManager::~AvatarManager()
{
}
void AvatarManager::onNewConnection()
{
fetchVCard( m_client->jid().bare() );
}
void AvatarManager::fetchVCard(const QString &jid)
{
qDebug() << Q_FUNC_INFO;
Jreen::IQ iq(Jreen::IQ::Get, jid );
iq.addExtension(new Jreen::VCard());
m_client->send( iq, this, SLOT( onNewIq( Jreen::IQ, int ) ), 0 );
}
void AvatarManager::onNewPresence(const Jreen::Presence& presence)
{
Jreen::VCardUpdate::Ptr update = presence.findExtension<Jreen::VCardUpdate>();
if(update)
{
qDebug() << "vcard: found update for " << presence.from().full();
if(!isCached(update->photoHash()))
{
qDebug() << presence.from().full() << "vcard: photo not cached, starting request..." << update->photoHash();
fetchVCard( presence.from().bare() );
}
else
{
qDebug() << presence.from().full() << "vcard: photo already cached no request necessary " << update->photoHash();
m_JidsAvatarHashes.insert( update->photoHash(), presence.from().bare() );
Q_ASSERT(!this->avatar(presence.from().bare()).isNull());
emit newAvatar(presence.from().bare());
}
}
else
{
qDebug() << Q_FUNC_INFO << presence.from().full() << "got no statusupdateextension";
//TODO: do we want this? might fetch avatars for broken clients
fetchVCard( presence.from().bare() );
}
}
void AvatarManager::onNewIq(const Jreen::IQ& iq, int context)
{
Jreen::VCard *vcard = iq.findExtension<Jreen::VCard>().data();
if(vcard)
{
iq.accept();
qDebug() << Q_FUNC_INFO << "Got vcard from " << iq.from().full();
QString id = iq.from().full();
QString avatarHash;
const Jreen::VCard::Photo &photo = vcard->photo();
if (!photo.data().isEmpty()) {
qDebug() << "vcard: got photo data" << id;
avatarHash = QCryptographicHash::hash(photo.data(), QCryptographicHash::Sha1).toHex();
if (!m_cacheDir.exists())
m_cacheDir.mkpath( avatarDir( avatarHash ).absolutePath() );
QFile file(avatarPath(avatarHash));
if (file.open(QIODevice::WriteOnly)) {
file.write(photo.data());
file.close();
}
m_cachedAvatars.append(avatarHash);
m_JidsAvatarHashes.insert( avatarHash, iq.from().bare() );
Q_ASSERT(!this->avatar(iq.from().bare()).isNull());
emit newAvatar(iq.from().bare());
}
else
{
qDebug() << "vcard: got no photo data" << id;
}
// got own presence
if ( m_client->jid().bare() == id )
{
qDebug() << Q_FUNC_INFO << "got own vcard";
Jreen::Presence presence = m_client->presence();
Jreen::VCardUpdate::Ptr update = presence.findExtension<Jreen::VCardUpdate>();
if (update->photoHash() != avatarHash)
{
qDebug() << Q_FUNC_INFO << "Updating own presence...";
update->setPhotoHash(avatarHash);
m_client->send(presence);
}
}
}
}
QPixmap AvatarManager::avatar(const QString &jid) const
{
if( isCached( avatarHash( jid ) ) )
{
return QPixmap( avatarPath( avatarHash( jid ) ) );
}
else
{
return QPixmap();
}
}
QString AvatarManager::avatarHash(const QString &jid) const
{
//qDebug() << Q_FUNC_INFO << jid << m_JidsAvatarHashes.key(jid);
return m_JidsAvatarHashes.key(jid);
}
QDir AvatarManager::avatarDir(const QString &avatarHash) const
{
return m_cacheDir;
}
QString AvatarManager::avatarPath(const QString &avatarHash) const
{
Q_ASSERT(!avatarHash.contains("@"));
return avatarDir(avatarHash).absoluteFilePath(avatarHash);
}
bool AvatarManager::isCached(const QString &avatarHash) const
{
return m_cachedAvatars.contains( avatarHash );
}
void AvatarManager::onNewAvatar(const QString& jid)
{
qDebug() << Q_FUNC_INFO << "Found new Avatar..." << jid;
}

View File

@ -0,0 +1,43 @@
#ifndef AVATARMANAGER_H
#define AVATARMANAGER_H
#include <jreen/client.h>
#include <QObject>
#include <QDir>
class AvatarManager : public QObject
{
Q_OBJECT
public:
AvatarManager(Jreen::Client *client);
virtual ~AvatarManager();
QPixmap avatar(const QString &jid) const;
signals:
void newAvatar( const QString &jid );
private slots:
void onNewPresence( const Jreen::Presence& presence );
void onNewIq(const Jreen::IQ &iq, int context = 0 );
void onNewConnection();
void onNewAvatar( const QString &jid );
private:
void fetchVCard( const QString &jid);
QString avatarHash(const QString &jid) const;
QString avatarPath(const QString &avatarHash) const;
QDir avatarDir(const QString &avatarHash) const;
bool isCached(const QString &avatarHash) const;
Jreen::Client *m_client;
QStringList m_cachedAvatars;
QDir m_cacheDir;
QMap<QString, QString> m_JidsAvatarHashes;
};
#endif // AVATARMANAGER_H

View File

@ -110,6 +110,7 @@ JabberPlugin::connectPlugin( bool startup )
QObject::connect( p, SIGNAL( disconnected() ), SLOT( onDisconnected() ) );
QObject::connect( p, SIGNAL( authError( int, QString ) ), SLOT( onAuthError( int, QString ) ) );
QObject::connect( p, SIGNAL( avatarReceived( QString, QPixmap ) ), SIGNAL( avatarReceived( QString, QPixmap ) ) );
return true;
}

View File

@ -25,6 +25,8 @@
#include "utils/tomahawkutils.h"
#include <jreen/capabilities.h>
#include <jreen/vcardupdate.h>
#include <jreen/vcard.h>
#include <qjson/parser.h>
#include <qjson/serializer.h>
@ -37,6 +39,14 @@
#include <QThread>
#include <QVariant>
#include <QMap>
#include <QCryptographicHash>
#include <QDir>
#include <QFile>
#include <QPixmap>
//remove
#include <QLabel>
#include <QtGui/QLabel>
#define TOMAHAWK_FEATURE QLatin1String( "tomahawk:sip:v1" )
@ -57,6 +67,12 @@ Jabber_p::Jabber_p( const QString& jid, const QString& password, const QString&
m_client->registerStanzaExtension(new TomahawkSipMessageFactory);
m_client->setResource( QString( "tomahawk%1" ).arg( QString::number( qrand() % 10000 ) ) );
// add VCardUpdate extension to own presence
m_client->presence().addExtension( new Jreen::VCardUpdate() );
// initialize the AvatarManager
m_avatarManager = new AvatarManager(m_client);
// setup disco
m_client->disco()->setSoftwareVersion( "Tomahawk Player", TOMAHAWK_VERSION, CMAKE_SYSTEM );
m_client->disco()->addIdentity( Jreen::Disco::Identity( "client", "type", "tomahawk", "en" ) );
@ -80,6 +96,8 @@ Jabber_p::Jabber_p( const QString& jid, const QString& password, const QString&
connect(m_client, SIGNAL(newPresence(Jreen::Presence)), SLOT(onNewPresence(Jreen::Presence)));
connect(m_client, SIGNAL(newIQ(Jreen::IQ)), SLOT(onNewIq(Jreen::IQ)));
connect(m_avatarManager, SIGNAL(newAvatar(QString)), SLOT(onNewAvatar(QString)));
// connect
qDebug() << "Connecting to the XMPP server...";
@ -329,6 +347,7 @@ void Jabber_p::onNewPresence( const Jreen::Presence& presence)
Jreen::JID jid = presence.from();
QString fulljid( jid.full() );
qDebug() << Q_FUNC_INFO << "* New presence: " << fulljid << presence.subtype();
if( jid == m_jid )
@ -411,11 +430,18 @@ Jabber_p::onNewIq( const Jreen::IQ &iq, int context )
{
qDebug() << "Sent SipMessage... what now?!";
}
/*else if(context == RequestedVCard )
{
qDebug() << "Requested VCard... what now?!";
}*/
else
{
TomahawkSipMessage *sipMessage = iq.findExtension<TomahawkSipMessage>().data();
if(sipMessage)
{
iq.accept();
qDebug() << Q_FUNC_INFO << "Got SipMessage ...";
qDebug() << "ip" << sipMessage->ip();
qDebug() << "port" << sipMessage->port();
@ -466,8 +492,10 @@ Jabber_p::presenceMeansOnline( Jreen::Presence::Type p )
}
void
Jabber_p::handlePeerStatus( const QString& fulljid, Jreen::Presence::Type presenceType )
Jabber_p::handlePeerStatus( const Jreen::JID& jid, Jreen::Presence::Type presenceType )
{
QString fulljid = jid.full();
// "going offline" event
if ( !presenceMeansOnline( presenceType ) &&
( !m_peers.contains( fulljid ) ||
@ -497,7 +525,12 @@ Jabber_p::handlePeerStatus( const QString& fulljid, Jreen::Presence::Type presen
{
m_peers[ fulljid ] = presenceType;
qDebug() << Q_FUNC_INFO << "* Peer goes online:" << fulljid;
emit peerOnline( fulljid );
if(!m_avatarManager->avatar(jid.bare()).isNull())
onNewAvatar( jid.bare() );
return;
}
@ -505,3 +538,20 @@ Jabber_p::handlePeerStatus( const QString& fulljid, Jreen::Presence::Type presen
m_peers[ fulljid ] = presenceType;
}
void Jabber_p::onNewAvatar(const QString& jid)
{
qDebug() << Q_FUNC_INFO << jid;
Q_ASSERT(!m_avatarManager->avatar( jid ).isNull());
// find peers for the jid
QStringList peers = m_peers.keys();
foreach(const QString &peer, peers)
{
if( peer.startsWith(jid) )
{
emit avatarReceived ( peer, m_avatarManager->avatar( jid ) );
}
}
emit avatarReceived ( jid, m_avatarManager->avatar( jid ) );
}

View File

@ -22,6 +22,8 @@
#include "../sipdllmacro.h"
#include "avatarmanager.h"
#include <jreen/client.h>
#include <jreen/disco.h>
#include <jreen/message.h>
@ -63,6 +65,7 @@ signals:
void connected();
void disconnected();
void jidChanged( const QString& );
void avatarReceived( const QString&, const QPixmap& avatar );
void authError( int, const QString& );
public slots:
@ -82,10 +85,11 @@ private slots:
qDebug() << e;
}
virtual void onNewIq( const Jreen::IQ &iq, int context = NoContext );
virtual void onNewAvatar( const QString &jid );
private:
bool presenceMeansOnline( Jreen::Presence::Type p );
void handlePeerStatus( const QString &fulljid, Jreen::Presence::Type presenceType );
void handlePeerStatus( const Jreen::JID &jid, Jreen::Presence::Type presenceType );
Jreen::Client *m_client;
Jreen::MUCRoom *m_room;
@ -95,9 +99,11 @@ private:
QMap<QString, Jreen::Presence::Type> m_peers;
QString m_server;
enum IqContext { NoContext, RequestDisco, RequestedDisco, SipMessageSent };
enum IqContext { NoContext, RequestDisco, RequestedDisco, SipMessageSent, RequestedVCard };
QStringList m_legacy_peers;
AvatarManager *m_avatarManager;
};
#endif // JABBER_H

View File

@ -537,6 +537,7 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
SourceTreeItem* sti = SourcesModel::indexToTreeItem( index );
bool status = !( !sti || sti->source().isNull() || !sti->source()->isOnline() );
QPixmap avatar( RESPATH "images/user-avatar.png" );
QString tracks;
int figWidth = 0;
@ -544,10 +545,12 @@ SourceDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, co
{
tracks = QString::number( sti->source()->trackCount() );
figWidth = painter->fontMetrics().width( tracks );
if ( !sti->source()->avatar().isNull() )
avatar = sti->source()->avatar();
}
QRect iconRect = option.rect.adjusted( 4, 6, -option.rect.width() + option.rect.height() - 12 + 4, -6 );
painter->drawPixmap( iconRect, QPixmap( RESPATH "images/user-avatar.png" ).scaledToHeight( iconRect.height(), Qt::SmoothTransformation ) );
painter->drawPixmap( iconRect, avatar.scaledToHeight( iconRect.height(), Qt::SmoothTransformation ) );
if ( ( option.state & QStyle::State_Selected ) == QStyle::State_Selected )
{