diff --git a/src/libtomahawk/tomahawksettings.cpp b/src/libtomahawk/tomahawksettings.cpp index c707237ab..18a5deedf 100644 --- a/src/libtomahawk/tomahawksettings.cpp +++ b/src/libtomahawk/tomahawksettings.cpp @@ -482,6 +482,20 @@ TomahawkSettings::setTwitterCachedMentionsSinceId( qint64 cachedId ) setValue( "twitter/cachedmentionssinceid", cachedId ); } +QHash +TomahawkSettings::twitterCachedPeers() const +{ + QMutexLocker locker( m_safety ); + return value( "twitter/cachedpeers", QHash() ).toHash(); +} + +void +TomahawkSettings::setTwitterCachedPeers( const QHash &cachedPeers ) +{ + QMutexLocker locker( m_safety ); + setValue( "twitter/cachedpeers", cachedPeers ); +} + bool TomahawkSettings::scrobblingEnabled() const { diff --git a/src/libtomahawk/tomahawksettings.h b/src/libtomahawk/tomahawksettings.h index 5aae2a546..25ea4c937 100644 --- a/src/libtomahawk/tomahawksettings.h +++ b/src/libtomahawk/tomahawksettings.h @@ -109,6 +109,9 @@ public: qint64 twitterCachedMentionsSinceId() const; void setTwitterCachedMentionsSinceId( qint64 sinceid ); + QHash twitterCachedPeers() const; + void setTwitterCachedPeers( const QHash &cachedPeers ); + /// XMPP Component Settings QString xmppBotServer() const; void setXmppBotServer( const QString &server ); diff --git a/src/sip/SipHandler.cpp b/src/sip/SipHandler.cpp index 51b118bc2..59f846638 100644 --- a/src/sip/SipHandler.cpp +++ b/src/sip/SipHandler.cpp @@ -21,6 +21,7 @@ SipHandler::SipHandler( QObject* parent ) SipHandler::~SipHandler() { + disconnectPlugins(); } diff --git a/src/sip/twitter/twitter.cpp b/src/sip/twitter/twitter.cpp index 60311d0d8..7bba948c7 100644 --- a/src/sip/twitter/twitter.cpp +++ b/src/sip/twitter/twitter.cpp @@ -14,11 +14,13 @@ TwitterPlugin::TwitterPlugin() : SipPlugin() - , m_isValid( false ) + , m_isAuthed( false ) , m_checkTimer( this ) , m_cachedFriendsSinceId( 0 ) , m_cachedMentionsSinceId( 0 ) + , m_cachedPeers() { + qDebug() << Q_FUNC_INFO; m_checkTimer.setInterval( 60000 ); m_checkTimer.setSingleShot( false ); QObject::connect( &m_checkTimer, SIGNAL( timeout() ), SLOT( checkTimerFired() ) ); @@ -28,7 +30,7 @@ TwitterPlugin::TwitterPlugin() bool TwitterPlugin::isValid() { - return m_isValid; + return m_isAuthed || !m_cachedPeers.isEmpty(); } const QString @@ -41,14 +43,22 @@ TwitterPlugin::name() bool TwitterPlugin::connectPlugin( bool /*startup*/ ) { - qDebug() << "TwitterPlugin connectPlugin called"; + qDebug() << Q_FUNC_INFO; TomahawkSettings *settings = TomahawkSettings::instance(); + m_cachedPeers = settings->twitterCachedPeers(); + QList peerlist = m_cachedPeers.keys(); + qStableSort( peerlist.begin(), peerlist.end() ); + foreach( QString key, peerlist ) + { + qDebug() << "TwitterPlugin found cached peer with host " << m_cachedPeers[key].toHash()["host"].toString() << " and port " << m_cachedPeers[key].toHash()["port"].toString(); + } + if ( settings->twitterOAuthToken().isEmpty() || settings->twitterOAuthTokenSecret().isEmpty() ) { - qDebug() << "Empty Twitter credentials; not connecting"; - return false; + qDebug() << "TwitterPlugin has empty Twitter credentials; not connecting"; + return m_cachedPeers.isEmpty(); } delete m_twitterAuth.data(); @@ -71,10 +81,14 @@ TwitterPlugin::connectPlugin( bool /*startup*/ ) void TwitterPlugin::disconnectPlugin() { + qDebug() << Q_FUNC_INFO; if( !m_friendsTimeline.isNull() ) m_friendsTimeline.data()->deleteLater(); if( !m_twitterAuth.isNull() ) m_twitterAuth.data()->deleteLater(); + + TomahawkSettings::instance()->setTwitterCachedPeers( m_cachedPeers ); + m_cachedPeers.empty(); } void @@ -82,13 +96,13 @@ TwitterPlugin::connectAuthVerifyReply( const QTweetUser &user ) { if ( user.id() == 0 ) { - qDebug() << "Could not authenticate to Twitter"; - m_isValid = false; + qDebug() << "TwitterPlugin could not authenticate to Twitter"; + m_isAuthed = false; } else { qDebug() << "TwitterPlugin successfully authenticated to Twitter as user " << user.screenName(); - m_isValid = true; + m_isAuthed = true; if ( !m_twitterAuth.isNull() ) { m_friendsTimeline = QWeakPointer( new QTweetFriendsTimeline( m_twitterAuth.data(), this ) ); @@ -99,8 +113,8 @@ TwitterPlugin::connectAuthVerifyReply( const QTweetUser &user ) } else { - qDebug() << "Twitter auth pointer was null!"; - m_isValid = false; + qDebug() << "TwitterPlugin auth pointer was null!"; + m_isAuthed = false; } } } @@ -112,11 +126,13 @@ TwitterPlugin::checkTimerFired() { if ( m_cachedFriendsSinceId == 0 ) m_cachedFriendsSinceId = TomahawkSettings::instance()->twitterCachedFriendsSinceId(); + qDebug() << "TwitterPlugin using friend timeline id of " << m_cachedFriendsSinceId; if ( !m_friendsTimeline.isNull() ) m_friendsTimeline.data()->fetch( m_cachedFriendsSinceId, 0, 800 ); if ( m_cachedMentionsSinceId == 0 ) m_cachedMentionsSinceId = TomahawkSettings::instance()->twitterCachedMentionsSinceId(); + qDebug() << "TwitterPlugin using mentions timeline id of " << m_cachedMentionsSinceId; if ( !m_mentions.isNull() ) m_mentions.data()->fetch( m_cachedMentionsSinceId, 0, 800 ); } @@ -125,7 +141,7 @@ TwitterPlugin::checkTimerFired() void TwitterPlugin::friendsTimelineStatuses( const QList< QTweetStatus >& statuses ) { - qDebug() << "TwitterPlugin checking friends"; + qDebug() << Q_FUNC_INFO; QRegExp regex( QString( "^(@[a-zA-Z0-9]+ )?Got Tomahawk\\?(.*)$" ) ); foreach( QTweetStatus status, statuses ) { @@ -135,6 +151,10 @@ TwitterPlugin::friendsTimelineStatuses( const QList< QTweetStatus >& statuses ) if ( regex.exactMatch( statusText ) ) { qDebug() << "TwitterPlugin found an exact tweet from friend " << status.user().screenName(); + QHash peerData; + peerData["host"] = QVariant::fromValue( "somehost" ); + peerData["port"] = QVariant::fromValue( "port" ); + m_cachedPeers[status.user().screenName()] = QVariant::fromValue< QHash< QString, QVariant > >( peerData ); } } @@ -144,7 +164,7 @@ TwitterPlugin::friendsTimelineStatuses( const QList< QTweetStatus >& statuses ) void TwitterPlugin::mentionsStatuses( const QList< QTweetStatus >& statuses ) { - qDebug() << "TwitterPlugin checking mentions"; + qDebug() << Q_FUNC_INFO; QRegExp regex( QString( "^(@[a-zA-Z0-9]+ )?Got Tomahawk\\?(.*)$" ) ); foreach( QTweetStatus status, statuses ) { @@ -154,6 +174,10 @@ TwitterPlugin::mentionsStatuses( const QList< QTweetStatus >& statuses ) if ( regex.exactMatch( statusText ) ) { qDebug() << "TwitterPlugin found an exact matching mention from user " << status.user().screenName(); + QHash peerData; + peerData["host"] = QVariant::fromValue( "somehost" ); + peerData["port"] = QVariant::fromValue( "port" ); + m_cachedPeers[status.user().screenName()] = QVariant::fromValue< QHash< QString, QVariant > >( peerData ); } } diff --git a/src/sip/twitter/twitter.h b/src/sip/twitter/twitter.h index 89d56f2cc..49d489e27 100644 --- a/src/sip/twitter/twitter.h +++ b/src/sip/twitter/twitter.h @@ -56,10 +56,11 @@ private: QWeakPointer m_twitterAuth; QWeakPointer m_friendsTimeline; QWeakPointer m_mentions; - bool m_isValid; + bool m_isAuthed; QTimer m_checkTimer; qint64 m_cachedFriendsSinceId; qint64 m_cachedMentionsSinceId; + QHash m_cachedPeers; }; #endif