diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ endif() find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Widgets DBus X11Extras) -find_package(KF5 REQUIRED COMPONENTS Activities Auth IdleTime Config DBusAddons Solid I18n GlobalAccel KIO NotifyConfig Screen KDELibs4Support Wayland) +find_package(KF5 REQUIRED COMPONENTS Activities Auth IdleTime Config DBusAddons Solid I18n GlobalAccel KIO NotifyConfig Screen Wayland DocTools Crash Notifications) find_package(LibKWorkspace CONFIG REQUIRED) find_package(KF5BluezQt) diff --git a/daemon/CMakeLists.txt b/daemon/CMakeLists.txt --- a/daemon/CMakeLists.txt +++ b/daemon/CMakeLists.txt @@ -39,6 +39,7 @@ powerdevilbrightnesslogic.cpp powerdevilscreenbrightnesslogic.cpp powerdevilkeyboardbrightnesslogic.cpp + powerdevilpowermanagement.cpp brightnessosdwidget.cpp kwinkscreenhelpereffect.cpp ) @@ -106,8 +107,8 @@ powerdevilpolicyagent.h PowerDevil::PolicyAgent powermanagementpolicyagentadaptor PowerManagementPolicyAgentAdaptor) -qt5_add_dbus_adaptor(powerdevil_SRCS ${KDE4_DBUS_INTERFACES_DIR}/kf5_org.freedesktop.PowerManagement.xml powerdevilfdoconnector.h PowerDevil::FdoConnector powermanagementfdoadaptor PowerManagementFdoAdaptor) -qt5_add_dbus_adaptor(powerdevil_SRCS ${KDE4_DBUS_INTERFACES_DIR}/kf5_org.freedesktop.PowerManagement.Inhibit.xml powerdevilfdoconnector.h PowerDevil::FdoConnector powermanagementinhibitadaptor PowerManagementInhibitAdaptor) +qt5_add_dbus_adaptor(powerdevil_SRCS ${SOLID_DBUS_INTERFACES_DIR}/kf5_org.freedesktop.PowerManagement.xml powerdevilfdoconnector.h PowerDevil::FdoConnector powermanagementfdoadaptor PowerManagementFdoAdaptor) +qt5_add_dbus_adaptor(powerdevil_SRCS ${SOLID_DBUS_INTERFACES_DIR}/kf5_org.freedesktop.PowerManagement.Inhibit.xml powerdevilfdoconnector.h PowerDevil::FdoConnector powermanagementinhibitadaptor PowerManagementInhibitAdaptor) # Backends diff --git a/daemon/actions/bundled/CMakeLists.txt b/daemon/actions/bundled/CMakeLists.txt --- a/daemon/actions/bundled/CMakeLists.txt +++ b/daemon/actions/bundled/CMakeLists.txt @@ -3,6 +3,7 @@ add_library(powerdevil${_name}action_config MODULE ${actionconfig_SRCS}) target_link_libraries(powerdevil${_name}action_config + powerdevilcore powerdevilui Qt5::Widgets KF5::CoreAddons @@ -18,7 +19,7 @@ add_powerdevil_bundled_action(keyboardbrightnesscontrol) add_powerdevil_bundled_action(dimdisplay) add_powerdevil_bundled_action(runscript KF5::KIOCore KF5::KIOWidgets) -add_powerdevil_bundled_action(suspendsession KF5::KIOCore KF5::KIOWidgets KF5::Solid KF5::KDELibs4Support) +add_powerdevil_bundled_action(suspendsession KF5::KIOCore KF5::KIOWidgets) if(HAVE_WIRELESS_SUPPORT) add_powerdevil_bundled_action(wirelesspowersaving KF5::NetworkManagerQt KF5::BluezQt) endif() @@ -30,14 +31,13 @@ add_library(powerdevilhandlebuttoneventsaction_config MODULE ${actionconfig_SRCS}) target_link_libraries(powerdevilhandlebuttoneventsaction_config + powerdevilcore powerdevilui Qt5::Widgets KF5::CoreAddons KF5::I18n KF5::ConfigGui - KF5::Solid KF5::XmlGui - KF5::KDELibs4Support ) install(TARGETS powerdevilhandlebuttoneventsaction_config DESTINATION ${PLUGIN_INSTALL_DIR}) install(FILES powerdevilhandlebuttoneventsaction.desktop DESTINATION ${SERVICES_INSTALL_DIR}) diff --git a/daemon/actions/bundled/handlebuttoneventsconfig.cpp b/daemon/actions/bundled/handlebuttoneventsconfig.cpp --- a/daemon/actions/bundled/handlebuttoneventsconfig.cpp +++ b/daemon/actions/bundled/handlebuttoneventsconfig.cpp @@ -23,8 +23,7 @@ #include "suspendsession.h" #include "upower_interface.h" -#include -#include +#include "powerdevilpowermanagement.h" #include #include @@ -94,19 +93,17 @@ QList boxes; boxes << m_lidCloseCombo << m_powerButtonCombo; - QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates(); - Q_FOREACH (QComboBox *box, boxes) { box->addItem(QIcon::fromTheme("dialog-cancel"), i18n("Do nothing"), (uint)SuspendSession::None); - if (methods.contains(Solid::PowerManagement::SuspendState)) { + if (PowerManagement::instance()->canSuspend()) { box->addItem(QIcon::fromTheme("system-suspend"), i18n("Suspend"), (uint)SuspendSession::ToRamMode); } - if (methods.contains(Solid::PowerManagement::HibernateState)) { + if (PowerManagement::instance()->canHibernate()) { box->addItem(QIcon::fromTheme("system-suspend-hibernate"), i18n("Hibernate"), (uint)SuspendSession::ToDiskMode); } - if (methods.contains(Solid::PowerManagement::HybridSuspendState)) { - box->addItem(QIcon::fromTheme("system-suspend-hybrid"), i18n("Hybrid suspend"), (uint)SuspendSession::SuspendHybridMode); - } +// if (methods.contains(Solid::PowerManagement::HybridSuspendState)) { +// box->addItem(QIcon::fromTheme("system-suspend-hybrid"), i18n("Hybrid suspend"), (uint)SuspendSession::SuspendHybridMode); +// } box->addItem(QIcon::fromTheme("system-shutdown"), i18n("Shut down"), (uint)SuspendSession::ShutdownMode); box->addItem(QIcon::fromTheme("system-lock-screen"), i18n("Lock screen"), (uint)SuspendSession::LockScreenMode); if (box != m_lidCloseCombo) { diff --git a/daemon/actions/bundled/suspendsessionconfig.cpp b/daemon/actions/bundled/suspendsessionconfig.cpp --- a/daemon/actions/bundled/suspendsessionconfig.cpp +++ b/daemon/actions/bundled/suspendsessionconfig.cpp @@ -20,11 +20,11 @@ #include "suspendsessionconfig.h" +#include "powerdevilpowermanagement.h" + #include #include -#include - #include #include #include @@ -78,17 +78,15 @@ m_idleTime->setValue(0); m_idleTime->setSuffix(i18n(" min")); - QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates(); - - if (methods.contains(Solid::PowerManagement::SuspendState)) { + if (PowerManagement::instance()->canSuspend()) { m_comboBox->addItem(QIcon::fromTheme("system-suspend"), i18n("Suspend"), (uint)SuspendSession::ToRamMode); } - if (methods.contains(Solid::PowerManagement::HibernateState)) { + if (PowerManagement::instance()->canHibernate()) { m_comboBox->addItem(QIcon::fromTheme("system-suspend-hibernate"), i18n("Hibernate"), (uint)SuspendSession::ToDiskMode); } - if (methods.contains(Solid::PowerManagement::HybridSuspendState)) { - m_comboBox->addItem(QIcon::fromTheme("system-suspend-hybrid"), i18n("Hybrid suspend"), (uint)SuspendSession::SuspendHybridMode); - } +// if (methods.contains(Solid::PowerManagement::HybridSuspendState)) { +// m_comboBox->addItem(QIcon::fromTheme("system-suspend-hybrid"), i18n("Hybrid suspend"), (uint)SuspendSession::SuspendHybridMode); +// } m_comboBox->addItem(QIcon::fromTheme("system-shutdown"), i18n("Shut down"), (uint)SuspendSession::ShutdownMode); m_comboBox->addItem(QIcon::fromTheme("system-lock-screen"), i18n("Lock screen"), (uint)SuspendSession::LockScreenMode); diff --git a/daemon/powerdevilpowermanagement.h b/daemon/powerdevilpowermanagement.h new file mode 100644 --- /dev/null +++ b/daemon/powerdevilpowermanagement.h @@ -0,0 +1,59 @@ +/******************************************************************** +Copyright 2016 Martin Gräßlin + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) version 3, or any +later version accepted by the membership of KDE e.V. (or its +successor approved by the membership of KDE e.V.), which shall +act as a proxy defined in Section 6 of version 3 of the license. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library. If not, see . +*********************************************************************/ + +#ifndef POWERDEVIL_POWERMANAGEMENT_H +#define POWERDEVIL_POWERMANAGEMENT_H + +#include + +namespace PowerDevil { + +class Q_DECL_EXPORT PowerManagement : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool canSuspend READ canSuspend NOTIFY canSuspendChanged) + Q_PROPERTY(bool canHibernate READ canHibernate NOTIFY canHibernateChanged) +public: + virtual ~PowerManagement(); + + bool canSuspend() const; + bool canHibernate() const; + + static PowerManagement *instance(); + +public Q_SLOTS: + void suspend(); + void hibernate(); + +Q_SIGNALS: + void canSuspendChanged(); + void canHibernateChanged(); + +protected: + explicit PowerManagement(); + +private: + class Private; + QScopedPointer d; +}; + +} // namespace PowerDevil + +#endif // POWERDEVIL_POWERMANAGEMENT_H diff --git a/daemon/powerdevilpowermanagement.cpp b/daemon/powerdevilpowermanagement.cpp new file mode 100644 --- /dev/null +++ b/daemon/powerdevilpowermanagement.cpp @@ -0,0 +1,198 @@ +/******************************************************************** +Copyright 2016 Martin Gräßlin + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) version 3, or any +later version accepted by the membership of KDE e.V. (or its +successor approved by the membership of KDE e.V.), which shall +act as a proxy defined in Section 6 of version 3 of the license. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library. If not, see . +*********************************************************************/ + +#include "powerdevilpowermanagement.h" + +#include +#include +#include + +namespace PowerDevil { + +static const QString s_fdoPowerService = QStringLiteral("org.freedesktop.PowerManagement"); +static const QString s_fdoPowerPath = QStringLiteral("/org/freedesktop/PowerManagement"); + +class PowerManagementInstance : public PowerManagement +{ + Q_OBJECT +public: + explicit PowerManagementInstance() : PowerManagement() {} +}; +Q_GLOBAL_STATIC(PowerManagementInstance, s_instance) + +class PowerManagement::Private +{ +public: + Private(PowerManagement *q); + void update(); + void setCanSuspend(bool set); + void setCanHibernate(bool set); + + bool serviceRegistered; + bool canSuspend; + bool canHibernate; + QScopedPointer fdoPowerServiceWatcher; + +private: + void updateProperty(const QString &dbusName, void (Private::*setter)(bool)); + PowerManagement *q; +}; + +PowerManagement::Private::Private(PowerManagement *q) + : serviceRegistered(false) + , canSuspend(false) + , canHibernate(false) + , fdoPowerServiceWatcher(new QDBusServiceWatcher(s_fdoPowerService, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForUnregistration | QDBusServiceWatcher::WatchForRegistration)) + , q(q) +{ +} + +void PowerManagement::Private::update() +{ + serviceRegistered = true; + updateProperty(QStringLiteral("CanSuspend"), &Private::setCanSuspend); + updateProperty(QStringLiteral("CanHibernate"), &Private::setCanHibernate); +} + +void PowerManagement::Private::updateProperty(const QString &dbusName, void (Private::*setter)(bool)) +{ + QDBusMessage message = QDBusMessage::createMethodCall(s_fdoPowerService, + s_fdoPowerPath, + s_fdoPowerService, + dbusName); + QDBusPendingReply reply = QDBusConnection::sessionBus().asyncCall(message); + QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(reply, q); + QObject::connect(callWatcher, &QDBusPendingCallWatcher::finished, q, + [this, setter](QDBusPendingCallWatcher *self) { + QDBusPendingReply reply = *self; + self->deleteLater(); + if (!reply.isValid()) { + return; + } + ((this)->*setter)(reply.value()); + } + ); + +} + +void PowerManagement::Private::setCanHibernate(bool set) +{ + if (canHibernate == set) { + return; + } + canHibernate = set; + q->canHibernateChanged(); +} + +void PowerManagement::Private::setCanSuspend(bool set) +{ + if (canSuspend == set) { + return; + } + canSuspend = set; + q->canSuspendChanged(); +} + +PowerManagement *PowerManagement::instance() +{ + return s_instance; +} + +PowerManagement::PowerManagement() + : QObject() + , d(new Private(this)) +{ + connect(d->fdoPowerServiceWatcher.data(), &QDBusServiceWatcher::serviceRegistered, this, [this] { d->update(); }); + connect(d->fdoPowerServiceWatcher.data(), &QDBusServiceWatcher::serviceUnregistered, this, + [this] { + d->serviceRegistered = false; + d->setCanSuspend(false); + d->setCanHibernate(false); + } + ); + + // check whether the service is registered + QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DBus"), + QStringLiteral("/"), + QStringLiteral("org.freedesktop.DBus"), + QStringLiteral("ListNames")); + QDBusPendingReply async = QDBusConnection::sessionBus().asyncCall(message); + QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(async, this); + connect(callWatcher, &QDBusPendingCallWatcher::finished, this, + [this](QDBusPendingCallWatcher *self) { + QDBusPendingReply reply = *self; + self->deleteLater(); + if (!reply.isValid()) { + return; + } + if (reply.value().contains(s_fdoPowerService)) { + d->update(); + } + } + ); +} + +PowerManagement::~PowerManagement() +{ +} + +void PowerManagement::suspend() +{ + if (!d->serviceRegistered) { + return; + } + if (!d->canSuspend) { + return; + } + QDBusMessage message = QDBusMessage::createMethodCall(s_fdoPowerService, + s_fdoPowerPath, + s_fdoPowerService, + QStringLiteral("Suspend")); + QDBusConnection::sessionBus().asyncCall(message); +} + +void PowerManagement::hibernate() +{ + if (!d->serviceRegistered) { + return; + } + if (!d->canHibernate) { + return; + } + QDBusMessage message = QDBusMessage::createMethodCall(s_fdoPowerService, + s_fdoPowerPath, + s_fdoPowerService, + QStringLiteral("Hibernate")); + QDBusConnection::sessionBus().asyncCall(message); +} + +bool PowerManagement::canSuspend() const +{ + return d->canSuspend; +} + +bool PowerManagement::canHibernate() const +{ + return d->canHibernate; +} + +} // namespace PowerDevil + +#include "powerdevilpowermanagement.moc" diff --git a/kcmodule/activities/CMakeLists.txt b/kcmodule/activities/CMakeLists.txt --- a/kcmodule/activities/CMakeLists.txt +++ b/kcmodule/activities/CMakeLists.txt @@ -15,9 +15,7 @@ add_library(kcm_powerdevilactivitiesconfig MODULE ${kcm_powerdevil_activities_SRCS}) target_link_libraries(kcm_powerdevilactivitiesconfig - KF5::Solid KF5::Activities - KF5::KDELibs4Support powerdevilconfigcommonprivate ) diff --git a/kcmodule/activities/activitywidget.cpp b/kcmodule/activities/activitywidget.cpp --- a/kcmodule/activities/activitywidget.cpp +++ b/kcmodule/activities/activitywidget.cpp @@ -24,9 +24,10 @@ #include "../daemon/actions/bundled/suspendsession.h" +#include "powerdevilpowermanagement.h" + #include #include -#include #include #include #include @@ -91,13 +92,11 @@ using namespace PowerDevil::BundledActions; - QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates(); - - if (methods.contains(Solid::PowerManagement::SuspendState)) { + if (PowerDevil::PowerManagement::instance()->canSuspend()) { m_ui->alwaysActionBox->addItem(QIcon::fromTheme("system-suspend"), i18n("Suspend"), (uint)SuspendSession::ToRamMode); } - if (methods.contains(Solid::PowerManagement::HibernateState)) { + if (PowerDevil::PowerManagement::instance()->canHibernate()) { m_ui->alwaysActionBox->addItem(QIcon::fromTheme("system-suspend-hibernate"), i18n("Hibernate"), (uint)SuspendSession::ToDiskMode); } diff --git a/kcmodule/global/CMakeLists.txt b/kcmodule/global/CMakeLists.txt --- a/kcmodule/global/CMakeLists.txt +++ b/kcmodule/global/CMakeLists.txt @@ -14,9 +14,7 @@ target_link_libraries(kcm_powerdevilglobalconfig KF5::ConfigWidgets KF5::KIOWidgets - KF5::Solid KF5::NotifyConfig - KF5::KDELibs4Support powerdevilconfigcommonprivate ) diff --git a/kcmodule/global/GeneralPage.cpp b/kcmodule/global/GeneralPage.cpp --- a/kcmodule/global/GeneralPage.cpp +++ b/kcmodule/global/GeneralPage.cpp @@ -24,10 +24,11 @@ #include "actions/bundled/suspendsession.h" +#include "powerdevilpowermanagement.h" + #include #include #include -#include #include #include @@ -101,13 +102,11 @@ } } - QSet< Solid::PowerManagement::SleepState > methods = Solid::PowerManagement::supportedSleepStates(); - BatteryCriticalCombo->addItem(QIcon::fromTheme("dialog-cancel"), i18n("Do nothing"), PowerDevil::BundledActions::SuspendSession::None); - if (methods.contains(Solid::PowerManagement::SuspendState)) { + if (PowerDevil::PowerManagement::instance()->canSuspend()) { BatteryCriticalCombo->addItem(QIcon::fromTheme("system-suspend"), i18n("Suspend"), PowerDevil::BundledActions::SuspendSession::ToRamMode); } - if (methods.contains(Solid::PowerManagement::HibernateState)) { + if (PowerDevil::PowerManagement::instance()->canHibernate()) { BatteryCriticalCombo->addItem(QIcon::fromTheme("system-suspend-hibernate"), i18n("Hibernate"), PowerDevil::BundledActions::SuspendSession::ToDiskMode); } BatteryCriticalCombo->addItem(QIcon::fromTheme("system-shutdown"), i18n("Shut down"), PowerDevil::BundledActions::SuspendSession::ShutdownMode); diff --git a/kcmodule/profiles/CMakeLists.txt b/kcmodule/profiles/CMakeLists.txt --- a/kcmodule/profiles/CMakeLists.txt +++ b/kcmodule/profiles/CMakeLists.txt @@ -15,10 +15,8 @@ target_link_libraries(kcm_powerdevilprofilesconfig KF5::KIOWidgets - KF5::Solid KF5::ConfigGui KF5::ConfigWidgets - KF5::KDELibs4Support powerdevilui powerdevilconfigcommonprivate ) diff --git a/kcmodule/profiles/EditPage.cpp b/kcmodule/profiles/EditPage.cpp --- a/kcmodule/profiles/EditPage.cpp +++ b/kcmodule/profiles/EditPage.cpp @@ -24,6 +24,7 @@ #include #include +#include #include @@ -50,7 +51,6 @@ #include #include -#include K_PLUGIN_FACTORY(PowerDevilProfilesKCMFactory, registerPlugin(); @@ -79,10 +79,9 @@ if (m_profilesConfig->groupList().isEmpty()) { // Use the generator - QSet methods = Solid::PowerManagement::supportedSleepStates(); PowerDevil::ProfileGenerator::generateProfiles( - methods.contains(Solid::PowerManagement::SuspendState), - methods.contains(Solid::PowerManagement::HibernateState) + PowerDevil::PowerManagement::instance()->canSuspend(), + PowerDevil::PowerManagement::instance()->canHibernate() ); m_profilesConfig->reparseConfiguration(); } @@ -195,10 +194,9 @@ "Are you sure you want to continue?"), i18n("Restore Default Profiles")); if (ret == KMessageBox::Continue) { qCDebug(POWERDEVIL) << "Restoring defaults."; - QSet methods = Solid::PowerManagement::supportedSleepStates(); PowerDevil::ProfileGenerator::generateProfiles( - methods.contains(Solid::PowerManagement::SuspendState), - methods.contains(Solid::PowerManagement::HibernateState) + PowerDevil::PowerManagement::instance()->canSuspend(), + PowerDevil::PowerManagement::instance()->canHibernate() ); load();