26 #include "partdownloader.h" |
26 #include "partdownloader.h" |
27 #include "partdownloadrequest.h" |
27 #include "partdownloadrequest.h" |
28 #include "mainwindow.h" |
28 #include "mainwindow.h" |
29 #include "documentmanager.h" |
29 #include "documentmanager.h" |
30 |
30 |
31 PartDownloadRequest::PartDownloadRequest (QString url, QString dest, bool primary, PartDownloader* parent) : |
31 PartDownloadRequest::PartDownloadRequest(QString url, QString dest, bool primary, PartDownloader* parent) : |
32 QObject (parent), |
32 QObject(parent), |
33 HierarchyElement (parent), |
33 HierarchyElement(parent), |
34 m_state (State::Requesting), |
34 m_state(State::Requesting), |
35 m_prompt (parent), |
35 m_prompt(parent), |
36 m_url (url), |
36 m_url(url), |
37 m_destination (dest), |
37 m_destination(dest), |
38 m_filePath (parent->downloadPath() + DIRSLASH + dest), |
38 m_filePath(parent->downloadPath() + DIRSLASH + dest), |
39 m_networkManager (new QNetworkAccessManager), |
39 m_networkManager(new QNetworkAccessManager), |
40 m_isFirstUpdate (true), |
40 m_isFirstUpdate(true), |
41 m_isPrimary (primary), |
41 m_isPrimary(primary), |
42 m_filePointer (nullptr) |
42 m_filePointer(nullptr) |
43 { |
43 { |
44 // Make sure that we have a valid destination. |
44 // Make sure that we have a valid destination. |
45 QString dirpath = Dirname (filePath()); |
45 QString dirpath = Dirname(filePath()); |
46 QDir dir (dirpath); |
46 QDir dir(dirpath); |
47 |
47 |
48 if (not dir.exists()) |
48 if (not dir.exists()) |
49 { |
49 { |
50 print ("Creating %1...\n", dirpath); |
50 print("Creating %1...\n", dirpath); |
51 |
51 |
52 if (not dir.mkpath (dirpath)) |
52 if (not dir.mkpath(dirpath)) |
53 Critical (format (tr ("Couldn't create the directory %1!"), dirpath)); |
53 Critical(format(tr("Couldn't create the directory %1!"), dirpath)); |
54 } |
54 } |
55 |
55 |
56 m_networkReply = m_networkManager->get (QNetworkRequest (QUrl (url))); |
56 m_networkReply = m_networkManager->get(QNetworkRequest(QUrl(url))); |
57 connect (networkReply(), SIGNAL (finished()), this, SLOT (downloadFinished())); |
57 connect(networkReply(), SIGNAL(finished()), this, SLOT(downloadFinished())); |
58 connect (networkReply(), SIGNAL (readyRead()), this, SLOT (readFromNetworkReply())); |
58 connect(networkReply(), SIGNAL(readyRead()), this, SLOT(readFromNetworkReply())); |
59 connect (networkReply(), SIGNAL (downloadProgress (qint64, qint64)), |
59 connect(networkReply(), SIGNAL(downloadProgress(qint64, qint64)), |
60 this, SLOT (updateDownloadProgress (qint64, qint64))); |
60 this, SLOT(updateDownloadProgress(qint64, qint64))); |
61 } |
61 } |
62 |
62 |
63 PartDownloadRequest::~PartDownloadRequest() {} |
63 PartDownloadRequest::~PartDownloadRequest() {} |
64 |
64 |
65 bool PartDownloadRequest::failed() const |
65 bool PartDownloadRequest::failed() const |
124 |
124 |
125 void PartDownloadRequest::updateToTable() |
125 void PartDownloadRequest::updateToTable() |
126 { |
126 { |
127 QTableWidget* table = prompt()->progressTable(); |
127 QTableWidget* table = prompt()->progressTable(); |
128 |
128 |
129 switch (m_state) |
129 switch(m_state) |
130 { |
130 { |
131 case State::Requesting: |
131 case State::Requesting: |
132 case State::Downloading: |
132 case State::Downloading: |
133 { |
133 { |
134 QWidget* cellwidget = table->cellWidget (tableRow(), PartDownloader::ProgressColumn); |
134 QWidget* cellwidget = table->cellWidget(tableRow(), PartDownloader::ProgressColumn); |
135 QProgressBar* progressBar = qobject_cast<QProgressBar*> (cellwidget); |
135 QProgressBar* progressBar = qobject_cast<QProgressBar*>(cellwidget); |
136 |
136 |
137 if (not progressBar) |
137 if (not progressBar) |
138 { |
138 { |
139 progressBar = new QProgressBar; |
139 progressBar = new QProgressBar; |
140 table->setCellWidget (tableRow(), PartDownloader::ProgressColumn, progressBar); |
140 table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, progressBar); |
141 } |
141 } |
142 |
142 |
143 progressBar->setRange (0, numBytesTotal()); |
143 progressBar->setRange(0, numBytesTotal()); |
144 progressBar->setValue (numBytesRead()); |
144 progressBar->setValue(numBytesRead()); |
145 } |
145 } |
146 break; |
146 break; |
147 |
147 |
148 case State::Finished: |
148 case State::Finished: |
149 case State::Failed: |
149 case State::Failed: |
150 { |
150 { |
151 const QString text = (m_state == State::Finished) |
151 const QString text = (m_state == State::Finished) |
152 ? "<b><span style=\"color: #080\">FINISHED</span></b>" |
152 ? "<b><span style=\"color: #080\">FINISHED</span></b>" |
153 : "<b><span style=\"color: #800\">FAILED</span></b>"; |
153 : "<b><span style=\"color: #800\">FAILED</span></b>"; |
154 |
154 |
155 QLabel* lb = new QLabel (text); |
155 QLabel* lb = new QLabel(text); |
156 lb->setAlignment (Qt::AlignCenter); |
156 lb->setAlignment(Qt::AlignCenter); |
157 table->setCellWidget (tableRow(), PartDownloader::ProgressColumn, lb); |
157 table->setCellWidget(tableRow(), PartDownloader::ProgressColumn, lb); |
158 } |
158 } |
159 break; |
159 break; |
160 } |
160 } |
161 |
161 |
162 QLabel* label = qobject_cast<QLabel*> (table->cellWidget (tableRow(), PartDownloader::PartLabelColumn)); |
162 QLabel* label = qobject_cast<QLabel*>(table->cellWidget(tableRow(), PartDownloader::PartLabelColumn)); |
163 |
163 |
164 if (isFirstUpdate()) |
164 if (isFirstUpdate()) |
165 { |
165 { |
166 label = new QLabel (format ("<b>%1</b>", destination()), table); |
166 label = new QLabel(format("<b>%1</b>", destination()), table); |
167 table->setCellWidget (tableRow(), PartDownloader::PartLabelColumn, label); |
167 table->setCellWidget(tableRow(), PartDownloader::PartLabelColumn, label); |
168 } |
168 } |
169 |
169 |
170 // Make sure that the cell is big enough to contain the label |
170 // Make sure that the cell is big enough to contain the label |
171 if (table->columnWidth (PartDownloader::PartLabelColumn) < label->width()) |
171 if (table->columnWidth(PartDownloader::PartLabelColumn) < label->width()) |
172 table->setColumnWidth (PartDownloader::PartLabelColumn, label->width()); |
172 table->setColumnWidth(PartDownloader::PartLabelColumn, label->width()); |
173 |
173 |
174 m_isFirstUpdate = false; |
174 m_isFirstUpdate = false; |
175 } |
175 } |
176 |
176 |
177 void PartDownloadRequest::downloadFinished() |
177 void PartDownloadRequest::downloadFinished() |
178 { |
178 { |
179 if (networkReply()->error() != QNetworkReply::NoError) |
179 if (networkReply()->error() != QNetworkReply::NoError) |
180 { |
180 { |
181 if (isPrimary() and not prompt()->isAborted()) |
181 if (isPrimary() and not prompt()->isAborted()) |
182 Critical (networkReply()->errorString()); |
182 Critical(networkReply()->errorString()); |
183 |
183 |
184 print ("Unable to download %1: %2\n", destination(), networkReply()->errorString()); |
184 print("Unable to download %1: %2\n", destination(), networkReply()->errorString()); |
185 m_state = State::Failed; |
185 m_state = State::Failed; |
186 } |
186 } |
187 else if (m_state != State::Failed) |
187 else if (m_state != State::Failed) |
188 { |
188 { |
189 m_state = State::Finished; |
189 m_state = State::Finished; |
197 m_filePointer->close(); |
197 m_filePointer->close(); |
198 delete m_filePointer; |
198 delete m_filePointer; |
199 m_filePointer = nullptr; |
199 m_filePointer = nullptr; |
200 |
200 |
201 if (m_state == State::Failed) |
201 if (m_state == State::Failed) |
202 QFile::remove (filePath()); |
202 QFile::remove(filePath()); |
203 } |
203 } |
204 |
204 |
205 if (m_state != State::Finished) |
205 if (m_state != State::Finished) |
206 { |
206 { |
207 prompt()->checkIfFinished(); |
207 prompt()->checkIfFinished(); |
208 return; |
208 return; |
209 } |
209 } |
210 |
210 |
211 // Try to load this file now. |
211 // Try to load this file now. |
212 LDDocument* document = m_documents->openDocument (filePath(), false, not isPrimary()); |
212 LDDocument* document = m_documents->openDocument(filePath(), false, not isPrimary()); |
213 |
213 |
214 if (document == nullptr) |
214 if (document == nullptr) |
215 return; |
215 return; |
216 |
216 |
217 // Iterate through this file and check for errors. If there's any that stems |
217 // Iterate through this file and check for errors. If there's any that stems |
218 // from unknown file references, try resolve that by downloading the reference. |
218 // from unknown file references, try resolve that by downloading the reference. |
219 // This is why downloading a part may end up downloading multiple files, as |
219 // This is why downloading a part may end up downloading multiple files, as |
220 // it resolves dependencies. |
220 // it resolves dependencies. |
221 for (LDObject* obj : document->objects()) |
221 for (LDObject* obj : document->objects()) |
222 { |
222 { |
223 LDError* err = dynamic_cast<LDError*> (obj); |
223 LDError* err = dynamic_cast<LDError*>(obj); |
224 |
224 |
225 if (err == nullptr or err->fileReferenced().isEmpty()) |
225 if (err == nullptr or err->fileReferenced().isEmpty()) |
226 continue; |
226 continue; |
227 |
227 |
228 QString dest = err->fileReferenced(); |
228 QString dest = err->fileReferenced(); |
229 prompt()->downloadFromPartsTracker (dest); |
229 prompt()->downloadFromPartsTracker(dest); |
230 } |
230 } |
231 |
231 |
232 prompt()->addFile (document); |
232 prompt()->addFile(document); |
233 |
233 |
234 if (isPrimary()) |
234 if (isPrimary()) |
235 { |
235 { |
236 m_documents->addRecentFile (filePath()); |
236 m_documents->addRecentFile(filePath()); |
237 prompt()->setPrimaryFile (document); |
237 prompt()->setPrimaryFile(document); |
238 } |
238 } |
239 |
239 |
240 prompt()->checkIfFinished(); |
240 prompt()->checkIfFinished(); |
241 } |
241 } |
242 |
242 |
243 void PartDownloadRequest::updateDownloadProgress (int64 recv, int64 total) |
243 void PartDownloadRequest::updateDownloadProgress(int64 recv, int64 total) |
244 { |
244 { |
245 m_numBytesRead = recv; |
245 m_numBytesRead = recv; |
246 m_numBytesTotal = total; |
246 m_numBytesTotal = total; |
247 m_state = State::Downloading; |
247 m_state = State::Downloading; |
248 updateToTable(); |
248 updateToTable(); |
253 if (m_state == State::Failed) |
253 if (m_state == State::Failed) |
254 return; |
254 return; |
255 |
255 |
256 if (m_filePointer == nullptr) |
256 if (m_filePointer == nullptr) |
257 { |
257 { |
258 m_filePath.replace ("\\", "/"); |
258 m_filePath.replace("\\", "/"); |
259 |
259 |
260 // We have already asked the user whether we can overwrite so we're good to go here. |
260 // We have already asked the user whether we can overwrite so we're good to go here. |
261 m_filePointer = new QFile (filePath().toLocal8Bit()); |
261 m_filePointer = new QFile(filePath().toLocal8Bit()); |
262 |
262 |
263 if (not m_filePointer->open (QIODevice::WriteOnly)) |
263 if (not m_filePointer->open(QIODevice::WriteOnly)) |
264 { |
264 { |
265 Critical (format (tr ("Couldn't open %1 for writing: %2"), filePath(), strerror (errno))); |
265 Critical(format(tr("Couldn't open %1 for writing: %2"), filePath(), strerror(errno))); |
266 m_state = State::Failed; |
266 m_state = State::Failed; |
267 networkReply()->abort(); |
267 networkReply()->abort(); |
268 updateToTable(); |
268 updateToTable(); |
269 prompt()->checkIfFinished(); |
269 prompt()->checkIfFinished(); |
270 return; |
270 return; |
271 } |
271 } |
272 } |
272 } |
273 |
273 |
274 m_filePointer->write (networkReply()->readAll()); |
274 m_filePointer->write(networkReply()->readAll()); |
275 } |
275 } |
276 |
276 |
277 bool PartDownloadRequest::isFinished() const |
277 bool PartDownloadRequest::isFinished() const |
278 { |
278 { |
279 return isOneOf (m_state, State::Finished, State::Failed); |
279 return isOneOf(m_state, State::Finished, State::Failed); |
280 } |
280 } |
281 |
281 |
282 void PartDownloadRequest::abort() |
282 void PartDownloadRequest::abort() |
283 { |
283 { |
284 networkReply()->abort(); |
284 networkReply()->abort(); |