2022-03-19 17:16:56 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2015-04-19 07:49:29 +05:30
|
|
|
*
|
2022-03-19 17:16:56 +05:30
|
|
|
* 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 07:49:29 +05:30
|
|
|
*
|
2022-03-19 17:16:56 +05:30
|
|
|
* 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 07:49:29 +05:30
|
|
|
*
|
2022-03-19 17:16:56 +05:30
|
|
|
* 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 07:49:29 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ExportInstanceDialog.h"
|
|
|
|
#include "ui_ExportInstanceDialog.h"
|
|
|
|
#include <BaseInstance.h>
|
|
|
|
#include <MMCZip.h>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2022-11-04 01:11:55 +05:30
|
|
|
#include <QFileSystemModel>
|
2015-04-19 07:49:29 +05:30
|
|
|
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QSaveFile>
|
2022-11-02 03:15:15 +05:30
|
|
|
#include <QStack>
|
2022-11-04 01:11:55 +05:30
|
|
|
#include "StringUtils.h"
|
2015-04-19 07:49:29 +05:30
|
|
|
#include "SeparatorPrefixTree.h"
|
2021-11-20 20:52:22 +05:30
|
|
|
#include "Application.h"
|
2015-06-01 04:49:12 +05:30
|
|
|
#include <icons/IconList.h>
|
2015-06-04 00:40:28 +05:30
|
|
|
#include <FileSystem.h>
|
2015-04-19 07:49:29 +05:30
|
|
|
|
|
|
|
ExportInstanceDialog::ExportInstanceDialog(InstancePtr instance, QWidget *parent)
|
2018-07-15 18:21:05 +05:30
|
|
|
: QDialog(parent), ui(new Ui::ExportInstanceDialog), m_instance(instance)
|
2015-04-19 07:49:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->setupUi(this);
|
|
|
|
auto model = new QFileSystemModel(this);
|
2023-03-02 16:55:36 +05:30
|
|
|
auto root = instance->instanceRoot();
|
|
|
|
proxyModel = new PackIgnoreProxy(root, this);
|
2018-07-15 18:21:05 +05:30
|
|
|
loadPackIgnore();
|
|
|
|
proxyModel->setSourceModel(model);
|
|
|
|
ui->treeView->setModel(proxyModel);
|
|
|
|
ui->treeView->setRootIndex(proxyModel->mapFromSource(model->index(root)));
|
|
|
|
ui->treeView->sortByColumn(0, Qt::AscendingOrder);
|
|
|
|
|
|
|
|
connect(proxyModel, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(rowsInserted(QModelIndex,int,int)));
|
|
|
|
|
|
|
|
model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
|
|
|
|
model->setRootPath(root);
|
|
|
|
auto headerView = ui->treeView->header();
|
|
|
|
headerView->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
headerView->setSectionResizeMode(0, QHeaderView::Stretch);
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ExportInstanceDialog::~ExportInstanceDialog()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
delete ui;
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
2015-06-01 04:49:12 +05:30
|
|
|
/// Save icon to instance's folder is needed
|
|
|
|
void SaveIcon(InstancePtr m_instance)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto iconKey = m_instance->iconKey();
|
2021-11-20 20:52:22 +05:30
|
|
|
auto iconList = APPLICATION->icons();
|
2018-07-15 18:21:05 +05:30
|
|
|
auto mmcIcon = iconList->icon(iconKey);
|
2019-06-01 01:22:58 +05:30
|
|
|
if(!mmcIcon || mmcIcon->isBuiltIn()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto path = mmcIcon->getFilePath();
|
|
|
|
if(!path.isNull()) {
|
|
|
|
QFileInfo inInfo (path);
|
|
|
|
FS::copy(path, FS::PathCombine(m_instance->instanceRoot(), inInfo.fileName())) ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto & image = mmcIcon->m_images[mmcIcon->type()];
|
|
|
|
auto & icon = image.icon;
|
|
|
|
auto sizes = icon.availableSizes();
|
|
|
|
if(sizes.size() == 0)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2019-06-01 01:22:58 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto areaOf = [](QSize size)
|
|
|
|
{
|
|
|
|
return size.width() * size.height();
|
|
|
|
};
|
|
|
|
QSize largest = sizes[0];
|
|
|
|
// find variant with largest area
|
|
|
|
for(auto size: sizes)
|
|
|
|
{
|
|
|
|
if(areaOf(largest) < areaOf(size))
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2019-06-01 01:22:58 +05:30
|
|
|
largest = size;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2019-06-01 01:22:58 +05:30
|
|
|
auto pixmap = icon.pixmap(largest);
|
|
|
|
pixmap.save(FS::PathCombine(m_instance->instanceRoot(), iconKey + ".png"));
|
2015-06-01 04:49:12 +05:30
|
|
|
}
|
|
|
|
|
2015-04-19 07:49:29 +05:30
|
|
|
bool ExportInstanceDialog::doExport()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto name = FS::RemoveInvalidFilenameChars(m_instance->name());
|
|
|
|
|
|
|
|
const QString output = QFileDialog::getSaveFileName(
|
|
|
|
this, tr("Export %1").arg(m_instance->name()),
|
2023-03-02 22:10:49 +05:30
|
|
|
FS::PathCombine(QDir::homePath(), name + ".zip"), "Zip (*.zip)", nullptr);
|
2018-07-15 18:21:05 +05:30
|
|
|
if (output.isEmpty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SaveIcon(m_instance);
|
|
|
|
|
|
|
|
auto & blocked = proxyModel->blockedPaths();
|
|
|
|
using std::placeholders::_1;
|
2022-01-28 17:07:22 +05:30
|
|
|
auto files = QFileInfoList();
|
|
|
|
if (!MMCZip::collectFileListRecursively(m_instance->instanceRoot(), nullptr, &files,
|
|
|
|
std::bind(&SeparatorPrefixTree<'/'>::covers, blocked, _1))) {
|
|
|
|
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!MMCZip::compressDirFiles(output, m_instance->instanceRoot(), files))
|
2022-01-25 03:32:06 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
|
|
|
|
return false;
|
2022-01-25 03:32:06 +05:30
|
|
|
}
|
|
|
|
return true;
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::done(int result)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
savePackIgnore();
|
|
|
|
if (result == QDialog::Accepted)
|
|
|
|
{
|
|
|
|
if (doExport())
|
|
|
|
{
|
|
|
|
QDialog::done(QDialog::Accepted);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QDialog::done(result);
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::rowsInserted(QModelIndex parent, int top, int bottom)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
//WARNING: possible off-by-one?
|
|
|
|
for(int i = top; i < bottom; i++)
|
|
|
|
{
|
2022-02-04 19:32:12 +05:30
|
|
|
auto node = proxyModel->index(i, 0, parent);
|
2018-07-15 18:21:05 +05:30
|
|
|
if(proxyModel->shouldExpand(node))
|
|
|
|
{
|
|
|
|
auto expNode = node.parent();
|
|
|
|
if(!expNode.isValid())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ui->treeView->expand(node);
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString ExportInstanceDialog::ignoreFileName()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return FS::PathCombine(m_instance->instanceRoot(), ".packignore");
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::loadPackIgnore()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto filename = ignoreFileName();
|
|
|
|
QFile ignoreFile(filename);
|
|
|
|
if(!ignoreFile.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto data = ignoreFile.readAll();
|
|
|
|
auto string = QString::fromUtf8(data);
|
2022-05-02 22:40:45 +05:30
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
|
|
|
proxyModel->setBlockedPaths(string.split('\n', Qt::SkipEmptyParts));
|
|
|
|
#else
|
2018-07-15 18:21:05 +05:30
|
|
|
proxyModel->setBlockedPaths(string.split('\n', QString::SkipEmptyParts));
|
2022-05-02 22:40:45 +05:30
|
|
|
#endif
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ExportInstanceDialog::savePackIgnore()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto data = proxyModel->blockedPaths().toStringList().join('\n').toUtf8();
|
|
|
|
auto filename = ignoreFileName();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FS::write(filename, data);
|
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
|
|
|
qWarning() << e.cause();
|
|
|
|
}
|
2015-04-19 07:49:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#include "ExportInstanceDialog.moc"
|