1 // AddCatalogDialog.cxx - part of GUI launcher using Qt5
3 // Written by James Turner, started March 2015.
5 // Copyright (C) 2015 James Turner <zakalawe@mac.com>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "AddCatalogDialog.hxx"
22 #include "ui_AddCatalogDialog.h"
24 #include <QPushButton>
27 #include <Include/version.h>
29 using namespace simgear::pkg;
31 AddCatalogDialog::AddCatalogDialog(QWidget *parent, RootRef root) :
34 ui(new Ui::AddCatalogDialog),
39 connect(ui->urlEdit, &QLineEdit::textEdited,
40 this, &AddCatalogDialog::onUrlTextChanged);
45 AddCatalogDialog::~AddCatalogDialog()
50 CatalogRef AddCatalogDialog::addedCatalog()
55 void AddCatalogDialog::setUrlAndDownload(QUrl url)
61 void AddCatalogDialog::onUrlTextChanged()
63 m_catalogUrl = QUrl::fromUserInput(ui->urlEdit->text());
67 void AddCatalogDialog::updateUi()
69 QPushButton* b = ui->buttonBox->button(QDialogButtonBox::Ok);
73 b->setText(tr("Next"));
74 b->setEnabled(m_catalogUrl.isValid() && !m_catalogUrl.isRelative());
77 case STATE_DOWNLOADING:
81 case STATE_DOWNLOAD_FAILED:
87 b->setText(tr("Okay"));
91 if (m_state == STATE_FINISHED) {
92 QString catDesc = QString::fromStdString(m_result->description());
93 QString s = tr("Successfully retrieved aircraft information from '%1'. "
94 "%2 aircraft are included in this hangar.").arg(catDesc).arg(m_result->packages().size());
95 ui->resultsSummaryLabel->setText(s);
96 } else if (m_state == STATE_DOWNLOAD_FAILED) {
97 Delegate::StatusCode code = m_result->status();
98 qWarning() << Q_FUNC_INFO << "failed with code" << code;
101 case Delegate::FAIL_DOWNLOAD:
102 s = tr("Failed to download aircraft descriptions from '%1'. "
103 "Check the address (URL) and your network connection.").arg(m_catalogUrl.toString());
106 case Delegate::FAIL_VERSION:
107 s = tr("The provided hangar is for a different version of FlightGear. "
108 "(This is version %1)").arg(QString::fromUtf8(FLIGHTGEAR_VERSION));
112 s = tr("Unknown error occured trying to set up the hangar.");
115 ui->resultsSummaryLabel->setText(s);
119 void AddCatalogDialog::startDownload()
121 Q_ASSERT(m_catalogUrl.isValid());
123 m_result = Catalog::createFromUrl(m_packageRoot, m_catalogUrl.toString().toStdString());
124 m_result->addStatusCallback(this, &AddCatalogDialog::onCatalogStatusChanged);
125 m_state = STATE_DOWNLOADING;
127 ui->stack->setCurrentIndex(STATE_DOWNLOADING);
130 void AddCatalogDialog::accept()
137 case STATE_DOWNLOADING:
138 case STATE_DOWNLOAD_FAILED:
139 // can't happen, button is disabled
148 void AddCatalogDialog::reject()
150 if (m_result && !m_result->id().empty()) {
151 // user may have successfully download the catalog, but choosen
152 // not to add it. so remove it here
153 m_packageRoot->removeCatalogById(m_result->id());
159 void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat)
161 Delegate::StatusCode s = cat->status();
162 qDebug() << Q_FUNC_INFO << "cat status:" << s;
164 case Delegate::STATUS_REFRESHED:
165 m_state = STATE_FINISHED;
168 case Delegate::STATUS_IN_PROGRESS:
169 // don't jump to STATE_FINISHED
172 // all the actual failure codes
174 m_state = STATE_DOWNLOAD_FAILED;
178 ui->stack->setCurrentIndex(STATE_FINISHED);