/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. #include "twitterconfigwidget.h"
  19. #include "twitter.h"
  20. #include "ui_twitterconfigwidget.h"
  21. #include "tomahawksettings.h"
  22. #include "utils/tomahawkutils.h"
  23. #include "database/database.h"
  24. #include "tomahawkoauthtwitter.h"
  25. #include <QTweetLib/qtweetaccountverifycredentials.h>
  26. #include <QTweetLib/qtweetstatusupdate.h>
  27. #include <QTweetLib/qtweetdirectmessagenew.h>
  28. #include <QMessageBox>
  29. #include "utils/logger.h"
  30. TwitterConfigWidget::TwitterConfigWidget( TwitterPlugin* plugin, QWidget *parent ) :
  31. QWidget( parent ),
  32. ui( new Ui::TwitterConfigWidget ),
  33. m_plugin( plugin )
  34. {
  35. ui->setupUi( this );
  36. connect( ui->twitterAuthenticateButton, SIGNAL( pressed() ),
  37. this, SLOT( authDeauthTwitter() ) );
  38. connect( ui->twitterTweetGotTomahawkButton, SIGNAL( pressed() ),
  39. this, SLOT( startPostGotTomahawkStatus() ) );
  40. connect( ui->twitterTweetComboBox, SIGNAL( currentIndexChanged( int ) ),
  41. this, SLOT( tweetComboBoxIndexChanged( int ) ) );
  42. ui->twitterTweetComboBox->setCurrentIndex( 0 );
  43. ui->twitterTweetGotTomahawkButton->setText( tr( "Tweet!" ) );
  44. if ( m_plugin->twitterOAuthToken().isEmpty() || m_plugin->twitterOAuthTokenSecret().isEmpty() || m_plugin->twitterScreenName().isEmpty() )
  45. {
  46. ui->twitterStatusLabel->setText( tr( "Status: No saved credentials" ) );
  47. ui->twitterAuthenticateButton->setText( tr( "Authenticate" ) );
  48. ui->twitterSyncGroupBox->setVisible( false );
  49. emit twitterAuthed( false );
  50. }
  51. else
  52. {
  53. ui->twitterStatusLabel->setText( tr( "Status: Credentials saved for %1" ).arg( m_plugin->twitterScreenName() ) );
  54. ui->twitterAuthenticateButton->setText( tr( "De-authenticate" ) );
  55. ui->twitterSyncGroupBox->setVisible( true );
  56. ui->twitterUserTweetLineEdit->setVisible( false );
  57. emit twitterAuthed( true );
  58. }
  59. }
  60. TwitterConfigWidget::~TwitterConfigWidget()
  61. {
  62. delete ui;
  63. }
  64. void
  65. TwitterConfigWidget::authDeauthTwitter()
  66. {
  67. if ( ui->twitterAuthenticateButton->text() == tr( "Authenticate" ) ) //FIXME: don't rely on UI strings here!
  68. authenticateTwitter();
  69. else
  70. deauthenticateTwitter();
  71. }
  72. void
  73. TwitterConfigWidget::authenticateTwitter()
  74. {
  75. qDebug() << Q_FUNC_INFO;
  76. TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( TomahawkUtils::nam(), this );
  77. twitAuth->authorizePin();
  78. m_plugin->setTwitterOAuthToken( twitAuth->oauthToken() );
  79. m_plugin->setTwitterOAuthTokenSecret( twitAuth->oauthTokenSecret() );
  80. QTweetAccountVerifyCredentials *credVerifier = new QTweetAccountVerifyCredentials( twitAuth, this );
  81. connect( credVerifier, SIGNAL( parsedUser( const QTweetUser & ) ), SLOT( authenticateVerifyReply( const QTweetUser & ) ) );
  82. connect( credVerifier, SIGNAL( error( QTweetNetBase::ErrorCode, QString ) ), SLOT( authenticateVerifyError( QTweetNetBase::ErrorCode, QString ) ) );
  83. credVerifier->verify();
  84. }
  85. void
  86. TwitterConfigWidget::authenticateVerifyReply( const QTweetUser &user )
  87. {
  88. qDebug() << Q_FUNC_INFO;
  89. if ( user.id() == 0 )
  90. {
  91. QMessageBox::critical( this, tr("Tweetin' Error"), tr("The credentials could not be verified.\nYou may wish to try re-authenticating.") );
  92. emit twitterAuthed( false );
  93. return;
  94. }
  95. m_plugin->setTwitterScreenName( user.screenName() );
  96. m_plugin->setTwitterCachedFriendsSinceId( 0 );
  97. m_plugin->setTwitterCachedMentionsSinceId( 0 );
  98. ui->twitterStatusLabel->setText( tr( "Status: Credentials saved for %1" ).arg( m_plugin->twitterScreenName() ) );
  99. ui->twitterAuthenticateButton->setText( tr( "De-authenticate" ) );
  100. ui->twitterSyncGroupBox->setVisible( true );
  101. ui->twitterTweetComboBox->setCurrentIndex( 0 );
  102. ui->twitterUserTweetLineEdit->setVisible( false );
  103. ui->twitterTweetGotTomahawkButton->setText( tr( "Tweet!" ) );
  104. m_plugin->connectPlugin( false );
  105. emit twitterAuthed( true );
  106. emit sizeHintChanged();
  107. }
  108. void
  109. TwitterConfigWidget::authenticateVerifyError( QTweetNetBase::ErrorCode code, const QString &errorMsg )
  110. {
  111. qDebug() << Q_FUNC_INFO;
  112. qDebug() << "Error validating credentials, error code is " << code << ", error message is " << errorMsg;
  113. ui->twitterStatusLabel->setText(tr("Status: Error validating credentials"));
  114. emit twitterAuthed( false );
  115. return;
  116. }
  117. void
  118. TwitterConfigWidget::deauthenticateTwitter()
  119. {
  120. qDebug() << Q_FUNC_INFO;
  121. m_plugin->setTwitterOAuthToken( QString() );
  122. m_plugin->setTwitterOAuthTokenSecret( QString() );
  123. m_plugin->setTwitterScreenName( QString() );
  124. ui->twitterStatusLabel->setText(tr("Status: No saved credentials"));
  125. ui->twitterAuthenticateButton->setText( tr( "Authenticate" ) );
  126. ui->twitterSyncGroupBox->setVisible( false );
  127. emit twitterAuthed( false );
  128. emit sizeHintChanged();
  129. }
  130. void
  131. TwitterConfigWidget::tweetComboBoxIndexChanged( int index )
  132. {
  133. Q_UNUSED( index );
  134. if ( ui->twitterTweetComboBox->currentText() == tr( "Global Tweet" ) ) //FIXME: use data!
  135. ui->twitterUserTweetLineEdit->setVisible( false );
  136. else
  137. ui->twitterUserTweetLineEdit->setVisible( true );
  138. if ( ui->twitterTweetComboBox->currentText() == tr( "Direct Message" ) ) //FIXME: use data!
  139. ui->twitterTweetGotTomahawkButton->setText( tr( "Send Message!" ) );
  140. else if ( ui->twitterTweetComboBox->currentText() == tr( "@Mention" ) )
  141. ui->twitterTweetGotTomahawkButton->setText( tr( "Send Mention!" ) );
  142. else
  143. ui->twitterTweetGotTomahawkButton->setText( tr( "Tweet!" ) );
  144. }
  145. void
  146. TwitterConfigWidget::startPostGotTomahawkStatus()
  147. {
  148. qDebug() << Q_FUNC_INFO;
  149. m_postGTtype = ui->twitterTweetComboBox->currentText();
  150. if ( m_postGTtype != "Global Tweet" && ( ui->twitterUserTweetLineEdit->text().isEmpty() || ui->twitterUserTweetLineEdit->text() == "@" ) )
  151. {
  152. QMessageBox::critical( this, tr("Tweetin' Error"), tr("You must enter a user name for this type of tweet.") );
  153. return;
  154. }
  155. qDebug() << "Posting Got Tomahawk status";
  156. if ( m_plugin->twitterOAuthToken().isEmpty() || m_plugin->twitterOAuthTokenSecret().isEmpty() || m_plugin->twitterScreenName().isEmpty() )
  157. {
  158. QMessageBox::critical( this, tr("Tweetin' Error"), tr("Your saved credentials could not be loaded.\nYou may wish to try re-authenticating.") );
  159. emit twitterAuthed( false );
  160. return;
  161. }
  162. TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( TomahawkUtils::nam(), this );
  163. twitAuth->setOAuthToken( m_plugin->twitterOAuthToken().toLatin1() );
  164. twitAuth->setOAuthTokenSecret( m_plugin->twitterOAuthTokenSecret().toLatin1() );
  165. QTweetAccountVerifyCredentials *credVerifier = new QTweetAccountVerifyCredentials( twitAuth, this );
  166. connect( credVerifier, SIGNAL( parsedUser(const QTweetUser &) ), SLOT( postGotTomahawkStatusAuthVerifyReply(const QTweetUser &) ) );
  167. credVerifier->verify();
  168. }
  169. void
  170. TwitterConfigWidget::postGotTomahawkStatusAuthVerifyReply( const QTweetUser &user )
  171. {
  172. qDebug() << Q_FUNC_INFO;
  173. if ( user.id() == 0 )
  174. {
  175. QMessageBox::critical( this, tr("Tweetin' Error"), tr("Your saved credentials could not be verified.\nYou may wish to try re-authenticating.") );
  176. emit twitterAuthed( false );
  177. return;
  178. }
  179. m_plugin->setTwitterScreenName( user.screenName() );
  180. TomahawkOAuthTwitter *twitAuth = new TomahawkOAuthTwitter( TomahawkUtils::nam(), this );
  181. twitAuth->setOAuthToken( m_plugin->twitterOAuthToken().toLatin1() );
  182. twitAuth->setOAuthTokenSecret( m_plugin->twitterOAuthTokenSecret().toLatin1() );
  183. if ( m_postGTtype != "Direct Message" )
  184. {
  185. QTweetStatusUpdate *statUpdate = new QTweetStatusUpdate( twitAuth, this );
  186. connect( statUpdate, SIGNAL( postedStatus(const QTweetStatus &) ), SLOT( postGotTomahawkStatusUpdateReply(const QTweetStatus &) ) );
  187. connect( statUpdate, SIGNAL( error(QTweetNetBase::ErrorCode, const QString&) ), SLOT( postGotTomahawkStatusUpdateError(QTweetNetBase::ErrorCode, const QString &) ) );
  188. QString uuid = QUuid::createUuid();
  189. QString message = QString( "Got Tomahawk? {" ) + Database::instance()->dbid() + QString( "} (" ) + uuid.mid( 1, 8 ) + QString( ")" ) + QString( " http://gettomahawk.com" );
  190. if ( m_postGTtype == "@Mention" )
  191. {
  192. QString user = ui->twitterUserTweetLineEdit->text();
  193. if ( user.startsWith( "@" ) )
  194. user.remove( 0, 1 );
  195. message = QString( "@" ) + user + QString( " " ) + message;
  196. }
  197. statUpdate->post( message );
  198. }
  199. else
  200. {
  201. QTweetDirectMessageNew *statUpdate = new QTweetDirectMessageNew( twitAuth, this );
  202. connect( statUpdate, SIGNAL( parsedDirectMessage(const QTweetDMStatus &)), SLOT( postGotTomahawkDirectMessageReply(const QTweetDMStatus &) ) );
  203. connect( statUpdate, SIGNAL( error(QTweetNetBase::ErrorCode, const QString&) ), SLOT( postGotTomahawkStatusUpdateError(QTweetNetBase::ErrorCode, const QString &) ) );
  204. QString uuid = QUuid::createUuid();
  205. QString message = QString( "Got Tomahawk? {" ) + Database::instance()->dbid() + QString( "} (" ) + uuid.mid( 1, 8 ) + QString( ")" ) + QString( " http://gettomahawk.com" );
  206. QString user = ui->twitterUserTweetLineEdit->text();
  207. if ( user.startsWith( "@" ) )
  208. user.remove( 0, 1 );
  209. statUpdate->post( user, message );
  210. }
  211. }
  212. void
  213. TwitterConfigWidget::postGotTomahawkStatusUpdateReply( const QTweetStatus& status )
  214. {
  215. if ( status.id() == 0 )
  216. QMessageBox::critical( this, tr("Tweetin' Error"), tr("There was an error posting your status -- sorry!") );
  217. else
  218. QMessageBox::information( this, tr("Tweeted!"), tr("Your tweet has been posted!") );
  219. }
  220. void
  221. TwitterConfigWidget::postGotTomahawkDirectMessageReply( const QTweetDMStatus& status )
  222. {
  223. if ( status.id() == 0 )
  224. QMessageBox::critical( this, tr("Tweetin' Error"), tr("There was an error posting your direct message -- sorry!") );
  225. else
  226. QMessageBox::information( this, tr("Tweeted!"), tr("Your message has been posted!") );
  227. }
  228. void
  229. TwitterConfigWidget::postGotTomahawkStatusUpdateError( QTweetNetBase::ErrorCode code, const QString& errorMsg )
  230. {
  231. qDebug() << Q_FUNC_INFO;
  232. qDebug() << "Error posting Got Tomahawk message, error code is " << code << ", error message is " << errorMsg;
  233. QMessageBox::critical( this, tr("Tweetin' Error"), tr("There was an error posting your status -- sorry!") );
  234. }