From aa1436eded72c0cd8ae9f077c641a0c7886e70d4 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Wed, 16 Feb 2011 23:08:27 +0100 Subject: [PATCH] Clean my plugin stuff up. --- src/sip/CMakeLists.txt | 3 + src/sip/twitter/twitter.cpp | 25 ++++- src/sip/twitter/twitter.h | 4 + src/sip/twitter/twitterconfigwidget.cpp | 139 ++++++++++++++++++++++++ src/sip/twitter/twitterconfigwidget.h | 37 +++++++ src/sip/twitter/twitterconfigwidget.ui | 106 ++++++++++++++++++ 6 files changed, 311 insertions(+), 3 deletions(-) create mode 100644 src/sip/CMakeLists.txt create mode 100644 src/sip/twitter/twitterconfigwidget.cpp create mode 100644 src/sip/twitter/twitterconfigwidget.h create mode 100644 src/sip/twitter/twitterconfigwidget.ui diff --git a/src/sip/CMakeLists.txt b/src/sip/CMakeLists.txt new file mode 100644 index 000000000..e2b01058d --- /dev/null +++ b/src/sip/CMakeLists.txt @@ -0,0 +1,3 @@ +ADD_SUBDIRECTORY( jabber ) +ADD_SUBDIRECTORY( twitter ) +ADD_SUBDIRECTORY( zeroconf ) diff --git a/src/sip/twitter/twitter.cpp b/src/sip/twitter/twitter.cpp index 9ea960d92..9467a593e 100644 --- a/src/sip/twitter/twitter.cpp +++ b/src/sip/twitter/twitter.cpp @@ -29,13 +29,14 @@ TwitterPlugin::TwitterPlugin() , m_keyCache() , m_finishedFriends( false ) , m_finishedMentions( false ) + , m_configWidget( 0 ) { qDebug() << Q_FUNC_INFO; m_checkTimer.setInterval( 60000 ); m_checkTimer.setSingleShot( false ); connect( &m_checkTimer, SIGNAL( timeout() ), SLOT( checkTimerFired() ) ); m_checkTimer.start(); - + m_connectTimer.setInterval( 60000 ); m_connectTimer.setSingleShot( false ); connect( &m_connectTimer, SIGNAL( timeout() ), SLOT( connectTimerFired() ) ); @@ -55,19 +56,37 @@ TwitterPlugin::name() return QString( MYNAME ); } +const QString +TwitterPlugin::friendlyName() +{ + return QString("Twitter"); +} + const QString TwitterPlugin::accountName() { return QString( TomahawkSettings::instance()->twitterScreenName() ); } +QWidget* TwitterPlugin::configWidget() +{ + if( m_configWidget ) + return m_configWidget; + + m_configWidget = new TwitterConfigWidget( this ); + + return m_configWidget; +} + + + bool TwitterPlugin::connectPlugin( bool /*startup*/ ) { qDebug() << Q_FUNC_INFO; - + TomahawkSettings *settings = TomahawkSettings::instance(); - + m_cachedPeers = settings->twitterCachedPeers(); QList peerlist = m_cachedPeers.keys(); qStableSort( peerlist.begin(), peerlist.end() ); diff --git a/src/sip/twitter/twitter.h b/src/sip/twitter/twitter.h index c06942c99..05a64d35f 100644 --- a/src/sip/twitter/twitter.h +++ b/src/sip/twitter/twitter.h @@ -1,6 +1,8 @@ #ifndef TWITTER_H #define TWITTER_H +#include "twitterconfigwidget.h" + #include #include #include @@ -87,6 +89,8 @@ private: QSet m_keyCache; bool m_finishedFriends; bool m_finishedMentions; + + TwitterConfigWidget *m_configWidget; }; #endif diff --git a/src/sip/twitter/twitterconfigwidget.cpp b/src/sip/twitter/twitterconfigwidget.cpp new file mode 100644 index 000000000..6b193aa8a --- /dev/null +++ b/src/sip/twitter/twitterconfigwidget.cpp @@ -0,0 +1,139 @@ +#include "twitterconfigwidget.h" +#include "ui_twitterconfigwidget.h" + +#include "tomahawksettings.h" +#include "utils/tomahawkutils.h" +#include "database/database.h" + +#include "tomahawkoauthtwitter.h" +#include +#include + +#include + +TwitterConfigWidget::TwitterConfigWidget(SipPlugin* plugin, QWidget *parent) : + QWidget(parent), + ui(new Ui::TwitterConfigWidget), + m_plugin(plugin) +{ + ui->setupUi(this); + + connect(ui->twitterAuthenticateButton, SIGNAL(pressed()), + this, SLOT(authenticateTwitter())); + connect(ui->twitterTweetGotTomahawkButton, SIGNAL(pressed()), + this, SLOT(startPostGotTomahawkStatus())); + + + TomahawkSettings* s = TomahawkSettings::instance(); + if ( s->twitterOAuthToken().isEmpty() || s->twitterOAuthTokenSecret().isEmpty() ) + { + ui->twitterStatusLabel->setText("Status: No saved credentials"); + ui->twitterAuthenticateButton->setText( "Authenticate" ); + ui->twitterInstructionsBox->setVisible( false ); + } + else + { + ui->twitterStatusLabel->setText("Status: Credentials saved"); + ui->twitterAuthenticateButton->setText( "Re-authenticate" ); + ui->twitterInstructionsBox->setVisible( true ); + } + +} + +TwitterConfigWidget::~TwitterConfigWidget() +{ + delete ui; +} + +void +TwitterConfigWidget::authenticateTwitter() +{ + qDebug() << Q_FUNC_INFO; + TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( this ); + twitAuth->setNetworkAccessManager( TomahawkUtils::nam() ); + twitAuth->authorizePin(); + if ( !twitAuth->oauthToken().isEmpty() && !twitAuth->oauthTokenSecret().isEmpty() ) + { + TomahawkSettings* s = TomahawkSettings::instance(); + s->setTwitterOAuthToken( twitAuth->oauthToken() ); + s->setTwitterOAuthTokenSecret( twitAuth->oauthTokenSecret() ); + ui->twitterStatusLabel->setText("Status: Credentials saved"); + ui->twitterAuthenticateButton->setText( "Re-authenticate" ); + ui->twitterInstructionsBox->setVisible( true ); + TomahawkSettings::instance()->setTwitterCachedFriendsSinceId( 0 ); + TomahawkSettings::instance()->setTwitterCachedMentionsSinceId( 0 ); + m_plugin->connectPlugin( false ); + + } + else + { + TomahawkSettings* s = TomahawkSettings::instance(); + s->setTwitterOAuthToken( QString() ); + s->setTwitterOAuthTokenSecret( QString() ); + ui->twitterStatusLabel->setText("Status: No saved credentials"); + ui->twitterAuthenticateButton->setText( "Authenticate" ); + ui->twitterInstructionsBox->setVisible( false ); + QMessageBox::critical( 0, QString("Tweetin' Error"), QString("There was an error validating your authentication") ); + } +} + + +void +TwitterConfigWidget::startPostGotTomahawkStatus() +{ + qDebug() << "Posting Got Tomahawk status"; + TomahawkSettings* s = TomahawkSettings::instance(); + if ( s->twitterOAuthToken().isEmpty() || s->twitterOAuthTokenSecret().isEmpty() ) + { + QMessageBox::critical( 0, QString("Tweetin' Error"), QString("Your saved credentials could not be loaded.\nYou may wish to try re-authenticating.") ); + return; + } + TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( this ); + twitAuth->setNetworkAccessManager( TomahawkUtils::nam() ); + twitAuth->setOAuthToken( s->twitterOAuthToken().toLatin1() ); + twitAuth->setOAuthTokenSecret( s->twitterOAuthTokenSecret().toLatin1() ); + QTweetAccountVerifyCredentials *credVerifier = new QTweetAccountVerifyCredentials( twitAuth, this ); + connect( credVerifier, SIGNAL( parsedUser(const QTweetUser &) ), SLOT( postGotTomahawkStatusAuthVerifyReply(const QTweetUser &) ) ); + credVerifier->verify(); +} + + +void +TwitterConfigWidget::postGotTomahawkStatusAuthVerifyReply( const QTweetUser &user ) +{ + if ( user.id() == 0 ) + { + QMessageBox::critical( 0, QString("Tweetin' Error"), QString("Your saved credentials could not be verified.\nYou may wish to try re-authenticating.") ); + return; + } + TomahawkSettings* s = TomahawkSettings::instance(); + s->setTwitterScreenName( user.screenName() ); + TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( this ); + twitAuth->setNetworkAccessManager( TomahawkUtils::nam() ); + twitAuth->setOAuthToken( s->twitterOAuthToken().toLatin1() ); + twitAuth->setOAuthTokenSecret( s->twitterOAuthTokenSecret().toLatin1() ); + QTweetStatusUpdate *statUpdate = new QTweetStatusUpdate( twitAuth, this ); + connect( statUpdate, SIGNAL( postedStatus(const QTweetStatus &) ), SLOT( postGotTomahawkStatusUpdateReply(const QTweetStatus &) ) ); + connect( statUpdate, SIGNAL( error(QTweetNetBase::ErrorCode, const QString&) ), SLOT( postGotTomahawkStatusUpdateError(QTweetNetBase::ErrorCode, const QString &) ) ); + QString uuid = QUuid::createUuid(); + statUpdate->post( QString( "Got Tomahawk? {" ) + Database::instance()->dbid() + QString( "} (" ) + uuid.mid( 1, 8 ) + QString( ")" ) ); +} + + +void +TwitterConfigWidget::postGotTomahawkStatusUpdateReply( const QTweetStatus& status ) +{ + if ( status.id() == 0 ) + QMessageBox::critical( 0, QString("Tweetin' Error"), QString("There was an error posting your status -- sorry!") ); + else + QMessageBox::information( 0, QString("Tweeted!"), QString("Your tweet has been posted!") ); +} + + +void +TwitterConfigWidget::postGotTomahawkStatusUpdateError( QTweetNetBase::ErrorCode code, const QString& errorMsg ) +{ + qDebug() << Q_FUNC_INFO; + qDebug() << "Error posting Got Tomahawk message, error code is " << code << ", error message is " << errorMsg; + QMessageBox::critical( 0, QString("Tweetin' Error"), QString("There was an error posting your status -- sorry!") ); +} diff --git a/src/sip/twitter/twitterconfigwidget.h b/src/sip/twitter/twitterconfigwidget.h new file mode 100644 index 000000000..355c45777 --- /dev/null +++ b/src/sip/twitter/twitterconfigwidget.h @@ -0,0 +1,37 @@ +#ifndef TWITTERCONFIGWIDGET_H +#define TWITTERCONFIGWIDGET_H + +#include "sip/SipPlugin.h" + +#include +#include +#include + +#include + + +namespace Ui { + class TwitterConfigWidget; +} + +class TwitterConfigWidget : public QWidget +{ + Q_OBJECT + +public: + explicit TwitterConfigWidget(SipPlugin* plugin = 0, QWidget *parent = 0); + ~TwitterConfigWidget(); + +private slots: + void authenticateTwitter(); + void startPostGotTomahawkStatus(); + void postGotTomahawkStatusAuthVerifyReply( const QTweetUser &user ); + void postGotTomahawkStatusUpdateReply( const QTweetStatus &status ); + void postGotTomahawkStatusUpdateError( QTweetNetBase::ErrorCode, const QString &errorMsg ); + +private: + Ui::TwitterConfigWidget *ui; + SipPlugin *m_plugin; +}; + +#endif // TWITTERCONFIGWIDGET_H diff --git a/src/sip/twitter/twitterconfigwidget.ui b/src/sip/twitter/twitterconfigwidget.ui new file mode 100644 index 000000000..ad35c9d55 --- /dev/null +++ b/src/sip/twitter/twitterconfigwidget.ui @@ -0,0 +1,106 @@ + + + + + TwitterConfigWidget + + + Twitter + + + + + + + + + 0 + 0 + + + + Authenticating with Twitter allows you to discover and play music from your Twitter friends running Tomahawk. + +This feature works best when you have set a static host name in the "Network" settings tab under Advanced Settings, but may work even if you do not. Please note: this discovery uses Direct Messages and will only work when both Twitter users have followed each other. Tomahawk will attempt to "clean up" after itself to keep your Direct Message inbox tidy, but may miss some. + +When you press the button your web browser will launch and take you to Twitter.com to authenticate. You must copy and paste the PIN number into the dialog box that appears. + + + true + + + + + + + + + Status: No saved credentials + + + Qt::AutoText + + + + + + + Authenticate with Twitter + + + + + + + + + + + Instructions + + + + + + + + How it works is simple: just press the button below to tweet "Got Tomahawk?" and some necessary information. Then be (very) patient. Twitter is an asynchronous protocol so it can take a bit! + +If connections to peers seem to have been lost, just press the button again to re-post a tweet for resynchronization. + + + + true + + + + + + + Press here to have Tomahawk post a tweet + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + \ No newline at end of file