| 1 | #pragma once |
| 2 | |
| 3 | #include "config.h" |
| 4 | |
| 5 | // Qt |
| 6 | #include <QTimer> |
| 7 | |
| 8 | #include <GUI/QtDNSClient.hxx> |
| 9 | #include <Main/options.hxx> |
| 10 | |
| 11 | #include <simgear/io/HTTPClient.hxx> |
| 12 | #include <simgear/io/HTTPRepository.hxx> |
| 13 | |
| 14 | using simgear::HTTPRepository; |
| 15 | |
| 16 | |
| 17 | class UpdateFGData : public QObject |
| 18 | { |
| 19 | Q_OBJECT |
| 20 | public: |
| 21 | UpdateFGData(QObject* pr) : QObject(pr), |
| 22 | m_dns(new QtDNSClient(this, "fgdata")) |
| 23 | { |
| 24 | m_updateTimer.setInterval(20); |
| 25 | |
| 26 | connect(m_dns, &QtDNSClient::finished, [this]() { |
| 27 | auto baseServer = m_dns->result(); |
| 28 | baseServer += QString("/fgdata_%1_%2").arg(FLIGHTGEAR_MAJOR_VERSION).arg(FLIGHTGEAR_MINOR_VERSION); |
| 29 | m_updateServerUri = baseServer.toStdString(); |
| 30 | qInfo() << Q_FUNC_INFO << "will update from" << baseServer; |
| 31 | createRepository(); |
| 32 | }); |
| 33 | |
| 34 | connect(m_dns, &QtDNSClient::failed, [this](QString msg) { |
| 35 | emit failed(tr("Update of data files failed due to a DNS error: %1").arg(msg)); |
| 36 | }); |
| 37 | |
| 38 | m_dns->makeDNSRequest(); |
| 39 | } |
| 40 | |
| 41 | void createRepository() |
| 42 | { |
| 43 | const auto rp = flightgear::Options::sharedInstance()->downloadedDataRoot(); |
| 44 | m_repo.reset(new simgear::HTTPRepository(rp, &m_http)); |
| 45 | m_repo->setBaseUrl(m_updateServerUri); |
| 46 | // in rapid RC scenarios, this can cause us to skip updates we need, and |
| 47 | // then fail to check again later. |
| 48 | m_repo->setRecheckTimeoutEnabled(false); |
| 49 | m_repo->setMaximumPermittedFailureCount(0); |
| 50 | |
| 51 | m_repo->update(); |
| 52 | |
| 53 | connect(&m_updateTimer, &QTimer::timeout, this, &UpdateFGData::onUpdateRepo); |
| 54 | m_updateTimer.start(); |
| 55 | } |
| 56 | |
| 57 | void onUpdateRepo() |
| 58 | { |
| 59 | m_repo->process(); |
| 60 | m_http.update(); |
| 61 | |
| 62 | |
| 63 | if (!m_repo->isDoingSync()) { |
| 64 | qInfo() << Q_FUNC_INFO << "finished sync"; |
| 65 | m_updateTimer.stop(); |
| 66 | emit finished(); |
| 67 | } |
| 68 | |
| 69 | const auto status = m_repo->failure(); |
| 70 | if (status != HTTPRepository::REPO_NO_ERROR) { |
| 71 | m_error = true; |
| 72 | QString errMsg = QString::fromStdString(m_repo->resultCodeAsString(status)); |
| 73 | m_updateTimer.stop(); |
| 74 | emit failed(errMsg); |
| 75 | } |
| 76 | |
| 77 | const auto dlBytes = m_repo->bytesDownloaded(); |
| 78 | emit downloadProgress(dlBytes, dlBytes + m_repo->bytesToDownload()); |
| 79 | |
| 80 | const auto lp = m_repo->lastCheckedPath(); |
| 81 | emit installProgress(tr("Updated %1").arg(QString::fromStdString(lp.utf8Str())), -1); |
| 82 | } |
| 83 | |
| 84 | ~UpdateFGData() = default; |
| 85 | |
| 86 | signals: |
| 87 | void finished(); |
| 88 | |
| 89 | void extractionError(QString file, QString msg); |
| 90 | |
| 91 | void installProgress(QString fileName, int percent); |
| 92 | |
| 93 | void downloadProgress(quint64 cur, quint64 total); |
| 94 | |
| 95 | void failed(QString message); |
| 96 | |
| 97 | private: |
| 98 | QtDNSClient* m_dns; |
| 99 | std::string m_updateServerUri; |
| 100 | |
| 101 | QTimer m_updateTimer; |
| 102 | |
| 103 | std::unique_ptr<simgear::HTTPRepository> m_repo; |
| 104 | simgear::HTTP::Client m_http; |
| 105 | |
| 106 | QUrl m_downloadUrl; |
| 107 | |
| 108 | bool m_error = false; |
| 109 | }; |
| 110 |