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 globals->set_fg_root(path.toStdString());
80 bool SetupRootDialog::validatePath(QString path)
82 // check assorted files exist in the root location, to avoid any chance of
83 // selecting an incomplete base package. This is probably overkill but does
85 QStringList files = QStringList()
88 << "Materials/base/materials-base.xml"
90 << "Timezone/zone.tab";
97 Q_FOREACH(QString s, files) {
106 bool SetupRootDialog::validateVersion(QString path)
108 std::string ver = fgBasePackageVersion(SGPath(path.toStdString()));
109 return (ver == FLIGHTGEAR_VERSION);
112 SetupRootDialog::~SetupRootDialog()
117 void SetupRootDialog::onBrowse()
119 m_browsedPath = QFileDialog::getExistingDirectory(this,
120 tr("Choose FlightGear data folder"));
121 if (m_browsedPath.isEmpty()) {
125 if (!validatePath(m_browsedPath)) {
126 m_promptState = ChoseInvalidLocation;
131 if (!validateVersion(m_browsedPath)) {
132 m_promptState = ChoseInvalidVersion;
137 globals->set_fg_root(m_browsedPath.toStdString());
140 settings.setValue("fg-root", m_browsedPath);
142 accept(); // we're done
145 void SetupRootDialog::onDownload()
147 QUrl downloadUrl("http://download.flightgear.org/flightgear/Shared/");
148 QDesktopServices::openUrl(downloadUrl);
151 void SetupRootDialog::updatePromptText()
154 QString curRoot = QString::fromStdString(globals->get_fg_root());
155 switch (m_promptState) {
156 case DefaultPathCheckFailed:
157 t = tr("This copy of FlightGear does not include the base data files. " \
158 "Please select a suitable folder containing a previously download set of files.");
161 case ExplicitPathCheckFailed:
162 t = tr("The requested location '%1' does not appear to be a valid set of data files for FlightGear").arg(curRoot);
165 case VersionCheckFailed:
167 QString curVer = QString::fromStdString(fgBasePackageVersion(globals->get_fg_root()));
168 t = tr("Detected incompatible version of the data files: version %1 found, but this is FlightGear %2. " \
169 "(At location: '%3') " \
170 "Please install or select a matching set of data files.").arg(curVer).arg(QString::fromLatin1(FLIGHTGEAR_VERSION)).arg(curRoot);
174 case ChoseInvalidLocation:
175 t = tr("The choosen location (%1) does not appear to contain FlightGear data files. Please try another location.").arg(m_browsedPath);
178 case ChoseInvalidVersion:
180 QString curVer = QString::fromStdString(fgBasePackageVersion(m_browsedPath.toStdString()));
181 t = tr("The choosen location (%1) contains files for version %2, but this is FlightGear %3. " \
182 "Please update or try another location").arg(m_browsedPath).arg(curVer).arg(QString::fromLatin1(FLIGHTGEAR_VERSION));
187 m_ui->promptText->setText(t);