2022-07-17 04:44:54 +05:30
|
|
|
#include "BlockedModsDialog.h"
|
|
|
|
#include <QDesktopServices>
|
2022-11-01 19:36:36 +05:30
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include "Application.h"
|
|
|
|
#include "ui_BlockedModsDialog.h"
|
2022-07-17 04:44:54 +05:30
|
|
|
|
2022-10-24 16:38:38 +05:30
|
|
|
#include <QDebug>
|
2022-10-25 13:49:19 +05:30
|
|
|
#include <QStandardPaths>
|
2022-07-17 04:44:54 +05:30
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
BlockedModsDialog::BlockedModsDialog(QWidget* parent, const QString& title, const QString& text, QList<BlockedMod>& mods)
|
|
|
|
: QDialog(parent), ui(new Ui::BlockedModsDialog), mods(mods)
|
|
|
|
{
|
2022-07-17 04:44:54 +05:30
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
auto openAllButton = ui->buttonBox->addButton(tr("Open All"), QDialogButtonBox::ActionRole);
|
|
|
|
connect(openAllButton, &QPushButton::clicked, this, &BlockedModsDialog::openAll);
|
|
|
|
|
2022-10-25 13:49:19 +05:30
|
|
|
connect(&watcher, &QFileSystemWatcher::directoryChanged, this, &BlockedModsDialog::directoryChanged);
|
|
|
|
|
|
|
|
hashing_task = shared_qobject_ptr<ConcurrentTask>(new ConcurrentTask(this, "MakeHashesTask", 10));
|
2022-11-01 19:36:36 +05:30
|
|
|
|
2022-10-24 16:38:38 +05:30
|
|
|
qDebug() << "Mods List: " << mods;
|
|
|
|
|
2022-10-25 13:49:19 +05:30
|
|
|
setupWatch();
|
2022-10-26 08:48:14 +05:30
|
|
|
scanPaths();
|
2022-10-25 13:49:19 +05:30
|
|
|
|
2022-07-17 04:44:54 +05:30
|
|
|
this->setWindowTitle(title);
|
|
|
|
ui->label->setText(text);
|
2022-10-31 11:09:12 +05:30
|
|
|
ui->labelModsFound->setText(tr("Please download the missing mods."));
|
2022-10-24 16:38:38 +05:30
|
|
|
update();
|
2022-07-17 04:44:54 +05:30
|
|
|
}
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
BlockedModsDialog::~BlockedModsDialog()
|
|
|
|
{
|
2022-07-17 04:44:54 +05:30
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::openAll()
|
|
|
|
{
|
|
|
|
for (auto& mod : mods) {
|
2022-10-24 16:38:38 +05:30
|
|
|
QDesktopServices::openUrl(mod.websiteUrl);
|
2022-07-17 04:44:54 +05:30
|
|
|
}
|
|
|
|
}
|
2022-10-24 16:38:38 +05:30
|
|
|
|
2022-10-26 08:48:14 +05:30
|
|
|
/// @brief update UI with current status of the blocked mod detection
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::update()
|
|
|
|
{
|
2022-10-24 16:38:38 +05:30
|
|
|
QString text;
|
|
|
|
QString span;
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
for (auto& mod : mods) {
|
2022-10-24 16:38:38 +05:30
|
|
|
if (mod.matched) {
|
|
|
|
// ✔ -> html for HEAVY CHECK MARK : ✔
|
2022-10-31 11:09:12 +05:30
|
|
|
span = QString(tr("<span style=\"color:green\"> ✔ Found at %1 </span>")).arg(mod.localPath);
|
2022-10-24 16:38:38 +05:30
|
|
|
} else {
|
|
|
|
// ✘ -> html for HEAVY BALLOT X : ✘
|
2022-10-31 11:09:12 +05:30
|
|
|
span = QString(tr("<span style=\"color:red\"> ✘ Not Found </span>"));
|
2022-10-24 16:38:38 +05:30
|
|
|
}
|
2022-10-31 11:09:12 +05:30
|
|
|
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 16:38:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ui->textBrowser->setText(text);
|
2022-10-25 23:29:37 +05:30
|
|
|
|
|
|
|
if (allModsMatched()) {
|
2022-10-31 11:09:12 +05:30
|
|
|
ui->labelModsFound->setText(tr("All mods found ✔"));
|
2022-10-25 23:29:37 +05:30
|
|
|
} else {
|
2022-10-31 11:09:12 +05:30
|
|
|
ui->labelModsFound->setText(tr("Please download the missing mods."));
|
2022-10-25 23:29:37 +05:30
|
|
|
}
|
2022-10-24 16:38:38 +05:30
|
|
|
}
|
|
|
|
|
2022-10-26 08:48:14 +05:30
|
|
|
/// @brief Signal fired when a watched direcotry has changed
|
|
|
|
/// @param path the path to the changed directory
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::directoryChanged(QString path)
|
|
|
|
{
|
2022-10-25 13:49:19 +05:30
|
|
|
qDebug() << "Directory changed: " << path;
|
2022-10-26 08:48:14 +05:30
|
|
|
scanPath(path);
|
2022-10-25 13:49:19 +05:30
|
|
|
}
|
|
|
|
|
2022-10-26 08:48:14 +05:30
|
|
|
/// @brief add the user downloads folder and the global mods folder to the filesystem watcher
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::setupWatch()
|
|
|
|
{
|
2022-10-25 13:49:19 +05:30
|
|
|
const QString downloadsFolder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
|
|
|
|
const QString modsFolder = APPLICATION->settings()->get("CentralModsDir").toString();
|
|
|
|
watcher.addPath(downloadsFolder);
|
|
|
|
watcher.addPath(modsFolder);
|
|
|
|
}
|
|
|
|
|
2022-10-26 08:48:14 +05:30
|
|
|
/// @brief scan all watched folder
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::scanPaths()
|
|
|
|
{
|
|
|
|
for (auto& dir : watcher.directories()) {
|
2022-10-26 08:48:14 +05:30
|
|
|
scanPath(dir);
|
2022-10-25 13:49:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
/// @brief Scan the directory at path, skip paths that do not contain a file name
|
2022-10-26 08:48:14 +05:30
|
|
|
/// of a blocked mod we are looking for
|
|
|
|
/// @param path the directory to scan
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::scanPath(QString path)
|
|
|
|
{
|
2022-10-25 13:49:19 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hash_task = Hashing::createBlockedModHasher(file, ModPlatform::Provider::FLAME, "sha1");
|
|
|
|
|
|
|
|
qDebug() << "Creating Hash task for path: " << file;
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
connect(hash_task.get(), &Task::succeeded, [this, hash_task, file] { checkMatchHash(hash_task->getResult(), file); });
|
|
|
|
connect(hash_task.get(), &Task::failed, [file] { qDebug() << "Failed to hash path: " << file; });
|
|
|
|
|
2022-10-25 13:49:19 +05:30
|
|
|
hashing_task->addTask(hash_task);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashing_task->start();
|
|
|
|
}
|
|
|
|
|
2022-10-31 11:09:12 +05:30
|
|
|
/// @brief check if the computed hash for the provided path matches a blocked
|
2022-10-26 08:48:14 +05:30
|
|
|
/// 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 19:36:36 +05:30
|
|
|
void BlockedModsDialog::checkMatchHash(QString hash, QString path)
|
|
|
|
{
|
2022-10-25 13:49:19 +05:30
|
|
|
bool match = false;
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
qDebug() << "Checking for match on hash: " << hash << "| From path:" << path;
|
2022-10-25 13:49:19 +05:30
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
for (auto& mod : mods) {
|
2022-10-25 13:49:19 +05:30
|
|
|
if (mod.matched) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mod.hash.compare(hash, Qt::CaseInsensitive) == 0) {
|
|
|
|
mod.matched = true;
|
|
|
|
mod.localPath = path;
|
|
|
|
match = true;
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
qDebug() << "Hash match found:" << mod.name << hash << "| From path:" << path;
|
2022-10-25 13:49:19 +05:30
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 11:09:12 +05:30
|
|
|
/// @brief Check if the name of the file at path matches the name of a blocked mod we are searching for
|
2022-10-26 08:48:14 +05:30
|
|
|
/// @param path the path to check
|
|
|
|
/// @return boolean: did the path match the name of a blocked mod?
|
2022-11-01 19:36:36 +05:30
|
|
|
bool BlockedModsDialog::checkValidPath(QString path)
|
|
|
|
{
|
2022-10-25 13:49:19 +05:30
|
|
|
QFileInfo file = QFileInfo(path);
|
|
|
|
QString filename = file.fileName();
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
for (auto& mod : mods) {
|
2022-10-25 13:49:19 +05:30
|
|
|
if (mod.name.compare(filename, Qt::CaseInsensitive) == 0) {
|
2022-11-01 19:36:36 +05:30
|
|
|
qDebug() << "Name match found:" << mod.name << "| From path:" << path;
|
2022-10-25 13:49:19 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
bool BlockedModsDialog::allModsMatched()
|
|
|
|
{
|
|
|
|
return std::all_of(mods.begin(), mods.end(), [](auto const& mod) { return mod.matched; });
|
2022-10-25 13:49:19 +05:30
|
|
|
}
|
|
|
|
|
2022-10-26 08:48:14 +05:30
|
|
|
/// qDebug print support for the BlockedMod struct
|
2022-11-01 19:36:36 +05:30
|
|
|
QDebug operator<<(QDebug debug, const BlockedMod& m)
|
|
|
|
{
|
2022-10-24 16:38:38 +05:30
|
|
|
QDebugStateSaver saver(debug);
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
debug.nospace() << "{ name: " << m.name << ", websiteUrl: " << m.websiteUrl << ", hash: " << m.hash << ", matched: " << m.matched
|
|
|
|
<< ", localPath: " << m.localPath << "}";
|
2022-10-24 16:38:38 +05:30
|
|
|
|
|
|
|
return debug;
|
2022-10-26 08:48:14 +05:30
|
|
|
}
|