diff --git a/PowerDevilSettings.kcfg b/PowerDevilSettings.kcfg --- a/PowerDevilSettings.kcfg +++ b/PowerDevilSettings.kcfg @@ -10,6 +10,9 @@ true + + true + diff --git a/daemon/powerdevilcore.h b/daemon/powerdevilcore.h --- a/daemon/powerdevilcore.h +++ b/daemon/powerdevilcore.h @@ -154,6 +154,7 @@ void onNotificationTimeout(); void onServiceRegistered(const QString &service); void onLidClosedChanged(bool closed); + void onAboutToSuspend(); }; } diff --git a/daemon/powerdevilcore.cpp b/daemon/powerdevilcore.cpp --- a/daemon/powerdevilcore.cpp +++ b/daemon/powerdevilcore.cpp @@ -137,6 +137,8 @@ this, SLOT(onBatteryRemainingTimeChanged(qulonglong))); connect(m_backend, SIGNAL(lidClosedChanged(bool)), this, SLOT(onLidClosedChanged(bool))); + connect(m_backend, &BackendInterface::aboutToSuspend, + this, &Core::onAboutToSuspend); connect(KIdleTime::instance(), SIGNAL(timeoutReached(int,int)), this, SLOT(onKIdleTimeoutReached(int,int))); connect(KIdleTime::instance(), SIGNAL(resumingFromIdle()), @@ -757,6 +759,40 @@ Q_EMIT lidClosedChanged(closed); } +void Core::onAboutToSuspend() +{ + if (PowerDevilSettings::pausePlayersOnSuspend()) { + qCDebug(POWERDEVIL) << "Pausing all media players before suspending"; + + QDBusPendingCall listNamesCall = QDBusConnection::sessionBus().interface()->asyncCall(QStringLiteral("ListNames")); + QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(listNamesCall, this); + connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) { + QDBusPendingReply reply = *watcher; + watcher->deleteLater(); + + if (reply.isError()) { + qCWarning(POWERDEVIL) << "Failed to fetch list of DBus service names for pausing players on suspend" << reply.error().message(); + return; + } + + const QStringList &services = reply.value(); + for (const QString &serviceName : services) { + if (!serviceName.startsWith(QLatin1String("org.mpris.MediaPlayer2."))) { + continue; + } + + qCDebug(POWERDEVIL) << "Pausing media player with service name" << serviceName; + + QDBusMessage pauseMsg = QDBusMessage::createMethodCall(serviceName, + QStringLiteral("/org/mpris/MediaPlayer2"), + QStringLiteral("org.mpris.MediaPlayer2.Player"), + QStringLiteral("Pause")); + QDBusConnection::sessionBus().asyncCall(pauseMsg); + } + }); + } +} + void Core::registerActionTimeout(Action* action, int timeout) { // Register the timeout with KIdleTime diff --git a/kcmodule/global/GeneralPage.cpp b/kcmodule/global/GeneralPage.cpp --- a/kcmodule/global/GeneralPage.cpp +++ b/kcmodule/global/GeneralPage.cpp @@ -124,6 +124,8 @@ connect(BatteryCriticalCombo, SIGNAL(currentIndexChanged(int)), SLOT(changed())); + connect(pausePlayersCheckBox, &QCheckBox::stateChanged, this, static_cast(&KCModule::changed)); + if (!hasPowerSupplyBattery) { BatteryCriticalLabel->hide(); BatteryCriticalCombo->hide(); @@ -150,6 +152,8 @@ lowPeripheralSpin->setValue(PowerDevilSettings::peripheralBatteryLowLevel()); BatteryCriticalCombo->setCurrentIndex(BatteryCriticalCombo->findData(PowerDevilSettings::batteryCriticalAction())); + + pausePlayersCheckBox->setChecked(PowerDevilSettings::pausePlayersOnSuspend()); } void GeneralPage::configureNotifications() @@ -165,6 +169,8 @@ PowerDevilSettings::setBatteryCriticalAction(BatteryCriticalCombo->itemData(BatteryCriticalCombo->currentIndex()).toInt()); + PowerDevilSettings::setPausePlayersOnSuspend(pausePlayersCheckBox->checkState() == Qt::Checked); + PowerDevilSettings::self()->save(); // Notify Daemon diff --git a/kcmodule/global/generalPage.ui b/kcmodule/global/generalPage.ui --- a/kcmodule/global/generalPage.ui +++ b/kcmodule/global/generalPage.ui @@ -6,7 +6,7 @@ 0 0 - 494 + 510 276 @@ -113,7 +113,7 @@ - + Qt::Vertical @@ -126,7 +126,7 @@ - + @@ -162,6 +162,20 @@ + + + + Enabled + + + + + + + Pause media players when suspending: + + +