-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatinfo.cpp
328 lines (309 loc) · 8.58 KB
/
formatinfo.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Log2Log Chat Log Converter
* Libraries
* Format Information
*
* @author Deltik
*
* License:
* This file is part of Log2Log.
*
* Log2Log 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.
*
* Log2Log 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 Log2Log. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "formatinfo.h"
/**
* Constructor: No Parameters
*/
FormatInfo::FormatInfo()
{
xmlsrc = ":/resources/formats.xml";
// Open formats file
QFile* file = new QFile(xmlsrc);
if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
// Create XML stream reader from file
QXmlStreamReader xml(file);
num_of_readNexts = 0;
}
/**
* Constructor: With Format To Search For
* @param QString unixname The format to look up
*/
FormatInfo::FormatInfo(QString unixname)
{
xmlsrc = ":/resources/formats.xml";
// Open formats file
QFile* file = new QFile(xmlsrc);
if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
// Create XML stream reader from file
QXmlStreamReader xml(file);
num_of_readNexts = 0;
// Read the XML file
while(!xml.atEnd())
{
// Read next item
QXmlStreamReader::TokenType token = xml.readNext(); num_of_readNexts ++;
// Skip beginning of file
if (token == QXmlStreamReader::StartDocument) continue;
// If token is an element...
if(token == QXmlStreamReader::StartElement)
{
// Bingo. If we found the format we're looking for...
if (xml.attributes().value("id").toString() == unixname)
{
dig_the_information_out(xml);
break;
}
}
}
}
/**
* Destructor
*/
FormatInfo::~FormatInfo()
{
}
/**
* Browser: Reset Pointer
*/
void FormatInfo::pointerReset()
{
FormatInfo();
}
/**
* Browser: Point to Next Format
* @returns bool true if there is a next to point at
*/
bool FormatInfo::pointerNext()
{
// Open formats file
QFile* file = new QFile(xmlsrc);
if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
{
return false;
}
// Create XML stream reader from file
QXmlStreamReader xml(file);
QXmlStreamReader::TokenType token;
// Catch up
for (int i = 0; i < num_of_readNexts; i ++)
{
token = xml.readNext();
}
// Dare to go on
token = xml.readNext(); num_of_readNexts ++;
// Repeat until <format> found or reached the end
while (!(token == QXmlStreamReader::StartElement && xml.name() == "format") && !xml.atEnd())
{
token = xml.readNext(); num_of_readNexts ++;
}
if (token == QXmlStreamReader::StartElement && xml.name() == "format")
{
return true;
}
// No more
return false;
}
/**
* Browser: Point to Previous Format
* @returns bool true if there is a previous to point at
*/
bool FormatInfo::pointerPrevious()
{
// TODO:
// This is not a critical feature, so FormatInfo can work fine without it.
return false; // <-- That's a semicolon. :)
}
/**
* Browser: Get Information from Current Pointer Location
* @returns bool true if successful
*/
bool FormatInfo::pointerDig()
{
// Open formats file
QFile* file = new QFile(xmlsrc);
if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
{
return false;
}
// Create XML stream reader from file
QXmlStreamReader xml(file);
QXmlStreamReader::TokenType token;
// Catch up
for (int i = 0; i < num_of_readNexts; i ++)
{
token = xml.readNext();
}
return dig_the_information_out(xml);
}
/**
* Dig the Information Out
* @precondition reader is currently pointing at a <format>
* @param QXmlStreamReader reader The reader to pick up from
* @returns bool true if successful
*/
bool FormatInfo::dig_the_information_out(QXmlStreamReader &reader)
{
// Check if reader is pointing at an opening <format> element
if(reader.tokenType() != QXmlStreamReader::StartElement && reader.name() == "format") return false;
// Just one thing: Store the unixname
fName_unix = reader.attributes().value("id").toString();
// Okay, it's a <format>. Let's move on
reader.readNext();
// Clean Variables
fName_display = "";
fName_client = "";
fName_class = "";
fIcon_path = "";
QIcon fIconn(":/images/etc/blank.png");
fIcon = fIconn;
fTo = NULL;
fFrom = NULL;
fPrecision = NULL;
fAccuracy = NULL;
fTime = NULL;
fTimezone = NULL;
fStatus = NULL;
fSystem = NULL;
fAliases = NULL;
fGroup = NULL;
fFromInfo = "";
fToInfo = "";
// Keep going until </format>
while(!(reader.tokenType() == QXmlStreamReader::EndElement && reader.name() == "format"))
{
QString currentElement = "format";
if (reader.tokenType() == QXmlStreamReader::StartElement)
{
currentElement = reader.name().toString();
reader.readNext();
if (reader.tokenType() != QXmlStreamReader::Characters)
{
continue;
}
}
if (currentElement == "name")
{
fName_display = reader.text().toString();
}
if (currentElement == "client")
{
fName_client = reader.text().toString();
}
if (currentElement == "class")
{
fName_class = reader.text().toString();
}
if (currentElement == "icon")
{
fIcon_path = reader.text().toString();
QIcon fIconn(fIcon_path);
fIcon = fIconn;
}
if (currentElement == "default")
{
QString path_proc = reader.text().toString();
path_proc = path_proc.replace("~", QDir::homePath());
path_proc = path_proc.replace("%APPDATA%", QStandardPaths::displayName(QStandardPaths::DataLocation));
path_proc = path_proc.replace("///", "/");
path_proc = path_proc.replace("//", "/");
fDefaultPath = QDir::toNativeSeparators(path_proc);
}
if (currentElement == "to")
{
if (reader.text() == "true")
fTo = true;
else
fTo = false;
}
if (currentElement == "from")
{
if (reader.text() == "true")
fFrom = true;
else
fFrom = false;
}
if (currentElement == "precision")
{
fPrecision = (int)reader.text().toString().toInt();
}
if (currentElement == "accuracy")
{
if (reader.text() == "true")
fAccuracy = true;
else
fAccuracy = false;
}
if (currentElement == "time")
{
if (reader.text() == "yes") fTime = 2;
else if (reader.text() == "group") fTime = 1;
else fTime = 0;
}
if (currentElement == "timezone")
{
if (reader.text() == "yes") fTimezone = 2;
else if (reader.text() == "group") fTimezone = 1;
else fTimezone = 0;
}
if (currentElement == "status")
{
if (reader.text() == "true")
fStatus = true;
else
fStatus = false;
}
if (currentElement == "system")
{
if (reader.text() == "true")
fSystem = true;
else
fSystem = false;
}
if (currentElement == "aliases")
{
if (reader.text() == "true")
fAliases = true;
else
fAliases = false;
}
if (currentElement == "group")
{
if (reader.text() == "true")
fGroup = true;
else
fGroup = false;
}
if (currentElement == "frominfo")
{
fFromInfo = reader.text().toString();
}
if (currentElement == "toinfo")
{
fToInfo = reader.text().toString();
}
// Next!
reader.readNext();
}
// Success or Failure
if (!fName_unix.isEmpty())
return true;
return false;
}