Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "simpledownloader.h"
- #include <QEventLoop>
- SimpleDownloader::SimpleDownloader()
- {
- //QEventLoop *loop = new QEventLoop();
- //bool mc = connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(DownloadFinished(QNetworkReply*)));
- //loop->exec();
- //loop.deleteLater();
- //qWarning("Manager Finished Connect: %d", mc);
- }
- void SimpleDownloader::InitDownload(QString url, QString saveDirectory) {
- QUrl urlU(url);
- QNetworkRequest request(urlU);
- QNetworkReply *reply = manager.get(request);
- qWarning("Request Started");
- //QEventLoop *loop = new QEventLoop();
- bool rc = reply->connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(DownloadProgress(qint64,qint64)));
- bool rc2 = reply->connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(DownloadError(QNetworkReply::NetworkError)));
- bool rc3 = reply->connect(reply, SIGNAL(finished()), this, SLOT(ReplyDownloadFinished()));
- //bool rc3 = reply->connect(reply, SIGNAL(readyRead()), this, SLOT(ReplyDownloadFinished()));
- //loop->exec();
- qWarning("Reply Progress Connect: %d | Reply error connect: %d", rc, rc2);
- this->currentDownload = url;
- this->currentSaveDirectory = saveDirectory;
- this->reply = reply;
- timer = new QTime();
- timer->start();
- }
- void SimpleDownloader::DownloadFinished(QNetworkReply *reply) {
- if (reply->error() == QNetworkReply::NoError) {
- qWarning("Download Finished");
- QUrl url = reply->url();
- delete timer;
- if (reply->error()) {
- qCritical("Download Error.");
- emit Finished("", false, "There was an error downloading the file.");
- } else {
- QString filename = saveFileName(url);
- if (saveReply(filename, reply)) {
- qWarning("Download succeeded.");
- emit Finished(filename, true, "");
- } else {
- qWarning("Download failed.");
- emit Finished(filename, false, "Could not write to the download file. Please make sure it exists.");
- }
- }
- } else {
- qWarning("Download error.");
- }
- }
- void SimpleDownloader::ReplyDownloadFinished() {
- if (reply->error() == QNetworkReply::NoError) {
- qWarning("Download Finished");
- QUrl url = this->reply->url();
- delete timer;
- if (reply->error()) {
- qCritical("Download Error.");
- emit Finished("", false, "There was an error downloading the file.");
- } else {
- QString filename = saveFileName(url);
- if (saveReply(filename, reply)) {
- qWarning("Download succeeded.");
- emit Finished(filename, true, "");
- } else {
- qWarning("Download failed.");
- emit Finished(filename, false, "Could not write to the download file. Please make sure it exists.");
- }
- }
- } else {
- qWarning("Download error.");
- }
- }
- bool SimpleDownloader::saveReply(QString filename, QIODevice *data) {
- QFile file(filename);
- QFileInfo fileInformation(file);
- QDir dir(fileInformation.dir());
- dir.mkpath(dir.path());
- if (file.open(QFile::WriteOnly)) {
- file.write(data->readAll());
- file.close();
- return true;
- } else
- return false;
- }
- void SimpleDownloader::DownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
- // Convert to integers.
- int br = bytesReceived;
- int bt = bytesTotal;
- // Get speed. Borrowed from QT Documentation.
- double speed = bytesReceived * 1000.0 / timer->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";
- }
- QString speedString = QString::number(speed);
- emit Progress(br, bt, "Downloading at " + speedString + " " + unit);
- }
- QString SimpleDownloader::saveFileName(QUrl url) {
- // Borrowed from the QT Examples
- QString path = url.path();
- QString basename = QFileInfo(path).fileName();
- if (basename.isEmpty())
- basename = "download";
- if (QFile::exists(basename)) {
- // already exists, don't overwrite
- int i = 0;
- basename += '.';
- while (QFile::exists(basename + QString::number(i)))
- ++i;
- basename += QString::number(i);
- }
- basename = basename.prepend(this->currentSaveDirectory + "/");
- return basename;
- }
- void SimpleDownloader::DownloadError(QNetworkReply::NetworkError error) {
- qCritical("Network Error");
- //qCritical(error);
- }
Add Comment
Please, Sign In to add comment