From be1291f6896c3788099f234b66f2fe27366d5e1b Mon Sep 17 00:00:00 2001 From: James Turner Date: Mon, 23 Nov 2015 22:58:00 +0000 Subject: [PATCH] Change handling of default hangar / catalog. - no longer re-add it on startup, add an explicit button in the UI (will add a more obvious item to the aircraft list in a follow-up commit) --- src/GUI/AddCatalogDialog.cxx | 6 ++++ src/GUI/AddCatalogDialog.hxx | 1 + src/GUI/PathsDialog.cxx | 49 ++++++++++++++++++++++++++++++--- src/GUI/PathsDialog.hxx | 3 +- src/GUI/PathsDialog.ui | 39 +++++++++++++++----------- src/Network/HTTPClient.cxx | 53 ++++++++++++++++++++++++++++-------- src/Network/HTTPClient.hxx | 5 ++++ 7 files changed, 123 insertions(+), 33 deletions(-) diff --git a/src/GUI/AddCatalogDialog.cxx b/src/GUI/AddCatalogDialog.cxx index abca2c51d..27ba210b9 100644 --- a/src/GUI/AddCatalogDialog.cxx +++ b/src/GUI/AddCatalogDialog.cxx @@ -52,6 +52,12 @@ CatalogRef AddCatalogDialog::addedCatalog() return m_result; } +void AddCatalogDialog::setUrlAndDownload(QUrl url) +{ + m_catalogUrl = url; + startDownload(); +} + void AddCatalogDialog::onUrlTextChanged() { m_catalogUrl = QUrl::fromUserInput(ui->urlEdit->text()); diff --git a/src/GUI/AddCatalogDialog.hxx b/src/GUI/AddCatalogDialog.hxx index d67393dfa..88a748892 100644 --- a/src/GUI/AddCatalogDialog.hxx +++ b/src/GUI/AddCatalogDialog.hxx @@ -42,6 +42,7 @@ public: simgear::pkg::CatalogRef addedCatalog(); + void setUrlAndDownload(QUrl url); private slots: virtual void reject(); virtual void accept(); diff --git a/src/GUI/PathsDialog.cxx b/src/GUI/PathsDialog.cxx index 3abc281a8..9282de1e0 100644 --- a/src/GUI/PathsDialog.cxx +++ b/src/GUI/PathsDialog.cxx @@ -9,6 +9,8 @@ #include "AddCatalogDialog.hxx" #include
+#include
+#include PathsDialog::PathsDialog(QWidget *parent, simgear::pkg::RootRef root) : QDialog(parent), @@ -22,6 +24,8 @@ PathsDialog::PathsDialog(QWidget *parent, simgear::pkg::RootRef root) : connect(m_ui->addCatalog, &QToolButton::clicked, this, &PathsDialog::onAddCatalog); + connect(m_ui->addDefaultCatalogButton, &QPushButton::clicked, + this, &PathsDialog::onAddDefaultCatalog); connect(m_ui->removeCatalog, &QToolButton::clicked, this, &PathsDialog::onRemoveCatalog); @@ -120,26 +124,60 @@ void PathsDialog::onRemoveAircraftPath() void PathsDialog::onAddCatalog() { - AddCatalogDialog* dlg = new AddCatalogDialog(this, m_packageRoot); + QScopedPointer dlg(new AddCatalogDialog(this, m_packageRoot)); dlg->exec(); if (dlg->result() == QDialog::Accepted) { m_catalogsModel->refresh(); } } +void PathsDialog::onAddDefaultCatalog() +{ + // check it's not a duplicate somehow + FGHTTPClient* http = static_cast(globals->get_subsystem("http")); + if (http->isDefaultCatalogInstalled()) + return; + + QScopedPointer dlg(new AddCatalogDialog(this, m_packageRoot)); + QUrl url(QString::fromStdString(http->getDefaultCatalogUrl())); + dlg->setUrlAndDownload(url); + dlg->exec(); + if (dlg->result() == QDialog::Accepted) { + m_catalogsModel->refresh(); + updateUi(); + } +} + void PathsDialog::onRemoveCatalog() { QModelIndex mi = m_ui->catalogsList->currentIndex(); + FGHTTPClient* http = static_cast(globals->get_subsystem("http")); + if (mi.isValid()) { + QString s = QStringLiteral("Remove aircraft hangar '%1'? All installed aircraft from this " + "hangar will be removed."); + QString pkgId = mi.data(CatalogIdRole).toString(); + + if (pkgId.toStdString() == http->getDefaultCatalogId()) { + s = QStringLiteral("Remove 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 { + s = s.arg(mi.data(Qt::DisplayRole).toString()); + } + QMessageBox mb; - mb.setText(QStringLiteral("Remove aircraft hangar '%1'?").arg(mi.data(Qt::DisplayRole).toString())); + mb.setText(s); mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No); mb.setDefaultButton(QMessageBox::No); mb.exec(); - QString pkgId = mi.data(CatalogIdRole).toString(); - m_packageRoot->removeCatalogById(pkgId.toStdString()); + if (mb.result() == QMessageBox::Yes) { + m_packageRoot->removeCatalogById(pkgId.toStdString()); + } } + + updateUi(); } void PathsDialog::onChangeDownloadDir() @@ -173,4 +211,7 @@ void PathsDialog::updateUi() QString m = tr("Download location: %1").arg(s); m_ui->downloadLocation->setText(m); + + FGHTTPClient* http = static_cast(globals->get_subsystem("http")); + m_ui->addDefaultCatalogButton->setEnabled(!http->isDefaultCatalogInstalled()); } diff --git a/src/GUI/PathsDialog.hxx b/src/GUI/PathsDialog.hxx index bbd15341e..acafe3d06 100644 --- a/src/GUI/PathsDialog.hxx +++ b/src/GUI/PathsDialog.hxx @@ -32,7 +32,8 @@ private slots: void onAddCatalog(); void onRemoveCatalog(); - + void onAddDefaultCatalog(); + void onChangeDownloadDir(); void onClearDownloadDir(); private: diff --git a/src/GUI/PathsDialog.ui b/src/GUI/PathsDialog.ui index 50e5f2dc8..393c10ee6 100644 --- a/src/GUI/PathsDialog.ui +++ b/src/GUI/PathsDialog.ui @@ -161,20 +161,7 @@ 0 - - - - Qt::Horizontal - - - - 567 - 20 - - - - - + @@ -193,7 +180,7 @@ - + @@ -212,7 +199,27 @@ - + + + + Qt::Horizontal + + + + 567 + 20 + + + + + + + + Restore default aircraft hangar + + + + diff --git a/src/Network/HTTPClient.cxx b/src/Network/HTTPClient.cxx index 19c963797..57b6626dd 100644 --- a/src/Network/HTTPClient.cxx +++ b/src/Network/HTTPClient.cxx @@ -147,18 +147,6 @@ void FGHTTPClient::init() _packageDelegate.reset(new FGDelegate); packageRoot->addDelegate(_packageDelegate.get()); - - const char * defaultCatalogId = fgGetString("/sim/package-system/default-catalog/id", "org.flightgear.official" ); - const char * defaultCatalogUrl = fgGetString("/sim/package-system/default-catalog/url", - "http://fgfs.goneabitbursar.com/pkg/" FLIGHTGEAR_VERSION "/catalog.xml"); - // setup default catalog if not present - pkg::Catalog* defaultCatalog = packageRoot->getCatalogById( defaultCatalogId ); - if (!defaultCatalog) { - // always show this message - SG_LOG(SG_GENERAL, SG_ALERT, "default catalog not found, installing '" - << defaultCatalogId << "' from '" << defaultCatalogUrl << "'."); - pkg::Catalog::createFromUrl(packageRoot,defaultCatalogUrl); - } // start a refresh now packageRoot->refresh(); @@ -167,6 +155,47 @@ 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(); +} + +void FGHTTPClient::addDefaultCatalog() +{ + pkg::CatalogRef defaultCatalog = getDefaultCatalog(); + if (!defaultCatalog.valid()) { + pkg::Catalog::createFromUrl(globals->packageRoot(), getDefaultCatalogUrl()); + } +} + +std::string FGHTTPClient::getDefaultCatalogId() const +{ + return _getDefaultCatalogId(); +} + +std::string FGHTTPClient::getDefaultCatalogUrl() const +{ + return fgGetString("/sim/package-system/default-catalog/url", + "http://fgfs.goneabitbursar.com/pkg/" FLIGHTGEAR_VERSION "/catalog.xml");; +} + static naRef f_package_existingInstall( pkg::Package& pkg, const nasal::CallContext& ctx ) { diff --git a/src/Network/HTTPClient.hxx b/src/Network/HTTPClient.hxx index a203501f0..3a92ee1ef 100644 --- a/src/Network/HTTPClient.hxx +++ b/src/Network/HTTPClient.hxx @@ -41,6 +41,11 @@ public: virtual void shutdown(); virtual void update(double); + bool isDefaultCatalogInstalled() const; + void addDefaultCatalog(); + + std::string getDefaultCatalogId() const; + std::string getDefaultCatalogUrl() const; private: class FGDelegate; -- 2.39.5