OSDN Git Service

Fixes: - Show extension of projects to add files to
[qt-creator-jp/qt-creator-jp.git] / src / plugins / projectexplorer / projectfilewizardextension.cpp
1 /***************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact:  Qt Software Information (qt-info@nokia.com)
8 **
9 **
10 ** Non-Open Source Usage
11 **
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.
16 **
17 ** GNU General Public License Usage
18 **
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:
24 **
25 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
26 ** http://www.gnu.org/copyleft/gpl.html.
27 **
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.
31 **
32 ***************************************************************************/
33
34 #include "projectfilewizardextension.h"
35 #include "projectexplorer.h"
36 #include "projectnodes.h"
37 #include "nodesvisitor.h"
38 #include "projectwizardpage.h"
39
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>
46
47 #include <QtCore/QVariant>
48 #include <QtCore/QDebug>
49 #include <QtCore/QFileInfo>
50 #include <QtCore/QMultiMap>
51
52 enum { debugExtension = 0 };
53
54 namespace ProjectExplorer {
55
56 typedef QList<ProjectNode *> ProjectNodeList;
57
58 namespace Internal {
59
60 // --------- AllProjectNodesVisitor. Figure out all projects.
61 // No sooner said then done.
62 class AllProjectNodesVisitor : public NodesVisitor
63 {
64     AllProjectNodesVisitor(ProjectNodeList &l) : m_projectNodes(l) {}
65 public:
66
67     static ProjectNodeList allProjects(const ProjectExplorerPlugin *pe);
68
69     virtual void visitProjectNode(ProjectNode *node);
70
71 private:
72     ProjectNodeList &m_projectNodes;
73 };
74
75 ProjectNodeList AllProjectNodesVisitor::allProjects(const ProjectExplorerPlugin *pe)
76 {
77     ProjectNodeList rc;
78     AllProjectNodesVisitor visitor(rc);
79     pe->session()->sessionNode()->accept(&visitor);
80     return rc;
81 }
82
83 void AllProjectNodesVisitor::visitProjectNode(ProjectNode *node)
84 {
85     if (node->supportedActions().contains(ProjectNode::AddFile))
86         m_projectNodes << node;
87 }
88
89 // --------- ProjectWizardContext
90 struct ProjectWizardContext
91 {
92     Core::IVersionControl *versionControl;
93     ProjectNodeList projects;
94     ProjectWizardPage *page;
95 };
96
97 // ---- ProjectFileWizardExtension
98 ProjectFileWizardExtension::ProjectFileWizardExtension()
99   : m_context(0)
100 {
101 }
102
103 ProjectFileWizardExtension::~ProjectFileWizardExtension()
104 {
105     delete m_context;
106 }
107
108 void ProjectFileWizardExtension::firstExtensionPageShown(const QList<Core::GeneratedFile> &files)
109 {
110     if (debugExtension)
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());
116
117     const QString directory = QFileInfo(fileNames.front()).absolutePath();
118     m_context->versionControl = Core::ICore::instance()->vcsManager()->findVersionControlForDirectory(directory);
119
120     m_context->page->setFilesDisplay(fileNames);
121
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);
126 }
127
128 static ProjectNode *currentProject()
129 {
130     if (Node *currentNode = ProjectExplorerPlugin::instance()->currentNode())
131         if (ProjectNode *currentProjectNode = qobject_cast<ProjectNode *>(currentNode))
132             return currentProjectNode;
133     return 0;
134 }
135
136 QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const Core::IWizard *wizard)
137 {
138     if (!m_context)
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();
147     if (hasProjects) {
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());
156             if (current == pn)
157                 currentIndex = i;
158         }
159         m_context->page->setProjects(projectNames);
160         if (currentIndex != -1)
161             m_context->page->setCurrentProjectIndex(currentIndex);
162     }
163     m_context->page->setAddToProjectEnabled(hasProjects && wizard->kind() != Core::IWizard::ProjectWizard);
164
165     return QList<QWizardPage *>() << m_context->page;
166 }
167
168 bool ProjectFileWizardExtension::process(const QList<Core::GeneratedFile> &files, QString *errorMessage)
169 {
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);
179         }
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(",")));
185                 return false;
186             }
187         }
188     }
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());
194                 return false;
195             }
196         }
197     }
198
199     return true;
200 }
201
202 } // namespace Internal
203 } // namespace ProjectExplorer