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>
|
2024-07-05 12:48:14 +00:00
|
|
|
#include <QTextDocumentFragment>
|
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) {
|
2024-03-11 14:31:28 +00:00
|
|
|
const auto importPaths = engine->importPathList();
|
2023-10-03 15:43:35 +00:00
|
|
|
for (const auto &path : importPaths) {
|
2024-03-11 14:31:28 +00:00
|
|
|
const auto fullPath = path + QStringLiteral("/") + relativeFilePath;
|
2023-10-03 15:43:35 +00:00
|
|
|
QFile file(fullPath);
|
|
|
|
if (file.exists()) {
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 14:31:28 +00:00
|
|
|
return {};
|
2023-10-03 15:43:35 +00:00
|
|
|
}
|
|
|
|
|
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 {};
|
|
|
|
}
|
|
|
|
|
2024-03-11 14:31:28 +00:00
|
|
|
QString selectedFilePath;
|
|
|
|
const auto maybeFileUrl = QUrl(filePath).toLocalFile(); // support local file URLs (e.g. "file:///foo/bar/baz.txt")
|
2023-09-22 10:02:30 +00:00
|
|
|
|
2024-03-11 14:31:28 +00:00
|
|
|
if (QFile::exists(maybeFileUrl))
|
|
|
|
selectedFilePath = maybeFileUrl;
|
|
|
|
else
|
|
|
|
selectedFilePath = selector->selector()->select(filePath);
|
|
|
|
|
|
|
|
if (selectedFilePath.startsWith(QLatin1String("qrc:/"))) // for some reason doesn't work with the "qrc:/" prefix, drop it
|
|
|
|
selectedFilePath.remove(0, 3);
|
|
|
|
|
|
|
|
QFile file(selectedFilePath);
|
2023-09-22 10:02:30 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2024-03-11 14:31:28 +00:00
|
|
|
const auto resolvedFilePath = resolveFileUsingQmlImportPaths(m_engine, filePath);
|
|
|
|
if (resolvedFilePath.isEmpty()) {
|
2023-10-03 15:43:35 +00:00
|
|
|
qWarning() << Q_FUNC_INFO << "Can't find file in QML import paths" << filePath;
|
|
|
|
return {};
|
|
|
|
}
|
2024-03-11 14:31:28 +00:00
|
|
|
file.setFileName(resolvedFilePath);
|
2023-10-03 15:43:35 +00:00
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
2024-03-11 14:31:28 +00:00
|
|
|
qWarning() << Q_FUNC_INFO << "Error opening existing file" << resolvedFilePath << "for reading";
|
2023-10-03 15:43:35 +00:00
|
|
|
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();
|
|
|
|
}
|
2024-07-05 12:48:14 +00:00
|
|
|
|
|
|
|
QString StringUtilsInternal::plainText(const QString &htmlFragment) const
|
|
|
|
{
|
|
|
|
return QTextDocumentFragment::fromHtml(htmlFragment).toPlainText();
|
|
|
|
}
|