From c26acdd6e590e3aec46abd62dc204011718acd8c Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 25 May 2016 21:48:06 +0100 Subject: [PATCH] Fallback URL when official catalog is not found. Tolerate the case where a URL matching the exact FG version is not found; lookup a generic URL, in the hope it supports our version (additional changes will make this more likely) --- src/GUI/AddCatalogDialog.cxx | 26 ++++++-- src/GUI/PathsDialog.cxx | 2 +- src/Network/HTTPClient.cxx | 119 +++++++++-------------------------- src/Network/HTTPClient.hxx | 8 +-- 4 files changed, 56 insertions(+), 99 deletions(-) diff --git a/src/GUI/AddCatalogDialog.cxx b/src/GUI/AddCatalogDialog.cxx index 95a499bd9..b533093ed 100644 --- a/src/GUI/AddCatalogDialog.cxx +++ b/src/GUI/AddCatalogDialog.cxx @@ -24,6 +24,8 @@ #include #include +#include
+#include #include using namespace simgear::pkg; @@ -94,20 +96,24 @@ void AddCatalogDialog::updateUi() } if (m_state == STATE_FINISHED) { - QString catDesc = QString::fromStdString(m_result->description()); + QString catDesc = QString::fromStdString(m_result->name()); QString s = tr("Successfully retrieved aircraft information from '%1'. " "%2 aircraft are included in this hangar.").arg(catDesc).arg(m_result->packages().size()); ui->resultsSummaryLabel->setText(s); } else if (m_state == STATE_DOWNLOAD_FAILED) { Delegate::StatusCode code = m_result->status(); - qWarning() << Q_FUNC_INFO << "failed with code" << code; QString s; switch (code) { case Delegate::FAIL_DOWNLOAD: - s = tr("Failed to download aircraft descriptions from '%1'. " + s = tr("Failed to download aircraft descriptions from '%1'. " "Check the address (URL) and your network connection.").arg(m_catalogUrl.toString()); break; + case Delegate::FAIL_NOT_FOUND: + s = tr("Failed to download aircraft descriptions at '%1'. " + "Check the URL is correct.").arg(m_catalogUrl.toString()); + break; + case Delegate::FAIL_VERSION: s = tr("The provided hangar is for a different version of FlightGear. " "(This is version %1)").arg(QString::fromUtf8(FLIGHTGEAR_VERSION)); @@ -164,7 +170,6 @@ void AddCatalogDialog::reject() void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat) { Delegate::StatusCode s = cat->status(); - qDebug() << Q_FUNC_INFO << "cat status:" << s; switch (s) { case Delegate::STATUS_REFRESHED: m_state = STATE_FINISHED; @@ -174,6 +179,19 @@ void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat) // don't jump to STATE_FINISHED return; + case Delegate::FAIL_NOT_FOUND: + { + FGHTTPClient* http = globals->get_subsystem(); + if (cat->url() == http->getDefaultCatalogUrl()) { + cat->setUrl(http->getDefaultCatalogFallbackUrl()); + cat->refresh(); // and trigger another refresh + return; + } + + m_state = STATE_DOWNLOAD_FAILED; + break; + } + // all the actual failure codes default: m_state = STATE_DOWNLOAD_FAILED; diff --git a/src/GUI/PathsDialog.cxx b/src/GUI/PathsDialog.cxx index 4dcb6a042..10854f9ed 100644 --- a/src/GUI/PathsDialog.cxx +++ b/src/GUI/PathsDialog.cxx @@ -222,7 +222,7 @@ void AddOnsPage::onRemoveCatalog() QString pkgId = mi.data(CatalogIdRole).toString(); if (pkgId.toStdString() == http->getDefaultCatalogId()) { - s = QString("Remove default aircraft hangar? " + s = QString("Remove the default aircraft hangar? " "This hangar contains all the default aircraft included with FlightGear. " "If you change your mind in the future, click the 'restore' button."); } else { diff --git a/src/Network/HTTPClient.cxx b/src/Network/HTTPClient.cxx index cf4b490db..f186ca0be 100644 --- a/src/Network/HTTPClient.cxx +++ b/src/Network/HTTPClient.cxx @@ -47,71 +47,34 @@ typedef nasal::Ghost NasalPackage; typedef nasal::Ghost NasalCatalog; typedef nasal::Ghost NasalInstall; +const char* OFFICIAL_CATALOG_ID = "org.flightgear.official"; -class FGHTTPClient::FGDelegate : public pkg::Delegate -{ -public: - virtual void refreshComplete() - { - SG_LOG(SG_IO, SG_INFO, "all Catalogs refreshed"); - - // auto-update; make this controlled by a property - pkg::Root* r = globals->packageRoot(); - - pkg::PackageList toBeUpdated(r->packagesNeedingUpdate()); - pkg::PackageList::const_iterator it; - for (it = toBeUpdated.begin(); it != toBeUpdated.end(); ++it) { - assert((*it)->isInstalled()); - SG_LOG(SG_IO, SG_INFO, "updating:" << (*it)->id()); - r->scheduleToUpdate((*it)->install()); +// fallback URL is used when looking up a version-specific catalog fails +const char* FALLBACK_CATALOG_URL = "http://fgfs.goneabitbursar.com/pkg/catalog.xml"; + +namespace { + + std::string _getDefaultCatalogId() + { + return fgGetString("/sim/package-system/default-catalog/id", + OFFICIAL_CATALOG_ID); } - } - - virtual void catalogRefreshed(pkg::CatalogRef aCat, StatusCode aReason) - { - if (aCat.ptr() == NULL) { - SG_LOG(SG_IO, SG_INFO, "refresh of all catalogs done"); - return; - } - - switch (aReason) { - case pkg::Delegate::STATUS_SUCCESS: - case pkg::Delegate::STATUS_REFRESHED: - SG_LOG(SG_IO, SG_INFO, "refresh of Catalog done:" << aCat->url()); - break; - - case pkg::Delegate::STATUS_IN_PROGRESS: - SG_LOG(SG_IO, SG_INFO, "refresh of Catalog started:" << aCat->url()); - break; - - default: - SG_LOG(SG_IO, SG_WARN, "refresh of Catalog " << aCat->url() << " failed:" << aReason); + + pkg::CatalogRef getDefaultCatalog() + { + if (!globals->packageRoot()) + return pkg::CatalogRef(); + + return globals->packageRoot()->getCatalogById(_getDefaultCatalogId()); } - } - - virtual void startInstall(pkg::InstallRef aInstall) - { - SG_LOG(SG_IO, SG_INFO, "beginning install of:" << aInstall->package()->id() - << " to local path:" << aInstall->path()); - } - - virtual void installProgress(pkg::InstallRef aInstall, unsigned int aBytes, unsigned int aTotal) - { - } - - virtual void finishInstall(pkg::InstallRef aInstall, StatusCode aReason) - { - if (aReason == STATUS_SUCCESS) { - SG_LOG(SG_IO, SG_INFO, "finished install of:" << aInstall->package()->id() - << " to local path:" << aInstall->path()); - } else { - SG_LOG(SG_IO, SG_WARN, "install failed of:" << aInstall->package()->id() - << " to local path:" << aInstall->path()); - } + std::string _getDefaultCatalogUrl() + { + return fgGetString("/sim/package-system/default-catalog/url", + "http://fgfs.goneabitbursar.com/pkg/" FLIGHTGEAR_VERSION "/catalog.xml"); + } +} // of anonymous namespace - } -}; // of FGHTTPClient::FGDelegate FGHTTPClient::FGHTTPClient() : _inited(false) @@ -144,10 +107,7 @@ void FGHTTPClient::init() if (packageRoot) { // package system needs access to the HTTP engine too packageRoot->setHTTPClient(_http.get()); - - _packageDelegate.reset(new FGDelegate); - packageRoot->addDelegate(_packageDelegate.get()); - + // start a refresh now // setting 'force' true to work around the problem where a slightly stale // catalog exists, but aircraft are modified - this causes an MD5 sum @@ -158,23 +118,6 @@ void FGHTTPClient::init() _inited = true; } -namespace { - -std::string _getDefaultCatalogId() -{ - return fgGetString("/sim/package-system/default-catalog/id", "org.flightgear.official" ); -} - -pkg::CatalogRef getDefaultCatalog() -{ - if (!globals->packageRoot()) - return pkg::CatalogRef(); - - return globals->packageRoot()->getCatalogById(_getDefaultCatalogId()); -} - -} // of anonymous namespace - bool FGHTTPClient::isDefaultCatalogInstalled() const { return getDefaultCatalog().valid(); @@ -195,8 +138,12 @@ std::string FGHTTPClient::getDefaultCatalogId() const std::string FGHTTPClient::getDefaultCatalogUrl() const { - return fgGetString("/sim/package-system/default-catalog/url", - "http://fgfs.goneabitbursar.com/pkg/" FLIGHTGEAR_VERSION "/catalog.xml");; + return _getDefaultCatalogUrl(); +} + +std::string FGHTTPClient::getDefaultCatalogFallbackUrl() const +{ + return std::string(FALLBACK_CATALOG_URL); } static naRef f_package_existingInstall( pkg::Package& pkg, @@ -335,12 +282,6 @@ void FGHTTPClient::postinit() void FGHTTPClient::shutdown() { - pkg::Root* packageRoot = globals->packageRoot(); - if (packageRoot && _packageDelegate.get()) { - packageRoot->removeDelegate(_packageDelegate.get()); - } - - _packageDelegate.reset(); _http.reset(); _inited = false; diff --git a/src/Network/HTTPClient.hxx b/src/Network/HTTPClient.hxx index fe2396585..84e01dca2 100644 --- a/src/Network/HTTPClient.hxx +++ b/src/Network/HTTPClient.hxx @@ -46,14 +46,12 @@ public: std::string getDefaultCatalogId() const; std::string getDefaultCatalogUrl() const; - - static const char* subsystemName() { return "http"; } -private: - class FGDelegate; + std::string getDefaultCatalogFallbackUrl() const; + static const char* subsystemName() { return "http"; } +private: bool _inited; std::auto_ptr _http; - std::auto_ptr _packageDelegate; }; #endif // FG_HTTP_CLIENT_HXX -- 2.39.5