|
1 | 1 | #include "devicequirks.h"
|
| 2 | +#include <QFile> |
| 3 | +#include <QDebug> |
| 4 | +#include <QMessageBox> |
| 5 | +#include <QDir> |
| 6 | + |
| 7 | + |
| 8 | +bool parseNumberList(QString str, QList<uint8_t> &list) { |
| 9 | + size_t pos = 0; |
| 10 | + size_t start = 0; |
| 11 | + bool parsing = true; |
| 12 | + while( parsing ) |
| 13 | + { |
| 14 | + std::string ccStr; |
| 15 | + |
| 16 | + pos = str.toStdString().find_first_of( ",", start ); |
| 17 | + if( std::string::npos == pos ) |
| 18 | + { |
| 19 | + ccStr = str.toStdString().substr( start ); |
| 20 | + parsing = false; |
| 21 | + } |
| 22 | + else |
| 23 | + { |
| 24 | + ccStr = str.toStdString().substr( start, pos-start ); |
| 25 | + start = pos + 1; |
| 26 | + } |
| 27 | + QString result = QString::fromStdString(ccStr); |
| 28 | + list.push_back(result.toInt()); |
| 29 | + } |
| 30 | + return true; |
| 31 | +} |
2 | 32 |
|
3 | 33 | DeviceQuirks::DeviceQuirks(QObject *parent) : QObject(parent)
|
4 | 34 | {
|
| 35 | + QString errorStr; |
| 36 | + int errorLine; |
| 37 | + int errorColumn; |
| 38 | + |
| 39 | + qDebug() << QDir(".").absolutePath(); |
| 40 | + QFile mfxml("Quirks.xml"); |
| 41 | + if (!mfxml.open(QFile::ReadOnly | QFile::Text)) { |
| 42 | + QMessageBox::warning(nullptr, tr("QXmlStream Quirks"), |
| 43 | + tr("Cannot read file %1:\n%2.") |
| 44 | + .arg(mfxml.fileName()) |
| 45 | + .arg(mfxml.errorString())); |
| 46 | + this->setReady(false); |
| 47 | + return; |
| 48 | + } |
| 49 | + if (!domDocument.setContent(&mfxml, false,&errorStr, &errorLine, &errorColumn)) { |
| 50 | + QMessageBox::information(nullptr, tr("Quirks Database"), tr("Parse Error at line %1, column %2:\n%3") |
| 51 | + .arg(errorLine) |
| 52 | + .arg(errorColumn) |
| 53 | + .arg(errorStr)); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + QDomElement root = domDocument.documentElement(); |
| 58 | + if (root.tagName() != "QuirkData") { |
| 59 | + QMessageBox::information(nullptr, tr("Quirks Database"), |
| 60 | + tr("The file is not an Quirk file.")); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + QDomElement child = root.firstChildElement("Quirk"); |
| 65 | + while (!child.isNull()) { |
| 66 | + QString name = child.attribute("name"); |
| 67 | + QuirksEntry *entry = new QuirksEntry(); |
| 68 | + if (child.hasAttribute("CommandClass")) |
| 69 | + parseNumberList(child.attribute("CommandClass"), entry->CommandClasses); |
| 70 | + if (child.hasChildNodes()) { |
| 71 | + QDomNode options = child.firstChild(); |
| 72 | + while (!options.isNull()) { |
| 73 | + if (options.nodeName().toUpper() == "OPTION") |
| 74 | + entry->options.push_back(options.toElement().attribute("name")); |
| 75 | + else if (options.nodeName().toUpper() == "HELP") |
| 76 | + entry->Help = options.firstChild().toText().data(); |
| 77 | + options = options.nextSibling(); |
| 78 | + } |
| 79 | + } |
| 80 | + this->m_quirks.insert(name, entry); |
| 81 | + child = child.nextSiblingElement(); |
| 82 | + } |
| 83 | + |
| 84 | + mfxml.close(); |
| 85 | + this->setReady(true); |
| 86 | + this->dump(); |
| 87 | +} |
5 | 88 |
|
| 89 | +DeviceQuirks &DeviceQuirks::GetInstance() { |
| 90 | + static DeviceQuirks instance; |
| 91 | + return instance; |
6 | 92 | }
|
| 93 | + |
| 94 | +void DeviceQuirks::setReady(bool ready) { |
| 95 | + this->m_Ready = ready; |
| 96 | +} |
| 97 | +bool DeviceQuirks::isReady() const { |
| 98 | + return this->m_Ready; |
| 99 | +} |
| 100 | + |
| 101 | +void DeviceQuirks::dump() { |
| 102 | + QHash<QString, QuirksEntry*>::iterator it; |
| 103 | + for (it = this->m_quirks.begin(); it != this->m_quirks.end(); ++it) { |
| 104 | + qDebug() << it.key(); |
| 105 | + qDebug() << "\tOptions:"; |
| 106 | + for (int i = 0; i < it.value()->options.count(); ++i) |
| 107 | + qDebug() << "\t\t" << it.value()->options.at(i); |
| 108 | + qDebug() << "\tCommandClasses:"; |
| 109 | + for (int i = 0; i < it.value()->CommandClasses.count(); ++i) |
| 110 | + qDebug() << "\t\t" << it.value()->CommandClasses.at(i); |
| 111 | + qDebug() << "\tHelp:"; |
| 112 | + qDebug() << "\t\t" << it.value()->Help; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
0 commit comments