2022-07-17 04:44:54 +05:30
|
|
|
#include "BlockedModsDialog.h"
|
2022-11-12 04:15:40 +05:30
|
|
|
#include <qfileinfo.h>
|
2022-07-17 04:44:54 +05:30
|
|
|
#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-11-12 02:40:45 +05:30
|
|
|
#include <QDragEnterEvent>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileInfo>
|
2022-11-12 04:37:57 +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)
|
2022-11-12 04:15:40 +05:30
|
|
|
: QDialog(parent), ui(new Ui::BlockedModsDialog), m_mods(mods)
|
2022-11-01 19:36:36 +05:30
|
|
|
{
|
2022-11-12 04:15:40 +05:30
|
|
|
m_hashing_task = shared_qobject_ptr<ConcurrentTask>(new ConcurrentTask(this, "MakeHashesTask", 10));
|
|
|
|
connect(m_hashing_task.get(), &Task::finished, this, &BlockedModsDialog::hashTaskFinished);
|
2022-11-12 04:37:57 +05:30
|
|
|
|
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-11-12 02:40:45 +05:30
|
|
|
auto downloadFolderButton = ui->buttonBox->addButton(tr("Add Download Folder"), QDialogButtonBox::ActionRole);
|
|
|
|
connect(downloadFolderButton, &QPushButton::clicked, this, &BlockedModsDialog::addDownloadFolder);
|
|
|
|
|
2022-11-12 04:15:40 +05:30
|
|
|
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &BlockedModsDialog::directoryChanged);
|
2022-10-25 13:49:19 +05:30
|
|
|
|
2022-11-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] Mods List: " << mods;
|
2022-10-24 16:38:38 +05:30
|
|
|
|
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-11-12 02:40:45 +05:30
|
|
|
|
|
|
|
setAcceptDrops(true);
|
|
|
|
|
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-12 04:37:57 +05:30
|
|
|
void BlockedModsDialog::dragEnterEvent(QDragEnterEvent* e)
|
|
|
|
{
|
2022-11-12 02:40:45 +05:30
|
|
|
if (e->mimeData()->hasUrls()) {
|
|
|
|
e->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 04:37:57 +05:30
|
|
|
void BlockedModsDialog::dropEvent(QDropEvent* e)
|
2022-11-12 02:40:45 +05:30
|
|
|
{
|
2022-11-12 04:37:57 +05:30
|
|
|
foreach (const QUrl& url, e->mimeData()->urls()) {
|
2022-11-12 04:15:40 +05:30
|
|
|
QString filePath = url.toLocalFile();
|
|
|
|
qDebug() << "[Blocked Mods Dialog] Dropped file:" << filePath;
|
|
|
|
addHashTask(filePath);
|
|
|
|
|
|
|
|
// watch for changes
|
|
|
|
QFileInfo file = QFileInfo(filePath);
|
|
|
|
QString path = file.dir().absolutePath();
|
|
|
|
qDebug() << "[Blocked Mods Dialog] Adding watch path:" << path;
|
|
|
|
m_watcher.addPath(path);
|
2022-11-12 02:40:45 +05:30
|
|
|
}
|
2022-11-12 04:15:40 +05:30
|
|
|
scanPaths();
|
|
|
|
update();
|
2022-11-12 02:40:45 +05:30
|
|
|
}
|
|
|
|
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::openAll()
|
|
|
|
{
|
2022-11-12 04:15:40 +05:30
|
|
|
for (auto& mod : m_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-11-12 04:37:57 +05:30
|
|
|
void BlockedModsDialog::addDownloadFolder()
|
|
|
|
{
|
|
|
|
QString dir =
|
|
|
|
QFileDialog::getExistingDirectory(this, tr("Select directory where you downloaded the mods"),
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation), QFileDialog::ShowDirsOnly);
|
2022-11-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] Adding watch path:" << dir;
|
|
|
|
m_watcher.addPath(dir);
|
|
|
|
scanPath(dir, true);
|
|
|
|
update();
|
2022-11-12 02:40:45 +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-12 04:15:40 +05:30
|
|
|
for (auto& mod : m_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-11-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] Directory changed: " << path;
|
2022-11-12 02:40:45 +05:30
|
|
|
validateMatchedMods();
|
2022-11-12 04:15:40 +05:30
|
|
|
scanPath(path, true);
|
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();
|
2022-11-12 04:15:40 +05:30
|
|
|
m_watcher.addPath(downloadsFolder);
|
|
|
|
m_watcher.addPath(modsFolder);
|
2022-10-25 13:49:19 +05:30
|
|
|
}
|
|
|
|
|
2022-10-26 08:48:14 +05:30
|
|
|
/// @brief scan all watched folder
|
2022-11-01 19:36:36 +05:30
|
|
|
void BlockedModsDialog::scanPaths()
|
|
|
|
{
|
2022-11-12 04:15:40 +05:30
|
|
|
for (auto& dir : m_watcher.directories()) {
|
|
|
|
scanPath(dir, false);
|
2022-10-25 13:49:19 +05:30
|
|
|
}
|
2022-11-12 04:15:40 +05:30
|
|
|
runHashTask();
|
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-12 04:15:40 +05:30
|
|
|
void BlockedModsDialog::scanPath(QString path, bool start_task)
|
2022-11-01 19:36:36 +05:30
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-12 02:40:45 +05:30
|
|
|
addHashTask(file);
|
|
|
|
}
|
|
|
|
|
2022-11-12 04:15:40 +05:30
|
|
|
if (start_task) {
|
|
|
|
runHashTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief add a hashing task for the file located at path, add the path to the pending set if the hasing task is already running
|
|
|
|
/// @param path the path to the local file being hashed
|
2022-11-12 04:37:57 +05:30
|
|
|
void BlockedModsDialog::addHashTask(QString path)
|
|
|
|
{
|
2022-11-12 04:15:40 +05:30
|
|
|
if (m_hashing_task->isRunning()) {
|
|
|
|
qDebug() << "[Blocked Mods Dialog] adding a Hash task for" << path << "to the pending set.";
|
|
|
|
m_pending_hash_paths.insert(path);
|
|
|
|
} else {
|
|
|
|
buildHashTask(path);
|
|
|
|
}
|
2022-11-12 02:40:45 +05:30
|
|
|
}
|
2022-10-25 13:49:19 +05:30
|
|
|
|
2022-11-12 04:37:57 +05:30
|
|
|
/// @brief add a hashing task for the file located at path and connect it to check that hash against
|
2022-11-12 02:40:45 +05:30
|
|
|
/// our blocked mods list
|
|
|
|
/// @param path the path to the local file being hashed
|
2022-11-12 04:37:57 +05:30
|
|
|
void BlockedModsDialog::buildHashTask(QString path)
|
|
|
|
{
|
2022-11-12 02:40:45 +05:30
|
|
|
auto hash_task = Hashing::createBlockedModHasher(path, ModPlatform::Provider::FLAME, "sha1");
|
2022-10-25 13:49:19 +05:30
|
|
|
|
2022-11-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] Creating Hash task for path: " << path;
|
2022-11-01 19:36:36 +05:30
|
|
|
|
2022-11-12 02:40:45 +05:30
|
|
|
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 13:49:19 +05:30
|
|
|
|
2022-11-12 04:15:40 +05:30
|
|
|
m_hashing_task->addTask(hash_task);
|
2022-10-25 13:49:19 +05:30
|
|
|
}
|
|
|
|
|
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-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] Checking for match on hash: " << hash << "| From path:" << path;
|
2022-10-25 13:49:19 +05:30
|
|
|
|
2022-11-12 04:15:40 +05:30
|
|
|
for (auto& mod : m_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-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] 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-12 04:15:40 +05:30
|
|
|
for (auto& mod : m_mods) {
|
2022-10-25 13:49:19 +05:30
|
|
|
if (mod.name.compare(filename, Qt::CaseInsensitive) == 0) {
|
2022-11-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] 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()
|
|
|
|
{
|
2022-11-12 04:15:40 +05:30
|
|
|
return std::all_of(m_mods.begin(), m_mods.end(), [](auto const& mod) { return mod.matched; });
|
2022-10-25 13:49:19 +05:30
|
|
|
}
|
|
|
|
|
2022-11-12 02:40:45 +05:30
|
|
|
/// @brief ensure matched file paths still exist
|
2022-11-12 04:37:57 +05:30
|
|
|
void BlockedModsDialog::validateMatchedMods()
|
|
|
|
{
|
2022-11-12 02:40:45 +05:30
|
|
|
bool changed = false;
|
2022-11-12 04:15:40 +05:30
|
|
|
for (auto& mod : m_mods) {
|
2022-11-12 02:40:45 +05:30
|
|
|
if (mod.matched) {
|
|
|
|
QFileInfo file = QFileInfo(mod.localPath);
|
|
|
|
if (!file.exists() || !file.isFile()) {
|
|
|
|
mod.localPath = "";
|
|
|
|
mod.matched = false;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 04:37:57 +05:30
|
|
|
/// @brief run hash task or mark a pending run if it is already runing
|
|
|
|
void BlockedModsDialog::runHashTask()
|
|
|
|
{
|
2022-11-12 04:15:40 +05:30
|
|
|
if (!m_hashing_task->isRunning()) {
|
|
|
|
m_rehash_pending = false;
|
|
|
|
m_hashing_task->start();
|
|
|
|
} else {
|
|
|
|
qDebug() << "[Blocked Mods Dialog] queueing another run of the hashing task";
|
|
|
|
qDebug() << "[Blocked Mods Dialog] pending hash tasks:" << m_pending_hash_paths;
|
|
|
|
m_rehash_pending = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-12 04:37:57 +05:30
|
|
|
void BlockedModsDialog::hashTaskFinished()
|
|
|
|
{
|
2022-11-12 04:15:40 +05:30
|
|
|
qDebug() << "[Blocked Mods Dialog] All hash tasks finished";
|
|
|
|
if (m_rehash_pending) {
|
|
|
|
qDebug() << "[Blocked Mods Dialog] there was a pending rehash, building and running tasks";
|
|
|
|
|
|
|
|
auto path = m_pending_hash_paths.begin();
|
|
|
|
while (path != m_pending_hash_paths.end()) {
|
|
|
|
buildHashTask(*path);
|
|
|
|
path = m_pending_hash_paths.erase(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
runHashTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|