2022-07-17 00:14:54 +01:00
|
|
|
#include "BlockedModsDialog.h"
|
|
|
|
#include <QDesktopServices>
|
2022-11-01 14:06:36 +00:00
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include "Application.h"
|
|
|
|
#include "ui_BlockedModsDialog.h"
|
2022-07-17 00:14:54 +01:00
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
|
2022-10-24 12:08:38 +01:00
|
|
|
#include <QDebug>
|
2022-10-25 09:19:19 +01:00
|
|
|
#include <QStandardPaths>
|
2022-11-11 21:10:45 +00:00
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileInfo>
|
2022-07-17 00:14:54 +01:00
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
BlockedModsDialog::BlockedModsDialog(QWidget* parent, const QString& title, const QString& text, QList<BlockedMod>& mods)
|
|
|
|
: QDialog(parent), ui(new Ui::BlockedModsDialog), mods(mods)
|
|
|
|
{
|
2022-11-11 21:10:45 +00:00
|
|
|
hashing_task = shared_qobject_ptr<ConcurrentTask>(new ConcurrentTask(this, "MakeHashesTask", 10));
|
|
|
|
|
2022-07-17 00:14:54 +01:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
auto openAllButton = ui->buttonBox->addButton(tr("Open All"), QDialogButtonBox::ActionRole);
|
|
|
|
connect(openAllButton, &QPushButton::clicked, this, &BlockedModsDialog::openAll);
|
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
auto downloadFolderButton = ui->buttonBox->addButton(tr("Add Download Folder"), QDialogButtonBox::ActionRole);
|
|
|
|
connect(downloadFolderButton, &QPushButton::clicked, this, &BlockedModsDialog::addDownloadFolder);
|
|
|
|
|
2022-10-25 09:19:19 +01:00
|
|
|
connect(&watcher, &QFileSystemWatcher::directoryChanged, this, &BlockedModsDialog::directoryChanged);
|
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
|
2022-10-24 12:08:38 +01:00
|
|
|
qDebug() << "Mods List: " << mods;
|
|
|
|
|
2022-10-25 09:19:19 +01:00
|
|
|
setupWatch();
|
2022-10-26 04:18:14 +01:00
|
|
|
scanPaths();
|
2022-10-25 09:19:19 +01:00
|
|
|
|
2022-07-17 00:14:54 +01:00
|
|
|
this->setWindowTitle(title);
|
|
|
|
ui->label->setText(text);
|
2022-10-31 05:39:12 +00:00
|
|
|
ui->labelModsFound->setText(tr("Please download the missing mods."));
|
2022-11-11 21:10:45 +00:00
|
|
|
|
|
|
|
setAcceptDrops(true);
|
|
|
|
|
2022-10-24 12:08:38 +01:00
|
|
|
update();
|
2022-07-17 00:14:54 +01:00
|
|
|
}
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
BlockedModsDialog::~BlockedModsDialog()
|
|
|
|
{
|
2022-07-17 00:14:54 +01:00
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
void BlockedModsDialog::dragEnterEvent(QDragEnterEvent *e) {
|
|
|
|
if (e->mimeData()->hasUrls()) {
|
|
|
|
e->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockedModsDialog::dropEvent(QDropEvent *e)
|
|
|
|
{
|
|
|
|
foreach (const QUrl &url, e->mimeData()->urls()) {
|
|
|
|
QString file = url.toLocalFile();
|
|
|
|
qDebug() << "Dropped file:" << file;
|
|
|
|
addHashTask(file);
|
|
|
|
}
|
|
|
|
hashing_task->start();
|
|
|
|
}
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
void BlockedModsDialog::openAll()
|
|
|
|
{
|
|
|
|
for (auto& mod : mods) {
|
2022-10-24 12:08:38 +01:00
|
|
|
QDesktopServices::openUrl(mod.websiteUrl);
|
2022-07-17 00:14:54 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-24 12:08:38 +01:00
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
void BlockedModsDialog::addDownloadFolder() {
|
|
|
|
QString dir = QFileDialog::getExistingDirectory(
|
|
|
|
this,
|
|
|
|
tr("Select directory where you downloaded the mods"),
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation),
|
|
|
|
QFileDialog::ShowDirsOnly);
|
|
|
|
watcher.addPath(dir);
|
|
|
|
scanPath(dir);
|
|
|
|
}
|
|
|
|
|
2022-10-26 04:18:14 +01:00
|
|
|
/// @brief update UI with current status of the blocked mod detection
|
2022-11-01 14:06:36 +00:00
|
|
|
void BlockedModsDialog::update()
|
|
|
|
{
|
2022-10-24 12:08:38 +01:00
|
|
|
QString text;
|
|
|
|
QString span;
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
for (auto& mod : mods) {
|
2022-10-24 12:08:38 +01:00
|
|
|
if (mod.matched) {
|
|
|
|
// ✔ -> html for HEAVY CHECK MARK : ✔
|
2022-10-31 05:39:12 +00:00
|
|
|
span = QString(tr("<span style=\"color:green\"> ✔ Found at %1 </span>")).arg(mod.localPath);
|
2022-10-24 12:08:38 +01:00
|
|
|
} else {
|
|
|
|
// ✘ -> html for HEAVY BALLOT X : ✘
|
2022-10-31 05:39:12 +00:00
|
|
|
span = QString(tr("<span style=\"color:red\"> ✘ Not Found </span>"));
|
2022-10-24 12:08:38 +01:00
|
|
|
}
|
2022-10-31 05:39:12 +00:00
|
|
|
text += QString(tr("%1: <a href='%2'>%2</a> <p>Hash: %3 %4</p> <br/>")).arg(mod.name, mod.websiteUrl, mod.hash, span);
|
2022-10-24 12:08:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ui->textBrowser->setText(text);
|
2022-10-25 18:59:37 +01:00
|
|
|
|
|
|
|
if (allModsMatched()) {
|
2022-10-31 05:39:12 +00:00
|
|
|
ui->labelModsFound->setText(tr("All mods found ✔"));
|
2022-10-25 18:59:37 +01:00
|
|
|
} else {
|
2022-10-31 05:39:12 +00:00
|
|
|
ui->labelModsFound->setText(tr("Please download the missing mods."));
|
2022-10-25 18:59:37 +01:00
|
|
|
}
|
2022-10-24 12:08:38 +01:00
|
|
|
}
|
|
|
|
|
2022-10-26 04:18:14 +01:00
|
|
|
/// @brief Signal fired when a watched direcotry has changed
|
|
|
|
/// @param path the path to the changed directory
|
2022-11-01 14:06:36 +00:00
|
|
|
void BlockedModsDialog::directoryChanged(QString path)
|
|
|
|
{
|
2022-10-25 09:19:19 +01:00
|
|
|
qDebug() << "Directory changed: " << path;
|
2022-11-11 21:10:45 +00:00
|
|
|
validateMatchedMods();
|
2022-10-26 04:18:14 +01:00
|
|
|
scanPath(path);
|
2022-10-25 09:19:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-26 04:18:14 +01:00
|
|
|
/// @brief add the user downloads folder and the global mods folder to the filesystem watcher
|
2022-11-01 14:06:36 +00:00
|
|
|
void BlockedModsDialog::setupWatch()
|
|
|
|
{
|
2022-10-25 09:19:19 +01:00
|
|
|
const QString downloadsFolder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
|
|
const QString modsFolder = APPLICATION->settings()->get("CentralModsDir").toString();
|
|
|
|
watcher.addPath(downloadsFolder);
|
|
|
|
watcher.addPath(modsFolder);
|
|
|
|
}
|
|
|
|
|
2022-10-26 04:18:14 +01:00
|
|
|
/// @brief scan all watched folder
|
2022-11-01 14:06:36 +00:00
|
|
|
void BlockedModsDialog::scanPaths()
|
|
|
|
{
|
|
|
|
for (auto& dir : watcher.directories()) {
|
2022-10-26 04:18:14 +01:00
|
|
|
scanPath(dir);
|
2022-10-25 09:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
/// @brief Scan the directory at path, skip paths that do not contain a file name
|
2022-10-26 04:18:14 +01:00
|
|
|
/// of a blocked mod we are looking for
|
|
|
|
/// @param path the directory to scan
|
2022-11-01 14:06:36 +00:00
|
|
|
void BlockedModsDialog::scanPath(QString path)
|
|
|
|
{
|
2022-10-25 09:19:19 +01:00
|
|
|
QDir scan_dir(path);
|
|
|
|
QDirIterator scan_it(path, QDir::Filter::Files | QDir::Filter::Hidden, QDirIterator::NoIteratorFlags);
|
|
|
|
while (scan_it.hasNext()) {
|
|
|
|
QString file = scan_it.next();
|
|
|
|
|
|
|
|
if (!checkValidPath(file)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
addHashTask(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashing_task->start();
|
|
|
|
}
|
2022-10-25 09:19:19 +01:00
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
/// @brief add a hashing task for the file located at path and connect it to check that hash against
|
|
|
|
/// our blocked mods list
|
|
|
|
/// @param path the path to the local file being hashed
|
|
|
|
void BlockedModsDialog::addHashTask(QString path) {
|
|
|
|
auto hash_task = Hashing::createBlockedModHasher(path, ModPlatform::Provider::FLAME, "sha1");
|
2022-10-25 09:19:19 +01:00
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
qDebug() << "Creating Hash task for path: " << path;
|
2022-11-01 14:06:36 +00:00
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
connect(hash_task.get(), &Task::succeeded, [this, hash_task, path] { checkMatchHash(hash_task->getResult(), path); });
|
|
|
|
connect(hash_task.get(), &Task::failed, [path] { qDebug() << "Failed to hash path: " << path; });
|
2022-10-25 09:19:19 +01:00
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
hashing_task->addTask(hash_task);
|
2022-10-25 09:19:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-31 05:39:12 +00:00
|
|
|
/// @brief check if the computed hash for the provided path matches a blocked
|
2022-10-26 04:18:14 +01:00
|
|
|
/// mod we are looking for
|
|
|
|
/// @param hash the computed hash for the provided path
|
|
|
|
/// @param path the path to the local file being compared
|
2022-11-01 14:06:36 +00:00
|
|
|
void BlockedModsDialog::checkMatchHash(QString hash, QString path)
|
|
|
|
{
|
2022-10-25 09:19:19 +01:00
|
|
|
bool match = false;
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
qDebug() << "Checking for match on hash: " << hash << "| From path:" << path;
|
2022-10-25 09:19:19 +01:00
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
for (auto& mod : mods) {
|
2022-10-25 09:19:19 +01:00
|
|
|
if (mod.matched) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mod.hash.compare(hash, Qt::CaseInsensitive) == 0) {
|
|
|
|
mod.matched = true;
|
|
|
|
mod.localPath = path;
|
|
|
|
match = true;
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
qDebug() << "Hash match found:" << mod.name << hash << "| From path:" << path;
|
2022-10-25 09:19:19 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 05:39:12 +00:00
|
|
|
/// @brief Check if the name of the file at path matches the name of a blocked mod we are searching for
|
2022-10-26 04:18:14 +01:00
|
|
|
/// @param path the path to check
|
|
|
|
/// @return boolean: did the path match the name of a blocked mod?
|
2022-11-01 14:06:36 +00:00
|
|
|
bool BlockedModsDialog::checkValidPath(QString path)
|
|
|
|
{
|
2022-10-25 09:19:19 +01:00
|
|
|
QFileInfo file = QFileInfo(path);
|
|
|
|
QString filename = file.fileName();
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
for (auto& mod : mods) {
|
2022-10-25 09:19:19 +01:00
|
|
|
if (mod.name.compare(filename, Qt::CaseInsensitive) == 0) {
|
2022-11-01 14:06:36 +00:00
|
|
|
qDebug() << "Name match found:" << mod.name << "| From path:" << path;
|
2022-10-25 09:19:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
bool BlockedModsDialog::allModsMatched()
|
|
|
|
{
|
|
|
|
return std::all_of(mods.begin(), mods.end(), [](auto const& mod) { return mod.matched; });
|
2022-10-25 09:19:19 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 21:10:45 +00:00
|
|
|
/// @brief ensure matched file paths still exist
|
|
|
|
void BlockedModsDialog::validateMatchedMods() {
|
|
|
|
bool changed = false;
|
|
|
|
for (auto& mod : mods) {
|
|
|
|
if (mod.matched) {
|
|
|
|
QFileInfo file = QFileInfo(mod.localPath);
|
|
|
|
if (!file.exists() || !file.isFile()) {
|
|
|
|
mod.localPath = "";
|
|
|
|
mod.matched = false;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 04:18:14 +01:00
|
|
|
/// qDebug print support for the BlockedMod struct
|
2022-11-01 14:06:36 +00:00
|
|
|
QDebug operator<<(QDebug debug, const BlockedMod& m)
|
|
|
|
{
|
2022-10-24 12:08:38 +01:00
|
|
|
QDebugStateSaver saver(debug);
|
|
|
|
|
2022-11-01 14:06:36 +00:00
|
|
|
debug.nospace() << "{ name: " << m.name << ", websiteUrl: " << m.websiteUrl << ", hash: " << m.hash << ", matched: " << m.matched
|
|
|
|
<< ", localPath: " << m.localPath << "}";
|
2022-10-24 12:08:38 +01:00
|
|
|
|
|
|
|
return debug;
|
2022-10-26 04:18:14 +01:00
|
|
|
}
|