summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/ui/pages/instance/BackupPage.cpp
blob: 6af5a528f3536422c6062197c427d52d455728c2 (plain)
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
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  Copyright (C) 2026 Project Tick
 *
 *  This program 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, version 3.
 *
 *  This program 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 this program.  If not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "BackupPage.h"
#include <QCheckBox>
#include <QInputDialog>
#include <QMessageBox>
#include "Application.h"
#include "ui_BackupDialog.h"

BackupPage::BackupPage(MinecraftInstance* inst, QWidget* parent)
	: QWidget(parent),
	  ui(new Ui::BackupDialog),
	  m_instance(inst),
	  m_backupManager(new BackupManager(this))
{
	ui->setupUi(this);
	setupConnections();
}

BackupPage::~BackupPage()
{
	delete ui;
}

QIcon BackupPage::icon() const
{
	return QIcon::fromTheme("screenshot-placeholder");
}

void BackupPage::retranslate()
{
	ui->retranslateUi(this);
}

void BackupPage::openedImpl()
{
	refreshBackupList();
	updateEstimatedSize();
}

void BackupPage::closedImpl()
{
	// Nothing to do
}

void BackupPage::updateEstimatedSize()
{
	BackupOptions options = getSelectedOptions();
	qint64 estimatedSize  = options.estimateSize();

	// Format size
	double sizeMB	 = estimatedSize / (1024.0 * 1024.0);
	QString sizeText = QString("Estimated Size: ~%1 MB").arg(sizeMB, 0, 'f', 1);

	ui->estimatedSizeLabel->setText(sizeText);
}

void BackupPage::setupConnections()
{
	// Connect checkboxes to update estimated size
	connect(ui->includeSaves, &QCheckBox::toggled, this, &BackupPage::updateEstimatedSize);
	connect(ui->includeConfig, &QCheckBox::toggled, this, &BackupPage::updateEstimatedSize);
	connect(ui->includeMods, &QCheckBox::toggled, this, &BackupPage::updateEstimatedSize);
	connect(ui->includeResourcePacks, &QCheckBox::toggled, this, &BackupPage::updateEstimatedSize);
	connect(ui->includeShaderPacks, &QCheckBox::toggled, this, &BackupPage::updateEstimatedSize);
	connect(ui->includeScreenshots, &QCheckBox::toggled, this, &BackupPage::updateEstimatedSize);
	connect(ui->includeOptions, &QCheckBox::toggled, this, &BackupPage::updateEstimatedSize);

	// Connect custom paths changes
	connect(ui->customPathsList, &QListWidget::itemChanged, this, &BackupPage::updateEstimatedSize);

	// Other connections
	connect(ui->refreshButton, &QPushButton::clicked, this, &BackupPage::refreshBackupList);
	connect(ui->backupList, &QListWidget::currentRowChanged, this, &BackupPage::updateBackupDetails);
}

void BackupPage::refreshBackupList()
{
	ui->backupList->clear();

	// Convert MinecraftInstance* to InstancePtr
	auto instancePtr = m_instance->shared_from_this();
	m_backups		 = m_backupManager->listBackups(instancePtr);

	for (const InstanceBackup& backup : m_backups)
	{
		QString displayText = QString("%1 - %2 (%3)")
								  .arg(backup.name())
								  .arg(backup.createdAt().toString("yyyy-MM-dd HH:mm"))
								  .arg(backup.displaySize());
		ui->backupList->addItem(displayText);
	}

	updateBackupDetails();
}

void BackupPage::updateBackupDetails()
{
	int currentRow = ui->backupList->currentRow();
	if (currentRow < 0 || currentRow >= m_backups.size())
	{
		ui->backupDetails->clear();
		ui->restoreButton->setEnabled(false);
		ui->deleteButton->setEnabled(false);
		return;
	}

	const InstanceBackup& backup = m_backups[currentRow];

	QString details;
	details += tr("<b>Name:</b> %1<br>").arg(backup.name());
	details += tr("<b>Created:</b> %1<br>").arg(backup.createdAt().toString("yyyy-MM-dd HH:mm:ss"));
	details += tr("<b>Size:</b> %1<br>").arg(backup.displaySize());

	if (!backup.description().isEmpty())
	{
		details += tr("<b>Description:</b> %1<br>").arg(backup.description());
	}

	if (!backup.includedPaths().isEmpty())
	{
		details += tr("<b>Included:</b> %1").arg(backup.includedPaths().join(", "));
	}

	ui->backupDetails->setHtml(details);
	ui->restoreButton->setEnabled(true);
	ui->deleteButton->setEnabled(true);
}

void BackupPage::on_createButton_clicked()
{
	bool ok;
	QString backupName =
		QInputDialog::getText(this, tr("Create Backup"), tr("Backup name:"), QLineEdit::Normal, QString(), &ok);

	if (!ok)
	{
		return;
	}

	BackupOptions options = getSelectedOptions();

	// Disable UI during backup
	ui->createButton->setEnabled(false);
	ui->restoreButton->setEnabled(false);
	ui->deleteButton->setEnabled(false);
	ui->createButton->setText(tr("Creating..."));

	// Connect signals for this operation
	connect(
		m_backupManager,
		&BackupManager::backupCreated,
		this,
		[this](const QString&, const QString&)
		{
			ui->createButton->setEnabled(true);
			ui->restoreButton->setEnabled(true);
			ui->deleteButton->setEnabled(true);
			ui->createButton->setText(tr("Create Backup"));
			QMessageBox::information(this, tr("Success"), tr("Backup created successfully!"));
			refreshBackupList();
			disconnect(m_backupManager, &BackupManager::backupCreated, this, nullptr);
			disconnect(m_backupManager, &BackupManager::backupFailed, this, nullptr);
		},
		Qt::SingleShotConnection);

	connect(
		m_backupManager,
		&BackupManager::backupFailed,
		this,
		[this](const QString&, const QString& error)
		{
			ui->createButton->setEnabled(true);
			ui->restoreButton->setEnabled(true);
			ui->deleteButton->setEnabled(true);
			ui->createButton->setText(tr("Create Backup"));
			QMessageBox::critical(this, tr("Error"), tr("Failed to create backup: %1").arg(error));
			disconnect(m_backupManager, &BackupManager::backupCreated, this, nullptr);
			disconnect(m_backupManager, &BackupManager::backupFailed, this, nullptr);
		},
		Qt::SingleShotConnection);

	auto instancePtr = m_instance->shared_from_this();
	m_backupManager->createBackupAsync(instancePtr, backupName, options);
}

void BackupPage::on_restoreButton_clicked()
{
	int currentRow = ui->backupList->currentRow();
	if (currentRow < 0 || currentRow >= m_backups.size())
	{
		return;
	}

	const InstanceBackup& backup = m_backups[currentRow];

	// Create custom dialog with checkbox
	QMessageBox msgBox(this);
	msgBox.setWindowTitle(tr("Restore Backup"));
	msgBox.setText(tr("Are you sure you want to restore backup '%1'?").arg(backup.name()));
	msgBox.setInformativeText(tr("This will overwrite current instance data."));
	msgBox.setIcon(QMessageBox::Question);

	QCheckBox* safetyCheckBox = new QCheckBox(tr("Create safety backup before restoring (recommended)"));
	safetyCheckBox->setChecked(true);
	msgBox.setCheckBox(safetyCheckBox);

	msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
	msgBox.setDefaultButton(QMessageBox::No);

	if (msgBox.exec() != QMessageBox::Yes)
	{
		return;
	}

	bool createSafetyBackup = safetyCheckBox->isChecked();

	// Disable UI during restore
	ui->createButton->setEnabled(false);
	ui->restoreButton->setEnabled(false);
	ui->deleteButton->setEnabled(false);
	ui->restoreButton->setText(tr("Restoring..."));

	// Connect signals for this operation
	connect(
		m_backupManager,
		&BackupManager::backupRestored,
		this,
		[this](const QString&, const QString&)
		{
			ui->createButton->setEnabled(true);
			ui->restoreButton->setEnabled(true);
			ui->deleteButton->setEnabled(true);
			ui->restoreButton->setText(tr("Restore"));
			QMessageBox::information(this, tr("Success"), tr("Backup restored successfully!"));
			refreshBackupList();
			disconnect(m_backupManager, &BackupManager::backupRestored, this, nullptr);
			disconnect(m_backupManager, &BackupManager::restoreFailed, this, nullptr);
		},
		Qt::SingleShotConnection);

	connect(
		m_backupManager,
		&BackupManager::restoreFailed,
		this,
		[this](const QString&, const QString& error)
		{
			ui->createButton->setEnabled(true);
			ui->restoreButton->setEnabled(true);
			ui->deleteButton->setEnabled(true);
			ui->restoreButton->setText(tr("Restore"));
			QMessageBox::critical(this, tr("Error"), tr("Failed to restore backup: %1").arg(error));
			disconnect(m_backupManager, &BackupManager::backupRestored, this, nullptr);
			disconnect(m_backupManager, &BackupManager::restoreFailed, this, nullptr);
		},
		Qt::SingleShotConnection);

	auto instancePtr = m_instance->shared_from_this();
	m_backupManager->restoreBackupAsync(instancePtr, backup, createSafetyBackup);
}

void BackupPage::on_deleteButton_clicked()
{
	int currentRow = ui->backupList->currentRow();
	if (currentRow < 0 || currentRow >= m_backups.size())
	{
		return;
	}

	const InstanceBackup& backup = m_backups[currentRow];

	auto result = QMessageBox::question(this,
										tr("Delete Backup"),
										tr("Are you sure you want to delete backup '%1'?").arg(backup.name()),
										QMessageBox::Yes | QMessageBox::No,
										QMessageBox::No);

	if (result != QMessageBox::Yes)
	{
		return;
	}

	if (m_backupManager->deleteBackup(backup))
	{
		refreshBackupList();
		QMessageBox::information(this, tr("Success"), tr("Backup deleted successfully!"));
	}
	else
	{
		QMessageBox::critical(this, tr("Error"), tr("Failed to delete backup."));
	}
}

void BackupPage::on_backupList_currentRowChanged(int)
{
	updateBackupDetails();
}

BackupOptions BackupPage::getSelectedOptions() const
{
	BackupOptions options;
	options.includeSaves		 = ui->includeSaves->isChecked();
	options.includeConfig		 = ui->includeConfig->isChecked();
	options.includeMods			 = ui->includeMods->isChecked();
	options.includeResourcePacks = ui->includeResourcePacks->isChecked();
	options.includeShaderPacks	 = ui->includeShaderPacks->isChecked();
	options.includeScreenshots	 = ui->includeScreenshots->isChecked();
	options.includeOptions		 = ui->includeOptions->isChecked();

	// Get custom paths from list widget
	QStringList customPaths;
	for (int i = 0; i < ui->customPathsList->count(); ++i)
	{
		customPaths.append(ui->customPathsList->item(i)->text());
	}
	options.customPaths = customPaths;

	return options;
}

void BackupPage::on_addCustomPathButton_clicked()
{
	QString path = QInputDialog::getText(this,
										 tr("Add Custom Path"),
										 tr("Enter relative path to include (e.g., \"logs\", \"crash-reports\"):"),
										 QLineEdit::Normal,
										 QString(),
										 nullptr);

	if (!path.isEmpty())
	{
		// Check if already exists
		for (int i = 0; i < ui->customPathsList->count(); ++i)
		{
			if (ui->customPathsList->item(i)->text() == path)
			{
				return; // Already exists
			}
		}
		ui->customPathsList->addItem(path);
		updateEstimatedSize();
	}
}

void BackupPage::on_removeCustomPathButton_clicked()
{
	int currentRow = ui->customPathsList->currentRow();
	if (currentRow >= 0)
	{
		delete ui->customPathsList->takeItem(currentRow);
		updateEstimatedSize();
	}
}