File indexing completed on 2024-04-28 04:55:51

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005     SPDX-FileCopyrightText: 2010-2011 Ramin Gomari <ramin.gomari@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 
0009     This code is loosely inspired by the "now listening" plugin of KMess.
0010     Therefore some parts are also
0011     SPDX-FileCopyrightText: 2006 Diederik van der Boor <vdboor--at--codingdomain.com>
0012 */
0013 
0014 #include "mpris.h"
0015 
0016 #include <QDBusConnection>
0017 #include <QDBusConnectionInterface>
0018 #include <QDBusInterface>
0019 #include <QDBusMetaType>
0020 #include <QDBusReply>
0021 
0022 Q_DECLARE_METATYPE(MPRIS::MprisStatusStruct)
0023 
0024 /**
0025  * Marshall the MprisStatus structure into a DBus argument
0026  */
0027 QDBusArgument &operator<<(QDBusArgument &argument, const MPRIS::MprisStatusStruct  &status)
0028 {
0029     argument.beginStructure();
0030     argument << status.Status << status.hasShuffle << status.hasRepeat << status.hasPlaylistRepeat;
0031     argument.endStructure();
0032     return argument;
0033 }
0034 
0035 /**
0036  * Demarshall the MprisPlayerStatus structure from a DBus argument
0037  */
0038 const QDBusArgument &operator>>(const QDBusArgument &argument, MPRIS::MprisStatusStruct  &status)
0039 {
0040     argument.beginStructure();
0041     argument >> status.Status >> status.hasShuffle >> status.hasRepeat >> status.hasPlaylistRepeat;
0042     argument.endStructure();
0043     return argument;
0044 }
0045 
0046 MPRIS::MPRIS(const QString PlayerName)
0047 {
0048     qDBusRegisterMetaType<MprisStatusStruct>();
0049     const QString serviceName = QStringLiteral("org.mpris.%1").arg(PlayerName);
0050     if (QDBusConnection::sessionBus().interface()->isServiceRegistered(serviceName).value()) {
0051         valid = true;
0052     } else {
0053         valid = false;
0054         return;
0055     }
0056     QDBusInterface playerInterface(serviceName,
0057                                    QLatin1String("/Player"),
0058                                    QLatin1String("org.freedesktop.MediaPlayer"));
0059     QDBusReply<MprisStatusStruct> getStatus = playerInterface.call(QLatin1String("GetStatus"));
0060     QDBusReply< QMap<QString, QVariant> > getMetadata = playerInterface.call(QLatin1String("GetMetadata"));
0061 
0062     if (!getStatus.isValid() || !getMetadata.isValid()) {
0063         valid = false;
0064         return;
0065     }
0066     status = getStatus.value();
0067     trackInfo = getMetadata.value();
0068     QDBusInterface rootInterface(serviceName,
0069                                  QLatin1String("/"),
0070                                  QLatin1String("org.freedesktop.MediaPlayer"));
0071     QDBusReply<QString> getIdentity = rootInterface.call(QLatin1String("Identity"));
0072     if (getIdentity.isValid()) {
0073         Identity = getIdentity.value();
0074     } else {
0075         valid = false;
0076         return;
0077     }
0078 }
0079 
0080 MPRIS::~MPRIS()
0081 {
0082 
0083 }
0084 
0085 QStringList MPRIS::getRunningPlayers()
0086 {
0087     QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value().filter(QLatin1String("org.mpris."));
0088     services.removeDuplicates();
0089     services.replaceInStrings(QLatin1String("org.mpris."), QString());
0090     return services;
0091 }
0092