1 /***************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Qt Software Information (qt-info@nokia.com)
10 ** Non-Open Source Usage
12 ** Licensees may use this file in accordance with the Qt Beta Version
13 ** License Agreement, Agreement version 2.2 provided with the Software or,
14 ** alternatively, in accordance with the terms contained in a written
15 ** agreement between you and Nokia.
17 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU General
20 ** Public License versions 2.0 or 3.0 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.GPL included in the packaging
22 ** of this file. Please review the following information to ensure GNU
23 ** General Public Licensing requirements will be met:
25 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
26 ** http://www.gnu.org/copyleft/gpl.html.
28 ** In addition, as a special exception, Nokia gives you certain additional
29 ** rights. These rights are described in the Nokia Qt GPL Exception
30 ** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
32 ***************************************************************************/
34 #include "projectfilewizardextension.h"
35 #include "projectexplorer.h"
36 #include "projectnodes.h"
37 #include "nodesvisitor.h"
38 #include "projectwizardpage.h"
40 #include <coreplugin/basefilewizard.h>
41 #include <coreplugin/dialogs/iwizard.h>
42 #include <coreplugin/filemanager.h>
43 #include <coreplugin/icore.h>
44 #include <coreplugin/iversioncontrol.h>
45 #include <coreplugin/vcsmanager.h>
47 #include <QtCore/QVariant>
48 #include <QtCore/QDebug>
49 #include <QtCore/QFileInfo>
50 #include <QtCore/QMultiMap>
52 enum { debugExtension = 0 };
54 namespace ProjectExplorer {
56 typedef QList<ProjectNode *> ProjectNodeList;
60 // --------- AllProjectNodesVisitor. Figure out all projects.
61 // No sooner said then done.
62 class AllProjectNodesVisitor : public NodesVisitor
64 AllProjectNodesVisitor(ProjectNodeList &l) : m_projectNodes(l) {}
67 static ProjectNodeList allProjects(const ProjectExplorerPlugin *pe);
69 virtual void visitProjectNode(ProjectNode *node);
72 ProjectNodeList &m_projectNodes;
75 ProjectNodeList AllProjectNodesVisitor::allProjects(const ProjectExplorerPlugin *pe)
78 AllProjectNodesVisitor visitor(rc);
79 pe->session()->sessionNode()->accept(&visitor);
83 void AllProjectNodesVisitor::visitProjectNode(ProjectNode *node)
85 if (node->supportedActions().contains(ProjectNode::AddFile))
86 m_projectNodes << node;
89 // --------- ProjectWizardContext
90 struct ProjectWizardContext
92 Core::IVersionControl *versionControl;
93 ProjectNodeList projects;
94 ProjectWizardPage *page;
97 // ---- ProjectFileWizardExtension
98 ProjectFileWizardExtension::ProjectFileWizardExtension()
103 ProjectFileWizardExtension::~ProjectFileWizardExtension()
108 void ProjectFileWizardExtension::firstExtensionPageShown(const QList<Core::GeneratedFile> &files)
111 qDebug() << Q_FUNC_INFO << files.size();
112 // Setup files display and version control depending on path
113 QStringList fileNames;
114 foreach (const Core::GeneratedFile &f, files)
115 fileNames.push_back(f.path());
117 const QString directory = QFileInfo(fileNames.front()).absolutePath();
118 m_context->versionControl = Core::ICore::instance()->vcsManager()->findVersionControlForDirectory(directory);
120 m_context->page->setFilesDisplay(fileNames);
122 const bool canAddToVCS = m_context->versionControl && m_context->versionControl->supportsOperation(Core::IVersionControl::AddOperation);
123 if (m_context->versionControl)
124 m_context->page->setVCSDisplay(m_context->versionControl->name());
125 m_context->page->setAddToVersionControlEnabled(canAddToVCS);
128 static ProjectNode *currentProject()
130 if (Node *currentNode = ProjectExplorerPlugin::instance()->currentNode())
131 if (ProjectNode *currentProjectNode = qobject_cast<ProjectNode *>(currentNode))
132 return currentProjectNode;
136 QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const Core::IWizard *wizard)
139 m_context = new ProjectWizardContext;
140 // Init context with page and projects
141 m_context->page = new ProjectWizardPage;
142 m_context->versionControl = 0;
143 m_context->projects = AllProjectNodesVisitor::allProjects(ProjectExplorerPlugin::instance());
144 // Set up project list which remains the same over duration of wizard execution
145 // Disable "add project to project"
146 const bool hasProjects = !m_context->projects.empty();
148 // Compile list of names and find current project if there is one
149 QStringList projectNames;
150 ProjectNode *current = currentProject();
151 int currentIndex = -1;
152 const int count = m_context->projects.size();
153 for (int i = 0; i < count; i++) {
154 ProjectNode *pn = m_context->projects.at(i);
155 projectNames.push_back(QFileInfo(pn->path()).fileName());
159 m_context->page->setProjects(projectNames);
160 if (currentIndex != -1)
161 m_context->page->setCurrentProjectIndex(currentIndex);
163 m_context->page->setAddToProjectEnabled(hasProjects && wizard->kind() != Core::IWizard::ProjectWizard);
165 return QList<QWizardPage *>() << m_context->page;
168 bool ProjectFileWizardExtension::process(const QList<Core::GeneratedFile> &files, QString *errorMessage)
170 typedef QMultiMap<FileType, QString> TypeFileMap;
171 // Add files to project && version control
172 if (m_context->page->addToProject()) {
173 ProjectNode *project = m_context->projects.at(m_context->page->currentProjectIndex());
174 // Split into lists by file type and add
175 TypeFileMap typeFileMap;
176 foreach (const Core::GeneratedFile &generatedFile, files) {
177 const QString path = generatedFile.path();
178 typeFileMap.insert(typeForFileName(Core::ICore::instance()->mimeDatabase(), path), path);
180 foreach (FileType type, typeFileMap.uniqueKeys()) {
181 const QStringList files = typeFileMap.values(type);
182 if (!project->addFiles(type, files)) {
183 *errorMessage = tr("Failed to add one or more files to project\n'%1' (%2).").
184 arg(project->path(), files.join(QLatin1String(",")));
189 // Add files to version control
190 if (m_context->page->addToVersionControl()) {
191 foreach (const Core::GeneratedFile &generatedFile, files) {
192 if (!m_context->versionControl->vcsAdd(generatedFile.path())) {
193 *errorMessage = tr("Failed to add '%1' to the version control system.").arg(generatedFile.path());
202 } // namespace Internal
203 } // namespace ProjectExplorer