File indexing completed on 2024-04-28 16:55:13

0001 /***************************************************************************
0002  *   Copyright (C) 2010 by Dario Freddi <drf@kde.org>                      *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "powerdevilaction.h"
0021 
0022 #include "powerdevilcore.h"
0023 #include "powerdevil_debug.h"
0024 
0025 #include <QDebug>
0026 
0027 namespace PowerDevil
0028 {
0029 
0030 class Action::Private
0031 {
0032 public:
0033     Private() {}
0034     ~Private() {}
0035 
0036     PowerDevil::Core *core;
0037 
0038     QVector< int > registeredIdleTimeouts;
0039     PowerDevil::PolicyAgent::RequiredPolicies requiredPolicies;
0040 };
0041 
0042 Action::Action(QObject* parent)
0043         : QObject(parent)
0044         , d(new Private)
0045 {
0046     d->core = qobject_cast<PowerDevil::Core*>(parent);
0047 }
0048 
0049 Action::~Action()
0050 {
0051     delete d;
0052 }
0053 
0054 void Action::registerIdleTimeout(int msec)
0055 {
0056     d->registeredIdleTimeouts.append(msec);
0057     d->core->registerActionTimeout(this, msec);
0058 }
0059 
0060 bool Action::unloadAction()
0061 {
0062     // Remove all registered idle timeouts, if any
0063     d->core->unregisterActionTimeouts(this);
0064     d->registeredIdleTimeouts.clear();
0065 
0066     // Ok, let's see if the action has to do something for being unloaded
0067     return onUnloadAction();
0068 }
0069 
0070 bool Action::onUnloadAction()
0071 {
0072     // Usually nothing has to be done, so let's just happily return true
0073     return true;
0074 }
0075 
0076 bool Action::isSupported()
0077 {
0078     return true;
0079 }
0080 
0081 BackendInterface* Action::backend() const
0082 {
0083     return d->core->backend();
0084 }
0085 
0086 Core* Action::core()
0087 {
0088     return d->core;
0089 }
0090 
0091 void Action::trigger(const QVariantMap& args)
0092 {
0093     if (args.contains(QStringLiteral("Explicit")) && args[QStringLiteral("Explicit")].toBool()) {
0094         // The action was explicitly triggered by the user, hence any policy check is bypassed.
0095         triggerImpl(args);
0096     } else {
0097         // The action was taken automatically: let's check if we have the rights to do that
0098         PolicyAgent::RequiredPolicies unsatisfiablePolicies = PolicyAgent::instance()->requirePolicyCheck(d->requiredPolicies);
0099         if (unsatisfiablePolicies == PolicyAgent::None) {
0100             // Ok, let's trigger the action
0101             triggerImpl(args);
0102         } else {
0103             // TODO: Notify somehow?
0104             qCWarning(POWERDEVIL) << "Unsatisfied policies, the action has been aborted";
0105         }
0106     }
0107 }
0108 
0109 void Action::setRequiredPolicies(PolicyAgent::RequiredPolicies requiredPolicies)
0110 {
0111     d->requiredPolicies = requiredPolicies;
0112 }
0113 
0114 }