src/download.cpp

Sun, 22 Sep 2013 23:27:07 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Sun, 22 Sep 2013 23:27:07 +0300
changeset 491
7d1b5ecd76c0
parent 484
5b5c77c7f3dd
child 493
16766ac1bbd9
permissions
-rw-r--r--

it's LDForge's 1st birthday!
- moved the project qmake code into ldforge.pro, removing src/src.pro in the process, with UI_DIR there is no need to have the main qmake in src/
- added moc #include directives to relevant source files, speeding up compile

426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013 Santeri Piippo
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 452
diff changeset
4 *
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 452
diff changeset
9 *
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
455
c5d14d112034 Code formatting: use same separators everywhere, remove extra spaces from license headers, simplified message manager api a bit
Santeri Piippo <crimsondusk64@gmail.com>
parents: 452
diff changeset
14 *
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
19 #include <QNetworkAccessManager>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
20 #include <QNetworkRequest>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
21 #include <QNetworkReply>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
22 #include <QDir>
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
23 #include <QProgressBar>
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
24 #include <QPushButton>
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #include "download.h"
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 #include "ui_downloadfrom.h"
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 #include "types.h"
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 #include "gui.h"
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
29 #include "file.h"
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
30 #include "gldraw.h"
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
31 #include "configDialog.h"
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
461
fbcc91ae1dd2 - added "Go to line" action, renamed the config classes to proper camelcase
Santeri Piippo <crimsondusk64@gmail.com>
parents: 460
diff changeset
33 cfg (String, net_downloadpath, "");
fbcc91ae1dd2 - added "Go to line" action, renamed the config classes to proper camelcase
Santeri Piippo <crimsondusk64@gmail.com>
parents: 460
diff changeset
34 cfg (Bool, net_guesspaths, true);
fbcc91ae1dd2 - added "Go to line" action, renamed the config classes to proper camelcase
Santeri Piippo <crimsondusk64@gmail.com>
parents: 460
diff changeset
35 cfg (Bool, net_autoclose, false);
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
36
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 constexpr const char* PartDownloader::k_OfficialURL,
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 *PartDownloader::k_UnofficialURL;
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
42 void PartDownloader::k_download() {
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
43 str path = getDownloadPath();
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
44 if (path == "" || QDir (path).exists() == false) {
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
45 critical (PartDownloader::tr ("You need to specify a valid path for "
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
46 "downloaded files in the configuration to download paths."));
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
47
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
48 (new ConfigDialog (ConfigDialog::DownloadTab, null))->exec();
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
49 return;
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
50 }
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
51
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
52 PartDownloader* dlg = new PartDownloader;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 dlg->exec();
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 // -----------------------------------------------------------------------------
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
58 str PartDownloader::getDownloadPath() {
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
59 str path = net_downloadpath;
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
60
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
61 #if DIRSLASH_CHAR != '/'
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
62 path.replace (DIRSLASH, "/");
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
63 #endif
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
64
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
65 return path;
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
66 }
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
67
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
68 // =============================================================================
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
69 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
70 PartDownloader::PartDownloader (QWidget* parent) : QDialog (parent) {
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 ui = new Ui_DownloadFrom;
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 ui->setupUi (this);
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
73 ui->fname->setFocus();
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
74 ui->progress->horizontalHeader()->setResizeMode (PartLabelColumn, QHeaderView::Stretch);
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
75
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
76 m_downloadButton = new QPushButton (tr ("Download"));
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
77 ui->buttonBox->addButton (m_downloadButton, QDialogButtonBox::ActionRole);
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
78 getButton (Abort)->setEnabled (false);
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
79
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 connect (ui->source, SIGNAL (currentIndexChanged (int)), this, SLOT (sourceChanged (int)));
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
81 connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this, SLOT (buttonClicked (QAbstractButton*)));
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
86 PartDownloader::~PartDownloader() {
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 delete ui;
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
92 str PartDownloader::getURL() const {
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
93 const Source src = getSource();
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
94 str dest;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 switch (src) {
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 case PartsTracker:
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
98 dest = ui->fname->text();
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
99 modifyDest (dest);
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
100 return str (k_UnofficialURL) + dest;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 case CustomURL:
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 return ui->fname->text();
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 // Shouldn't happen
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 return "";
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
112 void PartDownloader::modifyDest (str& dest) const {
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
113 dest = dest.simplified();
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
114
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
115 // If the user doesn't want us to guess, stop right here.
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
116 if (net_guesspaths == false)
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
117 return;
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
118
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
119 // Ensure .dat extension
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
120 if (dest.right (4) != ".dat") {
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
121 /* Remove the existing extension, if any. It may be we're here over a
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
122 typo in the .dat extension. */
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
123 const int dotpos = dest.lastIndexOf (".");
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
124 if (dotpos != -1 && dotpos >= dest.length() - 4)
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
125 dest.chop (dest.length() - dotpos);
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
126
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
127 dest += ".dat";
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
128 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
129
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
130 /* If the part starts with s\ or s/, then use parts/s/. Same goes with
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
131 48\ and p/48/. */
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
132 if (dest.left (2) == "s\\" || dest.left (2) == "s/") {
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
133 dest.remove (0, 2);
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
134 dest.prepend ("parts/s/");
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
135 } elif (dest.left (3) == "48\\" || dest.left (3) == "48/") {
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
136 dest.remove (0, 3);
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
137 dest.prepend ("p/48/");
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
138 }
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
139
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
140 /* Try determine where to put this part. We have four directories:
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
141 parts/, parts/s/, p/, and p/48/. If we haven't already specified
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
142 either parts/ or p/, we need to add it automatically. Part files
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
143 are numbers wit a possible u prefix for parts with unknown number
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
144 which can be followed by any of:
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
145 - c** (composites)
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
146 - d** (formed stickers)
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
147 - p** (patterns)
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
148 - a lowercase alphabetic letter for variants
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
149
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
150 Subfiles (usually) have an s** prefix, in which case we use parts/s/.
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
151 Note that the regex starts with a '^' so it won't catch already fully
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
152 given part file names. */
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
153 str partRegex = "^u?[0-9]+(c[0-9][0-9]+)*(d[0-9][0-9]+)*[a-z]?(p[0-9a-z][0-9a-z]+)*";
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
154 str subpartRegex = partRegex + "s[0-9][0-9]+";
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
155
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
156 partRegex += "\\.dat$";
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
157 subpartRegex += "\\.dat$";
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
158
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
159 if (QRegExp (subpartRegex).exactMatch (dest))
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
160 dest.prepend ("parts/s/");
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
161 elif (QRegExp (partRegex).exactMatch (dest))
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
162 dest.prepend ("parts/");
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
163 elif (dest.left (6) != "parts/" && dest.left (2) != "p/")
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
164 dest.prepend ("p/");
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
165 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
166
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
167 // =============================================================================
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
168 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
169 PartDownloader::Source PartDownloader::getSource() const {
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
170 return (Source) ui->source->currentIndex();
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
171 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
172
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
173 // =============================================================================
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
174 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
175 void PartDownloader::sourceChanged (int i) {
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
176 if (i == CustomURL)
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
177 ui->fileNameLabel->setText (tr ("URL:"));
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
178 else
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
179 ui->fileNameLabel->setText (tr ("File name:"));
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
180 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
181
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
182 // =============================================================================
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
183 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
184 void PartDownloader::buttonClicked (QAbstractButton* btn) {
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
185 if (btn == getButton (Close)) {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
186 reject();
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
187 } elif (btn == getButton (Abort)) {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
188 setAborted (true);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
189
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
190 for (PartDownloadRequest* req : m_requests)
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
191 req->abort();
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
192 } elif (btn == getButton (Download)) {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
193 str dest = ui->fname->text();
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
194 setPrimaryFile (null);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
195 setAborted (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
196
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
197 if (getSource() == CustomURL)
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
198 dest = basename (getURL());
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
199
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
200 modifyDest (dest);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
201
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
202 if (QFile::exists (PartDownloader::getDownloadPath() + DIRSLASH + dest)) {
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
203 const str overwritemsg = fmt (tr ("%1 already exists in download directory. Overwrite?"), dest);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
204
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
205 if (!confirm (tr ("Overwrite?"), overwritemsg))
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
206 return;
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
207 }
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
208
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
209 m_downloadButton->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
210 ui->progress->setEnabled (true);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
211 ui->fname->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
212 ui->source->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
213 downloadFile (dest, getURL(), true);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
214 getButton (Close)->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
215 getButton (Abort)->setEnabled (true);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
216 getButton (Download)->setEnabled (false);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
217 }
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
218 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
219
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
220 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
221 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
222 void PartDownloader::downloadFile (str dest, str url, bool primary) {
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
223 const int row = ui->progress->rowCount();
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
224
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
225 // Don't download files repeadetly.
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
226 if (m_filesToDownload.find (dest) != -1u)
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
227 return;
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
228
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
229 modifyDest (dest);
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
230 print ("DOWNLOAD: %1 -> %2\n", url, PartDownloader::getDownloadPath() + DIRSLASH + dest);
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
231 PartDownloadRequest* req = new PartDownloadRequest (url, dest, primary, this);
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
232
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
233 m_filesToDownload << dest;
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
234 m_requests << req;
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
235 ui->progress->insertRow (row);
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
236 req->setTableRow (row);
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
237 req->updateToTable();
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
238 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
239
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
240 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
241 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
242 void PartDownloader::checkIfFinished() {
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
243 bool failed = aborted();
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
244
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
245 // If there is some download still working, we're not finished.
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
246 for (PartDownloadRequest* req : m_requests) {
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
247 if (!req->isFinished())
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
248 return;
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
249
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
250 if (req->state() == PartDownloadRequest::Failed)
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
251 failed = true;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
252 }
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
253
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
254 for (PartDownloadRequest* req : m_requests)
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
255 delete req;
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
256
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
257 m_requests.clear();
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
258
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
259 // Update everything now
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
260 if (primaryFile()) {
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
261 LDFile::setCurrent (primaryFile());
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
262 reloadAllSubfiles();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
263 g_win->fullRefresh();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
264 g_win->R()->resetAngles();
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
265 }
452
47cc663e4ff4 ask the user for ext prog paths instead of telling to go to configuration if no path is defined
Santeri Piippo <crimsondusk64@gmail.com>
parents: 451
diff changeset
266
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
267 if (net_autoclose && !failed) {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
268 // Close automatically if desired.
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
269 accept();
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
270 } else {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
271 // Allow the prompt be closed now.
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
272 getButton (Abort)->setEnabled (false);
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
273 getButton (Close)->setEnabled (true);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
274 }
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
275 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
276
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
277 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
278 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
279 QPushButton* PartDownloader::getButton (PartDownloader::Button i) {
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
280 typedef QDialogButtonBox QDBB;
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
281 alias btnbox = ui->buttonBox;
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
282
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
283 switch (i) {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
284 case Download:
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
285 return m_downloadButton;
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
286
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
287 case Abort:
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
288 return qobject_cast<QPushButton*> (btnbox->button (QDBB::Abort));
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
289
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
290 case Close:
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
291 return qobject_cast<QPushButton*> (btnbox->button (QDBB::Close));
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
292 }
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
293
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
294 return null;
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
295 }
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
296
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
297 // =============================================================================
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
298 // -----------------------------------------------------------------------------
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
299 PartDownloadRequest::PartDownloadRequest (str url, str dest, bool primary, PartDownloader* parent) :
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
300 QObject (parent),
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
301 m_prompt (parent),
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
302 m_url (url),
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
303 m_dest (dest),
482
5e96648f416f redirect the user to configuration if the download path is not given
Santeri Piippo <crimsondusk64@gmail.com>
parents: 475
diff changeset
304 m_fpath (PartDownloader::getDownloadPath() + DIRSLASH + dest),
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
305 m_nam (new QNetworkAccessManager),
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
306 m_firstUpdate (true),
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
307 m_state (Requesting),
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
308 m_primary (primary),
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
309 m_fp (null)
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
310 {
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
311 // Make sure that we have a valid destination.
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
312 str dirpath = dirname (m_fpath);
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
313
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
314 QDir dir (dirpath);
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
315 if (!dir.exists()) {
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
316 print ("Creating %1...\n", dirpath);
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
317 if (!dir.mkpath (dirpath))
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
318 critical (fmt (tr ("Couldn't create the directory %1!"), dirpath));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
319 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
320
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
321 m_reply = m_nam->get (QNetworkRequest (QUrl (url)));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
322 connect (m_reply, SIGNAL (finished()), this, SLOT (downloadFinished()));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
323 connect (m_reply, SIGNAL (readyRead()), this, SLOT (readyRead()));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
324 connect (m_reply, SIGNAL (downloadProgress (qint64, qint64)), this, SLOT (downloadProgress (qint64, qint64)));
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
325 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
326
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
327 // =============================================================================
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
328 // -----------------------------------------------------------------------------
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
329 PartDownloadRequest::~PartDownloadRequest() {}
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
330
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
331 // =============================================================================
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
332 // -----------------------------------------------------------------------------
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
333 void PartDownloadRequest::updateToTable() {
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
334 const int labelcol = PartDownloader::PartLabelColumn,
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
335 progcol = PartDownloader::ProgressColumn;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
336 QTableWidget* table = m_prompt->ui->progress;
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
337 QProgressBar* prog;
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
338
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
339 switch (m_state) {
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
340 case Requesting:
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
341 case Downloading:
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
342 prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), progcol));
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
343
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
344 if (!prog) {
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
345 prog = new QProgressBar;
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
346 table->setCellWidget (tableRow(), progcol, prog);
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
347 }
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
348
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
349 prog->setRange (0, m_bytesTotal);
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
350 prog->setValue (m_bytesRead);
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
351 break;
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
352
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
353 case Finished:
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
354 case Failed:
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
355 {
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
356 QLabel* lb = new QLabel ((m_state == Finished) ? "<b><span style=\"color: #080\">FINISHED</span></b>" :
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
357 "<b><span style=\"color: #800\">FAILED</span></b>");
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
358 lb->setAlignment (Qt::AlignCenter);
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
359 table->setCellWidget (tableRow(), progcol, lb);
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
360 }
428
53e577061dc8 handle errors
Santeri Piippo <crimsondusk64@gmail.com>
parents: 427
diff changeset
361 break;
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
362 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
363
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
364 QLabel* lb = qobject_cast<QLabel*> (table->cellWidget (tableRow(), labelcol));
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
365 if (m_firstUpdate) {
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
366 lb = new QLabel (fmt ("<b>%1</b>", m_dest), table);
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
367 table->setCellWidget (tableRow(), labelcol, lb);
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
368 }
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
369
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
370 // Make sure that the cell is big enough to contain the label
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
371 if (table->columnWidth (labelcol) < lb->width())
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
372 table->setColumnWidth (labelcol, lb->width());
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
373
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
374 m_firstUpdate = false;
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
375 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
376
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
377 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
378 // -----------------------------------------------------------------------------
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
379 void PartDownloadRequest::downloadFinished() {
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
380 if (m_reply->error() != QNetworkReply::NoError) {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
381 if (m_primary && !m_prompt->aborted())
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
382 critical (m_reply->errorString());
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
383
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
384 m_state = Failed;
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
385 } elif (state() != Failed)
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
386 m_state = Finished;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
387
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
388 m_bytesRead = m_bytesTotal;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
389 updateToTable();
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
390
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
391 if (m_fp) {
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
392 m_fp->close();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
393 delete m_fp;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
394 m_fp = null;
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
395
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
396 if (m_state == Failed)
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
397 QFile::remove (m_fpath);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
398 }
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
399
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
400 if (m_state != Finished) {
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
401 m_prompt->checkIfFinished();
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
402 return;
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
403 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
404
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
405 // Try to load this file now.
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
406 LDFile* f = openDATFile (m_fpath, false);
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
407 if (!f)
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
408 return;
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
409
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
410 f->setImplicit (!m_primary);
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
411
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
412 // Iterate through this file and check for errors. If there's any that stems
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
413 // from unknown file references, try resolve that by downloading the reference.
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
414 // This is why downloading a part may end up downloading multiple files, as
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
415 // it resolves dependencies.
484
5b5c77c7f3dd made LDFile interfacing a bit clearer
Santeri Piippo <crimsondusk64@gmail.com>
parents: 482
diff changeset
416 for (LDObject* obj : f->objects()) {
460
b230ae09c8e5 Cut the Object-suffix from LDObject types, it doesn't help things at all
Santeri Piippo <crimsondusk64@gmail.com>
parents: 455
diff changeset
417 LDError* err = dynamic_cast<LDError*> (obj);
451
7ef26b45ed55 minor change
Santeri Piippo <crimsondusk64@gmail.com>
parents: 449
diff changeset
418 if (!err || err->fileRef().isEmpty())
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
419 continue;
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
420
430
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
421 str dest = err->fileRef();
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
422 m_prompt->modifyDest (dest);
8458cf2719d1 added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents: 429
diff changeset
423 m_prompt->downloadFile (dest, str (PartDownloader::k_UnofficialURL) + dest, false);
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
424 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
425
449
f462f0bd3c51 made downloaded files be stored into recent files
Santeri Piippo <crimsondusk64@gmail.com>
parents: 435
diff changeset
426 if (m_primary) {
f462f0bd3c51 made downloaded files be stored into recent files
Santeri Piippo <crimsondusk64@gmail.com>
parents: 435
diff changeset
427 addRecentFile (m_fpath);
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
428 m_prompt->setPrimaryFile (f);
449
f462f0bd3c51 made downloaded files be stored into recent files
Santeri Piippo <crimsondusk64@gmail.com>
parents: 435
diff changeset
429 }
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
430
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
431 m_prompt->checkIfFinished();
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
432 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
433
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
434 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
435 // -----------------------------------------------------------------------------
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
436 void PartDownloadRequest::downloadProgress (int64 recv, int64 total) {
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
437 m_bytesRead = recv;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
438 m_bytesTotal = total;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
439 m_state = Downloading;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
440 updateToTable();
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
441 }
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
442
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
443 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
444 // -----------------------------------------------------------------------------
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
445 void PartDownloadRequest::readyRead() {
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
446 if (state() == Failed)
427
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
447 return;
d308149fbc90 now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents: 426
diff changeset
448
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
449 if (m_fp == null) {
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
450 m_fpath.replace ("\\", "/");
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
451
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
452 // We have already asked the user whether we can overwrite so we're good
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
453 // to go here.
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
454 m_fp = new QFile (m_fpath.toLocal8Bit());
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
455 if (!m_fp->open (QIODevice::WriteOnly)) {
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
456 critical (fmt (tr ("Couldn't open %1 for writing: %2"), m_fpath, strerror (errno)));
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
457 m_state = Failed;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
458 m_reply->abort();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
459 updateToTable();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
460 m_prompt->checkIfFinished();
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
461 return;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
462 }
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
463 }
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
464
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
465 m_fp->write (m_reply->readAll());
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
466 }
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
467
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
468 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
469 // -----------------------------------------------------------------------------
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
470 bool PartDownloadRequest::isFinished() const {
431
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
471 return m_state == Finished || m_state == Failed;
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
472 }
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
473
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
474 // =============================================================================
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
475 // -----------------------------------------------------------------------------
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
476 const PartDownloadRequest::State& PartDownloadRequest::state() const {
ec1e2059319b stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 430
diff changeset
477 return m_state;
429
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
478 }
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
479
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
480 // =============================================================================
3488534b2b31 More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 428
diff changeset
481 // -----------------------------------------------------------------------------
432
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
482 void PartDownloadRequest::abort() {
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
483 m_reply->abort();
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
484 }
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
485
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
486 // =============================================================================
ef382b98a8af finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents: 431
diff changeset
487 // -----------------------------------------------------------------------------
426
be5746bf0f12 added missing files
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
488 DEFINE_ACTION (DownloadFrom, 0) {
435
bc3a51394953 Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents: 433
diff changeset
489 PartDownloader::k_download();
491
7d1b5ecd76c0 it's LDForge's 1st birthday!
Santeri Piippo <crimsondusk64@gmail.com>
parents: 484
diff changeset
490 }
7d1b5ecd76c0 it's LDForge's 1st birthday!
Santeri Piippo <crimsondusk64@gmail.com>
parents: 484
diff changeset
491 #include "moc_download.cpp"

mercurial