#include "PersistentData.h" #include #include #include #include #include #include PersistentData::PersistentData(const QString &datafileName, QObject *parent) : QObject(parent) , isChangedFlag(false) { // load persistant data, if available this->filename = datafileName; QFileInfo dataFileInfo(this->filename); QString dataFilePath = dataFileInfo.path(); QDir dir; if ( ! dir.exists(dataFilePath)) { qCritical() << "Persistent data file does not exist!"; qCritical() << " --> create new: " << this->filename; dir.mkpath(dataFilePath); } this->read(); } void PersistentData::serializeToFile() { this->save(); } void PersistentData::save() { QFile fileOut(this->filename); if (fileOut.open(QIODevice::WriteOnly)) { QDataStream out(&fileOut); out.setVersion(QDataStream::Qt_4_6); out << this->hash; fileOut.flush(); fileOut.close(); } } void PersistentData::read() { QFile fileIn(this->filename); if (fileIn.open(QIODevice::ReadOnly)) { QDataStream in(&fileIn); in.setVersion(QDataStream::Qt_4_6); in >> hash; fileIn.close(); } } QVariant PersistentData::getParameter(const QString & key) const { #if defined (ARCH_DesktopLinux) // note: QVariant.toString() returns empty string for custom types qDebug() << "VendingData::getParameter() key = " << key << " value = " << hash.value(key).toString(); #endif return hash.value(key); } QVariant PersistentData::getParameter(const QString & key) { #if defined (ARCH_DesktopLinux) // note: QVariant.toString() returns empty string for custom types qDebug() << "VendingData::getParameter() key = " << key << " value = " << hash.value(key).toString(); #endif return hash.value(key); } void PersistentData::setParameter(const QString & key, QVariant value) { this->isChangedFlag = true; #if defined (ARCH_DesktopLinux) // note: QVariant.toString() returns empty string for custom types qDebug() << "VendingData::setParameter() key = " << key << " value = " << value.toString(); #endif this->hash.insert(key, value); } void PersistentData::clearParameter(const QString & key) { this->isChangedFlag = true; this->hash.remove(key); } bool PersistentData::hasParameter(const QString & key) const { return hash.contains(key); } uint PersistentData::getUintParameter(const QString & key) const { qDebug() << "PersistentData::getUintParameter() key = " << key << " value = " << hash.value(key).toString(); uint returnValue = 0; bool ok; returnValue = hash.value(key).toString().toUInt(&ok); if (!ok) returnValue = 0; return returnValue; } QList PersistentData::uniqueKeys() const { return hash.uniqueKeys(); } void PersistentData::setDCFirmwareVersion(const QString & fw_version) { if (this->hash["dc_fw_version"].toString() != fw_version) { this->isChangedFlag = true; this->hash.insert("dc_fw_version", fw_version); } } QString PersistentData::getDCFirmwareVersion() { return this->hash["dc_fw_version"].toString(); }