sammarks

simpledownloader.cpp

Apr 14th, 2011
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "simpledownloader.h"
  2.  
  3. #include <QEventLoop>
  4.  
  5. SimpleDownloader::SimpleDownloader()
  6. {
  7.     //QEventLoop *loop = new QEventLoop();
  8.     //bool mc = connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(DownloadFinished(QNetworkReply*)));
  9.     //loop->exec();
  10.     //loop.deleteLater();
  11.     //qWarning("Manager Finished Connect: %d", mc);
  12. }
  13.  
  14. void SimpleDownloader::InitDownload(QString url, QString saveDirectory) {
  15.     QUrl urlU(url);
  16.     QNetworkRequest request(urlU);
  17.     QNetworkReply *reply = manager.get(request);
  18.     qWarning("Request Started");
  19.     //QEventLoop *loop = new QEventLoop();
  20.     bool rc = reply->connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(DownloadProgress(qint64,qint64)));
  21.     bool rc2 = reply->connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(DownloadError(QNetworkReply::NetworkError)));
  22.     bool rc3 = reply->connect(reply, SIGNAL(finished()), this, SLOT(ReplyDownloadFinished()));
  23.     //bool rc3 = reply->connect(reply, SIGNAL(readyRead()), this, SLOT(ReplyDownloadFinished()));
  24.     //loop->exec();
  25.     qWarning("Reply Progress Connect: %d | Reply error connect: %d", rc, rc2);
  26.  
  27.     this->currentDownload = url;
  28.     this->currentSaveDirectory = saveDirectory;
  29.     this->reply = reply;
  30.  
  31.     timer = new QTime();
  32.     timer->start();
  33. }
  34.  
  35. void SimpleDownloader::DownloadFinished(QNetworkReply *reply) {
  36.     if (reply->error() == QNetworkReply::NoError) {
  37.         qWarning("Download Finished");
  38.         QUrl url = reply->url();
  39.         delete timer;
  40.         if (reply->error()) {
  41.             qCritical("Download Error.");
  42.             emit Finished("", false, "There was an error downloading the file.");
  43.         } else {
  44.             QString filename = saveFileName(url);
  45.             if (saveReply(filename, reply)) {
  46.                 qWarning("Download succeeded.");
  47.                 emit Finished(filename, true, "");
  48.             } else {
  49.                 qWarning("Download failed.");
  50.                 emit Finished(filename, false, "Could not write to the download file. Please make sure it exists.");
  51.             }
  52.         }
  53.     } else {
  54.         qWarning("Download error.");
  55.     }
  56. }
  57.  
  58. void SimpleDownloader::ReplyDownloadFinished() {
  59.     if (reply->error() == QNetworkReply::NoError) {
  60.         qWarning("Download Finished");
  61.         QUrl url = this->reply->url();
  62.         delete timer;
  63.         if (reply->error()) {
  64.             qCritical("Download Error.");
  65.             emit Finished("", false, "There was an error downloading the file.");
  66.         } else {
  67.             QString filename = saveFileName(url);
  68.             if (saveReply(filename, reply)) {
  69.                 qWarning("Download succeeded.");
  70.                 emit Finished(filename, true, "");
  71.             } else {
  72.                 qWarning("Download failed.");
  73.                 emit Finished(filename, false, "Could not write to the download file. Please make sure it exists.");
  74.             }
  75.         }
  76.     } else {
  77.         qWarning("Download error.");
  78.     }
  79. }
  80.  
  81. bool SimpleDownloader::saveReply(QString filename, QIODevice *data) {
  82.     QFile file(filename);
  83.     QFileInfo fileInformation(file);
  84.     QDir dir(fileInformation.dir());
  85.     dir.mkpath(dir.path());
  86.     if (file.open(QFile::WriteOnly)) {
  87.         file.write(data->readAll());
  88.         file.close();
  89.         return true;
  90.     } else
  91.         return false;
  92. }
  93.  
  94. void SimpleDownloader::DownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
  95.     // Convert to integers.
  96.     int br = bytesReceived;
  97.     int bt = bytesTotal;
  98.  
  99.     // Get speed. Borrowed from QT Documentation.
  100.     double speed = bytesReceived * 1000.0 / timer->elapsed();
  101.     QString unit;
  102.     if (speed < 1024) {
  103.         unit = "bytes/sec";
  104.     } else if (speed < 1024*1024) {
  105.         speed /= 1024;
  106.         unit = "kB/s";
  107.     } else {
  108.         speed /= 1024*1024;
  109.         unit = "MB/s";
  110.     }
  111.  
  112.     QString speedString = QString::number(speed);
  113.     emit Progress(br, bt, "Downloading at " + speedString + " " + unit);
  114. }
  115.  
  116. QString SimpleDownloader::saveFileName(QUrl url) {
  117.     // Borrowed from the QT Examples
  118.  
  119.     QString path = url.path();
  120.     QString basename = QFileInfo(path).fileName();
  121.  
  122.     if (basename.isEmpty())
  123.         basename = "download";
  124.  
  125.     if (QFile::exists(basename)) {
  126.         // already exists, don't overwrite
  127.         int i = 0;
  128.         basename += '.';
  129.         while (QFile::exists(basename + QString::number(i)))
  130.             ++i;
  131.  
  132.         basename += QString::number(i);
  133.     }
  134.  
  135.     basename = basename.prepend(this->currentSaveDirectory + "/");
  136.  
  137.     return basename;
  138. }
  139.  
  140. void SimpleDownloader::DownloadError(QNetworkReply::NetworkError error) {
  141.     qCritical("Network Error");
  142.     //qCritical(error);
  143. }
Add Comment
Please, Sign In to add comment