]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AddCatalogDialog.cxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[flightgear.git] / src / GUI / AddCatalogDialog.cxx
1 // AddCatalogDialog.cxx - part of GUI launcher using Qt5
2 //
3 // Written by James Turner, started March 2015.
4 //
5 // Copyright (C) 2015 James Turner <zakalawe@mac.com>
6 //
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.
11 //
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.
16 //
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.
20
21 #include "AddCatalogDialog.hxx"
22 #include "ui_AddCatalogDialog.h"
23
24 #include <QPushButton>
25 #include <QDebug>
26
27 #include <Include/version.h>
28
29 using namespace simgear::pkg;
30
31 AddCatalogDialog::AddCatalogDialog(QWidget *parent, RootRef root) :
32     QDialog(parent, Qt::Dialog
33                     | Qt::CustomizeWindowHint
34                     | Qt::WindowTitleHint
35                     | Qt::WindowSystemMenuHint
36                     | Qt::WindowContextHelpButtonHint
37                     | Qt::MSWindowsFixedSizeDialogHint),
38     m_state(STATE_START),
39     ui(new Ui::AddCatalogDialog),
40     m_packageRoot(root)
41 {
42     ui->setupUi(this);
43
44     connect(ui->urlEdit, &QLineEdit::textEdited,
45             this, &AddCatalogDialog::onUrlTextChanged);
46
47     updateUi();
48 }
49
50 AddCatalogDialog::~AddCatalogDialog()
51 {
52     delete ui;
53 }
54
55 CatalogRef AddCatalogDialog::addedCatalog()
56 {
57     return m_result;
58 }
59
60 void AddCatalogDialog::setUrlAndDownload(QUrl url)
61 {
62     m_catalogUrl = url;
63     startDownload();
64 }
65
66 void AddCatalogDialog::onUrlTextChanged()
67 {
68     m_catalogUrl = QUrl::fromUserInput(ui->urlEdit->text());
69     updateUi();
70 }
71
72 void AddCatalogDialog::updateUi()
73 {
74     QPushButton* b = ui->buttonBox->button(QDialogButtonBox::Ok);
75
76     switch (m_state) {
77     case STATE_START:
78         b->setText(tr("Next"));
79         b->setEnabled(m_catalogUrl.isValid() && !m_catalogUrl.isRelative());
80         break;
81
82     case STATE_DOWNLOADING:
83         b->setEnabled(false);
84         break;
85
86     case STATE_DOWNLOAD_FAILED:
87         b->setEnabled(false);
88         break;
89
90     case STATE_FINISHED:
91         b->setEnabled(true);
92         b->setText(tr("Okay"));
93         break;
94     }
95
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;
104         QString s;
105         switch (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());
109             break;
110
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));
114             break;
115
116         default:
117             s = tr("Unknown error occured trying to set up the hangar.");
118         }
119
120         ui->resultsSummaryLabel->setText(s);
121     }
122 }
123
124 void AddCatalogDialog::startDownload()
125 {
126     Q_ASSERT(m_catalogUrl.isValid());
127
128     m_result = Catalog::createFromUrl(m_packageRoot, m_catalogUrl.toString().toStdString());
129     m_result->addStatusCallback(this, &AddCatalogDialog::onCatalogStatusChanged);
130     m_state = STATE_DOWNLOADING;
131     updateUi();
132     ui->stack->setCurrentIndex(STATE_DOWNLOADING);
133 }
134
135 void AddCatalogDialog::accept()
136 {
137     switch (m_state) {
138     case STATE_START:
139         startDownload();
140         break;
141
142     case STATE_DOWNLOADING:
143     case STATE_DOWNLOAD_FAILED:
144         // can't happen, button is disabled
145         break;
146
147     case STATE_FINISHED:
148         QDialog::accept();
149         break;
150     }
151 }
152
153 void AddCatalogDialog::reject()
154 {
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());
159     }
160
161     QDialog::reject();
162 }
163
164 void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat)
165 {
166     Delegate::StatusCode s = cat->status();
167     qDebug() << Q_FUNC_INFO << "cat status:" << s;
168     switch (s) {
169     case Delegate::STATUS_REFRESHED:
170         m_state = STATE_FINISHED;
171         break;
172
173     case Delegate::STATUS_IN_PROGRESS:
174         // don't jump to STATE_FINISHED
175         return;
176
177     // all the actual failure codes
178     default:
179         m_state = STATE_DOWNLOAD_FAILED;
180         break;
181     }
182
183     ui->stack->setCurrentIndex(STATE_FINISHED);
184     updateUi();
185 }
186