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 +++++++++++++++++++++++---------------------- src/dialog.h | 11 +- src/filedownloader.cpp | 84 ++++------- src/filedownloader.h | 40 +---- src/httpxmldownloader.cpp | 22 --- src/httpxmldownloader.h | 19 --- src/main.cpp | 24 +-- src/sessionsiconholder.cpp | 66 ++++----- src/sessionsiconholder.h | 4 - src/vsession.h | 3 - src/xsession.cpp | 2 + 11 files changed, 254 insertions(+), 384 deletions(-) delete mode 100644 src/httpxmldownloader.cpp delete mode 100644 src/httpxmldownloader.h (limited to 'src') 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; diff --git a/src/dialog.h b/src/dialog.h index ccdccd2..b38aeac 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -29,6 +29,7 @@ class Dialog : public QDialog { static const int TAB_MY_COURSES = 1; static const int TAB_ALL_VMS = 2; static const int TAB_COUNT = 3; + public: // Public methods explicit Dialog(int defaultTab = -1, bool examMode = false, QWidget *parent = nullptr); ~Dialog(); @@ -40,10 +41,13 @@ class Dialog : public QDialog { void selectPreviousSession(); void setTheme(); void startSession(const QString& name); + void downloadData(const QString& locationIds); protected: // Overrides void changeEvent(QEvent *e); void mousePressEvent(QMouseEvent *event); + bool eventFilter(QObject *target, QEvent *event); + void keyPressEvent(QKeyEvent * e); private: // Private vars n methods Ui::Dialog *ui; @@ -62,7 +66,6 @@ class Dialog : public QDialog { void onTabButtonChanged(int tab); void configClearButton(); void setListModel(SessionTreeModel *model); - void keyPressEvent(QKeyEvent * e); void selectFirstElement(); void checkAutostart(); @@ -85,12 +88,6 @@ class Dialog : public QDialog { void on_leftKey(); void on_rightKey(); void on_spaceKey(); - - public slots: - void addSessionsAfterDownload(QNetworkReply* reply); - void addNewsAfterDownload(QNetworkReply* reply); - void addHelpAfterDownload(QNetworkReply* reply); - bool eventFilter(QObject *target, QEvent *event); }; #endif // DIALOG_H diff --git a/src/filedownloader.cpp b/src/filedownloader.cpp index e0b0289..30f30da 100644 --- a/src/filedownloader.cpp +++ b/src/filedownloader.cpp @@ -1,72 +1,46 @@ -/* - * filedownloader.cpp - * - * Created on: Mar 7, 2014 - * Author: nils - */ - -#include - #include "filedownloader.h" +#include +#include + // Maximum size of download -#define MAXSIZE (200000) +#define MAXSIZE (400000) static QNetworkAccessManager m_WebCtrl; -FileDownloader::FileDownloader(const QUrl& fileUrl, QObject *parent) : - QObject(parent), started(false), url(fileUrl) { -} - -FileDownloader::~FileDownloader() { +static void killReply(QNetworkReply *reply); -} - -bool FileDownloader::downloadFile() { - if (this->started) - return true; - QNetworkRequest request(this->url); +bool FileDownloader::download(const QUrl& fileUrl, const std::function & f) { + QNetworkRequest request(fileUrl); request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); QNetworkReply *reply = m_WebCtrl.get(request); if (reply == nullptr) return false; - this->started = true; - connect(reply, SIGNAL(finished()), SLOT(fileDownloaded())); - connect(reply, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); - return true; -} - -/* - * Slots from networkreply - */ - -void FileDownloader::downloadFailed(QNetworkReply::NetworkError) { - QNetworkReply *reply = reinterpret_cast(this->sender()); - killReply(reply); - emit downloaded(this->url, QByteArray()); -} - -void FileDownloader::fileDownloaded() { - QNetworkReply *reply = reinterpret_cast(this->sender()); - if (reply == nullptr) - return; - QByteArray downloadedData(reply->readAll()); - killReply(reply); - //emit a signal - emit downloaded(this->url, downloadedData); -} - -void FileDownloader::downloadProgress(qint64 received, qint64 totalSize) { - QNetworkReply *reply = reinterpret_cast(this->sender()); - if (reply == nullptr) - return; - if (received > MAXSIZE || totalSize > MAXSIZE) { + // Handle normal finish + QObject::connect(reply, &QNetworkReply::finished, [reply, f, fileUrl]() { + QByteArray downloadedData(reply->readAll()); killReply(reply); - emit downloaded(this->url, QByteArray()); - } + f(QNetworkReply::NoError, downloadedData); + }); + // Handle files that are too large + QObject::connect(reply, &QNetworkReply::downloadProgress, [reply, f, fileUrl](qint64 received, qint64 totalSize) { + if (received > MAXSIZE || totalSize > MAXSIZE) { + qDebug() << fileUrl << "too large"; + killReply(reply); + f(QNetworkReply::NetworkError::UnknownNetworkError, QByteArray()); + } + }); + // Handle errors + QObject::connect(reply, static_cast(&QNetworkReply::error), + [reply, f, fileUrl](QNetworkReply::NetworkError err) { + qDebug() << fileUrl << "error" << err; + killReply(reply); + f(err, QByteArray()); + }); + return true; } -void FileDownloader::killReply(QNetworkReply *reply) { +static void killReply(QNetworkReply *reply) { if (reply == nullptr) return; reply->blockSignals(true); diff --git a/src/filedownloader.h b/src/filedownloader.h index ec49c23..b20c6a8 100644 --- a/src/filedownloader.h +++ b/src/filedownloader.h @@ -1,51 +1,15 @@ -/* - * filedownloader.h - * - * Created on: Mar 7, 2014 - * Author: nils - */ - #ifndef FILEDOWNLOADER_H_ #define FILEDOWNLOADER_H_ #include #include -#include -#include #include -class FileDownloader : public QObject +class FileDownloader { - Q_OBJECT - public: - explicit FileDownloader(const QUrl& fileUrl, QObject *parent = nullptr); - - virtual ~FileDownloader(); - - bool downloadFile(); - -signals: - - /** - * Triggered when the download has finished. - * On error, downloadedData will be empty. - */ - void downloaded(const QUrl& url, const QByteArray& downloadedData); - -private slots: - - void fileDownloaded(); - void downloadFailed(QNetworkReply::NetworkError code); - void downloadProgress(qint64 received, qint64 totalSize); - -private: - - void killReply(QNetworkReply *reply); - - bool started; - QUrl url; + static bool download(const QUrl& fileUrl, const std::function & f); }; diff --git a/src/httpxmldownloader.cpp b/src/httpxmldownloader.cpp deleted file mode 100644 index c143995..0000000 --- a/src/httpxmldownloader.cpp +++ /dev/null @@ -1,22 +0,0 @@ - -#include "httpxmldownloader.h" -#include -#include - -HttpXmlDownloader::HttpXmlDownloader() { - nam = new QNetworkAccessManager(this); -} - -QNetworkReply* HttpXmlDownloader::makeRequest(const QString& xmlurl, const QString& locationIds) { - QUrl url(xmlurl); - if (!locationIds.isEmpty()) { - QUrlQuery query(url); - query.addQueryItem("locations", locationIds); - url.setQuery(query); - } - return nam->get(QNetworkRequest(url)); -} - -void HttpXmlDownloader::connectSlot(QObject* obj, const char* slot) { - QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), obj, slot); -} diff --git a/src/httpxmldownloader.h b/src/httpxmldownloader.h deleted file mode 100644 index dbca643..0000000 --- a/src/httpxmldownloader.h +++ /dev/null @@ -1,19 +0,0 @@ - -#ifndef HTTPTEST_H_ -#define HTTPTEST_H_ - -#include -#include -#include -#include - -class HttpXmlDownloader : public QObject { - Q_OBJECT - QNetworkAccessManager* nam; -public: - HttpXmlDownloader(); - QNetworkReply* makeRequest(const QString& xmlurl, const QString& locationIds = QString()); - void connectSlot(QObject* obj, const char* slot); -}; - -#endif /* HTTPTEST_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 46d8bc5..044f477 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,9 +16,7 @@ #include "command_line_options.h" #include "dialog.h" #include "globals.h" -#include "vsession.h" #include "xsession.h" -#include "httpxmldownloader.h" #include "choosersettings.h" int main(int argc, char *argv[]) { @@ -288,27 +286,7 @@ int main(int argc, char *argv[]) { Dialog w(defaultTab, cmdOptions.contains("exam-mode")); - /* DOWNLOAD VSESSIONS */ - HttpXmlDownloader httpxmldownloader; - httpxmldownloader.connectSlot(&w, - SLOT(addSessionsAfterDownload(QNetworkReply*))); - - // read xml and add items later - if (cmdOptions.contains("exam-mode")) { - httpxmldownloader.makeRequest(g_urlBase + "list?exams=exam-mode", locationIds); - } else { - httpxmldownloader.makeRequest(g_urlBase + "list", locationIds); - } - - /* DOWNLOAD NEWS */ - HttpXmlDownloader news_downloader; - news_downloader.connectSlot(&w, SLOT(addNewsAfterDownload(QNetworkReply*))); - news_downloader.makeRequest(g_urlBase + "news"); - - /* DOWNLOAD HELP-SECTION */ - HttpXmlDownloader help_downloader; - help_downloader.connectSlot(&w, SLOT(addHelpAfterDownload(QNetworkReply*))); - help_downloader.makeRequest(g_urlBase + "help"); + w.downloadData(locationIds); w.setTheme(); diff --git a/src/sessionsiconholder.cpp b/src/sessionsiconholder.cpp index 5180658..8eecf26 100644 --- a/src/sessionsiconholder.cpp +++ b/src/sessionsiconholder.cpp @@ -1,10 +1,3 @@ -/* - * sessionsiconholder.cpp - * - * Created on: Mar 7, 2014 - * Author: nils - */ - #include #include #include @@ -29,34 +22,6 @@ SessionsIconHolder::SessionsIconHolder() { QDir().mkpath(TEMP_PATH_ICONS); } -void SessionsIconHolder::afterDownload(const QUrl& url, const QByteArray& downloadedData) { - // save the data to disk - QString strUrl(url.toString()); - QString file_path(url2filename(strUrl)); - QFile file(file_path); - if (!file.open(QFile::WriteOnly)) { - if (g_debugMode) { - qDebug() << "Could not write file: " << file_path; - } - return; - } - file.write(downloadedData); - - if (file.write(downloadedData) != downloadedData.length()) { - if (g_debugMode) { - qDebug() << "Could not write file: " << file_path; - } - return; - } - - file.close(); - - QIcon icon(file_path); - icons.insert(strUrl, icon); - - emit iconDownloaded(url, icon); -} - QIcon SessionsIconHolder::getIcon(const QString& name) { // check if icon was loaded before if (icons.contains(name)) { @@ -100,10 +65,33 @@ QIcon SessionsIconHolder::getIcon(const QUrl& url) { icons.insert(strUrl, QIcon()); // else load icon from url - FileDownloader* fileDownloader = new FileDownloader(url, this); - QObject::connect(fileDownloader, SIGNAL(downloaded(const QUrl&, const QByteArray&)), - this, SLOT(afterDownload(const QUrl&, const QByteArray&))); - fileDownloader->downloadFile(); + FileDownloader::download(url, [this, url](QNetworkReply::NetworkError, const QByteArray &downloadedData) { + // save the data to disk + QString strUrl(url.toString()); + QString file_path(url2filename(strUrl)); + QFile file(file_path); + if (!file.open(QFile::WriteOnly)) { + if (g_debugMode) { + qDebug() << "Could not write file: " << file_path; + } + return; + } + file.write(downloadedData); + + if (file.write(downloadedData) != downloadedData.length()) { + if (g_debugMode) { + qDebug() << "Could not write file: " << file_path; + } + return; + } + + file.close(); + + QIcon icon(file_path); + icons.insert(strUrl, icon); + + emit iconDownloaded(url, icon); + }); return QIcon(); } diff --git a/src/sessionsiconholder.h b/src/sessionsiconholder.h index 06ca73c..048236a 100644 --- a/src/sessionsiconholder.h +++ b/src/sessionsiconholder.h @@ -17,7 +17,6 @@ #include "globals.h" #include "sessionsiconholder.h" -#include "filedownloader.h" class SessionTreeModel; @@ -36,9 +35,6 @@ public: QIcon getIcon(const QString& name); QIcon getIcon(const QUrl& url); static SessionsIconHolder* get() { if (instance == nullptr) instance = new SessionsIconHolder(); return instance; } - -public slots: - void afterDownload(const QUrl& url, const QByteArray& downloadedData); }; #endif /* SESSIONSICONHOLDER_H_ */ diff --git a/src/vsession.h b/src/vsession.h index 08885cc..18d5c38 100644 --- a/src/vsession.h +++ b/src/vsession.h @@ -6,7 +6,6 @@ #include #include #include "session.h" -#include "httpxmldownloader.h" #include "globals.h" enum ImgType { @@ -88,9 +87,7 @@ class VSession : public Session { bool operator<(const Session& other) const; - static QList readXmlDir(const QString& path); static QList readXmlFile(const QString& filepath); - static void addSessionsAfterDownload(QNetworkReply* reply); private: QList keywords_; diff --git a/src/xsession.cpp b/src/xsession.cpp index d72aecf..2d7214a 100644 --- a/src/xsession.cpp +++ b/src/xsession.cpp @@ -23,9 +23,11 @@ void XSession::init(const QString& name, const QString& exec, this->exec_ = exec; this->comment_ = comment; this->icon_ = icon; + this->priority_ = 0; } bool XSession::init(const QString& filename) { + this->priority_ = 0; QSettings settings(filename, QSettings::IniFormat); settings.setIniCodec("UTF-8"); -- cgit v1.2.3-55-g7522