-
Notifications
You must be signed in to change notification settings - Fork 25
/
parser.cpp
174 lines (154 loc) · 6.33 KB
/
parser.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**************************************************************************
* Copyright (c) 2012-2015 Raffaele Pertile <[email protected]>
* This file is part of touchegg-gce.
*
* touchegg-gce 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.
*
* touchegg-gce 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 touchegg-gce. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#include "parser.h"
#include "general.h"
#include <QDebug>
Parser::Parser(QIODevice *dev):
m_reader(dev)
{
}
Parser::~Parser(){
QIODevice *input = m_reader.device();
input->close();
delete input;
}
void Parser::sStart(){
if (m_reader.name() != QString::fromUtf8("touchégg"))
throw(err(QString::fromUtf8("Found string: %1, touchégg expected.").arg(m_reader.name().toString())));
state.append("TOUCHEGG");
}
void Parser::sTouchegg(){
if (m_reader.isStartElement()){
if (m_reader.name() == "settings"){
state.append("SETTINGS");
} else if (m_reader.name() == "application"){
if (!m_reader.attributes().hasAttribute("name"))
throw(err("Attribute 'name' expected."));
appKey = m_reader.attributes().value("name").toString();
appKey = Memory::addGroup(appKey);
state.append("APPLICATION");
}
else
throw(QString("Unrecognized string \"%1\" for token \"%2\".")
.arg(m_reader.name().toString())
.arg(m_reader.tokenString()));
} else if (m_reader.isEndElement() && m_reader.name() == QString::fromUtf8("touchégg")){
state.removeLast();
return;
} else
throw(QString("Unrecognized string \"%1\" for token \"%2\".")
.arg(m_reader.name().toString())
.arg(m_reader.tokenString()));
}
void Parser::sSettings(){
if (m_reader.isStartElement()){
if (m_reader.name() == "property"){
if (!m_reader.attributes().hasAttribute("name"))
throw(err("Attribute 'name' expected."));
state.append("PROPERTY");
sProperty(m_reader.attributes().value("name").toString());
}
else
throw(QString("Unrecognized string \"%1\" for token \"%2\".")
.arg(m_reader.name().toString())
.arg(m_reader.tokenString()));
} else if (m_reader.isEndElement() && m_reader.name() == "settings"){
state.removeLast();
return;
} else
throw(QString("Unrecognized string \"%1\" for token \"%2\".")
.arg(m_reader.name().toString())
.arg(m_reader.tokenString()));
}
void Parser::sProperty(QString key){
Memory::addProp(key,m_reader.readElementText());
state.removeLast();
}
void Parser::sApplication(){
if (m_reader.isEndElement() && m_reader.name() == "application"){
state.removeLast();
return;
} else if (m_reader.isStartElement() && m_reader.name() == "gesture"){
auto attrs = m_reader.attributes();
if (!(attrs.hasAttribute("type") ||
attrs.hasAttribute("fingers") ||
attrs.hasAttribute("direction")))
throw(err("Missing gesture attribute."));
ges = new gesture;
ges->type = attrs.value("type").toString();
ges->num = attrs.value("fingers").toString().toInt();
ges->direction = attrs.value("direction").toString();
state.append("GESTURE");
} else
throw(QString("Unrecognized string \"%1\" for token \"%2\".")
.arg(m_reader.name().toString())
.arg(m_reader.tokenString()));
}
void Parser::sGesture(){
if (m_reader.isEndElement() && m_reader.name() == "gesture"){
state.removeLast();
return;
} else if (m_reader.isStartElement() && m_reader.name() == "action"){
state.append("ACTION");
auto attrs = m_reader.attributes();
if (!attrs.hasAttribute("type"))
throw(err("Attribute 'type' expected."));
Group *g = Memory::getGroup(appKey);
g->addGest(ges->num, Lists::gT(ges->type), Lists::gD(ges->direction));
Gesture *gest = g->getGest(ges->num, Lists::gT(ges->type), Lists::gD(ges->direction));
gest->setAction(Lists::aT(attrs.value("type").toString()));
//sAction(g->getGest(ges->num, Lists::gT(ges->type), Lists::gD(ges->direction))->getAction());->setAction(Lists::aT(attrs.value("type").toString()))
QStringList params = m_reader.readElementText().split(":");
foreach(const QString &p, params)
gest->getAction()->addParam(p.section("=",0,0),p.section("=",1));
state.removeLast();
} else
throw(QString("Unrecognized string \"%1\" for token \"%2\".")
.arg(m_reader.name().toString())
.arg(m_reader.tokenString()));
}
Parser::error Parser::err(const QString &msg)
{
return error(m_reader,msg);
}
bool Parser::loadAll(){
state = QStringList();
state.append("START");
Memory();
try{
m_reader.readNextStartElement();
sStart();
while(state.last() != "START"){
m_reader.readNextStartElement();
if (state.last() == "TOUCHEGG") sTouchegg();
else if (state.last() == "SETTINGS") sSettings();
else if (state.last() == "APPLICATION") sApplication();
else if (state.last() == "GESTURE") sGesture();
else throw(err("Unhandled state."));
if (m_reader.hasError())
throw(err("Xml stream error."));
}
qDebug() << "Config file loaded.";
} catch(const error & e){
qWarning() << QString("Error while parsing at:%1:%2\nstate:\t"+state.last()).arg(e.line).arg(e.pos);
if (m_reader.hasError()) qWarning() << m_reader.errorString();
else qWarning() << e.msg;
return false;
}
return true;
}