diff --git a/src/diagnosticsdialog.cpp b/src/diagnosticsdialog.cpp index 865cce92f..3a4a6db71 100644 --- a/src/diagnosticsdialog.cpp +++ b/src/diagnosticsdialog.cpp @@ -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 diff --git a/src/libtomahawk/sip/SipHandler.cpp b/src/libtomahawk/sip/SipHandler.cpp index 46b6d18b4..d22e21bc7 100644 --- a/src/libtomahawk/sip/SipHandler.cpp +++ b/src/libtomahawk/sip/SipHandler.cpp @@ -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; } diff --git a/src/libtomahawk/sip/SipHandler.h b/src/libtomahawk/sip/SipHandler.h index 93b689cb7..2076a859a 100644 --- a/src/libtomahawk/sip/SipHandler.h +++ b/src/libtomahawk/sip/SipHandler.h @@ -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 m_peersSipInfos; + QHash m_peersSipInfos; QHash m_usernameAvatars; }; diff --git a/src/libtomahawk/sip/SipPlugin.h b/src/libtomahawk/sip/SipPlugin.h index f6b7b334a..3ac9fd2cc 100644 --- a/src/libtomahawk/sip/SipPlugin.h +++ b/src/libtomahawk/sip/SipPlugin.h @@ -20,6 +20,8 @@ #ifndef SIPPLUGIN_H #define SIPPLUGIN_H +#include "sipinfo.h" + #include #include #include @@ -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 ); diff --git a/src/libtomahawk/sip/sipinfo.cpp b/src/libtomahawk/sip/sipinfo.cpp index 445122ed5..ca192e22a 100644 --- a/src/libtomahawk/sip/sipinfo.cpp +++ b/src/libtomahawk/sip/sipinfo.cpp @@ -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(); diff --git a/src/sip/jreen/jabber.cpp b/src/sip/jreen/jabber.cpp index d030238bf..314e84c3a 100644 --- a/src/sip/jreen/jabber.cpp +++ b/src/sip/jreen/jabber.cpp @@ -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 ); } } }