-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.cpp
More file actions
75 lines (61 loc) · 2.03 KB
/
Copy pathloader.cpp
File metadata and controls
75 lines (61 loc) · 2.03 KB
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
#include <QDebug>
#include <QFile>
#include <iostream>
#include "loader.hpp"
namespace loader
{
Loader::Loader()
{
connect(&m_network_manager, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(OnError(QNetworkReply::NetworkError)));
}
void Loader::load(const QUrl& folder_url)
{
m_folder_reply = m_network_manager.get(QNetworkRequest(folder_url));
connect(m_folder_reply, SIGNAL(finished()), this, SLOT(OnDownloadFolder()));
qDebug() << "Loading " << folder_url.path(QUrl::ComponentFormattingOption::DecodeReserved) << "\n";
}
void Loader::OnDownloadFolder()
{
std::cout << "Downloaded" << std::endl;
QStringList files = m_parser.parse(*m_folder_reply);
const char* moex_ftp_url = "http://ftp.moex.com";
for(auto elem : files)
{
m_urls.push_back(QUrl(moex_ftp_url + elem));
}
connect(&m_network_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(OnDownloadFile(QNetworkReply*)));
download_files();
}
void Loader::download_files()
{
if(!m_urls.isEmpty())
{
m_curr_download = m_network_manager.get((QNetworkRequest(m_urls.front())));
connect(m_curr_download, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(OnDownloadProgress(qint64,qint64)));
m_urls.pop_front();
}
}
void Loader::OnDownloadFile(QNetworkReply* reply)
{
QString fname(reply->url().fileName());
QFile file(fname);
file.open(QFile::ReadWrite);
file.write(reply->readAll());
file.close();
download_files();
}
void Loader::OnDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
int percent = bytesReceived * 100 / bytesTotal;
QString fname = m_curr_download->url().fileName();
std::cout << "\r downloading... " << fname.toStdString() << " -> " << bytesReceived << " / " << bytesTotal << " (" << percent << "%)";
}
void Loader::OnError(QNetworkReply::NetworkError code)
{
std::cout << "Cause errror while downloading. Error code:" << code << std::endl;
}
void Loader::print_status()
{
qDebug() << m_network_manager.configuration().name() << "\n";
}
}