1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2025-09-05 10:12:46 +00:00

Add MPRIS support

Besides MPRIS support, this also adds support for hasPrevious() and
hasNext() in several parts to determine whether the
player/playlist/shuffler has a previous or next item.
This commit is contained in:
Chris Josten 2021-08-31 01:29:51 +02:00
parent 757327ceac
commit 54235f298e
23 changed files with 2574 additions and 2 deletions

View file

@ -23,6 +23,7 @@ void registerTypes(const char *uri) {
qmlRegisterType<ApiClient>(uri, 1, 0, "ApiClient");
qmlRegisterType<ServerDiscoveryModel>(uri, 1, 0, "ServerDiscoveryModel");
qmlRegisterType<ViewModel::PlaybackManager>(uri, 1, 0, "PlaybackManager");
qmlRegisterType<ViewModel::PlatformMediaControl>(uri, 1, 0, "PlatformMediaControl");
qmlRegisterUncreatableType<ViewModel::Item>(uri, 1, 0, "Item", "Acquire one via ItemLoader or exposed properties");
qmlRegisterUncreatableType<ViewModel::User>(uri, 1, 0, "User", "Acquire one via UserLoader or exposed properties");
qmlRegisterUncreatableType<EventBus>(uri, 1, 0, "EventBus", "Obtain one via your ApiClient");

View file

@ -35,6 +35,10 @@ void Playlist::clearList() {
emit listCleared();
}
bool Playlist::hasPrevious() {
return m_shuffler->hasPrevious();
}
void Playlist::previous() {
m_shuffler->previous();
int curItem = m_shuffler->currentItem();
@ -54,6 +58,10 @@ void Playlist::previous() {
emit currentItemChanged();
}
bool Playlist::hasNext() {
return m_shuffler->hasNext();
}
void Playlist::next() {
// Determine the new current item
if (!m_queue.isEmpty()) {

View file

@ -44,6 +44,14 @@ void NoShuffle::previous() {
m_index = previousIndex();
}
bool NoShuffle::hasPrevious() const {
return m_index > 0;
}
bool NoShuffle::hasNext() const {
return m_index < m_playlist->listSize() - 1;
}
int NoShuffle::currentItem() const {
return m_index;
}
@ -150,5 +158,13 @@ void RandomShuffle::next() {
m_next = random(m_playlist->listSize());
}
bool RandomShuffle::hasPrevious() const {
return true;
}
bool RandomShuffle::hasNext() const {
return true;
}
} // NS Model
} // NS Jellyfin

View file

@ -0,0 +1,117 @@
/*
* This file was generated by qdbusxml2cpp version 0.8
* Command line was: qdbusxml2cpp org.mpris.MediaPlayer2.xml -a ../include/JellyfinQt/platform/freedesktop/mediaplayer2.h:../src/platform/freedesktop/mediaplayer2.cpp
*
* qdbusxml2cpp is Copyright (C) 2020 The Qt Company Ltd.
*
* This is an auto-generated file.
* Do not edit! All changes made to it will be lost.
*/
#include "JellyfinQt/platform/freedesktop/mediaplayer2.h"
#include "JellyfinQt/viewmodel/platformmediacontrol.h"
#include <QtCore/QMetaObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
namespace Jellyfin {
namespace Platform {
namespace FreeDesktop {
/*
* Implementation of adaptor class MediaPlayer2Adaptor
*/
MediaPlayer2Adaptor::MediaPlayer2Adaptor(ViewModel::PlatformMediaControl *parent)
: QDBusAbstractAdaptor(parent),
m_mediaControl(parent)
{
// constructor
setAutoRelaySignals(true);
}
MediaPlayer2Adaptor::~MediaPlayer2Adaptor()
{
// destructor
}
bool MediaPlayer2Adaptor::canQuit() const
{
// get the value of property CanQuit
return m_mediaControl->canQuit();
}
bool MediaPlayer2Adaptor::canRaise() const
{
// get the value of property CanRaise
return m_mediaControl->canRaise();
}
bool MediaPlayer2Adaptor::canSetFullscreen() const
{
// get the value of property CanSetFullscreen
return qvariant_cast< bool >(parent()->property("CanSetFullscreen"));
}
QString MediaPlayer2Adaptor::desktopEntry() const
{
// get the value of property DesktopEntry
return m_mediaControl->desktopFile();
}
bool MediaPlayer2Adaptor::fullscreen() const
{
// get the value of property Fullscreen
return qvariant_cast< bool >(parent()->property("Fullscreen"));
}
void MediaPlayer2Adaptor::setFullscreen(bool value)
{
// set the value of property Fullscreen
parent()->setProperty("Fullscreen", QVariant::fromValue(value));
}
bool MediaPlayer2Adaptor::hasTrackList() const
{
// get the value of property HasTrackList
//return qvariant_cast< bool >(parent()->property("HasTrackList"));
return false;
}
QString MediaPlayer2Adaptor::identity() const
{
// get the value of property Identity
return m_mediaControl->playerName();
}
QStringList MediaPlayer2Adaptor::supportedMimeTypes() const
{
// get the value of property SupportedMimeTypes
return qvariant_cast< QStringList >(parent()->property("SupportedMimeTypes"));
}
QStringList MediaPlayer2Adaptor::supportedUriSchemes() const
{
// get the value of property SupportedUriSchemes
QStringList supportedUriSchemes;
supportedUriSchemes << "urn";
return supportedUriSchemes;
}
void MediaPlayer2Adaptor::Quit()
{
m_mediaControl->requestQuit();
}
void MediaPlayer2Adaptor::Raise()
{
m_mediaControl->requestRaise();
}
} // NS FreeDesktop
} // NS Platform
} // NS Jellyfin

View file

@ -0,0 +1,325 @@
/*
* This file was generated by qdbusxml2cpp version 0.8
* Command line was: qdbusxml2cpp org.mpris.MediaPlayer2.Player.xml -a ../include/JellyfinQt/platform/freedesktop/mediaplayer2player.h:../src/platform/freedesktop/mediaplayer2player.cpp
*
* qdbusxml2cpp is Copyright (C) 2020 The Qt Company Ltd.
*
* This is an auto-generated file.
* Do not edit! All changes made to it will be lost.
*/
#include "JellyfinQt/platform/freedesktop/mediaplayer2player.h"
#include "JellyfinQt/viewmodel/item.h"
#include "JellyfinQt/viewmodel/platformmediacontrol.h"
#include "JellyfinQt/viewmodel/playbackmanager.h"
#include <QtCore/QMetaObject>
#include <QtCore/QByteArray>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
namespace Jellyfin {
namespace Platform {
namespace FreeDesktop {
/*
* Implementation of adaptor class PlayerAdaptor
*/
PlayerAdaptor::PlayerAdaptor(ViewModel::PlatformMediaControl *parent)
: QDBusAbstractAdaptor(parent),
m_mediaControl(parent) {
// constructor
//setAutoRelaySignals(true);
onPlaybackManagerChanged(m_mediaControl->playbackManager());
connect(m_mediaControl, &ViewModel::PlatformMediaControl::playbackManagerChanged, this, &PlayerAdaptor::onPlaybackManagerChanged);
/*if (m_mediaControl != nullptr && m_mediaControl->playbackManager() != nullptr) {
}*/
}
PlayerAdaptor::~PlayerAdaptor() {
// destructor
}
bool PlayerAdaptor::canControl() const
{
// get the value of property CanControl
return true;
}
bool PlayerAdaptor::canGoNext() const
{
// get the value of property CanGoNext
return canPlay() && m_mediaControl->playbackManager()->hasNext();
}
bool PlayerAdaptor::canGoPrevious() const
{
// get the value of property CanGoPrevious
return canPlay() && m_mediaControl->playbackManager()->hasPrevious();
}
bool PlayerAdaptor::canPause() const
{
// get the value of property CanPause
return canPlay();
}
bool PlayerAdaptor::canPlay() const
{
// get the value of property CanPlay
return m_mediaControl->playbackManager()->queue()->rowCount(QModelIndex()) > 0;
}
bool PlayerAdaptor::canSeek() const
{
// get the value of property CanSeek
return m_mediaControl->playbackManager()->seekable();
}
QString PlayerAdaptor::loopStatus() const
{
// get the value of property LoopStatus
return "None";
}
void PlayerAdaptor::setLoopStatus(const QString &value)
{
// set the value of property LoopStatus
parent()->setProperty("LoopStatus", QVariant::fromValue(value));
}
double PlayerAdaptor::maximumRate() const
{
// get the value of property MaximumRate
return 1.0;
}
QVariantMap PlayerAdaptor::metadata() const
{
// get the value of property Metadata
QVariantMap map;
if (m_mediaControl->playbackManager() == nullptr || m_mediaControl->playbackManager()->dataItem().isNull()) {
return map;
}
ViewModel::PlaybackManager *plybkMgr = m_mediaControl->playbackManager();
QSharedPointer<Model::Item> item = plybkMgr->dataItem();
if (!item.isNull()) {
map[QStringLiteral("mpris:trackid")] = QVariant::fromValue<QDBusObjectPath>(QDBusObjectPath(QStringLiteral("/nl/netsoj/chris/jellyfinqt/item/").append(item->jellyfinId())));
if (item->runTimeTicks().has_value()) {
map[QStringLiteral("mpris:length")] = item->runTimeTicks().value() / 10;
}
map[QStringLiteral("xesam:title")] = item->name();
if (!item->albumPrimaryImageTagNull()) {
map[QStringLiteral("mpris:artUrl")] = QStringLiteral("%1/Items/%2/Images/Primary?tag=%3").arg(plybkMgr->apiClient()->baseUrl(),
item->jellyfinId(), item->albumPrimaryImageTag());
}
QStringList albumArtists;
QList<NameGuidPair> tmp = item->albumArtists();
for (auto it = tmp.cbegin(); it != tmp.cend(); it++) {
albumArtists << it->name();
}
map[QStringLiteral("xesam:albumArtist")] = albumArtists;
map[QStringLiteral("xesam:album")] = item->album();
map[QStringLiteral("xesam:artist")] = item->artists();
if (item->parentIndexNumber().has_value()) {
map[QStringLiteral("xesam:discNumber")] = item->parentIndexNumber().value();
}
if (item->indexNumber().has_value()) {
map[QStringLiteral("xesam:trackNumber")] = item->indexNumber().value();
}
map[QStringLiteral("xesam:contentCreated")] = item->dateCreated();
map[QStringLiteral("xesam:genre")] = item->genres();
map[QStringLiteral("xesam:lastUsed")] = item->userData()->lastPlayedDate();
}
return map;
}
double PlayerAdaptor::minimumRate() const
{
// get the value of property MinimumRate
return 1.0;
}
QString PlayerAdaptor::playbackStatus() const
{
// get the value of property PlaybackStatus
if (m_mediaControl == nullptr || m_mediaControl->playbackManager() == nullptr) {
return "Stopped";
}
switch(m_mediaControl->playbackManager()->playbackState()) {
case QMediaPlayer::StoppedState:
return "Stopped";
case QMediaPlayer::PlayingState:
return "Playing";
case QMediaPlayer::PausedState:
return "Paused";
default:
return "Stopped";
}
}
qlonglong PlayerAdaptor::position() const
{
// get the value of property Position
return m_mediaControl->playbackManager()->position() * 1000;
}
double PlayerAdaptor::rate() const
{
// get the value of property Rate
return 1.0;
}
void PlayerAdaptor::setRate(double value)
{
// set the value of property Rate
parent()->setProperty("Rate", QVariant::fromValue(value));
}
bool PlayerAdaptor::shuffle() const
{
// get the value of property Shuffle
return false;
}
void PlayerAdaptor::setShuffle(bool value)
{
// set the value of property Shuffle
parent()->setProperty("Shuffle", QVariant::fromValue(value));
}
double PlayerAdaptor::volume() const
{
// get the value of property Volume
return qvariant_cast< double >(parent()->property("Volume"));
}
void PlayerAdaptor::setVolume(double value)
{
// set the value of property Volume
parent()->setProperty("Volume", QVariant::fromValue(value));
}
void PlayerAdaptor::Next()
{
// handle method call org.mpris.MediaPlayer2.Player.Next
m_mediaControl->playbackManager()->next();
}
void PlayerAdaptor::OpenUri(const QString &Uri)
{
// handle method call org.mpris.MediaPlayer2.Player.OpenUri
QMetaObject::invokeMethod(parent(), "OpenUri", Q_ARG(QString, Uri));
}
void PlayerAdaptor::Pause()
{
// handle method call org.mpris.MediaPlayer2.Player.Pause
m_mediaControl->playbackManager()->pause();
}
void PlayerAdaptor::Play()
{
// handle method call org.mpris.MediaPlayer2.Player.Play
m_mediaControl->playbackManager()->play();
}
void PlayerAdaptor::PlayPause()
{
// handle method call org.mpris.MediaPlayer2.Player.PlayPause
if (m_mediaControl->playbackManager()->playbackState() == QMediaPlayer::PlayingState) {
m_mediaControl->playbackManager()->pause();
} else {
m_mediaControl->playbackManager()->play();
}
}
void PlayerAdaptor::Previous()
{
// handle method call org.mpris.MediaPlayer2.Player.Previous
m_mediaControl->playbackManager()->previous();
}
void PlayerAdaptor::Seek(qlonglong Offset)
{
// handle method call org.mpris.MediaPlayer2.Player.Seek
m_mediaControl->playbackManager()->seek(m_mediaControl->playbackManager()->position() + Offset / 1000);
}
void PlayerAdaptor::SetPosition(const QDBusObjectPath &TrackId, qlonglong Position)
{
// handle method call org.mpris.MediaPlayer2.Player.SetPosition
if (TrackId.path() == QStringLiteral("/nl/netsoj/chris/jellyfinqt/item/").append(m_mediaControl->playbackManager()->dataItem()->jellyfinId())) {
m_mediaControl->playbackManager()->seek(Position / 1000);
}
}
void PlayerAdaptor::Stop()
{
// handle method call org.mpris.MediaPlayer2.Player.Stop
QMetaObject::invokeMethod(parent(), "Stop");
}
void PlayerAdaptor::notifyPropertiesChanged(QStringList properties) {
QDBusMessage signal = QDBusMessage::createSignal("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "PropertiesChanged");
signal << "org.mpris.MediaPlayer2.Player"; // 1st argument: interface name
QVariantMap changedProperties;
for (auto it = properties.cbegin(); it != properties.cend(); it++) {
changedProperties[*it] = property(it->toLocal8Bit().data());
}
signal << changedProperties; // 2nd argument: changed properties
signal << QStringList(); // 3th argument: invalidated properties
QDBusConnection::sessionBus().send(signal);
}
void PlayerAdaptor::onCurrentItemChanged(ViewModel::Item *item) {
Q_UNUSED(item)
QStringList properties;
properties << "Metadata" << "Position" << "CanPlay" << "CanPause" << "CanGoNext" << "CanGoPrevious";
notifyPropertiesChanged(properties);
}
void PlayerAdaptor::onPlaybackStateChanged(QMediaPlayer::State state) {
Q_UNUSED(state)
QStringList properties;
properties << "PlaybackStatus" << "Position";
notifyPropertiesChanged(properties);
}
void PlayerAdaptor::onMediaStatusChanged(QMediaPlayer::MediaStatus status) {
Q_UNUSED(status)
QStringList properties;
properties << "PlaybackStatus" << "Position";
notifyPropertiesChanged(properties);
}
void PlayerAdaptor::onPositionChanged(qint64 position) {
Q_UNUSED(position)
/*QStringList properties;
properties << "Position";
notifyPropertiesChanged(properties);*/
}
void PlayerAdaptor::onSeekableChanged(bool seekable) {
QStringList properties;
properties << "CanSeek";
notifyPropertiesChanged(properties);
}
void PlayerAdaptor::onPlaybackManagerChanged(ViewModel::PlaybackManager *newPlaybackManager) {
if (newPlaybackManager != nullptr) {
connect(newPlaybackManager, &ViewModel::PlaybackManager::itemChanged, this, &PlayerAdaptor::onCurrentItemChanged);
connect(newPlaybackManager, &ViewModel::PlaybackManager::playbackStateChanged, this, &PlayerAdaptor::onPlaybackStateChanged);
connect(newPlaybackManager, &ViewModel::PlaybackManager::mediaStatusChanged, this, &PlayerAdaptor::onMediaStatusChanged);
connect(newPlaybackManager, &ViewModel::PlaybackManager::positionChanged, this, &PlayerAdaptor::onPositionChanged);
connect(newPlaybackManager, &ViewModel::PlaybackManager::seekableChanged, this, &PlayerAdaptor::onSeekableChanged);
}
}
} // NS FreeDesktop
} // NS Platform
} // NS Jellyfin

View file

@ -0,0 +1,87 @@
/*
* Sailfin: a Jellyfin client written using Qt
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "JellyfinQt/viewmodel/platformmediacontrol.h"
#include "JellyfinQt/platform/freedesktop/mediaplayer2.h"
#include "JellyfinQt/platform/freedesktop/mediaplayer2player.h"
#include <QDebug>
#include <QString>
#include <QtDBus/QtDBus>
namespace Jellyfin {
namespace ViewModel {
using Platform::FreeDesktop::MediaPlayer2Adaptor;
using Platform::FreeDesktop::PlayerAdaptor;
class PlatformMediaControlPrivate {
public:
PlatformMediaControlPrivate(PlatformMediaControl *parent);
void setupConnection();
private:
PlatformMediaControl *q_ptr;
Q_DECLARE_PUBLIC(PlatformMediaControl)
MediaPlayer2Adaptor *m_mainAdaptor;
PlayerAdaptor *m_playerAdaptor;
QDBusConnection m_connection;
public slots:
// MPRIS Player methods
void Quit();
};
PlatformMediaControl::PlatformMediaControl(QObject *parent)
: QObject(parent) {
d_ptr = new PlatformMediaControlPrivate(this);
}
void PlatformMediaControl::setup() {
Q_D(PlatformMediaControl);
d->setupConnection();
}
PlatformMediaControlPrivate::PlatformMediaControlPrivate(PlatformMediaControl *parent)
: q_ptr(parent),
m_mainAdaptor(new MediaPlayer2Adaptor(parent)),
m_playerAdaptor(new PlayerAdaptor(parent)),
m_connection(QDBusConnection::sessionBus()) {
}
void PlatformMediaControlPrivate::setupConnection() {
Q_Q(PlatformMediaControl);
if(!m_connection.registerObject(QStringLiteral("/org/mpris/MediaPlayer2"), q)) {
qWarning() << "MediaPlayer2 dbus object not registered: " << m_connection.lastError();
}
if (!m_connection.registerService(QStringLiteral("org.mpris.MediaPlayer2.sailfin.instance").append(QString::number(QCoreApplication::applicationPid())))) {
qWarning() << "Could not aqcuire DBus name: " << m_connection.lastError();
}
}
void PlatformMediaControlPrivate::Quit() {
Q_Q(PlatformMediaControl);
q->requestQuit();
}
}
}

View file

@ -0,0 +1,29 @@
/*
* Sailfin: a Jellyfin client written using Qt
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "JellyfinQt/viewmodel/platformmediacontrol.h"
namespace Jellyfin {
namespace ViewModel {
PlatformMediaControl::PlatformMediaControl(QObject *parent)
: QObject(parent) {}
}
}

View file

@ -84,6 +84,9 @@ void PlaybackManager::setItem(QSharedPointer<Model::Item> newItem) {
}
emit itemChanged(m_displayItem);
emit hasNextChanged(m_queue->hasNext());
emit hasPreviousChanged(m_queue->hasPrevious());
if (m_apiClient == nullptr) {
qWarning() << "apiClient is not set on this MediaSource instance! Aborting.";
return;