Sat, 03 Aug 2013 02:18:41 +0300
stability to 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 | ||
19 | #ifndef LDFORGE_DOWNLOAD_H | |
20 | #define LDFORGE_DOWNLOAD_H | |
21 | ||
22 | #include <QDialog> | |
23 | #include "common.h" | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
24 | #include "types.h" |
426 | 25 | |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
26 | class LDFile; |
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
27 | class QFile; |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
28 | class PartDownloadRequest; |
426 | 29 | class Ui_DownloadFrom; |
30 | class QNetworkAccessManager; | |
31 | class QNetworkRequest; | |
32 | class QNetworkReply; | |
33 | ||
34 | // ============================================================================= | |
35 | // ----------------------------------------------------------------------------- | |
36 | extern class PartDownloader { | |
37 | public: | |
38 | constexpr static const char* k_OfficialURL = "http://ldraw.org/library/official/", | |
39 | *k_UnofficialURL = "http://ldraw.org/library/unofficial/"; | |
40 | ||
41 | PartDownloader() {} | |
42 | void download(); | |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
43 | static str getDownloadPath(); |
426 | 44 | void operator()() { download(); } |
45 | } g_PartDownloader; | |
46 | ||
47 | // ============================================================================= | |
48 | // ----------------------------------------------------------------------------- | |
49 | class PartDownloadPrompt : public QDialog { | |
50 | Q_OBJECT | |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
51 | PROPERTY (LDFile*, primaryFile, setPrimaryFile) |
426 | 52 | |
53 | public: | |
54 | enum Source { | |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
55 | PartsTracker, |
426 | 56 | CustomURL, |
57 | }; | |
58 | ||
59 | explicit PartDownloadPrompt (QWidget* parent = null); | |
60 | virtual ~PartDownloadPrompt(); | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
61 | str getURL() const; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
62 | Source getSource() const; |
430
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
63 | void downloadFile (str dest, str url, bool primary); |
8458cf2719d1
added config option, refined logic and regexps. This behaves coherently now. :)
Santeri Piippo <crimsondusk64@gmail.com>
parents:
429
diff
changeset
|
64 | void modifyDest (str& dest) const; |
426 | 65 | |
66 | public slots: | |
67 | void sourceChanged (int i); | |
68 | void startDownload(); | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
69 | void checkIfFinished(); |
426 | 70 | |
71 | protected: | |
72 | Ui_DownloadFrom* ui; | |
73 | friend class PartDownloadRequest; | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
74 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
75 | private: |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
76 | List<str> m_filesToDownload; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
77 | List<PartDownloadRequest*> m_requests; |
426 | 78 | }; |
79 | ||
80 | // ============================================================================= | |
81 | // ----------------------------------------------------------------------------- | |
82 | class PartDownloadRequest : public QObject { | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
83 | Q_OBJECT |
426 | 84 | PROPERTY (int, tableRow, setTableRow) |
85 | ||
86 | public: | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
87 | enum TableColumn { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
88 | PartLabelColumn, |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
89 | ProgressColumn, |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
90 | }; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
91 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
92 | enum State { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
93 | Requesting, |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
94 | Downloading, |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
95 | Finished, |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
96 | Failed, |
427
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 | |
428 | 99 | explicit PartDownloadRequest (str url, str dest, bool primary, PartDownloadPrompt* parent); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
100 | PartDownloadRequest (const PartDownloadRequest&) = delete; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
101 | virtual ~PartDownloadRequest(); |
426 | 102 | void updateToTable(); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
103 | bool isFinished() const; |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
104 | const State& state() const; |
426 | 105 | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
106 | void operator= (const PartDownloadRequest&) = delete; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
107 | |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
108 | public slots: |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
109 | void downloadFinished(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
110 | void readyRead(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
111 | void downloadProgress (qint64 recv, qint64 total); |
428 | 112 | void downloadError(); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
113 | |
426 | 114 | private: |
115 | PartDownloadPrompt* m_prompt; | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
116 | str m_url, m_dest, m_fpath; |
426 | 117 | QNetworkAccessManager* m_nam; |
118 | QNetworkReply* m_reply; | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
119 | bool m_firstUpdate; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
120 | State m_state; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
121 | int64 m_bytesRead, m_bytesTotal; |
428 | 122 | bool m_primary; |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
123 | QFile* m_fp; |
426 | 124 | }; |
125 | ||
126 | #endif // LDFORGE_DOWNLOAD_H |