summaryrefslogtreecommitdiff
path: root/client/DownloadClient.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'client/DownloadClient.cpp')
-rw-r--r--client/DownloadClient.cpp110
1 files changed, 110 insertions, 0 deletions
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 <QtCore>
+#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<string, string> file(filename, 0);
+ vector<pair<string, string> > fileList;
+ fileList.push_back(file);
+ Download(fileList);
+}
+
+void DownloadClient::Download(const pair<string, string> &file)
+{
+ vector<pair<string, string> > fileList;
+ fileList.push_back(file);
+ Download(fileList);
+}
+
+void DownloadClient::Download(const vector<pair<string, string> > &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<string, string> 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<string, string> 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();
+ }
+}