summaryrefslogtreecommitdiff
path: root/client/FileOpThread.cpp
blob: 2677d2d7ac74d11c78e334b068e19726081cc3f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 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 <QtCore>
#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<Config::FileEntry> 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<pair<string, string> > files;
	vector<Config::FileEntry> 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<string, string> file = make_pair(downloadList[i].path, downloadList[i].md5);
				files.push_back(file);
			}
		}
		else
		{
			pair<string, string> 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");
}