diff options
| author | Jonathan Bauer | 2011-03-21 11:47:21 +0100 |
|---|---|---|
| committer | Jonathan Bauer | 2011-03-21 11:47:21 +0100 |
| commit | e7329f1acbb02d6f4b1ab2be66282291bff60ccf (patch) | |
| tree | db88b2fea7c36081778fe34c10d1f2883a634824 /src/downloadManager.cpp | |
| parent | small steps towards const correctness... (diff) | |
| download | fbgui-e7329f1acbb02d6f4b1ab2be66282291bff60ccf.tar.gz fbgui-e7329f1acbb02d6f4b1ab2be66282291bff60ccf.tar.xz fbgui-e7329f1acbb02d6f4b1ab2be66282291bff60ccf.zip | |
minor code convention fixes
Diffstat (limited to 'src/downloadManager.cpp')
| -rw-r--r-- | src/downloadManager.cpp | 214 |
1 files changed, 0 insertions, 214 deletions
diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp deleted file mode 100644 index 43c27e9..0000000 --- a/src/downloadManager.cpp +++ /dev/null @@ -1,214 +0,0 @@ -#include "downloadManager.h" -#include "fbgui.h" - -int downloadManager::downloaded = 0; -// ---------------------------------------------------------------------------------------- -downloadManager::downloadManager(){ - qxtLog->debug() << "Initializing download manager..."; - checkDownloadDirectory(); - qnam = new QNetworkAccessManager(); - dip = false; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::checkDownloadDirectory() -{ - // check if downloadPath exists, if not create it. - downloadDir = QDir(downloadPath); - if (!downloadDir.exists()){ - qxtLog->debug() << "Download directory: " << downloadDir.path() << " doesn't exist."; - QDir::current().mkdir(downloadPath); - if (downloadDir.exists()){ - qxtLog->debug() << "Created download directory: " << downloadDir.path(); - } - else { - qxtLog->debug() << "Failed to create directory: " << downloadDir.path(); - // try to save to /tmp/fbgui - downloadDir.setPath(QDir::tempPath () + "/fbgui"); - if (!downloadDir.exists()){ - QDir::current().mkdir(QDir::tempPath () + "/fbgui"); - if (!downloadDir.exists()){ - // TODO: dont exit, this shouldn't happen anyway (right?) - qxtLog->debug() << "Fatal, no target for downloads. Exiting..."; - exit(EXIT_FAILURE); - } - } - qxtLog->debug() << "Saving downloads to: " << downloadDir.absolutePath(); - } - } - else qxtLog->debug() << "Download directory: " << downloadDir.path() << " exists."; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFile(const QString& filename) -{ - QUrl fileUrl(baseURL.resolved(QUrl(filename))); - this->processDownloadRequest(fileUrl); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFile(const QUrl& fileUrl) -{ - this->processDownloadRequest(fileUrl); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::processDownloadRequest(const QUrl& url) -{ - if (url.isEmpty()){ - qxtLog->debug() << "No URL specified for download."; - return; - } - // if download in progress, enqueue file and return. - if (dip){ - dlQ.enqueue(url); - qxtLog->debug() << "Download in progress! Queued:" << url.toString() - << "(" << dlQ.size() << " in queue)"; - return; - } - // no running downloads: enqueue and start next download. - dlQ.enqueue(url); - qxtLog->debug() << "Enqueueing:" << url.toString(); - startNextDownload(); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::startNextDownload() -{ - if (dlQ.isEmpty()){ - emit downloadQueueEmpty(); - qxtLog->debug() << "Download manager ready. (1)"; - return; - } - qxtLog->debug() << "Starting next download: " << dlQ.head().toString() - << " (" << dlQ.size() - 1 << " in queue.)"; - - // dequeue next URL to download. - QUrl url = dlQ.dequeue(); - - // get filename from URL. - QString tmp = url.path(); - tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); - - // check if filename exists on target file system - if (downloadDir.exists(tmp)){ - qxtLog->debug() << "File already exists: " << downloadDir.absoluteFilePath(tmp); - outfile.setFileName(QString(downloadDir.absolutePath() + "/" + tmp + ".\%1").arg(downloaded)); - } - else - outfile.setFileName(downloadDir.absoluteFilePath(tmp)); - qxtLog->debug() << "Saving to: " << outfile.fileName(); - - // try to open for writing - if (!outfile.open(QIODevice::WriteOnly)){ - qxtLog->debug() << "No write access to " << outfile.fileName() << " . Skipping download..."; - 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 to process downloads -// ---------------------------------------------------------------------------------------- -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) -{ - 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); - qxtLog->debug() << "Download progress of " << currentDownload->url().toString() - << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)"; - } - return; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFinished() -{ - // check for errors - if (currentDownload->error()){ - currentDownload->deleteLater(); - outfile.remove(); - int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - qxtLog->debug() << "Download of " << currentDownload->url().toString() - << " failed with HTTP error code: " << statusCode; - emit notify(QString("Download failed! HTTP Status Code: %1").arg(statusCode)); - } - else{ - // end download - currentDownload->deleteLater(); - outfile.close(); - downloaded++; - qxtLog->debug() << "Download of " << currentDownload->url().toString() - << " finished. (downloaded = "<< downloaded << ")"; - emit notify(QString("Successfully downloaded %1").arg(currentDownload->url().toString())); - } - dip = false; - // process next in queue - if (dlQ.isEmpty()){ - emit downloadQueueEmpty(); - qxtLog->debug() << "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)) { - qxtLog->debug() << "Renamed file!"; -} -else { - qxtLog->debug() << "Failure to rename file!"; -} -*/ |
