| 1 | #pragma once |
| 2 | |
| 3 | // std |
| 4 | #include <deque> |
| 5 | #include <memory> |
| 6 | |
| 7 | // Qt |
| 8 | #include <QThread> |
| 9 | #include <QTimer> |
| 10 | #include <QNetworkReply> |
| 11 | #include <QPointer> |
| 12 | |
| 13 | #include <GUI/QtDNSClient.hxx> |
| 14 | #include <Main/options.hxx> |
| 15 | |
| 16 | // forward decls |
| 17 | class QNetworkAccessManager; |
| 18 | |
| 19 | namespace simgear { class ArchiveExtractor; } |
| 20 | |
| 21 | class DownloadTerrasyncSharedData : public QThread |
| 22 | { |
| 23 | Q_OBJECT |
| 24 | public: |
| 25 | DownloadTerrasyncSharedData(QObject* pr, QNetworkAccessManager* nam); |
| 26 | |
| 27 | ~DownloadTerrasyncSharedData(); |
| 28 | |
| 29 | signals: |
| 30 | |
| 31 | void extractionError(QString file, QString msg); |
| 32 | |
| 33 | void installProgress(QString fileName, int percent); |
| 34 | |
| 35 | void downloadProgress(quint64 cur, quint64 total); |
| 36 | |
| 37 | void failed(QString message); |
| 38 | |
| 39 | // emitted when we start downloading a new archive, for user feedback |
| 40 | void beginArchive(QString name); |
| 41 | protected: |
| 42 | void run() override; |
| 43 | |
| 44 | private slots: |
| 45 | void onDownloadProgress(quint64 got, quint64 total); |
| 46 | void onReplyFinished(); |
| 47 | void processBytes(); |
| 48 | void onNetworkError(QNetworkReply::NetworkError code); |
| 49 | |
| 50 | private: |
| 51 | struct DownloadItem |
| 52 | { |
| 53 | DownloadItem(QString uri, const SGPath& path, QString n) : |
| 54 | relativeURIPath(uri), terrasyncPath(path), archiveName(n) |
| 55 | {} |
| 56 | |
| 57 | QString relativeURIPath; |
| 58 | SGPath terrasyncPath; |
| 59 | QString archiveName; |
| 60 | }; |
| 61 | |
| 62 | void startNextDownload(); |
| 63 | void updateProgress(); |
| 64 | |
| 65 | QString m_serverUri; |
| 66 | |
| 67 | QNetworkAccessManager* m_networkManager = nullptr; |
| 68 | QtDNSClient* m_dns = nullptr; |
| 69 | std::mutex m_mutex; |
| 70 | std::condition_variable m_bufferWait; |
| 71 | QByteArray m_buffer; |
| 72 | quint64 m_extractedBytes = 0; |
| 73 | QTimer m_updateTimer; |
| 74 | bool m_error = false; |
| 75 | bool m_done = false; |
| 76 | QPointer<QNetworkReply> m_download; |
| 77 | std::unique_ptr<simgear::ArchiveExtractor> m_archive; |
| 78 | bool m_haveFirstMByte = false; |
| 79 | quint64 m_totalSize = 0; |
| 80 | uint32_t m_pathPrefixLength = 0; |
| 81 | |
| 82 | // current item is the front one |
| 83 | std::deque<DownloadItem> m_items; |
| 84 | }; |
| 85 |