diff --git a/src/plugins/projectexplorer/iprojectmanager.h b/src/plugins/projectexplorer/iprojectmanager.h index ff01c83a113..0e4d5f07331 100644 --- a/src/plugins/projectexplorer/iprojectmanager.h +++ b/src/plugins/projectexplorer/iprojectmanager.h @@ -29,6 +29,8 @@ #include +namespace Utils { class MimeType; } + namespace ProjectExplorer { class Project; @@ -38,6 +40,10 @@ class PROJECTEXPLORER_EXPORT IProjectManager : public QObject Q_OBJECT public: + IProjectManager(); + // Finds a IProjectManager matching the passed MimeType. + static IProjectManager *managerForMimeType(const Utils::MimeType &mt); + virtual QString mimeType() const = 0; // FileName is a canonical path of a checked-to-exist file. virtual Project *openProject(const QString &fileName) = 0; diff --git a/src/plugins/projectexplorer/jsonwizard/jsonkitspage.cpp b/src/plugins/projectexplorer/jsonwizard/jsonkitspage.cpp index 29e4f93020e..23576222a3c 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonkitspage.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonkitspage.cpp @@ -33,8 +33,6 @@ #include -#include - #include #include #include @@ -105,18 +103,12 @@ void JsonKitsPage::setPreferredFeatures(const QVariant &data) void JsonKitsPage::setupProjectFiles(const JsonWizard::GeneratorFiles &files) { Project *project = nullptr; - QList managerList = ExtensionSystem::PluginManager::getObjects(); - foreach (const JsonWizard::GeneratorFile &f, files) { + for (const JsonWizard::GeneratorFile &f : files) { if (f.file.attributes() & GeneratedFile::OpenProjectAttribute) { const QFileInfo fi(f.file.path()); const QString path = fi.absoluteFilePath(); - - Utils::MimeType mt = Utils::mimeTypeForFile(fi); - if (!mt.isValid()) - continue; - - auto manager = Utils::findOrDefault(managerList, Utils::equal(&IProjectManager::mimeType, mt.name())); + IProjectManager *manager = IProjectManager::managerForMimeType(Utils::mimeTypeForFile(fi)); project = manager ? manager->openProject(path) : nullptr; if (project) { if (setupProject(project)) diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.cpp index cf23511e347..88efc6f5e71 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardscannergenerator.cpp @@ -32,8 +32,6 @@ #include -#include - #include #include #include @@ -103,22 +101,13 @@ Core::GeneratedFiles JsonWizardScannerGenerator::fileList(Utils::MacroExpander * result = scan(project.absolutePath(), project); - QList projectManagers - = ExtensionSystem::PluginManager::getObjects(); - int projectCount = 0; for (auto it = result.begin(); it != result.end(); ++it) { const QString relPath = project.relativeFilePath(it->path()); it->setBinary(binaryPattern.match(relPath).hasMatch()); - - Utils::MimeType mt = Utils::mimeTypeForFile(relPath); - if (mt.isValid()) { - bool found = Utils::anyOf(projectManagers, [mt](IProjectManager *m) { - return mt.matchesName(m->mimeType()); - }); - if (found && !(onlyFirst && projectCount++)) - it->setAttributes(it->attributes() | Core::GeneratedFile::OpenProjectAttribute); - } + bool found = IProjectManager::managerForMimeType(Utils::mimeTypeForFile(relPath)); + if (found && !(onlyFirst && projectCount++)) + it->setAttributes(it->attributes() | Core::GeneratedFile::OpenProjectAttribute); } return result; diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 6c13bb6eb84..5eb06df0262 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -122,6 +122,7 @@ #include #include #include +#include #include #include #include @@ -340,6 +341,7 @@ public: QStringList m_profileMimeTypes; AppOutputPane *m_outputPane = nullptr; + Utils::ObjectPool m_projectManagers; QList > m_recentProjects; // pair of filename, displayname static const int m_maxRecentProjects = 25; @@ -1453,9 +1455,6 @@ void ProjectExplorerPluginPrivate::closeAllProjects() void ProjectExplorerPlugin::extensionsInitialized() { // Register factories for all project managers - QList projectManagers = - ExtensionSystem::PluginManager::getObjects(); - QStringList allGlobPatterns; const QString filterSeparator = QLatin1String(";;"); @@ -1474,7 +1473,7 @@ void ProjectExplorerPlugin::extensionsInitialized() }); factory->addMimeType(QStringLiteral("inode/directory")); - foreach (IProjectManager *manager, projectManagers) { + dd->m_projectManagers.forEachObject([&](IProjectManager *manager) { const QString mimeType = manager->mimeType(); factory->addMimeType(mimeType); Utils::MimeType mime = Utils::mimeTypeForName(mimeType); @@ -1482,7 +1481,7 @@ void ProjectExplorerPlugin::extensionsInitialized() filterStrings.append(mime.filterString()); dd->m_profileMimeTypes += mimeType; - } + }); addAutoReleasedObject(factory); @@ -1660,11 +1659,6 @@ void ProjectExplorerPlugin::showOpenProjectError(const OpenProjectResult &result } } -static QList allProjectManagers() -{ - return ExtensionSystem::PluginManager::getObjects(); -} - static void appendError(QString &errorString, const QString &error) { if (error.isEmpty()) @@ -1677,8 +1671,6 @@ static void appendError(QString &errorString, const QString &error) ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProjects(const QStringList &fileNames) { - const QList projectManagers = allProjectManagers(); - QList openedPro; QList alreadyOpen; QString errorString; @@ -1702,34 +1694,28 @@ ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProjects(con Utils::MimeType mt = Utils::mimeTypeForFile(fileName); if (mt.isValid()) { - bool foundProjectManager = false; - foreach (IProjectManager *manager, projectManagers) { - if (mt.matchesName(manager->mimeType())) { - foundProjectManager = true; - if (!QFileInfo(filePath).isFile()) { - appendError(errorString, - tr("Failed opening project \"%1\": Project is not a file").arg(fileName)); - } else if (Project *pro = manager->openProject(filePath)) { - QObject::connect(pro, &Project::parsingFinished, [pro]() { - emit SessionManager::instance()->projectFinishedParsing(pro); - }); - QString restoreError; - Project::RestoreResult restoreResult = pro->restoreSettings(&restoreError); - if (restoreResult == Project::RestoreResult::Ok) { - connect(pro, &Project::fileListChanged, - m_instance, &ProjectExplorerPlugin::fileListChanged); - SessionManager::addProject(pro); - openedPro += pro; - } else { - if (restoreResult == Project::RestoreResult::Error) - appendError(errorString, restoreError); - delete pro; - } + if (IProjectManager *manager = IProjectManager::managerForMimeType(mt)) { + if (!QFileInfo(filePath).isFile()) { + appendError(errorString, + tr("Failed opening project \"%1\": Project is not a file").arg(fileName)); + } else if (Project *pro = manager->openProject(filePath)) { + QObject::connect(pro, &Project::parsingFinished, [pro]() { + emit SessionManager::instance()->projectFinishedParsing(pro); + }); + QString restoreError; + Project::RestoreResult restoreResult = pro->restoreSettings(&restoreError); + if (restoreResult == Project::RestoreResult::Ok) { + connect(pro, &Project::fileListChanged, + m_instance, &ProjectExplorerPlugin::fileListChanged); + SessionManager::addProject(pro); + openedPro += pro; + } else { + if (restoreResult == Project::RestoreResult::Error) + appendError(errorString, restoreError); + delete pro; } - break; } - } - if (!foundProjectManager) { + } else { appendError(errorString, tr("Failed opening project \"%1\": No plugin can open project type \"%2\".") .arg(QDir::toNativeSeparators(fileName)) .arg(mt.name())); @@ -3401,11 +3387,11 @@ ProjectExplorerSettings ProjectExplorerPlugin::projectExplorerSettings() QStringList ProjectExplorerPlugin::projectFilePatterns() { QStringList patterns; - foreach (const IProjectManager *pm, allProjectManagers()) { + dd->m_projectManagers.forEachObject([&](IProjectManager *pm) { Utils::MimeType mt = Utils::mimeTypeForName(pm->mimeType()); if (mt.isValid()) patterns.append(mt.globPatterns()); - } + }); return patterns; } @@ -3422,4 +3408,17 @@ QList > ProjectExplorerPlugin::recentProjects() return dd->recentProjects(); } +IProjectManager::IProjectManager() +{ + dd->m_projectManagers.addObject(this); +} + +IProjectManager *IProjectManager::managerForMimeType(const Utils::MimeType &mt) +{ + if (!mt.isValid()) + return nullptr; + auto pred = [mt](IProjectManager *m) { return mt.matchesName(m->mimeType()); }; + return dd->m_projectManagers.findObject(pred); +} + } // namespace ProjectExplorer