PageRenderTime 144ms CodeModel.GetById 40ms app.highlight 57ms RepoModel.GetById 44ms app.codeStats 0ms

/src/sip/twitter/twitterconfigwidget.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 270 lines | 217 code | 36 blank | 17 comment | 34 complexity | 132d0c613f39539f73f8d94619b36310 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "twitterconfigwidget.h"
 20#include "twitter.h"
 21#include "ui_twitterconfigwidget.h"
 22
 23#include "tomahawksettings.h"
 24#include "utils/tomahawkutils.h"
 25#include "database/database.h"
 26
 27#include "tomahawkoauthtwitter.h"
 28#include <QTweetLib/qtweetaccountverifycredentials.h>
 29#include <QTweetLib/qtweetstatusupdate.h>
 30#include <QTweetLib/qtweetdirectmessagenew.h>
 31
 32#include <QMessageBox>
 33
 34#include "utils/logger.h"
 35
 36
 37TwitterConfigWidget::TwitterConfigWidget( TwitterPlugin* plugin, QWidget *parent ) :
 38    QWidget( parent ),
 39    ui( new Ui::TwitterConfigWidget ),
 40    m_plugin( plugin )
 41{
 42    ui->setupUi( this );
 43
 44    connect( ui->twitterAuthenticateButton, SIGNAL( pressed() ),
 45            this, SLOT( authDeauthTwitter() ) );
 46    connect( ui->twitterTweetGotTomahawkButton, SIGNAL( pressed() ),
 47            this, SLOT( startPostGotTomahawkStatus() ) );
 48    connect( ui->twitterTweetComboBox, SIGNAL( currentIndexChanged( int ) ),
 49            this, SLOT( tweetComboBoxIndexChanged( int ) ) );
 50
 51    ui->twitterTweetComboBox->setCurrentIndex( 0 );
 52    ui->twitterTweetGotTomahawkButton->setText( tr( "Tweet!" ) );
 53
 54    if ( m_plugin->twitterOAuthToken().isEmpty() || m_plugin->twitterOAuthTokenSecret().isEmpty() || m_plugin->twitterScreenName().isEmpty() )
 55    {
 56        ui->twitterStatusLabel->setText( tr( "Status: No saved credentials" ) );
 57        ui->twitterAuthenticateButton->setText( tr( "Authenticate" ) );
 58        ui->twitterSyncGroupBox->setVisible( false );
 59
 60        emit twitterAuthed( false );
 61    }
 62    else
 63    {
 64        ui->twitterStatusLabel->setText( tr( "Status: Credentials saved for %1" ).arg( m_plugin->twitterScreenName() ) );
 65        ui->twitterAuthenticateButton->setText( tr( "De-authenticate" ) );
 66        ui->twitterSyncGroupBox->setVisible( true );
 67        ui->twitterUserTweetLineEdit->setVisible( false );
 68
 69        emit twitterAuthed( true );
 70    }
 71
 72}
 73
 74TwitterConfigWidget::~TwitterConfigWidget()
 75{
 76    delete ui;
 77}
 78
 79void
 80TwitterConfigWidget::authDeauthTwitter()
 81{
 82    if ( ui->twitterAuthenticateButton->text() == tr( "Authenticate" ) ) //FIXME: don't rely on UI strings here!
 83        authenticateTwitter();
 84    else
 85        deauthenticateTwitter();
 86}
 87
 88void
 89TwitterConfigWidget::authenticateTwitter()
 90{
 91    qDebug() << Q_FUNC_INFO;
 92    TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( TomahawkUtils::nam(), this );
 93    twitAuth->authorizePin();
 94
 95    m_plugin->setTwitterOAuthToken( twitAuth->oauthToken() );
 96    m_plugin->setTwitterOAuthTokenSecret( twitAuth->oauthTokenSecret() );
 97
 98    QTweetAccountVerifyCredentials *credVerifier = new QTweetAccountVerifyCredentials( twitAuth, this );
 99    connect( credVerifier, SIGNAL( parsedUser( const QTweetUser & ) ), SLOT( authenticateVerifyReply( const QTweetUser & ) ) );
100    connect( credVerifier, SIGNAL( error( QTweetNetBase::ErrorCode, QString ) ), SLOT( authenticateVerifyError( QTweetNetBase::ErrorCode, QString ) ) );
101    credVerifier->verify();
102}
103
104void
105TwitterConfigWidget::authenticateVerifyReply( const QTweetUser &user )
106{
107    qDebug() << Q_FUNC_INFO;
108    if ( user.id() == 0 )
109    {
110        QMessageBox::critical( this, tr("Tweetin' Error"), tr("The credentials could not be verified.\nYou may wish to try re-authenticating.") );
111        emit twitterAuthed( false );
112        return;
113    }
114
115    m_plugin->setTwitterScreenName( user.screenName() );
116    m_plugin->setTwitterCachedFriendsSinceId( 0 );
117    m_plugin->setTwitterCachedMentionsSinceId( 0 );
118
119    ui->twitterStatusLabel->setText( tr( "Status: Credentials saved for %1" ).arg( m_plugin->twitterScreenName() ) );
120    ui->twitterAuthenticateButton->setText( tr( "De-authenticate" ) );
121    ui->twitterSyncGroupBox->setVisible( true );
122    ui->twitterTweetComboBox->setCurrentIndex( 0 );
123    ui->twitterUserTweetLineEdit->setVisible( false );
124    ui->twitterTweetGotTomahawkButton->setText( tr( "Tweet!" ) );
125
126    m_plugin->connectPlugin( false );
127
128    emit twitterAuthed( true );
129    emit sizeHintChanged();
130}
131
132void
133TwitterConfigWidget::authenticateVerifyError( QTweetNetBase::ErrorCode code, const QString &errorMsg )
134{
135    qDebug() << Q_FUNC_INFO;
136    qDebug() << "Error validating credentials, error code is " << code << ", error message is " << errorMsg;
137    ui->twitterStatusLabel->setText(tr("Status: Error validating credentials"));
138    emit twitterAuthed( false );
139    return;
140}
141
142void
143TwitterConfigWidget::deauthenticateTwitter()
144{
145    qDebug() << Q_FUNC_INFO;
146    m_plugin->setTwitterOAuthToken( QString() );
147    m_plugin->setTwitterOAuthTokenSecret( QString() );
148    m_plugin->setTwitterScreenName( QString() );
149
150    ui->twitterStatusLabel->setText(tr("Status: No saved credentials"));
151    ui->twitterAuthenticateButton->setText( tr( "Authenticate" ) );
152    ui->twitterSyncGroupBox->setVisible( false );
153
154    emit twitterAuthed( false );
155    emit sizeHintChanged();
156}
157
158void
159TwitterConfigWidget::tweetComboBoxIndexChanged( int index )
160{
161    Q_UNUSED( index );
162    if ( ui->twitterTweetComboBox->currentText() == tr( "Global Tweet" ) ) //FIXME: use data!
163        ui->twitterUserTweetLineEdit->setVisible( false );
164    else
165        ui->twitterUserTweetLineEdit->setVisible( true );
166
167    if ( ui->twitterTweetComboBox->currentText() == tr( "Direct Message" ) ) //FIXME: use data!
168        ui->twitterTweetGotTomahawkButton->setText( tr( "Send Message!" ) );
169    else if ( ui->twitterTweetComboBox->currentText() == tr( "@Mention" ) )
170        ui->twitterTweetGotTomahawkButton->setText( tr( "Send Mention!" ) );
171    else
172        ui->twitterTweetGotTomahawkButton->setText( tr( "Tweet!" ) );
173}
174
175void
176TwitterConfigWidget::startPostGotTomahawkStatus()
177{
178    qDebug() << Q_FUNC_INFO;
179    m_postGTtype = ui->twitterTweetComboBox->currentText();
180
181    if ( m_postGTtype != "Global Tweet" && ( ui->twitterUserTweetLineEdit->text().isEmpty() || ui->twitterUserTweetLineEdit->text() == "@" ) )
182    {
183        QMessageBox::critical( this, tr("Tweetin' Error"), tr("You must enter a user name for this type of tweet.") );
184        return;
185    }
186
187    qDebug() << "Posting Got Tomahawk status";
188    if ( m_plugin->twitterOAuthToken().isEmpty() || m_plugin->twitterOAuthTokenSecret().isEmpty() || m_plugin->twitterScreenName().isEmpty() )
189    {
190        QMessageBox::critical( this, tr("Tweetin' Error"), tr("Your saved credentials could not be loaded.\nYou may wish to try re-authenticating.") );
191        emit twitterAuthed( false );
192        return;
193    }
194    TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( TomahawkUtils::nam(), this );
195    twitAuth->setOAuthToken( m_plugin->twitterOAuthToken().toLatin1() );
196    twitAuth->setOAuthTokenSecret( m_plugin->twitterOAuthTokenSecret().toLatin1() );
197    QTweetAccountVerifyCredentials *credVerifier = new QTweetAccountVerifyCredentials( twitAuth, this );
198    connect( credVerifier, SIGNAL( parsedUser(const QTweetUser &) ), SLOT( postGotTomahawkStatusAuthVerifyReply(const QTweetUser &) ) );
199    credVerifier->verify();
200}
201
202void
203TwitterConfigWidget::postGotTomahawkStatusAuthVerifyReply( const QTweetUser &user )
204{
205    qDebug() << Q_FUNC_INFO;
206    if ( user.id() == 0 )
207    {
208        QMessageBox::critical( this, tr("Tweetin' Error"), tr("Your saved credentials could not be verified.\nYou may wish to try re-authenticating.") );
209        emit twitterAuthed( false );
210        return;
211    }
212    m_plugin->setTwitterScreenName( user.screenName() );
213    TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( TomahawkUtils::nam(), this );
214    twitAuth->setOAuthToken( m_plugin->twitterOAuthToken().toLatin1() );
215    twitAuth->setOAuthTokenSecret( m_plugin->twitterOAuthTokenSecret().toLatin1() );
216    if ( m_postGTtype != "Direct Message" )
217    {
218        QTweetStatusUpdate *statUpdate = new QTweetStatusUpdate( twitAuth, this );
219        connect( statUpdate, SIGNAL( postedStatus(const QTweetStatus &) ), SLOT( postGotTomahawkStatusUpdateReply(const QTweetStatus &) ) );
220        connect( statUpdate, SIGNAL( error(QTweetNetBase::ErrorCode, const QString&) ), SLOT( postGotTomahawkStatusUpdateError(QTweetNetBase::ErrorCode, const QString &) ) );
221        QString uuid = QUuid::createUuid();
222        QString message = QString( "Got Tomahawk? {" ) + Database::instance()->dbid() + QString( "} (" ) + uuid.mid( 1, 8 ) + QString( ")" ) + QString( " http://gettomahawk.com" );
223        if ( m_postGTtype == "@Mention" )
224        {
225            QString user = ui->twitterUserTweetLineEdit->text();
226            if ( user.startsWith( "@" ) )
227                user.remove( 0, 1 );
228            message = QString( "@" ) + user + QString( " " ) + message;
229        }
230        statUpdate->post( message );
231    }
232    else
233    {
234        QTweetDirectMessageNew *statUpdate = new QTweetDirectMessageNew( twitAuth, this );
235        connect( statUpdate, SIGNAL( parsedDirectMessage(const QTweetDMStatus &)), SLOT( postGotTomahawkDirectMessageReply(const QTweetDMStatus &) ) );
236        connect( statUpdate, SIGNAL( error(QTweetNetBase::ErrorCode, const QString&) ), SLOT( postGotTomahawkStatusUpdateError(QTweetNetBase::ErrorCode, const QString &) ) );
237        QString uuid = QUuid::createUuid();
238        QString message = QString( "Got Tomahawk? {" ) + Database::instance()->dbid() + QString( "} (" ) + uuid.mid( 1, 8 ) + QString( ")" ) + QString( " http://gettomahawk.com" );
239        QString user = ui->twitterUserTweetLineEdit->text();
240        if ( user.startsWith( "@" ) )
241            user.remove( 0, 1 );
242        statUpdate->post( user, message );
243    }
244}
245
246void
247TwitterConfigWidget::postGotTomahawkStatusUpdateReply( const QTweetStatus& status )
248{
249    if ( status.id() == 0 )
250        QMessageBox::critical( this, tr("Tweetin' Error"), tr("There was an error posting your status -- sorry!") );
251    else
252        QMessageBox::information( this, tr("Tweeted!"), tr("Your tweet has been posted!") );
253}
254
255void
256TwitterConfigWidget::postGotTomahawkDirectMessageReply( const QTweetDMStatus& status )
257{
258    if ( status.id() == 0 )
259        QMessageBox::critical( this, tr("Tweetin' Error"), tr("There was an error posting your direct message -- sorry!") );
260    else
261        QMessageBox::information( this, tr("Tweeted!"), tr("Your message has been posted!") );
262}
263
264void
265TwitterConfigWidget::postGotTomahawkStatusUpdateError( QTweetNetBase::ErrorCode code, const QString& errorMsg )
266{
267    qDebug() << Q_FUNC_INFO;
268    qDebug() << "Error posting Got Tomahawk message, error code is " << code << ", error message is " << errorMsg;
269    QMessageBox::critical( this, tr("Tweetin' Error"), tr("There was an error posting your status -- sorry!") );
270}