File indexing completed on 2024-05-12 04:19:46
0001 /* 0002 Gwenview: an image viewer 0003 Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org> 0004 0005 This program is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU General Public License 0007 as published by the Free Software Foundation; either version 2 0008 of the License, or (at your option) any later version. 0009 0010 This program is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 GNU General Public License for more details. 0014 0015 You should have received a copy of the GNU General Public License 0016 along with this program; if not, write to the Free Software 0017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "mpris2service.h" 0021 0022 // lib 0023 #include "lockscreenwatcher.h" 0024 #include "mprismediaplayer2.h" 0025 #include "mprismediaplayer2player.h" 0026 #include <slideshow.h> 0027 // Qt 0028 #include <QDBusConnection> 0029 // std 0030 #include <unistd.h> 0031 0032 namespace Gwenview 0033 { 0034 inline QString mediaPlayer2ObjectPath() 0035 { 0036 return QStringLiteral("/org/mpris/MediaPlayer2"); 0037 } 0038 0039 Mpris2Service::Mpris2Service(SlideShow *slideShow, 0040 ContextManager *contextManager, 0041 QAction *toggleSlideShowAction, 0042 QAction *fullScreenAction, 0043 QAction *previousAction, 0044 QAction *nextAction, 0045 QObject *parent) 0046 : QObject(parent) 0047 , m_fullscreenAction(fullScreenAction) 0048 { 0049 new MprisMediaPlayer2(mediaPlayer2ObjectPath(), fullScreenAction, this); 0050 new MprisMediaPlayer2Player(mediaPlayer2ObjectPath(), slideShow, contextManager, toggleSlideShowAction, fullScreenAction, previousAction, nextAction, this); 0051 0052 // To avoid appearing in the media controller on the lock screen, 0053 // which might be not expected or wanted for Gwenview, 0054 // the MPRIS service is unregistered while the lockscreen is active. 0055 auto lockScreenWatcher = new LockScreenWatcher(this); 0056 connect(lockScreenWatcher, &LockScreenWatcher::isLockedChanged, this, &Mpris2Service::onLockScreenLockedChanged); 0057 connect(fullScreenAction, &QAction::toggled, this, &Mpris2Service::onFullScreenActionToggled); 0058 0059 if (!lockScreenWatcher->isLocked() && fullScreenAction->isChecked()) { 0060 registerOnDBus(); 0061 } 0062 } 0063 0064 Mpris2Service::~Mpris2Service() 0065 { 0066 unregisterOnDBus(); 0067 } 0068 0069 void Mpris2Service::registerOnDBus() 0070 { 0071 QDBusConnection sessionBus = QDBusConnection::sessionBus(); 0072 0073 // try to register MPRIS presentation object 0074 // to be done before registering the service name, so it is already present 0075 // when controllers react to the service name having appeared 0076 const bool objectRegistered = sessionBus.registerObject(mediaPlayer2ObjectPath(), this, QDBusConnection::ExportAdaptors); 0077 0078 // try to register MPRIS presentation service 0079 if (objectRegistered) { 0080 mMpris2ServiceName = QStringLiteral("org.mpris.MediaPlayer2.Gwenview"); 0081 0082 bool serviceRegistered = QDBusConnection::sessionBus().registerService(mMpris2ServiceName); 0083 0084 // Perhaps not the first instance? Try again with another name, as specified by MPRIS2 spec: 0085 if (!serviceRegistered) { 0086 mMpris2ServiceName = mMpris2ServiceName + QLatin1String(".instance") + QString::number(getpid()); 0087 serviceRegistered = QDBusConnection::sessionBus().registerService(mMpris2ServiceName); 0088 } 0089 if (!serviceRegistered) { 0090 mMpris2ServiceName.clear(); 0091 sessionBus.unregisterObject(mediaPlayer2ObjectPath()); 0092 } 0093 } 0094 } 0095 0096 void Mpris2Service::unregisterOnDBus() 0097 { 0098 if (mMpris2ServiceName.isEmpty()) { 0099 return; 0100 } 0101 0102 QDBusConnection sessionBus = QDBusConnection::sessionBus(); 0103 sessionBus.unregisterService(mMpris2ServiceName); 0104 sessionBus.unregisterObject(mediaPlayer2ObjectPath()); 0105 } 0106 0107 void Mpris2Service::onLockScreenLockedChanged(bool isLocked) 0108 { 0109 if (isLocked) { 0110 unregisterOnDBus(); 0111 } else { 0112 registerOnDBus(); 0113 } 0114 } 0115 0116 void Mpris2Service::onFullScreenActionToggled() 0117 { 0118 if (m_fullscreenAction->isChecked()) { 0119 registerOnDBus(); 0120 } else { 0121 unregisterOnDBus(); 0122 } 0123 } 0124 } 0125 0126 #include "moc_mpris2service.cpp"