2015-06-18 03:01:01 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2013 Albert Vaca <albertvaka@gmail.com>
|
2015-06-18 03:01:01 +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
|
2015-06-18 03:01:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mprisremoteplugin.h"
|
|
|
|
|
|
|
|
#include <KPluginFactory>
|
|
|
|
|
|
|
|
#include <QDebug>
|
2024-07-31 17:39:28 +01:00
|
|
|
#include <QStandardPaths>
|
2015-06-18 03:01:01 +01:00
|
|
|
|
|
|
|
#include <core/device.h>
|
2024-07-31 17:39:28 +01:00
|
|
|
#include <filetransferjob.h>
|
2015-06-18 03:01:01 +01:00
|
|
|
|
2024-07-31 17:39:28 +01:00
|
|
|
#include "albumart_cache.h"
|
2020-05-26 17:55:47 +01:00
|
|
|
#include "plugin_mprisremote_debug.h"
|
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(MprisRemotePlugin, "kdeconnect_mprisremote.json")
|
2015-06-18 03:01:01 +01:00
|
|
|
|
2023-07-31 08:25:45 +01:00
|
|
|
void 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)
|
2023-07-31 08:25:45 +01:00
|
|
|
return;
|
2015-06-18 03:01:01 +01:00
|
|
|
|
2024-07-31 17:39:28 +01:00
|
|
|
if (np.get<bool>(QStringLiteral("transferringAlbumArt"), false)) {
|
|
|
|
AlbumArtCache::instance()->handleAlbumArt(np);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:22:45 +01:00
|
|
|
if (np.has(QStringLiteral("player"))) {
|
2019-01-30 23:52:56 +00:00
|
|
|
const QString player = np.get<QString>(QStringLiteral("player"));
|
2022-09-10 22:23:52 +01:00
|
|
|
if (!m_players.contains(player)) {
|
|
|
|
m_players[player] = new MprisRemotePlayer(player, this);
|
2019-01-30 23:52:56 +00:00
|
|
|
}
|
|
|
|
m_players[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"));
|
2019-07-25 22:40:58 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Remove players not available any more
|
2019-07-25 22:40:58 +01:00
|
|
|
for (auto iter = m_players.begin(); iter != m_players.end();) {
|
|
|
|
if (!players.contains(iter.key())) {
|
|
|
|
iter.value()->deleteLater();
|
|
|
|
iter = m_players.erase(iter);
|
|
|
|
} else {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
// Add new players
|
|
|
|
for (const QString &player : players) {
|
2019-07-25 22:40:58 +01:00
|
|
|
if (!m_players.contains(player)) {
|
|
|
|
m_players[player] = new MprisRemotePlayer(player, this);
|
|
|
|
requestPlayerStatus(player);
|
|
|
|
}
|
2018-04-27 23:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_players.empty()) {
|
|
|
|
m_currentPlayer = QString();
|
|
|
|
} else if (!m_players.contains(m_currentPlayer)) {
|
2019-05-05 15:03:02 +01:00
|
|
|
m_currentPlayer = m_players.firstKey();
|
2018-04-27 23:22:45 +01:00
|
|
|
}
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
Q_EMIT propertiesChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-08-12 12:26:05 +01:00
|
|
|
return QLatin1String("/modules/kdeconnect/devices/%1/mprisremote").arg(device()->id());
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlugin::requestPlayerStatus(const QString &player)
|
2015-06-18 03:01:01 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST,
|
|
|
|
{{QStringLiteral("player"), player}, {QStringLiteral("requestNowPlaying"), true}, {QStringLiteral("requestVolume"), true}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MprisRemotePlugin::requestPlayerList()
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("requestPlayerList"), true}});
|
2018-03-04 19:48:51 +00:00
|
|
|
sendPacket(np);
|
2015-06-18 03:01:01 +01:00
|
|
|
}
|
|
|
|
|
2024-07-31 17:39:28 +01:00
|
|
|
void MprisRemotePlugin::requestAlbumArt(const QString &player, const QString &album_art_url)
|
|
|
|
{
|
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), player}, {QStringLiteral("albumArtUrl"), album_art_url}});
|
|
|
|
qInfo(KDECONNECT_PLUGIN_MPRISREMOTE) << "Requesting album art " << np.serialize();
|
|
|
|
sendPacket(np);
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlugin::sendAction(const QString &action)
|
2015-06-18 03:01:01 +01:00
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("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
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("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)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("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)
|
|
|
|
{
|
2022-09-10 22:23:52 +01:00
|
|
|
NetworkPacket np(PACKET_TYPE_MPRIS_REQUEST, {{QStringLiteral("player"), m_currentPlayer}, {QStringLiteral("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
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
void MprisRemotePlugin::setPlayer(const QString &player)
|
2015-06-18 03:01:01 +01:00
|
|
|
{
|
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::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();
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:19:53 +01:00
|
|
|
bool MprisRemotePlugin::canSeek() const
|
|
|
|
{
|
|
|
|
auto player = m_players.value(m_currentPlayer);
|
|
|
|
return player ? player->canSeek() : false;
|
|
|
|
}
|
|
|
|
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_mprisremoteplugin.cpp"
|
2015-06-18 03:01:01 +01:00
|
|
|
#include "mprisremoteplugin.moc"
|