diff options
-rw-r--r-- | src/downloadmanager.cpp | 25 | ||||
-rw-r--r-- | src/fbgui.cpp | 90 | ||||
-rw-r--r-- | src/fbgui.h | 8 |
3 files changed, 51 insertions, 72 deletions
diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index 9f7a628..eb00354 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -7,6 +7,7 @@ DownloadManager::DownloadManager(){ qxtLog->debug() << "Initializing download manager..."; checkDownloadDirectory(); qnam = new QNetworkAccessManager(); + qnam->moveToThread(&dmThread); dip = false; } // ------------------------------------------------------------------------------------------------------- @@ -197,27 +198,3 @@ void DownloadManager::downloadFinished() } startNextDownload(); } -/******************************************************************************************************** -* - ** dead code: Header filename fetching & renaming ** - -const QByteArray cd = "Content-Disposition"; -QByteArray cdc = currentDownload->rawHeader(cd); -int x = cdc.indexOf("filename=\"") + 10; -cdc.remove(0, x).chop(1); -if (!cdc.isEmpty()) - currentTargetFilename = cdc; -else - currentTargetFilename = QString("download.\%1").arg(downloaded); - -QString tmp = outfile.fileName(); -tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); -qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename; - -if (outfile.rename(downloadPath + "/" + currentTargetFilename)) { - qxtLog->debug() << "Renamed file!"; -} -else { - qxtLog->debug() << "Failure to rename file!"; -} -*/ diff --git a/src/fbgui.cpp b/src/fbgui.cpp index 1bfad79..4dbbc58 100644 --- a/src/fbgui.cpp +++ b/src/fbgui.cpp @@ -6,9 +6,11 @@ #include "sysinfolibsysfs.h" #include <iostream> +#include <QThread> #include <QtWebKit> #include <QxtCore> +QThread dmThread; QString binPath(""); QUrl baseURL(""); QString downloadPath(""); @@ -49,6 +51,7 @@ fbgui::fbgui() QObject::connect(_webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), jsi, SLOT(attachToDOM())); + // initialize download manager DownloadManager* dm = new DownloadManager(); QObject::connect(dm, SIGNAL(downloadInfo(const QString&, const double&)), @@ -61,11 +64,15 @@ fbgui::fbgui() jsi, SLOT(updateProgressBar(const int&, const double&, const QString&))); QObject::connect(dm, SIGNAL(downloadQueueEmpty()), jsi, SLOT(callbackOnFinished())); + // move download manager to its own thread + dm->moveToThread(&dmThread); + dmThread.start(); // show filler page _webView->load(QUrl("qrc:/html/preload.html")); // start watching for fileToTriggerURL watchForTrigger(); + //if (checkHost()) loadURL(); // set properties setWindowTitle("fbgui"); @@ -73,6 +80,9 @@ fbgui::fbgui() setWindowFlags(Qt::FramelessWindowHint); showFullScreen(); } +fbgui::~fbgui(){ + dmThread.quit(); +} //------------------------------------------------------------------------------------------- // Layout / actions setup //------------------------------------------------------------------------------------------- @@ -125,33 +135,30 @@ void fbgui::createActions() */ void fbgui::watchForTrigger() { - // check if the directory to fileToTriggerURL exists - QFileInfo fi(fileToTriggerURL); - if (!fi.absoluteDir().exists()){ - qxtLog->debug() << "[watcher] " << fi.absolutePath() << " does not exists!"; - if (QDir::home().mkdir(fi.absolutePath())) - qxtLog->debug() << "[watcher] Successfully created " << fi.absolutePath(); - else - qxtLog->debug() << "[watcher] Failed to create " << fi.absolutePath(); - } - else - qxtLog->debug() << "[watcher] " << fi.absolutePath() << " exists!"; - // check if fileToTriggerURL already exists - if (fi.exists()){ + QFile file(fileToTriggerURL); + if (file.exists()){ qxtLog->debug() << "[watcher] " << fileToTriggerURL << " exists already!"; - // load URL + // try to load URL if (checkHost()) loadURL(); } else { - // watch the path where trigger file is expected - qxtLog->debug() << "[watcher] Watching " << fi.absolutePath() - << " for file: " << fi.fileName(); - QStringList pathToWatch(fi.absolutePath()); - _watcher = new QFileSystemWatcher(pathToWatch, this); - QObject::connect(_watcher, SIGNAL(directoryChanged(const QString&)), - this, SLOT(checkForTrigger(const QString&))); + // create it + if (file.open(QIODevice::WriteOnly)){ + qxtLog->debug() << "[gui] Created: " << fileToTriggerURL; + file.close(); + } + else { + qxtLog->debug() << "[gui] Creation of " << fileToTriggerURL << " failed! Exiting..."; + exit(EXIT_FAILURE); + } } + // watch the path where trigger file is expected + qxtLog->debug() << "[watcher] Watching " << fileToTriggerURL; + _watcher = new QFileSystemWatcher(QStringList(fileToTriggerURL), this); + QObject::connect(_watcher, SIGNAL(fileChanged(const QString&)), + this, SLOT(prepareURLLoad())); + } //------------------------------------------------------------------------------------------- /** @@ -164,23 +171,15 @@ void fbgui::watchForTrigger() * @see fbgui::checkHost() * @see fbgui::loadURL() */ -void fbgui::checkForTrigger(const QString& dirname) +void fbgui::prepareURLLoad() { - // check if fileToTriggerURL exists in the directory where the change occured - QFileInfo tfi(fileToTriggerURL); - QFileInfo fi(dirname + "/" + tfi.fileName()); - if (fi.exists()){ - qxtLog->debug() << "[watcher] " << fileToTriggerURL << " detected."; - // disconnect _watcher, his job is done - qxtLog->debug() << "[watcher] disconnected."; - _watcher->disconnect(this); - _watcher->deleteLater(); // memory problems with watcher - // load URL if host exists - if (checkHost()) loadURL(); - } - else - // do nothing / keep watching - qxtLog->debug() << "[watcher] weird file!"; + qxtLog->debug() << "[watcher] " << fileToTriggerURL << " changed!"; + // disconnect _watcher, his job is done + qxtLog->debug() << "[watcher] disconnected."; + _watcher->disconnect(this); + _watcher->deleteLater(); + // try to load URL + if (checkHost()) loadURL(); } //------------------------------------------------------------------------------------------- // Preparations for URL load @@ -215,14 +214,14 @@ bool fbgui::checkHost() const */ void fbgui::loadURL() { - - qxtLog->debug() << "[gui] Loading URL..."; - QByteArray postData = generatePOSTData(); - qxtLog->debug() << "[gui] POST data: " << postData; - QNetworkRequest req(baseURL); - // show arrow cursor - qApp->setOverrideCursor(QCursor(Qt::ArrowCursor)); - _webView->load(req, QNetworkAccessManager::PostOperation, postData); + if (checkHost()){ + qxtLog->debug() << "[gui] Loading URL..."; + QByteArray postData = generatePOSTData(); + QNetworkRequest req(baseURL); + // show arrow cursor + qApp->setOverrideCursor(QCursor(Qt::ArrowCursor)); + _webView->load(req, QNetworkAccessManager::PostOperation, postData); + } } //------------------------------------------------------------------------------------------- /** @@ -268,6 +267,7 @@ QByteArray fbgui::generatePOSTData() postData.append(si.getInfo("mac")); postData.append("&hardwarehash=" + hash.toHex()); postData.append("&serialnumber=" + serial); + qxtLog->debug() << "[post] POST data: " << postData; return postData; } //------------------------------------------------------------------------------------------- diff --git a/src/fbgui.h b/src/fbgui.h index 4ad17df..287b295 100644 --- a/src/fbgui.h +++ b/src/fbgui.h @@ -34,6 +34,7 @@ // Global settings variables +extern QThread dmThread; extern QString serialLocation; extern QString fileToTriggerURL; extern QString sessionID; @@ -49,6 +50,7 @@ class fbgui : public QMainWindow public: fbgui(); + ~fbgui(); private: //------------------- @@ -100,9 +102,9 @@ private slots: // toggles debug console when action _toggleDebugConsole happens. void toggleDebugConsole(); - // checks if the change reported by _watcher is the one we are looking for - // e.g. fileToTriggerURL has been created, ignores other events. - void checkForTrigger(const QString& dirname); + // triggered by fileChanged Signal of _watcher + // deletes _watcher, since we don't need it anymore and tries to load URL. + void prepareURLLoad(); }; #endif // FBGUI_H |