1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-06 22:26:32 +02:00

Clean my plugin stuff up.

This commit is contained in:
Dominik Schmidt
2011-02-16 23:08:27 +01:00
parent 97e68bff49
commit aa1436eded
6 changed files with 311 additions and 3 deletions

3
src/sip/CMakeLists.txt Normal file
View File

@@ -0,0 +1,3 @@
ADD_SUBDIRECTORY( jabber )
ADD_SUBDIRECTORY( twitter )
ADD_SUBDIRECTORY( zeroconf )

View File

@@ -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<QString> peerlist = m_cachedPeers.keys();
qStableSort( peerlist.begin(), peerlist.end() );

View File

@@ -1,6 +1,8 @@
#ifndef TWITTER_H
#define TWITTER_H
#include "twitterconfigwidget.h"
#include <QTimer>
#include <QWeakPointer>
#include <QSet>
@@ -87,6 +89,8 @@ private:
QSet<QString> m_keyCache;
bool m_finishedFriends;
bool m_finishedMentions;
TwitterConfigWidget *m_configWidget;
};
#endif

View File

@@ -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 <qtweetaccountverifycredentials.h>
#include <qtweetstatusupdate.h>
#include <QMessageBox>
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!") );
}

View File

@@ -0,0 +1,37 @@
#ifndef TWITTERCONFIGWIDGET_H
#define TWITTERCONFIGWIDGET_H
#include "sip/SipPlugin.h"
#include <qtweetstatus.h>
#include <qtweetuser.h>
#include <qtweetnetbase.h>
#include <QWidget>
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

View File

@@ -0,0 +1,106 @@
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>TwitterConfigWidget</class>
<widget class="QWidget" name="TwitterConfigWidget">
<attribute name="title">
<string>Twitter</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_10">
<item>
<layout class="QVBoxLayout" name="twitterVertLayout">
<item>
<widget class="QLabel" name="twitterInfoLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>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 &quot;Network&quot; 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 &quot;clean up&quot; 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.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="twitterHorizontalLayout">
<item>
<widget class="QLabel" name="twitterStatusLabel">
<property name="text">
<string>Status: No saved credentials</string>
</property>
<property name="textFormat">
<enum>Qt::AutoText</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="twitterAuthenticateButton">
<property name="text">
<string>Authenticate with Twitter</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="twitterInstructionsBox">
<property name="title">
<string>Instructions</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
<item>
<layout class="QVBoxLayout" name="twitterInstructionsVLayout">
<item>
<widget class="QLabel" name="twitterInstructionsInfoLabel">
<property name="text">
<string>How it works is simple: just press the button below to tweet &quot;Got Tomahawk?&quot; 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.
</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="twitterTweetGotTomahawkButton">
<property name="text">
<string>Press here to have Tomahawk post a tweet</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="twitterVertSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<pixmapfunction/>
<connections/>
</ui>