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 0021 #include "powerdevilactionpool.h" 0022 0023 #include "powerdevilaction.h" 0024 #include "powerdevilcore.h" 0025 #include "powerdevil_debug.h" 0026 0027 #include <config-powerdevil.h> 0028 0029 #include <KConfigGroup> 0030 0031 #include <KPluginFactory> 0032 #include <QDBusConnection> 0033 #include <QDebug> 0034 0035 namespace PowerDevil 0036 { 0037 0038 class ActionPoolHelper 0039 { 0040 public: 0041 ActionPoolHelper() : q(nullptr) {} 0042 ~ActionPoolHelper() { 0043 delete q; 0044 } 0045 ActionPool *q; 0046 }; 0047 0048 Q_GLOBAL_STATIC(ActionPoolHelper, s_globalActionPool) 0049 0050 ActionPool *ActionPool::instance() 0051 { 0052 if (!s_globalActionPool->q) { 0053 new ActionPool; 0054 } 0055 0056 return s_globalActionPool->q; 0057 } 0058 0059 ActionPool::ActionPool() 0060 { 0061 Q_ASSERT(!s_globalActionPool->q); 0062 s_globalActionPool->q = this; 0063 } 0064 0065 ActionPool::~ActionPool() 0066 { 0067 clearCache(); 0068 } 0069 0070 void ActionPool::clearCache() 0071 { 0072 QHash< QString, Action* >::iterator i = m_actionPool.begin(); 0073 while (i != m_actionPool.end()) { 0074 // Delete the associated action and erase 0075 i.value()->deleteLater(); 0076 i = m_actionPool.erase(i); 0077 } 0078 } 0079 0080 void ActionPool::init(PowerDevil::Core *parent) 0081 { 0082 const QVector<KPluginMetaData> offers = KPluginMetaData::findPlugins(QStringLiteral("powerdevil/action")); 0083 for (const KPluginMetaData &data : offers) { 0084 if (auto plugin = KPluginFactory::instantiatePlugin<PowerDevil::Action>(data, parent).plugin) { 0085 m_actionPool.insert(data.value(QStringLiteral("X-KDE-PowerDevil-Action-ID")), plugin); 0086 } 0087 } 0088 0089 // Verify support 0090 QHash<QString,Action*>::iterator i = m_actionPool.begin(); 0091 while (i != m_actionPool.end()) { 0092 Action *action = i.value(); 0093 if (!action->isSupported()) { 0094 i = m_actionPool.erase(i); 0095 action->deleteLater(); 0096 } else { 0097 ++i; 0098 } 0099 } 0100 0101 // Register DBus objects 0102 for (const KPluginMetaData &offer : offers) { 0103 QString actionId = offer.value(QStringLiteral("X-KDE-PowerDevil-Action-ID")); 0104 if (offer.value(QStringLiteral("X-KDE-PowerDevil-Action-RegistersDBusInterface"), false) && m_actionPool.contains(actionId)) { 0105 QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/kde/Solid/PowerManagement/Actions/") + actionId, m_actionPool[actionId]); 0106 } 0107 } 0108 } 0109 0110 Action* ActionPool::loadAction(const QString& actionId, const KConfigGroup& group, PowerDevil::Core *parent) 0111 { 0112 Q_UNUSED(parent); 0113 // Let's retrieve the action 0114 if (m_actionPool.contains(actionId)) { 0115 Action *retaction = m_actionPool[actionId]; 0116 0117 if (group.isValid()) { 0118 0119 if (m_activeActions.contains(actionId)) { 0120 // We are reloading the action: let's unload it first then. 0121 retaction->onProfileUnload(); 0122 retaction->unloadAction(); 0123 m_activeActions.removeOne(actionId); 0124 } 0125 0126 retaction->loadAction(group); 0127 m_activeActions.append(actionId); 0128 } 0129 0130 return retaction; 0131 } else { 0132 // Hmm... troubles in configuration. Np, let's just return 0 and let the core handle this 0133 return nullptr; 0134 } 0135 } 0136 0137 void ActionPool::unloadAllActiveActions() 0138 { 0139 for (const QString &action : qAsConst(m_activeActions)) { 0140 m_actionPool[action]->onProfileUnload(); 0141 m_actionPool[action]->unloadAction(); 0142 } 0143 m_activeActions.clear(); 0144 } 0145 0146 }