]> git.mxchange.org Git - flightgear.git/blob - src/GUI/SetupRootDialog.cxx
The QUrl header is needed for compilation on Linux
[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         qDebug() << "Restoring FG-root:" << path;
74         globals->set_fg_root(path.toStdString());
75         return true;
76     } else {
77         return false;
78     }
79 }
80
81 bool SetupRootDialog::validatePath(QString path)
82 {
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
85     // no harm
86     QStringList files = QStringList()
87         << "version"
88         << "preferences.xml"
89         << "Materials/base/materials-base.xml"
90         << "gui/menubar.xml"
91         << "Timezone/zone.tab";
92
93     QDir d(path);
94     if (!d.exists()) {
95         return false;
96     }
97
98     Q_FOREACH(QString s, files) {
99         if (!d.exists(s)) {
100             return false;
101         }
102     }
103
104     return true;
105 }
106
107 bool SetupRootDialog::validateVersion(QString path)
108 {
109     std::string ver = fgBasePackageVersion(SGPath(path.toStdString()));
110     return (ver == FLIGHTGEAR_VERSION);
111 }
112
113 SetupRootDialog::~SetupRootDialog()
114 {
115
116 }
117
118 void SetupRootDialog::onBrowse()
119 {
120     m_browsedPath = QFileDialog::getExistingDirectory(this,
121                                                      tr("Choose FlightGear data folder"));
122     if (m_browsedPath.isEmpty()) {
123         return;
124     }
125
126     if (!validatePath(m_browsedPath)) {
127         m_promptState = ChoseInvalidLocation;
128         updatePromptText();
129         return;
130     }
131
132     if (!validateVersion(m_browsedPath)) {
133         m_promptState = ChoseInvalidVersion;
134         updatePromptText();
135         return;
136     }
137
138     globals->set_fg_root(m_browsedPath.toStdString());
139
140     QSettings settings;
141     settings.setValue("fg-root", m_browsedPath);
142
143     accept(); // we're done
144 }
145
146 void SetupRootDialog::onDownload()
147 {
148     QUrl downloadUrl("http://download.flightgear.org/flightgear/Shared/");
149     QDesktopServices::openUrl(downloadUrl);
150 }
151
152 void SetupRootDialog::updatePromptText()
153 {
154     QString t;
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.");
160         break;
161
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);
164         break;
165
166     case VersionCheckFailed:
167     {
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);
172         break;
173     }
174
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);
177         break;
178
179     case ChoseInvalidVersion:
180     {
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));
184         break;
185     }
186     }
187
188     m_ui->promptText->setText(t);
189 }
190