diff options
| author | Niklas Goby | 2011-03-21 12:03:04 +0100 |
|---|---|---|
| committer | Niklas Goby | 2011-03-21 12:03:04 +0100 |
| commit | f2b5a82610ea4b9de9ff5f3ae83cd1715e79bc24 (patch) | |
| tree | 7234b2e8f2083fe23193bc1654c70ec395d20b12 /src/downloadManager.cpp | |
| parent | some changes in the fbgui, ipWatcher (diff) | |
| parent | and the files ^^ (diff) | |
| download | fbgui-f2b5a82610ea4b9de9ff5f3ae83cd1715e79bc24.tar.gz fbgui-f2b5a82610ea4b9de9ff5f3ae83cd1715e79bc24.tar.xz fbgui-f2b5a82610ea4b9de9ff5f3ae83cd1715e79bc24.zip | |
Merge branch 'master' of git.openslx.org:lsfks/master-teamprojekt/fbgui
Conflicts:
src/fbgui.cpp
Diffstat (limited to 'src/downloadManager.cpp')
| -rw-r--r-- | src/downloadManager.cpp | 202 |
1 files changed, 0 insertions, 202 deletions
diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp deleted file mode 100644 index 1271a8c..0000000 --- a/src/downloadManager.cpp +++ /dev/null @@ -1,202 +0,0 @@ -#include "downloadManager.h" -#include "fbgui.h" -#include <QFileInfo> -#include <QByteArray> - -int downloadManager::downloaded = 0; -// ---------------------------------------------------------------------------------------- -downloadManager::downloadManager() -{ - logView->write(QString("testing @ DM init")); - qnam = new QNetworkAccessManager(); - dip = false; - downloadDir = QDir(downloadPath); - /* check if downloadPath exists, if not create it. */ - if (!downloadDir.exists()){ - if (debug) qDebug() << "Download directory: " << downloadDir.path() << "doesn't exist."; - QDir::current().mkdir(downloadPath); - if (downloadDir.exists() && debug) - qDebug() << "Created download directory: " << downloadDir.path(); - } - else if (debug) qDebug() << "Download directory: " << downloadDir.path() << "exists."; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFile(QString& filename) -{ - if (debug) qDebug() << "Received downloadFile signal for:" << filename; - QUrl fileUrl; - fileUrl = baseURL.resolved(QUrl(filename)); - if (debug) qDebug() << "fileUrl: " << fileUrl; - this->processDownloadRequest(fileUrl); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFile(QUrl& fileUrl) -{ - if (debug) qDebug() << "Received downloadFile signal for:" << fileUrl; - this->processDownloadRequest(fileUrl); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::processDownloadRequest(QUrl& url) -{ - if (url.isEmpty()){ - if (debug) qDebug() << "No URL specified for download."; - return; - } - /* if download in progress, enqueue file and return. */ - if (dip) - { - if (debug) qDebug() << "Download in progress! Enqueueing:" << url.toString() - << "(" << dlQ.size() << "in queue)"; - dlQ.enqueue(url); - return; - } - /* no running downloads: enqueue and start next download. */ - dlQ.enqueue(url); - if (debug) qDebug() << "Enqueueing:" << url.toString() << endl; - startNextDownload(); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::startNextDownload() -{ - - if (dlQ.isEmpty()) - { - emit downloadQueueEmpty(); - if (debug) qDebug() << "Download manager ready. (1)"; - return; - } - if (debug) qDebug() << "Starting next download: " << dlQ.head().toString() - << "(" << dlQ.size() << "in queue.)"; - - /* dequeue next URL to download. */ - QUrl url = dlQ.dequeue(); - - /* get temporary filename from URL. */ - QString tmp = url.path(); - tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); - - /* check if filename exists on target file system */ - QFileInfo fi(downloadPath + "/" + tmp); - if (fi.exists()){ - QString qs = QString(fi.absoluteFilePath() + ".\%1").arg(downloaded); - outfile.setFileName(qs); - } - else - outfile.setFileName(downloadPath + "/" + tmp); - - if (!outfile.open(QIODevice::WriteOnly)){ - if (debug) qDebug() << "Couldn't open file! Skipping..."; - return; - } - - /* send the request for the file */ - QNetworkRequest request(url); - currentDownload = qnam->get(request); - lastProgress = 0; - currentProgress = 0; - dip = true; - dltime.start(); - QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady())); - QObject::connect(currentDownload, SIGNAL(metaDataChanged()), this, SLOT(processMetaInfo())); - QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)), - this, SLOT(downloadProgress(qint64, qint64))); - QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished())); -} -// ---------------------------------------------------------------------------------------- -// Private slots -// ---------------------------------------------------------------------------------------- -void downloadManager::processMetaInfo(){ - /* fetch filesize from header & filename from url (for now) */ - const QByteArray cltag = "Content-Length"; - QByteArray clinfo = currentDownload->rawHeader(cltag); - QFileInfo fi(outfile); - emit downloadInfo(outfile.fileName(), clinfo.toDouble()); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadReady() -{ - /* data ready, save it */ - outfile.write(currentDownload->readAll()); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) -{ - // "fix" for the weird bytesTotal = -1 initial reading... - if (bytesIn > bytesTotal) - return; - /* calculate current speed */ - double speed = bytesIn * 1000 / dltime.elapsed(); - QString unit; - if (speed < 1024) { - unit = "bytes/sec"; - } - else if (speed < 1024*1024) { - speed /= 1024; - unit = "KB/s"; - } - else { - speed /= 1024*1024; - unit = "MB/s"; - } - /* update progress only if difference higher than the updateInterval setting */ - currentProgress = ((bytesIn * 100) / bytesTotal); - if (currentProgress - lastProgress >= updateInterval){ - lastProgress = currentProgress; - emit updateProgress(currentProgress, speed, unit); - if (debug) qDebug() << "Download progress of " << currentDownload->url().toString() - << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)"; - } - return; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFinished() -{ - /* check for errors */ - if (currentDownload->error()){ - if (debug) qDebug() << "Download of" << currentDownload->url().toString() - << "failed with status code: " << currentDownload->error(); - currentDownload->deleteLater(); - outfile.remove(); - return; - } - // TODO Handle errors. - if (debug) qDebug() << "NetworkCode: " << currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - /* end download */ - outfile.close(); - currentDownload->deleteLater(); - downloaded++; - dip = false; - if (debug) qDebug() << "Download of " << currentDownload->url().toString() - << "finished. (dlcount = "<< downloaded << ")"; - if (dlQ.isEmpty()){ - emit downloadQueueEmpty(); - if (debug) qDebug() << "Download manager ready. (2)"; - return; - } - 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)) { - if (debug) qDebug() << "Renamed file!"; -} -else { - if (debug) qDebug() << "Failure to rename file!"; -} -*/ |
