#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(); } }