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