Sat, 03 Aug 2013 00:56:02 +0300
added config option, refined logic and regexps. This behaves coherently now. :)
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> |
426 | 24 | #include "download.h" |
25 | #include "ui_downloadfrom.h" | |
26 | #include "types.h" | |
27 | #include "gui.h" | |
28 | #include "build/moc_download.cpp" | |
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" |
426 | 31 | |
32 | PartDownloader g_PartDownloader; | |
33 | ||
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
34 | cfg (str, net_downloadpath, ""); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
35 | |
426 | 36 | constexpr const char* PartDownloader::k_OfficialURL, |
37 | *PartDownloader::k_UnofficialURL; | |
38 | ||
39 | // ============================================================================= | |
40 | // ----------------------------------------------------------------------------- | |
41 | 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
|
42 | str path = getDownloadPath(); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
43 | 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
|
44 | 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
|
45 | "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
|
46 | return; |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
47 | } |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
48 | |
426 | 49 | PartDownloadPrompt* dlg = new PartDownloadPrompt; |
50 | dlg->exec(); | |
51 | } | |
52 | ||
53 | // ============================================================================= | |
54 | // ----------------------------------------------------------------------------- | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
55 | str PartDownloader::getDownloadPath() { |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
56 | 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
|
57 | |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
58 | #if DIRSLASH_CHAR != '/' |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
59 | path.replace (DIRSLASH, "/"); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
60 | #endif |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
61 | |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
62 | return path; |
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 | |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
65 | // ============================================================================= |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
66 | // ----------------------------------------------------------------------------- |
426 | 67 | PartDownloadPrompt::PartDownloadPrompt (QWidget* parent) : QDialog (parent) { |
68 | ui = new Ui_DownloadFrom; | |
69 | ui->setupUi (this); | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
70 | ui->fname->setFocus(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
71 | |
426 | 72 | connect (ui->source, SIGNAL (currentIndexChanged (int)), this, SLOT (sourceChanged (int))); |
73 | connect (ui->buttonBox, SIGNAL (accepted()), this, SLOT (startDownload())); | |
74 | } | |
75 | ||
76 | // ============================================================================= | |
77 | // ----------------------------------------------------------------------------- | |
78 | PartDownloadPrompt::~PartDownloadPrompt() { | |
79 | delete ui; | |
80 | } | |
81 | ||
82 | // ============================================================================= | |
83 | // ----------------------------------------------------------------------------- | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
84 | str PartDownloadPrompt::getURL() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
85 | 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
|
86 | str dest; |
426 | 87 | |
88 | switch (src) { | |
89 | case PartsTracker: | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
90 | 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
|
91 | modifyDest (dest); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
92 | return str (PartDownloader::k_UnofficialURL) + dest; |
426 | 93 | |
94 | case CustomURL: | |
95 | return ui->fname->text(); | |
96 | } | |
97 | ||
98 | // Shouldn't happen | |
99 | return ""; | |
100 | } | |
101 | ||
102 | // ============================================================================= | |
103 | // ----------------------------------------------------------------------------- | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
104 | 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
|
105 | dest = dest.simplified(); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
106 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
107 | if (getSource() == CustomURL) |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
108 | dest = basename (dest); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
109 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
110 | // 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
|
111 | if (dest.right (4) != ".dat") { |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
112 | // 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
|
113 | // 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
|
114 | 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
|
115 | 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
|
116 | dest.chop (dest.length() - dotpos); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
117 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
118 | dest += ".dat"; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
119 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
120 | |
428 | 121 | // If the part starts with s\ or s/, then use parts/s/. Same goes with |
122 | // 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
|
123 | 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
|
124 | 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
|
125 | 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
|
126 | } 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
|
127 | 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
|
128 | dest.prepend ("p/48/"); |
428 | 129 | } |
130 | ||
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
131 | /* 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
|
132 | * 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
|
133 | * 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
|
134 | * are numbers which can be followed by: |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
135 | * - c** (composites) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
136 | * - d** (formed stickers) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
137 | * - a lowercase alphabetic letter for variants |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
138 | * |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
139 | * 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
|
140 | * 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
|
141 | * file names. |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
142 | */ |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
143 | { |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
144 | 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
|
145 | str subpartRegex = partRegex + "s[0-9][0-9]+"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
146 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
147 | partRegex += "\\.dat$"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
148 | subpartRegex += "\\.dat$"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
149 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
150 | 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
|
151 | 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
|
152 | 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
|
153 | dest.prepend ("parts/"); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
154 | 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
|
155 | dest.prepend ("p/"); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
156 | } |
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 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
159 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
160 | // ----------------------------------------------------------------------------- |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
161 | PartDownloadPrompt::Source PartDownloadPrompt::getSource() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
162 | return (Source) ui->source->currentIndex(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
163 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
164 | |
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 | // ----------------------------------------------------------------------------- |
426 | 167 | void PartDownloadPrompt::sourceChanged (int i) { |
168 | if (i == CustomURL) | |
169 | ui->fileNameLabel->setText (tr ("URL:")); | |
170 | else | |
171 | ui->fileNameLabel->setText (tr ("File name:")); | |
172 | } | |
173 | ||
427
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 | // ----------------------------------------------------------------------------- |
426 | 176 | void PartDownloadPrompt::startDownload() { |
177 | ui->progress->setEnabled (true); | |
178 | ui->buttonBox->setEnabled (false); | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
179 | ui->fname->setEnabled (false); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
180 | ui->source->setEnabled (false); |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
181 | |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
182 | str 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
|
183 | modifyDest (dest); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
184 | downloadFile (dest, getURL(), true); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
185 | } |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
186 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
187 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
188 | // ----------------------------------------------------------------------------- |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
189 | void PartDownloadPrompt::downloadFile (str dest, str url, bool primary) { |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
190 | const int row = ui->progress->rowCount(); |
426 | 191 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
192 | // 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
|
193 | if (m_filesToDownload.find (dest) != -1u) |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
194 | return; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
195 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
196 | modifyDest (dest); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
197 | 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
|
198 | PartDownloadRequest* req = new PartDownloadRequest (url, dest, primary, this); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
199 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
200 | m_filesToDownload << dest; |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
201 | m_requests << req; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
202 | ui->progress->insertRow (row); |
426 | 203 | req->setTableRow (row); |
204 | req->updateToTable(); | |
205 | } | |
206 | ||
207 | // ============================================================================= | |
208 | // ----------------------------------------------------------------------------- | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
209 | void PartDownloadPrompt::checkIfFinished() { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
210 | // If there is some download still working, we're not finished. |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
211 | for (PartDownloadRequest* req : m_requests) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
212 | if (!req->isFinished()) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
213 | return; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
214 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
215 | // Update everything now |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
216 | reloadAllSubfiles(); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
217 | g_win->fullRefresh(); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
218 | g_win->R()->resetAngles(); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
219 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
220 | // Allow the prompt be closed now. |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
221 | ui->buttonBox->disconnect (SIGNAL (accepted())); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
222 | connect (ui->buttonBox, SIGNAL (accepted()), this, SLOT (accept())); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
223 | ui->buttonBox->setEnabled (true); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
224 | ui->progress->setEnabled (false); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
225 | |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
226 | // accept(); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
227 | } |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
228 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
229 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
230 | // ----------------------------------------------------------------------------- |
428 | 231 | PartDownloadRequest::PartDownloadRequest (str url, str dest, bool primary, PartDownloadPrompt* parent) : |
426 | 232 | QObject (parent), |
233 | m_prompt (parent), | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
234 | m_url (url), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
235 | 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
|
236 | m_fpath (PartDownloader::getDownloadPath() + dest), |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
237 | m_nam (new QNetworkAccessManager), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
238 | m_firstUpdate (true), |
428 | 239 | m_state (Requesting), |
240 | m_primary (primary) | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
241 | { |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
242 | m_fpath.replace ("\\", "/"); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
243 | QFile::remove (m_fpath); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
244 | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
245 | // Make sure that we have a valid destination. |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
246 | str dirpath = dirname (m_fpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
247 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
248 | QDir dir (dirpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
249 | if (!dir.exists()) { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
250 | print ("Creating %1...\n", dirpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
251 | if (!dir.mkpath (dirpath)) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
252 | critical (fmt (tr ("Couldn't create the directory %1!"), dirpath)); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
253 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
254 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
255 | m_reply = m_nam->get (QNetworkRequest (QUrl (url))); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
256 | connect (m_reply, SIGNAL (finished()), this, SLOT (downloadFinished())); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
257 | connect (m_reply, SIGNAL (readyRead()), this, SLOT (readyRead())); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
258 | connect (m_reply, SIGNAL (downloadProgress (qint64, qint64)), this, SLOT (downloadProgress (qint64, qint64))); |
428 | 259 | connect (m_reply, SIGNAL (error (QNetworkReply::NetworkError)), this, SLOT (downloadError())); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
260 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
261 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
262 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
263 | // ----------------------------------------------------------------------------- |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
264 | PartDownloadRequest::~PartDownloadRequest() {} |
426 | 265 | |
266 | // ============================================================================= | |
267 | // ----------------------------------------------------------------------------- | |
268 | void PartDownloadRequest::updateToTable() { | |
269 | QTableWidget* table = m_prompt->ui->progress; | |
428 | 270 | QProgressBar* prog; |
271 | ||
272 | switch (m_state) { | |
273 | case Requesting: | |
274 | case Downloading: | |
275 | prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), ProgressColumn)); | |
426 | 276 | |
428 | 277 | if (!prog) { |
278 | prog = new QProgressBar; | |
279 | table->setCellWidget (tableRow(), ProgressColumn, prog); | |
280 | } | |
281 | ||
282 | prog->setRange (0, m_bytesTotal); | |
283 | prog->setValue (m_bytesRead); | |
284 | break; | |
285 | ||
286 | case Finished: | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
287 | case Failed: |
428 | 288 | case Aborted: |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
289 | { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
290 | 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
|
291 | "<b><span style=\"color: #800\">FAILED</span></b>"); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
292 | lb->setAlignment (Qt::AlignCenter); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
293 | table->setCellWidget (tableRow(), ProgressColumn, lb); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
294 | } |
428 | 295 | break; |
426 | 296 | } |
297 | ||
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
298 | QLabel* lb = qobject_cast<QLabel*> (table->cellWidget (tableRow(), PartLabelColumn)); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
299 | 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
|
300 | 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
|
301 | table->setCellWidget (tableRow(), PartLabelColumn, lb); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
302 | } |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
303 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
304 | // 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
|
305 | if (table->columnWidth (PartLabelColumn) < lb->width()) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
306 | table->setColumnWidth (PartLabelColumn, lb->width()); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
307 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
308 | m_firstUpdate = false; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
309 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
310 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
311 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
312 | // ----------------------------------------------------------------------------- |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
313 | void PartDownloadRequest::downloadFinished() { |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
314 | m_state = m_reply->error() == QNetworkReply::NoError ? Finished : Failed; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
315 | m_bytesRead = m_bytesTotal; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
316 | updateToTable(); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
317 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
318 | if (m_state != Finished) { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
319 | m_prompt->checkIfFinished(); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
320 | return; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
321 | } |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
322 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
323 | // Try to load this file now. |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
324 | LDFile* f = openDATFile (m_fpath, false); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
325 | if (!f) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
326 | return; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
327 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
328 | f->setImplicit (!m_primary); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
329 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
330 | // 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
|
331 | // 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
|
332 | // 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
|
333 | // it resolves dependencies. |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
334 | for (LDObject* obj : *f) { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
335 | if (obj->getType() != LDObject::Error) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
336 | continue; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
337 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
338 | LDErrorObject* err = static_cast<LDErrorObject*> (obj); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
339 | if (err->fileRef() == "") |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
340 | continue; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
341 | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
342 | 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
|
343 | 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
|
344 | 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
|
345 | } |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
346 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
347 | if (m_primary) |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
348 | LDFile::setCurrent (f); |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
349 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
350 | m_prompt->checkIfFinished(); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
351 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
352 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
353 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
354 | // ----------------------------------------------------------------------------- |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
355 | void PartDownloadRequest::downloadProgress (int64 recv, int64 total) { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
356 | m_bytesRead = recv; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
357 | m_bytesTotal = total; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
358 | m_state = Downloading; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
359 | updateToTable(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
360 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
361 | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
362 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
363 | // ----------------------------------------------------------------------------- |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
364 | void PartDownloadRequest::readyRead() { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
365 | QFile f (m_fpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
366 | if (!f.open (QIODevice::Append)) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
367 | return; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
368 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
369 | f.write (m_reply->readAll()); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
370 | f.close(); |
426 | 371 | } |
372 | ||
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 | // ----------------------------------------------------------------------------- |
428 | 375 | void PartDownloadRequest::downloadError() { |
376 | if (m_primary) | |
377 | critical (m_reply->errorString()); | |
378 | ||
379 | QFile::remove (m_fpath); | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
380 | m_state = Failed; |
428 | 381 | updateToTable(); |
382 | } | |
383 | ||
426 | 384 | // ============================================================================= |
385 | // ----------------------------------------------------------------------------- | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
386 | bool PartDownloadRequest::isFinished() const { |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
387 | return m_state == Finished || m_state == Failed || m_state == Aborted; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
388 | } |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
389 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
390 | // ============================================================================= |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
391 | // ----------------------------------------------------------------------------- |
426 | 392 | DEFINE_ACTION (DownloadFrom, 0) { |
393 | g_PartDownloader.download(); | |
394 | } |