forked from neundorf/CuteCom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginmanager.cpp
134 lines (124 loc) · 4.66 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
126
127
128
129
130
131
132
133
134
/*
* 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());
if (item)
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, &MacroPlugin::unload, this, &PluginManager::removePlugin);
connect(macro, &MacroPlugin::sendCmd, this, &PluginManager::sendCmd);
/* 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, &NetProxyPlugin::unload, this, &PluginManager::removePlugin);
connect(proxy, &NetProxyPlugin::sendCmd, this, &PluginManager::sendCmd);
connect(this, &PluginManager::recvCmd, proxy, &NetProxyPlugin::proxyCmd);
/* common plugin initialization */
addPlugin((Plugin *)proxy->plugin());
} else if (type == en_plugin_type::PLUGIN_TYPE_BYTE_COUNTER) {
CounterPlugin *counter = new CounterPlugin(m_parent, m_settings);
connect(counter, &CounterPlugin::unload, this, &PluginManager::removePlugin);
connect(this, &PluginManager::recvCmd, counter, &CounterPlugin::rxBytes);
/* common plugin initialization */
addPlugin((Plugin *)counter->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();
m_list.removeOne(plugin);
plugin = NULL;
}
/**
* @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)) {
TRACE << "[PluginManager::processCmd]: " << cmd->toLatin1();
*cmd = new_cmd;
}
}
}
}