]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AddCatalogDialog.cxx
Remove some debug output.
[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::onUrlTextChanged()
56 {
57     m_catalogUrl = QUrl::fromUserInput(ui->urlEdit->text());
58     updateUi();
59 }
60
61 void AddCatalogDialog::updateUi()
62 {
63     QPushButton* b = ui->buttonBox->button(QDialogButtonBox::Ok);
64
65     switch (m_state) {
66     case STATE_START:
67         b->setText(tr("Next"));
68         b->setEnabled(m_catalogUrl.isValid() && !m_catalogUrl.isRelative());
69         break;
70
71     case STATE_DOWNLOADING:
72         b->setEnabled(false);
73         break;
74
75     case STATE_DOWNLOAD_FAILED:
76         b->setEnabled(false);
77         break;
78
79     case STATE_FINISHED:
80         b->setEnabled(true);
81         b->setText(tr("Okay"));
82         break;
83     }
84
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::FailureCode code = m_result->status();
92         qWarning() << Q_FUNC_INFO << "failed with code" << code;
93         QString s;
94         switch (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());
98             break;
99
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));
103             break;
104
105         default:
106             s = tr("Unknown error occured trying to set up the hangar.");
107         }
108
109         ui->resultsSummaryLabel->setText(s);
110     }
111 }
112
113 void AddCatalogDialog::startDownload()
114 {
115     Q_ASSERT(m_catalogUrl.isValid());
116
117     m_result = Catalog::createFromUrl(m_packageRoot, m_catalogUrl.toString().toStdString());
118     m_result->addStatusCallback(this, &AddCatalogDialog::onCatalogStatusChanged);
119     m_state = STATE_DOWNLOADING;
120     updateUi();
121     ui->stack->setCurrentIndex(STATE_DOWNLOADING);
122 }
123
124 void AddCatalogDialog::accept()
125 {
126     switch (m_state) {
127     case STATE_START:
128         startDownload();
129         break;
130
131     case STATE_DOWNLOADING:
132     case STATE_DOWNLOAD_FAILED:
133         // can't happen, button is disabled
134         break;
135
136     case STATE_FINISHED:
137         QDialog::accept();
138         break;
139     }
140 }
141
142 void AddCatalogDialog::reject()
143 {
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());
148     }
149
150     QDialog::reject();
151 }
152
153 void AddCatalogDialog::onCatalogStatusChanged(Catalog* cat)
154 {
155     Delegate::FailureCode s = cat->status();
156     qDebug() << Q_FUNC_INFO << "cat status:" << s;
157     switch (s) {
158     case Delegate::CATALOG_REFRESHED:
159         m_state = STATE_FINISHED;
160         break;
161
162     case Delegate::FAIL_IN_PROGRESS:
163         // don't jump to STATE_FINISHED
164         return;
165
166     // all the actual failure codes
167     default:
168         m_state = STATE_DOWNLOAD_FAILED;
169         break;
170     }
171
172     ui->stack->setCurrentIndex(STATE_FINISHED);
173     updateUi();
174 }
175