]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AddCatalogDialog.cxx
GUI support for VIA/Discontinuity
[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),
33     m_state(STATE_START),
34     ui(new Ui::AddCatalogDialog),
35     m_packageRoot(root)
36 {
37     ui->setupUi(this);
38
39     connect(ui->urlEdit, &QLineEdit::textEdited,
40             this, &AddCatalogDialog::onUrlTextChanged);
41
42     updateUi();
43 }
44
45 AddCatalogDialog::~AddCatalogDialog()
46 {
47     delete ui;
48 }
49
50 CatalogRef AddCatalogDialog::addedCatalog()
51 {
52     return m_result;
53 }
54
55 void AddCatalogDialog::setUrlAndDownload(QUrl url)
56 {
57     m_catalogUrl = url;
58     startDownload();
59 }
60
61 void AddCatalogDialog::onUrlTextChanged()
62 {
63     m_catalogUrl = QUrl::fromUserInput(ui->urlEdit->text());
64     updateUi();
65 }
66
67 void AddCatalogDialog::updateUi()
68 {
69     QPushButton* b = ui->buttonBox->button(QDialogButtonBox::Ok);
70
71     switch (m_state) {
72     case STATE_START:
73         b->setText(tr("Next"));
74         b->setEnabled(m_catalogUrl.isValid() && !m_catalogUrl.isRelative());
75         break;
76
77     case STATE_DOWNLOADING:
78         b->setEnabled(false);
79         break;
80
81     case STATE_DOWNLOAD_FAILED:
82         b->setEnabled(false);
83         break;
84
85     case STATE_FINISHED:
86         b->setEnabled(true);
87         b->setText(tr("Okay"));
88         break;
89     }
90
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;
99         QString s;
100         switch (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());
104             break;
105
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));
109             break;
110
111         default:
112             s = tr("Unknown error occured trying to set up the hangar.");
113         }
114
115         ui->resultsSummaryLabel->setText(s);
116     }
117 }
118
119 void AddCatalogDialog::startDownload()
120 {
121     Q_ASSERT(m_catalogUrl.isValid());
122
123     m_result = Catalog::createFromUrl(m_packageRoot, m_catalogUrl.toString().toStdString());
124     m_result->addStatusCallback(this, &AddCatalogDialog::onCatalogStatusChanged);
125     m_state = STATE_DOWNLOADING;
126     updateUi();
127     ui->stack->setCurrentIndex(STATE_DOWNLOADING);
128 }
129
130 void AddCatalogDialog::accept()
131 {
132     switch (m_state) {
133     case STATE_START:
134         startDownload();
135         break;
136
137     case STATE_DOWNLOADING:
138     case STATE_DOWNLOAD_FAILED:
139         // can't happen, button is disabled
140         break;
141
142     case STATE_FINISHED:
143         QDialog::accept();
144         break;
145     }
146 }
147
148 void AddCatalogDialog::reject()
149 {
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());
154     }
155
156     QDialog::reject();
157 }
158
159 void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat)
160 {
161     Delegate::StatusCode s = cat->status();
162     qDebug() << Q_FUNC_INFO << "cat status:" << s;
163     switch (s) {
164     case Delegate::STATUS_REFRESHED:
165         m_state = STATE_FINISHED;
166         break;
167
168     case Delegate::STATUS_IN_PROGRESS:
169         // don't jump to STATE_FINISHED
170         return;
171
172     // all the actual failure codes
173     default:
174         m_state = STATE_DOWNLOAD_FAILED;
175         break;
176     }
177
178     ui->stack->setCurrentIndex(STATE_FINISHED);
179     updateUi();
180 }
181