From 7a8be902d21f5cf84ef74633a2dda910af3699a1 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 9 Jul 2019 15:49:44 +0200 Subject: Rewrite list/news/help download and backup file handling --- src/dialog.cpp | 191 ++++++++++++++++++--------------------------------------- 1 file changed, 58 insertions(+), 133 deletions(-) (limited to 'src/dialog.cpp') diff --git a/src/dialog.cpp b/src/dialog.cpp index d87753a..388290d 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -26,6 +26,8 @@ static bool isProcessRunning(const QString &binary); +static QDomDocument toDomDocument(const QString& what, const QByteArray& data, const QString& backupFile, const QString& mandatoryChild); + Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { @@ -460,44 +462,21 @@ void Dialog::downloadData(const QString& locationIds) { // // Download lecture XML FileDownloader::download(listUrl, [this](QNetworkReply::NetworkError err, const QByteArray& data) { - QString temp_filename; - - if (err != QNetworkReply::NoError) { - qDebug() << "Error reading from URL: " << err; - - QFile backup_file(TEMP_PATH_XML_LIST); + QList sessions; + QDomDocument doc = toDomDocument(QStringLiteral("lecture list"), data, TEMP_PATH_XML_LIST, QStringLiteral("settings")); + sessions = VSession::loadFromXmlDocument(doc); - if (!backup_file.open(QIODevice::ReadOnly)) { - qDebug() << "Cannot read backup file " << TEMP_PATH_XML_LIST << " either"; - this->removeStatusString(STR_LOADING); + this->removeStatusString(STR_LOADING); + if (sessions.isEmpty()) { + if (err == QNetworkReply::NoError) { + this->addStatusString(STR_NO_ITEMS); + } else { this->addStatusString(STR_URL_ERROR); - return; } - qDebug() << "Using backup file " << TEMP_PATH_XML_LIST; - backup_file.close(); - - temp_filename = TEMP_PATH_XML_LIST; } else { - // 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(temp_filename); - - this->removeStatusString(STR_LOADING); - - if (!sessions.isEmpty()) { - qSort(sessions.begin(), sessions.end(), myLessThan); + qSort(sessions.begin(), sessions.end(), sessionComparator); this->addItems(sessions, TAB_ALL_VMS); - // TODO: Filter user's classes and add to tab[TAB_MY_COURSES] - bool showEdit = false; + bool showEdit = false; // Only show edit button if at least one lecture is editable for (QList::const_iterator it = sessions.begin(); it != sessions.end(); ++it) { if (reinterpret_cast(*it)->canEdit()) { showEdit = true; @@ -507,8 +486,6 @@ void Dialog::downloadData(const QString& locationIds) { if (showEdit) { ui->chkAdminMode->setVisible(true); } - } else { - this->addStatusString(STR_NO_ITEMS); } checkAutostart(); @@ -522,133 +499,46 @@ void Dialog::downloadData(const QString& locationIds) { FileDownloader::download(QUrl(Config::isSet(Config::URL_NEWS) ? Config::get(Config::URL_NEWS) : Config::get(Config::URL_BASE).append("/news")), [this](QNetworkReply::NetworkError err, const QByteArray& data) { - if (err != QNetworkReply::NoError) { - if (g_debugMode) { - qDebug() << "Could not get news. Try to get cached news."; - } - - // try to get cached news - QFile backup_file(TEMP_PATH_NEWS); - - if (!backup_file.open(QIODevice::ReadOnly)) { - if (g_debugMode) { - qDebug() << "Cannot read backup file " << TEMP_PATH_NEWS << " either"; - } - this->ui->newsTextBrowser->setText(QCoreApplication::instance()->translate("Dialog", "Could not get news.")); - return; - } - if (g_debugMode) { - qDebug() << "Used backup file " << TEMP_PATH_NEWS; - } - backup_file.close(); - return; - } - QDomDocument doc; - if (!doc.setContent(data)) { - qDebug() << "News XML contains errors."; - return; - } + QDomDocument doc = toDomDocument(QStringLiteral("news"), data, TEMP_PATH_NEWS, QStringLiteral("news")); QDomElement newsNode = doc.firstChildElement("news"); QDomElement timeNode = newsNode.firstChildElement("date"); QDomElement infoNode = newsNode.firstChildElement("info"); + QDomElement headlineNode = newsNode.firstChildElement("headline"); QDateTime timestamp; timestamp.setTime_t(timeNode.text().toUInt()); if (timeNode.isNull() || infoNode.isNull()) { + qDebug() << "Could not load news. Network:" << err; ui->newsTextBrowser->setText(QCoreApplication::instance()->translate("Dialog", "Could not get news. (//news/date or //news/info missing)")); } else { // format and print news - ui->newsTextBrowser->setText(QString("

" + newsNode.firstChildElement("headline").text() + "

" + ui->newsTextBrowser->setText(QString("

" + headlineNode.text() + "

" + timestamp.toString(Qt::SystemLocaleShortDate) + "

" + infoNode.text() + "

")); } - if (ui->helpBox->isHidden() && UserConfig::getLastNewsTime() < timestamp.toTime_t()) { + if (ui->helpBox->isHidden() + && (UserConfig::getLastNewsTime() < timestamp.toTime_t() || UserConfig::getLastNewsTime() > QDateTime::currentMSecsSinceEpoch() / 1000)) { // show news if not seen before on_helpNewsButton_clicked(); } // update ini UserConfig::setLastNewsTime(timestamp.toTime_t()); - - // make backup - QFile file(TEMP_PATH_NEWS); - if (!file.open(QIODevice::WriteOnly)) { - if (g_debugMode) { - qDebug() << "Could not write XML to " << TEMP_PATH_NEWS; - } - return; - } - - if (file.write(data) != data.length()) { - return; - } - if (!file.setPermissions(QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther)) { - if (g_debugMode) { - qDebug() << "Could not change permissions of file: " << TEMP_PATH_NEWS; - } - } - file.close(); }); // // Download help FileDownloader::download(QUrl(Config::isSet(Config::URL_HELP) ? Config::get(Config::URL_HELP) : Config::get(Config::URL_BASE).append("/help")), [this](QNetworkReply::NetworkError err, const QByteArray& data) { - if (err != QNetworkReply::NoError) { - if (g_debugMode) { - qDebug() << "Could not get help xml. Try to get cached help..."; - } - - // try to get cached news - QFile backup_file(TEMP_PATH_HELP); - - if (!backup_file.open(QIODevice::ReadOnly)) { - if (g_debugMode) { - qDebug() << "Cannot read backup file " << TEMP_PATH_HELP << " either"; - } - this->ui->helpTextBrowser->setText(QCoreApplication::instance()->translate("Dialog", "Could not get help.")); - return; - } - if (g_debugMode) { - qDebug() << "Used backup file " << TEMP_PATH_HELP; - } - backup_file.close(); - return; - } - QDomDocument doc; - if (!doc.setContent(data)) { - if (g_debugMode) { - qDebug() << "Help file contains errors."; - } - return; - } - - QDomElement newsNode = doc.firstChildElement("news").firstChildElement("info"); - if (newsNode.isNull()) { + QDomDocument doc = toDomDocument(QStringLiteral("help"), data, TEMP_PATH_HELP, QStringLiteral("news")); + QDomElement helpTextNode = doc.firstChildElement("news").firstChildElement("info"); + if (helpTextNode.isNull()) { + qDebug() << "Could not load help. Network:" << err; ui->helpTextBrowser->setText(QCoreApplication::instance()->translate("Dialog", "Could not get help (XML has no //news/info)")); } else { - ui->helpTextBrowser->setText(newsNode.text()); - } - - // make backup - QFile file(TEMP_PATH_HELP); - if (!file.open(QIODevice::WriteOnly)) { - if (g_debugMode) { - qDebug() << "Could not write XML to " << TEMP_PATH_HELP; - } - return; - } - - if (file.write(data) != data.length()) { - return; - } - if (!file.setPermissions(QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther | QFile::WriteUser | QFile::WriteGroup | QFile::WriteOther)) { - if (g_debugMode) { - qDebug() << "Could not change permissions of file: " << TEMP_PATH_HELP; - } + ui->helpTextBrowser->setText(helpTextNode.text()); } - file.close(); }); // } @@ -950,3 +840,38 @@ static bool isProcessRunning(const QString &binary) } return false; } + +static QDomDocument toDomDocument(const QString& what, const QByteArray& data, const QString& backupFile, const QString& mandatoryChild) +{ + QDomDocument doc; + if (!data.isEmpty()) { + if (!doc.setContent(data)) { + qDebug() << "Downloaded" << what << "XML contains errors."; + } + } else { + qDebug() << "No content downloaded for" << what; + } + QFile backup(backupFile); + if (doc.isNull() || !doc.hasChildNodes()) { + if (backup.open(QFile::ReadOnly)) { + if (!doc.setContent(&backup)) { + qDebug() << "Could not load" << what << "backup."; + } + } + } + if (doc.isNull() || !doc.hasChildNodes()) + return QDomDocument(); // Above methods failed + if (doc.firstChildElement(mandatoryChild).isNull()) { + qDebug() << "Downloaded" << what << "xml doesn't contain mandatory root node" << mandatoryChild; + return QDomDocument(); + } + if (!backup.isOpen()) { + // If it were already open we'd have read the backup, so no need to write it out again... + if (!backup.open(QFile::WriteOnly)) { + qDebug() << "Cannot open" << what << "backup file" << backupFile << "for writing"; + } else { + backup.write(data); + } + } + return doc; +} -- cgit v1.2.3-55-g7522