From 4ff543fe1f36bef43b9cc5b9b8d80dd354cfdc3c Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 30 Nov 2018 15:00:48 +0100 Subject: WTF did we have two classes for downloading files? --- src/dialog.cpp | 363 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 189 insertions(+), 174 deletions(-) (limited to 'src/dialog.cpp') diff --git a/src/dialog.cpp b/src/dialog.cpp index 8399e62..5035b56 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "unistd.h" #include "stdio.h" @@ -19,6 +20,7 @@ #include "globals.h" #include "vsession.h" #include "choosersettings.h" +#include "filedownloader.h" Dialog::Dialog(int defaultTab, bool examMode, QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { @@ -411,67 +413,211 @@ void Dialog::onCenterTimer() { } } -void Dialog::addSessionsAfterDownload(QNetworkReply* reply) { - QString temp_filename; +/** + * Download lecture list, news and help + */ +void Dialog::downloadData(const QString& locationIds) { + QUrl listUrl(g_urlBase + "list"); + QUrlQuery listQuery(listUrl); - if (reply->error() != QNetworkReply::NoError) { - qDebug() << "Error reading from URL: " << reply->error(); + if (!locationIds.isEmpty()) { + listQuery.addQueryItem("locations", locationIds); + } + if (examMode_) { + listQuery.addQueryItem("exams", "exam-mode"); + } + listUrl.setQuery(listQuery); + // + // 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); + + if (!backup_file.open(QIODevice::ReadOnly)) { + qDebug() << "Cannot read backup file " << TEMP_PATH_XML_LIST << " either"; + this->removeStatusString(STR_LOADING); + 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); + this->addItems(sessions, TAB_ALL_VMS); + // TODO: Filter user's classes and add to tab[TAB_MY_COURSES] + bool showEdit = false; + for (QList::const_iterator it = sessions.begin(); it != sessions.end(); ++it) { + if (reinterpret_cast(*it)->canEdit()) { + showEdit = true; + break; + } + } + if (showEdit) { + ui->chkAdminMode->setVisible(true); + } + } else { + this->addStatusString(STR_NO_ITEMS); + } + + checkAutostart(); - QFile backup_file(TEMP_PATH_XML_LIST); + // select last-session + selectPreviousSession(); + userInteracted_ = true; + }); + // + // News + FileDownloader::download(QUrl(g_urlBase + "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."; + } - if (!backup_file.open(QIODevice::ReadOnly)) { - qDebug() << "Cannot read backup file " << TEMP_PATH_XML_LIST << " either"; - this->removeStatusString(STR_LOADING); - this->addStatusString(STR_URL_ERROR); + // 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; } - qDebug() << "Using backup file " << TEMP_PATH_XML_LIST; - backup_file.close(); + QDomElement newsNode = doc.firstChildElement("news"); + QDomElement timeNode = newsNode.firstChildElement("date"); + QDomElement infoNode = newsNode.firstChildElement("info"); + QDateTime timestamp; + timestamp.setTime_t(timeNode.text().toUInt()); - temp_filename = TEMP_PATH_XML_LIST; - } else { - QByteArray data = reply->readAll(); + if (timeNode.isNull() || infoNode.isNull()) { + 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() + "

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

" + + infoNode.text() + "

")); + } - // write xml to temporary file - temp_filename = QDir::tempPath() + "/vmchooser2/vmchooser-XXXXXX.xml"; - QTemporaryFile tmpfile(temp_filename); - if (!tmpfile.open() || tmpfile.write(data) == -1) { + if (ChooserSettings::getSetting("last-news").toUInt() < timestamp.toTime_t()) { + // show news if not seen before + on_helpNewsButton_clicked(); + } + + // update ini + ChooserSettings::setSetting("last-news", QString::number(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; } - tmpfile.close(); - tmpfile.setAutoRemove(false); - temp_filename = tmpfile.fileName(); - } - QList sessions = VSession::readXmlFile(temp_filename); + 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(g_urlBase + "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..."; + } - this->removeStatusString(STR_LOADING); + // try to get cached news + QFile backup_file(TEMP_PATH_HELP); - if (!sessions.isEmpty()) { - qSort(sessions.begin(), sessions.end(), myLessThan); - this->addItems(sessions, TAB_ALL_VMS); - // TODO: Filter user's classes and add to tab[TAB_MY_COURSES] - bool showEdit = false; - for (QList::const_iterator it = sessions.begin(); it != sessions.end(); ++it) { - if (reinterpret_cast(*it)->canEdit()) { - showEdit = true; - break; + 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; } - if (showEdit) { - ui->chkAdminMode->setVisible(true); + QDomDocument doc; + if (!doc.setContent(data)) { + if (g_debugMode) { + qDebug() << "Help file contains errors."; + } + return; } - } else { - this->addStatusString(STR_NO_ITEMS); - } - checkAutostart(); + QDomElement newsNode = doc.firstChildElement("news").firstChildElement("info"); + if (newsNode.isNull()) { + ui->helpTextBrowser->setText(QCoreApplication::instance()->translate("Dialog", "Could not get help (XML has no //news/info)")); + } else { + ui->helpTextBrowser->setText(newsNode.text()); + } - // select last-session - selectPreviousSession(); - userInteracted_ = true; -} + // 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; + } + } + file.close(); + }); + // +} void Dialog::mousePressEvent(QMouseEvent * event) { QDialog::mousePressEvent(event); @@ -638,137 +784,6 @@ void Dialog::on_helpNewsButton_clicked() { } } -void Dialog::addNewsAfterDownload(QNetworkReply* reply) { - if (reply->error() != 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; - } - QByteArray data = reply->readAll(); - QDomDocument doc; - if (!doc.setContent(data)) { - qDebug() << "News XML contains errors."; - return; - } - QDomElement newsNode = doc.firstChildElement("news"); - QDomElement timeNode = newsNode.firstChildElement("date"); - QDomElement infoNode = newsNode.firstChildElement("info"); - QDateTime timestamp; - timestamp.setTime_t(timeNode.text().toUInt()); - - if (timeNode.isNull() || infoNode.isNull()) { - 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() + "

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

" - + infoNode.text() + "

")); - } - - if (ChooserSettings::getSetting("last-news").toUInt() < timestamp.toTime_t()) { - // show news if not seen before - on_helpNewsButton_clicked(); - } - - // update ini - ChooserSettings::setSetting("last-news", QString::number(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(); -} - -void Dialog::addHelpAfterDownload(QNetworkReply* reply) { - if (reply->error() != 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; - } - - QByteArray data = reply->readAll(); - 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()) { - 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; - } - } - file.close(); -} - void Dialog::keyPressEvent(QKeyEvent* event) { switch(event->key()) { case Qt::Key_Return: this->on_pushButtonStart_clicked(); break; -- cgit v1.2.3-55-g7522