From d7c03e03ce6eba2ac3ccf4f4dbd2c10a504e6e0f Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Tue, 16 Apr 2013 09:39:31 +0200
Subject: [PATCH] * Store PeerInfos in a temporary ControlConnection while the
 SIP is pending, so they don't get deleted again immediately.

---
 src/accounts/xmpp/sip/XmppSip.cpp   |  4 ++--
 src/libtomahawk/network/Servent.cpp | 28 +++++++++++++-----------
 src/libtomahawk/network/Servent.h   |  4 ++--
 src/libtomahawk/sip/PeerInfo.cpp    |  6 +++---
 src/libtomahawk/sip/SipInfo.cpp     | 33 ++++++++++++++++-------------
 5 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/src/accounts/xmpp/sip/XmppSip.cpp b/src/accounts/xmpp/sip/XmppSip.cpp
index 2193b796b..cd878d392 100644
--- a/src/accounts/xmpp/sip/XmppSip.cpp
+++ b/src/accounts/xmpp/sip/XmppSip.cpp
@@ -880,7 +880,7 @@ XmppSipPlugin::onNewIq( const Jreen::IQ& iq )
             handlePeerStatus( jid, Jreen::Presence::Available );
         }
     }
-    else if ( context == RequestVersion)
+    else if ( context == RequestVersion )
     {
         Jreen::SoftwareVersion::Ptr softwareVersion = iq.payload<Jreen::SoftwareVersion>();
         if ( softwareVersion )
@@ -932,7 +932,7 @@ XmppSipPlugin::onNewIq( const Jreen::IQ& iq )
             Tomahawk::peerinfo_ptr peerInfo = PeerInfo::get( this, iq.from().full() );
             if ( peerInfo.isNull() )
             {
-                tDebug() << Q_FUNC_INFO << "no valid peerInfo for " << iq.from().full();
+                tDebug() << Q_FUNC_INFO << "no valid peerInfo for" << iq.from().full();
                 return;
             }
             peerInfo->setSipInfo( info );
diff --git a/src/libtomahawk/network/Servent.cpp b/src/libtomahawk/network/Servent.cpp
index 13449426e..44084791e 100644
--- a/src/libtomahawk/network/Servent.cpp
+++ b/src/libtomahawk/network/Servent.cpp
@@ -311,7 +311,7 @@ Servent::registerPeer( const Tomahawk::peerinfo_ptr& peerInfo )
 
     if ( peerInfo->type() == Tomahawk::PeerInfo::Local )
     {
-        peerInfoDebug(peerInfo) << "YAY, we need to establish the connection now.. thinking";
+        peerInfoDebug(peerInfo) << "we need to establish the connection now... thinking";
         if ( !connectedToSession( peerInfo->sipInfo().nodeId() ) )
         {
             connectToPeer( peerInfo );
@@ -336,17 +336,17 @@ Servent::registerPeer( const Tomahawk::peerinfo_ptr& peerInfo )
     else
     {
         SipInfo info;
+        QString peerId = peerInfo->id();
+        QString key = uuid();
+        ControlConnection* conn = new ControlConnection( this );
+
+        const QString& nodeid = Database::instance()->impl()->dbid();
+        conn->setName( peerInfo->contactId() );
+        conn->setId( nodeid );
+        conn->addPeerInfo( peerInfo );
+
         if ( visibleExternally() )
         {
-            QString peerId = peerInfo->id();
-            QString key = uuid();
-            ControlConnection* conn = new ControlConnection( this );
-
-            const QString& nodeid = Database::instance()->impl()->dbid();
-            conn->setName( peerInfo->contactId() );
-            conn->setId( nodeid );
-            conn->addPeerInfo( peerInfo );
-
             registerOffer( key, conn );
             info.setVisible( true );
             info.setHost( externalAddress() );
@@ -414,6 +414,8 @@ void Servent::handleSipInfo( const Tomahawk::peerinfo_ptr& peerInfo )
     else
     {
         tDebug() << Q_FUNC_INFO << "They are not visible, doing nothing atm";
+        if ( peerInfo->controlConnection() )
+            delete peerInfo->controlConnection();
     }
 }
 
@@ -714,10 +716,13 @@ Servent::connectToPeer( const peerinfo_ptr& peerInfo )
     SipInfo sipInfo = peerInfo->sipInfo();
 
     peerInfoDebug( peerInfo ) << "connectToPeer: search for already established connections to the same nodeid:" << m_controlconnections.count() << "connections";
+    if ( peerInfo->controlConnection() )
+        delete peerInfo->controlConnection();
 
     bool isDupe = false;
     ControlConnection* conn = 0;
     // try to find a ControlConnection with the same SipInfo, then we dont need to try to connect again
+
     foreach ( ControlConnection* c, m_controlconnections )
     {
         Q_ASSERT( c );
@@ -769,7 +774,6 @@ Servent::connectToPeer( const peerinfo_ptr& peerInfo )
     m["nodeid"]    = Database::instance()->impl()->dbid();
 
     peerInfoDebug(peerInfo) << "No match found, creating a new ControlConnection...";
-
     conn = new ControlConnection( this );
     conn->addPeerInfo( peerInfo );
     conn->setFirstMessage( m );
@@ -787,7 +791,7 @@ Servent::connectToPeer( const peerinfo_ptr& peerInfo )
 
 
 void
-Servent::connectToPeer( const QString& ha, int port, const QString &key, Connection* conn )
+Servent::connectToPeer( const QString& ha, int port, const QString& key, Connection* conn )
 {
     tDebug( LOGVERBOSE ) << "Servent::connectToPeer:" << ha << ":" << port
                          << thread() << QThread::currentThread();
diff --git a/src/libtomahawk/network/Servent.h b/src/libtomahawk/network/Servent.h
index 7452fdb57..622c3f84d 100644
--- a/src/libtomahawk/network/Servent.h
+++ b/src/libtomahawk/network/Servent.h
@@ -118,8 +118,8 @@ public slots:
 
 public:
     void connectToPeer( const Tomahawk::peerinfo_ptr& ha );
-    void connectToPeer( const QString& ha, int port, const QString &key, Connection* conn );
-    void reverseOfferRequest( ControlConnection* orig_conn, const QString &theirdbid, const QString& key, const QString& theirkey );
+    void connectToPeer( const QString& ha, int port, const QString& key, Connection* conn );
+    void reverseOfferRequest( ControlConnection* orig_conn, const QString& theirdbid, const QString& key, const QString& theirkey );
 
     bool visibleExternally() const { return !m_externalHostname.isNull() || (m_externalPort > 0 && !m_externalAddress.isNull()); }
     QString externalAddress() const { return !m_externalHostname.isNull() ? m_externalHostname : m_externalAddress.toString(); }
diff --git a/src/libtomahawk/sip/PeerInfo.cpp b/src/libtomahawk/sip/PeerInfo.cpp
index 29e907062..0eec5a3fa 100644
--- a/src/libtomahawk/sip/PeerInfo.cpp
+++ b/src/libtomahawk/sip/PeerInfo.cpp
@@ -263,7 +263,7 @@ PeerInfo::setSipInfo( const SipInfo& sipInfo )
 
     m_sipInfo = sipInfo;
 
-    tLog() << "id: " << id() << " info changed" << sipInfo;
+    tLog() << "id:" << id() << "info changed" << sipInfo;
     emit sipInfoChanged();
 }
 
@@ -304,7 +304,7 @@ PeerInfo::setAvatar( const QPixmap& avatar )
 
     m_avatarHash = hash;
     m_avatarBuffer = ba;
-    
+
     delete m_avatar;
     delete m_fancyAvatar;
     m_avatar = 0;
@@ -331,7 +331,7 @@ PeerInfo::avatar( TomahawkUtils::ImageMode style, const QSize& size ) const
             delete m_avatar;
             m_avatar = 0;
         }
-        
+
         m_avatarBuffer.clear();
     }
 
diff --git a/src/libtomahawk/sip/SipInfo.cpp b/src/libtomahawk/sip/SipInfo.cpp
index ef26ee7b3..466dd3f15 100644
--- a/src/libtomahawk/sip/SipInfo.cpp
+++ b/src/libtomahawk/sip/SipInfo.cpp
@@ -35,12 +35,13 @@ public:
     {
     }
 
-    SipInfoPrivate( const SipInfoPrivate& other ) : QSharedData( other ),
-        visible(other.visible),
-        host(other.host),
-        port(other.port),
-        nodeId(other.nodeId),
-        key(other.key)
+    SipInfoPrivate( const SipInfoPrivate& other )
+        : QSharedData( other )
+        , visible( other.visible )
+        , host( other.host )
+        , port( other.port )
+        , nodeId( other.nodeId )
+        , key( other.key )
     {
     }
     ~SipInfoPrivate() { }
@@ -93,10 +94,10 @@ SipInfo::clear()
 bool
 SipInfo::isValid() const
 {
-//    qDebug() << Q_FUNC_INFO << d->visible << d->host.hostName() << d->port << d->nodeId << d->key;
-    if( !d->visible.isNull() )
+//    tDebug() << Q_FUNC_INFO << d->visible << d->host << d->port << d->nodeId << d->key;
+    if ( !d->visible.isNull() )
     {
-        if(
+        if (
             // visible and all data available
             (  d->visible.toBool() && !d->host.isEmpty() && ( d->port > 0 ) && !d->nodeId.isNull() && !d->key.isNull() )
             // invisible and no data available
@@ -112,7 +113,7 @@ SipInfo::isValid() const
 void
 SipInfo::setVisible( bool visible )
 {
-    d->visible.setValue(visible);
+    d->visible.setValue( visible );
 }
 
 
@@ -195,7 +196,7 @@ SipInfo::toJson() const
     // build variant map
     QVariantMap m;
     m["visible"] = isVisible();
-    if( isVisible() )
+    if ( isVisible() )
     {
         m["ip"] = host();
         m["port"] = port();
@@ -227,7 +228,7 @@ SipInfo::fromJson( QString json )
     QVariantMap m = v.toMap();
 
     info.setVisible( m["visible"].toBool() );
-    if( m["visible"].toBool() )
+    if ( m["visible"].toBool() )
     {
         info.setHost( m["host"].toString() );
         info.setPort( m["port"].toInt() );
@@ -242,7 +243,7 @@ SipInfo::fromJson( QString json )
 QDebug
 operator<< ( QDebug dbg, const SipInfo& info )
 {
-    if( !info.isValid() )
+    if ( !info.isValid() )
         dbg.nospace() << "info is invalid";
     else
         dbg.nospace() << info.toJson();
@@ -250,7 +251,9 @@ operator<< ( QDebug dbg, const SipInfo& info )
     return dbg.maybeSpace();
 }
 
-bool operator==( const SipInfo& one, const SipInfo& two )
+
+bool
+operator==( const SipInfo& one, const SipInfo& two )
 {
     // check valid/invalid combinations first, so we don't try to access any invalid sipInfos (->assert)
     if ( ( one.isValid() && !two.isValid() ) || ( !one.isValid() && two.isValid() ) )
@@ -272,6 +275,7 @@ bool operator==( const SipInfo& one, const SipInfo& two )
     return false;
 }
 
+
 const QString
 SipInfo::debugString() const
 {
@@ -281,6 +285,5 @@ SipInfo::debugString() const
                       .arg( d->port )
                       .arg( d->nodeId )
                       .arg( d->key );
-
 }