| 28 #include "gui.h" |
28 #include "gui.h" |
| 29 #include "build/moc_download.cpp" |
29 #include "build/moc_download.cpp" |
| 30 #include "file.h" |
30 #include "file.h" |
| 31 #include "gldraw.h" |
31 #include "gldraw.h" |
| 32 |
32 |
| 33 PartDownloader g_PartDownloader; |
|
| 34 |
|
| 35 cfg (str, net_downloadpath, ""); |
33 cfg (str, net_downloadpath, ""); |
| 36 cfg (bool, net_guesspaths, true); |
34 cfg (bool, net_guesspaths, true); |
| 37 cfg (bool, net_autoclose, false); |
35 cfg (bool, net_autoclose, false); |
| 38 |
36 |
| 39 constexpr const char* PartDownloader::k_OfficialURL, |
37 constexpr const char* PartDownloader::k_OfficialURL, |
| 40 *PartDownloader::k_UnofficialURL; |
38 *PartDownloader::k_UnofficialURL; |
| 41 |
39 |
| 42 // ============================================================================= |
40 // ============================================================================= |
| 43 // ----------------------------------------------------------------------------- |
41 // ----------------------------------------------------------------------------- |
| 44 void PartDownloader::download() { |
42 void PartDownloader::k_download() { |
| 45 str path = getDownloadPath(); |
43 str path = getDownloadPath(); |
| 46 if (path == "" || QDir (path).exists() == false) { |
44 if (path == "" || QDir (path).exists() == false) { |
| 47 critical (PartDownloadPrompt::tr ("You need to specify a valid path for " |
45 critical (PartDownloader::tr ("You need to specify a valid path for " |
| 48 "downloaded files in the configuration to download paths.")); |
46 "downloaded files in the configuration to download paths.")); |
| 49 return; |
47 return; |
| 50 } |
48 } |
| 51 |
49 |
| 52 PartDownloadPrompt* dlg = new PartDownloadPrompt; |
50 PartDownloader* dlg = new PartDownloader; |
| 53 dlg->exec(); |
51 dlg->exec(); |
| 54 } |
52 } |
| 55 |
53 |
| 56 // ============================================================================= |
54 // ============================================================================= |
| 57 // ----------------------------------------------------------------------------- |
55 // ----------------------------------------------------------------------------- |
| 65 return path; |
63 return path; |
| 66 } |
64 } |
| 67 |
65 |
| 68 // ============================================================================= |
66 // ============================================================================= |
| 69 // ----------------------------------------------------------------------------- |
67 // ----------------------------------------------------------------------------- |
| 70 PartDownloadPrompt::PartDownloadPrompt (QWidget* parent) : QDialog (parent) { |
68 PartDownloader::PartDownloader (QWidget* parent) : QDialog (parent) { |
| 71 ui = new Ui_DownloadFrom; |
69 ui = new Ui_DownloadFrom; |
| 72 ui->setupUi (this); |
70 ui->setupUi (this); |
| 73 ui->fname->setFocus(); |
71 ui->fname->setFocus(); |
| |
72 ui->progress->horizontalHeader()->setResizeMode (PartLabelColumn, QHeaderView::Stretch); |
| 74 |
73 |
| 75 m_downloadButton = new QPushButton (tr ("Download")); |
74 m_downloadButton = new QPushButton (tr ("Download")); |
| 76 ui->buttonBox->addButton (m_downloadButton, QDialogButtonBox::ActionRole); |
75 ui->buttonBox->addButton (m_downloadButton, QDialogButtonBox::ActionRole); |
| 77 ui->buttonBox->button (QDialogButtonBox::Abort)->setEnabled (false); |
76 ui->buttonBox->button (QDialogButtonBox::Abort)->setEnabled (false); |
| 78 |
77 |
| 80 connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this, SLOT (buttonClicked (QAbstractButton*))); |
79 connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this, SLOT (buttonClicked (QAbstractButton*))); |
| 81 } |
80 } |
| 82 |
81 |
| 83 // ============================================================================= |
82 // ============================================================================= |
| 84 // ----------------------------------------------------------------------------- |
83 // ----------------------------------------------------------------------------- |
| 85 PartDownloadPrompt::~PartDownloadPrompt() { |
84 PartDownloader::~PartDownloader() { |
| 86 delete ui; |
85 delete ui; |
| 87 } |
86 } |
| 88 |
87 |
| 89 // ============================================================================= |
88 // ============================================================================= |
| 90 // ----------------------------------------------------------------------------- |
89 // ----------------------------------------------------------------------------- |
| 91 str PartDownloadPrompt::getURL() const { |
90 str PartDownloader::getURL() const { |
| 92 const Source src = getSource(); |
91 const Source src = getSource(); |
| 93 str dest; |
92 str dest; |
| 94 |
93 |
| 95 switch (src) { |
94 switch (src) { |
| 96 case PartsTracker: |
95 case PartsTracker: |
| 97 dest = ui->fname->text(); |
96 dest = ui->fname->text(); |
| 98 modifyDest (dest); |
97 modifyDest (dest); |
| 99 return str (PartDownloader::k_UnofficialURL) + dest; |
98 return str (k_UnofficialURL) + dest; |
| 100 |
99 |
| 101 case CustomURL: |
100 case CustomURL: |
| 102 return ui->fname->text(); |
101 return ui->fname->text(); |
| 103 } |
102 } |
| 104 |
103 |
| 106 return ""; |
105 return ""; |
| 107 } |
106 } |
| 108 |
107 |
| 109 // ============================================================================= |
108 // ============================================================================= |
| 110 // ----------------------------------------------------------------------------- |
109 // ----------------------------------------------------------------------------- |
| 111 void PartDownloadPrompt::modifyDest (str& dest) const { |
110 void PartDownloader::modifyDest (str& dest) const { |
| 112 dest = dest.simplified(); |
111 dest = dest.simplified(); |
| 113 |
112 |
| 114 // If the user doesn't want us to guess, stop right here. |
113 // If the user doesn't want us to guess, stop right here. |
| 115 if (net_guesspaths == false) |
114 if (net_guesspaths == false) |
| 116 return; |
115 return; |
| 164 } |
163 } |
| 165 } |
164 } |
| 166 |
165 |
| 167 // ============================================================================= |
166 // ============================================================================= |
| 168 // ----------------------------------------------------------------------------- |
167 // ----------------------------------------------------------------------------- |
| 169 PartDownloadPrompt::Source PartDownloadPrompt::getSource() const { |
168 PartDownloader::Source PartDownloader::getSource() const { |
| 170 return (Source) ui->source->currentIndex(); |
169 return (Source) ui->source->currentIndex(); |
| 171 } |
170 } |
| 172 |
171 |
| 173 // ============================================================================= |
172 // ============================================================================= |
| 174 // ----------------------------------------------------------------------------- |
173 // ----------------------------------------------------------------------------- |
| 175 void PartDownloadPrompt::sourceChanged (int i) { |
174 void PartDownloader::sourceChanged (int i) { |
| 176 if (i == CustomURL) |
175 if (i == CustomURL) |
| 177 ui->fileNameLabel->setText (tr ("URL:")); |
176 ui->fileNameLabel->setText (tr ("URL:")); |
| 178 else |
177 else |
| 179 ui->fileNameLabel->setText (tr ("File name:")); |
178 ui->fileNameLabel->setText (tr ("File name:")); |
| 180 } |
179 } |
| 181 |
180 |
| 182 void PartDownloadPrompt::buttonClicked (QAbstractButton* btn) { |
181 void PartDownloader::buttonClicked (QAbstractButton* btn) { |
| 183 if (btn == getButton (Close)) { |
182 if (btn == getButton (Close)) { |
| 184 reject(); |
183 reject(); |
| 185 } elif (btn == getButton (Abort)) { |
184 } elif (btn == getButton (Abort)) { |
| 186 setAborted (true); |
185 setAborted (true); |
| 187 |
186 |
| 215 } |
214 } |
| 216 } |
215 } |
| 217 |
216 |
| 218 // ============================================================================= |
217 // ============================================================================= |
| 219 // ----------------------------------------------------------------------------- |
218 // ----------------------------------------------------------------------------- |
| 220 void PartDownloadPrompt::downloadFile (str dest, str url, bool primary) { |
219 void PartDownloader::downloadFile (str dest, str url, bool primary) { |
| 221 const int row = ui->progress->rowCount(); |
220 const int row = ui->progress->rowCount(); |
| 222 |
221 |
| 223 // Don't download files repeadetly. |
222 // Don't download files repeadetly. |
| 224 if (m_filesToDownload.find (dest) != -1u) |
223 if (m_filesToDownload.find (dest) != -1u) |
| 225 return; |
224 return; |
| 235 req->updateToTable(); |
234 req->updateToTable(); |
| 236 } |
235 } |
| 237 |
236 |
| 238 // ============================================================================= |
237 // ============================================================================= |
| 239 // ----------------------------------------------------------------------------- |
238 // ----------------------------------------------------------------------------- |
| 240 void PartDownloadPrompt::checkIfFinished() { |
239 void PartDownloader::checkIfFinished() { |
| 241 bool failed = aborted(); |
240 bool failed = aborted(); |
| 242 |
241 |
| 243 // If there is some download still working, we're not finished. |
242 // If there is some download still working, we're not finished. |
| 244 for (PartDownloadRequest* req : m_requests) { |
243 for (PartDownloadRequest* req : m_requests) { |
| 245 if (!req->isFinished()) |
244 if (!req->isFinished()) |
| 272 } |
271 } |
| 273 } |
272 } |
| 274 |
273 |
| 275 // ============================================================================= |
274 // ============================================================================= |
| 276 // ----------------------------------------------------------------------------- |
275 // ----------------------------------------------------------------------------- |
| 277 QPushButton* PartDownloadPrompt::getButton (PartDownloadPrompt::Button i) { |
276 QPushButton* PartDownloader::getButton (PartDownloader::Button i) { |
| 278 typedef QDialogButtonBox QDBB; |
277 typedef QDialogButtonBox QDBB; |
| 279 alias btnbox = ui->buttonBox; |
278 alias btnbox = ui->buttonBox; |
| 280 |
279 |
| 281 switch (i) { |
280 switch (i) { |
| 282 case Download: |
281 case Download: |
| 292 return null; |
291 return null; |
| 293 } |
292 } |
| 294 |
293 |
| 295 // ============================================================================= |
294 // ============================================================================= |
| 296 // ----------------------------------------------------------------------------- |
295 // ----------------------------------------------------------------------------- |
| 297 PartDownloadRequest::PartDownloadRequest (str url, str dest, bool primary, PartDownloadPrompt* parent) : |
296 PartDownloadRequest::PartDownloadRequest (str url, str dest, bool primary, PartDownloader* parent) : |
| 298 QObject (parent), |
297 QObject (parent), |
| 299 m_prompt (parent), |
298 m_prompt (parent), |
| 300 m_url (url), |
299 m_url (url), |
| 301 m_dest (dest), |
300 m_dest (dest), |
| 302 m_fpath (PartDownloader::getDownloadPath() + dest), |
301 m_fpath (PartDownloader::getDownloadPath() + dest), |
| 327 PartDownloadRequest::~PartDownloadRequest() {} |
326 PartDownloadRequest::~PartDownloadRequest() {} |
| 328 |
327 |
| 329 // ============================================================================= |
328 // ============================================================================= |
| 330 // ----------------------------------------------------------------------------- |
329 // ----------------------------------------------------------------------------- |
| 331 void PartDownloadRequest::updateToTable() { |
330 void PartDownloadRequest::updateToTable() { |
| |
331 const int labelcol = PartDownloader::PartLabelColumn, |
| |
332 progcol = PartDownloader::ProgressColumn; |
| 332 QTableWidget* table = m_prompt->ui->progress; |
333 QTableWidget* table = m_prompt->ui->progress; |
| 333 QProgressBar* prog; |
334 QProgressBar* prog; |
| 334 |
335 |
| 335 switch (m_state) { |
336 switch (m_state) { |
| 336 case Requesting: |
337 case Requesting: |
| 337 case Downloading: |
338 case Downloading: |
| 338 prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), ProgressColumn)); |
339 prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), progcol)); |
| 339 |
340 |
| 340 if (!prog) { |
341 if (!prog) { |
| 341 prog = new QProgressBar; |
342 prog = new QProgressBar; |
| 342 table->setCellWidget (tableRow(), ProgressColumn, prog); |
343 table->setCellWidget (tableRow(), progcol, prog); |
| 343 } |
344 } |
| 344 |
345 |
| 345 prog->setRange (0, m_bytesTotal); |
346 prog->setRange (0, m_bytesTotal); |
| 346 prog->setValue (m_bytesRead); |
347 prog->setValue (m_bytesRead); |
| 347 break; |
348 break; |
| 350 case Failed: |
351 case Failed: |
| 351 { |
352 { |
| 352 QLabel* lb = new QLabel ((m_state == Finished) ? "<b><span style=\"color: #080\">FINISHED</span></b>" : |
353 QLabel* lb = new QLabel ((m_state == Finished) ? "<b><span style=\"color: #080\">FINISHED</span></b>" : |
| 353 "<b><span style=\"color: #800\">FAILED</span></b>"); |
354 "<b><span style=\"color: #800\">FAILED</span></b>"); |
| 354 lb->setAlignment (Qt::AlignCenter); |
355 lb->setAlignment (Qt::AlignCenter); |
| 355 table->setCellWidget (tableRow(), ProgressColumn, lb); |
356 table->setCellWidget (tableRow(), progcol, lb); |
| 356 } |
357 } |
| 357 break; |
358 break; |
| 358 } |
359 } |
| 359 |
360 |
| 360 QLabel* lb = qobject_cast<QLabel*> (table->cellWidget (tableRow(), PartLabelColumn)); |
361 QLabel* lb = qobject_cast<QLabel*> (table->cellWidget (tableRow(), labelcol)); |
| 361 if (m_firstUpdate) { |
362 if (m_firstUpdate) { |
| 362 lb = new QLabel (fmt ("<b>%1</b>", m_dest), table); |
363 lb = new QLabel (fmt ("<b>%1</b>", m_dest), table); |
| 363 table->setCellWidget (tableRow(), PartLabelColumn, lb); |
364 table->setCellWidget (tableRow(), labelcol, lb); |
| 364 } |
365 } |
| 365 |
366 |
| 366 // Make sure that the cell is big enough to contain the label |
367 // Make sure that the cell is big enough to contain the label |
| 367 if (table->columnWidth (PartLabelColumn) < lb->width()) |
368 if (table->columnWidth (labelcol) < lb->width()) |
| 368 table->setColumnWidth (PartLabelColumn, lb->width()); |
369 table->setColumnWidth (labelcol, lb->width()); |
| 369 |
370 |
| 370 m_firstUpdate = false; |
371 m_firstUpdate = false; |
| 371 } |
372 } |
| 372 |
373 |
| 373 // ============================================================================= |
374 // ============================================================================= |
| 481 } |
482 } |
| 482 |
483 |
| 483 // ============================================================================= |
484 // ============================================================================= |
| 484 // ----------------------------------------------------------------------------- |
485 // ----------------------------------------------------------------------------- |
| 485 DEFINE_ACTION (DownloadFrom, 0) { |
486 DEFINE_ACTION (DownloadFrom, 0) { |
| 486 g_PartDownloader.download(); |
487 PartDownloader::k_download(); |
| 487 } |
488 } |