]> git.mxchange.org Git - flightgear.git/blob - src/GUI/SetupRootDialog.cxx
Very crude work on GUI base package selection.
[flightgear.git] / src / GUI / SetupRootDialog.cxx
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "SetupRootDialog.hxx"
6
7 #include <QFileDialog>
8 #include <QDesktopServices>
9 #include <QDir>
10 #include <QFileInfo>
11 #include <QMessageBox>
12 #include <QSettings>
13 #include <QDebug>
14 #include <QSettings>
15
16 #include "ui_SetupRootDialog.h"
17
18 #include <Main/globals.hxx>
19 #include <Main/fg_init.hxx>
20 #include <Include/version.h>
21
22 SetupRootDialog::SetupRootDialog(bool usedDefaultPath) :
23     QDialog()
24 {
25     m_ui.reset(new Ui::SetupRootDialog);
26     m_ui->setupUi(this);
27
28     connect(m_ui->browseButton, &QPushButton::clicked,
29              this, &SetupRootDialog::onBrowse);
30     connect(m_ui->downloadButton, &QPushButton::clicked,
31             this, &SetupRootDialog::onDownload);
32     connect(m_ui->buttonBox, &QDialogButtonBox::rejected,
33             this, &QDialog::reject);
34
35     m_promptState = usedDefaultPath ? DefaultPathCheckFailed : ExplicitPathCheckFailed;
36     std::string ver = fgBasePackageVersion(globals->get_fg_root());
37     if (!ver.empty()) {
38         Q_ASSERT(ver != FLIGHTGEAR_VERSION); // otherwise what are we doing in here?!
39         m_promptState = VersionCheckFailed;
40     }
41
42     m_ui->versionLabel->setText(tr("FlightGear version %1").arg(FLIGHTGEAR_VERSION));
43     m_ui->bigIcon->setPixmap(QPixmap(":/app-icon-large"));
44     updatePromptText();
45 }
46
47 bool SetupRootDialog::restoreUserSelectedRoot()
48 {
49     QSettings settings;
50     QString path = settings.value("fg-root").toString();
51     if (validatePath(path) && validateVersion(path)) {
52         qDebug() << "Restoring FG-root:" << path;
53         globals->set_fg_root(path.toStdString());
54         return true;
55     } else {
56         return false;
57     }
58 }
59
60 bool SetupRootDialog::validatePath(QString path)
61 {
62     // check assorted files exist in the root location, to avoid any chance of
63     // selecting an incomplete base package. This is probably overkill but does
64     // no harm
65     QStringList files = QStringList()
66         << "version"
67         << "preferences.xml"
68         << "Materials/base/materials-base.xml"
69         << "gui/menubar.xml"
70         << "Timezone/zone.tab";
71
72     QDir d(path);
73     if (!d.exists()) {
74         return false;
75     }
76
77     Q_FOREACH(QString s, files) {
78         if (!d.exists(s)) {
79             return false;
80         }
81     }
82
83     return true;
84 }
85
86 bool SetupRootDialog::validateVersion(QString path)
87 {
88     std::string ver = fgBasePackageVersion(SGPath(path.toStdString()));
89     return (ver == FLIGHTGEAR_VERSION);
90 }
91
92 SetupRootDialog::~SetupRootDialog()
93 {
94
95 }
96
97 void SetupRootDialog::onBrowse()
98 {
99     m_browsedPath = QFileDialog::getExistingDirectory(this,
100                                                      tr("Choose FlightGear data folder"));
101     if (m_browsedPath.isEmpty()) {
102         return;
103     }
104
105     if (!validatePath(m_browsedPath)) {
106         m_promptState = ChoseInvalidLocation;
107         updatePromptText();
108         return;
109     }
110
111     if (!validateVersion(m_browsedPath)) {
112         m_promptState = ChoseInvalidVersion;
113         updatePromptText();
114         return;
115     }
116
117     globals->set_fg_root(m_browsedPath.toStdString());
118
119     QSettings settings;
120     settings.setValue("fg-root", m_browsedPath);
121
122     accept(); // we're done
123 }
124
125 void SetupRootDialog::onDownload()
126 {
127     QUrl downloadUrl("http://download.flightgear.org/flightgear/Shared/");
128     QDesktopServices::openUrl(downloadUrl);
129 }
130
131 void SetupRootDialog::updatePromptText()
132 {
133     QString t;
134     QString curRoot = QString::fromStdString(globals->get_fg_root());
135     switch (m_promptState) {
136     case DefaultPathCheckFailed:
137         t = tr("This copy of FlightGear does not include the base data files. " \
138                "Please select a suitable folder containing a previously download set of files.");
139         break;
140
141     case ExplicitPathCheckFailed:
142         t = tr("The requested location '%1' does not appear to be a valid set of data files for FlightGear").arg(curRoot);
143         break;
144
145     case VersionCheckFailed:
146     {
147         QString curVer = QString::fromStdString(fgBasePackageVersion(globals->get_fg_root()));
148         t = tr("Detected incompatible version of the data files: version %1 found, but this is FlightGear %2. " \
149                "(At location: '%3') " \
150                "Please install or select a matching set of data files.").arg(curVer).arg(QString::fromLatin1(FLIGHTGEAR_VERSION)).arg(curRoot);
151         break;
152     }
153
154     case ChoseInvalidLocation:
155         t = tr("The choosen location (%1) does not appear to contain FlightGear data files. Please try another location.").arg(m_browsedPath);
156         break;
157
158     case ChoseInvalidVersion:
159     {
160         QString curVer = QString::fromStdString(fgBasePackageVersion(m_browsedPath.toStdString()));
161         t = tr("The choosen location (%1) contains files for version %2, but this is FlightGear %3. " \
162                "Please update or try another location").arg(m_browsedPath).arg(curVer).arg(QString::fromLatin1(FLIGHTGEAR_VERSION));
163         break;
164     }
165     }
166
167     m_ui->promptText->setText(t);
168 }
169