2022-08-09 05:53:50 +01:00
|
|
|
#include "Resource.h"
|
|
|
|
|
2023-02-12 09:44:39 +00:00
|
|
|
#include <QFileInfo>
|
2023-08-14 17:16:53 +01:00
|
|
|
#include <QRegularExpression>
|
2022-08-10 18:42:24 +01:00
|
|
|
|
2022-08-09 05:53:50 +01:00
|
|
|
#include "FileSystem.h"
|
|
|
|
|
|
|
|
Resource::Resource(QObject* parent) : QObject(parent) {}
|
|
|
|
|
|
|
|
Resource::Resource(QFileInfo file_info) : QObject()
|
|
|
|
{
|
|
|
|
setFile(file_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::setFile(QFileInfo file_info)
|
|
|
|
{
|
|
|
|
m_file_info = file_info;
|
|
|
|
parseFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::parseFile()
|
|
|
|
{
|
|
|
|
QString file_name{ m_file_info.fileName() };
|
|
|
|
|
|
|
|
m_type = ResourceType::UNKNOWN;
|
|
|
|
|
|
|
|
m_internal_id = file_name;
|
|
|
|
|
|
|
|
if (m_file_info.isDir()) {
|
|
|
|
m_type = ResourceType::FOLDER;
|
|
|
|
m_name = file_name;
|
|
|
|
} else if (m_file_info.isFile()) {
|
2022-08-13 15:58:39 +01:00
|
|
|
if (file_name.endsWith(".disabled")) {
|
2022-08-09 05:53:50 +01:00
|
|
|
file_name.chop(9);
|
2022-08-13 15:58:39 +01:00
|
|
|
m_enabled = false;
|
|
|
|
}
|
2022-08-09 05:53:50 +01:00
|
|
|
|
|
|
|
if (file_name.endsWith(".zip") || file_name.endsWith(".jar")) {
|
|
|
|
m_type = ResourceType::ZIPFILE;
|
|
|
|
file_name.chop(4);
|
2023-02-19 22:21:53 +00:00
|
|
|
} else if (file_name.endsWith(".nilmod")) {
|
|
|
|
m_type = ResourceType::ZIPFILE;
|
|
|
|
file_name.chop(7);
|
2022-08-09 05:53:50 +01:00
|
|
|
} else if (file_name.endsWith(".litemod")) {
|
|
|
|
m_type = ResourceType::LITEMOD;
|
|
|
|
file_name.chop(8);
|
|
|
|
} else {
|
|
|
|
m_type = ResourceType::SINGLEFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_name = file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_changed_date_time = m_file_info.lastModified();
|
|
|
|
}
|
|
|
|
|
2022-08-10 18:42:24 +01:00
|
|
|
static void removeThePrefix(QString& string)
|
|
|
|
{
|
|
|
|
QRegularExpression regex(QStringLiteral("^(?:the|teh) +"), QRegularExpression::CaseInsensitiveOption);
|
|
|
|
string.remove(regex);
|
|
|
|
string = string.trimmed();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, bool> Resource::compare(const Resource& other, SortType type) const
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
default:
|
2022-08-13 15:58:39 +01:00
|
|
|
case SortType::ENABLED:
|
|
|
|
if (enabled() && !other.enabled())
|
|
|
|
return { 1, type == SortType::ENABLED };
|
|
|
|
if (!enabled() && other.enabled())
|
|
|
|
return { -1, type == SortType::ENABLED };
|
2023-06-08 00:37:54 +01:00
|
|
|
break;
|
2022-08-10 18:42:24 +01:00
|
|
|
case SortType::NAME: {
|
|
|
|
QString this_name{ name() };
|
|
|
|
QString other_name{ other.name() };
|
|
|
|
|
|
|
|
removeThePrefix(this_name);
|
|
|
|
removeThePrefix(other_name);
|
|
|
|
|
|
|
|
auto compare_result = QString::compare(this_name, other_name, Qt::CaseInsensitive);
|
|
|
|
if (compare_result != 0)
|
|
|
|
return { compare_result, type == SortType::NAME };
|
2023-06-08 00:37:54 +01:00
|
|
|
break;
|
2022-08-10 18:42:24 +01:00
|
|
|
}
|
|
|
|
case SortType::DATE:
|
|
|
|
if (dateTimeChanged() > other.dateTimeChanged())
|
|
|
|
return { 1, type == SortType::DATE };
|
|
|
|
if (dateTimeChanged() < other.dateTimeChanged())
|
|
|
|
return { -1, type == SortType::DATE };
|
2023-06-08 00:37:54 +01:00
|
|
|
break;
|
2022-08-10 18:42:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return { 0, false };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resource::applyFilter(QRegularExpression filter) const
|
|
|
|
{
|
|
|
|
return filter.match(name()).hasMatch();
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:58:39 +01:00
|
|
|
bool Resource::enable(EnableAction action)
|
|
|
|
{
|
|
|
|
if (m_type == ResourceType::UNKNOWN || m_type == ResourceType::FOLDER)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QString path = m_file_info.absoluteFilePath();
|
|
|
|
QFile file(path);
|
|
|
|
|
|
|
|
bool enable = true;
|
|
|
|
switch (action) {
|
|
|
|
case EnableAction::ENABLE:
|
|
|
|
enable = true;
|
|
|
|
break;
|
|
|
|
case EnableAction::DISABLE:
|
|
|
|
enable = false;
|
|
|
|
break;
|
|
|
|
case EnableAction::TOGGLE:
|
|
|
|
default:
|
|
|
|
enable = !enabled();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_enabled == enable)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
// m_enabled is false, but there's no '.disabled' suffix.
|
|
|
|
// TODO: Report error?
|
|
|
|
if (!path.endsWith(".disabled"))
|
|
|
|
return false;
|
|
|
|
path.chop(9);
|
|
|
|
|
|
|
|
if (!file.rename(path))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
path += ".disabled";
|
|
|
|
|
|
|
|
if (!file.rename(path))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setFile(QFileInfo(path));
|
|
|
|
|
|
|
|
m_enabled = enable;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-08 18:51:28 +01:00
|
|
|
bool Resource::destroy(bool attemptTrash)
|
2022-08-09 05:53:50 +01:00
|
|
|
{
|
|
|
|
m_type = ResourceType::UNKNOWN;
|
2023-07-08 18:51:28 +01:00
|
|
|
return (attemptTrash && FS::trash(m_file_info.filePath())) || FS::deletePath(m_file_info.filePath());
|
2022-08-09 05:53:50 +01:00
|
|
|
}
|
2023-02-12 09:44:39 +00:00
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
bool Resource::isSymLinkUnder(const QString& instPath) const
|
2023-02-12 09:44:39 +00:00
|
|
|
{
|
|
|
|
if (isSymLink())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto instDir = QDir(instPath);
|
|
|
|
|
|
|
|
auto relAbsPath = instDir.relativeFilePath(m_file_info.absoluteFilePath());
|
|
|
|
auto relCanonPath = instDir.relativeFilePath(m_file_info.canonicalFilePath());
|
|
|
|
|
|
|
|
return relAbsPath != relCanonPath;
|
|
|
|
}
|
|
|
|
|
2023-08-14 17:16:53 +01:00
|
|
|
bool Resource::isMoreThanOneHardLink() const
|
2023-02-12 09:44:39 +00:00
|
|
|
{
|
|
|
|
return FS::hardLinkCount(m_file_info.absoluteFilePath()) > 1;
|
|
|
|
}
|