forked from qt-creator/qt-creator
ProjectExplorer: Use a local object pool for IProjectManagers
Change-Id: I94ada96da2bf7ce4c95c0ae8f393e6303e79ff60 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -29,6 +29,8 @@
|
||||
|
||||
#include <QObject>
|
||||
|
||||
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;
|
||||
|
||||
@@ -33,8 +33,6 @@
|
||||
|
||||
#include <coreplugin/featureprovider.h>
|
||||
|
||||
#include <extensionsystem/pluginmanager.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/macroexpander.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
@@ -105,18 +103,12 @@ void JsonKitsPage::setPreferredFeatures(const QVariant &data)
|
||||
void JsonKitsPage::setupProjectFiles(const JsonWizard::GeneratorFiles &files)
|
||||
{
|
||||
Project *project = nullptr;
|
||||
QList<IProjectManager *> managerList = ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
||||
|
||||
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))
|
||||
|
||||
@@ -32,8 +32,6 @@
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
|
||||
#include <extensionsystem/pluginmanager.h>
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/qtcassert.h>
|
||||
@@ -103,23 +101,14 @@ Core::GeneratedFiles JsonWizardScannerGenerator::fileList(Utils::MacroExpander *
|
||||
|
||||
result = scan(project.absolutePath(), project);
|
||||
|
||||
QList<IProjectManager *> projectManagers
|
||||
= ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
||||
|
||||
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());
|
||||
});
|
||||
bool found = IProjectManager::managerForMimeType(Utils::mimeTypeForFile(relPath));
|
||||
if (found && !(onlyFirst && projectCount++))
|
||||
it->setAttributes(it->attributes() | Core::GeneratedFile::OpenProjectAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -122,6 +122,7 @@
|
||||
#include <utils/fileutils.h>
|
||||
#include <utils/macroexpander.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
#include <utils/objectpool.h>
|
||||
#include <utils/parameteraction.h>
|
||||
#include <utils/processhandle.h>
|
||||
#include <utils/qtcassert.h>
|
||||
@@ -340,6 +341,7 @@ public:
|
||||
QStringList m_profileMimeTypes;
|
||||
AppOutputPane *m_outputPane = nullptr;
|
||||
|
||||
Utils::ObjectPool<IProjectManager> m_projectManagers;
|
||||
QList<QPair<QString, QString> > 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<IProjectManager*> projectManagers =
|
||||
ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
||||
|
||||
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<IProjectManager*> allProjectManagers()
|
||||
{
|
||||
return ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
||||
}
|
||||
|
||||
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<IProjectManager*> projectManagers = allProjectManagers();
|
||||
|
||||
QList<Project*> openedPro;
|
||||
QList<Project *> alreadyOpen;
|
||||
QString errorString;
|
||||
@@ -1702,10 +1694,7 @@ 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 (IProjectManager *manager = IProjectManager::managerForMimeType(mt)) {
|
||||
if (!QFileInfo(filePath).isFile()) {
|
||||
appendError(errorString,
|
||||
tr("Failed opening project \"%1\": Project is not a file").arg(fileName));
|
||||
@@ -1726,10 +1715,7 @@ ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProjects(con
|
||||
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<QPair<QString, QString> > 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
|
||||
|
||||
Reference in New Issue
Block a user