-
Notifications
You must be signed in to change notification settings - Fork 161
/
settingsdialogpaths.cpp
281 lines (242 loc) · 9.08 KB
/
settingsdialogpaths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "settingsdialogpaths.h"
#include "shared/appconfig.h"
#include "ui_settingsdialog.h"
#include <iplugingame.h>
PathsSettingsTab::PathsSettingsTab(Settings& s, SettingsDialog& d)
: SettingsTab(s, d), m_gameDir(settings().game().plugin()->gameDirectory())
{
ui->baseDirEdit->setText(settings().paths().base());
ui->managedGameDirEdit->setText(QDir::toNativeSeparators(
m_gameDir.absoluteFilePath(settings().game().plugin()->binaryName())));
QString basePath = settings().paths().base();
QDir baseDir(basePath);
for (const auto& dir :
{std::make_pair(ui->downloadDirEdit, settings().paths().downloads(false)),
std::make_pair(ui->modDirEdit, settings().paths().mods(false)),
std::make_pair(ui->cacheDirEdit, settings().paths().cache(false)),
std::make_pair(ui->profilesDirEdit, settings().paths().profiles(false)),
std::make_pair(ui->overwriteDirEdit, settings().paths().overwrite(false))}) {
QString storePath = baseDir.relativeFilePath(dir.second);
storePath = dir.second;
dir.first->setText(storePath);
}
QObject::connect(ui->browseBaseDirBtn, &QPushButton::clicked, [&] {
on_browseBaseDirBtn_clicked();
});
QObject::connect(ui->browseCacheDirBtn, &QPushButton::clicked, [&] {
on_browseCacheDirBtn_clicked();
});
QObject::connect(ui->browseDownloadDirBtn, &QPushButton::clicked, [&] {
on_browseDownloadDirBtn_clicked();
});
QObject::connect(ui->browseGameDirBtn, &QPushButton::clicked, [&] {
on_browseGameDirBtn_clicked();
});
QObject::connect(ui->browseModDirBtn, &QPushButton::clicked, [&] {
on_browseModDirBtn_clicked();
});
QObject::connect(ui->browseOverwriteDirBtn, &QPushButton::clicked, [&] {
on_browseOverwriteDirBtn_clicked();
});
QObject::connect(ui->browseProfilesDirBtn, &QPushButton::clicked, [&] {
on_browseProfilesDirBtn_clicked();
});
QObject::connect(ui->baseDirEdit, &QLineEdit::editingFinished, [&] {
on_baseDirEdit_editingFinished();
});
QObject::connect(ui->cacheDirEdit, &QLineEdit::editingFinished, [&] {
on_cacheDirEdit_editingFinished();
});
QObject::connect(ui->downloadDirEdit, &QLineEdit::editingFinished, [&] {
on_downloadDirEdit_editingFinished();
});
QObject::connect(ui->modDirEdit, &QLineEdit::editingFinished, [&] {
on_modDirEdit_editingFinished();
});
QObject::connect(ui->overwriteDirEdit, &QLineEdit::editingFinished, [&] {
on_overwriteDirEdit_editingFinished();
});
QObject::connect(ui->profilesDirEdit, &QLineEdit::editingFinished, [&] {
on_profilesDirEdit_editingFinished();
});
}
void PathsSettingsTab::update()
{
using Setter = void (PathSettings::*)(const QString&);
using Directory = std::tuple<QString, Setter, std::wstring>;
QString basePath = settings().paths().base();
for (const Directory& dir :
{Directory{ui->downloadDirEdit->text(), &PathSettings::setDownloads,
AppConfig::downloadPath()},
Directory{ui->cacheDirEdit->text(), &PathSettings::setCache,
AppConfig::cachePath()},
Directory{ui->modDirEdit->text(), &PathSettings::setMods,
AppConfig::modsPath()},
Directory{ui->overwriteDirEdit->text(), &PathSettings::setOverwrite,
AppConfig::overwritePath()},
Directory{ui->profilesDirEdit->text(), &PathSettings::setProfiles,
AppConfig::profilesPath()}}) {
QString path;
Setter setter;
std::wstring defaultName;
std::tie(path, setter, defaultName) = dir;
QString realPath = path;
realPath = PathSettings::resolve(realPath, ui->baseDirEdit->text());
if (!QDir(realPath).exists()) {
if (!QDir().mkpath(realPath)) {
QMessageBox::warning(
parentWidget(), QObject::tr("Error"),
QObject::tr("Failed to create \"%1\", you may not have the "
"necessary permissions. Path remains unchanged.")
.arg(realPath));
continue;
}
}
if (QFileInfo(realPath) !=
QFileInfo(basePath + "/" + QString::fromStdWString(defaultName))) {
(settings().paths().*setter)(path);
} else {
(settings().paths().*setter)("");
}
}
if (QFileInfo(ui->baseDirEdit->text()) !=
QFileInfo(qApp->property("dataPath").toString())) {
settings().paths().setBase(ui->baseDirEdit->text());
} else {
settings().paths().setBase("");
}
if (m_gameDir != settings().game().plugin()->gameDirectory()) {
settings().game().setDirectory(m_gameDir.absolutePath());
}
}
void PathsSettingsTab::on_browseBaseDirBtn_clicked()
{
QString temp = QFileDialog::getExistingDirectory(
&dialog(), QObject::tr("Select base directory"), ui->baseDirEdit->text());
if (!temp.isEmpty()) {
ui->baseDirEdit->setText(temp);
}
}
void PathsSettingsTab::on_browseDownloadDirBtn_clicked()
{
QString searchPath = ui->downloadDirEdit->text();
searchPath = PathSettings::resolve(searchPath, ui->baseDirEdit->text());
QString temp = QFileDialog::getExistingDirectory(
&dialog(), QObject::tr("Select download directory"), searchPath);
if (!temp.isEmpty()) {
ui->downloadDirEdit->setText(temp);
}
}
void PathsSettingsTab::on_browseModDirBtn_clicked()
{
QString searchPath = ui->modDirEdit->text();
searchPath = PathSettings::resolve(searchPath, ui->baseDirEdit->text());
QString temp = QFileDialog::getExistingDirectory(
&dialog(), QObject::tr("Select mod directory"), searchPath);
if (!temp.isEmpty()) {
ui->modDirEdit->setText(temp);
}
}
void PathsSettingsTab::on_browseCacheDirBtn_clicked()
{
QString searchPath = ui->cacheDirEdit->text();
searchPath = PathSettings::resolve(searchPath, ui->baseDirEdit->text());
QString temp = QFileDialog::getExistingDirectory(
&dialog(), QObject::tr("Select cache directory"), searchPath);
if (!temp.isEmpty()) {
ui->cacheDirEdit->setText(temp);
}
}
void PathsSettingsTab::on_browseProfilesDirBtn_clicked()
{
QString searchPath = ui->profilesDirEdit->text();
searchPath = PathSettings::resolve(searchPath, ui->baseDirEdit->text());
QString temp = QFileDialog::getExistingDirectory(
&dialog(), QObject::tr("Select profiles directory"), searchPath);
if (!temp.isEmpty()) {
ui->profilesDirEdit->setText(temp);
}
}
void PathsSettingsTab::on_browseOverwriteDirBtn_clicked()
{
QString searchPath = ui->overwriteDirEdit->text();
searchPath = PathSettings::resolve(searchPath, ui->baseDirEdit->text());
QString temp = QFileDialog::getExistingDirectory(
&dialog(), QObject::tr("Select overwrite directory"), searchPath);
if (!temp.isEmpty()) {
ui->overwriteDirEdit->setText(temp);
}
}
void PathsSettingsTab::on_browseGameDirBtn_clicked()
{
QFileInfo oldGameExe(ui->managedGameDirEdit->text());
// this gets the name of the game executable
//
// spaces in the name are interpreted as separators ";" in the filter,
// so we replace them with the single character matching wildcard "?"
//
QString temp = QFileDialog::getOpenFileName(
&dialog(), QObject::tr("Select game executable"), oldGameExe.absolutePath(),
oldGameExe.fileName().replace(QRegularExpression(" "), "?"));
if (temp.isEmpty()) {
return;
}
// we need to find the game folder corresponding to the executable
//
// some game plugins have executable in subfolder, e.g. bin/game.exe,
// so we need to go up the parent folders until the concatenation of
// the folder and the binary path equals the game executable specified
// by the user
//
QFileInfo newExe(temp);
const auto binaryPath = settings().game().plugin()->binaryName();
QDir folder = newExe.absoluteDir();
while (folder.exists() && !folder.isRoot() &&
QFileInfo(folder.filePath(binaryPath)) != newExe) {
folder.cdUp();
}
if (folder.exists(binaryPath)) {
m_gameDir = folder;
ui->managedGameDirEdit->setText(QDir::toNativeSeparators(
m_gameDir.absoluteFilePath(settings().game().plugin()->binaryName())));
} else {
QMessageBox::warning(
parentWidget(), QObject::tr("Error"),
QObject::tr("The given path was not recognized as a valid game installation. "
"The current game plugin requires the executable to be in a \"%1\" "
"subfolder of the game directory.")
.arg(QFileInfo(binaryPath).path()));
}
}
void PathsSettingsTab::on_baseDirEdit_editingFinished()
{
normalizePath(ui->baseDirEdit);
}
void PathsSettingsTab::on_downloadDirEdit_editingFinished()
{
normalizePath(ui->downloadDirEdit);
}
void PathsSettingsTab::on_modDirEdit_editingFinished()
{
normalizePath(ui->modDirEdit);
}
void PathsSettingsTab::on_cacheDirEdit_editingFinished()
{
normalizePath(ui->cacheDirEdit);
}
void PathsSettingsTab::on_profilesDirEdit_editingFinished()
{
normalizePath(ui->profilesDirEdit);
}
void PathsSettingsTab::on_overwriteDirEdit_editingFinished()
{
normalizePath(ui->overwriteDirEdit);
}
void PathsSettingsTab::normalizePath(QLineEdit* lineEdit)
{
QString text = lineEdit->text();
while (text.endsWith('/') || text.endsWith('\\')) {
text.chop(1);
}
lineEdit->setText(text);
}