src/partdownloadrequest.cpp

Wed, 10 Jan 2018 22:54:09 +0200

author
Santeri Piippo
date
Wed, 10 Jan 2018 22:54:09 +0200
changeset 1226
d1199d965235
parent 1222
34def2630300
child 1228
ecb6ea961b1e
permissions
-rw-r--r--

drop Qt4 support

994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1222
34def2630300 happy new year 2018
Santeri Piippo
parents: 1217
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <QDir>
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <QNetworkAccessManager>
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include <QNetworkRequest>
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include <QNetworkReply>
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include <QTableWidget>
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24 #include <QProgressBar>
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #include <QLabel>
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #include "partdownloader.h"
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #include "partdownloadrequest.h"
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 #include "mainwindow.h"
998
5be0ce31ce60 Now compiles again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 994
diff changeset
29 #include "documentmanager.h"
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
31 PartDownloadRequest::PartDownloadRequest(QString url, QString dest, bool primary, PartDownloader* parent) :
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
32 QObject(parent),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
33 HierarchyElement(parent),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
34 m_state(State::Requesting),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
35 m_prompt(parent),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
36 m_url(url),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
37 m_destination(dest),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
38 m_filePath(parent->downloadPath() + DIRSLASH + dest),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
39 m_networkManager(new QNetworkAccessManager),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
40 m_isFirstUpdate(true),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
41 m_isPrimary(primary),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
42 m_filePointer(nullptr)
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 // Make sure that we have a valid destination.
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
45 QString dirpath = Dirname(filePath());
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
46 QDir dir(dirpath);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 if (not dir.exists())
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
50 print("Creating %1...\n", dirpath);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
52 if (not dir.mkpath(dirpath))
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
53 Critical(format(tr("Couldn't create the directory %1!"), dirpath));
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
56 m_networkReply = m_networkManager->get(QNetworkRequest(QUrl(url)));
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
57 connect(networkReply(), SIGNAL(finished()), this, SLOT(downloadFinished()));
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
58 connect(networkReply(), SIGNAL(readyRead()), this, SLOT(readFromNetworkReply()));
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
59 connect(networkReply(), SIGNAL(downloadProgress(qint64, qint64)),
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
60 this, SLOT(updateDownloadProgress(qint64, qint64)));
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 PartDownloadRequest::~PartDownloadRequest() {}
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 bool PartDownloadRequest::failed() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 return m_state == State::Failed;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 int PartDownloadRequest::tableRow() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 return m_tableRow;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
75 void PartDownloadRequest::setTableRow(int value)
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 m_tableRow = value;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 PartDownloader* PartDownloadRequest::prompt() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 return m_prompt;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 QString PartDownloadRequest::url() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 return m_url;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 QString PartDownloadRequest::destination() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 return m_destination;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 QString PartDownloadRequest::filePath() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 return m_filePath;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 bool PartDownloadRequest::isFirstUpdate() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 return m_isFirstUpdate;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 qint64 PartDownloadRequest::numBytesRead() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 return m_numBytesRead;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 qint64 PartDownloadRequest::numBytesTotal() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 return m_numBytesTotal;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 bool PartDownloadRequest::isPrimary() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 return m_isPrimary;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 QNetworkReply* PartDownloadRequest::networkReply() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 return m_networkReply;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 void PartDownloadRequest::updateToTable()
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 QTableWidget* table = prompt()->progressTable();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
129 switch(m_state)
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 case State::Requesting:
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 case State::Downloading:
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
134 QWidget* cellwidget = table->cellWidget(tableRow(), PartDownloader::ProgressColumn);
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
135 QProgressBar* progressBar = qobject_cast<QProgressBar*>(cellwidget);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 if (not progressBar)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
139 progressBar = new QProgressBar;
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
140 table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, progressBar);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
143 progressBar->setRange(0, numBytesTotal());
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
144 progressBar->setValue(numBytesRead());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
145 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
146 break;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
147
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
148 case State::Finished:
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
149 case State::Failed:
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
150 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
151 const QString text = (m_state == State::Finished)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
152 ? "<b><span style=\"color: #080\">FINISHED</span></b>"
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
153 : "<b><span style=\"color: #800\">FAILED</span></b>";
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
154
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
155 QLabel* lb = new QLabel(text);
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
156 lb->setAlignment(Qt::AlignCenter);
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
157 table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, lb);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
158 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
159 break;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
160 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
161
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
162 QLabel* label = qobject_cast<QLabel*>(table->cellWidget(tableRow(), PartDownloader::PartLabelColumn));
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
163
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
164 if (isFirstUpdate())
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
165 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
166 label = new QLabel(format("<b>%1</b>", destination()), table);
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
167 table->setCellWidget(tableRow(), PartDownloader::PartLabelColumn, label);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
168 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
169
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
170 // Make sure that the cell is big enough to contain the label
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
171 if (table->columnWidth(PartDownloader::PartLabelColumn) < label->width())
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
172 table->setColumnWidth(PartDownloader::PartLabelColumn, label->width());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
173
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
174 m_isFirstUpdate = false;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
175 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 void PartDownloadRequest::downloadFinished()
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 if (networkReply()->error() != QNetworkReply::NoError)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181 if (isPrimary() and not prompt()->isAborted())
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
182 Critical(networkReply()->errorString());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
183
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
184 print("Unable to download %1: %2\n", destination(), networkReply()->errorString());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
185 m_state = State::Failed;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
186 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
187 else if (m_state != State::Failed)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
188 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
189 m_state = State::Finished;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
190 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
191
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
192 m_numBytesRead = numBytesTotal();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
193 updateToTable();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
194
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
195 if (m_filePointer)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
196 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
197 m_filePointer->close();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
198 delete m_filePointer;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
199 m_filePointer = nullptr;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
200
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
201 if (m_state == State::Failed)
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
202 QFile::remove(filePath());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
203 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
204
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
205 if (m_state != State::Finished)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
206 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
207 prompt()->checkIfFinished();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
208 return;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
209 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
210
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
211 // Try to load this file now.
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
212 LDDocument* document = m_documents->openDocument(filePath(), false, not isPrimary());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
213
998
5be0ce31ce60 Now compiles again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 994
diff changeset
214 if (document == nullptr)
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
215 return;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
216
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
217 // Iterate through this file and check for errors. If there's any that stems
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
218 // from unknown file references, try resolve that by downloading the reference.
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
219 // This is why downloading a part may end up downloading multiple files, as
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
220 // it resolves dependencies.
998
5be0ce31ce60 Now compiles again
Teemu Piippo <crimsondusk64@gmail.com>
parents: 994
diff changeset
221 for (LDObject* obj : document->objects())
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
222 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
223 LDError* err = dynamic_cast<LDError*>(obj);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
225 if (err == nullptr or err->fileReferenced().isEmpty())
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
226 continue;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
227
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
228 QString dest = err->fileReferenced();
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
229 prompt()->downloadFromPartsTracker(dest);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
230 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
231
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
232 prompt()->addFile(document);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
233
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
234 if (isPrimary())
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
235 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
236 m_documents->addRecentFile(filePath());
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
237 prompt()->setPrimaryFile(document);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 prompt()->checkIfFinished();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
242
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
243 void PartDownloadRequest::updateDownloadProgress(int64 recv, int64 total)
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
244 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
245 m_numBytesRead = recv;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
246 m_numBytesTotal = total;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
247 m_state = State::Downloading;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
248 updateToTable();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
249 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
250
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
251 void PartDownloadRequest::readFromNetworkReply()
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
252 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
253 if (m_state == State::Failed)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
254 return;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
255
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
256 if (m_filePointer == nullptr)
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
257 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
258 m_filePath.replace("\\", "/");
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
259
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
260 // We have already asked the user whether we can overwrite so we're good to go here.
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
261 m_filePointer = new QFile(filePath().toLocal8Bit());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
262
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
263 if (not m_filePointer->open(QIODevice::WriteOnly))
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
264 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
265 Critical(format(tr("Couldn't open %1 for writing: %2"), filePath(), strerror(errno)));
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
266 m_state = State::Failed;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
267 networkReply()->abort();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
268 updateToTable();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
269 prompt()->checkIfFinished();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
270 return;
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
271 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
272 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
273
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
274 m_filePointer->write(networkReply()->readAll());
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
275 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
276
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
277 bool PartDownloadRequest::isFinished() const
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
278 {
1217
314e12e23c3a fix paren style
Santeri Piippo
parents: 998
diff changeset
279 return isOneOf(m_state, State::Finished, State::Failed);
994
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
280 }
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
281
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
282 void PartDownloadRequest::abort()
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
283 {
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
284 networkReply()->abort();
09e1a3e272ec Split PartDownloadRequest into its own file
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
285 }

mercurial