MNE-CPP  beta 1.0
commandmanager.cpp
1 
2 //*************************************************************************************************************
3 //=============================================================================================================
4 // Includes
5 //=============================================================================================================
6 
7 #include "commandmanager.h"
8 #include "rawcommand.h"
9 
10 
11 //*************************************************************************************************************
12 //=============================================================================================================
13 // Qt Includes
14 //=============================================================================================================
15 
16 #include <QVector>
17 #include <QDebug>
18 #include <QJsonObject>
19 #include <QStringList>
20 
21 
22 //*************************************************************************************************************
23 //=============================================================================================================
24 // USED NAMESPACES
25 //=============================================================================================================
26 
27 using namespace RTCOMMANDLIB;
28 
29 
30 //*************************************************************************************************************
31 //=============================================================================================================
32 // DEFINE MEMBER METHODS
33 //=============================================================================================================
34 
35 CommandManager::CommandManager(bool p_bIsActive, QObject *parent)
36 : QObject(parent)
37 , m_bIsActive(p_bIsActive)
38 {
39  init();
40 }
41 
42 
43 //*************************************************************************************************************
44 
45 CommandManager::CommandManager(const QByteArray &p_qByteArrayJsonDoc, bool p_bIsActive, QObject *parent)
46 : QObject(parent)
47 , m_bIsActive(p_bIsActive)
48 {
49  init();
50 
51  m_jsonDocumentOrigin = QJsonDocument::fromJson(p_qByteArrayJsonDoc);
52 
53  insert(m_jsonDocumentOrigin);
54 }
55 
56 
57 //*************************************************************************************************************
58 
59 CommandManager::CommandManager(const QJsonDocument &p_jsonDoc, bool p_bIsActive, QObject *parent)
60 : QObject(parent)
61 , m_bIsActive(p_bIsActive)
62 , m_jsonDocumentOrigin(p_jsonDoc)
63 {
64  init();
65 
66  insert(m_jsonDocumentOrigin);
67 }
68 
69 
70 //*************************************************************************************************************
71 
72 CommandManager::~CommandManager()
73 {
74  //Disconnect all connections which are created with the help of this manager.
75 // this->disconnectAll();
76 
77  //Remove commands which where inserted into the static command list
78 
79 
80 }
81 
82 
83 //*************************************************************************************************************
84 
86 {
87  m_qMapCommands.clear();
88 }
89 
90 
91 //*************************************************************************************************************
92 
93 void CommandManager::init()
94 {
95 
96 }
97 
98 
99 //*************************************************************************************************************
100 //ToDo connect all commands inserted in this class by default.
101 void CommandManager::insert(const QJsonDocument &p_jsonDocument)
102 {
103  QJsonObject t_jsonObjectCommand;
104 
105  //Switch to command object
106  if(p_jsonDocument.isObject() && p_jsonDocument.object().value(QString("commands")) != QJsonValue::Undefined)
107  t_jsonObjectCommand = p_jsonDocument.object().value(QString("commands")).toObject();
108  else
109  return;
110 
111  QJsonObject::Iterator it;
112  for(it = t_jsonObjectCommand.begin(); it != t_jsonObjectCommand.end(); ++it)
113  {
114  if(!m_qMapCommands.contains(it.key()))
115  m_qMapCommands.insert(it.key(), Command(it.key(), it.value().toObject(), true, this));
116  else
117  qWarning("Warning: CommandMap contains command %s already. Insertion skipped.\n", it.key().toLatin1().constData());
118  }
119 
120  emit commandMapChanged();
121 }
122 
123 
124 //*************************************************************************************************************
125 
126 void CommandManager::insert(const QString &p_sKey, const QString &p_sDescription)
127 {
128  Command t_command(p_sKey, p_sDescription, false, this);
129  insert(p_sKey, t_command);
130 }
131 
132 
133 //*************************************************************************************************************
134 
135 void CommandManager::insert(const QString &p_sKey, const Command &p_command)
136 {
137  Command t_command(p_command);
138  t_command.setParent(this);
139  m_qMapCommands.insert(p_sKey, t_command);
140  emit commandMapChanged();
141 }
142 
143 
144 //*************************************************************************************************************
145 
147 {
148  // If Manager is not active do not parse commands
149  if(!m_bIsActive)
150  return;
151 
152  CommandParser* t_pCommandParser = static_cast<CommandParser*>(p_pSubject);
153 
154  RawCommand t_rawCommand(t_pCommandParser->getRawCommand());
155  QString t_sCommandName = t_rawCommand.command();
156 
157  if(!this->hasCommand(t_sCommandName))
158  return;
159 
160  // check if number of parameters is right
161  if(t_rawCommand.count() >= m_qMapCommands[t_sCommandName].count())
162  {
163  m_qMapCommands[t_sCommandName].isJson() = t_rawCommand.isJson();
164 
165  //Parse Parameters
166  for(quint32 i = 0; i < m_qMapCommands[t_sCommandName].count(); ++i)
167  {
168  QVariant::Type t_type = m_qMapCommands[t_sCommandName][i].type();
169 
170  QVariant t_qVariantParam(t_rawCommand.pValues()[i]);
171 
172  if(t_qVariantParam.canConvert(t_type) && t_qVariantParam.convert(t_type))
173  m_qMapCommands[t_sCommandName][i] = t_qVariantParam;
174  else
175  return;
176  }
177 
178  m_qMapCommands[t_sCommandName].execute();
179  }
180 }
181 
182 
183 //*************************************************************************************************************
184 
186 {
187  return m_qMapCommands[key];
188 }
189 
190 
191 //*************************************************************************************************************
192 
193 const Command CommandManager::operator[] (const QString &key) const
194 {
195  return m_qMapCommands[key];
196 }
Command & operator[](const QString &key)
void insert(const QJsonDocument &p_jsonDocument)
QString command() const
Definition: rawcommand.h:169
Declaration of the RawCommand Class.
virtual void update(Subject *p_pSubject)
The Subject class provides the base class of every subject of the observer design pattern...
bool hasCommand(const QString &p_sCommand) const