diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/DownloadManager.cpp | 51 | ||||
| -rw-r--r-- | src/DownloadManager.h | 8 | ||||
| -rw-r--r-- | src/JSObject.cpp | 13 | ||||
| -rw-r--r-- | src/JSObject.h | 2 | ||||
| -rw-r--r-- | src/fbbrowser.cpp | 4 |
5 files changed, 43 insertions, 35 deletions
diff --git a/src/DownloadManager.cpp b/src/DownloadManager.cpp index 5bb5387..b89a371 100644 --- a/src/DownloadManager.cpp +++ b/src/DownloadManager.cpp @@ -1,26 +1,25 @@ #include "DownloadManager.h" -void DownloadManager::downloadFile(QString name) +void DownloadManager::downloadFile(QUrl& fileUrl) { - qDebug() << "Received downloadFile signal for:" << name; - this->processDownloadRequest(name); + qDebug() << "Received downloadFile signal for:" << fileUrl; + this->processDownloadRequest(fileUrl); } // ---------------------------------------------------------------------------------------- -void DownloadManager::processDownloadRequest(QString& filename) +void DownloadManager::processDownloadRequest(QUrl& url) { // Forge URL from the given filename and the base URL. - QUrl u = this->baseUrl.resolved(filename); // If download in progress, enqueue file and return. if (dip) { - qDebug() << "Download in progress! Enqueueing:" << u.toString() + qDebug() << "Download in progress! Enqueueing:" << url.toString() << "(" << dlQ.size() << "in queue)"; - dlQ.enqueue(u); + dlQ.enqueue(url); return; } // No running downloads: enqueue and start next download. - dlQ.enqueue(u); - qDebug() << "Enqueueing:" << u.toString() << endl; + dlQ.enqueue(url); + qDebug() << "Enqueueing:" << url.toString() << endl; startNextDownload(); } // ---------------------------------------------------------------------------------------- @@ -36,14 +35,21 @@ void DownloadManager::startNextDownload() } // Dequeue next URL to download. QUrl url = dlQ.dequeue(); - // Extract the filename from the URL - QString path = url.path(); - QString basename = QFileInfo(path).fileName(); + QString filepath = url.path(); + // Get filename from URL. + int i = filepath.lastIndexOf(QChar('/')); + filepath.remove(0, i + 1); + qDebug() << "From: " << url.toString() << endl + << "Parsed: " << filepath; + outfile.setFileName(filepath); + /* + qDebug() << "url.path() is:" << url.path(); if (basename.isEmpty()) this->filename = "download"; else this->filename = basename; outfile.setFileName(this->filename); + */ // If error upon opening, skip this file. if (!outfile.open(QIODevice::WriteOnly)) { @@ -72,7 +78,7 @@ void DownloadManager::startNextDownload() void DownloadManager::downloadReady() { // readyRead() fired, so save the readable data. - outfile.write(this->currentDownload->readAll()); + outfile.write(currentDownload->readAll()); } // ---------------------------------------------------------------------------------------- // This slot listens to the downloadProgress(..) @@ -95,32 +101,25 @@ void DownloadManager::downloadFinished() qDebug() << "Downloaded " << currentDownload->url().toString() << endl; // Close output file. outfile.close(); + currentDownload->deleteLater(); + ++downloaded; // If queue is empty, we are done. - // TODO: not sure if this is actually needed... dip = false; if (dlQ.isEmpty()) - qDebug() << "dlQ empty! returning..."; return; - // Queue not empty. - - // Delete current reply object. - currentDownload->deleteLater(); - // Initialise next download. - ++downloaded; - // qDebug() << "DM downloaded " << downloaded << "files"; + // Queue not empty: initialise next download. startNextDownload(); } // ---------------------------------------------------------------------------------------- // Constructor. -DownloadManager::DownloadManager(const QUrl& baseUrl) +DownloadManager::DownloadManager() { - this->qnam = new QNetworkAccessManager(); - this->baseUrl = baseUrl; + qnam = new QNetworkAccessManager(); dip = false; downloaded = 0; } // Destructor. DownloadManager::~DownloadManager() { - delete[] this->qnam; + delete[] qnam; } diff --git a/src/DownloadManager.h b/src/DownloadManager.h index 5007b27..9141330 100644 --- a/src/DownloadManager.h +++ b/src/DownloadManager.h @@ -10,20 +10,18 @@ class DownloadManager : public QObject Q_OBJECT public: - DownloadManager(const QUrl& baseUrl); + DownloadManager(); ~DownloadManager(); - void processDownloadRequest(QString& filename); + void processDownloadRequest(QUrl& url); int downloaded; private: // Object required for downloading. QNetworkAccessManager* qnam; - QUrl baseUrl; QQueue<QUrl> dlQ; QNetworkRequest request; QNetworkReply* currentDownload; QFile outfile; - QString filename; // Download-in-progress flag. bool dip; @@ -33,7 +31,7 @@ signals: void updateProgress(int i); public slots: - void downloadFile(QString name); + void downloadFile(QUrl& fileUrl); private slots: void startNextDownload(); diff --git a/src/JSObject.cpp b/src/JSObject.cpp index 3624a89..062f4cc 100644 --- a/src/JSObject.cpp +++ b/src/JSObject.cpp @@ -23,7 +23,18 @@ void JSObject::attachToDOM() //------------------------------------------------------------------------------------------------------- void JSObject::startDownload(QString filename) { - emit downloadFile(filename); + + if (filename.isEmpty()) + { + owner->evaluateJavaScript("alert(\"No filename!\")"); + return; + } + // Erm fix this + QString qs = "http://132.230.4.3/"; + qs.append(filename); + QUrl url(qs); + emit downloadFile(url); + } //------------------------------------------------------------------------------------------------------- void JSObject::updateProgress(int i) diff --git a/src/JSObject.h b/src/JSObject.h index 282b232..b012958 100644 --- a/src/JSObject.h +++ b/src/JSObject.h @@ -45,7 +45,7 @@ public: // the signals will be connected in the fbbrowser class with slots of the fbbrowser class signals: - void downloadFile(QString filename); + void downloadFile(QUrl& urlToFile); void signalQuitAll(); /* // should be the last signal to be emited. diff --git a/src/fbbrowser.cpp b/src/fbbrowser.cpp index 2b8bc7a..6528cc8 100644 --- a/src/fbbrowser.cpp +++ b/src/fbbrowser.cpp @@ -45,8 +45,8 @@ fbbrowser::fbbrowser(const QUrl & url) QObject::connect(jso, SIGNAL(signalQuitAll()), this, SLOT(quit())); // Initialize Download Manager. - dm = new DownloadManager(baseUrl); - QObject::connect(jso, SIGNAL(downloadFile(QString)), dm, SLOT(downloadFile(QString))); + dm = new DownloadManager(); + QObject::connect(jso, SIGNAL(downloadFile(QUrl&)), dm, SLOT(downloadFile(QUrl&))); QObject::connect(dm, SIGNAL(updateProgress(int)), jso, SLOT(updateProgress(int))); // Remove the window decoration, form to fullscreen, central view? |
