File indexing completed on 2024-05-12 09:41:26

0001 /*
0002  *   SPDX-FileCopyrightText: 2010 Dario Freddi <drf@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "powerdevilaction.h"
0008 
0009 #include "powerdevil_debug.h"
0010 #include "powerdevilcore.h"
0011 
0012 #include <QDebug>
0013 
0014 namespace PowerDevil
0015 {
0016 
0017 Action::Action(QObject *parent)
0018     : QObject(parent)
0019 {
0020     m_core = qobject_cast<PowerDevil::Core *>(parent);
0021 }
0022 
0023 Action::~Action()
0024 {
0025 }
0026 
0027 void Action::registerIdleTimeout(std::chrono::milliseconds timeout)
0028 {
0029     m_registeredIdleTimeouts.append(timeout);
0030     m_core->registerActionTimeout(this, timeout);
0031 }
0032 
0033 void Action::unregisterIdleTimeouts()
0034 {
0035     // Remove all registered idle timeouts, if any
0036     m_core->unregisterActionTimeouts(this);
0037     m_registeredIdleTimeouts.clear();
0038 }
0039 
0040 void Action::unloadAction()
0041 {
0042     unregisterIdleTimeouts();
0043 }
0044 
0045 bool Action::isSupported()
0046 {
0047     return true;
0048 }
0049 
0050 BackendInterface *Action::backend() const
0051 {
0052     return m_core->backend();
0053 }
0054 
0055 Core *Action::core()
0056 {
0057     return m_core;
0058 }
0059 
0060 void Action::trigger(const QVariantMap &args)
0061 {
0062     if (args.contains(QStringLiteral("Explicit")) && args[QStringLiteral("Explicit")].toBool()) {
0063         // The action was explicitly triggered by the user, hence any policy check is bypassed.
0064         triggerImpl(args);
0065     } else {
0066         // The action was taken automatically: let's check if we have the rights to do that
0067         PolicyAgent::RequiredPolicies unsatisfiablePolicies = PolicyAgent::instance()->requirePolicyCheck(m_requiredPolicies);
0068         if (unsatisfiablePolicies == PolicyAgent::None) {
0069             // Ok, let's trigger the action
0070             triggerImpl(args);
0071         } else {
0072             // TODO: Notify somehow?
0073             qCWarning(POWERDEVIL) << "Unsatisfied policies, the action has been aborted";
0074         }
0075     }
0076 }
0077 
0078 void Action::setRequiredPolicies(PolicyAgent::RequiredPolicies requiredPolicies)
0079 {
0080     m_requiredPolicies = requiredPolicies;
0081 }
0082 
0083 void Action::onIdleTimeout(std::chrono::milliseconds /*timeout*/)
0084 {
0085 }
0086 
0087 void Action::onWakeupFromIdle()
0088 {
0089 }
0090 
0091 void Action::onProfileLoad(const QString & /*previousProfile*/, const QString & /*newProfile*/)
0092 {
0093 }
0094 
0095 void Action::onProfileUnload()
0096 {
0097 }
0098 
0099 } // namespace PowerDevil
0100 
0101 #include "moc_powerdevilaction.cpp"