From af289133d4df82dc55cd9fd2df8541d9c08949cc Mon Sep 17 00:00:00 2001 From: Nils Schwabe Date: Fri, 4 Apr 2014 16:46:11 +0200 Subject: simplified the xml caching --- src/dialog.cpp | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) (limited to 'src/dialog.cpp') diff --git a/src/dialog.cpp b/src/dialog.cpp index 0967d8c..009da02 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "ui_dialog.h" #include "sessiontreeitem.h" @@ -293,53 +294,46 @@ void Dialog::onCenterTimer() { } void Dialog::addSessionsAfterDownload(QNetworkReply* reply) { + QString temp_filename; + if (reply->error() != QNetworkReply::NoError) { if (debugMode) { qDebug() << "Error reading from URL: " << reply->error(); } - QFile backup_file(xml_backup_filename); + QFile backup_file(xml_filename); if (!backup_file.open(QIODevice::ReadOnly)) { if (debugMode) { - qDebug() << "Cannot read backup file " << xml_backup_filename << " either"; + qDebug() << "Cannot read backup file " << xml_filename << " either"; } this->removeItem(QCoreApplication::instance()->translate("Dialog", "Loading..."), 1); this->addLabelItem(QCoreApplication::instance()->translate("Dialog", "URL Error"), 1); return; } if (debugMode) { - qDebug() << "Used backup file " << xml_backup_filename; + qDebug() << "Used backup file " << xml_filename; } backup_file.close(); - QList sessions = VSession::readXmlFile(xml_backup_filename); + QList sessions = VSession::readXmlFile(xml_filename); qSort(sessions.begin(), sessions.end(), myLessThan); this->addItems(sessions, 1); } else { - QFile file(xml_filename); - if (!file.open(QIODevice::WriteOnly)) { - if (debugMode) { - qDebug() << "Could not write XML to " << xml_filename; - } - return; - } - QByteArray data = reply->readAll(); - if (file.write(data) != data.length()) { - return; - } - - if (!file.setPermissions(QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther)) { - if (debugMode) { - qDebug() << "Could not change permissions of file: " << news_backup_filename; - } - } - file.close(); + // write xml to temporary file + temp_filename = QDir::tempPath() + "/vmchooser2/vmchooser-XXXXXX.xml"; + QTemporaryFile tmpfile(temp_filename); + if (!tmpfile.open() || tmpfile.write(data) == -1) { + return; + } + tmpfile.close(); + tmpfile.setAutoRemove(false); + temp_filename = tmpfile.fileName(); } - QList sessions = VSession::readXmlFile(xml_filename); + QList sessions = VSession::readXmlFile(temp_filename); this->removeItem(QCoreApplication::instance()->translate("Dialog", "Loading..."), 1); -- cgit v1.2.3-55-g7522