1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "mpris.h"
#include "config.h"
#include "gui/currentcover.h"
#include "mpd-interface/mpdconnection.h"
#include "playeradaptor.h"
#include "rootadaptor.h"
#include <QDBusObjectPath>
static inline qlonglong convertTime(qlonglong t)
{
return t * 1000000;
}
Mpris::Mpris(QObject* p)
: QObject(p)
{
QDBusConnection::sessionBus().registerService("org.mpris.MediaPlayer2." PROJECT_REV_ID);
new PlayerAdaptor(this);
new MediaPlayer2Adaptor(this);
QDBusConnection::sessionBus().registerObject("/org/mpris/MediaPlayer2", this, QDBusConnection::ExportAdaptors);
connect(this, SIGNAL(setRandom(bool)), MPDConnection::self(), SLOT(setRandom(bool)));
connect(this, SIGNAL(setRepeat(bool)), MPDConnection::self(), SLOT(setRepeat(bool)));
connect(this, SIGNAL(setSingle(bool)), MPDConnection::self(), SLOT(setSingle(bool)));
connect(this, SIGNAL(setSeekId(qint32, quint32)), MPDConnection::self(), SLOT(setSeekId(qint32, quint32)));
connect(this, SIGNAL(seek(qint32)), MPDConnection::self(), SLOT(seek(qint32)));
connect(this, SIGNAL(setVolume(int)), MPDConnection::self(), SLOT(setVolume(int)));
// connect(MPDConnection::self(), SIGNAL(currentSongUpdated(const Song &)), this, SLOT(updateCurrentSong(const Song &)));
// connect(MPDStatus::self(), SIGNAL(updated()), this, SLOT(updateStatus()));
connect(CurrentCover::self(), SIGNAL(coverFile(const QString&)), this, SLOT(updateCurrentCover(const QString&)));
}
Mpris::~Mpris()
{
QDBusConnection::sessionBus().unregisterService("org.mpris.MediaPlayer2." PROJECT_REV_ID);
}
void Mpris::Pause()
{
if (!status.isNull() && MPDState_Playing == status->state()) {
StdActions::self()->playPauseTrackAction->trigger();
}
}
void Mpris::Play()
{
if (!status.isNull() && status->playlistLength() > 0 && MPDState_Playing != status->state()) {
StdActions::self()->playPauseTrackAction->trigger();
}
}
void Mpris::SetPosition(const QDBusObjectPath& trackId, qlonglong pos)
{
if (trackId.path() == currentTrackId()) {
emit setSeekId(-1, pos / 1000000);
}
}
QString Mpris::PlaybackStatus() const
{
if (status.isNull()) {
return QLatin1String("Stopped");
}
switch (status->state()) {
case MPDState_Playing: return QLatin1String("Playing");
case MPDState_Paused: return QLatin1String("Paused");
default:
case MPDState_Stopped: return QLatin1String("Stopped");
}
}
void Mpris::SetLoopStatus(const QString& s)
{
if (status.isNull()) {
return;
}
bool repeat = (QLatin1String("None") != s);
bool single = (QLatin1String("Track") == s);
if (status->repeat() != repeat) {
emit setRepeat(repeat);
}
if (status->single() != single) {
emit setSingle(single);
}
}
qlonglong Mpris::Position() const
{
// Cant use MPDStatus, as we dont poll for track position, but use a timer instead!
//return MPDStatus::self()->timeElapsed();
return status.isNull() ? 0 : convertTime(status->guessedElapsed());
}
void Mpris::updateStatus()
{
updateStatus(MPDStatus::self());
}
void Mpris::updateStatus(MPDStatus* const status)
{
QVariantMap map;
// If the current song has not yet been updated, reject this status
// update and wait for the next unless the play queue has recently
// been emptied or the connection to MPD has been lost ...
if (status->songId() != currentSong.id && status->songId() != -1) {
return;
}
if (status != this->status) {
this->status = status;
}
if (status->repeat() != lastStatus.repeat || status->single() != lastStatus.single) {
map.insert("LoopStatus", LoopStatus());
}
if (status->random() != lastStatus.random) {
map.insert("Shuffle", Shuffle());
}
if (status->volume() != lastStatus.volume) {
map.insert("Volume", Volume());
}
if (status->state() != lastStatus.state || status->songId() != lastStatus.songId || status->nextSongId() != lastStatus.nextSongId || status->playlistLength() != lastStatus.playlistLength) {
map.insert("CanGoNext", CanGoNext());
map.insert("CanGoPrevious", CanGoPrevious());
}
if (status->state() != lastStatus.state) {
map.insert("PlaybackStatus", PlaybackStatus());
map.insert("CanPause", CanPause());
}
if (status->playlistLength() != lastStatus.playlistLength) {
map.insert("CanPlay", CanPlay());
}
if (status->songId() != lastStatus.songId || status->timeTotal() != lastStatus.timeTotal) {
map.insert("CanSeek", CanSeek());
}
if (status->timeElapsed() != lastStatus.timeElapsed) {
map.insert("Position", convertTime(status->timeElapsed()));
}
if (!map.isEmpty() || status->songId() != lastStatus.songId) {
if (!map.contains("Position")) {
map.insert("Position", convertTime(status->timeElapsed()));
}
map.insert("Metadata", Metadata());
signalUpdate(map);
}
lastStatus = status->getValues();
}
void Mpris::updateCurrentCover(const QString& fileName)
{
if (fileName != currentCover) {
currentCover = fileName;
signalUpdate("Metadata", Metadata());
}
}
void Mpris::updateCurrentSong(const Song& song)
{
qint32 lastSongId = currentSong.id;
currentSong = song;
if (song.id != lastSongId && (song.id == lastStatus.songId || -1 == lastStatus.songId)) {
// The update of the current song may come a little late.
// So reset song ID and update status once again.
if (-1 != lastStatus.songId) {
lastStatus.songId = lastSongId;
}
updateStatus();
}
else {
signalUpdate("Metadata", Metadata());
}
}
QVariantMap Mpris::Metadata() const
{
QVariantMap metadataMap;
if ((!currentSong.title.isEmpty() && !currentSong.artist.isEmpty()) || (currentSong.isStandardStream() && !currentSong.name().isEmpty())) {
metadataMap.insert("mpris:trackid", QDBusObjectPath(currentTrackId()));
QString artist = currentSong.artist;
QString album = currentSong.album;
QString title = currentSong.title;
if (currentSong.isStandardStream()) {
if (artist.isEmpty()) {
artist = currentSong.name();
}
else if (album.isEmpty()) {
album = currentSong.name();
}
if (title.isEmpty()) {
title = tr("(Stream)");
}
}
if (currentSong.time > 0) {
metadataMap.insert("mpris:length", convertTime(currentSong.time));
}
if (!album.isEmpty()) {
metadataMap.insert("xesam:album", album);
}
if (!currentSong.albumartist.isEmpty() && currentSong.albumartist != currentSong.artist) {
metadataMap.insert("xesam:albumArtist", QStringList() << currentSong.albumartist);
}
if (!artist.isEmpty()) {
metadataMap.insert("xesam:artist", QStringList() << artist);
}
if (!title.isEmpty()) {
metadataMap.insert("xesam:title", title);
}
if (!currentSong.genres[0].isEmpty()) {
metadataMap.insert("xesam:genre", QStringList() << currentSong.genres[0]);
}
if (currentSong.track > 0) {
metadataMap.insert("xesam:trackNumber", currentSong.track);
}
if (currentSong.disc > 0) {
metadataMap.insert("xesam:discNumber", currentSong.disc);
}
if (currentSong.year > 0) {
metadataMap.insert("xesam:contentCreated", QString("%1").arg(currentSong.year, 4, 10, QChar(u'0')));
}
if (!currentSong.file.isEmpty()) {
if (currentSong.isNonMPD()) {
metadataMap.insert("xesam:url", currentSong.file);
}
else if (MPDConnection::self()->getDetails().dirReadable) {
QString mpdDir = MPDConnection::self()->getDetails().dir;
if (!mpdDir.isEmpty()) {
metadataMap.insert("xesam:url", "file://" + mpdDir + currentSong.file);
}
}
}
if (!currentCover.isEmpty()) {
metadataMap.insert("mpris:artUrl", "file://" + currentCover);
}
}
return metadataMap;
}
void Mpris::Raise()
{
emit showMainWindow();
}
void Mpris::signalUpdate(const QString& property, const QVariant& value)
{
QVariantMap map;
map.insert(property, value);
signalUpdate(map);
}
void Mpris::signalUpdate(const QVariantMap& map)
{
if (map.isEmpty()) {
return;
}
QDBusMessage signal = QDBusMessage::createSignal("/org/mpris/MediaPlayer2",
"org.freedesktop.DBus.Properties",
"PropertiesChanged");
QVariantList args = QVariantList()
<< "org.mpris.MediaPlayer2.Player"
<< map
<< QStringList();
signal.setArguments(args);
QDBusConnection::sessionBus().send(signal);
}
QString Mpris::currentTrackId() const
{
return QString("/id/Track/%1").arg(QString::number(currentSong.id));
}
#include "moc_mpris.cpp"
|