// 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 "FileOpThread.h" #include "UpdatedConfig.h" #include "Utility.h" FileOpThread::~FileOpThread() { if (downloadClient != NULL) delete downloadClient; } void FileOpThread::run() { UpdatedConfig *cfg = UpdatedConfig::CurrentConfig(); /* delete files */ vector toDelete = cfg->GetDeleteList(); for (unsigned i = 0; i < toDelete.size(); i++) { Logger::Trace("Deleting file: \"%s\"\n", toDelete[i].path.c_str()); QString path = QString::fromLocal8Bit(toDelete[i].path.c_str()); QFile file(path); QFileInfo fileInfo(file); if (fileInfo.exists()) { file.remove(); } else { Logger::Info("File \"%s\" has already been deleted\n", toDelete[i].path.c_str()); } } /* download files */ vector > files; vector downloadList = cfg->GetDownloadList(); for (unsigned i = 0; i < downloadList.size(); i++) { QString path = QString::fromLocal8Bit(downloadList[i].path.c_str()); QFileInfo fileInfo(path); if (fileInfo.exists()) { Logger::Info("File \"%s\" already exists\n", downloadList[i].path.c_str()); QString filename = QString::fromLocal8Bit(downloadList[i].path.c_str()); QString md5string = QString::fromLocal8Bit(downloadList[i].md5.c_str()); QByteArray md5 = md5_sum(filename); if (md5 == md5string) { Logger::Trace("File %s is correct!\n", filename.toStdString().c_str()); } else { Logger::Error("File %s is incorrect or was updated. Need to download again!\n", filename.toStdString().c_str()); pair file = make_pair(downloadList[i].path, downloadList[i].md5); files.push_back(file); } } else { pair file = make_pair(downloadList[i].path, downloadList[i].md5); files.push_back(file); } } if (files.size() != 0) { Logger::Debug("Setting server address to %s\n", cfg->GetServerAddr().c_str()); QString addr = QString::fromLocal8Bit(cfg->GetServerAddr().c_str()); downloadClient = new DownloadClient(addr); // start downloading files and // wait for files to be downloaded QEventLoop loop; connect(downloadClient, SIGNAL(downloadFinished()), &loop, SLOT(quit())); downloadClient->Download(files); loop.exec(QEventLoop::ExcludeUserInputEvents); } else { Logger::Trace("No files to download from server.\n"); } Logger::Trace("Terminating Download Helper thread\n"); }