Last active
August 29, 2015 14:11
-
-
Save ricardomv/f2fe7c560e509d8f7bb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QGuiApplication> | |
#include <QDBusServiceWatcher> | |
#include <QDBusInterface> | |
#include <QDBusReply> | |
#include <QDBusConnectionInterface> | |
class Mpris2Player : public QDBusInterface | |
{ | |
Q_OBJECT | |
public: | |
Mpris2Player(QString serviceName) | |
: QDBusInterface(serviceName, | |
"/org/mpris/MediaPlayer2", | |
"org.mpris.MediaPlayer2.Player") { | |
qDebug() << "Connected to player :" << serviceName; | |
} | |
~Mpris2Player() { | |
qDebug() << "Disonnected from player :" << service(); | |
} | |
void Play() { this->call("Play"); } | |
void PlayPause() { this->call("PlayPause"); } | |
void Next() { this->call("Next"); } | |
void Previous() { this->call("Previous"); } | |
}; | |
class Mpris2Connection : public QObject | |
{ | |
Q_OBJECT | |
QDBusServiceWatcher *watcher; | |
public: | |
QList<Mpris2Player*> playerList; | |
Mpris2Connection() { | |
QDBusConnection bus = QDBusConnection::sessionBus(); | |
const QStringList services = bus.interface()->registeredServiceNames(); | |
foreach(QString name, services.filter("org.mpris.MediaPlayer2")) { | |
playerList.append(new Mpris2Player(name)); | |
} | |
watcher = new QDBusServiceWatcher(QString(), bus); | |
connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), | |
this, SLOT(serviceOwnerChanged(QString,QString,QString))); | |
} | |
~Mpris2Connection() { | |
delete watcher; | |
while (!playerList.isEmpty()) | |
delete playerList.takeFirst(); | |
} | |
private slots: | |
void serviceOwnerChanged(const QString &serviceName, | |
const QString &oldOwner, | |
const QString &newOwner) { | |
if (oldOwner.isEmpty() && serviceName.startsWith("org.mpris.MediaPlayer2.")) { | |
playerList.append(new Mpris2Player(serviceName)); | |
} else if (newOwner.isEmpty() && serviceName.startsWith("org.mpris.MediaPlayer2.")) { | |
for (int i = 0; i < playerList.size(); ++i) { | |
if (playerList.at(i)->service() == serviceName) { | |
delete playerList.at(i); | |
playerList.removeAt(i); | |
} | |
} | |
} | |
} | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
QGuiApplication app(argc, argv); | |
Mpris2Connection mediaControler; | |
mediaControler.playerList.first()->Next(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to test the code, but I'm getting
undefined reference to vtable
errors for theMpris2Player
and theMpris2Connection
classes. Any ideas?UPDATE: Removing Q_OBJECT macro works :) But I do get
QObject::connect: No such slot QObject::serviceOwnerChanged(QString,QString,QString) in ../Mprisqml/main.cpp:45
UPDATE 2: So basically a little googling tells you everything one needs to know. I'll improve the code in the next few days and see how it goes from there. BTW, removing Q_OBJECT is wrong, but Q_OBJECT derived classes must be defined in their own header file :)
UPDATE 3: I fixed your code, now I'm gonna implement it as a QML component