From a52063ee398a042a83e50fdfa79dd972b29d91b7 Mon Sep 17 00:00:00 2001 From: Tobias Hunger <tobias.hunger@digia.com> Date: Mon, 27 May 2013 11:17:47 +0200 Subject: [PATCH] Projects: Improve opening of projects Move logic to detect already open projects into ProjectExplorer itself, along with some check for the canonicalFilePath. Remove the same logic from the individual projectmanagers. Put check that the path is a file into project managers. So far all of them assume the project file to be a file (e.g. a xcode project manager would expect a directory though). Task-number: QTCREATORBUG-9350 Change-Id: I3901958395e3c594c8cfba9a85dc7d3ec3334afb Reviewed-by: Daniel Teske <daniel.teske@digia.com> --- .../autotoolsmanager.cpp | 20 ++-------- .../cmakeprojectmanager.cpp | 9 ++++- .../genericprojectmanager.cpp | 17 +++----- src/plugins/projectexplorer/iprojectmanager.h | 1 + .../projectexplorer/projectexplorer.cpp | 39 +++++++++++++++---- .../qbsprojectmanager/qbsprojectmanager.cpp | 8 +++- .../qmlprojectmanager/qmlprojectmanager.cpp | 20 +++------- .../qt4projectmanager/qt4projectmanager.cpp | 22 ++--------- 8 files changed, 65 insertions(+), 71 deletions(-) diff --git a/src/plugins/autotoolsprojectmanager/autotoolsmanager.cpp b/src/plugins/autotoolsprojectmanager/autotoolsmanager.cpp index 50e0a5a1a18..4e72214ab25 100644 --- a/src/plugins/autotoolsprojectmanager/autotoolsmanager.cpp +++ b/src/plugins/autotoolsprojectmanager/autotoolsmanager.cpp @@ -47,26 +47,14 @@ namespace Internal { Project *AutotoolsManager::openProject(const QString &fileName, QString *errorString) { - QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath(); - - if (canonicalFilePath.isEmpty()) { + if (!QFileInfo(fileName).isFile()) { if (errorString) - *errorString = tr("Failed opening project '%1': Project file does not exist") - .arg(QDir::toNativeSeparators(fileName)); + *errorString = tr("Failed opening project '%1': Project is not a file") + .arg(fileName); return 0; } - // Check whether the project is already open or not. - ProjectExplorerPlugin *projectExplorer = ProjectExplorerPlugin::instance(); - foreach (Project *pi, projectExplorer->session()->projects()) { - if (canonicalFilePath == pi->document()->fileName()) { - *errorString = tr("Failed opening project '%1': Project already open") - .arg(QDir::toNativeSeparators(canonicalFilePath)); - return 0; - } - } - - return new AutotoolsProject(this, canonicalFilePath); + return new AutotoolsProject(this, fileName); } QString AutotoolsManager::mimeType() const diff --git a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp index cadf683c6dc..63f13f62f92 100644 --- a/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeprojectmanager.cpp @@ -126,8 +126,13 @@ void CMakeManager::runCMake(ProjectExplorer::Project *project) ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName, QString *errorString) { - Q_UNUSED(errorString) - // TODO check whether this project is already opened + if (!QFileInfo(fileName).isFile()) { + if (errorString) + *errorString = tr("Failed opening project '%1': Project is not a file") + .arg(fileName); + return 0; + } + return new CMakeProject(this, fileName); } diff --git a/src/plugins/genericprojectmanager/genericprojectmanager.cpp b/src/plugins/genericprojectmanager/genericprojectmanager.cpp index 329eb9f682d..2ae768efa4f 100644 --- a/src/plugins/genericprojectmanager/genericprojectmanager.cpp +++ b/src/plugins/genericprojectmanager/genericprojectmanager.cpp @@ -51,21 +51,14 @@ QString Manager::mimeType() const ProjectExplorer::Project *Manager::openProject(const QString &fileName, QString *errorString) { - if (!QFileInfo(fileName).isFile()) + if (!QFileInfo(fileName).isFile()) { + if (errorString) + *errorString = tr("Failed opening project '%1': Project is not a file") + .arg(fileName); return 0; - - ProjectExplorer::ProjectExplorerPlugin *projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance(); - foreach (ProjectExplorer::Project *pi, projectExplorer->session()->projects()) { - if (fileName == pi->document()->fileName()) { - if (errorString) - *errorString = tr("Failed opening project '%1': Project already open") - .arg(QDir::toNativeSeparators(fileName)); - return 0; - } } - GenericProject *project = new GenericProject(this, fileName); - return project; + return new GenericProject(this, fileName); } void Manager::registerProject(GenericProject *project) diff --git a/src/plugins/projectexplorer/iprojectmanager.h b/src/plugins/projectexplorer/iprojectmanager.h index a85108a7827..3b7cd301b9c 100644 --- a/src/plugins/projectexplorer/iprojectmanager.h +++ b/src/plugins/projectexplorer/iprojectmanager.h @@ -50,6 +50,7 @@ public: IProjectManager() {} virtual QString mimeType() const = 0; + // fileName is a canonical path! virtual Project *openProject(const QString &fileName, QString *errorString) = 0; }; diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 29ed06b897c..ece65886ff3 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -1334,6 +1334,16 @@ static inline QList<IProjectManager*> allProjectManagers() return ExtensionSystem::PluginManager::getObjects<IProjectManager>(); } +static void appendError(QString *errorString, const QString &error) +{ + if (!errorString || error.isEmpty()) + return; + + if (!errorString->isEmpty()) + errorString->append(QLatin1Char('\n')); + errorString->append(error); +} + QList<Project *> ProjectExplorerPlugin::openProjects(const QStringList &fileNames, QString *errorString) { if (debug) @@ -1343,11 +1353,29 @@ QList<Project *> ProjectExplorerPlugin::openProjects(const QStringList &fileName QList<Project*> openedPro; foreach (const QString &fileName, fileNames) { + QTC_ASSERT(!fileName.isEmpty(), continue); + + QFileInfo fi = QFileInfo(fileName); + QString canonicalFilePath = fi.canonicalFilePath(); + bool found = false; + foreach (ProjectExplorer::Project *pi, session()->projects()) { + if (canonicalFilePath == pi->document()->fileName()) { + found = true; + break; + } + } + if (found) { + appendError(errorString, tr("Failed opening project '%1': Project already open") + .arg(QDir::toNativeSeparators(fileName))); + d->m_session->reportProjectLoadingProgress(); + continue; + } + if (const Core::MimeType mt = Core::ICore::mimeDatabase()->findByFile(QFileInfo(fileName))) { foreach (IProjectManager *manager, projectManagers) { if (manager->mimeType() == mt.type()) { QString tmp; - if (Project *pro = manager->openProject(fileName, &tmp)) { + if (Project *pro = manager->openProject(canonicalFilePath, &tmp)) { if (pro->restoreSettings()) { connect(pro, SIGNAL(fileListChanged()), this, SIGNAL(fileListChanged())); d->m_session->addProject(pro); @@ -1359,16 +1387,13 @@ QList<Project *> ProjectExplorerPlugin::openProjects(const QStringList &fileName delete pro; } } - if (errorString) { - if (!errorString->isEmpty() && !tmp.isEmpty()) - errorString->append(QLatin1Char('\n')); - errorString->append(tmp); - } - d->m_session->reportProjectLoadingProgress(); + if (!tmp.isEmpty()) + appendError(errorString, tmp); break; } } } + d->m_session->reportProjectLoadingProgress(); } updateActions(); diff --git a/src/plugins/qbsprojectmanager/qbsprojectmanager.cpp b/src/plugins/qbsprojectmanager/qbsprojectmanager.cpp index 2d84008a9e2..d2ed0946911 100644 --- a/src/plugins/qbsprojectmanager/qbsprojectmanager.cpp +++ b/src/plugins/qbsprojectmanager/qbsprojectmanager.cpp @@ -115,9 +115,13 @@ QString QbsManager::mimeType() const ProjectExplorer::Project *QbsManager::openProject(const QString &fileName, QString *errorString) { - Q_UNUSED(errorString); + if (!QFileInfo(fileName).isFile()) { + if (errorString) + *errorString = tr("Failed opening project '%1': Project is not a file") + .arg(fileName); + return 0; + } - // FIXME: This is way too simplistic! return new Internal::QbsProject(this, fileName); } diff --git a/src/plugins/qmlprojectmanager/qmlprojectmanager.cpp b/src/plugins/qmlprojectmanager/qmlprojectmanager.cpp index 233a0095213..210e6436938 100644 --- a/src/plugins/qmlprojectmanager/qmlprojectmanager.cpp +++ b/src/plugins/qmlprojectmanager/qmlprojectmanager.cpp @@ -49,22 +49,14 @@ QString Manager::mimeType() const ProjectExplorer::Project *Manager::openProject(const QString &fileName, QString *errorString) { - QFileInfo fileInfo(fileName); - ProjectExplorer::ProjectExplorerPlugin *projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance(); - - foreach (ProjectExplorer::Project *pi, projectExplorer->session()->projects()) { - if (fileName == pi->document()->fileName()) { - if (errorString) - *errorString = tr("Failed opening project '%1': Project already open") .arg(QDir::toNativeSeparators(fileName)); - return 0; - } + if (!QFileInfo(fileName).isFile()) { + if (errorString) + *errorString = tr("Failed opening project '%1': Project is not a file") + .arg(fileName); + return 0; } - if (fileInfo.isFile()) - return new QmlProject(this, fileName); - - *errorString = tr("Failed opening project '%1': Project file is not a file").arg(QDir::toNativeSeparators(fileName)); - return 0; + return new QmlProject(this, fileName); } void Manager::registerProject(QmlProject *project) diff --git a/src/plugins/qt4projectmanager/qt4projectmanager.cpp b/src/plugins/qt4projectmanager/qt4projectmanager.cpp index be93686f5a2..33926d23636 100644 --- a/src/plugins/qt4projectmanager/qt4projectmanager.cpp +++ b/src/plugins/qt4projectmanager/qt4projectmanager.cpp @@ -176,28 +176,14 @@ QString Qt4Manager::mimeType() const ProjectExplorer::Project *Qt4Manager::openProject(const QString &fileName, QString *errorString) { - // TODO Make all file paths relative & remove this hack - // We convert the path to an absolute one here because qt4project.cpp - // && profileevaluator use absolute/canonical file paths all over the place - // Correct fix would be to remove these calls ... - QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath(); - - if (canonicalFilePath.isEmpty()) { + if (!QFileInfo(fileName).isFile()) { if (errorString) - *errorString = tr("Failed opening project '%1': Project file does not exist").arg(QDir::toNativeSeparators(fileName)); + *errorString = tr("Failed opening project '%1': Project is not a file") + .arg(fileName); return 0; } - foreach (ProjectExplorer::Project *pi, projectExplorer()->session()->projects()) { - if (canonicalFilePath == pi->document()->fileName()) { - if (errorString) - *errorString = tr("Failed opening project '%1': Project already open").arg(QDir::toNativeSeparators(canonicalFilePath)); - return 0; - } - } - - Qt4Project *pro = new Qt4Project(this, canonicalFilePath); - return pro; + return new Qt4Project(this, fileName); } ProjectExplorer::ProjectExplorerPlugin *Qt4Manager::projectExplorer() const -- GitLab