2019-12-31 06:17:17 +00:00
|
|
|
#include "gamelistsettingswidget.h"
|
2020-01-24 04:51:25 +00:00
|
|
|
#include "common/assert.h"
|
2020-11-27 07:58:06 +00:00
|
|
|
#include "common/file_system.h"
|
2020-08-06 09:33:02 +00:00
|
|
|
#include "common/minizip_helpers.h"
|
2020-01-24 04:51:25 +00:00
|
|
|
#include "common/string_util.h"
|
2020-09-01 02:29:22 +00:00
|
|
|
#include "frontend-common/game_list.h"
|
2020-08-01 17:03:25 +00:00
|
|
|
#include "gamelistsearchdirectoriesmodel.h"
|
2019-12-31 06:17:17 +00:00
|
|
|
#include "qthostinterface.h"
|
|
|
|
#include "qtutils.h"
|
|
|
|
#include <QtCore/QAbstractTableModel>
|
2020-01-24 04:51:25 +00:00
|
|
|
#include <QtCore/QDebug>
|
2019-12-31 06:17:17 +00:00
|
|
|
#include <QtCore/QSettings>
|
2020-01-24 04:51:25 +00:00
|
|
|
#include <QtCore/QUrl>
|
|
|
|
#include <QtNetwork/QNetworkAccessManager>
|
|
|
|
#include <QtNetwork/QNetworkReply>
|
|
|
|
#include <QtNetwork/QNetworkRequest>
|
2019-12-31 06:17:17 +00:00
|
|
|
#include <QtWidgets/QFileDialog>
|
|
|
|
#include <QtWidgets/QHeaderView>
|
2020-07-28 16:51:52 +00:00
|
|
|
#include <QtWidgets/QMenu>
|
2019-12-31 06:17:17 +00:00
|
|
|
#include <QtWidgets/QMessageBox>
|
2020-01-24 04:51:25 +00:00
|
|
|
#include <QtWidgets/QProgressDialog>
|
2019-12-31 06:17:17 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-01-24 04:51:25 +00:00
|
|
|
static constexpr char REDUMP_DOWNLOAD_URL[] = "http://redump.org/datfile/psx/serial,version,description";
|
|
|
|
|
2019-12-31 06:17:17 +00:00
|
|
|
GameListSettingsWidget::GameListSettingsWidget(QtHostInterface* host_interface, QWidget* parent /* = nullptr */)
|
|
|
|
: QWidget(parent), m_host_interface(host_interface)
|
|
|
|
{
|
|
|
|
m_ui.setupUi(this);
|
|
|
|
|
2019-12-31 06:40:20 +00:00
|
|
|
m_search_directories_model = new GameListSearchDirectoriesModel(host_interface);
|
2019-12-31 06:17:17 +00:00
|
|
|
m_ui.searchDirectoryList->setModel(m_search_directories_model);
|
|
|
|
m_ui.searchDirectoryList->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
m_ui.searchDirectoryList->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
m_ui.searchDirectoryList->setAlternatingRowColors(true);
|
|
|
|
m_ui.searchDirectoryList->setShowGrid(false);
|
2019-12-31 06:40:20 +00:00
|
|
|
m_ui.searchDirectoryList->horizontalHeader()->setHighlightSections(false);
|
2019-12-31 06:17:17 +00:00
|
|
|
m_ui.searchDirectoryList->verticalHeader()->hide();
|
|
|
|
m_ui.searchDirectoryList->setCurrentIndex({});
|
2020-07-28 16:51:52 +00:00
|
|
|
m_ui.searchDirectoryList->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
|
2019-12-31 06:17:17 +00:00
|
|
|
|
2019-12-31 06:40:20 +00:00
|
|
|
connect(m_ui.searchDirectoryList, &QTableView::clicked, this, &GameListSettingsWidget::onDirectoryListItemClicked);
|
2020-07-28 16:51:52 +00:00
|
|
|
connect(m_ui.searchDirectoryList, &QTableView::customContextMenuRequested, this,
|
|
|
|
&GameListSettingsWidget::onDirectoryListContextMenuRequested);
|
2020-07-17 15:33:36 +00:00
|
|
|
connect(m_ui.addSearchDirectoryButton, &QPushButton::clicked, this,
|
2020-07-17 10:50:02 +00:00
|
|
|
&GameListSettingsWidget::onAddSearchDirectoryButtonClicked);
|
2020-07-17 15:33:36 +00:00
|
|
|
connect(m_ui.removeSearchDirectoryButton, &QPushButton::clicked, this,
|
2020-07-17 10:50:02 +00:00
|
|
|
&GameListSettingsWidget::onRemoveSearchDirectoryButtonClicked);
|
2020-07-17 15:33:36 +00:00
|
|
|
connect(m_ui.rescanAllGames, &QPushButton::clicked, this, &GameListSettingsWidget::onRescanAllGamesClicked);
|
|
|
|
connect(m_ui.scanForNewGames, &QPushButton::clicked, this, &GameListSettingsWidget::onScanForNewGamesClicked);
|
|
|
|
connect(m_ui.updateRedumpDatabase, &QPushButton::clicked, this,
|
2020-07-17 10:50:02 +00:00
|
|
|
&GameListSettingsWidget::onUpdateRedumpDatabaseButtonClicked);
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GameListSettingsWidget::~GameListSettingsWidget() = default;
|
|
|
|
|
|
|
|
void GameListSettingsWidget::resizeEvent(QResizeEvent* event)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
|
|
|
|
QtUtils::ResizeColumnsForTableView(m_ui.searchDirectoryList, {-1, 100});
|
|
|
|
}
|
|
|
|
|
2019-12-31 06:40:20 +00:00
|
|
|
void GameListSettingsWidget::onDirectoryListItemClicked(const QModelIndex& index)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const int row = index.row();
|
|
|
|
const int column = index.column();
|
|
|
|
if (column != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_search_directories_model->setEntryRecursive(row, !m_search_directories_model->isEntryRecursive(row));
|
|
|
|
}
|
|
|
|
|
2020-07-28 16:51:52 +00:00
|
|
|
void GameListSettingsWidget::onDirectoryListContextMenuRequested(const QPoint& point)
|
|
|
|
{
|
|
|
|
QModelIndexList selection = m_ui.searchDirectoryList->selectionModel()->selectedIndexes();
|
|
|
|
if (selection.size() < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const int row = selection[0].row();
|
|
|
|
|
|
|
|
QMenu menu;
|
|
|
|
menu.addAction(tr("Remove"), [this, row]() { m_search_directories_model->removeEntry(row); });
|
|
|
|
menu.addSeparator();
|
|
|
|
menu.addAction(tr("Open Directory..."),
|
|
|
|
[this, row]() { m_search_directories_model->openEntryInExplorer(this, row); });
|
|
|
|
menu.exec(m_ui.searchDirectoryList->mapToGlobal(point));
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:50:53 +00:00
|
|
|
void GameListSettingsWidget::addSearchDirectory(QWidget* parent_widget)
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-07-18 01:43:30 +00:00
|
|
|
QString dir =
|
|
|
|
QDir::toNativeSeparators(QFileDialog::getExistingDirectory(parent_widget, tr("Select Search Directory")));
|
|
|
|
|
2019-12-31 06:17:17 +00:00
|
|
|
if (dir.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMessageBox::StandardButton selection =
|
|
|
|
QMessageBox::question(this, tr("Scan Recursively?"),
|
|
|
|
tr("Would you like to scan the directory \"%1\" recursively?\n\nScanning recursively takes "
|
|
|
|
"more time, but will identify files in subdirectories.")
|
|
|
|
.arg(dir),
|
|
|
|
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
|
|
|
|
if (selection == QMessageBox::Cancel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const bool recursive = (selection == QMessageBox::Yes);
|
|
|
|
m_search_directories_model->addEntry(dir, recursive);
|
2020-01-24 04:50:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 10:50:02 +00:00
|
|
|
void GameListSettingsWidget::onAddSearchDirectoryButtonClicked()
|
2020-01-24 04:50:53 +00:00
|
|
|
{
|
|
|
|
addSearchDirectory(this);
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 10:50:02 +00:00
|
|
|
void GameListSettingsWidget::onRemoveSearchDirectoryButtonClicked()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
|
|
|
QModelIndexList selection = m_ui.searchDirectoryList->selectionModel()->selectedIndexes();
|
|
|
|
if (selection.size() < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const int row = selection[0].row();
|
|
|
|
m_search_directories_model->removeEntry(row);
|
|
|
|
}
|
|
|
|
|
2020-07-17 10:50:02 +00:00
|
|
|
void GameListSettingsWidget::onRescanAllGamesClicked()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-06-10 15:48:54 +00:00
|
|
|
m_host_interface->refreshGameList(true, false);
|
|
|
|
}
|
|
|
|
|
2020-07-17 10:50:02 +00:00
|
|
|
void GameListSettingsWidget::onScanForNewGamesClicked()
|
2020-06-10 15:48:54 +00:00
|
|
|
{
|
|
|
|
m_host_interface->refreshGameList(false, false);
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 10:50:02 +00:00
|
|
|
void GameListSettingsWidget::onUpdateRedumpDatabaseButtonClicked()
|
2019-12-31 06:17:17 +00:00
|
|
|
{
|
2020-01-24 04:51:25 +00:00
|
|
|
if (QMessageBox::question(this, tr("Download database from redump.org?"),
|
|
|
|
tr("Do you wish to download the disc database from redump.org?\n\nThis will download "
|
|
|
|
"approximately 4 megabytes over your current internet connection.")) != QMessageBox::Yes)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-27 14:42:09 +00:00
|
|
|
if (downloadRedumpDatabase(m_host_interface->getUserDirectoryRelativePath("redump.dat")))
|
2020-01-24 04:51:25 +00:00
|
|
|
m_host_interface->refreshGameList(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ExtractRedumpDatabase(const QByteArray& data, const QString& destination_path)
|
|
|
|
{
|
|
|
|
if (data.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
2020-08-06 09:33:02 +00:00
|
|
|
unzFile zf = MinizipHelpers::OpenUnzMemoryFile(data.constData(), data.size());
|
2020-01-24 04:51:25 +00:00
|
|
|
if (!zf)
|
|
|
|
{
|
|
|
|
qCritical() << "unzOpen2_64() failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the first file with a .dat extension (in case there's others)
|
|
|
|
if (unzGoToFirstFile(zf) != UNZ_OK)
|
|
|
|
{
|
|
|
|
qCritical() << "unzGoToFirstFile() failed";
|
|
|
|
unzClose(zf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dat_size = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char zip_filename_buffer[256];
|
|
|
|
unz_file_info64 file_info;
|
|
|
|
if (unzGetCurrentFileInfo64(zf, &file_info, zip_filename_buffer, sizeof(zip_filename_buffer), nullptr, 0, nullptr,
|
|
|
|
0) != UNZ_OK)
|
|
|
|
{
|
|
|
|
qCritical() << "unzGetCurrentFileInfo() failed";
|
|
|
|
unzClose(zf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* extension = std::strrchr(zip_filename_buffer, '.');
|
|
|
|
if (extension && StringUtil::Strcasecmp(extension, ".dat") == 0 && file_info.uncompressed_size > 0)
|
|
|
|
{
|
|
|
|
dat_size = static_cast<int>(file_info.uncompressed_size);
|
|
|
|
qInfo() << "Found redump dat file in zip: " << zip_filename_buffer << "(" << dat_size << " bytes)";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unzGoToNextFile(zf) != UNZ_OK)
|
|
|
|
{
|
|
|
|
qCritical() << "dat file not found in downloaded redump zip";
|
|
|
|
unzClose(zf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unzOpenCurrentFile(zf) != UNZ_OK)
|
|
|
|
{
|
|
|
|
qCritical() << "unzOpenCurrentFile() failed";
|
|
|
|
unzClose(zf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray dat_buffer;
|
|
|
|
dat_buffer.resize(dat_size);
|
|
|
|
if (unzReadCurrentFile(zf, dat_buffer.data(), dat_size) != dat_size)
|
|
|
|
{
|
|
|
|
qCritical() << "unzReadCurrentFile() failed";
|
|
|
|
unzClose(zf);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unzCloseCurrentFile(zf);
|
|
|
|
unzClose(zf);
|
|
|
|
|
|
|
|
QFile dat_output_file(destination_path);
|
|
|
|
if (!dat_output_file.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
|
|
|
{
|
|
|
|
qCritical() << "QFile::open() failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<int>(dat_output_file.write(dat_buffer)) != dat_buffer.size())
|
|
|
|
{
|
|
|
|
qCritical() << "QFile::write() failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dat_output_file.close();
|
|
|
|
qInfo() << "Wrote redump dat to " << destination_path;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GameListSettingsWidget::downloadRedumpDatabase(const QString& download_path)
|
|
|
|
{
|
|
|
|
Assert(!download_path.isEmpty());
|
|
|
|
|
|
|
|
QNetworkAccessManager manager;
|
|
|
|
|
|
|
|
QUrl url(QUrl::fromEncoded(QByteArray(REDUMP_DOWNLOAD_URL, sizeof(REDUMP_DOWNLOAD_URL) - 1)));
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
|
|
|
|
QNetworkReply* reply = manager.get(request);
|
|
|
|
|
|
|
|
QProgressDialog progress(tr("Downloading %1...").arg(REDUMP_DOWNLOAD_URL), tr("Cancel"), 0, 1);
|
|
|
|
progress.setAutoClose(false);
|
|
|
|
|
|
|
|
connect(reply, &QNetworkReply::downloadProgress, [&progress](quint64 received, quint64 total) {
|
|
|
|
progress.setRange(0, static_cast<int>(total));
|
|
|
|
progress.setValue(static_cast<int>(received));
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(&manager, &QNetworkAccessManager::finished, [this, &progress, &download_path](QNetworkReply* reply) {
|
|
|
|
if (reply->error() != QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
QMessageBox::critical(this, tr("Download failed"), reply->errorString());
|
|
|
|
progress.done(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
progress.setRange(0, 100);
|
|
|
|
progress.setValue(100);
|
|
|
|
progress.setLabelText(tr("Extracting..."));
|
|
|
|
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
|
|
|
|
|
|
|
|
const QByteArray data = reply->readAll();
|
|
|
|
if (!ExtractRedumpDatabase(data, download_path))
|
|
|
|
{
|
|
|
|
QMessageBox::critical(this, tr("Extract failed"), tr("Extracting game database failed."));
|
|
|
|
progress.done(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
progress.done(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
const int result = progress.exec();
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
// cancelled
|
|
|
|
reply->abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
return (result == 1);
|
2019-12-31 06:17:17 +00:00
|
|
|
}
|