forked from neundorf/CuteCom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginmanager.cpp
125 lines (115 loc) · 4.22 KB
/
pluginmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 208 Dimitris Tassopoulos <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
*/
#include "pluginmanager.h"
#define TRACE \
if (!debug) { \
} else \
qDebug()
static bool debug = false;
PluginManager::PluginManager(QFrame *parent, QVBoxLayout *layout, Settings *settings)
: m_parent(parent)
, m_layout(layout)
, m_settings(settings)
{
}
PluginManager::~PluginManager()
{
QListIterator<Plugin *> i(m_list);
while (i.hasNext()) {
Plugin *item = static_cast<Plugin *>(i.next());
removePlugin(item);
}
}
/**
* @brief [SLOT] Add a new plugin and initialize it depending its type
* @param type Supported plugin type (see: en_plugin_type)
*/
void PluginManager::addPluginType(en_plugin_type type)
{
TRACE << "[PluginManager] Adding new plugin: " << type;
if (type == en_plugin_type::PLUGIN_TYPE_MACROS) {
/* specific plugin initialization */
MacroPlugin *macro = new MacroPlugin(m_parent, m_settings);
connect(macro, SIGNAL(unload(Plugin *)), this, SLOT(removePlugin(Plugin *)));
connect(macro, SIGNAL(sendCmd(QByteArray)), this, SIGNAL(sendCmd(QByteArray)));
/* common plugin initialization */
addPlugin((Plugin *)macro->plugin());
} else if (type == en_plugin_type::PLUGIN_TYPE_NET_PROXY) {
NetProxyPlugin *proxy = new NetProxyPlugin(m_parent, m_settings);
connect(proxy, SIGNAL(unload(Plugin *)), this, SLOT(removePlugin(Plugin *)));
connect(proxy, SIGNAL(sendCmd(QByteArray)), this, SIGNAL(sendCmd(QByteArray)));
connect(this, SIGNAL(recvCmd(QByteArray)), proxy, SIGNAL(proxyCmd(QByteArray)));
/* common plugin initialization */
addPlugin((Plugin *)proxy->plugin());
}
}
/**
* @brief [SLOT] Remove an existing plugin
* @param plugin A pointer to the plugin to delete
*/
void PluginManager::removePlugin(Plugin *plugin)
{
TRACE << "[PluginManager] Removing plugin: " << plugin->name;
if (!plugin)
return;
if (plugin->frame) {
plugin->frame->close();
}
plugin->deleteLater();
}
/**
* @brief Adds a plugin in the manager list and also does the
* additional initialization
* @param item The plugin to add
*/
void PluginManager::addPlugin(Plugin *item)
{
if (!item)
return;
m_list.append(item);
/* if the plugin has also a frame then add it */
if (item->frame) {
QMargins mainMargins = m_layout->contentsMargins();
item->frame->setContentsMargins(mainMargins);
m_layout->addWidget(item->frame);
item->frame->show();
}
TRACE << "[PluginManager] Added new plugin: " << item->name;
}
/**
* @brief Inject and process the cmd data before they sent
* @param cmd The data
*/
void PluginManager::processCmd(QString *cmd)
{
TRACE << "[PluginManager] process: " << cmd;
QListIterator<Plugin *> i(m_list);
while (i.hasNext()) {
const Plugin *item = static_cast<const Plugin *>(i.next());
if (item->processCmd) {
QString new_cmd;
if (item->processCmd(cmd, &new_cmd)) {
// new_cmd = cmd + QString("_ADD");
*cmd = new_cmd;
}
}
}
}