From a8d44b04da8373640fa65570796e31dc6cb96043 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 21 Sep 2021 07:06:35 +0200 Subject: [PATCH] ProjectExplorer: FilePath-ify JsonWizardFactory Change-Id: Ib56daffeb7e6eac3879e00e41812c699338e1fcb Reviewed-by: Christian Stenger --- .../jsonwizard/jsonwizard_test.cpp | 14 ++--- .../jsonwizard/jsonwizardfactory.cpp | 59 +++++++++---------- .../jsonwizard/jsonwizardfactory.h | 14 ++--- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizard_test.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizard_test.cpp index e27e6d1ed6c..e9cba27c293 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizard_test.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizard_test.cpp @@ -106,7 +106,7 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsEmptyWizard() QString errorMessage; const QJsonObject wizard = createGeneralWizard(QJsonObject()); - const FactoryPtr factory(ProjectExplorer::JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), QDir(), &errorMessage)); + const FactoryPtr factory(ProjectExplorer::JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), {}, &errorMessage)); QVERIFY(factory == nullptr); QCOMPARE(qPrintable(errorMessage), "Page has no typeId set."); } @@ -117,7 +117,7 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsEmptyPage() const QJsonObject pages = createFieldPageJsonObject(QJsonArray()); const QJsonObject wizard = createGeneralWizard(pages); - const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), QDir(), &errorMessage)); + const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), {}, &errorMessage)); QVERIFY(factory == nullptr); QCOMPARE(qPrintable(errorMessage), "When parsing fields of page \"PE.Wizard.Page.Fields\": "); } @@ -150,7 +150,7 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsUnusedKeyAtFields() const QJsonObject wizard = createGeneralWizard(pages); QTest::ignoreMessage(QtWarningMsg, QRegularExpression("has unsupported keys: wrong")); - const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), QDir(), &errorMessage)); + const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizard.toVariantMap(), {}, &errorMessage)); QVERIFY(factory); QVERIFY(errorMessage.isEmpty()); } @@ -173,7 +173,7 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsCheckBox() }); const QJsonObject pages = createFieldPageJsonObject(widgets); const QJsonObject wizardObject = createGeneralWizard(pages); - const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), QDir(), &errorMessage)); + const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage)); QVERIFY2(factory, qPrintable(errorMessage)); Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap()); @@ -205,7 +205,7 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsLineEdit() }); const QJsonObject pages = createFieldPageJsonObject(widgets); const QJsonObject wizardObject = createGeneralWizard(pages); - const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), QDir(), &errorMessage)); + const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage)); QVERIFY2(factory, qPrintable(errorMessage)); Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap()); @@ -234,7 +234,7 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsComboBox() const QJsonObject pages = createFieldPageJsonObject(widgets); const QJsonObject wizardObject = createGeneralWizard(pages); - const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), QDir(), &errorMessage)); + const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage)); QVERIFY2(factory, qPrintable(errorMessage)); Utils::Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap()); @@ -292,7 +292,7 @@ void ProjectExplorer::ProjectExplorerPlugin::testJsonWizardsIconList() const QJsonObject pages = createFieldPageJsonObject(widgets); const QJsonObject wizardObject = createGeneralWizard(pages); - const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), QDir(), &errorMessage)); + const FactoryPtr factory(JsonWizardFactory::createWizardFactory(wizardObject.toVariantMap(), {}, &errorMessage)); QVERIFY2(factory, qPrintable(errorMessage)); Wizard *wizard = factory->runWizard({}, &parent, Id(), QVariantMap()); diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp index 54e03cee8db..c0066b0b36d 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.cpp @@ -217,8 +217,7 @@ QList JsonWizardFactory::createWizardFactories() if (path.isEmpty()) continue; - QDir dir(path.toString()); - if (!dir.exists()) { + if (!path.exists()) { if (verbose()) verboseLog.append(tr("Path \"%1\" does not exist when checking Json wizard search paths.\n") .arg(path.toUserOutput())); @@ -227,22 +226,19 @@ QList JsonWizardFactory::createWizardFactories() const QDir::Filters filters = QDir::Dirs|QDir::Readable|QDir::NoDotAndDotDot; const QDir::SortFlags sortflags = QDir::Name|QDir::IgnoreCase; - QFileInfoList dirs = dir.entryInfoList(filters, sortflags); + FilePaths dirs = path.dirEntries({}, filters, sortflags); while (!dirs.isEmpty()) { - const QFileInfo dirFi = dirs.takeFirst(); - const QDir current(dirFi.absoluteFilePath()); + const FilePath currentDir = dirs.takeFirst(); if (verbose()) verboseLog.append(tr("Checking \"%1\" for %2.\n") - .arg(QDir::toNativeSeparators(current.absolutePath())) + .arg(currentDir.toUserOutput()) .arg(wizardFileName)); - if (current.exists(wizardFileName)) { - QFile configFile(current.absoluteFilePath(wizardFileName)); - configFile.open(QIODevice::ReadOnly); + const FilePath currentFile = currentDir / wizardFileName; + if (currentFile.exists()) { QJsonParseError error; - const QByteArray fileData = configFile.readAll(); + const QByteArray fileData = currentFile.fileContents(); const QJsonDocument json = QJsonDocument::fromJson(fileData, &error); - configFile.close(); if (error.error != QJsonParseError::NoError) { int line = 1; @@ -256,7 +252,7 @@ QList JsonWizardFactory::createWizardFactories() } } verboseLog.append(tr("* Failed to parse \"%1\":%2:%3: %4\n") - .arg(configFile.fileName()) + .arg(currentFile.fileName()) .arg(line).arg(column) .arg(error.errorString())); continue; @@ -264,7 +260,7 @@ QList JsonWizardFactory::createWizardFactories() if (!json.isObject()) { verboseLog.append(tr("* Did not find a JSON object in \"%1\".\n") - .arg(configFile.fileName())); + .arg(currentFile.fileName())); continue; } @@ -279,7 +275,7 @@ QList JsonWizardFactory::createWizardFactories() continue; } - JsonWizardFactory *factory = createWizardFactory(data, current, &errorMessage); + JsonWizardFactory *factory = createWizardFactory(data, currentDir, &errorMessage); if (!factory) { verboseLog.append(tr("* Failed to create: %1\n").arg(errorMessage)); continue; @@ -287,7 +283,7 @@ QList JsonWizardFactory::createWizardFactories() result << factory; } else { - QFileInfoList subDirs = current.entryInfoList(filters, sortflags); + FilePaths subDirs = currentDir.dirEntries({}, filters, sortflags); if (!subDirs.isEmpty()) { // There is no QList::prepend(QList)... dirs.swap(subDirs); @@ -307,7 +303,8 @@ QList JsonWizardFactory::createWizardFactories() return result; } -JsonWizardFactory *JsonWizardFactory::createWizardFactory(const QVariantMap &data, const QDir &baseDir, +JsonWizardFactory *JsonWizardFactory::createWizardFactory(const QVariantMap &data, + const FilePath &baseDir, QString *errorMessage) { auto *factory = new JsonWizardFactory; @@ -381,7 +378,7 @@ Utils::Wizard *JsonWizardFactory::runWizardImpl(const FilePath &path, QWidget *p wizard->setWindowIcon(icon()); wizard->setWindowTitle(displayName()); - wizard->setValue(QStringLiteral("WizardDir"), m_wizardDir); + wizard->setValue(QStringLiteral("WizardDir"), m_wizardDir.toVariant()); QSet tmp = requiredFeatures(); tmp.subtract(pluginFeatures()); wizard->setValue(QStringLiteral("RequiredFeatures"), Utils::Id::toStringList(tmp)); @@ -537,7 +534,7 @@ void JsonWizardFactory::destroyAllFactories() s_generatorFactories.clear(); } -bool JsonWizardFactory::initialize(const QVariantMap &data, const QDir &baseDir, QString *errorMessage) +bool JsonWizardFactory::initialize(const QVariantMap &data, const FilePath &baseDir, QString *errorMessage) { QTC_ASSERT(errorMessage, return false); @@ -582,29 +579,27 @@ bool JsonWizardFactory::initialize(const QVariantMap &data, const QDir &baseDir, setCategory(strVal); strVal = data.value(QLatin1String(ICON_KEY)).toString(); - if (!strVal.isEmpty()) { - strVal = baseDir.absoluteFilePath(strVal); - if (!QFileInfo::exists(strVal)) { - *errorMessage = tr("Icon file \"%1\" not found.").arg(QDir::toNativeSeparators(strVal)); - return false; - } + const FilePath iconPath = baseDir.resolvePath(strVal); + if (!iconPath.exists()) { + *errorMessage = tr("Icon file \"%1\" not found.").arg(iconPath.toUserOutput()); + return false; } const QString iconText = data.value(QLatin1String(ICON_TEXT_KEY)).toString(); - setIcon(strVal.isEmpty() ? QIcon() : QIcon(strVal), iconText); + setIcon(strVal.isEmpty() ? QIcon() : QIcon(iconPath.toString()), iconText); strVal = data.value(QLatin1String(IMAGE_KEY)).toString(); if (!strVal.isEmpty()) { - strVal = baseDir.absoluteFilePath(strVal); - if (!QFileInfo::exists(strVal)) { - *errorMessage = tr("Image file \"%1\" not found.").arg(QDir::toNativeSeparators(strVal)); + const FilePath imagePath = baseDir.resolvePath(strVal); + if (!imagePath.exists()) { + *errorMessage = tr("Image file \"%1\" not found.").arg(imagePath.toUserOutput()); return false; } - setDescriptionImage(strVal); + setDescriptionImage(imagePath.toString()); } - strVal = baseDir.absoluteFilePath("detailsPage.qml"); - if (QFileInfo::exists(strVal)) - setDetailsPageQmlPath(strVal); + const FilePath detailsPage = baseDir.resolvePath(QString("detailsPage.qml")); + if (detailsPage.exists()) + setDetailsPageQmlPath(detailsPage.toString()); setRequiredFeatures(Utils::Id::fromStringList(data.value(QLatin1String(REQUIRED_FEATURES_KEY)).toStringList())); m_preferredFeatures = Utils::Id::fromStringList(data.value(QLatin1String(SUGGESTED_FEATURES_KEY)).toStringList()); diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h index 90a2ee34568..c3c6c5bd343 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfactory.h @@ -31,15 +31,10 @@ #include -#include +#include -#include #include -QT_BEGIN_NAMESPACE -class QDir; -QT_END_NAMESPACE - namespace ProjectExplorer { class JsonWizardFactory; @@ -94,7 +89,8 @@ private: // Create all wizards. As other plugins might register factories for derived // classes. Called when the new file dialog is shown for the first time. static QList createWizardFactories(); - static JsonWizardFactory *createWizardFactory(const QVariantMap &data, const QDir &baseDir, + static JsonWizardFactory *createWizardFactory(const QVariantMap &data, + const Utils::FilePath &baseDir, QString *errorMessage); static Utils::FilePaths &searchPaths(); @@ -102,10 +98,10 @@ private: static int verbose(); static void destroyAllFactories(); - bool initialize(const QVariantMap &data, const QDir &baseDir, QString *errorMessage); + bool initialize(const QVariantMap &data, const Utils::FilePath &baseDir, QString *errorMessage); QVariant m_enabledExpression; - QString m_wizardDir; + Utils::FilePath m_wizardDir; QList m_generators; QList m_pages;