Core: Use Result<ExternalTool *> in ExternalToolManager

.. and replace a use of FileReader

Change-Id: I84151207c3dda710486c1c89bb27b949e2d36989
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2025-04-15 14:13:02 +02:00
parent 4531abbe85
commit 871c93f652
4 changed files with 49 additions and 46 deletions

View File

@@ -330,7 +330,7 @@ static bool parseOutputAttribute(const QString &attribute, QXmlStreamReader *rea
return true; return true;
} }
ExternalTool * ExternalTool::createFromXml(const QByteArray &xml, QString *errorMessage, const QString &locale) Result<ExternalTool *> ExternalTool::createFromXml(const QByteArray &xml, const QString &locale)
{ {
int descriptionLocale = -1; int descriptionLocale = -1;
int nameLocale = -1; int nameLocale = -1;
@@ -427,25 +427,22 @@ ExternalTool * ExternalTool::createFromXml(const QByteArray &xml, QString *error
} }
} }
if (reader.hasError()) { if (reader.hasError()) {
if (errorMessage)
*errorMessage = reader.errorString();
delete tool; delete tool;
return nullptr; return ResultError(reader.errorString());
} }
return tool; return tool;
} }
ExternalTool * ExternalTool::createFromFile(const FilePath &fileName, QString *errorMessage, Result<ExternalTool *> ExternalTool::createFromFile(const FilePath &filePath, const QString &locale)
const QString &locale)
{ {
FilePath absFileName = fileName.absoluteFilePath(); const Result<QByteArray> contents = filePath.fileContents();
FileReader reader; if (!contents)
if (!reader.fetch(absFileName, errorMessage)) return ResultError(contents.error());
return nullptr; Result<ExternalTool *> res = ExternalTool::createFromXml(contents.value(), locale);
ExternalTool *tool = ExternalTool::createFromXml(reader.data(), errorMessage, locale); if (!res)
if (!tool) return ResultError(contents.error());
return nullptr; ExternalTool *tool = res.value();
tool->m_filePath = absFileName; tool->m_filePath = filePath.absoluteFilePath();
return tool; return tool;
} }

View File

@@ -55,10 +55,10 @@ public:
// all tools that are preset (changed or unchanged) have the original value here: // all tools that are preset (changed or unchanged) have the original value here:
std::shared_ptr<ExternalTool> preset() const; std::shared_ptr<ExternalTool> preset() const;
static ExternalTool *createFromXml(const QByteArray &xml, QString *errorMessage = nullptr, static Utils::Result<ExternalTool *> createFromXml(const QByteArray &xml,
const QString &locale = {}); const QString &locale = {});
static ExternalTool *createFromFile(const Utils::FilePath &fileName, QString *errorMessage = nullptr, static Utils::Result<ExternalTool *> createFromFile(const Utils::FilePath &filePath,
const QString &locale = {}); const QString &locale = {});
bool save(QString *errorMessage = nullptr) const; bool save(QString *errorMessage = nullptr) const;

View File

@@ -103,12 +103,13 @@ void ExternalToolManager::parseDirectory(const QString &directory,
const QList<QFileInfo> infoList = dir.entryInfoList(); const QList<QFileInfo> infoList = dir.entryInfoList();
for (const QFileInfo &info : infoList) { for (const QFileInfo &info : infoList) {
const QString &fileName = info.absoluteFilePath(); const QString &fileName = info.absoluteFilePath();
QString error; Result<ExternalTool *> res = ExternalTool::createFromFile(FilePath::fromString(fileName),
ExternalTool *tool = ExternalTool::createFromFile(Utils::FilePath::fromString(fileName), &error, ICore::userInterfaceLanguage()); ICore::userInterfaceLanguage());
if (!tool) { if (!res) {
qWarning() << Tr::tr("Error while parsing external tool %1: %2").arg(fileName, error); qWarning() << Tr::tr("Error while parsing external tool %1: %2").arg(fileName, res.error());
continue; continue;
} }
ExternalTool *tool = res.value();
if (tools->contains(tool->id())) { if (tools->contains(tool->id())) {
if (isPreset) { if (isPreset) {
// preset that was changed // preset that was changed

View File

@@ -89,10 +89,10 @@ private Q_SLOTS:
void ExternaltoolTest::testRead1() void ExternaltoolTest::testRead1()
{ {
QString error; const Result<ExternalTool *> res = ExternalTool::createFromXml(QByteArray(TEST_XML1));
ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML1), &error); QVERIFY(res.has_value());
QVERIFY(tool != 0); ExternalTool *tool = res.value();
QVERIFY(error.isEmpty()); QVERIFY(tool != nullptr);
QCOMPARE(tool->id(), QString::fromLatin1("lupdate")); QCOMPARE(tool->id(), QString::fromLatin1("lupdate"));
QVERIFY(tool->description().startsWith(QLatin1String("Synchronizes tran"))); QVERIFY(tool->description().startsWith(QLatin1String("Synchronizes tran")));
QCOMPARE(tool->displayName(), QString::fromLatin1("Update translations (lupdate)")); QCOMPARE(tool->displayName(), QString::fromLatin1("Update translations (lupdate)"));
@@ -111,10 +111,10 @@ void ExternaltoolTest::testRead1()
void ExternaltoolTest::testRead2() void ExternaltoolTest::testRead2()
{ {
QString error; const Result<ExternalTool *> res = ExternalTool::createFromXml(QByteArray(TEST_XML2));
ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML2), &error); QVERIFY(res.has_value());
QVERIFY(tool != 0); ExternalTool *tool = res.value();
QVERIFY(error.isEmpty()); QVERIFY(tool != nullptr);
QCOMPARE(tool->id(), QString::fromLatin1("sort")); QCOMPARE(tool->id(), QString::fromLatin1("sort"));
QVERIFY(tool->description().startsWith(QLatin1String("Sorts the"))); QVERIFY(tool->description().startsWith(QLatin1String("Sorts the")));
QCOMPARE(tool->displayName(), QString::fromLatin1("Sort")); QCOMPARE(tool->displayName(), QString::fromLatin1("Sort"));
@@ -132,10 +132,10 @@ void ExternaltoolTest::testRead2()
void ExternaltoolTest::testRead3() void ExternaltoolTest::testRead3()
{ {
QString error; const Result<ExternalTool *> res = ExternalTool::createFromXml(QByteArray(TEST_XML3));
ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML3), &error); QVERIFY(res.has_value());
QVERIFY(tool != 0); ExternalTool *tool = res.value();
QVERIFY(error.isEmpty()); QVERIFY(tool != nullptr);
QCOMPARE(tool->id(), QString::fromLatin1("vi")); QCOMPARE(tool->id(), QString::fromLatin1("vi"));
QVERIFY(tool->description().startsWith(QLatin1String("Opens the"))); QVERIFY(tool->description().startsWith(QLatin1String("Opens the")));
QCOMPARE(tool->displayName(), QString::fromLatin1("Edit with vi")); QCOMPARE(tool->displayName(), QString::fromLatin1("Edit with vi"));
@@ -155,35 +155,40 @@ void ExternaltoolTest::testRead3()
void ExternaltoolTest::testReadLocale() void ExternaltoolTest::testReadLocale()
{ {
QString error; QString error;
Result<ExternalTool *> res;
ExternalTool *tool; ExternalTool *tool;
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error); res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG));
QVERIFY(tool != 0); QVERIFY(res.has_value());
QVERIFY(error.isEmpty()); tool = res.value();
QVERIFY(tool != nullptr);
QCOMPARE(tool->description(), QString::fromLatin1("Hi")); QCOMPARE(tool->description(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayName(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi"));
delete tool; delete tool;
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("uk")); res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), QLatin1String("uk"));
QVERIFY(tool != 0); QVERIFY(res.has_value());
QVERIFY(error.isEmpty()); tool = res.value();
QVERIFY(tool != nullptr);
QCOMPARE(tool->description(), QString::fromLatin1("Hi")); QCOMPARE(tool->description(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayName(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi"));
delete tool; delete tool;
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("de_DE.UTF-8")); res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), QLatin1String("de_DE.UTF-8"));
QVERIFY(tool != 0); QVERIFY(res.has_value());
QVERIFY(error.isEmpty()); tool = res.value();
QVERIFY(tool != nullptr);
QCOMPARE(tool->description(), QString::fromLatin1("Hallo")); QCOMPARE(tool->description(), QString::fromLatin1("Hallo"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Hallo")); QCOMPARE(tool->displayName(), QString::fromLatin1("Hallo"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hallo")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hallo"));
delete tool; delete tool;
tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("de_CH")); res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), QLatin1String("de_CH"));
QVERIFY(tool != 0); QVERIFY(res.has_value());
QVERIFY(error.isEmpty()); tool = res.value();
QVERIFY(tool != nullptr);
QCOMPARE(tool->description(), QString::fromLatin1("Grüezi")); QCOMPARE(tool->description(), QString::fromLatin1("Grüezi"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Grüezi")); QCOMPARE(tool->displayName(), QString::fromLatin1("Grüezi"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Grüezi")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Grüezi"));