| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/FilesystemWidget.h" |
| 5 | |
| 6 | #include <QApplication> |
| 7 | #include <QCoreApplication> |
| 8 | #include <QFileInfo> |
| 9 | #include <QGridLayout> |
| 10 | #include <QHeaderView> |
| 11 | #include <QMenu> |
| 12 | #include <QStandardItemModel> |
| 13 | #include <QStyleFactory> |
| 14 | #include <QTreeView> |
| 15 | |
| 16 | #include <future> |
| 17 | |
| 18 | #include "Common/StringUtil.h" |
| 19 | |
| 20 | #include "DiscIO/DiscExtractor.h" |
| 21 | #include "DiscIO/DiscUtils.h" |
| 22 | #include "DiscIO/Filesystem.h" |
| 23 | #include "DiscIO/Volume.h" |
| 24 | |
| 25 | #include "DolphinQt/QtUtils/DolphinFileDialog.h" |
| 26 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 27 | #include "DolphinQt/QtUtils/ParallelProgressDialog.h" |
| 28 | #include "DolphinQt/Resources.h" |
| 29 | |
| 30 | #include "UICommon/UICommon.h" |
| 31 | |
| 32 | constexpr int ENTRY_PARTITION = Qt::UserRole; |
| 33 | constexpr int ENTRY_NAME = Qt::UserRole + 1; |
| 34 | constexpr int ENTRY_TYPE = Qt::UserRole + 2; |
| 35 | |
| 36 | enum class EntryType |
| 37 | { |
| 38 | Disc = -2, |
| 39 | Partition = -1, |
| 40 | File = 0, |
| 41 | Dir = 1 |
| 42 | }; |
| 43 | Q_DECLARE_METATYPE(EntryType); |
| 44 | |
| 45 | FilesystemWidget::FilesystemWidget(std::shared_ptr<DiscIO::Volume> volume) |
| 46 | : m_volume(std::move(volume)) |
| 47 | { |
| 48 | CreateWidgets(); |
| 49 | ConnectWidgets(); |
| 50 | PopulateView(); |
| 51 | } |
| 52 | |
| 53 | FilesystemWidget::~FilesystemWidget() = default; |
| 54 | |
| 55 | void FilesystemWidget::CreateWidgets() |
| 56 | { |
| 57 | // Note: At least with KDE Plasma, |
| 58 | // QTreeView takes on an ugly square-corner style when alone in a QVBoxLayout. |
| 59 | // QGridLayout does not produce this problem. |
| 60 | auto* const layout = new QGridLayout{this}; |
| 61 | |
| 62 | m_tree_model = new QStandardItemModel(0, 2); |
| 63 | m_tree_model->setHorizontalHeaderLabels({tr("Name"), tr("Size")}); |
| 64 | |
| 65 | m_tree_view = new QTreeView(this); |
| 66 | m_tree_view->setModel(m_tree_model); |
| 67 | m_tree_view->setContextMenuPolicy(Qt::CustomContextMenu); |
| 68 | m_tree_model->setParent(m_tree_view); |
| 69 | |
| 70 | auto* header = m_tree_view->header(); |
| 71 | |
| 72 | header->setSectionResizeMode(0, QHeaderView::Stretch); |
| 73 | header->setSectionResizeMode(1, QHeaderView::ResizeToContents); |
| 74 | header->setStretchLastSection(false); |
| 75 | header->setDefaultAlignment(Qt::AlignCenter); |
| 76 | |
| 77 | // Windows: Set style to (old) windows, which draws branch lines |
| 78 | #ifdef _WIN32 |
| 79 | if (QApplication::style()->objectName() == QStringLiteral("windowsvista")) |
| 80 | m_tree_view->setStyle(QStyleFactory::create(QStringLiteral("windows"))); |
| 81 | #endif |
| 82 | |
| 83 | layout->addWidget(m_tree_view); |
| 84 | } |
| 85 | |
| 86 | void FilesystemWidget::ConnectWidgets() |
| 87 | { |
| 88 | connect(m_tree_view, &QTreeView::customContextMenuRequested, this, |
| 89 | &FilesystemWidget::ShowContextMenu); |
| 90 | } |
| 91 | |
| 92 | void FilesystemWidget::PopulateView() |
| 93 | { |
| 94 | // Cache these two icons, the tree will use them a lot. |
| 95 | m_folder_icon = Resources::GetResourceIcon("isoproperties_folder"); |
| 96 | m_file_icon = Resources::GetResourceIcon("isoproperties_file"); |
| 97 | |
| 98 | auto* disc = new QStandardItem(tr("Disc")); |
| 99 | disc->setEditable(false); |
| 100 | disc->setIcon(Resources::GetResourceIcon("isoproperties_disc")); |
| 101 | disc->setData(QVariant::fromValue(EntryType::Disc), ENTRY_TYPE); |
| 102 | m_tree_model->appendRow(disc); |
| 103 | m_tree_view->expand(disc->index()); |
| 104 | |
| 105 | const auto& partitions = m_volume->GetPartitions(); |
| 106 | |
| 107 | for (size_t i = 0; i < partitions.size(); i++) |
| 108 | { |
| 109 | auto* item = new QStandardItem; |
| 110 | item->setEditable(false); |
| 111 | |
| 112 | item->setIcon(Resources::GetResourceIcon("isoproperties_disc")); |
| 113 | item->setData(static_cast<qlonglong>(i), ENTRY_PARTITION); |
| 114 | item->setData(QVariant::fromValue(EntryType::Partition), ENTRY_TYPE); |
| 115 | |
| 116 | PopulateDirectory(static_cast<int>(i), item, partitions[i]); |
| 117 | |
| 118 | disc->appendRow(item); |
| 119 | |
| 120 | if (m_volume->GetGamePartition() == partitions[i]) |
| 121 | m_tree_view->expand(item->index()); |
| 122 | } |
| 123 | |
| 124 | if (partitions.empty()) |
| 125 | PopulateDirectory(-1, disc, DiscIO::PARTITION_NONE); |
| 126 | } |
| 127 | |
| 128 | void FilesystemWidget::PopulateDirectory(int partition_id, QStandardItem* root, |
| 129 | const DiscIO::Partition& partition) |
| 130 | { |
| 131 | auto partition_type = m_volume->GetPartitionType(partition); |
| 132 | auto game_id = m_volume->GetGameID(partition); |
| 133 | auto title_id = m_volume->GetTitleID(partition); |
| 134 | |
| 135 | QString text = root->text(); |
| 136 | |
| 137 | if (!text.isEmpty()) |
| 138 | text += QStringLiteral(" - "); |
| 139 | |
| 140 | if (partition_type) |
| 141 | { |
| 142 | QString partition_type_str; |
| 143 | switch (partition_type.value()) |
| 144 | { |
| 145 | case DiscIO::PARTITION_DATA: |
| 146 | partition_type_str = tr("Data Partition (%1)").arg(partition_type.value()); |
| 147 | break; |
| 148 | case DiscIO::PARTITION_UPDATE: |
| 149 | partition_type_str = tr("Update Partition (%1)").arg(partition_type.value()); |
| 150 | break; |
| 151 | case DiscIO::PARTITION_CHANNEL: |
| 152 | partition_type_str = tr("Channel Partition (%1)").arg(partition_type.value()); |
| 153 | break; |
| 154 | case DiscIO::PARTITION_INSTALL: |
| 155 | partition_type_str = tr("Install Partition (%1)").arg(partition_type.value()); |
| 156 | break; |
| 157 | default: |
| 158 | partition_type_str = |
| 159 | tr("Other Partition (%1)").arg(partition_type.value(), 8, 16, QLatin1Char('0')); |
| 160 | break; |
| 161 | } |
| 162 | text += partition_type_str + QStringLiteral(" - "); |
| 163 | } |
| 164 | |
| 165 | text += QString::fromStdString(game_id); |
| 166 | |
| 167 | if (title_id) |
| 168 | { |
| 169 | text += QStringLiteral(" - %1 (").arg(title_id.value(), 16, 16, QLatin1Char('0')); |
| 170 | for (u32 i = 0; i < 4; i++) |
| 171 | { |
| 172 | char c = static_cast<char>(title_id.value() >> 8 * (3 - i)); |
| 173 | if (Common::IsPrintableCharacter(c)) |
| 174 | text += QLatin1Char(c); |
| 175 | else |
| 176 | text += QLatin1Char('.'); |
| 177 | } |
| 178 | text += QLatin1Char(')'); |
| 179 | } |
| 180 | |
| 181 | root->setText(text); |
| 182 | |
| 183 | const DiscIO::FileSystem* const file_system = m_volume->GetFileSystem(partition); |
| 184 | if (file_system) |
| 185 | PopulateDirectory(partition_id, root, file_system->GetRoot()); |
| 186 | } |
| 187 | |
| 188 | void FilesystemWidget::PopulateDirectory(int partition_id, QStandardItem* root, |
| 189 | const DiscIO::FileInfo& directory) |
| 190 | { |
| 191 | for (const auto& info : directory) |
| 192 | { |
| 193 | auto* item = new QStandardItem(QString::fromStdString(info.GetName())); |
| 194 | item->setEditable(false); |
| 195 | item->setIcon(info.IsDirectory() ? m_folder_icon : m_file_icon); |
| 196 | |
| 197 | if (info.IsDirectory()) |
| 198 | PopulateDirectory(partition_id, item, info); |
| 199 | |
| 200 | item->setData(partition_id, ENTRY_PARTITION); |
| 201 | item->setData(QString::fromStdString(info.GetPath()), ENTRY_NAME); |
| 202 | item->setData(QVariant::fromValue(info.IsDirectory() ? EntryType::Dir : EntryType::File), |
| 203 | ENTRY_TYPE); |
| 204 | |
| 205 | const auto size = info.GetTotalSize(); |
| 206 | |
| 207 | auto* size_item = new QStandardItem(QString::fromStdString(UICommon::FormatSize(size))); |
| 208 | size_item->setTextAlignment(Qt::AlignRight); |
| 209 | size_item->setEditable(false); |
| 210 | |
| 211 | root->appendRow({item, size_item}); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | QString FilesystemWidget::SelectFolder() |
| 216 | { |
| 217 | return DolphinFileDialog::getExistingDirectory(this, QObject::tr("Choose Folder to Extract To")); |
| 218 | } |
| 219 | |
| 220 | void FilesystemWidget::ShowContextMenu(const QPoint&) |
| 221 | { |
| 222 | auto* selection = m_tree_view->selectionModel(); |
| 223 | if (!selection->hasSelection()) |
| 224 | return; |
| 225 | |
| 226 | auto* item = m_tree_model->itemFromIndex(selection->selectedIndexes()[0]); |
| 227 | |
| 228 | QMenu* menu = new QMenu(this); |
| 229 | menu->setAttribute(Qt::WA_DeleteOnClose, true); |
| 230 | |
| 231 | EntryType type = item->data(ENTRY_TYPE).value<EntryType>(); |
| 232 | |
| 233 | DiscIO::Partition partition = type == EntryType::Disc ? |
| 234 | DiscIO::PARTITION_NONE : |
| 235 | GetPartitionFromID(item->data(ENTRY_PARTITION).toInt()); |
| 236 | QString path = item->data(ENTRY_NAME).toString(); |
| 237 | |
| 238 | const bool is_filesystem_root = (type == EntryType::Disc && m_volume->GetPartitions().empty()) || |
| 239 | type == EntryType::Partition; |
| 240 | |
| 241 | if (type == EntryType::Dir || is_filesystem_root) |
| 242 | { |
| 243 | menu->addAction(tr("Extract Files..."), this, [this, partition, path] { |
| 244 | auto folder = SelectFolder(); |
| 245 | |
| 246 | if (!folder.isEmpty()) |
| 247 | ExtractDirectory(partition, path, folder); |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | if (is_filesystem_root) |
| 252 | { |
| 253 | menu->addAction(tr("Extract System Data..."), this, [this, partition] { |
| 254 | auto folder = SelectFolder(); |
| 255 | |
| 256 | if (folder.isEmpty()) |
| 257 | return; |
| 258 | |
| 259 | if (ExtractSystemData(partition, folder)) |
| 260 | ModalMessageBox::information(this, tr("Success"), |
| 261 | tr("Successfully extracted system data.")); |
| 262 | else |
| 263 | ModalMessageBox::critical(this, tr("Error"), tr("Failed to extract system data.")); |
| 264 | }); |
| 265 | } |
| 266 | |
| 267 | switch (type) |
| 268 | { |
| 269 | case EntryType::Disc: |
| 270 | menu->addAction(tr("Extract Entire Disc..."), this, [this] { |
| 271 | auto folder = SelectFolder(); |
| 272 | |
| 273 | if (folder.isEmpty()) |
| 274 | return; |
| 275 | |
| 276 | if (m_volume->GetPartitions().empty()) |
| 277 | { |
| 278 | ExtractPartition(DiscIO::PARTITION_NONE, folder); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | for (DiscIO::Partition& p : m_volume->GetPartitions()) |
| 283 | { |
| 284 | if (const std::optional<u32> partition_type = m_volume->GetPartitionType(p)) |
| 285 | { |
| 286 | const std::string partition_name = DiscIO::NameForPartitionType(*partition_type, true); |
| 287 | ExtractPartition(p, folder + QChar(u'/') + QString::fromStdString(partition_name)); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | }); |
| 292 | break; |
| 293 | case EntryType::Partition: |
| 294 | menu->addAction(tr("Extract Entire Partition..."), this, [this, partition] { |
| 295 | auto folder = SelectFolder(); |
| 296 | if (!folder.isEmpty()) |
| 297 | ExtractPartition(partition, folder); |
| 298 | }); |
| 299 | break; |
| 300 | case EntryType::File: |
| 301 | menu->addAction(tr("Extract File..."), this, [this, partition, path] { |
| 302 | auto dest = |
| 303 | DolphinFileDialog::getSaveFileName(this, tr("Save File To"), QFileInfo(path).fileName()); |
| 304 | |
| 305 | if (!dest.isEmpty()) |
| 306 | ExtractFile(partition, path, dest); |
| 307 | }); |
| 308 | break; |
| 309 | case EntryType::Dir: |
| 310 | // Handled above the switch statement |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | menu->exec(QCursor::pos()); |
| 315 | } |
| 316 | |
| 317 | DiscIO::Partition FilesystemWidget::GetPartitionFromID(int id) |
| 318 | { |
| 319 | return id == -1 ? DiscIO::PARTITION_NONE : m_volume->GetPartitions()[id]; |
| 320 | } |
| 321 | |
| 322 | void FilesystemWidget::ExtractPartition(const DiscIO::Partition& partition, const QString& out) |
| 323 | { |
| 324 | ExtractDirectory(partition, QString{}, out + QStringLiteral("/files")); |
| 325 | ExtractSystemData(partition, out); |
| 326 | } |
| 327 | |
| 328 | bool FilesystemWidget::ExtractSystemData(const DiscIO::Partition& partition, const QString& out) |
| 329 | { |
| 330 | return DiscIO::ExportSystemData(*m_volume, partition, out.toStdString()); |
| 331 | } |
| 332 | |
| 333 | void FilesystemWidget::ExtractDirectory(const DiscIO::Partition& partition, const QString& path, |
| 334 | const QString& out) |
| 335 | { |
| 336 | const DiscIO::FileSystem* filesystem = m_volume->GetFileSystem(partition); |
| 337 | if (!filesystem) |
| 338 | return; |
| 339 | |
| 340 | std::unique_ptr<DiscIO::FileInfo> info = filesystem->FindFileInfo(path.toStdString()); |
| 341 | u32 size = info->GetTotalChildren(); |
| 342 | |
| 343 | ParallelProgressDialog dialog(this); |
| 344 | dialog.GetRaw()->setMinimum(0); |
| 345 | dialog.GetRaw()->setMaximum(size); |
| 346 | dialog.GetRaw()->setWindowTitle(tr("Progress")); |
| 347 | |
| 348 | const bool all = path.isEmpty(); |
| 349 | |
| 350 | std::future<void> future = std::async(std::launch::async, [&] { |
| 351 | int progress = 0; |
| 352 | |
| 353 | DiscIO::ExportDirectory( |
| 354 | *m_volume, partition, *info, true, path.toStdString(), out.toStdString(), |
| 355 | [all, &dialog, &progress](const std::string& current) { |
| 356 | dialog.SetLabelText( |
| 357 | (all ? QObject::tr("Extracting All Files...") : |
| 358 | QObject::tr("Extracting Directory...")) |
| 359 | .append(QStringLiteral(" %1").arg(QString::fromStdString(current)))); |
| 360 | dialog.SetValue(++progress); |
| 361 | |
| 362 | QCoreApplication::processEvents(); |
| 363 | return dialog.WasCanceled(); |
| 364 | }); |
| 365 | |
| 366 | dialog.Reset(); |
| 367 | }); |
| 368 | |
| 369 | dialog.GetRaw()->exec(); |
| 370 | future.get(); |
| 371 | } |
| 372 | |
| 373 | void FilesystemWidget::ExtractFile(const DiscIO::Partition& partition, const QString& path, |
| 374 | const QString& out) |
| 375 | { |
| 376 | const DiscIO::FileSystem* filesystem = m_volume->GetFileSystem(partition); |
| 377 | if (!filesystem) |
| 378 | return; |
| 379 | |
| 380 | bool success = DiscIO::ExportFile( |
| 381 | *m_volume, partition, filesystem->FindFileInfo(path.toStdString()).get(), out.toStdString()); |
| 382 | |
| 383 | if (success) |
| 384 | ModalMessageBox::information(this, tr("Success"), tr("Successfully extracted file.")); |
| 385 | else |
| 386 | ModalMessageBox::critical(this, tr("Error"), tr("Failed to extract file.")); |
| 387 | } |