2023-08-18 10:53:03 +00:00
|
|
|
#include "StatusQ/stringutilsinternal.h"
|
|
|
|
|
2023-09-22 10:02:30 +00:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileSelector>
|
|
|
|
#include <QQmlEngine>
|
|
|
|
#include <QQmlFileSelector>
|
2023-10-10 14:23:48 +00:00
|
|
|
#include <QUrl>
|
2023-09-22 10:02:30 +00:00
|
|
|
|
2023-09-22 10:08:28 +00:00
|
|
|
StringUtilsInternal::StringUtilsInternal(QQmlEngine* engine, QObject* parent)
|
2023-09-22 10:02:30 +00:00
|
|
|
: m_engine(engine)
|
|
|
|
, QObject(parent)
|
2023-09-22 10:08:28 +00:00
|
|
|
{ }
|
2023-08-18 10:53:03 +00:00
|
|
|
|
2023-09-22 10:08:28 +00:00
|
|
|
QString StringUtilsInternal::escapeHtml(const QString& unsafe) const
|
2023-08-18 10:53:03 +00:00
|
|
|
{
|
|
|
|
return unsafe.toHtmlEscaped();
|
|
|
|
}
|
|
|
|
|
2023-10-03 15:43:35 +00:00
|
|
|
QString resolveFileUsingQmlImportPaths(QQmlEngine *engine, const QString &relativeFilePath) {
|
|
|
|
QStringList importPaths = engine->importPathList();
|
|
|
|
for (const auto &path : importPaths) {
|
|
|
|
QString fullPath = path + "/" + relativeFilePath;
|
|
|
|
QFile file(fullPath);
|
|
|
|
if (file.exists()) {
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2023-09-22 10:02:30 +00:00
|
|
|
QString StringUtilsInternal::readTextFile(const QString& filePath) const
|
2023-08-18 10:53:03 +00:00
|
|
|
{
|
2023-09-22 10:02:30 +00:00
|
|
|
auto selector = QQmlFileSelector::get(m_engine);
|
|
|
|
if (!selector) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "No QQmlFileSelector available to load text file:" << filePath;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto resolvedFilePath = selector->selector()->select(filePath);
|
|
|
|
|
|
|
|
QFile file(resolvedFilePath);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2023-10-03 15:43:35 +00:00
|
|
|
auto fileUrl = resolveFileUsingQmlImportPaths(m_engine, filePath);
|
|
|
|
if (fileUrl.isEmpty()) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Can't find file in QML import paths" << filePath;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
file.setFileName(fileUrl);
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Error opening existing file" << fileUrl << "for reading";
|
|
|
|
return {};
|
|
|
|
}
|
2023-09-22 10:02:30 +00:00
|
|
|
}
|
2023-08-18 10:53:03 +00:00
|
|
|
|
2023-09-22 10:02:30 +00:00
|
|
|
return file.readAll();
|
2023-08-18 10:53:03 +00:00
|
|
|
}
|
2023-10-10 14:23:48 +00:00
|
|
|
|
|
|
|
QString StringUtilsInternal::extractDomainFromLink(const QString& link) const
|
|
|
|
{
|
|
|
|
const auto url = QUrl::fromUserInput(link);
|
|
|
|
if (!url.isValid()) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Invalid URL:" << link;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return url.host();
|
|
|
|
}
|