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) :
32 QDialog(parent, Qt::Dialog
33 | Qt::CustomizeWindowHint
35 | Qt::WindowSystemMenuHint
36 | Qt::WindowContextHelpButtonHint
37 | Qt::MSWindowsFixedSizeDialogHint),
39 ui(new Ui::AddCatalogDialog),
44 connect(ui->urlEdit, &QLineEdit::textEdited,
45 this, &AddCatalogDialog::onUrlTextChanged);
50 AddCatalogDialog::~AddCatalogDialog()
55 CatalogRef AddCatalogDialog::addedCatalog()
60 void AddCatalogDialog::setUrlAndDownload(QUrl url)
66 void AddCatalogDialog::onUrlTextChanged()
68 m_catalogUrl = QUrl::fromUserInput(ui->urlEdit->text());
72 void AddCatalogDialog::updateUi()
74 QPushButton* b = ui->buttonBox->button(QDialogButtonBox::Ok);
78 b->setText(tr("Next"));
79 b->setEnabled(m_catalogUrl.isValid() && !m_catalogUrl.isRelative());
82 case STATE_DOWNLOADING:
86 case STATE_DOWNLOAD_FAILED:
92 b->setText(tr("Okay"));
96 if (m_state == STATE_FINISHED) {
97 QString catDesc = QString::fromStdString(m_result->description());
98 QString s = tr("Successfully retrieved aircraft information from '%1'. "
99 "%2 aircraft are included in this hangar.").arg(catDesc).arg(m_result->packages().size());
100 ui->resultsSummaryLabel->setText(s);
101 } else if (m_state == STATE_DOWNLOAD_FAILED) {
102 Delegate::StatusCode code = m_result->status();
103 qWarning() << Q_FUNC_INFO << "failed with code" << code;
106 case Delegate::FAIL_DOWNLOAD:
107 s = tr("Failed to download aircraft descriptions from '%1'. "
108 "Check the address (URL) and your network connection.").arg(m_catalogUrl.toString());
111 case Delegate::FAIL_VERSION:
112 s = tr("The provided hangar is for a different version of FlightGear. "
113 "(This is version %1)").arg(QString::fromUtf8(FLIGHTGEAR_VERSION));
117 s = tr("Unknown error occured trying to set up the hangar.");
120 ui->resultsSummaryLabel->setText(s);
124 void AddCatalogDialog::startDownload()
126 Q_ASSERT(m_catalogUrl.isValid());
128 m_result = Catalog::createFromUrl(m_packageRoot, m_catalogUrl.toString().toStdString());
129 m_result->addStatusCallback(this, &AddCatalogDialog::onCatalogStatusChanged);
130 m_state = STATE_DOWNLOADING;
132 ui->stack->setCurrentIndex(STATE_DOWNLOADING);
135 void AddCatalogDialog::accept()
142 case STATE_DOWNLOADING:
143 case STATE_DOWNLOAD_FAILED:
144 // can't happen, button is disabled
153 void AddCatalogDialog::reject()
155 if (m_result && !m_result->id().empty()) {
156 // user may have successfully download the catalog, but choosen
157 // not to add it. so remove it here
158 m_packageRoot->removeCatalogById(m_result->id());
164 void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat)
166 Delegate::StatusCode s = cat->status();
167 qDebug() << Q_FUNC_INFO << "cat status:" << s;
169 case Delegate::STATUS_REFRESHED:
170 m_state = STATE_FINISHED;
173 case Delegate::STATUS_IN_PROGRESS:
174 // don't jump to STATE_FINISHED
177 // all the actual failure codes
179 m_state = STATE_DOWNLOAD_FAILED;
183 ui->stack->setCurrentIndex(STATE_FINISHED);