2015-06-18 03:01:01 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2013 Albert Vaca <albertvaka@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) 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mprisremoteplugin.h"
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
|
|
#include <core/device.h>
|
|
|
|
|
|
|
|
K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_mprisremote.json", registerPlugin< MprisRemotePlugin >(); )
|
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_MPRISREMOTE, "kdeconnect.plugin.mprisremote")
|
|
|
|
|
|
|
|
MprisRemotePlugin::MprisRemotePlugin(QObject* parent, const QVariantList& args)
|
|
|
|
: KdeConnectPlugin(parent, args)
|
2018-04-27 23:22:45 +01:00
|
|
|
, m_currentPlayer()
|
|
|
|
, m_players()
|
2015-06-18 03:01:01 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MprisRemotePlugin::~MprisRemotePlugin()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:48:51 +00:00
|
|
|
bool MprisRemotePlugin::receivePacket(const NetworkPacket& np)
|
2015-06-18 03:01:01 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
if (np.type() != PACKET_TYPE_MPRIS)
|
2015-06-18 03:01:01 +01:00
|
|
|
return false;
|
|
|
|
|
2018-04-27 23:22:45 +01:00
|
|
|
if (np.has(QStringLiteral("player"))) {
|
2018-04-27 23:23:45 +01:00
|
|
|
m_players[np.get<QString>(QStringLiteral("player"))]->parseNetworkPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
2016-11-26 14:38:08 +00:00
|
|
|
if (np.has(QStringLiteral("playerList"))) {
|
2018-04-27 23:22:45 +01:00
|
|
|
QStringList players = np.get<QStringList>(QStringLiteral("playerList"));
|
|
|
|
qDeleteAll(m_players);
|
|
|
|
m_players.clear();
|
|
|
|
for (const QString& player : players) {
|
|
|
|
m_players[player] = new MprisRemotePlayer();
|
2018-04-27 23:23:45 +01:00
|
|
|
requestPlayerStatus(player);
|
2018-04-27 23:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_players.empty()) {
|
|
|
|
m_currentPlayer = QString();
|
|
|
|
} else if (!m_players.contains(m_currentPlayer)) {
|
|
|
|
m_currentPlayer = m_players.keys().first();
|
|
|
|
}
|
|
|
|
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
Q_EMIT propertiesChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
long MprisRemotePlugin::position() const
|
|
|
|
{
|
2018-04-27 23:22:45 +01:00
|
|
|
auto player = m_players.value(m_currentPlayer);
|
|
|
|
return player ? player->position() : 0;
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString MprisRemotePlugin::dbusPath() const
|
|
|
|
{
|
|
|
|
return "/modules/kdeconnect/devices/" + device()->id() + "/mprisremote";
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:23:45 +01:00
|
|
|
void MprisRemotePlugin::requestPlayerStatus(const QString& player)
|
2015-06-18 03:01:01 +01:00
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {
|
2018-04-27 23:23:45 +01:00
|
|
|
{"player", player},
|
2016-06-21 19:07:12 +01:00
|
|
|
{"requestNowPlaying", true},
|
|
|
|
{"requestVolume", true}}
|
|
|
|
);
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisRemotePlugin::requestPlayerList()
|
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{"requestPlayerList", true}});
|
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisRemotePlugin::sendAction(const QString& action)
|
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {
|
2018-04-27 23:22:45 +01:00
|
|
|
{"player", m_currentPlayer},
|
2016-06-21 19:07:12 +01:00
|
|
|
{"action", action}
|
|
|
|
});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisRemotePlugin::seek(int offset) const
|
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {
|
2018-04-27 23:22:45 +01:00
|
|
|
{"player", m_currentPlayer},
|
2016-06-21 19:07:12 +01:00
|
|
|
{"Seek", offset}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisRemotePlugin::setVolume(int volume)
|
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {
|
2018-04-27 23:22:45 +01:00
|
|
|
{"player", m_currentPlayer},
|
2016-06-21 19:07:12 +01:00
|
|
|
{"setVolume",volume}
|
|
|
|
});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisRemotePlugin::setPosition(int position)
|
|
|
|
{
|
2018-03-04 19:48:51 +00:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {
|
2018-04-27 23:22:45 +01:00
|
|
|
{"player", m_currentPlayer},
|
2016-06-21 19:07:12 +01:00
|
|
|
{"SetPosition", position}
|
|
|
|
});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
|
2018-04-27 23:22:45 +01:00
|
|
|
m_players[m_currentPlayer]->setPosition(position);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisRemotePlugin::setPlayer(const QString& player)
|
|
|
|
{
|
2018-04-27 23:22:45 +01:00
|
|
|
if (m_currentPlayer != player) {
|
|
|
|
m_currentPlayer = player;
|
2018-04-27 23:23:45 +01:00
|
|
|
requestPlayerStatus(player);
|
2018-04-27 23:22:45 +01:00
|
|
|
Q_EMIT propertiesChanged();
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:22:45 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-06-18 03:01:01 +01:00
|
|
|
#include "mprisremoteplugin.moc"
|