diff --git a/src/libtomahawk/tomahawksettings.cpp b/src/libtomahawk/tomahawksettings.cpp index 18a5deedf..8f2f86941 100644 --- a/src/libtomahawk/tomahawksettings.cpp +++ b/src/libtomahawk/tomahawksettings.cpp @@ -430,70 +430,84 @@ QString TomahawkSettings::twitterOAuthToken() const { QMutexLocker locker( m_safety ); - return value( "twitter/oauthtoken" ).toString(); + return value( "twitter/OAuthToken" ).toString(); } void TomahawkSettings::setTwitterOAuthToken( const QString& oauthtoken ) { QMutexLocker locker( m_safety ); - setValue( "twitter/oauthtoken", oauthtoken ); + setValue( "twitter/OAuthToken", oauthtoken ); } QString TomahawkSettings::twitterOAuthTokenSecret() const { QMutexLocker locker( m_safety ); - return value( "twitter/oauthtokensecret" ).toString(); + return value( "twitter/OAuthTokenSecret" ).toString(); } void TomahawkSettings::setTwitterOAuthTokenSecret( const QString& oauthtokensecret ) { QMutexLocker locker( m_safety ); - setValue( "twitter/oauthtokensecret", oauthtokensecret ); + setValue( "twitter/OAuthTokenSecret", oauthtokensecret ); } qint64 TomahawkSettings::twitterCachedFriendsSinceId() const { QMutexLocker locker( m_safety ); - return value( "twitter/cachedfriendssinceid", 0 ).toLongLong(); + return value( "twitter/CachedFriendsSinceID", 0 ).toLongLong(); } void TomahawkSettings::setTwitterCachedFriendsSinceId( qint64 cachedId ) { QMutexLocker locker( m_safety ); - setValue( "twitter/cachedfriendssinceid", cachedId ); + setValue( "twitter/CachedFriendsSinceID", cachedId ); } qint64 TomahawkSettings::twitterCachedMentionsSinceId() const { QMutexLocker locker( m_safety ); - return value( "twitter/cachedmentionssinceid", 0 ).toLongLong(); + return value( "twitter/CachedMentionsSinceID", 0 ).toLongLong(); } void TomahawkSettings::setTwitterCachedMentionsSinceId( qint64 cachedId ) { QMutexLocker locker( m_safety ); - setValue( "twitter/cachedmentionssinceid", cachedId ); + setValue( "twitter/CachedMentionsSinceID", cachedId ); +} + +qint64 +TomahawkSettings::twitterCachedDirectMessagesSinceId() const +{ + QMutexLocker locker( m_safety ); + return value( "twitter/CachedDirectMessagesSinceID", 0 ).toLongLong(); +} + +void +TomahawkSettings::setTwitterCachedDirectMessagesSinceId( qint64 cachedId ) +{ + QMutexLocker locker( m_safety ); + setValue( "twitter/CachedDirectMessagesSinceID", cachedId ); } QHash TomahawkSettings::twitterCachedPeers() const { QMutexLocker locker( m_safety ); - return value( "twitter/cachedpeers", QHash() ).toHash(); + return value( "twitter/CachedPeers", QHash() ).toHash(); } void TomahawkSettings::setTwitterCachedPeers( const QHash &cachedPeers ) { QMutexLocker locker( m_safety ); - setValue( "twitter/cachedpeers", cachedPeers ); + setValue( "twitter/CachedPeers", cachedPeers ); } bool diff --git a/src/libtomahawk/tomahawksettings.h b/src/libtomahawk/tomahawksettings.h index 25ea4c937..f08b3be0c 100644 --- a/src/libtomahawk/tomahawksettings.h +++ b/src/libtomahawk/tomahawksettings.h @@ -109,6 +109,9 @@ public: qint64 twitterCachedMentionsSinceId() const; void setTwitterCachedMentionsSinceId( qint64 sinceid ); + qint64 twitterCachedDirectMessagesSinceId() const; + void setTwitterCachedDirectMessagesSinceId( qint64 sinceid ); + QHash twitterCachedPeers() const; void setTwitterCachedPeers( const QHash &cachedPeers ); diff --git a/src/sip/twitter/twitter.cpp b/src/sip/twitter/twitter.cpp index 7bba948c7..4ee4a7636 100644 --- a/src/sip/twitter/twitter.cpp +++ b/src/sip/twitter/twitter.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -18,12 +17,15 @@ TwitterPlugin::TwitterPlugin() , m_checkTimer( this ) , m_cachedFriendsSinceId( 0 ) , m_cachedMentionsSinceId( 0 ) + , m_cachedDirectMessagesSinceId( 0 ) , m_cachedPeers() + , m_finishedFriends( false ) + , m_finishedMentions( false ) { qDebug() << Q_FUNC_INFO; m_checkTimer.setInterval( 60000 ); m_checkTimer.setSingleShot( false ); - QObject::connect( &m_checkTimer, SIGNAL( timeout() ), SLOT( checkTimerFired() ) ); + connect( &m_checkTimer, SIGNAL( timeout() ), SLOT( checkTimerFired() ) ); m_checkTimer.start(); } @@ -68,7 +70,7 @@ TwitterPlugin::connectPlugin( bool /*startup*/ ) m_twitterAuth.data()->setOAuthTokenSecret( settings->twitterOAuthTokenSecret().toLatin1() ); QTweetAccountVerifyCredentials *credVerifier = new QTweetAccountVerifyCredentials( m_twitterAuth.data(), this ); - QObject::connect( credVerifier, SIGNAL( parsedUser(const QTweetUser &) ), SLOT( connectAuthVerifyReply(const QTweetUser &) ) ); + connect( credVerifier, SIGNAL( parsedUser(const QTweetUser &) ), SLOT( connectAuthVerifyReply(const QTweetUser &) ) ); credVerifier->verify(); /* QObject::connect( m_zeroconf, SIGNAL( tomahawkHostFound( const QString&, int, const QString&, const QString& ) ), @@ -107,9 +109,11 @@ TwitterPlugin::connectAuthVerifyReply( const QTweetUser &user ) { m_friendsTimeline = QWeakPointer( new QTweetFriendsTimeline( m_twitterAuth.data(), this ) ); m_mentions = QWeakPointer( new QTweetMentions( m_twitterAuth.data(), this ) ); - QObject::connect( m_friendsTimeline.data(), SIGNAL( parsedStatuses(const QList< QTweetStatus > &) ), SLOT( friendsTimelineStatuses(const QList &) ) ); - QObject::connect( m_mentions.data(), SIGNAL( parsedStatuses(const QList< QTweetStatus > &) ), SLOT( mentionsStatuses(const QList &) ) ); - QMetaObject::invokeMethod( this, "checkTimerFired", Qt::DirectConnection ); + m_directMessages = QWeakPointer( new QTweetDirectMessages( m_twitterAuth.data(), this ) ); + connect( m_friendsTimeline.data(), SIGNAL( parsedStatuses(const QList< QTweetStatus > &) ), SLOT( friendsTimelineStatuses(const QList &) ) ); + connect( m_mentions.data(), SIGNAL( parsedStatuses(const QList< QTweetStatus > &) ), SLOT( mentionsStatuses(const QList &) ) ); + connect( m_directMessages.data(), SIGNAL( parsedStatuses(const QList< QTweetDMStatus > &) ), SLOT( mentionsStatuses(const QList &) ) ); + QMetaObject::invokeMethod( this, "checkTimerFired", Qt::AutoConnection ); } else { @@ -122,24 +126,24 @@ TwitterPlugin::connectAuthVerifyReply( const QTweetUser &user ) void TwitterPlugin::checkTimerFired() { - if ( isValid() ) - { - 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 ); - } + if ( !isValid() ) + return; + + 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 ); } void -TwitterPlugin::friendsTimelineStatuses( const QList< QTweetStatus >& statuses ) +TwitterPlugin::friendsTimelineStatuses( const QList< QTweetStatus > &statuses ) { qDebug() << Q_FUNC_INFO; QRegExp regex( QString( "^(@[a-zA-Z0-9]+ )?Got Tomahawk\\?(.*)$" ) ); @@ -147,22 +151,27 @@ TwitterPlugin::friendsTimelineStatuses( const QList< QTweetStatus >& statuses ) { if ( status.id() > m_cachedFriendsSinceId ) m_cachedFriendsSinceId = status.id(); - QString statusText = status.text(); - if ( regex.exactMatch( statusText ) ) + if ( regex.exactMatch( status.text() ) ) { 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 ); + if ( ! m_cachedPeers.contains( status.user().screenName() ) ) + { + QHash< QString, QVariant > peerData; + peerData["sentuid"] = QVariant::fromValue< QString >( uuid() ); + m_cachedPeers[status.user().screenName()] = QVariant::fromValue< QHash< QString, QVariant > >( peerData ); + } } } TomahawkSettings::instance()->setTwitterCachedFriendsSinceId( m_cachedFriendsSinceId ); + + m_finishedFriends = true; + QMetaObject::invokeMethod( this, "pollDirectMessages", Qt::AutoConnection ); } void -TwitterPlugin::mentionsStatuses( const QList< QTweetStatus >& statuses ) +TwitterPlugin::mentionsStatuses( const QList< QTweetStatus > &statuses ) { qDebug() << Q_FUNC_INFO; QRegExp regex( QString( "^(@[a-zA-Z0-9]+ )?Got Tomahawk\\?(.*)$" ) ); @@ -170,18 +179,66 @@ TwitterPlugin::mentionsStatuses( const QList< QTweetStatus >& statuses ) { if ( status.id() > m_cachedMentionsSinceId ) m_cachedMentionsSinceId = status.id(); - QString statusText = status.text(); - if ( regex.exactMatch( statusText ) ) + if ( regex.exactMatch( status.text() ) ) { 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 ); + if ( ! m_cachedPeers.contains( status.user().screenName() ) ) + { + QHash< QString, QVariant > peerData; + peerData["sentuid"] = QVariant::fromValue< QString >( uuid() ); + m_cachedPeers[status.user().screenName()] = QVariant::fromValue< QHash< QString, QVariant > >( peerData ); + } } } TomahawkSettings::instance()->setTwitterCachedMentionsSinceId( m_cachedMentionsSinceId ); + + m_finishedMentions = true; + QMetaObject::invokeMethod( this, "pollDirectMessages", Qt::AutoConnection ); +} + +void +TwitterPlugin::pollDirectMessages() +{ + if ( !m_finishedMentions || !m_finishedFriends ) + return; + + m_finishedFriends = false; + m_finishedMentions = false; + + if ( !isValid() ) + return; + + if ( m_cachedDirectMessagesSinceId == 0 ) + m_cachedDirectMessagesSinceId = TomahawkSettings::instance()->twitterCachedDirectMessagesSinceId(); + qDebug() << "TwitterPlugin using direct messages id of " << m_cachedDirectMessagesSinceId; + if ( !m_directMessages.isNull() ) + m_directMessages.data()->fetch( m_cachedDirectMessagesSinceId, 0, 800 ); +} + +void +TwitterPlugin::directMessages( const QList< QTweetDMStatus > &messages ) +{ + qDebug() << Q_FUNC_INFO; + QRegExp regex( QString( "^(@[a-zA-Z0-9]+ )?Got Tomahawk\\?(.*)$" ) ); + foreach( QTweetDMStatus status, messages ) + { + if ( status.id() > m_cachedDirectMessagesSinceId ) + m_cachedDirectMessagesSinceId = status.id(); + if ( regex.exactMatch( status.text() ) ) + { + qDebug() << "TwitterPlugin found an exact matching mention from user " << status.sender().screenName(); + if ( ! m_cachedPeers.contains( status.sender().screenName() ) ) + { + QHash< QString, QVariant > peerData; + peerData["sentuid"] = QVariant::fromValue< QString >( uuid() ); + m_cachedPeers[status.sender().screenName()] = QVariant::fromValue< QHash< QString, QVariant > >( peerData ); + } + } + } + + TomahawkSettings::instance()->setTwitterCachedDirectMessagesSinceId( m_cachedDirectMessagesSinceId ); + } void diff --git a/src/sip/twitter/twitter.h b/src/sip/twitter/twitter.h index 49d489e27..9e100b16f 100644 --- a/src/sip/twitter/twitter.h +++ b/src/sip/twitter/twitter.h @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include #include "../sipdllmacro.h" #include "sip/SipPlugin.h" @@ -51,16 +53,22 @@ private slots: void checkTimerFired(); void friendsTimelineStatuses( const QList< QTweetStatus > &statuses ); void mentionsStatuses( const QList< QTweetStatus > &statuses ); + void pollDirectMessages(); + void directMessages( const QList< QTweetDMStatus > &messages ); private: - QWeakPointer m_twitterAuth; - QWeakPointer m_friendsTimeline; - QWeakPointer m_mentions; + QWeakPointer< TomahawkOAuthTwitter > m_twitterAuth; + QWeakPointer< QTweetFriendsTimeline > m_friendsTimeline; + QWeakPointer< QTweetMentions > m_mentions; + QWeakPointer< QTweetDirectMessages > m_directMessages; bool m_isAuthed; QTimer m_checkTimer; qint64 m_cachedFriendsSinceId; qint64 m_cachedMentionsSinceId; - QHash m_cachedPeers; + qint64 m_cachedDirectMessagesSinceId; + QHash< QString, QVariant > m_cachedPeers; + bool m_finishedFriends; + bool m_finishedMentions; }; #endif