-
Notifications
You must be signed in to change notification settings - Fork 3
/
mpris2player.h
97 lines (82 loc) · 2.77 KB
/
mpris2player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef MPRIS2PLAYER_H
#define MPRIS2PLAYER_H
#include <QDBusInterface>
#include <QMap>
#include <QDebug>
#include <QVariantMap>
#include <QDBusArgument>
class Mpris2Player : public QObject
{
Q_OBJECT
Q_PROPERTY(QVariantMap metadata READ metadata NOTIFY metadataNotify)
Q_PROPERTY(QString name MEMBER name)
public:
explicit Mpris2Player(QString serviceName, QObject *parent = 0) : QObject(parent),
iface(serviceName,
"/org/mpris/MediaPlayer2",
"org.mpris.MediaPlayer2.Player"), playerInterface(serviceName, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2"), name(playerInterface.property("Identity").toString()) {
QDBusConnection::sessionBus().connect(serviceName, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(metadataReceived(QDBusMessage)));
}
QVariantMap metadata() const {
QDBusInterface iface(this->iface.service(), "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties");
QDBusMessage result = iface.call("Get", "org.mpris.MediaPlayer2.Player", "Metadata");
const QDBusArgument &arg = result.arguments().at(0).value<QDBusVariant>().variant().value<QDBusArgument>();
arg.beginMap();
QVariantMap map;
while (!arg.atEnd()) {
QVariant var;
QString str;
arg.beginMapEntry();
arg >> str >> var;
arg.endMapEntry();
map.insert(str, var);
}
arg.endMap();
return map;
}
Q_INVOKABLE void playPause() {
iface.call("PlayPause");
}
Q_INVOKABLE void next() {
iface.call("Next");
}
Q_INVOKABLE void previous() {
iface.call("Previous");
}
Q_INVOKABLE void stop() {
iface.call("Stop");
}
Q_INVOKABLE void seek(const QVariant &position) {
iface.call("Seek", position.toLongLong());
}
Q_INVOKABLE void openUri(const QVariant &uri) {
iface.call("OpenUri", uri.toString());
}
Q_INVOKABLE void raise() {
playerInterface.call("Raise");
}
Q_INVOKABLE void quit() {
playerInterface.call("Quit");
}
QDBusInterface iface;
QDBusInterface playerInterface;
QString name;
signals:
QVariantMap metadataNotify(QVariantMap map);
private slots:
void metadataReceived(QDBusMessage msg) {
const QDBusArgument &arg = msg.arguments().at(1).value<QDBusArgument>();
arg.beginMap();
while (!arg.atEnd()) {
QString key;
arg.beginMapEntry();
arg >> key;
if (key == "Metadata") {
emit metadataNotify(metadata());
}
arg.endMapEntry();
}
arg.endMap();
}
};
#endif // MPRIS2PLAYER_H