Sat, 03 Aug 2013 04:32:13 +0300
finalized downloading
426 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 Santeri Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
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 | 25 | #include "download.h" |
26 | #include "ui_downloadfrom.h" | |
27 | #include "types.h" | |
28 | #include "gui.h" | |
29 | #include "build/moc_download.cpp" | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
30 | #include "file.h" |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
31 | #include "gldraw.h" |
426 | 32 | |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
33 | // TODO: Move this to common.h |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
34 | #define alias auto& |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
35 | |
426 | 36 | PartDownloader g_PartDownloader; |
37 | ||
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
38 | cfg (str, net_downloadpath, ""); |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
39 | cfg (bool, net_guesspaths, true); |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
40 | 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
|
41 | |
426 | 42 | constexpr const char* PartDownloader::k_OfficialURL, |
43 | *PartDownloader::k_UnofficialURL; | |
44 | ||
45 | // ============================================================================= | |
46 | // ----------------------------------------------------------------------------- | |
47 | void PartDownloader::download() { | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
48 | str path = getDownloadPath(); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
49 | if (path == "" || QDir (path).exists() == false) { |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
50 | critical (PartDownloadPrompt::tr ("You need to specify a valid path for " |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
51 | "downloaded files in the configuration to download paths.")); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
52 | return; |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
53 | } |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
54 | |
426 | 55 | PartDownloadPrompt* dlg = new PartDownloadPrompt; |
56 | dlg->exec(); | |
57 | } | |
58 | ||
59 | // ============================================================================= | |
60 | // ----------------------------------------------------------------------------- | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
61 | str PartDownloader::getDownloadPath() { |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
62 | 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
|
63 | |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
64 | #if DIRSLASH_CHAR != '/' |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
65 | path.replace (DIRSLASH, "/"); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
66 | #endif |
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 | return path; |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
69 | } |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
70 | |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
71 | // ============================================================================= |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
72 | // ----------------------------------------------------------------------------- |
426 | 73 | PartDownloadPrompt::PartDownloadPrompt (QWidget* parent) : QDialog (parent) { |
74 | ui = new Ui_DownloadFrom; | |
75 | ui->setupUi (this); | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
76 | ui->fname->setFocus(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
77 | |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
78 | m_downloadButton = new QPushButton (tr ("Download")); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
79 | ui->buttonBox->addButton (m_downloadButton, QDialogButtonBox::ActionRole); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
80 | ui->buttonBox->button (QDialogButtonBox::Abort)->setEnabled (false); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
81 | |
426 | 82 | connect (ui->source, SIGNAL (currentIndexChanged (int)), this, SLOT (sourceChanged (int))); |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
83 | connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this, SLOT (buttonClicked (QAbstractButton*))); |
426 | 84 | } |
85 | ||
86 | // ============================================================================= | |
87 | // ----------------------------------------------------------------------------- | |
88 | PartDownloadPrompt::~PartDownloadPrompt() { | |
89 | delete ui; | |
90 | } | |
91 | ||
92 | // ============================================================================= | |
93 | // ----------------------------------------------------------------------------- | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
94 | str PartDownloadPrompt::getURL() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
95 | 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
|
96 | str dest; |
426 | 97 | |
98 | switch (src) { | |
99 | case PartsTracker: | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
100 | 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
|
101 | modifyDest (dest); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
102 | return str (PartDownloader::k_UnofficialURL) + dest; |
426 | 103 | |
104 | case CustomURL: | |
105 | return ui->fname->text(); | |
106 | } | |
107 | ||
108 | // Shouldn't happen | |
109 | return ""; | |
110 | } | |
111 | ||
112 | // ============================================================================= | |
113 | // ----------------------------------------------------------------------------- | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
114 | void PartDownloadPrompt::modifyDest (str& dest) const { |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
115 | dest = dest.simplified(); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
116 | |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
117 | // 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
|
118 | if (net_guesspaths == false) |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
119 | return; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
120 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
121 | // 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
|
122 | if (dest.right (4) != ".dat") { |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
123 | // Remove the existing extension, if any. It may be we're here over a |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
124 | // 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
|
125 | 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
|
126 | 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
|
127 | dest.chop (dest.length() - dotpos); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
128 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
129 | dest += ".dat"; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
130 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
131 | |
428 | 132 | // If the part starts with s\ or s/, then use parts/s/. Same goes with |
133 | // 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
|
134 | 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
|
135 | 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
|
136 | 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
|
137 | } 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
|
138 | 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
|
139 | dest.prepend ("p/48/"); |
428 | 140 | } |
141 | ||
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
142 | /* Try determine where to put this part. We have four directories: |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
143 | * parts/, parts/s/, p/, and p/48/. If we haven't already specified |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
144 | * either parts/ or p/, we need to add it automatically. Part files |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
145 | * are numbers which can be followed by: |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
146 | * - c** (composites) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
147 | * - d** (formed stickers) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
148 | * - a lowercase alphabetic letter for variants |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
149 | * |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
150 | * Subfiles have an s** prefix, in which case we use parts/s/. Note that |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
151 | * the regex starts with a '^' so it won't catch already fully given part |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
152 | * file names. |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
153 | */ |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
154 | { |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
155 | str partRegex = "^u?[0-9]+(c[0-9][0-9]+)*(d[0-9][0-9]+)*[a-z]?(p[0-9a-z][0-9a-z]+)*"; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
156 | str subpartRegex = partRegex + "s[0-9][0-9]+"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
157 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
158 | partRegex += "\\.dat$"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
159 | subpartRegex += "\\.dat$"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
160 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
161 | if (QRegExp (subpartRegex).exactMatch (dest)) |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
162 | 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
|
163 | elif (QRegExp (partRegex).exactMatch (dest)) |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
164 | dest.prepend ("parts/"); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
165 | elif (dest.left (6) != "parts/" && dest.left (2) != "p/") |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
166 | dest.prepend ("p/"); |
427
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 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
169 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
170 | // ============================================================================= |
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 | PartDownloadPrompt::Source PartDownloadPrompt::getSource() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
173 | return (Source) ui->source->currentIndex(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
174 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
175 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
176 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
177 | // ----------------------------------------------------------------------------- |
426 | 178 | void PartDownloadPrompt::sourceChanged (int i) { |
179 | if (i == CustomURL) | |
180 | ui->fileNameLabel->setText (tr ("URL:")); | |
181 | else | |
182 | ui->fileNameLabel->setText (tr ("File name:")); | |
183 | } | |
184 | ||
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
185 | void PartDownloadPrompt::buttonClicked (QAbstractButton* btn) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
186 | if (btn == getButton (Close)) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
187 | reject(); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
188 | } elif (btn == getButton (Abort)) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
189 | setAborted (true); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
190 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
191 | for (PartDownloadRequest* req : m_requests) |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
192 | req->abort(); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
193 | } elif (btn == getButton (Download)) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
194 | str dest = ui->fname->text(); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
195 | setPrimaryFile (null); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
196 | setAborted (false); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
197 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
198 | if (getSource() == CustomURL) |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
199 | dest = basename (getURL()); |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
200 | |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
201 | modifyDest (dest); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
202 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
203 | if (QFile::exists (PartDownloader::getDownloadPath() + dest)) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
204 | 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
|
205 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
206 | if (!confirm (tr ("Overwrite?"), overwritemsg)) |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
207 | return; |
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 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
210 | m_downloadButton->setEnabled (false); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
211 | ui->progress->setEnabled (true); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
212 | ui->fname->setEnabled (false); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
213 | ui->source->setEnabled (false); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
214 | downloadFile (dest, getURL(), true); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
215 | getButton (Close)->setEnabled (false); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
216 | getButton (Abort)->setEnabled (true); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
217 | getButton (Download)->setEnabled (false); |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
218 | } |
429
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 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
222 | // ----------------------------------------------------------------------------- |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
223 | void PartDownloadPrompt::downloadFile (str dest, str url, bool primary) { |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
224 | const int row = ui->progress->rowCount(); |
426 | 225 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
226 | // 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
|
227 | if (m_filesToDownload.find (dest) != -1u) |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
228 | return; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
229 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
230 | modifyDest (dest); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
231 | print ("DOWNLOAD: %1 -> %2\n", url, PartDownloader::getDownloadPath() + dest); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
232 | PartDownloadRequest* req = new PartDownloadRequest (url, dest, primary, this); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
233 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
234 | m_filesToDownload << dest; |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
235 | m_requests << req; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
236 | ui->progress->insertRow (row); |
426 | 237 | req->setTableRow (row); |
238 | req->updateToTable(); | |
239 | } | |
240 | ||
241 | // ============================================================================= | |
242 | // ----------------------------------------------------------------------------- | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
243 | void PartDownloadPrompt::checkIfFinished() { |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
244 | bool failed = aborted(); |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
245 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
246 | // 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
|
247 | for (PartDownloadRequest* req : m_requests) { |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
248 | if (!req->isFinished()) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
249 | return; |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
250 | |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
251 | if (req->state() == PartDownloadRequest::Failed) |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
252 | failed = true; |
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 | |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
255 | for (PartDownloadRequest* req : m_requests) |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
256 | delete req; |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
257 | |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
258 | m_requests.clear(); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
259 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
260 | // Update everything now |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
261 | if (primaryFile()) { |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
262 | LDFile::setCurrent (primaryFile()); |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
263 | reloadAllSubfiles(); |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
264 | g_win->fullRefresh(); |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
265 | g_win->R()->resetAngles(); |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
266 | } |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
267 | |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
268 | if (net_autoclose && !failed) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
269 | // Close automatically if desired. |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
270 | accept(); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
271 | } else { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
272 | // Allow the prompt be closed now. |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
273 | getButton (Abort)->setEnabled (false); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
274 | getButton (Close)->setEnabled (true); |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
275 | } |
429
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 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
279 | // ----------------------------------------------------------------------------- |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
280 | QPushButton* PartDownloadPrompt::getButton (PartDownloadPrompt::Button i) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
281 | typedef QDialogButtonBox QDBB; |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
282 | alias btnbox = ui->buttonBox; |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
283 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
284 | switch (i) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
285 | case Download: |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
286 | return m_downloadButton; |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
287 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
288 | case Abort: |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
289 | return qobject_cast<QPushButton*> (btnbox->button (QDBB::Abort)); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
290 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
291 | case Close: |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
292 | return qobject_cast<QPushButton*> (btnbox->button (QDBB::Close)); |
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 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
295 | return null; |
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 | // ============================================================================= |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
299 | // ----------------------------------------------------------------------------- |
428 | 300 | PartDownloadRequest::PartDownloadRequest (str url, str dest, bool primary, PartDownloadPrompt* parent) : |
426 | 301 | QObject (parent), |
302 | m_prompt (parent), | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
303 | m_url (url), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
304 | m_dest (dest), |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
305 | m_fpath (PartDownloader::getDownloadPath() + dest), |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
306 | m_nam (new QNetworkAccessManager), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
307 | m_firstUpdate (true), |
428 | 308 | m_state (Requesting), |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
309 | m_primary (primary), |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
310 | m_fp (null) |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
311 | { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
312 | // Make sure that we have a valid destination. |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
313 | str dirpath = dirname (m_fpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
314 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
315 | QDir dir (dirpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
316 | if (!dir.exists()) { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
317 | print ("Creating %1...\n", dirpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
318 | if (!dir.mkpath (dirpath)) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
319 | critical (fmt (tr ("Couldn't create the directory %1!"), dirpath)); |
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 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
322 | m_reply = m_nam->get (QNetworkRequest (QUrl (url))); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
323 | connect (m_reply, SIGNAL (finished()), this, SLOT (downloadFinished())); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
324 | connect (m_reply, SIGNAL (readyRead()), this, SLOT (readyRead())); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
325 | 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
|
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 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
329 | // ----------------------------------------------------------------------------- |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
330 | PartDownloadRequest::~PartDownloadRequest() {} |
426 | 331 | |
332 | // ============================================================================= | |
333 | // ----------------------------------------------------------------------------- | |
334 | void PartDownloadRequest::updateToTable() { | |
335 | QTableWidget* table = m_prompt->ui->progress; | |
428 | 336 | QProgressBar* prog; |
337 | ||
338 | switch (m_state) { | |
339 | case Requesting: | |
340 | case Downloading: | |
341 | prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), ProgressColumn)); | |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
342 | |
428 | 343 | if (!prog) { |
344 | prog = new QProgressBar; | |
345 | table->setCellWidget (tableRow(), ProgressColumn, prog); | |
346 | } | |
347 | ||
348 | prog->setRange (0, m_bytesTotal); | |
349 | prog->setValue (m_bytesRead); | |
350 | break; | |
351 | ||
352 | case Finished: | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
353 | case Failed: |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
354 | { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
355 | 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
|
356 | "<b><span style=\"color: #800\">FAILED</span></b>"); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
357 | lb->setAlignment (Qt::AlignCenter); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
358 | table->setCellWidget (tableRow(), ProgressColumn, lb); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
359 | } |
428 | 360 | break; |
426 | 361 | } |
362 | ||
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
363 | QLabel* lb = qobject_cast<QLabel*> (table->cellWidget (tableRow(), PartLabelColumn)); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
364 | 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
|
365 | lb = new QLabel (fmt ("<b>%1</b>", m_dest), table); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
366 | table->setCellWidget (tableRow(), PartLabelColumn, lb); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
367 | } |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
368 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
369 | // Make sure that the cell is big enough to contain the label |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
370 | if (table->columnWidth (PartLabelColumn) < lb->width()) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
371 | table->setColumnWidth (PartLabelColumn, lb->width()); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
372 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
373 | m_firstUpdate = false; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
374 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
375 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
376 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
377 | // ----------------------------------------------------------------------------- |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
378 | void PartDownloadRequest::downloadFinished() { |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
379 | if (m_reply->error() != QNetworkReply::NoError) { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
380 | if (m_primary && !m_prompt->aborted()) |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
381 | critical (m_reply->errorString()); |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
382 | |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
383 | m_state = Failed; |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
384 | } elif (state() != Failed) |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
385 | m_state = Finished; |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
386 | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
387 | m_bytesRead = m_bytesTotal; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
388 | updateToTable(); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
389 | |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
390 | if (m_fp) { |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
391 | m_fp->close(); |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
392 | delete m_fp; |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
393 | m_fp = null; |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
394 | |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
395 | if (m_state == Failed) |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
396 | QFile::remove (m_fpath); |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
397 | } |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
398 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
399 | if (m_state != Finished) { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
400 | m_prompt->checkIfFinished(); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
401 | return; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
402 | } |
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 | // Try to load this file now. |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
405 | LDFile* f = openDATFile (m_fpath, false); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
406 | if (!f) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
407 | return; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
408 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
409 | f->setImplicit (!m_primary); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
410 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
411 | // 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
|
412 | // 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
|
413 | // 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
|
414 | // it resolves dependencies. |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
415 | for (LDObject* obj : *f) { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
416 | if (obj->getType() != LDObject::Error) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
417 | continue; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
418 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
419 | LDErrorObject* err = static_cast<LDErrorObject*> (obj); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
420 | if (err->fileRef() == "") |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
421 | continue; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
422 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
423 | 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
|
424 | 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
|
425 | 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
|
426 | } |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
427 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
428 | if (m_primary) |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
429 | m_prompt->setPrimaryFile (f); |
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 | 466 | } |
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 | 488 | DEFINE_ACTION (DownloadFrom, 0) { |
489 | g_PartDownloader.download(); | |
490 | } |