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;
}
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 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 *> 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<QByteArray> contents = filePath.fileContents();
if (!contents)
return ResultError(contents.error());
Result<ExternalTool *> res = ExternalTool::createFromXml(contents.value(), locale);
if (!res)
return ResultError(contents.error());
ExternalTool *tool = res.value();
tool->m_filePath = filePath.absoluteFilePath();
return tool;
}

View File

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

View File

@@ -103,12 +103,13 @@ void ExternalToolManager::parseDirectory(const QString &directory,
const QList<QFileInfo> 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<ExternalTool *> 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

View File

@@ -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<ExternalTool *> 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<ExternalTool *> 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<ExternalTool *> 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<ExternalTool *> 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"));