1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2015 Teemu 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 #pragma once |
|
20 #include <QDialog> |
|
21 #include "main.h" |
|
22 #include "basics.h" |
|
23 #include "ldDocument.h" |
|
24 |
|
25 class LDDocument; |
|
26 class QFile; |
|
27 class PartDownloadRequest; |
|
28 class Ui_DownloadFrom; |
|
29 class QNetworkAccessManager; |
|
30 class QNetworkRequest; |
|
31 class QNetworkReply; |
|
32 class QAbstractButton; |
|
33 |
|
34 class PartDownloader : public QDialog, public HierarchyElement |
|
35 { |
|
36 Q_OBJECT |
|
37 |
|
38 public: |
|
39 enum SourceType |
|
40 { |
|
41 PartsTracker, |
|
42 CustomURL, |
|
43 }; |
|
44 |
|
45 enum Button |
|
46 { |
|
47 Download, |
|
48 Abort, |
|
49 Close |
|
50 }; |
|
51 |
|
52 enum TableColumn |
|
53 { |
|
54 PartLabelColumn, |
|
55 ProgressColumn, |
|
56 }; |
|
57 |
|
58 using RequestList = QList<PartDownloadRequest*>; |
|
59 |
|
60 explicit PartDownloader (QWidget* parent = nullptr); |
|
61 virtual ~PartDownloader(); |
|
62 |
|
63 void addFile (LDDocument* f); |
|
64 QPushButton* button (Button i); |
|
65 Q_SLOT void buttonClicked (QAbstractButton* btn); |
|
66 Q_SLOT void checkIfFinished(); |
|
67 void checkValidPath(); |
|
68 void downloadFile (QString dest, QString url, bool primary); |
|
69 void downloadFromPartsTracker (QString file); |
|
70 QString downloadPath(); |
|
71 bool isAborted() const; |
|
72 void modifyDestination (QString& dest) const; |
|
73 LDDocument* primaryFile() const; |
|
74 class QTableWidget* progressTable() const; |
|
75 void setPrimaryFile (LDDocument* document); |
|
76 void setSourceType (SourceType src); |
|
77 Q_SLOT void sourceChanged (int i); |
|
78 SourceType sourceType() const; |
|
79 QString url(); |
|
80 |
|
81 signals: |
|
82 void primaryFileDownloaded(); |
|
83 |
|
84 private: |
|
85 class Ui_DownloadFrom& ui; |
|
86 QStringList m_filesToDownload; |
|
87 RequestList m_requests; |
|
88 QPushButton* m_downloadButton; |
|
89 SourceType m_source; |
|
90 QList<LDDocument*> m_files; |
|
91 LDDocument* m_primaryFile; |
|
92 bool m_isAborted; |
|
93 }; |
|
94 |
|
95 class PartDownloadRequest : public QObject |
|
96 { |
|
97 Q_OBJECT |
|
98 |
|
99 public: |
|
100 enum class State |
|
101 { |
|
102 Requesting, |
|
103 Downloading, |
|
104 Finished, |
|
105 Failed, |
|
106 }; |
|
107 |
|
108 explicit PartDownloadRequest (QString url, QString dest, bool primary, PartDownloader* parent); |
|
109 virtual ~PartDownloadRequest(); |
|
110 |
|
111 Q_SLOT void abort(); |
|
112 QString destination() const; |
|
113 Q_SLOT void downloadFinished(); |
|
114 bool failed() const; |
|
115 QString filePath() const; |
|
116 bool isFinished() const; |
|
117 bool isFirstUpdate() const; |
|
118 bool isPrimary() const; |
|
119 QNetworkReply* networkReply() const; |
|
120 qint64 numBytesRead() const; |
|
121 qint64 numBytesTotal() const; |
|
122 PartDownloader* prompt() const; |
|
123 Q_SLOT void readFromNetworkReply(); |
|
124 void setTableRow (int value); |
|
125 int tableRow() const; |
|
126 Q_SLOT void updateDownloadProgress (qint64 recv, qint64 total); |
|
127 void updateToTable(); |
|
128 QString url() const; |
|
129 |
|
130 private: |
|
131 int m_tableRow; |
|
132 State m_state; |
|
133 PartDownloader* m_prompt; |
|
134 QString m_url; |
|
135 QString m_destination; |
|
136 QString m_filePath; |
|
137 QNetworkAccessManager* m_networkManager; |
|
138 QNetworkReply* m_networkReply; |
|
139 bool m_isFirstUpdate; |
|
140 bool m_isPrimary; |
|
141 qint64 m_numBytesRead; |
|
142 qint64 m_numBytesTotal; |
|
143 QFile* m_filePointer; |
|
144 }; |
|