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