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 "lockscreenwatcher.h" | ||||
24 | #include "mprismediaplayer2.h" | ||||
25 | #include "mprismediaplayer2player.h" | ||||
26 | #include <slideshow.h> | ||||
27 | // Qt | ||||
28 | #include <QDBusConnection> | ||||
29 | // std | ||||
30 | #include <unistd.h> | ||||
31 | | ||||
32 | namespace Gwenview | ||||
33 | { | ||||
34 | | ||||
35 | inline QString mediaPlayer2ObjectPath() { return QStringLiteral("/org/mpris/MediaPlayer2"); } | ||||
36 | | ||||
37 | | ||||
38 | Mpris2Service::Mpris2Service(SlideShow* slideShow, ContextManager* contextManager, | ||||
39 | QAction* toggleSlideShowAction, QAction* fullScreenAction, | ||||
40 | QAction* previousAction, QAction* nextAction, | ||||
41 | QObject* parent) | ||||
42 | : QObject(parent) | ||||
43 | { | ||||
44 | new MprisMediaPlayer2(mediaPlayer2ObjectPath(), fullScreenAction, this); | ||||
45 | new MprisMediaPlayer2Player(mediaPlayer2ObjectPath(), slideShow, contextManager, | ||||
46 | toggleSlideShowAction, fullScreenAction, previousAction, nextAction, | ||||
47 | this); | ||||
48 | | ||||
49 | // To avoid appearing in the media controller on the lock screen, | ||||
50 | // which might be not expected or wanted for Gwenview, | ||||
51 | // the MPRIS service is unregistered while the lockscreen is active. | ||||
52 | LockScreenWatcher *lockScreenWatcher = new LockScreenWatcher(this); | ||||
53 | connect(lockScreenWatcher, &LockScreenWatcher::isLockedChanged, | ||||
54 | this, &Mpris2Service::onLockScreenLockedChanged); | ||||
55 | | ||||
56 | if (!lockScreenWatcher->isLocked()) { | ||||
57 | registerOnDBus(); | ||||
58 | } | ||||
59 | } | ||||
60 | | ||||
61 | Mpris2Service::~Mpris2Service() | ||||
62 | { | ||||
63 | unregisterOnDBus(); | ||||
64 | } | ||||
65 | | ||||
66 | void Mpris2Service::registerOnDBus() | ||||
67 | { | ||||
68 | QDBusConnection sessionBus = QDBusConnection::sessionBus(); | ||||
69 | | ||||
70 | // try to register MPRIS presentation object | ||||
71 | // to be done before registering the service name, so it is already present | ||||
72 | // when controllers react to the service name having appeared | ||||
73 | const bool objectRegistered = sessionBus.registerObject(mediaPlayer2ObjectPath(), this, QDBusConnection::ExportAdaptors); | ||||
74 | | ||||
75 | // try to register MPRIS presentation service | ||||
76 | if (objectRegistered) { | ||||
77 | mMpris2ServiceName = QStringLiteral("org.mpris.MediaPlayer2.Gwenview"); | ||||
78 | | ||||
79 | bool serviceRegistered = QDBusConnection::sessionBus().registerService(mMpris2ServiceName); | ||||
80 | | ||||
81 | // Perhaps not the first instance? Try again with another name, as specified by MPRIS2 spec: | ||||
82 | if (!serviceRegistered) { | ||||
83 | mMpris2ServiceName = mMpris2ServiceName + QLatin1String(".instance") + QString::number(getpid()); | ||||
84 | serviceRegistered = QDBusConnection::sessionBus().registerService(mMpris2ServiceName); | ||||
85 | } | ||||
86 | if (!serviceRegistered) { | ||||
87 | mMpris2ServiceName.clear(); | ||||
88 | sessionBus.unregisterObject(mediaPlayer2ObjectPath()); | ||||
89 | } | ||||
90 | } | ||||
91 | } | ||||
92 | | ||||
93 | void Mpris2Service::unregisterOnDBus() | ||||
94 | { | ||||
95 | if (mMpris2ServiceName.isEmpty()) { | ||||
96 | return; | ||||
97 | } | ||||
98 | | ||||
99 | QDBusConnection sessionBus = QDBusConnection::sessionBus(); | ||||
100 | sessionBus.unregisterService(mMpris2ServiceName); | ||||
101 | sessionBus.unregisterObject(mediaPlayer2ObjectPath()); | ||||
102 | } | ||||
103 | | ||||
104 | void Mpris2Service::onLockScreenLockedChanged(bool isLocked) | ||||
105 | { | ||||
106 | if (isLocked) { | ||||
107 | unregisterOnDBus(); | ||||
108 | } else { | ||||
109 | registerOnDBus(); | ||||
110 | } | ||||
111 | } | ||||
112 | | ||||
113 | } |