PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 20ms RepoModel.GetById 41ms app.codeStats 0ms

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