]> git.mxchange.org Git - flightgear.git/blob - src/GUI/SetupRootDialog.cxx
Merge /u/r-harrison/flightgear/ branch next into next
[flightgear.git] / src / GUI / SetupRootDialog.cxx
1 // SetupRootDialog.cxx - part of GUI launcher using Qt5
2 //
3 // Written by James Turner, started December 2014.
4 //
5 // Copyright (C) 2014 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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "SetupRootDialog.hxx"
26
27 #include <QFileDialog>
28 #include <QDesktopServices>
29 #include <QDir>
30 #include <QFileInfo>
31 #include <QMessageBox>
32 #include <QSettings>
33 #include <QDebug>
34 #include <QSettings>
35 #include <QUrl>
36
37 #include "ui_SetupRootDialog.h"
38
39 #include <Main/globals.hxx>
40 #include <Main/fg_init.hxx>
41 #include <Include/version.h>
42
43 SetupRootDialog::SetupRootDialog(bool usedDefaultPath) :
44     QDialog()
45 {
46     m_ui.reset(new Ui::SetupRootDialog);
47     m_ui->setupUi(this);
48
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);
55
56     m_promptState = usedDefaultPath ? DefaultPathCheckFailed : ExplicitPathCheckFailed;
57     std::string ver = fgBasePackageVersion(globals->get_fg_root());
58     if (!ver.empty()) {
59         Q_ASSERT(ver != FLIGHTGEAR_VERSION); // otherwise what are we doing in here?!
60         m_promptState = VersionCheckFailed;
61     }
62
63     m_ui->versionLabel->setText(tr("FlightGear version %1").arg(FLIGHTGEAR_VERSION));
64     m_ui->bigIcon->setPixmap(QPixmap(":/app-icon-large"));
65     updatePromptText();
66 }
67
68 bool SetupRootDialog::restoreUserSelectedRoot()
69 {
70     QSettings settings;
71     QString path = settings.value("fg-root").toString();
72     if (validatePath(path) && validateVersion(path)) {
73         globals->set_fg_root(path.toStdString());
74         return true;
75     } else {
76         return false;
77     }
78 }
79
80 bool SetupRootDialog::validatePath(QString path)
81 {
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
84     // no harm
85     QStringList files = QStringList()
86         << "version"
87         << "preferences.xml"
88         << "Materials/base/materials-base.xml"
89         << "gui/menubar.xml"
90         << "Timezone/zone.tab";
91
92     QDir d(path);
93     if (!d.exists()) {
94         return false;
95     }
96
97     Q_FOREACH(QString s, files) {
98         if (!d.exists(s)) {
99             return false;
100         }
101     }
102
103     return true;
104 }
105
106 bool SetupRootDialog::validateVersion(QString path)
107 {
108     std::string ver = fgBasePackageVersion(SGPath(path.toStdString()));
109     return (ver == FLIGHTGEAR_VERSION);
110 }
111
112 SetupRootDialog::~SetupRootDialog()
113 {
114
115 }
116
117 void SetupRootDialog::onBrowse()
118 {
119     m_browsedPath = QFileDialog::getExistingDirectory(this,
120                                                      tr("Choose FlightGear data folder"));
121     if (m_browsedPath.isEmpty()) {
122         return;
123     }
124
125     if (!validatePath(m_browsedPath)) {
126         m_promptState = ChoseInvalidLocation;
127         updatePromptText();
128         return;
129     }
130
131     if (!validateVersion(m_browsedPath)) {
132         m_promptState = ChoseInvalidVersion;
133         updatePromptText();
134         return;
135     }
136
137     globals->set_fg_root(m_browsedPath.toStdString());
138
139     QSettings settings;
140     settings.setValue("fg-root", m_browsedPath);
141
142     accept(); // we're done
143 }
144
145 void SetupRootDialog::onDownload()
146 {
147     QUrl downloadUrl("http://download.flightgear.org/flightgear/Shared/");
148     QDesktopServices::openUrl(downloadUrl);
149 }
150
151 void SetupRootDialog::updatePromptText()
152 {
153     QString t;
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.");
159         break;
160
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);
163         break;
164
165     case VersionCheckFailed:
166     {
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);
171         break;
172     }
173
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);
176         break;
177
178     case ChoseInvalidVersion:
179     {
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));
183         break;
184     }
185     }
186
187     m_ui->promptText->setText(t);
188 }
189