1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-06 14:16:32 +02:00

Use SipInfo in SipHandler & SipPlugin and fix Jabber to use the new API

This commit is contained in:
Dominik Schmidt
2011-05-09 02:03:57 +02:00
parent 9cd07349dc
commit 9c30600793
6 changed files with 48 additions and 61 deletions

View File

@@ -122,13 +122,13 @@ void DiagnosticsDialog::updateLogView()
} }
} }
QVariantMap sipInfo = SipHandler::instance()->sipInfo( peerId ); SipInfo sipInfo = SipHandler::instance()->sipInfo( peerId );
if( sipInfo.value( "visible").toBool() ) if( sipInfo.isVisible() )
log.append( log.append(
QString(" %1: %2:%3 (%4)\n") QString(" %1: %2:%3 (%4)\n")
.arg( peerId ) .arg( peerId )
.arg( sipInfo.value( "ip" ).toString() ) .arg( sipInfo.host().toString() )
.arg( sipInfo.value( "port" ).toString() ) .arg( sipInfo.port() )
.arg( connected ? "connected" : "not connected") .arg( connected ? "connected" : "not connected")
); );
else else

View File

@@ -80,7 +80,7 @@ const QPixmap SipHandler::avatar( const QString& name ) const
} }
} }
const QVariantMap const SipInfo
SipHandler::sipInfo(const QString& peerId) const SipHandler::sipInfo(const QString& peerId) const
{ {
return m_peersSipInfos.value( peerId ); return m_peersSipInfos.value( peerId );
@@ -193,6 +193,7 @@ SipHandler::hookUpPlugin( SipPlugin* sip )
QObject::connect( sip, SIGNAL( peerOnline( QString ) ), SLOT( onPeerOnline( QString ) ) ); QObject::connect( sip, SIGNAL( peerOnline( QString ) ), SLOT( onPeerOnline( QString ) ) );
QObject::connect( sip, SIGNAL( peerOffline( QString ) ), SLOT( onPeerOffline( QString ) ) ); QObject::connect( sip, SIGNAL( peerOffline( QString ) ), SLOT( onPeerOffline( QString ) ) );
QObject::connect( sip, SIGNAL( msgReceived( QString, QString ) ), SLOT( onMessage( QString, QString ) ) ); QObject::connect( sip, SIGNAL( msgReceived( QString, QString ) ), SLOT( onMessage( QString, QString ) ) );
QObject::connect( sip, SIGNAL( sipInfoReceived( QString, SipInfo ) ), SLOT( onSipInfo( QString, SipInfo ) ) );
QObject::connect( sip, SIGNAL( error( int, QString ) ), SLOT( onError( int, QString ) ) ); QObject::connect( sip, SIGNAL( error( int, QString ) ), SLOT( onError( int, QString ) ) );
QObject::connect( sip, SIGNAL( stateChanged( SipPlugin::ConnectionState ) ), SLOT( onStateChanged( SipPlugin::ConnectionState ) ) ); QObject::connect( sip, SIGNAL( stateChanged( SipPlugin::ConnectionState ) ), SLOT( onStateChanged( SipPlugin::ConnectionState ) ) );
@@ -483,39 +484,27 @@ SipHandler::onPeerOffline( const QString& jid )
qDebug() << "SIP offline:" << jid; qDebug() << "SIP offline:" << jid;
} }
void void
SipHandler::onMessage( const QString& from, const QString& msg ) SipHandler::onSipInfo( const QString& peerId, const SipInfo& info )
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO << "SIP Message:" << peerId << info;
qDebug() << "SIP Message:" << from << msg;
QJson::Parser parser;
bool ok;
QVariant v = parser.parse( msg.toAscii(), &ok );
if ( !ok || v.type() != QVariant::Map )
{
qDebug() << "Invalid JSON in XMPP msg";
return;
}
QVariantMap m = v.toMap();
/* /*
If only one party is externally visible, connection is obvious If only one party is externally visible, connection is obvious
If both are, peer with lowest IP address initiates the connection. If both are, peer with lowest IP address initiates the connection.
This avoids dupe connections. This avoids dupe connections.
*/ */
if ( m.value( "visible" ).toBool() ) if ( info.isVisible() )
{ {
if( !Servent::instance()->visibleExternally() || if( !Servent::instance()->visibleExternally() ||
Servent::instance()->externalAddress() <= m.value( "ip" ).toString() ) Servent::instance()->externalAddress() <= info.host().toString() )
{ {
qDebug() << "Initiate connection to" << from; qDebug() << "Initiate connection to" << peerId;
Servent::instance()->connectToPeer( m.value( "ip" ).toString(), Servent::instance()->connectToPeer( info.host().toString(),
m.value( "port" ).toInt(), info.port(),
m.value( "key" ).toString(), info.key(),
from, peerId,
m.value( "uniqname" ).toString() ); info.uniqname() );
} }
else else
{ {
@@ -527,7 +516,13 @@ SipHandler::onMessage( const QString& from, const QString& msg )
qDebug() << Q_FUNC_INFO << "They are not visible, doing nothing atm"; qDebug() << Q_FUNC_INFO << "They are not visible, doing nothing atm";
} }
m_peersSipInfos.insert( from, m ); m_peersSipInfos.insert( peerId, info );
}
void
SipHandler::onMessage( const QString& from, const QString& msg )
{
qDebug() << Q_FUNC_INFO;
} }

View File

@@ -52,7 +52,7 @@ public:
const QPixmap avatar( const QString& name ) const; const QPixmap avatar( const QString& name ) const;
//TODO: implement a proper SipInfo class and maybe attach it to the source //TODO: implement a proper SipInfo class and maybe attach it to the source
const QVariantMap sipInfo( const QString& peerId ) const; const SipInfo sipInfo( const QString& peerId ) const;
public slots: public slots:
void checkSettings(); void checkSettings();
@@ -87,6 +87,7 @@ signals:
void pluginRemoved( SipPlugin* p ); void pluginRemoved( SipPlugin* p );
private slots: private slots:
void onSipInfo( const QString& peerId, const SipInfo& info );
void onMessage( const QString&, const QString& ); void onMessage( const QString&, const QString& );
void onPeerOffline( const QString& ); void onPeerOffline( const QString& );
void onPeerOnline( const QString& ); void onPeerOnline( const QString& );
@@ -121,7 +122,7 @@ private:
QNetworkProxy m_proxy; QNetworkProxy m_proxy;
//TODO: move this to source //TODO: move this to source
QHash<QString, QVariantMap> m_peersSipInfos; QHash<QString, SipInfo> m_peersSipInfos;
QHash<QString, QPixmap> m_usernameAvatars; QHash<QString, QPixmap> m_usernameAvatars;
}; };

View File

@@ -20,6 +20,8 @@
#ifndef SIPPLUGIN_H #ifndef SIPPLUGIN_H
#define SIPPLUGIN_H #define SIPPLUGIN_H
#include "sipinfo.h"
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QMenu> #include <QMenu>
@@ -96,6 +98,7 @@ signals:
void peerOnline( const QString& ); void peerOnline( const QString& );
void peerOffline( const QString& ); void peerOffline( const QString& );
void msgReceived( const QString& from, const QString& msg ); void msgReceived( const QString& from, const QString& msg );
void sipInfoReceived( const QString& peerId, const SipInfo& info );
// new data for own source // new data for own source
void avatarReceived ( const QPixmap& avatar ); void avatarReceived ( const QPixmap& avatar );

View File

@@ -218,7 +218,7 @@ SipInfo::fromJson( QString json )
QDebug operator<< ( QDebug dbg, const SipInfo& info ) QDebug operator<< ( QDebug dbg, const SipInfo& info )
{ {
if( !isValid() ) if( !info.isValid() )
dbg.nospace() << "info is invalid"; dbg.nospace() << "info is invalid";
else else
dbg.nospace() << info.toJson(); dbg.nospace() << info.toJson();

View File

@@ -556,13 +556,9 @@ void JabberPlugin::onNewMessage(const Jreen::Message& message)
QString from = message.from().full(); QString from = message.from().full();
QString msg = message.body(); QString msg = message.body();
if ( msg.isEmpty() ) SipInfo info = SipInfo::fromJson( msg );
return;
QJson::Parser parser; if ( !info.isValid() )
bool ok;
QVariant v = parser.parse( msg.toAscii(), &ok );
if ( !ok || v.type() != QVariant::Map )
{ {
QString to = from; QString to = from;
QString response = QString( tr("I'm sorry -- I'm just an automatic presence used by Tomahawk Player" QString response = QString( tr("I'm sorry -- I'm just an automatic presence used by Tomahawk Player"
@@ -572,11 +568,12 @@ void JabberPlugin::onNewMessage(const Jreen::Message& message)
// this is not a sip message, so we send it directly through the client // this is not a sip message, so we send it directly through the client
m_client->send( Jreen::Message ( Jreen::Message::Chat, Jreen::JID(to), response) ); m_client->send( Jreen::Message ( Jreen::Message::Chat, Jreen::JID(to), response) );
emit msgReceived( from, msg );
return; return;
} }
qDebug() << Q_FUNC_INFO << "From:" << message.from().full() << ":" << message.body(); qDebug() << Q_FUNC_INFO << "From:" << message.from().full() << ":" << message.body();
emit msgReceived( from, msg ); emit sipInfoReceived( from, info );
} }
@@ -770,35 +767,26 @@ void JabberPlugin::onNewIq(const Jreen::IQ& iq, int context)
iq.accept(); iq.accept();
qDebug() << Q_FUNC_INFO << "Got SipMessage ..."; qDebug() << Q_FUNC_INFO << "Got SipMessage ...";
qDebug() << "ip" << sipMessage->ip(); qDebug() << Q_FUNC_INFO << "ip" << sipMessage->ip();
qDebug() << "port" << sipMessage->port(); qDebug() << Q_FUNC_INFO << "port" << sipMessage->port();
qDebug() << "uniqname" << sipMessage->uniqname(); qDebug() << Q_FUNC_INFO << "uniqname" << sipMessage->uniqname();
qDebug() << "key" << sipMessage->key(); qDebug() << Q_FUNC_INFO << "key" << sipMessage->key();
qDebug() << "visible" << sipMessage->visible(); qDebug() << Q_FUNC_INFO << "visible" << sipMessage->visible();
SipInfo info;
QVariantMap m; info.setVisible( sipMessage->visible() );
if( sipMessage->visible() ) if( sipMessage->visible() )
{ {
m["visible"] = true; info.setHost( QHostAddress( sipMessage->ip() ) );
m["ip"] = sipMessage->ip(); info.setPort( sipMessage->port() );
m["port"] = sipMessage->port(); info.setUniqname( sipMessage->uniqname() );
m["key"] = sipMessage->key(); info.setKey( sipMessage->key() );
m["uniqname"] = sipMessage->uniqname();
}
else
{
m["visible"] = false;
} }
Q_ASSERT( info.isValid() );
QJson::Serializer ser; qDebug() << Q_FUNC_INFO << "From:" << iq.from().full() << ":" << info;
QByteArray ba = ser.serialize( m ); emit sipInfoReceived( iq.from().full(), info );
QString msg = QString::fromAscii( ba );
QString from = iq.from().full();
qDebug() << Q_FUNC_INFO << "From:" << from << ":" << msg;
emit msgReceived( from, msg );
} }
} }
} }