1 // SetupRootDialog.cxx - part of GUI launcher using Qt5
3 // Written by James Turner, started December 2014.
5 // Copyright (C) 2014 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.
25 #include "SetupRootDialog.hxx"
27 #include <QFileDialog>
28 #include <QDesktopServices>
31 #include <QMessageBox>
37 #include "ui_SetupRootDialog.h"
39 #include <Main/globals.hxx>
40 #include <Main/fg_init.hxx>
41 #include <Include/version.h>
43 SetupRootDialog::SetupRootDialog(bool usedDefaultPath) :
46 m_ui.reset(new Ui::SetupRootDialog);
49 connect(m_ui->browseButton, &QPushButton::clicked,
50 this, &SetupRootDialog::onBrowse);
51 connect(m_ui->downloadButton, &QPushButton::clicked,
52 this, &SetupRootDialog::onDownload);
53 connect(m_ui->buttonBox, &QDialogButtonBox::rejected,
54 this, &QDialog::reject);
56 m_promptState = usedDefaultPath ? DefaultPathCheckFailed : ExplicitPathCheckFailed;
57 std::string ver = fgBasePackageVersion(globals->get_fg_root());
59 Q_ASSERT(ver != FLIGHTGEAR_VERSION); // otherwise what are we doing in here?!
60 m_promptState = VersionCheckFailed;
63 m_ui->versionLabel->setText(tr("FlightGear version %1").arg(FLIGHTGEAR_VERSION));
64 m_ui->bigIcon->setPixmap(QPixmap(":/app-icon-large"));
68 bool SetupRootDialog::restoreUserSelectedRoot()
71 QString path = settings.value("fg-root").toString();
72 if (validatePath(path) && validateVersion(path)) {
73 qDebug() << "Restoring FG-root:" << path;
74 globals->set_fg_root(path.toStdString());
81 bool SetupRootDialog::validatePath(QString path)
83 // check assorted files exist in the root location, to avoid any chance of
84 // selecting an incomplete base package. This is probably overkill but does
86 QStringList files = QStringList()
89 << "Materials/base/materials-base.xml"
91 << "Timezone/zone.tab";
98 Q_FOREACH(QString s, files) {
107 bool SetupRootDialog::validateVersion(QString path)
109 std::string ver = fgBasePackageVersion(SGPath(path.toStdString()));
110 return (ver == FLIGHTGEAR_VERSION);
113 SetupRootDialog::~SetupRootDialog()
118 void SetupRootDialog::onBrowse()
120 m_browsedPath = QFileDialog::getExistingDirectory(this,
121 tr("Choose FlightGear data folder"));
122 if (m_browsedPath.isEmpty()) {
126 if (!validatePath(m_browsedPath)) {
127 m_promptState = ChoseInvalidLocation;
132 if (!validateVersion(m_browsedPath)) {
133 m_promptState = ChoseInvalidVersion;
138 globals->set_fg_root(m_browsedPath.toStdString());
141 settings.setValue("fg-root", m_browsedPath);
143 accept(); // we're done
146 void SetupRootDialog::onDownload()
148 QUrl downloadUrl("http://download.flightgear.org/flightgear/Shared/");
149 QDesktopServices::openUrl(downloadUrl);
152 void SetupRootDialog::updatePromptText()
155 QString curRoot = QString::fromStdString(globals->get_fg_root());
156 switch (m_promptState) {
157 case DefaultPathCheckFailed:
158 t = tr("This copy of FlightGear does not include the base data files. " \
159 "Please select a suitable folder containing a previously download set of files.");
162 case ExplicitPathCheckFailed:
163 t = tr("The requested location '%1' does not appear to be a valid set of data files for FlightGear").arg(curRoot);
166 case VersionCheckFailed:
168 QString curVer = QString::fromStdString(fgBasePackageVersion(globals->get_fg_root()));
169 t = tr("Detected incompatible version of the data files: version %1 found, but this is FlightGear %2. " \
170 "(At location: '%3') " \
171 "Please install or select a matching set of data files.").arg(curVer).arg(QString::fromLatin1(FLIGHTGEAR_VERSION)).arg(curRoot);
175 case ChoseInvalidLocation:
176 t = tr("The choosen location (%1) does not appear to contain FlightGear data files. Please try another location.").arg(m_browsedPath);
179 case ChoseInvalidVersion:
181 QString curVer = QString::fromStdString(fgBasePackageVersion(m_browsedPath.toStdString()));
182 t = tr("The choosen location (%1) contains files for version %2, but this is FlightGear %3. " \
183 "Please update or try another location").arg(m_browsedPath).arg(curVer).arg(QString::fromLatin1(FLIGHTGEAR_VERSION));
188 m_ui->promptText->setText(t);