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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
#include "instancemanagerdialog.h"
#include "ui_instancemanagerdialog.h"
#include "instancemanager.h"
#include "createinstancedialog.h"
#include "settings.h"
#include "selectiondialog.h"
#include "plugincontainer.h"
#include "shared/appconfig.h"
#include <iplugingame.h>
namespace shell = MOBase::shell;
void openInstanceManager(PluginContainer& pc, QWidget* parent)
{
//CreateInstanceDialog dlg(pc, parent);
//dlg.exec();
InstanceManagerDialog dlg(pc, parent);
dlg.exec();
}
class InstanceInfo
{
public:
InstanceInfo(QDir dir, bool isPortable) :
m_dir(std::move(dir)), m_portable(isPortable),
m_settings(dir.filePath(QString::fromStdWString(AppConfig::iniFileName())))
{
}
QString name() const
{
if (m_portable) {
return QObject::tr("Portable");
} else {
return m_dir.dirName();
}
}
QString gameName() const
{
if (auto n=m_settings.game().name()) {
if (auto e=m_settings.game().edition()) {
if (!e->isEmpty()) {
return *n + " (" + *e + ")";
}
}
return *n;
} else {
return {};
}
}
QString gamePath() const
{
if (auto n=m_settings.game().directory()) {
return *n;
} else {
return {};
}
}
QString location() const
{
return m_dir.path();
}
QString baseDirectory() const
{
return m_settings.paths().base();
}
bool isPortable() const
{
return m_portable;
}
private:
QDir m_dir;
bool m_portable;
Settings m_settings;
};
InstanceManagerDialog::~InstanceManagerDialog() = default;
InstanceManagerDialog::InstanceManagerDialog(
const PluginContainer& pc, QWidget *parent) :
QDialog(parent), ui(new Ui::InstanceManagerDialog), m_pc(pc),
m_model(nullptr)
{
ui->setupUi(this);
ui->splitter->setSizes({200, 1});
ui->splitter->setStretchFactor(0, 0);
ui->splitter->setStretchFactor(1, 1);
m_model = new QStandardItemModel;
ui->list->setModel(m_model);
m_filter.setEdit(ui->filter);
m_filter.setList(ui->list);
m_filter.setUpdateDelay(false);
m_filter.setFilteredBorder(false);
updateInstances();
updateList();
connect(ui->createNew, &QPushButton::clicked, [&]{ createNew(); });
connect(ui->list->selectionModel(), &QItemSelectionModel::selectionChanged, [&]{ onSelection(); });
//connect(ui->list, &QListWidget::itemActivated, [&]{ openSelectedInstance(); });
connect(ui->rename, &QPushButton::clicked, [&]{ rename(); });
connect(ui->exploreLocation, &QPushButton::clicked, [&]{ exploreLocation(); });
connect(ui->exploreBaseDirectory, &QPushButton::clicked, [&]{ exploreBaseDirectory(); });
connect(ui->exploreGame, &QPushButton::clicked, [&]{ exploreGame(); });
connect(ui->switchToInstance, &QPushButton::clicked, [&]{ openSelectedInstance(); });
connect(ui->close, &QPushButton::clicked, [&]{ close(); });
}
void InstanceManagerDialog::updateInstances()
{
auto& m = InstanceManager::instance();
m_instances.clear();
if (m.portableInstanceExists()) {
m_instances.push_back(std::make_unique<InstanceInfo>(
m.portablePath(), true));
}
for (auto&& d : m.instancePaths()) {
m_instances.push_back(std::make_unique<InstanceInfo>(d, false));
}
}
void InstanceManagerDialog::updateList()
{
m_model->clear();
for (auto&& ii : m_instances) {
m_model->appendRow(new QStandardItem(ii->name()));
}
if (!m_instances.empty()) {
select(0);
}
}
void InstanceManagerDialog::select(std::size_t i)
{
if (i >= m_instances.size()) {
return;
}
const auto& ii = m_instances[i];
fillData(*ii);
ui->list->selectionModel()->select(
m_filter.mapFromSource(m_filter.sourceModel()->index(i, 0)),
QItemSelectionModel::ClearAndSelect);
}
void InstanceManagerDialog::openSelectedInstance()
{
const auto i = singleSelectionIndex();
if (i == NoSelection) {
return;
}
InstanceManager::instance().switchToInstance(m_instances[i]->name());
}
void InstanceManagerDialog::rename()
{
auto* i = singleSelection();
if (!i) {
return;
}
auto& m = InstanceManager::instance();
if (m.currentInstance() == i->name()) {
QMessageBox::information(this,
tr("Rename instance"), tr("The active instance cannot be renamed"));
return;
}
QDialog dlg(this);
dlg.setWindowTitle(tr("Rename instance"));
auto* ly = new QVBoxLayout(&dlg);
auto* bb = new QDialogButtonBox(
QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
auto* text = new QLineEdit(i->name());
text->selectAll();
auto* error = new QLabel;
ly->addWidget(new QLabel(tr("Instance name")));
ly->addWidget(text);
ly->addWidget(error);
ly->addStretch();
ly->addWidget(bb);
connect(text, &QLineEdit::textChanged, [&] {
bool okay = false;
if (!m.validInstanceName(text->text())) {
error->setText(tr("The instance name must be a valid folder name."));
} else {
const auto name = m.sanitizeInstanceName(text->text());
if ((name != i->name()) && m.instanceExists(text->text())) {
error->setText(tr("An instance with this name already exists."));
} else {
okay = true;
}
}
error->setVisible(!okay);
bb->button(QDialogButtonBox::Ok)->setEnabled(okay);
});
connect(bb, &QDialogButtonBox::accepted, [&]{ dlg.accept(); });
connect(bb, &QDialogButtonBox::rejected, [&]{ dlg.reject(); });
dlg.resize({400, 120});
if (dlg.exec() != QDialog::Accepted) {
return;
}
const QString newName = m.sanitizeInstanceName(text->text());
const QString src = QDir::toNativeSeparators(i->location());
const QString dest = QDir::toNativeSeparators(
QFileInfo(i->location()).dir().path() + "/" + newName);
const auto r = shell::Rename(src, dest, false);
if (!r) {
QMessageBox::critical(
this, tr("Error"),
tr("Failed to rename \"%1\" to \"%2\": %3")
.arg(src).arg(dest).arg(r.toString()));
return;
}
}
void InstanceManagerDialog::exploreLocation()
{
if (const auto* i=singleSelection()) {
shell::Explore(i->location());
}
}
void InstanceManagerDialog::exploreBaseDirectory()
{
if (const auto* i=singleSelection()) {
shell::Explore(i->baseDirectory());
}
}
void InstanceManagerDialog::exploreGame()
{
if (const auto* i=singleSelection()) {
shell::Explore(i->gamePath());
}
}
void InstanceManagerDialog::onSelection()
{
const auto i = singleSelectionIndex();
if (i == NoSelection) {
return;
}
select(i);
}
void InstanceManagerDialog::createNew()
{
CreateInstanceDialog dlg(m_pc, this);
dlg.exec();
}
std::size_t InstanceManagerDialog::singleSelectionIndex() const
{
const auto sel = m_filter.mapSelectionToSource(
ui->list->selectionModel()->selection());
if (sel.size() != 1) {
return NoSelection;
}
return static_cast<std::size_t>(sel.indexes()[0].row());
}
InstanceInfo* InstanceManagerDialog::singleSelection()
{
const auto i = singleSelectionIndex();
if (i == NoSelection) {
return nullptr;
}
return m_instances[i].get();
}
const InstanceInfo* InstanceManagerDialog::singleSelection() const
{
const auto i = singleSelectionIndex();
if (i == NoSelection) {
return nullptr;
}
return m_instances[i].get();
}
void InstanceManagerDialog::fillData(const InstanceInfo& ii)
{
ui->name->setText(ii.name());
ui->location->setText(ii.location());
ui->baseDirectory->setText(ii.baseDirectory());
ui->gameName->setText(ii.gameName());
ui->gameDir->setText(ii.gamePath());
const auto& m = InstanceManager::instance();
ui->rename->setEnabled(!ii.isPortable());
if (ii.isPortable()) {
ui->convertToPortable->setVisible(false);
ui->convertToGlobal->setVisible(true);
ui->convertToGlobal->setEnabled(true);
} else {
ui->convertToPortable->setVisible(true);
ui->convertToGlobal->setVisible(false);
if (m.portableInstanceExists()) {
ui->convertToPortable->setEnabled(false);
ui->convertToPortable->setToolTip(tr("A portable instance already exists."));
} else {
ui->convertToPortable->setEnabled(false);
ui->convertToPortable->setToolTip("");
}
}
}
|