2016-02-29 08:52:15 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/FilesystemWidget.h"
|
2016-02-29 08:52:15 +00:00
|
|
|
|
2017-08-25 18:32:37 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QFileDialog>
|
2018-06-27 12:04:41 +00:00
|
|
|
#include <QFileInfo>
|
2017-08-25 18:32:37 +00:00
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QProgressDialog>
|
|
|
|
#include <QStandardItemModel>
|
|
|
|
#include <QStyleFactory>
|
|
|
|
#include <QTreeView>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include <future>
|
|
|
|
|
|
|
|
#include "DiscIO/DiscExtractor.h"
|
|
|
|
#include "DiscIO/Filesystem.h"
|
2018-05-13 19:55:51 +00:00
|
|
|
#include "DiscIO/Volume.h"
|
|
|
|
|
2019-03-04 19:49:00 +00:00
|
|
|
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Resources.h"
|
2017-08-25 18:32:37 +00:00
|
|
|
|
2018-06-02 16:14:42 +00:00
|
|
|
#include "UICommon/UICommon.h"
|
|
|
|
|
2017-08-25 18:32:37 +00:00
|
|
|
constexpr int ENTRY_PARTITION = Qt::UserRole;
|
|
|
|
constexpr int ENTRY_NAME = Qt::UserRole + 1;
|
|
|
|
constexpr int ENTRY_TYPE = Qt::UserRole + 2;
|
|
|
|
|
|
|
|
enum class EntryType
|
|
|
|
{
|
|
|
|
Disc = -2,
|
|
|
|
Partition = -1,
|
|
|
|
File = 0,
|
|
|
|
Dir = 1
|
|
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(EntryType);
|
|
|
|
|
2019-03-21 22:04:56 +00:00
|
|
|
FilesystemWidget::FilesystemWidget(std::shared_ptr<DiscIO::Volume> volume)
|
|
|
|
: m_volume(std::move(volume))
|
2017-08-25 18:32:37 +00:00
|
|
|
{
|
|
|
|
CreateWidgets();
|
|
|
|
ConnectWidgets();
|
|
|
|
PopulateView();
|
|
|
|
}
|
|
|
|
|
2018-05-13 19:55:51 +00:00
|
|
|
FilesystemWidget::~FilesystemWidget() = default;
|
|
|
|
|
2017-08-25 18:32:37 +00:00
|
|
|
void FilesystemWidget::CreateWidgets()
|
|
|
|
{
|
|
|
|
auto* layout = new QVBoxLayout;
|
|
|
|
|
2018-06-02 16:14:42 +00:00
|
|
|
m_tree_model = new QStandardItemModel(0, 2);
|
|
|
|
m_tree_model->setHorizontalHeaderLabels({tr("Name"), tr("Size")});
|
|
|
|
|
2017-08-25 18:32:37 +00:00
|
|
|
m_tree_view = new QTreeView(this);
|
|
|
|
m_tree_view->setModel(m_tree_model);
|
|
|
|
m_tree_view->setContextMenuPolicy(Qt::CustomContextMenu);
|
2018-06-02 16:14:42 +00:00
|
|
|
|
|
|
|
auto* header = m_tree_view->header();
|
|
|
|
|
|
|
|
header->setSectionResizeMode(0, QHeaderView::Stretch);
|
|
|
|
header->setSectionResizeMode(1, QHeaderView::ResizeToContents);
|
|
|
|
header->setStretchLastSection(false);
|
2017-08-25 18:32:37 +00:00
|
|
|
|
|
|
|
// Windows: Set style to (old) windows, which draws branch lines
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (QApplication::style()->objectName() == QStringLiteral("windowsvista"))
|
|
|
|
m_tree_view->setStyle(QStyleFactory::create(QStringLiteral("windows")));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
layout->addWidget(m_tree_view);
|
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilesystemWidget::ConnectWidgets()
|
|
|
|
{
|
|
|
|
connect(m_tree_view, &QTreeView::customContextMenuRequested, this,
|
|
|
|
&FilesystemWidget::ShowContextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilesystemWidget::PopulateView()
|
|
|
|
{
|
2019-02-01 00:13:50 +00:00
|
|
|
// Cache these two icons, the tree will use them a lot.
|
|
|
|
m_folder_icon = Resources::GetScaledIcon("isoproperties_folder");
|
|
|
|
m_file_icon = Resources::GetScaledIcon("isoproperties_file");
|
|
|
|
|
2017-08-25 18:32:37 +00:00
|
|
|
auto* disc = new QStandardItem(tr("Disc"));
|
|
|
|
disc->setEditable(false);
|
|
|
|
disc->setIcon(Resources::GetScaledIcon("isoproperties_disc"));
|
|
|
|
disc->setData(QVariant::fromValue(EntryType::Disc), ENTRY_TYPE);
|
|
|
|
m_tree_model->appendRow(disc);
|
|
|
|
m_tree_view->expand(disc->index());
|
|
|
|
|
|
|
|
const auto& partitions = m_volume->GetPartitions();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < partitions.size(); i++)
|
|
|
|
{
|
|
|
|
auto* item = new QStandardItem(tr("Partition %1").arg(i));
|
|
|
|
item->setEditable(false);
|
|
|
|
|
|
|
|
item->setIcon(Resources::GetScaledIcon("isoproperties_disc"));
|
|
|
|
item->setData(static_cast<qlonglong>(i), ENTRY_PARTITION);
|
|
|
|
item->setData(QVariant::fromValue(EntryType::Partition), ENTRY_TYPE);
|
|
|
|
|
2017-09-15 17:44:23 +00:00
|
|
|
PopulateDirectory(static_cast<int>(i), item, partitions[i]);
|
2017-08-25 18:32:37 +00:00
|
|
|
|
|
|
|
disc->appendRow(item);
|
|
|
|
|
|
|
|
if (m_volume->GetGamePartition() == partitions[i])
|
|
|
|
m_tree_view->expand(item->index());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partitions.empty())
|
2017-09-15 17:44:23 +00:00
|
|
|
PopulateDirectory(-1, disc, DiscIO::PARTITION_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilesystemWidget::PopulateDirectory(int partition_id, QStandardItem* root,
|
|
|
|
const DiscIO::Partition& partition)
|
|
|
|
{
|
|
|
|
const DiscIO::FileSystem* const file_system = m_volume->GetFileSystem(partition);
|
|
|
|
if (file_system)
|
|
|
|
PopulateDirectory(partition_id, root, file_system->GetRoot());
|
2017-08-25 18:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FilesystemWidget::PopulateDirectory(int partition_id, QStandardItem* root,
|
|
|
|
const DiscIO::FileInfo& directory)
|
|
|
|
{
|
|
|
|
for (const auto& info : directory)
|
|
|
|
{
|
|
|
|
auto* item = new QStandardItem(QString::fromStdString(info.GetName()));
|
|
|
|
item->setEditable(false);
|
2019-02-01 00:13:50 +00:00
|
|
|
item->setIcon(info.IsDirectory() ? m_folder_icon : m_file_icon);
|
2017-08-25 18:32:37 +00:00
|
|
|
|
|
|
|
if (info.IsDirectory())
|
|
|
|
PopulateDirectory(partition_id, item, info);
|
|
|
|
|
|
|
|
item->setData(partition_id, ENTRY_PARTITION);
|
|
|
|
item->setData(QString::fromStdString(info.GetPath()), ENTRY_NAME);
|
|
|
|
item->setData(QVariant::fromValue(info.IsDirectory() ? EntryType::Dir : EntryType::File),
|
|
|
|
ENTRY_TYPE);
|
2018-06-02 16:14:42 +00:00
|
|
|
|
|
|
|
const auto size = info.GetTotalSize();
|
|
|
|
|
|
|
|
auto* size_item = new QStandardItem(QString::fromStdString(UICommon::FormatSize(size)));
|
|
|
|
size_item->setTextAlignment(Qt::AlignRight);
|
|
|
|
size_item->setEditable(false);
|
|
|
|
|
|
|
|
root->appendRow({item, size_item});
|
2017-08-25 18:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 00:24:09 +00:00
|
|
|
QString FilesystemWidget::SelectFolder()
|
2017-08-25 18:32:37 +00:00
|
|
|
{
|
2018-07-02 00:24:09 +00:00
|
|
|
return QFileDialog::getExistingDirectory(this, QObject::tr("Choose the folder to extract to"));
|
2017-08-25 18:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FilesystemWidget::ShowContextMenu(const QPoint&)
|
|
|
|
{
|
|
|
|
auto* selection = m_tree_view->selectionModel();
|
|
|
|
if (!selection->hasSelection())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto* item = m_tree_model->itemFromIndex(selection->selectedIndexes()[0]);
|
|
|
|
|
|
|
|
QMenu* menu = new QMenu(this);
|
|
|
|
|
|
|
|
EntryType type = item->data(ENTRY_TYPE).value<EntryType>();
|
|
|
|
|
2018-01-31 12:51:46 +00:00
|
|
|
DiscIO::Partition partition = type == EntryType::Disc ?
|
|
|
|
DiscIO::PARTITION_NONE :
|
|
|
|
GetPartitionFromID(item->data(ENTRY_PARTITION).toInt());
|
|
|
|
QString path = item->data(ENTRY_NAME).toString();
|
|
|
|
|
2018-05-05 14:39:51 +00:00
|
|
|
const bool is_filesystem_root = (type == EntryType::Disc && m_volume->GetPartitions().empty()) ||
|
|
|
|
type == EntryType::Partition;
|
|
|
|
|
|
|
|
if (type == EntryType::Dir || is_filesystem_root)
|
|
|
|
{
|
2018-07-09 08:02:10 +00:00
|
|
|
menu->addAction(tr("Extract Files..."), this, [this, partition, path] {
|
2018-05-05 14:39:51 +00:00
|
|
|
auto folder = SelectFolder();
|
|
|
|
|
|
|
|
if (!folder.isEmpty())
|
|
|
|
ExtractDirectory(partition, path, folder);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_filesystem_root)
|
2017-08-25 18:32:37 +00:00
|
|
|
{
|
2018-07-09 08:02:10 +00:00
|
|
|
menu->addAction(tr("Extract System Data..."), this, [this, partition] {
|
2017-08-25 18:32:37 +00:00
|
|
|
auto folder = SelectFolder();
|
|
|
|
|
2018-05-05 14:59:21 +00:00
|
|
|
if (folder.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ExtractSystemData(partition, folder))
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::information(this, tr("Success"),
|
|
|
|
tr("Successfully extracted system data."));
|
2018-05-05 14:59:21 +00:00
|
|
|
else
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::critical(this, tr("Error"), tr("Failed to extract system data."));
|
2017-08-25 18:32:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case EntryType::Disc:
|
2018-07-09 08:02:10 +00:00
|
|
|
menu->addAction(tr("Extract Entire Disc..."), this, [this, path] {
|
2017-08-25 18:32:37 +00:00
|
|
|
auto folder = SelectFolder();
|
|
|
|
|
|
|
|
if (folder.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_volume->GetPartitions().empty())
|
|
|
|
{
|
|
|
|
ExtractPartition(DiscIO::PARTITION_NONE, folder);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-05 14:27:36 +00:00
|
|
|
for (DiscIO::Partition& p : m_volume->GetPartitions())
|
|
|
|
{
|
|
|
|
if (const std::optional<u32> partition_type = m_volume->GetPartitionType(p))
|
|
|
|
{
|
2019-03-21 21:23:12 +00:00
|
|
|
const std::string partition_name = DiscIO::NameForPartitionType(*partition_type, true);
|
2018-05-05 14:27:36 +00:00
|
|
|
ExtractPartition(p, folder + QChar(u'/') + QString::fromStdString(partition_name));
|
|
|
|
}
|
|
|
|
}
|
2017-08-25 18:32:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case EntryType::Partition:
|
2018-07-09 08:02:10 +00:00
|
|
|
menu->addAction(tr("Extract Entire Partition..."), this, [this, partition] {
|
2017-08-25 18:32:37 +00:00
|
|
|
auto folder = SelectFolder();
|
|
|
|
if (!folder.isEmpty())
|
|
|
|
ExtractPartition(partition, folder);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case EntryType::File:
|
2018-07-09 08:02:10 +00:00
|
|
|
menu->addAction(tr("Extract File..."), this, [this, partition, path] {
|
2018-06-27 12:04:41 +00:00
|
|
|
auto dest =
|
|
|
|
QFileDialog::getSaveFileName(this, tr("Save File to"), QFileInfo(path).fileName());
|
2017-08-25 18:32:37 +00:00
|
|
|
|
|
|
|
if (!dest.isEmpty())
|
|
|
|
ExtractFile(partition, path, dest);
|
|
|
|
});
|
|
|
|
break;
|
2018-05-05 14:39:51 +00:00
|
|
|
case EntryType::Dir:
|
|
|
|
// Handled above the switch statement
|
|
|
|
break;
|
2017-08-25 18:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
menu->exec(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscIO::Partition FilesystemWidget::GetPartitionFromID(int id)
|
|
|
|
{
|
|
|
|
return id == -1 ? DiscIO::PARTITION_NONE : m_volume->GetPartitions()[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilesystemWidget::ExtractPartition(const DiscIO::Partition& partition, const QString& out)
|
|
|
|
{
|
2018-05-05 14:27:36 +00:00
|
|
|
ExtractDirectory(partition, QStringLiteral(""), out + QStringLiteral("/files"));
|
2017-08-25 18:32:37 +00:00
|
|
|
ExtractSystemData(partition, out);
|
|
|
|
}
|
|
|
|
|
2018-05-05 14:59:21 +00:00
|
|
|
bool FilesystemWidget::ExtractSystemData(const DiscIO::Partition& partition, const QString& out)
|
2017-08-25 18:32:37 +00:00
|
|
|
{
|
2018-05-05 14:59:21 +00:00
|
|
|
return DiscIO::ExportSystemData(*m_volume, partition, out.toStdString());
|
2017-08-25 18:32:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 01:55:16 +00:00
|
|
|
void FilesystemWidget::ExtractDirectory(const DiscIO::Partition& partition, const QString& path,
|
2017-08-25 18:32:37 +00:00
|
|
|
const QString& out)
|
|
|
|
{
|
2017-08-02 17:06:32 +00:00
|
|
|
const DiscIO::FileSystem* filesystem = m_volume->GetFileSystem(partition);
|
2017-09-15 17:44:23 +00:00
|
|
|
if (!filesystem)
|
|
|
|
return;
|
|
|
|
|
2017-08-25 18:32:37 +00:00
|
|
|
std::unique_ptr<DiscIO::FileInfo> info = filesystem->FindFileInfo(path.toStdString());
|
|
|
|
u32 size = info->GetTotalChildren();
|
|
|
|
|
|
|
|
QProgressDialog* dialog = new QProgressDialog(this);
|
2018-06-19 08:30:32 +00:00
|
|
|
dialog->setWindowFlags(dialog->windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
2017-08-25 18:32:37 +00:00
|
|
|
dialog->setMinimum(0);
|
|
|
|
dialog->setMaximum(size);
|
|
|
|
dialog->show();
|
2018-07-02 00:24:09 +00:00
|
|
|
dialog->setWindowTitle(tr("Progress"));
|
2017-08-25 18:32:37 +00:00
|
|
|
|
|
|
|
bool all = path.isEmpty();
|
|
|
|
|
|
|
|
DiscIO::ExportDirectory(
|
2017-08-02 17:06:32 +00:00
|
|
|
*m_volume, partition, *info, true, path.toStdString(), out.toStdString(),
|
2017-08-25 18:32:37 +00:00
|
|
|
[all, dialog](const std::string& current) {
|
|
|
|
dialog->setLabelText(
|
|
|
|
(all ? QObject::tr("Extracting All Files...") : QObject::tr("Extracting Directory..."))
|
|
|
|
.append(QStringLiteral(" %1").arg(QString::fromStdString(current))));
|
|
|
|
dialog->setValue(dialog->value() + 1);
|
|
|
|
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
return dialog->wasCanceled();
|
|
|
|
});
|
|
|
|
|
|
|
|
dialog->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FilesystemWidget::ExtractFile(const DiscIO::Partition& partition, const QString& path,
|
|
|
|
const QString& out)
|
|
|
|
{
|
2017-08-02 17:06:32 +00:00
|
|
|
const DiscIO::FileSystem* filesystem = m_volume->GetFileSystem(partition);
|
2017-09-15 17:44:23 +00:00
|
|
|
if (!filesystem)
|
|
|
|
return;
|
|
|
|
|
2017-08-25 18:32:37 +00:00
|
|
|
bool success = DiscIO::ExportFile(
|
|
|
|
*m_volume, partition, filesystem->FindFileInfo(path.toStdString()).get(), out.toStdString());
|
|
|
|
|
|
|
|
if (success)
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::information(this, tr("Success"), tr("Successfully extracted file."));
|
2017-08-25 18:32:37 +00:00
|
|
|
else
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::critical(this, tr("Error"), tr("Failed to extract file."));
|
2017-08-25 18:32:37 +00:00
|
|
|
}
|