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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "transfersavesdialog.h"
#include "ui_transfersavesdialog.h"
#include "iplugingame.h"
#include "isavegame.h"
#include "savegameinfo.h"
#include <utility.h>
#include <log.h>
#include <QtDebug>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFlags>
#include <QLabel>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMessageBox>
#include <QPushButton>
#include <QStringList>
using namespace MOBase;
using namespace MOShared;
//These two classes give the save-transfer box a smidgin of useful info even
//if save game isn't supported yet.
namespace {
class DummySave : public ISaveGame
{
public:
DummySave(QString const &filename) :
m_File(filename)
{}
~DummySave() {}
virtual QString getFilename() const override
{
return m_File;
}
virtual QDateTime getCreationTime() const override
{
return QFileInfo(m_File).birthTime();
}
virtual QString getSaveGroupIdentifier() const override
{
return m_File;
}
virtual QStringList allFiles() const override
{
return { m_File };
}
virtual bool hasScriptExtenderFile() const override
{
return false;
}
private:
QString m_File;
};
class DummyInfo : public SaveGameInfo
{
public:
virtual MOBase::ISaveGame const *getSaveGameInfo(QString const &file) const override
{
return new DummySave(file);
}
virtual MissingAssets getMissingAssets(QString const &) const override
{
return {};
}
MOBase::ISaveGameInfoWidget *getSaveGameWidget(QWidget *) const override
{
return nullptr;
}
virtual bool hasScriptExtenderSave(QString const &file) const override
{
return false;
}
};
} //end anonymous namespace
TransferSavesDialog::TransferSavesDialog(const Profile &profile, IPluginGame const *gamePlugin, QWidget *parent)
: TutorableDialog("TransferSaves", parent)
, ui(new Ui::TransferSavesDialog)
, m_Profile(profile)
, m_GamePlugin(gamePlugin)
{
ui->setupUi(this);
ui->label_2->setText(tr("Characters for profile %1").arg(m_Profile.name()));
refreshGlobalSaves();
refreshLocalSaves();
refreshGlobalCharacters();
refreshLocalCharacters();
}
TransferSavesDialog::~TransferSavesDialog()
{
delete ui;
}
void TransferSavesDialog::refreshGlobalSaves()
{
refreshSaves(m_GlobalSaves, m_GamePlugin->savesDirectory().absolutePath());
}
void TransferSavesDialog::refreshLocalSaves()
{
refreshSaves(m_LocalSaves, m_Profile.savePath());
}
void TransferSavesDialog::refreshGlobalCharacters()
{
refreshCharacters(m_GlobalSaves, ui->globalCharacterList, ui->copyToLocalBtn, ui->moveToLocalBtn);
}
void TransferSavesDialog::refreshLocalCharacters()
{
refreshCharacters(m_LocalSaves, ui->localCharacterList, ui->copyToGlobalBtn, ui->moveToGlobalBtn);
}
bool TransferSavesDialog::testOverwrite(OverwriteMode &overwriteMode, const QString &destinationFile)
{
QMessageBox::StandardButton res = overwriteMode == OVERWRITE_YES ? QMessageBox::Yes : QMessageBox::No;
if (overwriteMode == OVERWRITE_ASK) {
res = QMessageBox::question(this, tr("Overwrite"),
tr("Overwrite the file \"%1\"").arg(destinationFile),
QMessageBox::Yes | QMessageBox::No | QMessageBox::YesToAll | QMessageBox::NoToAll);
if (res == QMessageBox::YesToAll) {
overwriteMode = OVERWRITE_YES;
res = QMessageBox::Yes;
} else if (res == QMessageBox::NoToAll) {
overwriteMode = OVERWRITE_NO;
res = QMessageBox::No;
}
}
return res == QMessageBox::Yes;
}
#define MOVE_SAVES "Move all save games of character \"%1\""
#define COPY_SAVES "Copy all save games of character \"%1\""
#define TO_PROFILE "to the profile?"
#define TO_GLOBAL "to the global location? Please be aware that this will mess up the running number of save games."
void TransferSavesDialog::on_moveToLocalBtn_clicked()
{
QString character = ui->globalCharacterList->currentItem()->text();
if (transferCharacters(
character, MOVE_SAVES TO_PROFILE,
m_GamePlugin->savesDirectory(),
m_GlobalSaves[character],
m_Profile.savePath(),
[this](const QString &source, const QString &destination) -> bool {
return shellMove(source, destination, this);
},
"Failed to move {} to {}")) {
refreshGlobalSaves();
refreshGlobalCharacters();
refreshLocalSaves();
refreshLocalCharacters();
}
}
void TransferSavesDialog::on_copyToLocalBtn_clicked()
{
QString character = ui->globalCharacterList->currentItem()->text();
if (transferCharacters(
character, COPY_SAVES TO_PROFILE,
m_GamePlugin->savesDirectory(),
m_GlobalSaves[character],
m_Profile.savePath(),
[this](const QString &source, const QString &destination) -> bool {
return shellCopy(source, destination, this);
},
"Failed to copy {} to {}")) {
refreshLocalSaves();
refreshLocalCharacters();
}
}
void TransferSavesDialog::on_moveToGlobalBtn_clicked()
{
QString character = ui->localCharacterList->currentItem()->text();
if (transferCharacters(
character, MOVE_SAVES TO_GLOBAL,
m_Profile.savePath(),
m_LocalSaves[character],
m_GamePlugin->savesDirectory().absolutePath(),
[this](const QString &source, const QString &destination) -> bool {
return shellMove(source, destination, this);
},
"Failed to move {} to {}")) {
refreshGlobalSaves();
refreshGlobalCharacters();
refreshLocalSaves();
refreshLocalCharacters();
}
}
void TransferSavesDialog::on_copyToGlobalBtn_clicked()
{
QString character = ui->localCharacterList->currentItem()->text();
if (transferCharacters(
character, COPY_SAVES TO_GLOBAL,
m_Profile.savePath(),
m_LocalSaves[character],
m_GamePlugin->savesDirectory().absolutePath(),
[this](const QString &source, const QString &destination) -> bool {
return shellCopy(source, destination, this);
},
"Failed to copy {} to {}")) {
refreshGlobalSaves();
refreshGlobalCharacters();
}
}
void TransferSavesDialog::on_doneButton_clicked()
{
close();
}
void TransferSavesDialog::on_globalCharacterList_currentTextChanged(const QString ¤tText)
{
ui->globalSavesList->clear();
//sadly this can get called while we're resetting the list, with an invalid
//name, so we have to check.
SaveCollection::const_iterator saveList = m_GlobalSaves.find(currentText);
if (saveList != m_GlobalSaves.end()) {
for (SaveListItem const &save : saveList->second) {
ui->globalSavesList->addItem(QFileInfo(save->getFilename()).fileName());
}
}
}
void TransferSavesDialog::on_localCharacterList_currentTextChanged(const QString ¤tText)
{
ui->localSavesList->clear();
//sadly this can get called while we're resetting the list, with an invalid
//name, so we have to check.
SaveCollection::const_iterator saveList = m_LocalSaves.find(currentText);
if (saveList != m_LocalSaves.end()) {
for (SaveListItem const &save : saveList->second) {
ui->localSavesList->addItem(QFileInfo(save->getFilename()).fileName());
}
}
}
void TransferSavesDialog::refreshSaves(SaveCollection &saveCollection, QString const &savedir)
{
saveCollection.clear();
SaveGameInfo const *info = m_GamePlugin->feature<SaveGameInfo>();
if (info == nullptr) {
static DummyInfo dummyInfo;
info = &dummyInfo;
}
QStringList filters;
filters << QString("*.") + m_GamePlugin->savegameExtension();
QDir savesDir(savedir);
savesDir.setNameFilters(QStringList() << QString("*.") + m_GamePlugin->savegameExtension());
savesDir.setFilter(QDir::Files);
QDirIterator it(savesDir, QDirIterator::Subdirectories);
log::debug("reading save games from {}", savesDir.absolutePath());
QFileInfoList files;
while (it.hasNext()) {
it.next();
files.append(it.fileInfo());
}
std::sort(files.begin(), files.end(), [](auto const& lhs, auto const& rhs) {
return lhs.fileTime(QFileDevice::FileModificationTime) < rhs.fileTime(QFileDevice::FileModificationTime);
});
for (const QFileInfo &file: files) {
MOBase::ISaveGame const *save = info->getSaveGameInfo(file.absoluteFilePath());
saveCollection[save->getSaveGroupIdentifier()].push_back(
std::unique_ptr<MOBase::ISaveGame const>(save));
}
}
void TransferSavesDialog::refreshCharacters(const SaveCollection &saveCollection,
QListWidget *charList, QPushButton *copy, QPushButton *move)
{
charList->clear();
for (SaveCollection::value_type const &val : saveCollection) {
charList->addItem(val.first);
}
if (charList->count() > 0) {
charList->setCurrentRow(0);
copy->setEnabled(true);
move->setEnabled(true);
} else {
copy->setEnabled(false);
move->setEnabled(false);
}
}
bool TransferSavesDialog::transferCharacters(
QString const &character, char const *message,
QDir const& sourceDirectory,
SaveList &saves,
QDir const& destination,
const std::function<bool(const QString &, const QString &)> &method,
char const *errmsg)
{
if (QMessageBox::question(this, tr("Confirm"),
tr(message).arg(character),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) {
return false;
}
OverwriteMode overwriteMode = OVERWRITE_ASK;
for (SaveListItem const &save : saves) {
for (QString source : save->allFiles()) {
QFileInfo sourceFile(source);
QString destinationFile(destination.absoluteFilePath(sourceDirectory.relativeFilePath(source)));
//If the file is already there, let them skip (or not).
if (QFile::exists(destinationFile)) {
if (! testOverwrite(overwriteMode, destinationFile)) {
continue;
}
//OK, they want to remove it.
QFile::remove(destinationFile);
}
if (!method(sourceFile.absoluteFilePath(), destinationFile)) {
log::error(errmsg, sourceFile.absoluteFilePath(), destinationFile);
}
}
}
return true;
}
|