-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportwindow.cpp
More file actions
275 lines (245 loc) · 10.8 KB
/
Copy pathexportwindow.cpp
File metadata and controls
275 lines (245 loc) · 10.8 KB
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
#include "exportwindow.h"
#include "ui_exportwindow.h" // This header defines Ui::ExportWindow
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QSettings>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMessageBox>
#include <QDirIterator>
#include <QStandardPaths>
#include <QTemporaryDir>
#include <QProcess>
ExportWindow::ExportWindow(QWidget *parent) : QWizard(parent), ui(new Ui::SimsSwitcherExport) {
ui->setupUi(this);
// Enable multi-selection for both lists
ui->modPresetList->setSelectionMode(QAbstractItemView::MultiSelection);
ui->packPresetList->setSelectionMode(QAbstractItemView::MultiSelection);
ui->mcccPresetList->setSelectionMode(QAbstractItemView::MultiSelection);
loadPresets();
loadMCCCPresets();
}
ExportWindow::~ExportWindow() { delete ui; }
void ExportWindow::loadMCCCPresets() {
MainWindow *mainWindowPointer = qobject_cast<MainWindow *>(parentWidget());
if (!mainWindowPointer) {
QMessageBox::warning(this, "Error", "Main window pointer is invalid.");
return;
}
QString rootdir = mainWindowPointer->getRootDir();
QDir mcccPresetsDir(rootdir + "/mcccPresets");
if (!mcccPresetsDir.exists()) {
// Directory does not exist, nothing to populate
return;
}
QStringList filter;
filter << "*.cfg";
QFileInfoList fileList = mcccPresetsDir.entryInfoList(filter, QDir::Files);
for (const QFileInfo &fileInfo : fileList) {
QString fileName = fileInfo.completeBaseName(); // Get filename without extension
ui->mcccPresetList->addItem(fileName);
}
}
void ExportWindow::loadPresets() {
// Load mod and pack presets from QSettings and populate the UI lists
QSettings settings("Falcon", "SimsSwitcher");
settings.beginGroup("presets");
QStringList modPresets = settings.childKeys();
settings.endGroup();
settings.beginGroup("packPresets");
QStringList packPresets = settings.childKeys();
settings.endGroup();
ui->modPresetList->addItems(modPresets);
ui->packPresetList->addItems(packPresets);
}
void ExportWindow::on_browseButton_clicked() {
QString dirPath = QFileDialog::getExistingDirectory(this, "Select Export Folder", QString(),
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!dirPath.isEmpty())
ui->fileLineEdit->setText(dirPath);
}
// Add this helper function for zipping a folder using Windows' built-in zip support (or use a 3rd party library if preferred)
bool zipDirectory(const QString& sourceDir, const QString& zipFilePath) {
// Use PowerShell's Compress-Archive for simplicity (Windows 10+)
QString srcCopy = sourceDir;
QString zipCopy = zipFilePath;
srcCopy.replace("'", "''");
zipCopy.replace("'", "''");
QString command = QString(
"powershell -Command \"Compress-Archive -Path '%1\\*' -DestinationPath '%2' -Force\""
).arg(srcCopy, zipCopy);
int result = QProcess::execute(command);
return (result == 0);
}
void ExportWindow::on_exportButton_clicked() {
QString dirPath = ui->fileLineEdit->text();
if (dirPath.isEmpty()) {
QMessageBox::warning(this, "Error", "Please select a folder to export to.");
return;
}
QDir exportDir(dirPath);
if (!exportDir.exists()) {
if (!exportDir.mkpath(".")) {
QMessageBox::warning(this, "Error", "Failed to create export directory.");
return;
}
}
QSettings settings("Falcon", "SimsSwitcher");
int exportedCount = 0;
// Gather all selected presets
QList<QListWidgetItem*> modItems = ui->modPresetList->selectedItems();
QList<QListWidgetItem*> packItems = ui->packPresetList->selectedItems();
int totalToExport = modItems.size() + packItems.size();
// If exporting mod zips, count those too
bool exportZips = ui->exportModsZipOption->isChecked();
if (exportZips)
totalToExport += modItems.size();
// Setup progress bar
ui->exportProgressBar->setMinimum(0);
ui->exportProgressBar->setMaximum(totalToExport);
ui->exportProgressBar->setValue(0);
int progress = 0;
// Export selected mod presets as individual files
for (QListWidgetItem *item : modItems) {
QString key = item->text();
QStringList values = settings.value("presets/" + key).toStringList();
QJsonObject presetObj;
presetObj.insert("type", "modPreset");
presetObj.insert("name", key);
presetObj.insert("items", QJsonArray::fromStringList(values));
QString fileName = QString("modPreset_%1.json").arg(key);
QFile file(exportDir.filePath(fileName));
bool jsonExported = false;
if (file.open(QIODevice::WriteOnly)) {
file.write(QJsonDocument(presetObj).toJson());
file.close();
jsonExported = true;
}
ui->exportProgressBar->setValue(++progress);
QCoreApplication::processEvents(); // Allow UI to update
// --- Export ZIP of all mods in this preset if option is checked ---
bool zipExported = false;
if (exportZips) {
// Find the Mods directory (assume it's stored in settings or ask user to select if not found)
QString modsDir = settings.value("rootDirectory").toString();
if (modsDir.isEmpty()) {
QMessageBox::warning(this, "Error", "Mods directory not set in settings.");
ui->exportProgressBar->setValue(++progress);
QCoreApplication::processEvents();
continue;
}
QDir modsBaseDir(modsDir + "/Mods");
if (!modsBaseDir.exists()) {
QMessageBox::warning(this, "Error", "Mods directory does not exist: " + modsBaseDir.absolutePath());
ui->exportProgressBar->setValue(++progress);
QCoreApplication::processEvents();
continue;
}
// Create a temporary directory to collect the files/folders for this preset
QTemporaryDir tempDir;
if (!tempDir.isValid()) {
QMessageBox::warning(this, "Error", "Failed to create temporary directory for zipping.");
ui->exportProgressBar->setValue(++progress);
QCoreApplication::processEvents();
continue;
}
QString tempPresetDir = tempDir.path();
// Copy each mod in the preset to the temp directory
for (const QString& modName : values) {
QString srcPath = modsBaseDir.filePath(modName);
QFileInfo srcInfo(srcPath);
if (!srcInfo.exists())
continue;
QString destPath = tempPresetDir + "/" + modName;
if (srcInfo.isDir()) {
QDir().mkpath(destPath);
QDir srcDir(srcPath);
QDirIterator it(srcPath, QDir::NoDotAndDotDot | QDir::AllEntries, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString srcFile = it.next();
QString relPath = srcDir.relativeFilePath(srcFile);
QString destFile = destPath + "/" + relPath;
QFileInfo fi(srcFile);
if (fi.isDir()) {
QDir().mkpath(destFile);
} else {
QFile::copy(srcFile, destFile);
}
}
} else if (srcInfo.isFile()) {
QFile::copy(srcPath, destPath);
}
}
// Zip the temp directory
QString zipName = QString("modPreset_%1.zip").arg(key);
QString zipPath = exportDir.filePath(zipName);
if (zipDirectory(tempPresetDir, zipPath)) {
zipExported = true;
} else {
QMessageBox::warning(this, "Error", QString("Failed to create ZIP for preset '%1'.").arg(key));
}
ui->exportProgressBar->setValue(++progress);
QCoreApplication::processEvents();
}
// Only count as exported if at least one (json or zip) was created
if (jsonExported || zipExported)
++exportedCount;
}
// Export selected pack presets as individual files
for (QListWidgetItem *item : packItems) {
QString key = item->text();
QStringList values = settings.value("packPresets/" + key).toStringList();
QJsonObject presetObj;
presetObj.insert("type", "packPreset");
presetObj.insert("name", key);
presetObj.insert("items", QJsonArray::fromStringList(values));
QString fileName = QString("packPreset_%1.json").arg(key);
QFile file(exportDir.filePath(fileName));
if (file.open(QIODevice::WriteOnly)) {
file.write(QJsonDocument(presetObj).toJson());
file.close();
++exportedCount;
}
ui->exportProgressBar->setValue(++progress);
QCoreApplication::processEvents(); // Allow UI to update
}
// Export selected MCCC presets as individual files
QList<QListWidgetItem*> mcccItems = ui->mcccPresetList->selectedItems();
for (QListWidgetItem *item : mcccItems) {
QString key = item->text();
MainWindow *mainWindowPointer = qobject_cast<MainWindow *>(parentWidget());
if (!mainWindowPointer) {
QMessageBox::warning(this, "Error", "Main window pointer is invalid.");
continue;
}
QString rootdir = mainWindowPointer->getRootDir();
QString mcccPresetsDirPath = rootdir + "/mcccPresets";
QDir mcccPresetsDir(mcccPresetsDirPath);
if (!mcccPresetsDir.exists()) {
QMessageBox::warning(this, "Error", "MCCC presets directory does not exist: " + mcccPresetsDirPath);
continue;
}
QString presetFilePath = mcccPresetsDir.filePath(key + ".cfg");
QFileInfo presetFileInfo(presetFilePath);
if (!presetFileInfo.exists()) {
QMessageBox::warning(this, "Error", "MCCC preset file does not exist: " + presetFilePath);
continue;
}
QString destFilePath = exportDir.filePath(key + ".cfg");
if (QFile::copy(presetFilePath, destFilePath)) {
++exportedCount;
} else {
QMessageBox::warning(this, "Error", "Failed to copy MCCC preset file: " + presetFilePath);
}
ui->exportProgressBar->setValue(++progress);
QCoreApplication::processEvents(); // Allow UI to update
}
if (exportedCount > 0) {
QMessageBox::information(this, "Success", QString("Exported %1 preset(s) successfully!").arg(exportedCount));
accept();
} else {
QMessageBox::warning(this, "Error", "No presets were exported.");
}
}