2019-07-25 22:40:58 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2019 Matthijs Tijink <matthijstijink@gmail.com>
|
2019-07-25 22:40:58 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2019-07-25 22:40:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mprisremoteplayermediaplayer2player.h"
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "mprisremoteplayer.h"
|
2019-07-25 22:40:58 +01:00
|
|
|
#include "mprisremoteplugin.h"
|
|
|
|
|
|
|
|
#include <QDBusMessage>
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
MprisRemotePlayerMediaPlayer2Player::MprisRemotePlayerMediaPlayer2Player(MprisRemotePlayer *parent, MprisRemotePlugin *plugin)
|
|
|
|
: QDBusAbstractAdaptor{parent}
|
|
|
|
, m_parent{parent}
|
|
|
|
, m_plugin{plugin}
|
|
|
|
, m_controlsChanged{false}
|
|
|
|
, m_trackInfoChanged{false}
|
|
|
|
, m_positionChanged{false}
|
|
|
|
, m_volumeChanged{false}
|
|
|
|
, m_playingChanged{false}
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
connect(m_parent, &MprisRemotePlayer::controlsChanged, this, &MprisRemotePlayerMediaPlayer2Player::controlsChanged);
|
|
|
|
connect(m_parent, &MprisRemotePlayer::trackInfoChanged, this, &MprisRemotePlayerMediaPlayer2Player::trackInfoChanged);
|
|
|
|
connect(m_parent, &MprisRemotePlayer::positionChanged, this, &MprisRemotePlayerMediaPlayer2Player::positionChanged);
|
|
|
|
connect(m_parent, &MprisRemotePlayer::volumeChanged, this, &MprisRemotePlayerMediaPlayer2Player::volumeChanged);
|
|
|
|
connect(m_parent, &MprisRemotePlayer::playingChanged, this, &MprisRemotePlayerMediaPlayer2Player::playingChanged);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QString MprisRemotePlayerMediaPlayer2Player::PlaybackStatus() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
if (m_parent->playing()) {
|
|
|
|
return QStringLiteral("Playing");
|
|
|
|
} else {
|
|
|
|
return QStringLiteral("Paused");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
double MprisRemotePlayerMediaPlayer2Player::Rate() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QVariantMap MprisRemotePlayerMediaPlayer2Player::Metadata() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
QVariantMap metadata;
|
|
|
|
metadata[QStringLiteral("mpris:trackid")] = QVariant::fromValue<QDBusObjectPath>(QDBusObjectPath("/org/mpris/MediaPlayer2"));
|
|
|
|
|
|
|
|
if (m_parent->length() > 0) {
|
|
|
|
metadata[QStringLiteral("mpris:length")] = QVariant::fromValue<qlonglong>(m_parent->length() * qlonglong(1000));
|
|
|
|
}
|
|
|
|
if (!m_parent->title().isEmpty()) {
|
|
|
|
metadata[QStringLiteral("xesam:title")] = m_parent->title();
|
|
|
|
}
|
|
|
|
if (!m_parent->artist().isEmpty()) {
|
2023-03-25 14:13:31 +00:00
|
|
|
metadata[QStringLiteral("xesam:artist")] = QStringList{m_parent->artist()};
|
2019-07-25 22:40:58 +01:00
|
|
|
}
|
|
|
|
if (!m_parent->album().isEmpty()) {
|
|
|
|
metadata[QStringLiteral("xesam:album")] = m_parent->album();
|
|
|
|
}
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
double MprisRemotePlayerMediaPlayer2Player::Volume() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return m_parent->volume() / 100.0;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::setVolume(double volume) const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->setVolume(volume * 100.0 + 0.5);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
qlonglong MprisRemotePlayerMediaPlayer2Player::Position() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return m_plugin->position() * qlonglong(1000);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
double MprisRemotePlayerMediaPlayer2Player::MinimumRate() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
double MprisRemotePlayerMediaPlayer2Player::MaximumRate() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MprisRemotePlayerMediaPlayer2Player::CanGoNext() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return m_parent->canGoNext();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MprisRemotePlayerMediaPlayer2Player::CanGoPrevious() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return m_parent->canGoPrevious();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MprisRemotePlayerMediaPlayer2Player::CanPlay() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return m_parent->canPlay();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MprisRemotePlayerMediaPlayer2Player::CanPause() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return m_parent->canPause();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MprisRemotePlayerMediaPlayer2Player::CanSeek() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return m_parent->canSeek();
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool MprisRemotePlayerMediaPlayer2Player::CanControl() const
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::Next()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->sendAction(QStringLiteral("Next"));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::Previous()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->sendAction(QStringLiteral("Previous"));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::Pause()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->sendAction(QStringLiteral("Pause"));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::PlayPause()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->sendAction(QStringLiteral("PlayPause"));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::Stop()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->sendAction(QStringLiteral("Stop"));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::Play()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->sendAction(QStringLiteral("Play"));
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::Seek(qlonglong Offset)
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->seek(Offset);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::SetPosition(QDBusObjectPath TrackId, qlonglong Position)
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
Q_UNUSED(TrackId)
|
|
|
|
|
|
|
|
m_plugin->setPlayer(m_parent->identity());
|
|
|
|
m_plugin->setPosition(Position / 1000);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::OpenUri(QString Uri)
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
Q_UNUSED(Uri)
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::controlsChanged()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_controlsChanged = true;
|
|
|
|
QMetaObject::invokeMethod(this, &MprisRemotePlayerMediaPlayer2Player::emitPropertiesChanged, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::playingChanged()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_playingChanged = true;
|
|
|
|
QMetaObject::invokeMethod(this, &MprisRemotePlayerMediaPlayer2Player::emitPropertiesChanged, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::positionChanged()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_positionChanged = true;
|
|
|
|
QMetaObject::invokeMethod(this, &MprisRemotePlayerMediaPlayer2Player::emitPropertiesChanged, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::trackInfoChanged()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_trackInfoChanged = true;
|
|
|
|
QMetaObject::invokeMethod(this, &MprisRemotePlayerMediaPlayer2Player::emitPropertiesChanged, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::volumeChanged()
|
|
|
|
{
|
2019-07-25 22:40:58 +01:00
|
|
|
m_volumeChanged = true;
|
|
|
|
QMetaObject::invokeMethod(this, &MprisRemotePlayerMediaPlayer2Player::emitPropertiesChanged, Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlayerMediaPlayer2Player::emitPropertiesChanged()
|
|
|
|
{
|
|
|
|
// Always invoked "queued", so we can send all changes at once
|
|
|
|
// Check if things really changed (we might get called multiple times)
|
|
|
|
if (!m_controlsChanged && !m_trackInfoChanged && !m_positionChanged && !m_volumeChanged && !m_playingChanged)
|
|
|
|
return;
|
2019-07-25 22:40:58 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Qt doesn't automatically send the "org.freedesktop.DBus.Properties PropertiesChanged" signal, so do it manually
|
|
|
|
// With the current setup, it's hard to discover what properties changed. So just send all properties (not too large, usually)
|
2019-07-25 22:40:58 +01:00
|
|
|
QVariantMap properties;
|
|
|
|
if (m_trackInfoChanged) {
|
|
|
|
properties[QStringLiteral("Metadata")] = Metadata();
|
|
|
|
}
|
|
|
|
if (m_trackInfoChanged || m_positionChanged) {
|
|
|
|
properties[QStringLiteral("Position")] = Position();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_controlsChanged) {
|
|
|
|
properties[QStringLiteral("CanGoNext")] = CanGoNext();
|
|
|
|
properties[QStringLiteral("CanGoPrevious")] = CanGoPrevious();
|
|
|
|
properties[QStringLiteral("CanPlay")] = CanPlay();
|
|
|
|
properties[QStringLiteral("CanPause")] = CanPause();
|
|
|
|
properties[QStringLiteral("CanSeek")] = CanSeek();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_playingChanged) {
|
|
|
|
properties[QStringLiteral("PlaybackStatus")] = PlaybackStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_volumeChanged) {
|
|
|
|
properties[QStringLiteral("Volume")] = Volume();
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QVariant> args;
|
|
|
|
args.push_back(QVariant(QStringLiteral("org.mpris.MediaPlayer2.Player")));
|
|
|
|
args.push_back(properties);
|
|
|
|
args.push_back(QStringList{});
|
2022-09-10 22:23:52 +01:00
|
|
|
QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/org/mpris/MediaPlayer2"),
|
|
|
|
QStringLiteral("org.freedesktop.DBus.Properties"),
|
|
|
|
QStringLiteral("PropertiesChanged"));
|
2019-07-25 22:40:58 +01:00
|
|
|
message.setArguments(args);
|
2022-09-10 22:23:52 +01:00
|
|
|
// Send it over the correct DBus connection
|
2019-07-25 22:40:58 +01:00
|
|
|
m_parent->dbus().send(message);
|
|
|
|
|
2023-06-07 19:20:21 +01:00
|
|
|
if (m_positionChanged || m_trackInfoChanged) {
|
2019-07-25 22:40:58 +01:00
|
|
|
Q_EMIT Seeked(Position());
|
|
|
|
}
|
|
|
|
|
|
|
|
m_controlsChanged = false;
|
|
|
|
m_trackInfoChanged = false;
|
|
|
|
m_playingChanged = false;
|
|
|
|
m_positionChanged = false;
|
|
|
|
m_volumeChanged = false;
|
|
|
|
}
|
2023-07-26 09:15:11 +01:00
|
|
|
|
|
|
|
#include "moc_mprisremoteplayermediaplayer2player.cpp"
|