/** * @RDE_HEADER@ * Config class to parse XML-Files for Axis Camera Configuration * $Id$ * $HeadURL$ * $Author$ * Created by Terrell Scharfenberg, SLSE, 2016-05-27 */ #include "config.h" #include #include using namespace VapixConfig; Config::Config() { } // init is needed to clear the DomDocument for saving purpose void Config::init() { m_domDocument.clear(); m_root = m_domDocument.createElement(QLatin1String("Camera")); m_domDocument.appendChild(m_root); } // Reads XML and put created maps in maplists bool Config::processConfig(const QString &xmlConfig) { QFile f(xmlConfig); if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) return 0; m_domDocument.setContent(&f); m_root = m_domDocument.documentElement(); if (m_root.tagName() != QLatin1String("Camera")) return false; const QDomNodeList image = m_root.elementsByTagName(QLatin1String("Image")); const QDomNodeList window = m_root.elementsByTagName(QLatin1String("Motion")); const QDomNodeList server = m_root.elementsByTagName(QLatin1String("EventServer")); const QDomNodeList event = m_root.elementsByTagName(QLatin1String("Event")); const QDomNodeList action = m_root.elementsByTagName(QLatin1String("Action")); for (int i = 0; i <= image.size(); ++i) { const QDomElement tempElement = image.at(i).toElement(); QDomElement tempChild = tempElement.firstChild().toElement(); QMap imageMap; while (!tempChild.isNull()) { imageMap[tempChild.tagName()] = tempChild.firstChild().toText().data(); tempChild = tempChild.nextSibling().toElement(); } if (!imageMap.isEmpty()) { m_imageList.append(imageMap); } } for (int i = 0; i <= window.size(); ++i) { const QDomElement tempElement = window.at(i).toElement(); QDomElement tempChild = tempElement.firstChild().toElement(); QMap windowMap; while (!tempChild.isNull()) { windowMap[tempChild.tagName()] = tempChild.firstChild().toText().data(); tempChild = tempChild.nextSibling().toElement(); } if (!windowMap.isEmpty()) { m_windowList.append(windowMap); } } for (int i = 0; i <= server.size(); ++i) { const QDomElement tempElement = server.at(i).toElement(); QDomElement tempChild = tempElement.firstChild().toElement(); QMap serverMap; while (!tempChild.isNull()) { serverMap[tempChild.tagName()] = tempChild.firstChild().toText().data(); tempChild = tempChild.nextSibling().toElement(); } if (!serverMap.isEmpty()) { m_serverList.append(serverMap); } } for (int i = 0; i <= event.size(); ++i) { const QDomElement tempElement = event.at(i).toElement(); QDomElement tempChild = tempElement.firstChild().toElement(); QMap eventMap; while (!tempChild.isNull()) { eventMap[tempChild.tagName()] = tempChild.firstChild().toText().data(); tempChild = tempChild.nextSibling().toElement(); } if (!eventMap.isEmpty()) { m_eventList.append(eventMap); } } for (int i = 0; i <= action.size(); ++i) { const QDomElement tempElement = action.at(i).toElement(); QDomElement tempChild = tempElement.firstChild().toElement(); QMap actionMap; while (!tempChild.isNull()) { actionMap[tempChild.tagName()] = tempChild.firstChild().toText().data(); tempChild = tempChild.nextSibling().toElement(); } if (!actionMap.isEmpty()) { m_actionList.append(actionMap); } } f.close(); return true; } //Saves configuration from camera to XML file void Config::saveConfig(const QString &savePath) { QFile saveFile; saveFile.setFileName(savePath); if (saveFile.open(QIODevice::WriteOnly)) { QTextStream stream(&saveFile); stream << m_domDocument.toString(); saveFile.close(); } else qDebug("Failed to write data!"); } // used to fill XML void Config::addElementToXml(const QMap &givenMap, const QString &groupName) { QString tempName = groupName; if (!tempName.compare(QLatin1String("Event.*.Actions"))) tempName = QLatin1String("Action"); if (!tempName.compare(QLatin1String("EventServers"))) tempName = QLatin1String("EventServer"); QDomElement tempElem = m_domDocument.createElement(tempName); m_root.appendChild(tempElem); QDomElement tempChild; QDomText tempText; for (auto iterator = givenMap.begin(); iterator != givenMap.end(); ++iterator) { tempChild = m_domDocument.createElement(iterator.key()); tempElem.appendChild(tempChild); tempText = m_domDocument.createTextNode(iterator.value()); tempChild.appendChild(tempText); } }