2022-03-19 11:46:56 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-06-03 13:39:42 +01:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-03-19 11:46:56 +00:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2023-06-03 13:39:42 +01:00
|
|
|
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
|
2023-07-16 18:57:57 +01:00
|
|
|
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
|
2015-04-19 03:19:29 +01:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* 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.
|
2015-04-19 03:19:29 +01:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* 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.
|
2015-04-19 03:19:29 +01:00
|
|
|
*
|
2022-03-19 11:46:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-04-19 03:19:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ExportInstanceDialog.h"
|
|
|
|
#include <BaseInstance.h>
|
|
|
|
#include <MMCZip.h>
|
|
|
|
#include <QFileDialog>
|
2022-11-03 19:41:55 +00:00
|
|
|
#include <QFileSystemModel>
|
2023-06-29 22:01:36 +01:00
|
|
|
#include <QMessageBox>
|
2023-06-30 08:52:10 +01:00
|
|
|
#include "FileIgnoreProxy.h"
|
2023-07-16 18:57:57 +01:00
|
|
|
#include "QObjectPtr.h"
|
|
|
|
#include "ui/dialogs/CustomMessageBox.h"
|
|
|
|
#include "ui/dialogs/ProgressDialog.h"
|
2023-06-29 22:01:36 +01:00
|
|
|
#include "ui_ExportInstanceDialog.h"
|
2015-04-19 03:19:29 +01:00
|
|
|
|
2023-06-29 22:01:36 +01:00
|
|
|
#include <FileSystem.h>
|
|
|
|
#include <icons/IconList.h>
|
2015-04-19 03:19:29 +01:00
|
|
|
#include <QDebug>
|
2023-06-29 22:01:36 +01:00
|
|
|
#include <QFileInfo>
|
2015-04-19 03:19:29 +01:00
|
|
|
#include <QSaveFile>
|
2023-06-29 22:01:36 +01:00
|
|
|
#include <QSortFilterProxyModel>
|
2022-11-01 21:45:15 +00:00
|
|
|
#include <QStack>
|
2023-06-30 08:52:10 +01:00
|
|
|
#include <functional>
|
2021-11-20 15:22:22 +00:00
|
|
|
#include "Application.h"
|
2023-06-29 22:01:36 +01:00
|
|
|
#include "SeparatorPrefixTree.h"
|
2015-04-19 03:19:29 +01:00
|
|
|
|
2023-06-29 22:01:36 +01:00
|
|
|
ExportInstanceDialog::ExportInstanceDialog(InstancePtr instance, QWidget* parent)
|
2015-04-19 03:19:29 +01:00
|
|
|
: QDialog(parent), ui(new Ui::ExportInstanceDialog), m_instance(instance)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
auto model = new QFileSystemModel(this);
|
2023-06-03 13:39:42 +01:00
|
|
|
model->setIconProvider(&icons);
|
2023-03-02 11:25:36 +00:00
|
|
|
auto root = instance->instanceRoot();
|
2023-03-04 19:55:38 +00:00
|
|
|
proxyModel = new FileIgnoreProxy(root, this);
|
2015-04-19 03:19:29 +01:00
|
|
|
proxyModel->setSourceModel(model);
|
2023-06-29 22:01:36 +01:00
|
|
|
auto prefix = QDir(instance->instanceRoot()).relativeFilePath(instance->gameRoot());
|
2023-06-30 08:52:10 +01:00
|
|
|
proxyModel->ignoreFilesWithPath().insert({ FS::PathCombine(prefix, "logs"), FS::PathCombine(prefix, "crash-reports") });
|
2023-06-29 22:01:36 +01:00
|
|
|
proxyModel->ignoreFilesWithName().append({ ".DS_Store", "thumbs.db", "Thumbs.db" });
|
|
|
|
loadPackIgnore();
|
|
|
|
|
2015-04-19 03:19:29 +01:00
|
|
|
ui->treeView->setModel(proxyModel);
|
|
|
|
ui->treeView->setRootIndex(proxyModel->mapFromSource(model->index(root)));
|
2017-12-21 00:25:43 +00:00
|
|
|
ui->treeView->sortByColumn(0, Qt::AscendingOrder);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-07-16 18:53:58 +01:00
|
|
|
connect(proxyModel, SIGNAL(rowsInserted(QModelIndex, int, int)), SLOT(rowsInserted(QModelIndex, int, int)));
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2017-10-28 20:44:29 +01:00
|
|
|
model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
|
2015-04-19 03:19:29 +01:00
|
|
|
model->setRootPath(root);
|
|
|
|
auto headerView = ui->treeView->header();
|
|
|
|
headerView->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
headerView->setSectionResizeMode(0, QHeaderView::Stretch);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExportInstanceDialog::~ExportInstanceDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2015-06-01 00:19:12 +01:00
|
|
|
/// Save icon to instance's folder is needed
|
|
|
|
void SaveIcon(InstancePtr m_instance)
|
|
|
|
{
|
|
|
|
auto iconKey = m_instance->iconKey();
|
2021-11-20 15:22:22 +00:00
|
|
|
auto iconList = APPLICATION->icons();
|
2015-06-01 00:19:12 +01:00
|
|
|
auto mmcIcon = iconList->icon(iconKey);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!mmcIcon || mmcIcon->isBuiltIn()) {
|
2019-05-31 20:52:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto path = mmcIcon->getFilePath();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!path.isNull()) {
|
|
|
|
QFileInfo inInfo(path);
|
|
|
|
FS::copy(path, FS::PathCombine(m_instance->instanceRoot(), inInfo.fileName()))();
|
2019-05-31 20:52:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
auto& image = mmcIcon->m_images[mmcIcon->type()];
|
|
|
|
auto& icon = image.icon;
|
2019-05-31 20:52:58 +01:00
|
|
|
auto sizes = icon.availableSizes();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (sizes.size() == 0) {
|
2019-05-31 20:52:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-07-16 18:53:58 +01:00
|
|
|
auto areaOf = [](QSize size) { return size.width() * size.height(); };
|
2019-05-31 20:52:58 +01:00
|
|
|
QSize largest = sizes[0];
|
|
|
|
// find variant with largest area
|
2023-07-16 18:53:58 +01:00
|
|
|
for (auto size : sizes) {
|
|
|
|
if (areaOf(largest) < areaOf(size)) {
|
2019-05-31 20:52:58 +01:00
|
|
|
largest = size;
|
2015-06-01 00:19:12 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 20:52:58 +01:00
|
|
|
auto pixmap = icon.pixmap(largest);
|
|
|
|
pixmap.save(FS::PathCombine(m_instance->instanceRoot(), iconKey + ".png"));
|
2015-06-01 00:19:12 +01:00
|
|
|
}
|
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
void ExportInstanceDialog::doExport()
|
2015-04-19 03:19:29 +01:00
|
|
|
{
|
2015-10-05 00:47:27 +01:00
|
|
|
auto name = FS::RemoveInvalidFilenameChars(m_instance->name());
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-07-16 18:53:58 +01:00
|
|
|
const QString output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(m_instance->name()),
|
|
|
|
FS::PathCombine(QDir::homePath(), name + ".zip"), "Zip (*.zip)", nullptr);
|
|
|
|
if (output.isEmpty()) {
|
2023-07-16 18:57:57 +01:00
|
|
|
QDialog::done(QDialog::Rejected);
|
|
|
|
return;
|
2015-04-19 03:19:29 +01:00
|
|
|
}
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2015-06-01 00:19:12 +01:00
|
|
|
SaveIcon(m_instance);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2022-01-28 11:37:22 +00:00
|
|
|
auto files = QFileInfoList();
|
|
|
|
if (!MMCZip::collectFileListRecursively(m_instance->instanceRoot(), nullptr, &files,
|
2023-06-30 08:52:10 +01:00
|
|
|
std::bind(&FileIgnoreProxy::filterFile, proxyModel, std::placeholders::_1))) {
|
2022-01-28 11:37:22 +00:00
|
|
|
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
|
2023-07-16 18:57:57 +01:00
|
|
|
QDialog::done(QDialog::Rejected);
|
|
|
|
return;
|
2022-01-28 11:37:22 +00:00
|
|
|
}
|
2023-02-09 01:52:50 +00:00
|
|
|
|
2023-07-16 18:57:57 +01:00
|
|
|
auto task = makeShared<MMCZip::ExportToZipTask>(output, m_instance->instanceRoot(), files, "", true);
|
|
|
|
|
|
|
|
connect(task.get(), &Task::failed, this, [this, output](QString reason) {
|
|
|
|
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
|
|
|
|
QFile::remove(output);
|
|
|
|
});
|
|
|
|
connect(task.get(), &Task::aborted, this, [output] { QFile::remove(output); });
|
|
|
|
connect(task.get(), &Task::finished, this, [task] { task->deleteLater(); });
|
|
|
|
|
|
|
|
ProgressDialog progress(this);
|
|
|
|
progress.setSkipButton(true, tr("Abort"));
|
|
|
|
auto result = progress.execWithTask(task.get());
|
|
|
|
QDialog::done(result);
|
2015-04-19 03:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::done(int result)
|
|
|
|
{
|
|
|
|
savePackIgnore();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (result == QDialog::Accepted) {
|
2023-07-16 18:57:57 +01:00
|
|
|
doExport();
|
|
|
|
return;
|
2015-04-19 03:19:29 +01:00
|
|
|
}
|
|
|
|
QDialog::done(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::rowsInserted(QModelIndex parent, int top, int bottom)
|
|
|
|
{
|
2023-07-16 18:53:58 +01:00
|
|
|
// WARNING: possible off-by-one?
|
|
|
|
for (int i = top; i < bottom; i++) {
|
2022-02-04 14:02:12 +00:00
|
|
|
auto node = proxyModel->index(i, 0, parent);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (proxyModel->shouldExpand(node)) {
|
2015-04-19 03:19:29 +01:00
|
|
|
auto expNode = node.parent();
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!expNode.isValid()) {
|
2015-04-19 03:19:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ui->treeView->expand(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ExportInstanceDialog::ignoreFileName()
|
|
|
|
{
|
2015-10-05 00:47:27 +01:00
|
|
|
return FS::PathCombine(m_instance->instanceRoot(), ".packignore");
|
2015-04-19 03:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::loadPackIgnore()
|
|
|
|
{
|
|
|
|
auto filename = ignoreFileName();
|
|
|
|
QFile ignoreFile(filename);
|
2023-07-16 18:53:58 +01:00
|
|
|
if (!ignoreFile.open(QIODevice::ReadOnly)) {
|
2015-04-19 03:19:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto data = ignoreFile.readAll();
|
|
|
|
auto string = QString::fromUtf8(data);
|
2022-05-02 18:10:45 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
proxyModel->setBlockedPaths(string.split('\n', Qt::SkipEmptyParts));
|
|
|
|
#else
|
2015-04-19 03:19:29 +01:00
|
|
|
proxyModel->setBlockedPaths(string.split('\n', QString::SkipEmptyParts));
|
2022-05-02 18:10:45 +01:00
|
|
|
#endif
|
2015-04-19 03:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::savePackIgnore()
|
|
|
|
{
|
2015-06-03 20:10:28 +01:00
|
|
|
auto data = proxyModel->blockedPaths().toStringList().join('\n').toUtf8();
|
2015-04-19 03:19:29 +01:00
|
|
|
auto filename = ignoreFileName();
|
2023-07-16 18:53:58 +01:00
|
|
|
try {
|
2015-06-03 20:10:28 +01:00
|
|
|
FS::write(filename, data);
|
2023-07-16 18:53:58 +01:00
|
|
|
} catch (const Exception& e) {
|
2015-06-03 20:10:28 +01:00
|
|
|
qWarning() << e.cause();
|
2015-04-19 03:19:29 +01:00
|
|
|
}
|
|
|
|
}
|