Fri, 02 Aug 2013 22:02:40 +0300
now actually downloads parts
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" |
426 | 30 | |
31 | PartDownloader g_PartDownloader; | |
32 | ||
33 | constexpr const char* PartDownloader::k_OfficialURL, | |
34 | *PartDownloader::k_UnofficialURL; | |
35 | ||
36 | // ============================================================================= | |
37 | // ----------------------------------------------------------------------------- | |
38 | void PartDownloader::download() { | |
39 | PartDownloadPrompt* dlg = new PartDownloadPrompt; | |
40 | dlg->exec(); | |
41 | } | |
42 | ||
43 | // ============================================================================= | |
44 | // ----------------------------------------------------------------------------- | |
45 | PartDownloadPrompt::PartDownloadPrompt (QWidget* parent) : QDialog (parent) { | |
46 | ui = new Ui_DownloadFrom; | |
47 | ui->setupUi (this); | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
48 | ui->fname->setFocus(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
49 | |
426 | 50 | connect (ui->source, SIGNAL (currentIndexChanged (int)), this, SLOT (sourceChanged (int))); |
51 | connect (ui->buttonBox, SIGNAL (accepted()), this, SLOT (startDownload())); | |
52 | } | |
53 | ||
54 | // ============================================================================= | |
55 | // ----------------------------------------------------------------------------- | |
56 | PartDownloadPrompt::~PartDownloadPrompt() { | |
57 | delete ui; | |
58 | } | |
59 | ||
60 | // ============================================================================= | |
61 | // ----------------------------------------------------------------------------- | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
62 | str PartDownloadPrompt::getURL() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
63 | const Source src = getSource(); |
426 | 64 | |
65 | switch (src) { | |
66 | case OfficialLibrary: | |
67 | case PartsTracker: | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
68 | if (src == OfficialLibrary) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
69 | return str (PartDownloader::k_OfficialURL) + getDest(); |
426 | 70 | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
71 | return str (PartDownloader::k_UnofficialURL) + getDest(); |
426 | 72 | |
73 | case CustomURL: | |
74 | return ui->fname->text(); | |
75 | } | |
76 | ||
77 | // Shouldn't happen | |
78 | return ""; | |
79 | } | |
80 | ||
81 | // ============================================================================= | |
82 | // ----------------------------------------------------------------------------- | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
83 | str PartDownloadPrompt::getDest() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
84 | str fname = ui->fname->text(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
85 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
86 | if (getSource() == CustomURL) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
87 | fname = basename (fname); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
88 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
89 | // Ensure .dat extension |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
90 | if (fname.right (4) != ".dat") { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
91 | // 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
|
92 | // typo in the .dat extension. |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
93 | if (fname.lastIndexOf (".") >= fname.length() - 4) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
94 | fname.chop (fname.length() - fname.lastIndexOf (".")); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
95 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
96 | fname += ".dat"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
97 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
98 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
99 | /* 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
|
100 | * 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
|
101 | * 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
|
102 | * are numbers which can be followed by: |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
103 | * - c** (composites) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
104 | * - d** (formed stickers) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
105 | * - a lowercase alphabetic letter for variants |
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 | * 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
|
108 | * 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
|
109 | * file names. |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
110 | */ |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
111 | { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
112 | str partRegex = "^[0-9]+(c[0-9][0-9]+)*(d[0-9][0-9]+)*[a-z]?"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
113 | str subpartRegex = partRegex + "s[0-9][0-9]+"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
114 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
115 | partRegex += "\\.dat$"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
116 | subpartRegex += "\\.dat$"; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
117 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
118 | if (QRegExp (subpartRegex).exactMatch (fname)) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
119 | fname.prepend ("parts/s/"); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
120 | elif (QRegExp (partRegex).exactMatch (fname)) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
121 | fname.prepend ("parts/"); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
122 | elif (fname.left (6) != "parts/" && fname.left (2) != "p/") |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
123 | fname.prepend ("p/"); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
124 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
125 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
126 | return fname; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
127 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
128 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
129 | // ============================================================================= |
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 | str PartDownloadPrompt::fullFilePath() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
132 | return "/home/arezey/ldraw/downloads/" + getDest(); // FIXME: hehe |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
133 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
134 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
135 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
136 | // ----------------------------------------------------------------------------- |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
137 | PartDownloadPrompt::Source PartDownloadPrompt::getSource() const { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
138 | return (Source) ui->source->currentIndex(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
139 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
140 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
141 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
142 | // ----------------------------------------------------------------------------- |
426 | 143 | void PartDownloadPrompt::sourceChanged (int i) { |
144 | if (i == CustomURL) | |
145 | ui->fileNameLabel->setText (tr ("URL:")); | |
146 | else | |
147 | ui->fileNameLabel->setText (tr ("File name:")); | |
148 | } | |
149 | ||
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
150 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
151 | // ----------------------------------------------------------------------------- |
426 | 152 | void PartDownloadPrompt::startDownload() { |
153 | int row = ui->progress->rowCount(); | |
154 | ui->progress->setEnabled (true); | |
155 | ui->buttonBox->setEnabled (false); | |
156 | ui->progress->insertRow (row); | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
157 | ui->fname->setEnabled (false); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
158 | ui->source->setEnabled (false); |
426 | 159 | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
160 | PartDownloadRequest* req = new PartDownloadRequest (getURL(), getDest(), this); |
426 | 161 | req->setTableRow (row); |
162 | req->updateToTable(); | |
163 | } | |
164 | ||
165 | // ============================================================================= | |
166 | // ----------------------------------------------------------------------------- | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
167 | PartDownloadRequest::PartDownloadRequest (str url, str dest, PartDownloadPrompt* parent) : |
426 | 168 | QObject (parent), |
169 | m_prompt (parent), | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
170 | m_url (url), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
171 | m_dest (dest), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
172 | m_fpath (m_prompt->fullFilePath()), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
173 | m_nam (new QNetworkAccessManager), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
174 | m_firstUpdate (true), |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
175 | m_state (Requesting) |
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 | // Make sure that we have a valid destination. |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
178 | str dirpath = dirname (m_fpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
179 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
180 | QDir dir (dirpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
181 | if (!dir.exists()) { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
182 | print ("Creating %1...\n", dirpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
183 | if (!dir.mkpath (dirpath)) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
184 | critical (fmt (tr ("Couldn't create the directory %1!"), dirpath)); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
185 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
186 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
187 | m_reply = m_nam->get (QNetworkRequest (QUrl (url))); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
188 | connect (m_reply, SIGNAL (finished()), this, SLOT (downloadFinished())); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
189 | connect (m_reply, SIGNAL (readyRead()), this, SLOT (readyRead())); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
190 | 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
|
191 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
192 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
193 | // ============================================================================= |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
194 | // ----------------------------------------------------------------------------- |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
195 | PartDownloadRequest::~PartDownloadRequest() { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
196 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
197 | } |
426 | 198 | |
199 | // ============================================================================= | |
200 | // ----------------------------------------------------------------------------- | |
201 | void PartDownloadRequest::updateToTable() { | |
202 | QTableWidget* table = m_prompt->ui->progress; | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
203 | QProgressBar* progressBar = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), ProgressColumn)); |
426 | 204 | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
205 | if (!progressBar) { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
206 | progressBar = new QProgressBar; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
207 | table->setCellWidget (tableRow(), ProgressColumn, progressBar); |
426 | 208 | } |
209 | ||
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
210 | progressBar->setRange (0, m_bytesTotal); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
211 | progressBar->setValue (m_bytesRead); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
212 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
213 | if (m_firstUpdate) { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
214 | QLabel* lb = new QLabel (fmt ("<b>%1</b><br>%2", m_dest, m_url), table); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
215 | table->setCellWidget (tableRow(), PartLabelColumn, lb); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
216 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
217 | // Make sure that the cell is big enough to contain the label |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
218 | if (table->columnWidth (PartLabelColumn) < lb->width()) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
219 | table->setColumnWidth (PartLabelColumn, lb->width()); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
220 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
221 | table->setRowHeight (tableRow(), lb->height()); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
222 | m_firstUpdate = false; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
223 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
224 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
225 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
226 | void PartDownloadRequest::downloadFinished() { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
227 | m_state = Finished; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
228 | m_bytesRead = m_bytesTotal; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
229 | updateToTable(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
230 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
231 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
232 | void PartDownloadRequest::downloadProgress (int64 recv, int64 total) { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
233 | m_bytesRead = recv; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
234 | m_bytesTotal = total; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
235 | m_state = Downloading; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
236 | updateToTable(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
237 | } |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
238 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
239 | void PartDownloadRequest::readyRead() { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
240 | QFile f (m_fpath); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
241 | if (!f.open (QIODevice::Append)) |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
242 | return; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
243 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
244 | f.write (m_reply->readAll()); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
245 | f.close(); |
426 | 246 | } |
247 | ||
248 | // ============================================================================= | |
249 | // ----------------------------------------------------------------------------- | |
250 | DEFINE_ACTION (DownloadFrom, 0) { | |
251 | g_PartDownloader.download(); | |
252 | } |