File indexing completed on 2024-12-15 05:06:15
0001 /* 0002 SPDX-FileCopyrightText: 2012 Alex Merry <alex.merry@kdemail.net> 0003 SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "mprisservice.h" 0009 0010 #include <KActionCollection> 0011 #include <KGlobalAccel> 0012 #include <KLocalizedString> 0013 #include <KPluginFactory> 0014 0015 #include "multiplexer.h" 0016 #include "playercontainer.h" 0017 0018 K_PLUGIN_CLASS_WITH_JSON(MprisService, "mprisservice.json") 0019 0020 MprisService::MprisService(QObject *parent, const QList<QVariant> &) 0021 : KDEDModule(parent) 0022 , m_multiplexer(Multiplexer::self()) 0023 , m_actionCollection(new KActionCollection(this, QStringLiteral("mediacontrol"))) 0024 { 0025 m_activePlayer.setBinding([this] { 0026 return m_multiplexer->activePlayer().value(); 0027 }); 0028 0029 enableGlobalShortcuts(); 0030 } 0031 0032 MprisService::~MprisService() 0033 { 0034 } 0035 0036 void MprisService::onPlayPause() 0037 { 0038 if (!m_activePlayer) { 0039 return; 0040 } 0041 0042 const auto playbackStatus = m_activePlayer->playbackStatus(); 0043 if (playbackStatus == PlaybackStatus::Playing) { 0044 if (m_activePlayer->canPause()) { 0045 m_activePlayer->Pause(); 0046 } 0047 } else { 0048 if (m_activePlayer->canPlay()) { 0049 m_activePlayer->Play(); 0050 } 0051 } 0052 } 0053 0054 void MprisService::onNext() 0055 { 0056 if (m_activePlayer && m_activePlayer->canGoNext()) { 0057 m_activePlayer->Next(); 0058 } 0059 } 0060 0061 void MprisService::onPrevious() 0062 { 0063 if (m_activePlayer && m_activePlayer->canGoPrevious()) { 0064 m_activePlayer->Previous(); 0065 } 0066 } 0067 0068 void MprisService::onStop() 0069 { 0070 if (m_activePlayer && m_activePlayer->canStop()) { 0071 m_activePlayer->Stop(); 0072 } 0073 } 0074 0075 void MprisService::onPause() 0076 { 0077 if (m_activePlayer && m_activePlayer->canPause()) { 0078 m_activePlayer->Pause(); 0079 } 0080 } 0081 0082 void MprisService::onPlay() 0083 { 0084 if (m_activePlayer && m_activePlayer->canPlay()) { 0085 m_activePlayer->Play(); 0086 } 0087 } 0088 0089 void MprisService::onVolumeUp() 0090 { 0091 if (m_activePlayer && m_activePlayer->canControl()) { 0092 m_activePlayer->changeVolume(0.05, true); 0093 } 0094 } 0095 0096 void MprisService::onVolumeDown() 0097 { 0098 if (m_activePlayer && m_activePlayer->canControl()) { 0099 m_activePlayer->changeVolume(-0.05, true); 0100 } 0101 } 0102 0103 void MprisService::enableGlobalShortcuts() 0104 { 0105 m_actionCollection->setComponentDisplayName(i18nc("@title Name for global shortcuts category", "Media Controller")); 0106 0107 auto addGlobalShortcut = [this](const QString &name, const QString &text, const QKeySequence &keySequence, void (MprisService::*callback)()) { 0108 QAction *action = m_actionCollection->addAction(name); 0109 action->setText(text); 0110 KGlobalAccel::setGlobalShortcut(action, keySequence); 0111 connect(action, &QAction::triggered, this, callback); 0112 }; 0113 0114 addGlobalShortcut(QStringLiteral("playpausemedia"), i18nc("@title shortcut", "Play/Pause media playback"), Qt::Key_MediaPlay, &MprisService::onPlayPause); 0115 addGlobalShortcut(QStringLiteral("nextmedia"), i18nc("@title shortcut", "Media playback next"), Qt::Key_MediaNext, &MprisService::onNext); 0116 addGlobalShortcut(QStringLiteral("previousmedia"), i18nc("@title shortcut", "Media playback previous"), Qt::Key_MediaPrevious, &MprisService::onPrevious); 0117 addGlobalShortcut(QStringLiteral("stopmedia"), i18nc("@title shortcut", "Stop media playback"), Qt::Key_MediaStop, &MprisService::onStop); 0118 addGlobalShortcut(QStringLiteral("pausemedia"), i18nc("@title shortcut", "Pause media playback"), Qt::Key_MediaPause, &MprisService::onPause); 0119 addGlobalShortcut(QStringLiteral("playmedia"), i18nc("@title shortcut", "Play media playback"), QKeySequence(), &MprisService::onPlay); 0120 addGlobalShortcut(QStringLiteral("mediavolumeup"), i18nc("@title shortcut", "Media volume up"), QKeySequence(), &MprisService::onVolumeUp); 0121 addGlobalShortcut(QStringLiteral("mediavolumedown"), i18nc("@title shortcut", "Media volume down"), QKeySequence(), &MprisService::onVolumeDown); 0122 } 0123 0124 #include "moc_mprisservice.cpp" 0125 #include "mprisservice.moc"