Changeset View
Changeset View
Standalone View
Standalone View
lib/mpris2/mpris2service.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Gwenview: an image viewer | ||||
3 | Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
4 | | ||||
5 | This program is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU General Public License | ||||
7 | as published by the Free Software Foundation; either version 2 | ||||
8 | of the License, or (at your option) any later version. | ||||
9 | | ||||
10 | This program is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | GNU General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU General Public License | ||||
16 | along with this program; if not, write to the Free Software | ||||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "mpris2service.h" | ||||
21 | | ||||
22 | // lib | ||||
23 | #include "mprismediaplayer2.h" | ||||
24 | #include "mprismediaplayer2player.h" | ||||
25 | #include <slideshow.h> | ||||
26 | // Qt | ||||
27 | #include <QDBusConnection> | ||||
28 | // std | ||||
29 | #include <unistd.h> | ||||
30 | | ||||
31 | namespace Gwenview | ||||
32 | { | ||||
33 | | ||||
34 | inline QString mediaPlayer2ObjectPath() { return QStringLiteral("/org/mpris/MediaPlayer2"); } | ||||
35 | | ||||
36 | | ||||
37 | Mpris2Service::Mpris2Service(SlideShow* slideShow, ContextManager* contextManager, | ||||
38 | QAction* toggleSlideShowAction, QAction* fullScreenAction, | ||||
39 | QAction* previousAction, QAction* nextAction, | ||||
40 | QObject* parent) | ||||
41 | : QObject(parent) | ||||
42 | { | ||||
43 | QDBusConnection sessionBus = QDBusConnection::sessionBus(); | ||||
44 | | ||||
45 | // try to register MPRIS presentation object | ||||
46 | // to be done before registering the service name, so it is already present | ||||
47 | // when controllers react to the service name having appeared | ||||
48 | new MprisMediaPlayer2(mediaPlayer2ObjectPath(), fullScreenAction, this); | ||||
49 | new MprisMediaPlayer2Player(mediaPlayer2ObjectPath(), slideShow, contextManager, | ||||
50 | toggleSlideShowAction, fullScreenAction, previousAction, nextAction, this); | ||||
51 | | ||||
52 | const bool objectRegistered = sessionBus.registerObject(mediaPlayer2ObjectPath(), this, QDBusConnection::ExportAdaptors); | ||||
53 | | ||||
54 | // try to register MPRIS presentation service | ||||
55 | if (objectRegistered) { | ||||
56 | mMpris2ServiceName = QStringLiteral("org.mpris.MediaPlayer2.Gwenview"); | ||||
57 | | ||||
58 | bool serviceRegistered = QDBusConnection::sessionBus().registerService(mMpris2ServiceName); | ||||
59 | | ||||
60 | // Perhaps not the first instance? Try again with another name, as specified by MPRIS2 spec: | ||||
61 | if (!serviceRegistered) { | ||||
62 | mMpris2ServiceName = mMpris2ServiceName + QLatin1String(".instance") + QString::number(getpid()); | ||||
63 | serviceRegistered = QDBusConnection::sessionBus().registerService(mMpris2ServiceName); | ||||
64 | } | ||||
65 | if (!serviceRegistered) { | ||||
66 | mMpris2ServiceName.clear(); | ||||
67 | sessionBus.unregisterObject(mediaPlayer2ObjectPath()); | ||||
68 | } | ||||
69 | } | ||||
70 | } | ||||
71 | | ||||
72 | | ||||
73 | Mpris2Service::~Mpris2Service() | ||||
74 | { | ||||
75 | if (!mMpris2ServiceName.isEmpty()) { | ||||
76 | QDBusConnection sessionBus = QDBusConnection::sessionBus(); | ||||
77 | sessionBus.unregisterService(mMpris2ServiceName); | ||||
78 | sessionBus.unregisterObject(mediaPlayer2ObjectPath()); | ||||
79 | } | ||||
80 | } | ||||
81 | | ||||
82 | } |