From 871c93f652ddea81d8f69cfbcc100bdeb5fe17d9 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 15 Apr 2025 14:13:02 +0200 Subject: [PATCH] Core: Use Result in ExternalToolManager .. and replace a use of FileReader Change-Id: I84151207c3dda710486c1c89bb27b949e2d36989 Reviewed-by: Jarek Kobus --- src/plugins/coreplugin/externaltool.cpp | 25 ++++----- src/plugins/coreplugin/externaltool.h | 8 +-- .../coreplugin/externaltoolmanager.cpp | 9 ++-- .../externaltool/tst_externaltooltest.cpp | 53 ++++++++++--------- 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/plugins/coreplugin/externaltool.cpp b/src/plugins/coreplugin/externaltool.cpp index 9c0b55f2b62..6e14c95a9d8 100644 --- a/src/plugins/coreplugin/externaltool.cpp +++ b/src/plugins/coreplugin/externaltool.cpp @@ -330,7 +330,7 @@ static bool parseOutputAttribute(const QString &attribute, QXmlStreamReader *rea return true; } -ExternalTool * ExternalTool::createFromXml(const QByteArray &xml, QString *errorMessage, const QString &locale) +Result ExternalTool::createFromXml(const QByteArray &xml, const QString &locale) { int descriptionLocale = -1; int nameLocale = -1; @@ -427,25 +427,22 @@ ExternalTool * ExternalTool::createFromXml(const QByteArray &xml, QString *error } } if (reader.hasError()) { - if (errorMessage) - *errorMessage = reader.errorString(); delete tool; - return nullptr; + return ResultError(reader.errorString()); } return tool; } -ExternalTool * ExternalTool::createFromFile(const FilePath &fileName, QString *errorMessage, - const QString &locale) +Result ExternalTool::createFromFile(const FilePath &filePath, const QString &locale) { - FilePath absFileName = fileName.absoluteFilePath(); - FileReader reader; - if (!reader.fetch(absFileName, errorMessage)) - return nullptr; - ExternalTool *tool = ExternalTool::createFromXml(reader.data(), errorMessage, locale); - if (!tool) - return nullptr; - tool->m_filePath = absFileName; + const Result contents = filePath.fileContents(); + if (!contents) + return ResultError(contents.error()); + Result res = ExternalTool::createFromXml(contents.value(), locale); + if (!res) + return ResultError(contents.error()); + ExternalTool *tool = res.value(); + tool->m_filePath = filePath.absoluteFilePath(); return tool; } diff --git a/src/plugins/coreplugin/externaltool.h b/src/plugins/coreplugin/externaltool.h index d2e8be43d9b..cf6420dec6d 100644 --- a/src/plugins/coreplugin/externaltool.h +++ b/src/plugins/coreplugin/externaltool.h @@ -55,10 +55,10 @@ public: // all tools that are preset (changed or unchanged) have the original value here: std::shared_ptr preset() const; - static ExternalTool *createFromXml(const QByteArray &xml, QString *errorMessage = nullptr, - const QString &locale = {}); - static ExternalTool *createFromFile(const Utils::FilePath &fileName, QString *errorMessage = nullptr, - const QString &locale = {}); + static Utils::Result createFromXml(const QByteArray &xml, + const QString &locale = {}); + static Utils::Result createFromFile(const Utils::FilePath &filePath, + const QString &locale = {}); bool save(QString *errorMessage = nullptr) const; diff --git a/src/plugins/coreplugin/externaltoolmanager.cpp b/src/plugins/coreplugin/externaltoolmanager.cpp index b7f909222e4..855d58aa043 100644 --- a/src/plugins/coreplugin/externaltoolmanager.cpp +++ b/src/plugins/coreplugin/externaltoolmanager.cpp @@ -103,12 +103,13 @@ void ExternalToolManager::parseDirectory(const QString &directory, const QList infoList = dir.entryInfoList(); for (const QFileInfo &info : infoList) { const QString &fileName = info.absoluteFilePath(); - QString error; - ExternalTool *tool = ExternalTool::createFromFile(Utils::FilePath::fromString(fileName), &error, ICore::userInterfaceLanguage()); - if (!tool) { - qWarning() << Tr::tr("Error while parsing external tool %1: %2").arg(fileName, error); + Result res = ExternalTool::createFromFile(FilePath::fromString(fileName), + ICore::userInterfaceLanguage()); + if (!res) { + qWarning() << Tr::tr("Error while parsing external tool %1: %2").arg(fileName, res.error()); continue; } + ExternalTool *tool = res.value(); if (tools->contains(tool->id())) { if (isPreset) { // preset that was changed diff --git a/tests/auto/externaltool/tst_externaltooltest.cpp b/tests/auto/externaltool/tst_externaltooltest.cpp index d82337449ca..9e2efcb8102 100644 --- a/tests/auto/externaltool/tst_externaltooltest.cpp +++ b/tests/auto/externaltool/tst_externaltooltest.cpp @@ -89,10 +89,10 @@ private Q_SLOTS: void ExternaltoolTest::testRead1() { - QString error; - ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML1), &error); - QVERIFY(tool != 0); - QVERIFY(error.isEmpty()); + const Result res = ExternalTool::createFromXml(QByteArray(TEST_XML1)); + QVERIFY(res.has_value()); + ExternalTool *tool = res.value(); + QVERIFY(tool != nullptr); QCOMPARE(tool->id(), QString::fromLatin1("lupdate")); QVERIFY(tool->description().startsWith(QLatin1String("Synchronizes tran"))); QCOMPARE(tool->displayName(), QString::fromLatin1("Update translations (lupdate)")); @@ -111,10 +111,10 @@ void ExternaltoolTest::testRead1() void ExternaltoolTest::testRead2() { - QString error; - ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML2), &error); - QVERIFY(tool != 0); - QVERIFY(error.isEmpty()); + const Result res = ExternalTool::createFromXml(QByteArray(TEST_XML2)); + QVERIFY(res.has_value()); + ExternalTool *tool = res.value(); + QVERIFY(tool != nullptr); QCOMPARE(tool->id(), QString::fromLatin1("sort")); QVERIFY(tool->description().startsWith(QLatin1String("Sorts the"))); QCOMPARE(tool->displayName(), QString::fromLatin1("Sort")); @@ -132,10 +132,10 @@ void ExternaltoolTest::testRead2() void ExternaltoolTest::testRead3() { - QString error; - ExternalTool *tool = ExternalTool::createFromXml(QByteArray(TEST_XML3), &error); - QVERIFY(tool != 0); - QVERIFY(error.isEmpty()); + const Result res = ExternalTool::createFromXml(QByteArray(TEST_XML3)); + QVERIFY(res.has_value()); + ExternalTool *tool = res.value(); + QVERIFY(tool != nullptr); QCOMPARE(tool->id(), QString::fromLatin1("vi")); QVERIFY(tool->description().startsWith(QLatin1String("Opens the"))); QCOMPARE(tool->displayName(), QString::fromLatin1("Edit with vi")); @@ -155,35 +155,40 @@ void ExternaltoolTest::testRead3() void ExternaltoolTest::testReadLocale() { QString error; + Result res; ExternalTool *tool; - tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error); - QVERIFY(tool != 0); - QVERIFY(error.isEmpty()); + res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG)); + QVERIFY(res.has_value()); + tool = res.value(); + QVERIFY(tool != nullptr); QCOMPARE(tool->description(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayName(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi")); delete tool; - tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("uk")); - QVERIFY(tool != 0); - QVERIFY(error.isEmpty()); + res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), QLatin1String("uk")); + QVERIFY(res.has_value()); + tool = res.value(); + QVERIFY(tool != nullptr); QCOMPARE(tool->description(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayName(), QString::fromLatin1("Hi")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi")); delete tool; - tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("de_DE.UTF-8")); - QVERIFY(tool != 0); - QVERIFY(error.isEmpty()); + res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), QLatin1String("de_DE.UTF-8")); + QVERIFY(res.has_value()); + tool = res.value(); + QVERIFY(tool != nullptr); QCOMPARE(tool->description(), QString::fromLatin1("Hallo")); QCOMPARE(tool->displayName(), QString::fromLatin1("Hallo")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hallo")); delete tool; - tool = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), &error, QLatin1String("de_CH")); - QVERIFY(tool != 0); - QVERIFY(error.isEmpty()); + res = ExternalTool::createFromXml(QByteArray(TEST_XML_LANG), QLatin1String("de_CH")); + QVERIFY(res.has_value()); + tool = res.value(); + QVERIFY(tool != nullptr); QCOMPARE(tool->description(), QString::fromLatin1("Grüezi")); QCOMPARE(tool->displayName(), QString::fromLatin1("Grüezi")); QCOMPARE(tool->displayCategory(), QString::fromLatin1("Grüezi"));