// Copyright © 2010-2012 b0ris //. // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. //. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. //. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include "DownloadClient.h" #include "Utility.h" DownloadClient::DownloadClient(): isDownloading(false), current(NULL), currentId(-1) { } DownloadClient::DownloadClient(QString addr): SslClient(addr), isDownloading(false), current(NULL), currentId(-1) { } 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; } connect(this, SIGNAL(ReplyRecieved(SslClient::RequestType&, QByteArray&)), this, SLOT(FileDataRecieved(SslClient::RequestType&, QByteArray&))); isDownloading = true; files = fileList; DoDownload(); } void DownloadClient::DoDownload() { currentId++; if (currentId < (int)files.size()) { pair file = files[currentId]; QString filename = QString::fromLocal8Bit(file.first.c_str()); string md5 = file.second; current = new QFile(filename); if (! current->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) { Logger::Info("Can't open file: path isn't available\n"); DoDownload(); } Logger::Info("Downloading file (%d of %u): %s\n", currentId + 1, files.size(), file.first.c_str()); SendFileRequest(file.first); } else { Logger::Info("All files have been downloaded.\n"); isDownloading = false; emit downloadFinished(); } } void DownloadClient::FileDataRecieved(SslClient::RequestType &type, QByteArray &data) { Logger::Trace("Recieved file data: %u bytes\n", data.size()); if (type != RegularFile) { Logger::Error("Invalid reply recieved: %x\n", type); return; } if ((current == NULL) || (! current->isOpen())) { Logger::Error("File isn't open or in invalid state.\n"); return; } if (data.size() != 0) { current->write(data); current->waitForBytesWritten(-1); } else { Logger::Trace("Last (empty) packet recieved\n"); current->close(); // check file's md5 hash value pair file = files[currentId]; QString md5string = QString::fromLocal8Bit(file.second.c_str()); QString filename = QString::fromLocal8Bit(file.first.c_str()); QByteArray md5 = md5_sum(filename); if (md5 == md5string) { Logger::Trace("File %s was downloaded correctly!\n", filename.toStdString().c_str()); } else { Logger::Error("File %s was downloaded incorrectly! Removing and trying again!\n", filename.toStdString().c_str()); current->remove(); currentId--; } // free pointer to the current file delete current; current = NULL; // proceed to the next file DoDownload(); } }