From 49dcd70dd289f8536dc7c91eefca89be0a7b254e Mon Sep 17 00:00:00 2001 From: Alejandro Wainzinger Date: Thu, 11 Aug 2011 12:27:15 -0700 Subject: [PATCH] Implement most MPRIS methods, minus metadata and related methods. --- src/libtomahawk/audio/audioengine.cpp | 7 +- src/libtomahawk/audio/audioengine.h | 6 +- .../infoplugins/unix/mprisplugin.cpp | 101 +++++++++++++++--- .../infosystem/infoplugins/unix/mprisplugin.h | 3 +- .../unix/mprispluginplayeradaptor.cpp | 6 ++ .../unix/mprispluginplayeradaptor.h | 19 ++-- .../unix/mprispluginplayeradaptor.xml | 12 +-- src/libtomahawk/playlistinterface.h | 6 +- 8 files changed, 126 insertions(+), 34 deletions(-) diff --git a/src/libtomahawk/audio/audioengine.cpp b/src/libtomahawk/audio/audioengine.cpp index 49a55ca14..5b49cc4e2 100644 --- a/src/libtomahawk/audio/audioengine.cpp +++ b/src/libtomahawk/audio/audioengine.cpp @@ -224,7 +224,7 @@ AudioEngine::next() void -AudioEngine::seek( int ms ) +AudioEngine::seek( qint64 ms ) { if ( !m_playlist.isNull() && m_playlist.data()->seekRestrictions() == PlaylistInterface::NoSeek ) return; @@ -236,6 +236,11 @@ AudioEngine::seek( int ms ) } } +void +AudioEngine::seek( int ms ) +{ + seek( (qint64) ms ); +} void AudioEngine::setVolume( int percentage ) diff --git a/src/libtomahawk/audio/audioengine.h b/src/libtomahawk/audio/audioengine.h index be6a7ac44..410eec5d8 100644 --- a/src/libtomahawk/audio/audioengine.h +++ b/src/libtomahawk/audio/audioengine.h @@ -69,6 +69,9 @@ public: Tomahawk::result_ptr currentTrack() const { return m_currentTrack; } + qint64 currentTime() const { return m_mediaObject->currentTime(); } + qint64 currentTrackTotalTime() const { return m_mediaObject->totalTime(); } + public slots: void playPause(); void play(); @@ -78,7 +81,8 @@ public slots: void previous(); void next(); - void seek( int ms ); + void seek( qint64 ms ); + void seek( int ms ); // for compatibility with seekbar in audiocontrols void setVolume( int percentage ); void lowerVolume() { setVolume( volume() - AUDIO_VOLUME_STEP ); } void raiseVolume() { setVolume( volume() + AUDIO_VOLUME_STEP ); } diff --git a/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.cpp b/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.cpp index 3a0039b15..f8e1c1da4 100644 --- a/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.cpp +++ b/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.cpp @@ -16,8 +16,10 @@ * along with Tomahawk. If not, see . */ +#include #include +#include "audio/audioengine.h" #include "infosystem/infosystemworker.h" #include "artist.h" #include "result.h" @@ -58,7 +60,7 @@ bool MprisPlugin::canQuit() const { qDebug() << Q_FUNC_INFO; - return false; + return true; } bool @@ -109,6 +111,7 @@ MprisPlugin::Raise() void MprisPlugin::Quit() { + QApplication::quit(); } // org.mpris.MediaPlayer2.Player @@ -116,54 +119,83 @@ MprisPlugin::Quit() bool MprisPlugin::canControl() const { - return false; + return true; } bool MprisPlugin::canGoNext() const { - return false; + return true; } bool MprisPlugin::canGoPrevious() const { - return false; + return true; } bool MprisPlugin::canPause() const { - return false; + return true; } bool MprisPlugin::canPlay() const { - return false; + return true; } bool MprisPlugin::canSeek() const { - return false; + return true; } QString MprisPlugin::loopStatus() const { - return QString(""); + PlaylistInterface *p = AudioEngine::instance()->playlist(); + if (!p) + return "None"; + PlaylistInterface::RepeatMode mode = p->repeatMode(); + switch( mode ) + { + case PlaylistInterface::RepeatOne: + return "Track"; + break; + case PlaylistInterface::RepeatAll: + return "Playlist"; + break; + case PlaylistInterface::NoRepeat: + return "None"; + break; + default: + return QString("None"); + break; + } + + return QString("None"); } void -MprisPlugin::setLoopStatus(const QString &value) +MprisPlugin::setLoopStatus( const QString &value ) { + PlaylistInterface *p = AudioEngine::instance()->playlist(); + if (!p) + return; + if( value == "Track") + p->setRepeatMode( PlaylistInterface::RepeatOne ); + else if( value == "Playlist" ) + p->setRepeatMode( PlaylistInterface::RepeatAll ); + else if( value == "None" ) + p->setRepeatMode( PlaylistInterface::NoRepeat ); } double MprisPlugin::maximumRate() const { - return 0.0; + return 1.0; } QVariantMap @@ -175,92 +207,131 @@ MprisPlugin::metadata() const double MprisPlugin::minimumRate() const { - return 0.0; + return 1.0; } QString MprisPlugin::playbackStatus() const { + if( AudioEngine::instance()->state() == AudioEngine::Playing ) + return "Playing"; + else if( AudioEngine::instance()->state() == AudioEngine::Paused ) + return "Paused"; + else if( AudioEngine::instance()->state() == AudioEngine::Stopped ) + return "Stopped"; return QString(""); } qlonglong MprisPlugin::position() const { - return 0; + // Convert Tomahawk's milliseconds to microseconds + return (qlonglong) ( AudioEngine::instance()->currentTime() * 1000 ); } double MprisPlugin::rate() const { - return 0.0; + return 1.0; } void MprisPlugin::setRate( double value ) { + Q_UNUSED( value ); } bool MprisPlugin::shuffle() const { - return false; + PlaylistInterface *p = AudioEngine::instance()->playlist(); + if (!p) + return false; + return p->shuffled(); } void MprisPlugin::setShuffle( bool value ) { + PlaylistInterface *p = AudioEngine::instance()->playlist(); + if (!p) + return; + return p->setShuffled( value ); } double MprisPlugin::volume() const { - return 0.0; + return AudioEngine::instance()->volume(); +} + +void +MprisPlugin::setVolume( double value ) +{ + AudioEngine::instance()->setVolume( value ); } void MprisPlugin::Next() { + AudioEngine::instance()->next(); } void MprisPlugin::OpenUri(const QString &Uri) { + // TODO } void MprisPlugin::Pause() { + AudioEngine::instance()->pause(); } void MprisPlugin::Play() { + AudioEngine::instance()->play(); } void MprisPlugin::PlayPause() { + AudioEngine::instance()->playPause(); } void MprisPlugin::Previous() { + AudioEngine::instance()->previous(); } void MprisPlugin::Seek( qlonglong Offset ) { + qlonglong seekTime = position() + Offset; + if( seekTime < 0 ) + AudioEngine::instance()->seek( 0 ); + else if( seekTime > AudioEngine::instance()->currentTrackTotalTime() ) + Next(); + // seekTime is in microseconds, but we work internally in milliseconds + else + AudioEngine::instance()->seek( (qint64) ( seekTime / 1000 ) ); + + } void MprisPlugin::SetPosition( const QDBusObjectPath &TrackId, qlonglong Position ) { + // TODO } void MprisPlugin::Stop() { + AudioEngine::instance()->stop(); } // InfoPlugin Methods diff --git a/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.h b/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.h index 3168856d2..a1eca49a2 100644 --- a/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.h +++ b/src/libtomahawk/infosystem/infoplugins/unix/mprisplugin.h @@ -109,8 +109,9 @@ public: bool shuffle() const; void setShuffle(bool value); - Q_PROPERTY(double Volume READ volume) + Q_PROPERTY(double Volume READ volume WRITE setVolume) double volume() const; + void setVolume(double value); public slots: void namChangedSlot( QNetworkAccessManager* /*nam*/ ) {} // unused diff --git a/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.cpp b/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.cpp index 46f22cf2f..47e8c8c9f 100644 --- a/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.cpp +++ b/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.cpp @@ -141,6 +141,12 @@ double MprisPluginPlayerAdaptor::volume() const return qvariant_cast< double >(parent()->property("Volume")); } +void MprisPluginPlayerAdaptor::setVolume(double value) +{ + // set the value of property Volume + parent()->setProperty("Volume", qVariantFromValue(value)); +} + void MprisPluginPlayerAdaptor::Next() { // handle method call org.mpris.MediaPlayer2.Player.Next diff --git a/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.h b/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.h index 9445af3b6..d94cfc90e 100644 --- a/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.h +++ b/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.h @@ -9,8 +9,8 @@ * before re-generating it. */ -#ifndef MPRISPLUGINPLAYERADAPTOR_H_1312900500 -#define MPRISPLUGINPLAYERADAPTOR_H_1312900500 +#ifndef MPRISPLUGINPLAYERADAPTOR_H_1313089554 +#define MPRISPLUGINPLAYERADAPTOR_H_1313089554 #include #include @@ -37,17 +37,17 @@ class MprisPluginPlayerAdaptor: public QDBusAbstractAdaptor " \n" " \n" " \n" -" \n" +" \n" " \n" " \n" -" \n" -" \n" +" \n" +" \n" " \n" " \n" -" \n" +" \n" " \n" " \n" -" \n" +" \n" " \n" " \n" " \n" @@ -56,7 +56,7 @@ class MprisPluginPlayerAdaptor: public QDBusAbstractAdaptor " \n" " \n" " \n" -" \n" +" \n" " \n" " \n" " \n" @@ -118,8 +118,9 @@ public: // PROPERTIES bool shuffle() const; void setShuffle(bool value); - Q_PROPERTY(double Volume READ volume) + Q_PROPERTY(double Volume READ volume WRITE setVolume) double volume() const; + void setVolume(double value); public Q_SLOTS: // METHODS void Next(); diff --git a/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.xml b/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.xml index 8d060ae8c..4a7fc214b 100644 --- a/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.xml +++ b/src/libtomahawk/infosystem/infoplugins/unix/mprispluginplayeradaptor.xml @@ -8,17 +8,17 @@ - + - - + + - + - + @@ -27,7 +27,7 @@ - + diff --git a/src/libtomahawk/playlistinterface.h b/src/libtomahawk/playlistinterface.h index 9f46a3b11..f4c6650a1 100644 --- a/src/libtomahawk/playlistinterface.h +++ b/src/libtomahawk/playlistinterface.h @@ -36,12 +36,16 @@ class DLLEXPORT PlaylistInterface public: enum RepeatMode { NoRepeat, RepeatOne, RepeatAll }; + Q_ENUMS( RepeatMode ) enum ViewMode { Unknown, Tree, Flat, Album }; enum SeekRestrictions { NoSeekRestrictions, NoSeek }; enum SkipRestrictions { NoSkipRestrictions, NoSkipForwards, NoSkipBackwards, NoSkip }; enum RetryMode { NoRetry, Retry }; - PlaylistInterface( QObject* parent = 0 ) : m_object( parent ) {} + PlaylistInterface( QObject* parent = 0 ) : m_object( parent ) + { + qRegisterMetaType( "Tomahawk::PlaylistInterface::RepeatMode" ); + } virtual ~PlaylistInterface() {} virtual QList< Tomahawk::query_ptr > tracks() = 0;