From a19e9744751d4278f56cb3b6ff885c0068f03a3d Mon Sep 17 00:00:00 2001 From: Alex Borisov Date: Sun, 27 Nov 2011 12:31:03 +0200 Subject: File downloading (no MD5 hash check yet). Various fixes --- client/DownloadClient.cpp | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 client/DownloadClient.cpp (limited to 'client/DownloadClient.cpp') diff --git a/client/DownloadClient.cpp b/client/DownloadClient.cpp new file mode 100644 index 0000000..df4da25 --- /dev/null +++ b/client/DownloadClient.cpp @@ -0,0 +1,110 @@ + +#include +#include "DownloadClient.h" + + +DownloadClient::DownloadClient(): isDownloading(false), current(NULL), currentId(-1) +{ + connect(this, SIGNAL(ReplyRecieved(SslClient::RequestType&, QByteArray&)), + this, SLOT(FileDataRecieved(SslClient::RequestType&, QByteArray&))); +} + +DownloadClient::DownloadClient(QString addr): SslClient(addr), isDownloading(false), current(NULL), currentId(-1) +{ + DownloadClient(); +} + +void DownloadClient::Download(const string &filename) +{ + pair file(filename, 0); + vector > fileList; + fileList.push_back(file); + Download(fileList); +} + +void DownloadClient::Download(const pair &file) +{ + vector > fileList; + fileList.push_back(file); + Download(fileList); +} + +void DownloadClient::Download(const vector > &fileList) +{ + if (server.isEmpty()) + { + Logger::Error("Server address isn't set! Aborting.\n"); + return; + } + if (isDownloading) + { + Logger::Error("Previous downloading process wasn't finished! Aborting.\n"); + return; + } + isDownloading = true; + files = fileList; + DoDownload(); +} + +void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray &data) +{ + if (type != RegularFile) + { + Logger::Error("Invalid reply recieved: %x\n", type); + return; + } + + if ((current != NULL) && (current->isOpen())) + { + Logger::Error("File is not open or invalid state\n"); + return; + } + + pair file = files[currentId]; + string filename = file.first; + if (data.size() == FRAGMENT_SIZE) + { + Logger::Debug("data:\n %s", data.constData()); + current->write(data); + SendFileRequest(filename); + } + else + { + if (data.size() != 0) + { + Logger::Debug("data:\n %s", data.constData()); + current->write(data); + } + current->close(); + delete current; + current = NULL; + + /** + * @todo md5 hash check + */ + + DoDownload(); + } +} + +void DownloadClient::DoDownload() +{ + if (currentId < (int)files.size()) + { + currentId++; + pair file = files[currentId]; + QString filename = QString::fromLocal8Bit(file.first.c_str()); + string md5 = file.second; + current = new QFile(filename); + current->open(QIODevice::ReadWrite); + + Logger::Info("Downloading file %s\n", file.first.c_str()); + SendFileRequest(file.first); + } + else + { + Logger::Info("All files have been downloaded."); + isDownloading = false; + emit downloadFinished(); + } +} -- cgit v1.2.3