2013-07-29 17:43:13 +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/>.
|
|
|
|
*/
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
#include "mpriscontrolplugin.h"
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2013-09-03 01:14:27 +01:00
|
|
|
#include <QDBusArgument>
|
2013-07-29 17:43:13 +01:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusInterface>
|
|
|
|
#include <qdbusconnectioninterface.h>
|
|
|
|
#include <QDBusReply>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
|
2014-06-14 15:34:00 +01:00
|
|
|
#include <core/kdebugnamespace.h>
|
|
|
|
#include <core/device.h>
|
2013-11-06 21:16:55 +00:00
|
|
|
#include "mprisdbusinterface.h"
|
|
|
|
#include "propertiesdbusinterface.h"
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< MprisControlPlugin >(); )
|
2014-06-17 21:21:20 +01:00
|
|
|
K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_mpriscontrol", "kdeconnect-plugins") )
|
2013-08-13 05:35:58 +01:00
|
|
|
|
|
|
|
MprisControlPlugin::MprisControlPlugin(QObject* parent, const QVariantList& args)
|
|
|
|
: KdeConnectPlugin(parent, args)
|
2013-07-29 17:43:13 +01:00
|
|
|
{
|
2013-07-30 19:21:06 +01:00
|
|
|
|
|
|
|
//Detect new interfaces
|
|
|
|
connect(QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
|
|
|
|
this, SLOT(serviceOwnerChanged(QString,QString,QString)));
|
|
|
|
|
|
|
|
//Add existing interfaces
|
|
|
|
QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
|
|
|
|
Q_FOREACH (const QString& iface, interfaces) {
|
|
|
|
if (iface.startsWith("org.mpris.MediaPlayer2")) {
|
|
|
|
addPlayer(iface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::serviceOwnerChanged(const QString &name,
|
2013-07-30 19:21:06 +01:00
|
|
|
const QString &oldOwner,
|
|
|
|
const QString &newOwner)
|
|
|
|
{
|
|
|
|
Q_UNUSED(oldOwner);
|
|
|
|
|
|
|
|
if (name.startsWith("org.mpris.MediaPlayer2")) {
|
|
|
|
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Mpris (un)registered in bus" << name << oldOwner << newOwner;
|
2013-07-30 19:21:06 +01:00
|
|
|
|
|
|
|
if (oldOwner.isEmpty()) {
|
|
|
|
addPlayer(name);
|
|
|
|
} else if (newOwner.isEmpty()) {
|
|
|
|
removePlayer(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::addPlayer(const QString& service)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2013-08-10 04:21:55 +01:00
|
|
|
QDBusInterface mprisInterface(service, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2");
|
2013-09-03 01:35:08 +01:00
|
|
|
//FIXME: This call hangs and returns an empty string if KDED is still starting!
|
2014-02-28 17:50:06 +00:00
|
|
|
const QString identity = mprisInterface.property("Identity").toString();
|
2013-08-10 04:21:55 +01:00
|
|
|
playerList[identity] = service;
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Mpris addPlayer" << service << "->" << identity;
|
2013-07-30 19:21:06 +01:00
|
|
|
sendPlayerList();
|
2013-08-10 04:21:55 +01:00
|
|
|
|
|
|
|
OrgFreedesktopDBusPropertiesInterface* freedesktopInterface = new OrgFreedesktopDBusPropertiesInterface(service, "/org/mpris/MediaPlayer2", QDBusConnection::sessionBus(), this);
|
|
|
|
connect(freedesktopInterface, SIGNAL(PropertiesChanged(QString, QVariantMap, QStringList)), this, SLOT(propertiesChanged(QString, QVariantMap)));
|
|
|
|
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::propertiesChanged(const QString& propertyInterface, const QVariantMap& properties)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2013-08-13 22:26:42 +01:00
|
|
|
Q_UNUSED(propertyInterface);
|
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
NetworkPackage np(PACKAGE_TYPE_MPRIS);
|
|
|
|
bool somethingToSend = false;
|
|
|
|
if (properties.contains("Volume")) {
|
|
|
|
int volume = (int) (properties["Volume"].toDouble()*100);
|
|
|
|
if (volume != prevVolume) {
|
|
|
|
np.set("volume",volume);
|
|
|
|
prevVolume = volume;
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (properties.contains("Metadata")) {
|
|
|
|
QDBusArgument bullshit = qvariant_cast<QDBusArgument>(properties["Metadata"]);
|
|
|
|
QVariantMap nowPlayingMap;
|
|
|
|
bullshit >> nowPlayingMap;
|
|
|
|
if (nowPlayingMap.contains("xesam:title")) {
|
|
|
|
QString nowPlaying = nowPlayingMap["xesam:title"].toString();
|
|
|
|
if (nowPlayingMap.contains("xesam:artist")) {
|
|
|
|
nowPlaying = nowPlayingMap["xesam:artist"].toString() + " - " + nowPlaying;
|
|
|
|
}
|
|
|
|
np.set("nowPlaying",nowPlaying);
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (properties.contains("PlaybackStatus")) {
|
|
|
|
bool playing = (properties["PlaybackStatus"].toString() == "Playing");
|
|
|
|
np.set("isPlaying", playing);
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (somethingToSend) {
|
|
|
|
OrgFreedesktopDBusPropertiesInterface* interface = (OrgFreedesktopDBusPropertiesInterface*)sender();
|
|
|
|
const QString& service = interface->service();
|
|
|
|
const QString& player = playerList.key(service);
|
|
|
|
np.set("player", player);
|
2014-06-14 19:35:00 +01:00
|
|
|
sendPackage(np);
|
2013-08-10 04:21:55 +01:00
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::removePlayer(const QString& ifaceName)
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
2014-02-28 17:50:06 +00:00
|
|
|
const QString identity = playerList.key(ifaceName);
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Mpris removePlayer" << ifaceName << "->" << identity;
|
2013-09-03 01:35:08 +01:00
|
|
|
playerList.remove(identity);
|
2013-08-10 04:21:55 +01:00
|
|
|
sendPlayerList();
|
2013-07-29 17:43:13 +01:00
|
|
|
}
|
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
bool MprisControlPlugin::receivePackage (const NetworkPackage& np)
|
2013-07-29 17:43:13 +01:00
|
|
|
{
|
2013-09-03 01:06:56 +01:00
|
|
|
if (np.has("playerList")) {
|
|
|
|
return false; //Whoever sent this is an mpris client and not an mpris control!
|
|
|
|
}
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
//Send the player list
|
2014-02-28 17:50:06 +00:00
|
|
|
const QString player = np.get<QString>("player");
|
2013-08-10 04:21:55 +01:00
|
|
|
bool valid_player = playerList.contains(player);
|
|
|
|
if (!valid_player || np.get<bool>("requestPlayerList")) {
|
2013-08-13 05:35:58 +01:00
|
|
|
sendPlayerList();
|
2013-08-10 04:21:55 +01:00
|
|
|
if (!valid_player) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 17:43:13 +01:00
|
|
|
|
2013-08-10 04:21:55 +01:00
|
|
|
//Do something to the mpris interface
|
|
|
|
OrgMprisMediaPlayer2PlayerInterface mprisInterface(playerList[player], "/org/mpris/MediaPlayer2", QDBusConnection::sessionBus());
|
|
|
|
if (np.has("action")) {
|
|
|
|
const QString& action = np.get<QString>("action");
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Calling action" << action << "in" << playerList[player];
|
2014-10-10 19:47:35 +01:00
|
|
|
//TODO: Check for valid actions, currently we trust anything the other end sends us
|
2013-08-10 04:21:55 +01:00
|
|
|
mprisInterface.call(action);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
if (np.has("setVolume")) {
|
|
|
|
double volume = np.get<int>("setVolume")/100.f;
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Setting volume" << volume << "to" << playerList[player];
|
2013-08-10 04:21:55 +01:00
|
|
|
mprisInterface.setVolume(volume);
|
|
|
|
}
|
2013-10-29 19:17:42 +00:00
|
|
|
if (np.has("Seek")) {
|
|
|
|
int offset = np.get<int>("Seek");
|
2014-07-01 00:25:12 +01:00
|
|
|
kDebug(debugArea()) << "Seeking" << offset << "to" << playerList[player];
|
2013-10-29 19:17:42 +00:00
|
|
|
mprisInterface.Seek(offset);
|
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
|
|
|
|
//Send something read from the mpris interface
|
|
|
|
NetworkPackage answer(PACKAGE_TYPE_MPRIS);
|
|
|
|
bool somethingToSend = false;
|
|
|
|
if (np.get<bool>("requestNowPlaying")) {
|
|
|
|
|
|
|
|
QVariantMap nowPlayingMap = mprisInterface.metadata();
|
|
|
|
QString nowPlaying = nowPlayingMap["xesam:title"].toString();
|
|
|
|
if (nowPlayingMap.contains("xesam:artist")) {
|
|
|
|
nowPlaying = nowPlayingMap["xesam:artist"].toString() + " - " + nowPlaying;
|
|
|
|
}
|
|
|
|
|
|
|
|
answer.set("nowPlaying",nowPlaying);
|
|
|
|
|
|
|
|
bool playing = (mprisInterface.playbackStatus() == QLatin1String("Playing"));
|
|
|
|
answer.set("isPlaying", playing);
|
|
|
|
|
|
|
|
somethingToSend = true;
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|
2013-08-10 04:21:55 +01:00
|
|
|
if (np.get<bool>("requestVolume")) {
|
|
|
|
int volume = (int)(mprisInterface.volume() * 100);
|
|
|
|
answer.set("volume",volume);
|
|
|
|
somethingToSend = true;
|
|
|
|
}
|
|
|
|
if (somethingToSend) {
|
|
|
|
answer.set("player", player);
|
2014-06-14 19:35:00 +01:00
|
|
|
sendPackage(answer);
|
2013-07-29 17:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-30 19:21:06 +01:00
|
|
|
|
2013-08-13 05:35:58 +01:00
|
|
|
void MprisControlPlugin::sendPlayerList()
|
2013-07-30 19:21:06 +01:00
|
|
|
{
|
|
|
|
NetworkPackage np(PACKAGE_TYPE_MPRIS);
|
2013-08-10 04:21:55 +01:00
|
|
|
np.set("playerList",playerList.keys());
|
2014-06-14 19:35:00 +01:00
|
|
|
sendPackage(np);
|
2013-07-30 19:21:06 +01:00
|
|
|
}
|