11#include <QDialogButtonBox>
25 : QObject(parent), service_(service)
39 snapshotId_ = snapshotId;
44 snapshotId_ = snapshotId;
52 service_->cancelOperation();
66 switch (progress.state)
69 message =
"Starting...";
72 message = QString(
"Scanning files... (%1 found)").arg(progress.totalFiles);
75 message = QString(
"Copying: %1").arg(QString::fromStdString(progress.currentFile));
78 message =
"Compressing snapshot...";
81 message =
"Completed!";
84 message = QString(
"Failed: %1").arg(QString::fromStdString(progress.errorMessage));
87 message =
"Cancelled";
101 service_->createSnapshot(prefixPath_.toStdString(), snapshotName_.toStdString(),
102 snapshotDesc_.toStdString(), progressCallback);
111 .arg(QString::fromStdString(result->name)));
124 bool success = service_->restoreSnapshot(snapshotId_.toStdString(), progressCallback);
145 bool success = service_->deleteSnapshot(snapshotId_.toStdString());
170 themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
172 setWindowTitle(
"Wine Prefix Snapshots");
173 setMinimumSize(700, 500);
190 workerThread_->quit();
191 workerThread_->wait();
197 currentPrefixPath_ = prefixPath;
198 currentGameName_ = gameName;
200 QString labelText = QString(
"<b>Prefix:</b> %1").arg(prefixPath);
201 if (!gameName.isEmpty())
203 labelText = QString(
"<b>Game:</b> %1<br><b>Prefix:</b> %2").arg(gameName, prefixPath);
205 prefixLabel_->setText(labelText);
207 refreshSnapshotList();
211void PrefixSnapshotDialog::setupUI()
213 auto* mainLayout =
new QVBoxLayout(
this);
214 mainLayout->setSpacing(12);
217 prefixLabel_ =
new QLabel(
"No prefix selected");
218 prefixLabel_->setWordWrap(
true);
219 mainLayout->addWidget(prefixLabel_);
222 auto* splitter =
new QSplitter(Qt::Horizontal);
225 auto* listGroup =
new QGroupBox(
"Snapshots");
226 auto* listLayout =
new QVBoxLayout(listGroup);
228 snapshotTree_ =
new QTreeWidget;
229 snapshotTree_->setHeaderLabels({
"Name",
"Date",
"Size"});
230 snapshotTree_->setAlternatingRowColors(
true);
231 snapshotTree_->setRootIsDecorated(
false);
232 snapshotTree_->header()->setSectionResizeMode(0, QHeaderView::Stretch);
233 snapshotTree_->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
234 snapshotTree_->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
235 listLayout->addWidget(snapshotTree_);
237 connect(snapshotTree_, &QTreeWidget::itemSelectionChanged,
this,
238 &PrefixSnapshotDialog::onSnapshotSelectionChanged);
240 splitter->addWidget(listGroup);
243 auto* rightWidget =
new QWidget;
244 auto* rightLayout =
new QVBoxLayout(rightWidget);
245 rightLayout->setContentsMargins(0, 0, 0, 0);
248 auto* detailsGroup =
new QGroupBox(
"Details");
249 auto* detailsLayout =
new QVBoxLayout(detailsGroup);
251 detailsLabel_ =
new QLabel(
"Select a snapshot to view details");
252 detailsLabel_->setWordWrap(
true);
253 detailsLabel_->setAlignment(Qt::AlignTop);
254 detailsLayout->addWidget(detailsLabel_);
256 rightLayout->addWidget(detailsGroup);
259 auto* createGroup =
new QGroupBox(
"Create New Snapshot");
260 auto* createLayout =
new QGridLayout(createGroup);
262 createLayout->addWidget(
new QLabel(
"Name:"), 0, 0);
263 snapshotNameEdit_ =
new QLineEdit;
264 snapshotNameEdit_->setPlaceholderText(
"Enter snapshot name...");
265 createLayout->addWidget(snapshotNameEdit_, 0, 1);
267 createLayout->addWidget(
new QLabel(
"Description:"), 1, 0, Qt::AlignTop);
268 snapshotDescEdit_ =
new QTextEdit;
269 snapshotDescEdit_->setPlaceholderText(
"Optional description...");
270 snapshotDescEdit_->setMaximumHeight(80);
271 createLayout->addWidget(snapshotDescEdit_, 1, 1);
273 createBtn_ =
new QPushButton(
"Create Snapshot");
274 createBtn_->setToolTip(
"Create a new snapshot of the current prefix state");
275 createLayout->addWidget(createBtn_, 2, 0, 1, 2);
277 connect(createBtn_, &QPushButton::clicked,
this, &PrefixSnapshotDialog::onCreateSnapshot);
279 rightLayout->addWidget(createGroup);
282 auto* actionsGroup =
new QGroupBox(
"Actions");
283 auto* actionsLayout =
new QHBoxLayout(actionsGroup);
285 restoreBtn_ =
new QPushButton(
"Restore");
286 restoreBtn_->setToolTip(
"Restore prefix to selected snapshot state");
287 restoreBtn_->setEnabled(
false);
288 actionsLayout->addWidget(restoreBtn_);
290 deleteBtn_ =
new QPushButton(
"Delete");
291 deleteBtn_->setToolTip(
"Delete selected snapshot");
292 deleteBtn_->setEnabled(
false);
293 actionsLayout->addWidget(deleteBtn_);
295 connect(restoreBtn_, &QPushButton::clicked,
this, &PrefixSnapshotDialog::onRestoreSnapshot);
296 connect(deleteBtn_, &QPushButton::clicked,
this, &PrefixSnapshotDialog::onDeleteSnapshot);
298 rightLayout->addWidget(actionsGroup);
299 rightLayout->addStretch();
301 splitter->addWidget(rightWidget);
302 splitter->setSizes({400, 350});
304 mainLayout->addWidget(splitter, 1);
307 auto* progressLayout =
new QHBoxLayout;
308 statusLabel_ =
new QLabel;
309 progressLayout->addWidget(statusLabel_, 1);
311 progressBar_ =
new QProgressBar;
312 progressBar_->setVisible(
false);
313 progressBar_->setMinimum(0);
314 progressBar_->setMaximum(100);
315 progressLayout->addWidget(progressBar_);
317 mainLayout->addLayout(progressLayout);
320 auto* buttonBox =
new QDialogButtonBox;
321 closeBtn_ = buttonBox->addButton(QDialogButtonBox::Close);
322 connect(closeBtn_, &QPushButton::clicked,
this, &QDialog::accept);
323 mainLayout->addWidget(buttonBox);
326void PrefixSnapshotDialog::refreshSnapshotList()
328 snapshotTree_->clear();
330 if (currentPrefixPath_.isEmpty())
335 auto snapshots = snapshotService_->getSnapshotsForPrefix(currentPrefixPath_.toStdString());
338 std::sort(snapshots.begin(), snapshots.end(),
339 [](
const PrefixSnapshot& a,
const PrefixSnapshot& b)
340 { return a.created > b.created; });
342 for (
const auto& snapshot : snapshots)
344 auto* item =
new QTreeWidgetItem;
345 item->setText(0, QString::fromStdString(snapshot.name));
346 item->setText(1, formatDate(snapshot.created));
347 item->setText(2, formatSize(snapshot.sizeBytes));
348 item->setData(0, Qt::UserRole, QString::fromStdString(snapshot.id));
351 item->setData(0, Qt::UserRole + 1, QString::fromStdString(snapshot.description));
352 item->setData(0, Qt::UserRole + 2, QString::fromStdString(snapshot.wineVersion));
353 item->setData(0, Qt::UserRole + 3, QString::fromStdString(snapshot.architecture));
354 item->setData(0, Qt::UserRole + 4, snapshot.fileCount);
355 item->setData(0, Qt::UserRole + 5, snapshot.isIncremental);
357 if (snapshot.isIncremental)
359 item->setIcon(0, QIcon::fromTheme(
"document-save-as"));
360 item->setToolTip(0,
"Incremental snapshot");
363 snapshotTree_->addTopLevelItem(item);
367 int64_t totalSize = 0;
368 for (
const auto& s : snapshots)
370 totalSize += s.sizeBytes;
373 statusLabel_->setText(
374 QString(
"%1 snapshot(s), %2 total").arg(snapshots.size()).arg(formatSize(totalSize)));
377void PrefixSnapshotDialog::updateButtons()
379 bool hasSelection = !snapshotTree_->selectedItems().isEmpty();
380 bool canOperate = !operationInProgress_ && !currentPrefixPath_.isEmpty();
382 restoreBtn_->setEnabled(hasSelection && canOperate);
383 deleteBtn_->setEnabled(hasSelection && canOperate);
384 createBtn_->setEnabled(canOperate && !snapshotNameEdit_->text().trimmed().isEmpty());
385 closeBtn_->setEnabled(!operationInProgress_);
388void PrefixSnapshotDialog::onSnapshotSelectionChanged()
392 auto selected = snapshotTree_->selectedItems();
393 if (selected.isEmpty())
395 detailsLabel_->setText(
"Select a snapshot to view details");
399 auto* item = selected.first();
400 QString name = item->text(0);
401 QString date = item->text(1);
402 QString size = item->text(2);
403 QString desc = item->data(0, Qt::UserRole + 1).toString();
404 QString wineVersion = item->data(0, Qt::UserRole + 2).toString();
405 QString arch = item->data(0, Qt::UserRole + 3).toString();
406 int fileCount = item->data(0, Qt::UserRole + 4).toInt();
407 bool isIncremental = item->data(0, Qt::UserRole + 5).toBool();
409 QString details = QString(
"<b>%1</b><br><br>").arg(name);
413 details += QString(
"%1<br><br>").arg(desc);
416 details += QString(
"<b>Created:</b> %1<br>").arg(date);
417 details += QString(
"<b>Size:</b> %1<br>").arg(size);
418 details += QString(
"<b>Files:</b> %1<br>").arg(fileCount);
420 if (!wineVersion.isEmpty())
422 details += QString(
"<b>Wine Version:</b> %1<br>").arg(wineVersion);
426 details += QString(
"<b>Architecture:</b> %1<br>").arg(arch);
430 details +=
"<br><i>This is an incremental snapshot</i>";
433 detailsLabel_->setText(details);
436void PrefixSnapshotDialog::onCreateSnapshot()
438 QString name = snapshotNameEdit_->text().trimmed();
441 QMessageBox::warning(
this,
"Invalid Name",
"Please enter a name for the snapshot.");
445 if (currentPrefixPath_.isEmpty())
447 QMessageBox::warning(
this,
"No Prefix",
"No Wine prefix is set.");
451 operationInProgress_ =
true;
453 progressBar_->setValue(0);
454 progressBar_->setVisible(
true);
457 workerThread_ =
new QThread(
this);
458 worker_ =
new SnapshotWorker(snapshotService_.get());
459 worker_->moveToThread(workerThread_);
462 worker_->setCreateParams(currentPrefixPath_, name, snapshotDescEdit_->toPlainText());
468 Qt::QueuedConnection);
470 &PrefixSnapshotDialog::onOperationComplete, Qt::QueuedConnection);
472 connect(workerThread_, &QThread::finished, worker_, &QObject::deleteLater);
473 connect(workerThread_, &QThread::finished, workerThread_, &QObject::deleteLater);
475 workerThread_->start();
478void PrefixSnapshotDialog::onRestoreSnapshot()
480 auto selected = snapshotTree_->selectedItems();
481 if (selected.isEmpty())
484 QString snapshotId = selected.first()->data(0, Qt::UserRole).toString();
485 QString snapshotName = selected.first()->text(0);
487 auto reply = QMessageBox::question(
488 this,
"Restore Snapshot",
489 QString(
"Are you sure you want to restore the prefix to snapshot '%1'?\n\n"
490 "The current prefix state will be backed up before restoration.")
492 QMessageBox::Yes | QMessageBox::No);
494 if (reply != QMessageBox::Yes)
497 operationInProgress_ =
true;
499 progressBar_->setValue(0);
500 progressBar_->setVisible(
true);
503 workerThread_ =
new QThread(
this);
504 worker_ =
new SnapshotWorker(snapshotService_.get());
505 worker_->moveToThread(workerThread_);
508 worker_->setRestoreParams(snapshotId);
514 Qt::QueuedConnection);
516 &PrefixSnapshotDialog::onOperationComplete, Qt::QueuedConnection);
518 connect(workerThread_, &QThread::finished, worker_, &QObject::deleteLater);
519 connect(workerThread_, &QThread::finished, workerThread_, &QObject::deleteLater);
521 workerThread_->start();
524void PrefixSnapshotDialog::onDeleteSnapshot()
526 auto selected = snapshotTree_->selectedItems();
527 if (selected.isEmpty())
530 QString snapshotId = selected.first()->data(0, Qt::UserRole).toString();
531 QString snapshotName = selected.first()->text(0);
533 auto reply = QMessageBox::question(
534 this,
"Delete Snapshot",
535 QString(
"Are you sure you want to delete snapshot '%1'?\n\nThis action cannot be undone.")
537 QMessageBox::Yes | QMessageBox::No);
539 if (reply != QMessageBox::Yes)
542 operationInProgress_ =
true;
544 progressBar_->setValue(0);
545 progressBar_->setVisible(
true);
548 workerThread_ =
new QThread(
this);
549 worker_ =
new SnapshotWorker(snapshotService_.get());
550 worker_->moveToThread(workerThread_);
553 worker_->setDeleteParams(snapshotId);
559 Qt::QueuedConnection);
561 &PrefixSnapshotDialog::onOperationComplete, Qt::QueuedConnection);
563 connect(workerThread_, &QThread::finished, worker_, &QObject::deleteLater);
564 connect(workerThread_, &QThread::finished, workerThread_, &QObject::deleteLater);
566 workerThread_->start();
569void PrefixSnapshotDialog::onProgressUpdate(
const QString& message,
int progress)
571 statusLabel_->setText(message);
572 progressBar_->setValue(progress);
575void PrefixSnapshotDialog::onOperationComplete(
bool success,
const QString& message)
577 operationInProgress_ =
false;
578 progressBar_->setVisible(
false);
579 workerThread_ =
nullptr;
584 statusLabel_->setText(message);
585 snapshotNameEdit_->clear();
586 snapshotDescEdit_->clear();
587 refreshSnapshotList();
591 statusLabel_->setText(QString(
"<span style='color: %1;'>%2</span>")
592 .arg(themeManager_->colors().error.name(), message));
593 QMessageBox::warning(
this,
"Operation Failed", message);
599void PrefixSnapshotDialog::applyTheme()
601 const auto& c = themeManager_->colors();
604 setStyleSheet(QString(
"QDialog { background-color: %1; }").arg(c.windowBackground.name()));
607 snapshotTree_->setStyleSheet(QString(R
"(
609 background-color: %1;
610 border: 1px solid %2;
617 QTreeWidget::item:selected {
618 background-color: %4;
621 QTreeWidget::item:hover:!selected {
622 background-color: %6;
624 QHeaderView::section {
625 background-color: %7;
629 border-bottom: 1px solid %2;
632 .arg(c.inputBackground.name())
633 .arg(c.border.name())
634 .arg(c.textPrimary.name())
635 .arg(c.accent.name())
636 .arg(c.textOnAccent.name())
637 .arg(c.hoverBackground.name())
638 .arg(c.panelBackground.name()));
641 progressBar_->setStyleSheet(QString(R
"(
643 border: 1px solid %1;
646 background-color: %2;
649 QProgressBar::chunk {
650 background-color: %4;
654 .arg(c.border.name())
655 .arg(c.inputBackground.name())
656 .arg(c.textPrimary.name())
657 .arg(c.accent.name()));
660 QString buttonStyle = QString(R
"(
662 background-color: %1;
664 border: 1px solid %3;
670 background-color: %4;
672 QPushButton:pressed {
673 background-color: %5;
675 QPushButton:disabled {
676 background-color: %6;
680 .arg(c.cardBackground.name())
681 .arg(c.textPrimary.name())
682 .arg(c.border.name())
683 .arg(c.hoverBackground.name())
684 .arg(c.panelBackground.name())
685 .arg(c.panelBackground.name())
686 .arg(c.textMuted.name());
688 restoreBtn_->setStyleSheet(buttonStyle);
689 deleteBtn_->setStyleSheet(buttonStyle);
690 closeBtn_->setStyleSheet(buttonStyle);
693 createBtn_->setStyleSheet(QString(R
"(
695 background-color: %1;
703 background-color: %3;
705 QPushButton:disabled {
706 background-color: %4;
710 .arg(c.accent.name())
711 .arg(c.textOnAccent.name())
712 .arg(c.accentHover.name())
713 .arg(c.panelBackground.name())
714 .arg(c.textMuted.name()));
717 QString inputStyle = QString(R
"(
718 QLineEdit, QTextEdit {
719 background-color: %1;
721 border: 1px solid %3;
725 QLineEdit:focus, QTextEdit:focus {
729 .arg(c.inputBackground.name())
730 .arg(c.textPrimary.name())
731 .arg(c.border.name())
732 .arg(c.accent.name());
734 snapshotNameEdit_->setStyleSheet(inputStyle);
735 snapshotDescEdit_->setStyleSheet(inputStyle);
738 QString groupStyle = QString(R
"(
742 border: 1px solid %2;
748 subcontrol-origin: margin;
753 .arg(c.textPrimary.name())
754 .arg(c.border.name());
756 for (
auto* groupBox : findChildren<QGroupBox*>())
758 groupBox->setStyleSheet(groupStyle);
762 auto setLabelColor = [](QLabel* label,
const QColor& color)
764 QPalette p = label->palette();
765 p.setColor(QPalette::WindowText, color);
766 label->setPalette(p);
768 setLabelColor(prefixLabel_, c.textPrimary);
769 setLabelColor(detailsLabel_, c.textPrimary);
770 setLabelColor(statusLabel_, c.textSecondary);
773QString PrefixSnapshotDialog::formatSize(int64_t bytes)
const
777 return QString(
"%1 B").arg(bytes);
779 else if (bytes < 1024 * 1024)
781 return QString(
"%1 KB").arg(bytes / 1024);
783 else if (bytes < 1024 * 1024 * 1024)
785 return QString(
"%1 MB").arg(bytes / (1024 * 1024));
789 return QString(
"%1 GB").arg(
static_cast<double>(bytes) / (1024.0 * 1024 * 1024), 0,
'f', 1);
793QString PrefixSnapshotDialog::formatDate(
const std::chrono::system_clock::time_point& tp)
const
795 auto time = std::chrono::system_clock::to_time_t(tp);
796 std::tm* tm = std::localtime(&time);
799 std::strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M", tm);
800 return QString(buffer);
void setWinePrefix(const QString &prefixPath, const QString &gameName="")
PrefixSnapshotDialog(QWidget *parent=nullptr, ThemeManager *themeManager=nullptr)
~PrefixSnapshotDialog() override
Service for managing Wine prefix snapshots.
void setCreateParams(const QString &prefix, const QString &name, const QString &desc)
SnapshotWorker(PrefixSnapshotService *service, QObject *parent=nullptr)
void setDeleteParams(const QString &snapshotId)
void setRestoreParams(const QString &snapshotId)
void progressUpdate(const QString &message, int progress)
void operationComplete(bool success, const QString &message)
void themeChanged()
Emitted when theme or accent color changes.
Progress information during snapshot operations.