Sat, 03 Aug 2013 12:31:36 +0300
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
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; | |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
33 | class QAbstractButton; |
426 | 34 | |
35 | // ============================================================================= | |
36 | // ----------------------------------------------------------------------------- | |
435
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
37 | class PartDownloader : public QDialog { |
426 | 38 | Q_OBJECT |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
39 | PROPERTY (LDFile*, primaryFile, setPrimaryFile) |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
40 | PROPERTY (bool, aborted, setAborted) |
426 | 41 | |
42 | public: | |
435
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
43 | constexpr static const char* k_OfficialURL = "http://ldraw.org/library/official/", |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
44 | *k_UnofficialURL = "http://ldraw.org/library/unofficial/"; |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
45 | |
426 | 46 | enum Source { |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
47 | PartsTracker, |
426 | 48 | CustomURL, |
49 | }; | |
50 | ||
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
51 | enum Button { |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
52 | Download, |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
53 | Abort, |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
54 | Close |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
55 | }; |
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
56 | |
435
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
57 | enum TableColumn { |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
58 | PartLabelColumn, |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
59 | ProgressColumn, |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
60 | }; |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
61 | |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
62 | explicit PartDownloader (QWidget* parent = null); |
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
63 | virtual ~PartDownloader(); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
64 | str getURL() const; |
435
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
65 | static str getDownloadPath(); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
66 | 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
|
67 | 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
|
68 | void modifyDest (str& dest) const; |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
69 | QPushButton* getButton (Button i); |
435
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
70 | static void k_download(); |
426 | 71 | |
72 | public slots: | |
73 | void sourceChanged (int i); | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
74 | void checkIfFinished(); |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
75 | void buttonClicked (QAbstractButton* btn); |
426 | 76 | |
77 | protected: | |
78 | Ui_DownloadFrom* ui; | |
79 | friend class PartDownloadRequest; | |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
80 | |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
81 | private: |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
82 | List<str> m_filesToDownload; |
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
83 | List<PartDownloadRequest*> m_requests; |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
84 | QPushButton* m_downloadButton; |
426 | 85 | }; |
86 | ||
87 | // ============================================================================= | |
88 | // ----------------------------------------------------------------------------- | |
89 | class PartDownloadRequest : public QObject { | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
90 | Q_OBJECT |
426 | 91 | PROPERTY (int, tableRow, setTableRow) |
92 | ||
93 | public: | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
94 | enum State { |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
95 | Requesting, |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
96 | Downloading, |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
97 | Finished, |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
98 | Failed, |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
99 | }; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
100 | |
435
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
101 | explicit PartDownloadRequest (str url, str dest, bool primary, PartDownloader* parent); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
102 | PartDownloadRequest (const PartDownloadRequest&) = delete; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
103 | virtual ~PartDownloadRequest(); |
426 | 104 | void updateToTable(); |
429
3488534b2b31
More work on downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
428
diff
changeset
|
105 | bool isFinished() const; |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
106 | const State& state() const; |
426 | 107 | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
108 | void operator= (const PartDownloadRequest&) = delete; |
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 | public slots: |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
111 | void downloadFinished(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
112 | void readyRead(); |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
113 | void downloadProgress (qint64 recv, qint64 total); |
432
ef382b98a8af
finalized downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
431
diff
changeset
|
114 | void abort(); |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
115 | |
426 | 116 | private: |
435
bc3a51394953
Merged PartDownloader into the prompt and renamed the prompt to PartDownloader
Santeri Piippo <crimsondusk64@gmail.com>
parents:
432
diff
changeset
|
117 | PartDownloader* m_prompt; |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
118 | str m_url, m_dest, m_fpath; |
426 | 119 | QNetworkAccessManager* m_nam; |
120 | QNetworkReply* m_reply; | |
427
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
121 | bool m_firstUpdate; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
122 | State m_state; |
d308149fbc90
now actually downloads parts
Santeri Piippo <crimsondusk64@gmail.com>
parents:
426
diff
changeset
|
123 | int64 m_bytesRead, m_bytesTotal; |
428 | 124 | bool m_primary; |
431
ec1e2059319b
stability to downloading
Santeri Piippo <crimsondusk64@gmail.com>
parents:
430
diff
changeset
|
125 | QFile* m_fp; |
426 | 126 | }; |
127 | ||
128 | #endif // LDFORGE_DOWNLOAD_H |