-
Notifications
You must be signed in to change notification settings - Fork 4
/
cdownloadfile.cpp
96 lines (78 loc) · 2.05 KB
/
cdownloadfile.cpp
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
#include "cdownloadfile.h"
#include "ui_cdownloadfile.h"
#include "curlfile.h"
#include "skcore.h"
#include <QDebug>
CDownloadFile::CDownloadFile(QWidget *parent, const QString &urlFile) :
QDialog(parent),
ui(new Ui::CDownloadFile)
{
ui->setupUi(this);
CUrlFile u;
QList <urlItem_t> url;
u.readFile(urlFile, &url);
foreach (urlItem_t item, url)
{
ui->comboBox->addItem(item.name, item.url);
}
}
CDownloadFile::~CDownloadFile()
{
delete ui;
}
void CDownloadFile::on_pushButton_clicked()
{
done(DL_CANCEL);
}
void CDownloadFile::on_pushButton_2_clicked()
{
if (ui->comboBox->currentIndex() == -1)
{
return;
}
QNetworkRequest request(ui->comboBox->currentData().toString());
QNetworkReply *reply = m_manager.get(request);
m_reply = reply;
m_current = 0;
ui->pushButton_2->setEnabled(false);
ui->comboBox->setEnabled(false);
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(slotProgress(qint64,qint64)));
connect(&m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotDownloadFinished(QNetworkReply*)));
}
void CDownloadFile::slotReadyRead()
{
m_data += m_reply->readAll();
}
void CDownloadFile::slotDownloadFinished(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QFileDialog dlg(this);
m_filePath = dlg.getSaveFileName(this, tr("Save TLE File"), QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/data/catalogue/tle/unnamed.tle", "TLE (*.tle)");
if (!m_filePath.isEmpty())
{
QFile f(m_filePath);
if (f.open(SkFile::WriteOnly))
{
f.write(m_data);
f.close();
}
}
done(DL_OK);
}
else
{ // error
if (reply->error() != QNetworkReply::OperationCanceledError)
{
msgBoxError(this, reply->errorString());
}
}
reply->deleteLater();
done(DL_OK);
}
void CDownloadFile::slotProgress(qint64 current, qint64 total)
{
ui->progressBar->setRange(0, total / 1000);
ui->progressBar->setValue(current / 1000);
}