diff --git a/interfaces/dbusinterfaces.h b/interfaces/dbusinterfaces.h --- a/interfaces/dbusinterfaces.h +++ b/interfaces/dbusinterfaces.h @@ -134,6 +134,10 @@ Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY propertiesChangedProxy) Q_PROPERTY(int length READ length NOTIFY propertiesChangedProxy) Q_PROPERTY(QString nowPlaying READ nowPlaying NOTIFY propertiesChangedProxy) + Q_PROPERTY(QString title READ title NOTIFY propertiesChangedProxy) + Q_PROPERTY(QString artist READ artist NOTIFY propertiesChangedProxy) + Q_PROPERTY(QString album READ album NOTIFY propertiesChangedProxy) + Q_PROPERTY(QStringList playerList READ playerList NOTIFY propertiesChangedProxy) Q_PROPERTY(int volume READ volume WRITE setVolume NOTIFY propertiesChangedProxy) Q_PROPERTY(int position READ position WRITE setPosition NOTIFY propertiesChangedProxy) diff --git a/plugins/mprisremote/CMakeLists.txt b/plugins/mprisremote/CMakeLists.txt --- a/plugins/mprisremote/CMakeLists.txt +++ b/plugins/mprisremote/CMakeLists.txt @@ -1,4 +1,4 @@ -kdeconnect_add_plugin(kdeconnect_mprisremote JSON kdeconnect_mprisremote.json SOURCES mprisremoteplugin.cpp) +kdeconnect_add_plugin(kdeconnect_mprisremote JSON kdeconnect_mprisremote.json SOURCES mprisremoteplugin.cpp mprisremoteplayer.cpp) target_link_libraries(kdeconnect_mprisremote kdeconnectcore diff --git a/plugins/mprisremote/mprisremoteplayer.h b/plugins/mprisremote/mprisremoteplayer.h new file mode 100644 --- /dev/null +++ b/plugins/mprisremote/mprisremoteplayer.h @@ -0,0 +1,53 @@ +/** + * Copyright 2018 Nicolas Fella + * + * 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) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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. If not, see . + */ +#pragma once + +#include + +class MprisRemotePlayer { + +public: + explicit MprisRemotePlayer(); + virtual ~MprisRemotePlayer(); + + void parseNetworkPacket(const NetworkPacket& np); + long position() const; + void setPosition(long position); + int volume() const; + long length() const; + bool playing() const; + QString nowPlaying() const; + QString title() const; + QString artist() const; + QString album() const; + +private: + + QString id; + bool m_playing; + QString m_nowPlaying; + int m_volume; + long m_length; + long m_lastPosition; + qint64 m_lastPositionTime; + QString m_title; + QString m_artist; + QString m_album; +}; diff --git a/plugins/mprisremote/mprisremoteplayer.cpp b/plugins/mprisremote/mprisremoteplayer.cpp new file mode 100644 --- /dev/null +++ b/plugins/mprisremote/mprisremoteplayer.cpp @@ -0,0 +1,106 @@ +/** + * Copyright 2018 Nicolas Fella + * + * 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) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * 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. If not, see . + */ + +#include +#include + +#include "mprisremoteplayer.h" + +MprisRemotePlayer::MprisRemotePlayer() : + m_playing(false) + , m_nowPlaying() + , m_volume(50) + , m_length(0) + , m_lastPosition(0) + , m_lastPositionTime() + , m_title() + , m_artist() + , m_album() +{ +} + +MprisRemotePlayer::~MprisRemotePlayer() +{ +} + +void MprisRemotePlayer::parseNetworkPacket(const NetworkPacket& np) +{ + m_nowPlaying = np.get(QStringLiteral("nowPlaying"), m_nowPlaying); + m_title = np.get(QStringLiteral("title"), m_title); + m_artist = np.get(QStringLiteral("artist"), m_artist); + m_album = np.get(QStringLiteral("album"), m_album); + m_volume = np.get(QStringLiteral("volume"), m_volume); + m_length = np.get(QStringLiteral("length"), m_length); + if(np.has(QStringLiteral("pos"))){ + m_lastPosition = np.get(QStringLiteral("pos"), m_lastPosition); + m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); + } + m_playing = np.get(QStringLiteral("isPlaying"), m_playing); +} + +long MprisRemotePlayer::position() const +{ + if(m_playing) { + return m_lastPosition + (QDateTime::currentMSecsSinceEpoch() - m_lastPositionTime); + } else { + return m_lastPosition; + } +} + +void MprisRemotePlayer::setPosition(long position) +{ + m_lastPosition = position; + m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); +} + +int MprisRemotePlayer::volume() const +{ + return m_volume; +} + +long int MprisRemotePlayer::length() const +{ + return m_length; +} + +bool MprisRemotePlayer::playing() const +{ + return m_playing; +} + +QString MprisRemotePlayer::nowPlaying() const +{ + return m_nowPlaying; +} + +QString MprisRemotePlayer::title() const +{ + return m_title; +} + +QString MprisRemotePlayer::artist() const +{ + return m_artist; +} + +QString MprisRemotePlayer::album() const +{ + return m_album; +} diff --git a/plugins/mprisremote/mprisremoteplugin.h b/plugins/mprisremote/mprisremoteplugin.h --- a/plugins/mprisremote/mprisremoteplugin.h +++ b/plugins/mprisremote/mprisremoteplugin.h @@ -25,6 +25,8 @@ #include +#include "mprisremoteplayer.h" + #define PACKET_TYPE_MPRIS_REQUEST QStringLiteral("kdeconnect.mpris.request") #define PACKET_TYPE_MPRIS QStringLiteral("kdeconnect.mpris") @@ -40,18 +42,24 @@ Q_PROPERTY(QStringList playerList READ playerList NOTIFY propertiesChanged) Q_PROPERTY(QString player READ player WRITE setPlayer) Q_PROPERTY(QString nowPlaying READ nowPlaying NOTIFY propertiesChanged) + Q_PROPERTY(QString title READ title NOTIFY propertiesChanged) + Q_PROPERTY(QString artist READ artist NOTIFY propertiesChanged) + Q_PROPERTY(QString album READ album NOTIFY propertiesChanged) public: explicit MprisRemotePlugin(QObject* parent, const QVariantList &args); ~MprisRemotePlugin() override; long position() const; - int volume() const { return m_volume; } - int length() const { return m_length; } - bool isPlaying() const { return m_playing; } - QStringList playerList() const { return m_playerList; } - QString player() const { return m_player; } - QString nowPlaying() const { return m_nowPlaying; } + int volume() const; + int length() const; + bool isPlaying() const; + QStringList playerList() const; + QString player() const; + QString nowPlaying() const; + QString title() const; + QString artist() const; + QString album() const; void setVolume(int volume); void setPosition(int position); @@ -71,14 +79,8 @@ private: void requestPlayerStatus(); - QString m_player; - bool m_playing; - QString m_nowPlaying; - int m_volume; - long m_length; - long m_lastPosition; - qint64 m_lastPositionTime; - QStringList m_playerList; + QString m_currentPlayer; + QMap m_players; }; #endif diff --git a/plugins/mprisremote/mprisremoteplugin.cpp b/plugins/mprisremote/mprisremoteplugin.cpp --- a/plugins/mprisremote/mprisremoteplugin.cpp +++ b/plugins/mprisremote/mprisremoteplugin.cpp @@ -35,14 +35,8 @@ MprisRemotePlugin::MprisRemotePlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) - , m_player() - , m_playing(false) - , m_nowPlaying() - , m_volume(50) - , m_length(0) - , m_lastPosition(0) - , m_lastPositionTime() - , m_playerList() + , m_currentPlayer() + , m_players() { } @@ -55,34 +49,34 @@ if (np.type() != PACKET_TYPE_MPRIS) return false; - if (np.has(QStringLiteral("nowPlaying")) || np.has(QStringLiteral("volume")) || np.has(QStringLiteral("isPlaying")) || np.has(QStringLiteral("length")) || np.has(QStringLiteral("pos"))) { - if (np.get(QStringLiteral("player")) == m_player) { - m_nowPlaying = np.get(QStringLiteral("nowPlaying"), m_nowPlaying); - m_volume = np.get(QStringLiteral("volume"), m_volume); - m_length = np.get(QStringLiteral("length"), m_length); - if(np.has(QStringLiteral("pos"))){ - m_lastPosition = np.get(QStringLiteral("pos"), m_lastPosition); - m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); - } - m_playing = np.get(QStringLiteral("isPlaying"), m_playing); - } + if (np.has(QStringLiteral("player"))) { + m_players[m_currentPlayer]->parseNetworkPacket(np); } if (np.has(QStringLiteral("playerList"))) { - m_playerList = np.get(QStringLiteral("playerList"), QStringList()); + QStringList players = np.get(QStringLiteral("playerList")); + qDeleteAll(m_players); + m_players.clear(); + for (const QString& player : players) { + m_players[player] = new MprisRemotePlayer(); + } + + if (m_players.empty()) { + m_currentPlayer = QString(); + } else if (!m_players.contains(m_currentPlayer)) { + m_currentPlayer = m_players.keys().first(); + } + } Q_EMIT propertiesChanged(); return true; } long MprisRemotePlugin::position() const { - if(m_playing) { - return m_lastPosition + (QDateTime::currentMSecsSinceEpoch() - m_lastPositionTime); - } else { - return m_lastPosition; - } + auto player = m_players.value(m_currentPlayer); + return player ? player->position() : 0; } QString MprisRemotePlugin::dbusPath() const @@ -93,7 +87,7 @@ void MprisRemotePlugin::requestPlayerStatus() { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"requestNowPlaying", true}, {"requestVolume", true}} ); @@ -109,47 +103,101 @@ void MprisRemotePlugin::sendAction(const QString& action) { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"action", action} }); sendPacket(np); } void MprisRemotePlugin::seek(int offset) const { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"Seek", offset}}); sendPacket(np); } void MprisRemotePlugin::setVolume(int volume) { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"setVolume",volume} }); sendPacket(np); } void MprisRemotePlugin::setPosition(int position) { NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, { - {"player", m_player}, + {"player", m_currentPlayer}, {"SetPosition", position} }); sendPacket(np); - m_lastPosition = position; - m_lastPositionTime = QDateTime::currentMSecsSinceEpoch(); + m_players[m_currentPlayer]->setPosition(position); } void MprisRemotePlugin::setPlayer(const QString& player) { - if (m_player != player) { - m_player = player; + if (m_currentPlayer != player) { + m_currentPlayer = player; requestPlayerStatus(); + Q_EMIT propertiesChanged(); } } +bool MprisRemotePlugin::isPlaying() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->playing() : false; +} + +int MprisRemotePlugin::length() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->length() : 0; +} + +int MprisRemotePlugin::volume() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->volume() : 0; +} + +QString MprisRemotePlugin::player() const +{ + if (m_currentPlayer.isEmpty()) + return QString(); + return m_currentPlayer; +} + +QStringList MprisRemotePlugin::playerList() const +{ + return m_players.keys(); +} + +QString MprisRemotePlugin::nowPlaying() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->nowPlaying() : QString(); +} + +QString MprisRemotePlugin::title() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->title() : QString(); +} + +QString MprisRemotePlugin::album() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->album() : QString(); +} + +QString MprisRemotePlugin::artist() const +{ + auto player = m_players.value(m_currentPlayer); + return player ? player->artist() : QString(); +} + #include "mprisremoteplugin.moc"