Fix language getting reset to system default on saving preferences
[amule.git] / src / utils / plasmamule / qt-emc.cpp
blob35bc7699769ccf3f23cdbb023eb4be150701f803
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2010 Werner Mahr (Vollstrecker) <amule@vollstreckernet.de>
5 //
6 // Any parts of this program contributed by third-party developers are copyrighted
7 // by their respective authors.
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "qt-emc.h"
26 qtEmc::qtEmc(const QString &filename)
29 QStringList files = filename.split("file://");
31 for (QStringList::const_iterator constFilesIterator = files.constBegin(); constFilesIterator != files.constEnd(); constFilesIterator++)
33 if (!QString(*constFilesIterator).remove("\n").trimmed().isEmpty())
35 QFile collection(QString(*constFilesIterator).trimmed());
37 if (collection.open (QIODevice::ReadOnly))
39 valid = readBinary(collection);
40 } else {
41 valid = FALSE;
42 errorCode = BadFileFormat;
45 collection.close();
50 const int qtEmc::getError()
52 return errorCode;
55 const QString qtEmc::getErrorMessage()
58 switch (errorCode)
60 case BadFileFormat:
62 return QString("File format wasn't recognised");
65 case BadTagFormat:
67 return QString("Tag format -%1- not recognized.").arg(lastTag);
70 case UnknownTag:
72 return QString("An unknown Tag (%1 in type %2) was read from collection file").arg(lastTag).arg(lastTagType);
75 case UnknownTagType:
77 return QString("An unknown type of tag (%1) was read from file").arg(lastTagType);
80 case WrongTagCount:
82 return QString("Tag count claims to be %1").arg(lastTag);
85 case CorruptFile:
87 return QString("Your collection file was corrupted and ends to Early");
92 const QStringList qtEmc::getLinks()
94 return list;
97 const bool qtEmc::isValid()
99 return valid;
102 bool qtEmc::readBinary(QFile &collection)
104 int fileSize;
105 QString fileHash, fileName;
106 quint8 rating, sFileSize, tag, tagType;
107 quint16 length, mFileSize;
108 quint32 lFileSize, llength, tagCount;
109 quint64 xlFileSize;
111 QDataStream in(&collection);
112 in.setByteOrder(QDataStream::LittleEndian);
114 in >> emcVersion >> headerTagCount;
116 if (in.atEnd())
118 errorCode = CorruptFile;
119 return FALSE;
120 } else if (emcVersion > 0x02)
122 return readText(collection);
125 for (int i=1; i<=headerTagCount; i++)
127 in >> tagType >> tagFormat;
129 if (in.atEnd())
131 errorCode = CorruptFile;
132 return FALSE;
135 if (tagFormat != 1)
137 lastTag = tag;
138 errorCode = BadTagFormat;
139 return FALSE;
142 in >> tag;
144 if (in.atEnd())
146 errorCode = CorruptFile;
147 return FALSE;
150 switch (tag)
152 case 0x01: //FT_FILENAME
154 in >> length;
156 if (in.atEnd())
158 errorCode = CorruptFile;
159 return FALSE;
162 char* buffer = new char[length];
163 in.readRawData(buffer, length);
165 name = QString(buffer);
167 delete [] buffer;
168 break;
171 case 0x31: //FT_COLLECTIONAUTHOR
173 in >> length;
175 if (in.atEnd())
177 errorCode = CorruptFile;
178 return FALSE;
181 char* buffer = new char[length];
182 in.readRawData(buffer, length);
184 author = QString(buffer);
186 delete [] buffer;
187 break;
190 case 0x32: //FT_COLLECTIONAUTHORKEY
192 in >> llength;
194 if (in.atEnd())
196 errorCode = CorruptFile;
197 return FALSE;
200 char* buffer = new char[llength];
201 in.readRawData(buffer, llength);
202 authorKey = QString(buffer);
204 delete [] buffer;
205 break;
208 default:
210 lastTag = tag;
211 errorCode = UnknownTag;
212 return FALSE;
215 break;
219 in >> fileCount;
221 if (in.atEnd())
223 errorCode = CorruptFile;
224 return FALSE;
227 for (int i=1; i<=fileCount; i++)
229 fileHash.clear();
230 fileName.clear();
232 in >> tagCount;
234 if (in.atEnd())
236 errorCode = CorruptFile;
237 return FALSE;
240 if (tagCount > 5)
242 lastTag = tagCount;
243 errorCode = WrongTagCount;
244 return FALSE;
247 for (int j=1; j<=tagCount; j++)
249 in >> tag;
251 if (in.atEnd())
253 errorCode = CorruptFile;
254 return FALSE;
257 in >> tagType;
259 if (in.atEnd())
261 errorCode = CorruptFile;
262 return FALSE;
265 switch (tagType)
267 case 0x01: // FT_FILENAME
269 switch (tag)
271 case 0x82:
273 in >> length;
275 if (in.atEnd())
277 errorCode = CorruptFile;
278 return FALSE;
281 char* buffer = new char[length];
282 in.readRawData(buffer, length);
283 fileName = QString().fromLocal8Bit(buffer, length).trimmed();
285 if (fileName.length() > length)
287 fileName.chop(fileName.length() - length);
288 } else if (fileName.length() == length)
290 int pos=0;
292 for (int k=0; k<length; k++)
294 if (fileName.at(k) != buffer[k+pos])
296 pos++;
300 fileName.chop(pos);
303 delete [] buffer;
305 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
307 errorCode = CorruptFile;
308 return FALSE;
311 break;
314 default:
316 if (((tag^0x80) >= 0x11) && ((tag^0x80) <= 0x20))
319 length = (tag^0x80) - 0x10;
321 char* buffer = new char[length];
322 in.readRawData(buffer, length);
323 fileName = QString().fromLocal8Bit(buffer, length).trimmed();
325 if (fileName.length() > length)
327 fileName.chop(fileName.length() - length);
328 } else if (fileName.length() == length)
330 int pos=0;
332 for (int k=0; k<length; k++)
334 if (fileName.at(k) != buffer[k+pos])
336 pos++;
340 fileName.chop(pos);
343 delete [] buffer;
345 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
347 errorCode = CorruptFile;
348 return FALSE;
350 } else {
351 lastTag = tag;
352 lastTagType = tagType;
353 errorCode = UnknownTag;
354 return FALSE;
359 break;
362 case 0x02: //FT_FILESIZE
364 switch (tag)
366 case 0x83:
368 in >> lFileSize;
369 fileSize = lFileSize;
371 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
373 errorCode = CorruptFile;
374 return FALSE;
377 break;
380 case 0x88:
382 in >> mFileSize;
383 fileSize = mFileSize;
385 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
387 errorCode = CorruptFile;
388 return FALSE;
391 break;
394 case 0x89:
396 in >> sFileSize;
397 fileSize = sFileSize;
399 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
401 errorCode = CorruptFile;
402 return FALSE;
405 break;
408 case 0x8b:
410 in >> xlFileSize;
411 fileSize = xlFileSize;
413 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
415 errorCode = CorruptFile;
416 return FALSE;
419 break;
422 default:
424 lastTag = tag;
425 lastTagType = tagType;
426 errorCode = UnknownTag;
427 return FALSE;
431 break;
434 case 0x28: //FT_FILEHASH
436 switch (tag)
438 case 0x81: //FT_FILEHASH
440 char* buffer = new char[16];
441 in.readRawData(buffer, 16);
443 if (in.atEnd() && (j != tagCount) && (i =! fileCount))
445 errorCode = CorruptFile;
446 return FALSE;
449 for (int pos = 0; pos < 16; pos++)
451 fileHash.append(QString().setNum(((buffer[pos] >> 4) & 0xF), 16));
452 fileHash.append(QString().setNum((buffer[pos] & 0x0F), 16));
455 delete [] buffer;
456 break;
459 default:
461 lastTag = tag;
462 lastTagType = tagType;
463 errorCode = UnknownTag;
464 return FALSE;
468 break;
470 // The comment is read correctly. It will be stored when I know what to do with it.
471 case 0xf6: //FT_FILECOMMENT
473 if ((tag^0x80) == 0x02)
475 in >> length;
477 if (in.atEnd())
479 errorCode = CorruptFile;
480 return FALSE;
482 } else {
483 length = ((tag^0x80)-0x10);
486 char* buffer = new char[length];
487 in.readRawData(buffer, length);
489 delete [] buffer;
490 break;
492 // The file-rating is read correctly. It will be stored when I know what to do with it.
493 case 0xf7: //FT_FILERATING
495 in >> rating;
497 break;
500 default:
502 lastTagType = tagType;
503 errorCode = UnknownTagType;
504 return FALSE;
509 list.append(QString("ed2k://|file|%1|%2|%3|/").arg(fileName).arg(fileSize).arg(fileHash));
512 return TRUE;
515 bool qtEmc::readText(QFile &collection)
517 quint8 character;
518 QString tmp;
520 collection.seek(0);
521 QDataStream in(&collection);
522 in.setByteOrder(QDataStream::LittleEndian);
524 for (int i=0; i<=6; i++)
526 in >> character;
527 tmp.append(character);
530 if (tmp == "ed2k://")
532 while (!in.atEnd())
534 in >> character;
536 if (character == 0x0d)
538 list.append(tmp);
539 tmp.clear();
540 } else if (character != 0x0a)
542 tmp.append(character);
545 } else {
546 errorCode = BadFileFormat;
547 return FALSE;
550 return TRUE;