2010-03-24 21:58:17 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 21:58:17 +01:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mpris.h"
|
2010-12-04 23:27:58 +01:00
|
|
|
#include "mpris_common.h"
|
2010-03-24 21:58:17 +01:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2010-11-21 16:13:26 +01:00
|
|
|
#include <QDBusConnection>
|
2010-03-24 21:58:17 +01:00
|
|
|
|
2010-11-21 16:13:26 +01:00
|
|
|
#include "core/mpris_player.h"
|
2010-06-09 00:39:31 +02:00
|
|
|
#include "core/mpris_root.h"
|
2010-11-21 16:13:26 +01:00
|
|
|
#include "core/mpris_tracklist.h"
|
2010-12-04 23:27:58 +01:00
|
|
|
#include "engines/enginebase.h"
|
2010-11-21 16:13:26 +01:00
|
|
|
#include "playlist/playlist.h"
|
|
|
|
#include "playlist/playlistmanager.h"
|
|
|
|
#include "playlist/playlistsequence.h"
|
2010-03-24 21:58:17 +01:00
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
namespace mpris {
|
|
|
|
|
|
|
|
Mpris1::Mpris1(Player* player, ArtLoader* art_loader, QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
qDBusRegisterMetaType<DBusStatus>();
|
|
|
|
qDBusRegisterMetaType<Version>();
|
|
|
|
|
|
|
|
QDBusConnection::sessionBus().registerService("org.mpris.clementine");
|
|
|
|
root_ = new Mpris1Root(player, this);
|
|
|
|
player_ = new Mpris1Player(player, this);
|
|
|
|
tracklist_ = new Mpris1TrackList(player, this);
|
|
|
|
|
|
|
|
connect(art_loader, SIGNAL(ArtLoaded(Song,QString)),
|
|
|
|
player_, SLOT(CurrentSongChanged(Song,QString)));
|
2010-03-24 21:58:17 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
Mpris1Root::Mpris1Root(Player* player, QObject* parent)
|
|
|
|
: QObject(parent), player_(player) {
|
|
|
|
new MprisRoot(this);
|
|
|
|
QDBusConnection::sessionBus().registerObject("/", this);
|
2010-03-24 21:58:17 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
Mpris1Player::Mpris1Player(Player* player, QObject* parent)
|
|
|
|
: QObject(parent), player_(player) {
|
|
|
|
new MprisPlayer(this);
|
|
|
|
QDBusConnection::sessionBus().registerObject("/Player", this);
|
2010-11-21 16:13:26 +01:00
|
|
|
|
2010-12-07 22:14:40 +01:00
|
|
|
connect(player->engine(), SIGNAL(StateChanged(Engine::State)), SLOT(EngineStateChanged(Engine::State)));
|
|
|
|
connect(player_->playlists(), SIGNAL(PlaylistManagerInitialized()), SLOT(PlaylistManagerInitialized()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// when PlaylistManager gets it ready, we connect PlaylistSequence with this
|
|
|
|
void Mpris1Player::PlaylistManagerInitialized() {
|
|
|
|
connect(player_->playlists()->sequence(), SIGNAL(ShuffleModeChanged(PlaylistSequence::ShuffleMode)),
|
|
|
|
SLOT(ShuffleModeChanged()));
|
|
|
|
connect(player_->playlists()->sequence(), SIGNAL(RepeatModeChanged(PlaylistSequence::RepeatMode)),
|
|
|
|
SLOT(RepeatModeChanged()));
|
2010-03-24 21:58:17 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
Mpris1TrackList::Mpris1TrackList(Player* player, QObject* parent)
|
|
|
|
: QObject(parent), player_(player) {
|
|
|
|
new MprisTrackList(this);
|
|
|
|
QDBusConnection::sessionBus().registerObject("/TrackList", this);
|
|
|
|
|
2010-12-08 01:09:17 +01:00
|
|
|
connect(player->playlists(), SIGNAL(PlaylistChanged(Playlist*)), SLOT(PlaylistChanged(Playlist*)));
|
2010-12-04 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2010-12-08 01:09:17 +01:00
|
|
|
void Mpris1TrackList::PlaylistChanged(Playlist* playlist) {
|
|
|
|
emit TrackListChange(playlist->rowCount());
|
2010-12-04 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2010-12-07 22:14:40 +01:00
|
|
|
// we use the state from event and don't try to obtain it from Player
|
|
|
|
// later because only the event's version is really the current one
|
|
|
|
void Mpris1Player::EngineStateChanged(Engine::State state) {
|
|
|
|
emit StatusChange(GetStatus(state));
|
|
|
|
emit CapsChange(GetCaps(state));
|
2010-12-04 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mpris1Player::CurrentSongChanged(const Song& song, const QString& art_uri) {
|
|
|
|
last_metadata_ = Mpris1::GetMetadata(song);
|
|
|
|
|
|
|
|
if (!art_uri.isEmpty()) {
|
|
|
|
AddMetadata("arturl", art_uri, &last_metadata_);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit TrackChange(last_metadata_);
|
|
|
|
emit StatusChange(GetStatus());
|
|
|
|
emit CapsChange(GetCaps());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString Mpris1Root::Identity() {
|
2010-03-24 22:46:00 +01:00
|
|
|
return QString("%1 %2").arg(
|
|
|
|
QCoreApplication::applicationName(),
|
|
|
|
QCoreApplication::applicationVersion());
|
2010-03-24 21:58:17 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
Version Mpris1Root::MprisVersion() {
|
2010-03-24 21:58:17 +01:00
|
|
|
Version version;
|
|
|
|
version.major = 1;
|
|
|
|
version.minor = 0;
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Root::Quit() {
|
2010-03-24 21:58:17 +01:00
|
|
|
qApp->quit();
|
|
|
|
}
|
2010-11-21 16:13:26 +01:00
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::Pause() {
|
|
|
|
player_->Pause();
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::Stop() {
|
2010-11-21 16:13:26 +01:00
|
|
|
player_->Stop();
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::Prev() {
|
2010-12-05 12:39:06 +01:00
|
|
|
player_->Previous();
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::Play() {
|
|
|
|
player_->Play();
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::Next() {
|
2010-11-21 16:13:26 +01:00
|
|
|
player_->Next();
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::Repeat(bool repeat) {
|
2010-12-05 12:39:06 +01:00
|
|
|
player_->playlists()->sequence()->SetRepeatMode(
|
|
|
|
repeat ? PlaylistSequence::Repeat_Track : PlaylistSequence::Repeat_Off);
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-07 22:14:40 +01:00
|
|
|
void Mpris1Player::ShuffleModeChanged() {
|
|
|
|
emit StatusChange(GetStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mpris1Player::RepeatModeChanged() {
|
|
|
|
emit StatusChange(GetStatus());
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
DBusStatus Mpris1Player::GetStatus() const {
|
2010-12-07 22:14:40 +01:00
|
|
|
return GetStatus(player_->GetState());
|
|
|
|
}
|
|
|
|
|
|
|
|
DBusStatus Mpris1Player::GetStatus(Engine::State state) const {
|
2010-11-21 16:13:26 +01:00
|
|
|
DBusStatus status;
|
2010-12-07 22:14:40 +01:00
|
|
|
switch (state) {
|
2010-11-21 16:13:26 +01:00
|
|
|
case Engine::Empty:
|
|
|
|
case Engine::Idle:
|
|
|
|
status.play = DBusStatus::Mpris_Stopped;
|
|
|
|
break;
|
|
|
|
case Engine::Playing:
|
|
|
|
status.play = DBusStatus::Mpris_Playing;
|
|
|
|
break;
|
|
|
|
case Engine::Paused:
|
|
|
|
status.play = DBusStatus::Mpris_Paused;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
PlaylistManager* playlists_ = player_->playlists();
|
2010-11-21 16:13:26 +01:00
|
|
|
PlaylistSequence::RepeatMode repeat_mode = playlists_->sequence()->repeat_mode();
|
|
|
|
|
|
|
|
status.random = playlists_->sequence()->shuffle_mode() == PlaylistSequence::Shuffle_Off ? 0 : 1;
|
|
|
|
status.repeat = repeat_mode == PlaylistSequence::Repeat_Track ? 1 : 0;
|
|
|
|
status.repeat_playlist = (repeat_mode == PlaylistSequence::Repeat_Album ||
|
2010-12-03 16:01:36 +01:00
|
|
|
repeat_mode == PlaylistSequence::Repeat_Playlist ||
|
|
|
|
repeat_mode == PlaylistSequence::Repeat_Track) ? 1 : 0;
|
2010-11-21 16:13:26 +01:00
|
|
|
return status;
|
|
|
|
|
|
|
|
}
|
2010-12-04 23:27:58 +01:00
|
|
|
|
|
|
|
void Mpris1Player::VolumeSet(int volume) {
|
2010-11-21 16:13:26 +01:00
|
|
|
player_->SetVolume(volume);
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
int Mpris1Player::VolumeGet() const {
|
2010-12-05 12:39:06 +01:00
|
|
|
return player_->GetVolume();
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
2010-12-04 23:27:58 +01:00
|
|
|
|
|
|
|
void Mpris1Player::PositionSet(int pos) {
|
2010-11-21 16:13:26 +01:00
|
|
|
player_->Seek(pos/1000);
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
int Mpris1Player::PositionGet() const {
|
2010-12-05 12:39:06 +01:00
|
|
|
return player_->engine()->position();
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
QVariantMap Mpris1Player::GetMetadata() const {
|
|
|
|
return last_metadata_;
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
int Mpris1Player::GetCaps() const {
|
2010-12-07 22:14:40 +01:00
|
|
|
return GetCaps(player_->GetState());
|
|
|
|
}
|
|
|
|
|
|
|
|
int Mpris1Player::GetCaps(Engine::State state) const {
|
2010-11-21 16:13:26 +01:00
|
|
|
int caps = CAN_HAS_TRACKLIST;
|
2010-12-04 23:27:58 +01:00
|
|
|
PlaylistItemPtr current_item = player_->GetCurrentItem();
|
|
|
|
PlaylistManager* playlists = player_->playlists();
|
2010-11-21 16:13:26 +01:00
|
|
|
|
2010-12-07 22:14:40 +01:00
|
|
|
// play is disabled when playlist is empty or when last.fm stream is already playing
|
|
|
|
if (playlists->active()->rowCount() != 0
|
|
|
|
&& !(state == Engine::Playing && (player_->GetCurrentItem()->options() & PlaylistItem::LastFMControls))) {
|
2010-11-21 16:13:26 +01:00
|
|
|
caps |= CAN_PLAY;
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
if (current_item) {
|
2010-11-21 16:13:26 +01:00
|
|
|
caps |= CAN_PROVIDE_METADATA;
|
2010-12-05 12:39:06 +01:00
|
|
|
if (state == Engine::Playing && !(current_item->options() & PlaylistItem::PauseDisabled)) {
|
2010-11-21 16:13:26 +01:00
|
|
|
caps |= CAN_PAUSE;
|
|
|
|
}
|
2010-12-04 23:27:58 +01:00
|
|
|
if (state != Engine::Empty && current_item->Metadata().filetype() != Song::Type_Stream) {
|
2010-11-21 16:13:26 +01:00
|
|
|
caps |= CAN_SEEK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 01:21:20 +01:00
|
|
|
if (playlists->active()->next_row() != -1 ||
|
2010-12-04 23:27:58 +01:00
|
|
|
playlists->active()->current_item_options() & PlaylistItem::ContainsMultipleTracks) {
|
2010-11-21 16:13:26 +01:00
|
|
|
caps |= CAN_GO_NEXT;
|
|
|
|
}
|
2010-12-17 01:21:20 +01:00
|
|
|
if (playlists->active()->previous_row() != -1) {
|
2010-11-21 16:13:26 +01:00
|
|
|
caps |= CAN_GO_PREV;
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
2010-12-05 12:39:06 +01:00
|
|
|
void Mpris1Player::VolumeUp(int change) {
|
|
|
|
VolumeSet(VolumeGet() + change);
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-05 12:39:06 +01:00
|
|
|
void Mpris1Player::VolumeDown(int change) {
|
|
|
|
VolumeSet(VolumeGet() - change);
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::Mute() {
|
2010-11-21 16:13:26 +01:00
|
|
|
player_->Mute();
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1Player::ShowOSD() {
|
2010-11-21 16:13:26 +01:00
|
|
|
player_->ShowOSD();
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
int Mpris1TrackList::AddTrack(const QString& track, bool play) {
|
2010-12-05 12:39:06 +01:00
|
|
|
player_->playlists()->active()->InsertUrls(
|
|
|
|
QList<QUrl>() << QUrl(track), play);
|
|
|
|
return 0;
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1TrackList::DelTrack(int index) {
|
2010-12-05 12:39:06 +01:00
|
|
|
player_->playlists()->active()->removeRows(index, 1);
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
int Mpris1TrackList::GetCurrentTrack() const {
|
2010-12-17 01:21:20 +01:00
|
|
|
return player_->playlists()->active()->current_row();
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
int Mpris1TrackList::GetLength() const {
|
|
|
|
return player_->playlists()->active()->rowCount();
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
QVariantMap Mpris1TrackList::GetMetadata(int pos) const {
|
2010-12-11 11:58:15 +01:00
|
|
|
PlaylistItemPtr item = player_->GetItemAt(pos);
|
|
|
|
if (!item)
|
|
|
|
return QVariantMap();
|
|
|
|
|
|
|
|
return Mpris1::GetMetadata(item->Metadata());
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1TrackList::SetLoop(bool enable) {
|
2010-12-05 12:39:06 +01:00
|
|
|
player_->playlists()->active()->sequence()->SetRepeatMode(
|
|
|
|
enable ? PlaylistSequence::Repeat_Playlist : PlaylistSequence::Repeat_Off);
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1TrackList::SetRandom(bool enable) {
|
2010-12-05 12:39:06 +01:00
|
|
|
player_->playlists()->active()->sequence()->SetShuffleMode(
|
|
|
|
enable ? PlaylistSequence::Shuffle_All : PlaylistSequence::Shuffle_Off);
|
2010-11-21 16:13:26 +01:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
void Mpris1TrackList::PlayTrack(int index) {
|
2010-11-21 16:13:26 +01:00
|
|
|
player_->PlayAt(index, Engine::Manual, true);
|
|
|
|
}
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
QVariantMap Mpris1::GetMetadata(const Song& song) {
|
2010-11-21 16:13:26 +01:00
|
|
|
QVariantMap ret;
|
|
|
|
|
2010-12-04 23:27:58 +01:00
|
|
|
AddMetadata("location", song.filename(), &ret);
|
2010-11-21 16:13:26 +01:00
|
|
|
AddMetadata("title", song.PrettyTitle(), &ret);
|
|
|
|
AddMetadata("artist", song.artist(), &ret);
|
|
|
|
AddMetadata("album", song.album(), &ret);
|
|
|
|
AddMetadata("time", song.length(), &ret);
|
2010-12-04 23:27:58 +01:00
|
|
|
AddMetadata("mtime", song.length() * 1000, &ret);
|
2010-11-21 16:13:26 +01:00
|
|
|
AddMetadata("tracknumber", song.track(), &ret);
|
|
|
|
AddMetadata("year", song.year(), &ret);
|
|
|
|
AddMetadata("genre", song.genre(), &ret);
|
|
|
|
AddMetadata("disc", song.disc(), &ret);
|
|
|
|
AddMetadata("comment", song.comment(), &ret);
|
2010-12-03 16:01:36 +01:00
|
|
|
AddMetadata("audio-bitrate", song.bitrate(), &ret);
|
|
|
|
AddMetadata("audio-samplerate", song.samplerate(), &ret);
|
2010-11-21 16:13:26 +01:00
|
|
|
AddMetadata("bpm", song.bpm(), &ret);
|
|
|
|
AddMetadata("composer", song.composer(), &ret);
|
2010-12-03 16:01:36 +01:00
|
|
|
if (song.rating() != -1.0) {
|
2010-12-04 23:27:58 +01:00
|
|
|
AddMetadata("rating", song.rating() * 5, &ret);
|
2010-12-03 16:01:36 +01:00
|
|
|
}
|
2010-11-21 16:13:26 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-04 23:27:58 +01:00
|
|
|
|
|
|
|
} // namespace mpris
|
|
|
|
|
|
|
|
|
|
|
|
QDBusArgument& operator<< (QDBusArgument& arg, const Version& version) {
|
|
|
|
arg.beginStructure();
|
|
|
|
arg << version.major << version.minor;
|
|
|
|
arg.endStructure();
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QDBusArgument& operator>> (const QDBusArgument& arg, Version& version) {
|
|
|
|
arg.beginStructure();
|
|
|
|
arg >> version.major >> version.minor;
|
|
|
|
arg.endStructure();
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDBusArgument& operator<< (QDBusArgument& arg, const DBusStatus& status) {
|
|
|
|
arg.beginStructure();
|
|
|
|
arg << status.play;
|
|
|
|
arg << status.random;
|
|
|
|
arg << status.repeat;
|
|
|
|
arg << status.repeat_playlist;
|
|
|
|
arg.endStructure();
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QDBusArgument& operator>> (const QDBusArgument& arg, DBusStatus& status) {
|
|
|
|
arg.beginStructure();
|
|
|
|
arg >> status.play;
|
|
|
|
arg >> status.random;
|
|
|
|
arg >> status.repeat;
|
|
|
|
arg >> status.repeat_playlist;
|
|
|
|
arg.endStructure();
|
|
|
|
return arg;
|
|
|
|
}
|