1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 08:19:42 +01: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 );
if( sipInfo.value( "visible").toBool() )
SipInfo sipInfo = SipHandler::instance()->sipInfo( peerId );
if( sipInfo.isVisible() )
log.append(
QString(" %1: %2:%3 (%4)\n")
.arg( peerId )
.arg( sipInfo.value( "ip" ).toString() )
.arg( sipInfo.value( "port" ).toString() )
.arg( sipInfo.host().toString() )
.arg( sipInfo.port() )
.arg( connected ? "connected" : "not connected")
);
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
{
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( peerOffline( QString ) ), SLOT( onPeerOffline( 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( stateChanged( SipPlugin::ConnectionState ) ), SLOT( onStateChanged( SipPlugin::ConnectionState ) ) );
@ -483,39 +484,27 @@ SipHandler::onPeerOffline( const QString& jid )
qDebug() << "SIP offline:" << jid;
}
void
SipHandler::onMessage( const QString& from, const QString& msg )
SipHandler::onSipInfo( const QString& peerId, const SipInfo& info )
{
qDebug() << Q_FUNC_INFO;
qDebug() << "SIP Message:" << from << msg;
qDebug() << Q_FUNC_INFO << "SIP Message:" << peerId << info;
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 both are, peer with lowest IP address initiates the connection.
This avoids dupe connections.
*/
if ( m.value( "visible" ).toBool() )
if ( info.isVisible() )
{
if( !Servent::instance()->visibleExternally() ||
Servent::instance()->externalAddress() <= m.value( "ip" ).toString() )
Servent::instance()->externalAddress() <= info.host().toString() )
{
qDebug() << "Initiate connection to" << from;
Servent::instance()->connectToPeer( m.value( "ip" ).toString(),
m.value( "port" ).toInt(),
m.value( "key" ).toString(),
from,
m.value( "uniqname" ).toString() );
qDebug() << "Initiate connection to" << peerId;
Servent::instance()->connectToPeer( info.host().toString(),
info.port(),
info.key(),
peerId,
info.uniqname() );
}
else
{
@ -527,7 +516,13 @@ SipHandler::onMessage( const QString& from, const QString& msg )
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;
//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:
void checkSettings();
@ -87,6 +87,7 @@ signals:
void pluginRemoved( SipPlugin* p );
private slots:
void onSipInfo( const QString& peerId, const SipInfo& info );
void onMessage( const QString&, const QString& );
void onPeerOffline( const QString& );
void onPeerOnline( const QString& );
@ -121,7 +122,7 @@ private:
QNetworkProxy m_proxy;
//TODO: move this to source
QHash<QString, QVariantMap> m_peersSipInfos;
QHash<QString, SipInfo> m_peersSipInfos;
QHash<QString, QPixmap> m_usernameAvatars;
};

View File

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

View File

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

View File

@ -556,13 +556,9 @@ void JabberPlugin::onNewMessage(const Jreen::Message& message)
QString from = message.from().full();
QString msg = message.body();
if ( msg.isEmpty() )
return;
SipInfo info = SipInfo::fromJson( msg );
QJson::Parser parser;
bool ok;
QVariant v = parser.parse( msg.toAscii(), &ok );
if ( !ok || v.type() != QVariant::Map )
if ( !info.isValid() )
{
QString to = from;
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
m_client->send( Jreen::Message ( Jreen::Message::Chat, Jreen::JID(to), response) );
emit msgReceived( from, msg );
return;
}
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();
qDebug() << Q_FUNC_INFO << "Got SipMessage ...";
qDebug() << "ip" << sipMessage->ip();
qDebug() << "port" << sipMessage->port();
qDebug() << "uniqname" << sipMessage->uniqname();
qDebug() << "key" << sipMessage->key();
qDebug() << "visible" << sipMessage->visible();
qDebug() << Q_FUNC_INFO << "ip" << sipMessage->ip();
qDebug() << Q_FUNC_INFO << "port" << sipMessage->port();
qDebug() << Q_FUNC_INFO << "uniqname" << sipMessage->uniqname();
qDebug() << Q_FUNC_INFO << "key" << sipMessage->key();
qDebug() << Q_FUNC_INFO << "visible" << sipMessage->visible();
QVariantMap m;
SipInfo info;
info.setVisible( sipMessage->visible() );
if( sipMessage->visible() )
{
m["visible"] = true;
m["ip"] = sipMessage->ip();
m["port"] = sipMessage->port();
m["key"] = sipMessage->key();
m["uniqname"] = sipMessage->uniqname();
}
else
{
m["visible"] = false;
info.setHost( QHostAddress( sipMessage->ip() ) );
info.setPort( sipMessage->port() );
info.setUniqname( sipMessage->uniqname() );
info.setKey( sipMessage->key() );
}
Q_ASSERT( info.isValid() );
QJson::Serializer ser;
QByteArray ba = ser.serialize( m );
QString msg = QString::fromAscii( ba );
QString from = iq.from().full();
qDebug() << Q_FUNC_INFO << "From:" << from << ":" << msg;
emit msgReceived( from, msg );
qDebug() << Q_FUNC_INFO << "From:" << iq.from().full() << ":" << info;
emit sipInfoReceived( iq.from().full(), info );
}
}
}