/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 227 lines · 155 code · 46 blank · 26 comment · 14 complexity · c0601b1d0f775c5b8ab0b9f97538da31 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 <string.h>
  19. #include <QTimer>
  20. #include "infosystem/infosystemworker.h"
  21. #include "artist.h"
  22. #include "result.h"
  23. #include "tomahawksettings.h"
  24. #include "globalactionmanager.h"
  25. #include "utils/logger.h"
  26. #include "adiumplugin.h"
  27. #include "adium.h"
  28. QString adium_beforeStatus;
  29. QString adium_afterStatus;
  30. static void setStatus(const QString &status)
  31. {
  32. // The command that updates the status
  33. QString scriptqstr;
  34. scriptqstr.append(adium_beforeStatus);
  35. scriptqstr.append(status);
  36. scriptqstr.append(adium_afterStatus);
  37. const char* scriptstr = scriptqstr.toUtf8();
  38. script( scriptstr );
  39. }
  40. using namespace Tomahawk::InfoSystem;
  41. AdiumPlugin::AdiumPlugin()
  42. : InfoPlugin()
  43. {
  44. qDebug() << Q_FUNC_INFO;
  45. adium_beforeStatus = "if appIsRunning(\"Adium\") then\n";
  46. adium_beforeStatus.append("tell application \"Adium\"\n");
  47. adium_beforeStatus.append("set the status message of every account to \"");
  48. adium_afterStatus.append("\"\nend tell\n");
  49. adium_afterStatus.append("end if\n");
  50. adium_afterStatus.append("on appIsRunning(appName)\n");
  51. adium_afterStatus.append("tell application \"System Events\" to (name of processes) contains appName\n");
  52. adium_afterStatus.append("end appIsRunning\n");
  53. m_supportedPushTypes << InfoNowPlaying << InfoNowPaused << InfoNowResumed << InfoNowStopped;
  54. m_active = TomahawkSettings::instance()->nowPlayingEnabled();
  55. connect( TomahawkSettings::instance(), SIGNAL( changed() ),
  56. SLOT( settingsChanged() ), Qt::QueuedConnection );
  57. m_pauseTimer = new QTimer( this );
  58. m_pauseTimer->setSingleShot( true );
  59. connect( m_pauseTimer, SIGNAL( timeout() ),
  60. this, SLOT( clearStatus() ) );
  61. connect( GlobalActionManager::instance(), SIGNAL( shortLinkReady( QUrl, QUrl ) ),
  62. SLOT( shortLinkReady( QUrl, QUrl ) ) );
  63. }
  64. AdiumPlugin::~AdiumPlugin()
  65. {
  66. qDebug() << Q_FUNC_INFO;
  67. if( m_active )
  68. setStatus( "" );
  69. }
  70. void
  71. AdiumPlugin::shortLinkReady( QUrl longUrl, QUrl shortUrl )
  72. {
  73. // The URL we received is either from a previous track, or not requested by us
  74. if( longUrl != m_currentLongUrl )
  75. return;
  76. // Build the core of the now-playing string
  77. QString nowPlaying = "";
  78. nowPlaying.append( m_currentArtist );
  79. nowPlaying.append(" - ");
  80. nowPlaying.append( m_currentTitle );
  81. nowPlaying.replace( "\"", "\\\"" ); // Escape quotes, or Applescript gets confused
  82. // We failed to get the short URL, just update the status with the metadata
  83. if( ( longUrl.toString() == "" ) )
  84. {
  85. setStatus( nowPlaying );
  86. return;
  87. }
  88. // Add the short URL
  89. nowPlaying.append( " " );
  90. nowPlaying.append( shortUrl.toEncoded() );
  91. setStatus( nowPlaying );
  92. }
  93. void
  94. AdiumPlugin::clearStatus()
  95. {
  96. qDebug() << Q_FUNC_INFO;
  97. setStatus( "" );
  98. }
  99. void
  100. AdiumPlugin::settingsChanged()
  101. {
  102. m_active = TomahawkSettings::instance()->nowPlayingEnabled();
  103. }
  104. void
  105. AdiumPlugin::pushInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant input )
  106. {
  107. qDebug() << Q_FUNC_INFO;
  108. if( !m_active )
  109. return;
  110. switch ( type )
  111. {
  112. case InfoNowPlaying:
  113. audioStarted( input );
  114. break;
  115. case InfoNowPaused:
  116. audioPaused();
  117. return;
  118. case InfoNowResumed:
  119. audioResumed( input );
  120. break;
  121. case InfoNowStopped:
  122. audioStopped();
  123. break;
  124. default:
  125. return;
  126. }
  127. // Stop the pause timer always, unless pausing of course
  128. m_pauseTimer->stop();
  129. }
  130. /** Audio state slots */
  131. void
  132. AdiumPlugin::audioStarted( const QVariant &input )
  133. {
  134. qDebug() << Q_FUNC_INFO;
  135. if ( !input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
  136. return;
  137. InfoStringHash hash = input.value< Tomahawk::InfoSystem::InfoStringHash >();
  138. if ( !hash.contains( "title" ) || !hash.contains( "artist" ) )
  139. return;
  140. m_currentTitle = hash["title"];
  141. m_currentArtist = hash["artist"];
  142. // Request a short URL
  143. m_currentLongUrl = openLinkFromHash( hash );
  144. GlobalActionManager::instance()->shortenLink( m_currentLongUrl );
  145. }
  146. QUrl
  147. AdiumPlugin::openLinkFromHash( const Tomahawk::InfoSystem::InfoStringHash& hash ) const
  148. {
  149. QString title, artist, album;
  150. if( !hash.isEmpty() && hash.contains( "title" ) && hash.contains( "artist" ) )
  151. {
  152. title = hash["title"];
  153. artist = hash["artist"];
  154. if( hash.contains( "album" ) )
  155. album = hash["album"];
  156. }
  157. return GlobalActionManager::instance()->openLink( title, artist, album );
  158. }
  159. void
  160. AdiumPlugin::audioFinished( const QVariant &input )
  161. {
  162. //qDebug() << Q_FUNC_INFO;
  163. }
  164. void
  165. AdiumPlugin::audioStopped()
  166. {
  167. qDebug() << Q_FUNC_INFO;
  168. setStatus( "" );
  169. }
  170. void
  171. AdiumPlugin::audioPaused()
  172. {
  173. qDebug() << Q_FUNC_INFO;
  174. m_pauseTimer->start( 60 * 1000 );
  175. }
  176. void
  177. AdiumPlugin::audioResumed( const QVariant &input )
  178. {
  179. qDebug() << Q_FUNC_INFO;
  180. audioStarted( input );
  181. }