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::onUrlTextChanged()
57 m_catalogUrl = QUrl::fromUserInput(ui->urlEdit->text());
61 void AddCatalogDialog::updateUi()
63 QPushButton* b = ui->buttonBox->button(QDialogButtonBox::Ok);
67 b->setText(tr("Next"));
68 b->setEnabled(m_catalogUrl.isValid() && !m_catalogUrl.isRelative());
71 case STATE_DOWNLOADING:
75 case STATE_DOWNLOAD_FAILED:
81 b->setText(tr("Okay"));
85 if (m_state == STATE_FINISHED) {
86 QString catDesc = QString::fromStdString(m_result->description());
87 QString s = tr("Successfully retrieved aircraft information from '%1'. "
88 "%2 aircraft are included in this hangar.").arg(catDesc).arg(m_result->packages().size());
89 ui->resultsSummaryLabel->setText(s);
90 } else if (m_state == STATE_DOWNLOAD_FAILED) {
91 Delegate::StatusCode code = m_result->status();
92 qWarning() << Q_FUNC_INFO << "failed with code" << code;
95 case Delegate::FAIL_DOWNLOAD:
96 s = tr("Failed to download aircraft descriptions from '%1'. "
97 "Check the address (URL) and your network connection.").arg(m_catalogUrl.toString());
100 case Delegate::FAIL_VERSION:
101 s = tr("The provided hangar is for a different version of FlightGear. "
102 "(This is version %1)").arg(QString::fromUtf8(FLIGHTGEAR_VERSION));
106 s = tr("Unknown error occured trying to set up the hangar.");
109 ui->resultsSummaryLabel->setText(s);
113 void AddCatalogDialog::startDownload()
115 Q_ASSERT(m_catalogUrl.isValid());
117 m_result = Catalog::createFromUrl(m_packageRoot, m_catalogUrl.toString().toStdString());
118 m_result->addStatusCallback(this, &AddCatalogDialog::onCatalogStatusChanged);
119 m_state = STATE_DOWNLOADING;
121 ui->stack->setCurrentIndex(STATE_DOWNLOADING);
124 void AddCatalogDialog::accept()
131 case STATE_DOWNLOADING:
132 case STATE_DOWNLOAD_FAILED:
133 // can't happen, button is disabled
142 void AddCatalogDialog::reject()
144 if (m_result && !m_result->id().empty()) {
145 // user may have successfully download the catalog, but choosen
146 // not to add it. so remove it here
147 m_packageRoot->removeCatalogById(m_result->id());
153 void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat)
155 Delegate::StatusCode s = cat->status();
156 qDebug() << Q_FUNC_INFO << "cat status:" << s;
158 case Delegate::STATUS_REFRESHED:
159 m_state = STATE_FINISHED;
162 case Delegate::STATUS_IN_PROGRESS:
163 // don't jump to STATE_FINISHED
166 // all the actual failure codes
168 m_state = STATE_DOWNLOAD_FAILED;
172 ui->stack->setCurrentIndex(STATE_FINISHED);