projectfilewizardextension.cpp 13.3 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
#include "projectfilewizardextension.h"
#include "projectexplorer.h"
32
#include "session.h"
con's avatar
con committed
33
34
35
36
#include "projectnodes.h"
#include "nodesvisitor.h"
#include "projectwizardpage.h"

37
38
39
#include <utils/qtcassert.h>
#include <utils/stringutils.h>

con's avatar
con committed
40
41
42
43
44
45
#include <coreplugin/basefilewizard.h>
#include <coreplugin/dialogs/iwizard.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>
46
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
47
48

#include <QtCore/QVariant>
49
#include <QtCore/QtAlgorithms>
con's avatar
con committed
50
51
52
#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtCore/QMultiMap>
53
#include <QtCore/QDir>
con's avatar
con committed
54
55
56
57
58
59
60
61
62

enum { debugExtension = 0 };

namespace ProjectExplorer {

typedef QList<ProjectNode *> ProjectNodeList;

namespace Internal {

dt's avatar
dt committed
63
64
// AllProjectNodesVisitor: Retrieve all projects (*.pri/*.pro)
// which support adding files
con's avatar
con committed
65
66
67
class AllProjectNodesVisitor : public NodesVisitor
{
public:
68
    static ProjectNodeList allProjects();
con's avatar
con committed
69
70
71
72

    virtual void visitProjectNode(ProjectNode *node);

private:
73
    ProjectNodeList m_projectNodes;
con's avatar
con committed
74
75
};

76
ProjectNodeList AllProjectNodesVisitor::allProjects()
con's avatar
con committed
77
{
78
79
80
    AllProjectNodesVisitor visitor;
    ProjectExplorerPlugin::instance()->session()->sessionNode()->accept(&visitor);
    return visitor.m_projectNodes;
con's avatar
con committed
81
82
83
84
}

void AllProjectNodesVisitor::visitProjectNode(ProjectNode *node)
{
dt's avatar
dt committed
85
    if (node->supportedActions(node).contains(ProjectNode::AddFile))
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        m_projectNodes.push_back(node);
}

// ProjectEntry: Context entry for a *.pri/*.pro file. Stores name and path
// for quick sort and path search, provides operator<() for maps.
struct ProjectEntry {
    enum Type { ProFile, PriFile }; // Sort order: 'pro' before 'pri'

    ProjectEntry() : node(0), type(ProFile) {}
    explicit ProjectEntry(ProjectNode *node);

    int compare(const ProjectEntry &rhs) const;

    ProjectNode *node;
    QString nativeDirectory; // For matching against wizards' files, which are native.
    QString fileName;
    QString baseName;
    Type type;
};

ProjectEntry::ProjectEntry(ProjectNode *n) :
    node(n),
    type(ProFile)
{
    const QFileInfo fi(node->path());
    fileName = fi.fileName();
    baseName = fi.baseName();
    if (fi.suffix() != QLatin1String("pro"))
114
        type = PriFile;
115
116
117
118
119
120
    nativeDirectory = QDir::toNativeSeparators(fi.absolutePath());
}

// Sort helper that sorts by base name and puts '*.pro' before '*.pri'
int ProjectEntry::compare(const ProjectEntry &rhs) const
{
121
122
    if (const int drc = nativeDirectory.compare(rhs.nativeDirectory))
        return drc;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    if (const int brc = baseName.compare(rhs.baseName))
        return brc;
    if (type < rhs.type)
        return -1;
    if (type > rhs.type)
        return 1;
    return 0;
}

inline bool operator<(const ProjectEntry &pe1, const ProjectEntry &pe2)
{
    return pe1.compare(pe2) < 0;
}

QDebug operator<<(QDebug d, const ProjectEntry &e)
{
    d.nospace() << e.nativeDirectory << ' ' << e.fileName << ' ' << e.type;
    return d;
con's avatar
con committed
141
142
143
}

// --------- ProjectWizardContext
144
145
struct ProjectWizardContext
{
146
147
148
149
150
    ProjectWizardContext();
    void clear();

    QList<Core::IVersionControl*> versionControls;
    QList<ProjectEntry> projects;
con's avatar
con committed
151
    ProjectWizardPage *page;
152
153
    bool repositoryExists; // Is VCS 'add' sufficient, or should a repository be created?
    QString commonDirectory;
con's avatar
con committed
154
155
};

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
ProjectWizardContext::ProjectWizardContext() :
    page(0),
    repositoryExists(false)
{
}

void ProjectWizardContext::clear()
{
    versionControls.clear();
    projects.clear();
    commonDirectory.clear();
    page = 0;
    repositoryExists = false;
}

con's avatar
con committed
171
// ---- ProjectFileWizardExtension
172
173
ProjectFileWizardExtension::ProjectFileWizardExtension()
  : m_context(0)
con's avatar
con committed
174
175
176
177
178
179
180
181
{
}

ProjectFileWizardExtension::~ProjectFileWizardExtension()
{
    delete m_context;
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Find the project the new files should be added to given their common
// path. Either a direct match on the directory or the directory with
// the longest matching path (list containing"/project/subproject1" matching
// common path "/project/subproject1/newuserpath").
// This relies on 'pro' occurring before 'pri' in the list.
static int findMatchingProject(const QList<ProjectEntry> &projects,
                               const QString &commonPath)
{
    if (projects.isEmpty() || commonPath.isEmpty())
        return -1;

    int bestMatch = -1;
    int bestMatchLength = 0;
    const int count = projects.size();
    for (int p = 0; p < count; p++) {
        // Direct match or better match? (note that the wizards' files are native).
        const QString &projectDirectory = projects.at(p).nativeDirectory;
        if (projectDirectory == commonPath)
            return p;
        if (projectDirectory.size() > bestMatchLength
            && commonPath.startsWith(projectDirectory)) {
            bestMatchLength = projectDirectory.size();
            bestMatch = p;
        }
    }
    return bestMatch;
}

con's avatar
con committed
210
211
212
213
void ProjectFileWizardExtension::firstExtensionPageShown(const QList<Core::GeneratedFile> &files)
{
    if (debugExtension)
        qDebug() << Q_FUNC_INFO << files.size();
214
215
    // Parametrize wizard page: find best project to add to, set up files display and
    // version control depending on path
con's avatar
con committed
216
217
218
    QStringList fileNames;
    foreach (const Core::GeneratedFile &f, files)
        fileNames.push_back(f.path());
219
220
221
222
223
224
225
226
227
228
    m_context->commonDirectory = Utils::commonPath(fileNames);
    m_context->page->setFilesDisplay(m_context->commonDirectory, fileNames);
    // Find best project (Entry at 0 is 'None').
    const int bestProjectIndex = findMatchingProject(m_context->projects, m_context->commonDirectory);
    if (bestProjectIndex == -1) {
        m_context->page->setCurrentProjectIndex(0);
    } else {
        m_context->page->setCurrentProjectIndex(bestProjectIndex + 1);
    }
    initializeVersionControlChoices();
con's avatar
con committed
229
230
}

231
void ProjectFileWizardExtension::initializeVersionControlChoices()
con's avatar
con committed
232
{
233
234
235
236
    // Figure out version control situation:
    // 1) Directory is managed and VCS supports "Add" -> List it
    // 2) Directory is managed and VCS does not support "Add" -> None available
    // 3) Directory is not managed -> Offer all VCS that support "CreateRepository"
237
    m_context->versionControls.clear();
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    if (!m_context->commonDirectory.isEmpty()) {
        Core::IVersionControl *managingControl = Core::ICore::instance()->vcsManager()->findVersionControlForDirectory(m_context->commonDirectory);
        if (managingControl) {
            // Under VCS
            if (managingControl->supportsOperation(Core::IVersionControl::AddOperation)) {
                m_context->versionControls.push_back(managingControl);
                m_context->repositoryExists = true;
            }
        } else {
            // Create
            foreach (Core::IVersionControl *vc, ExtensionSystem::PluginManager::instance()->getObjects<Core::IVersionControl>())
                if (vc->supportsOperation(Core::IVersionControl::CreateRepositoryOperation))
                    m_context->versionControls.push_back(vc);
            m_context->repositoryExists = false;
        }
    } // has a common root.
    // Compile names
    //: No version control system selected
    QStringList versionControlChoices = QStringList(tr("<None>"));
    foreach(const Core::IVersionControl *c, m_context->versionControls)
        versionControlChoices.push_back(c->displayName());
    m_context->page->setVersionControls(versionControlChoices);
    // Enable adding to version control by default.
    if (m_context->repositoryExists && versionControlChoices.size() >= 2)
        m_context->page->setVersionControlIndex(1);
con's avatar
con committed
263
264
265
266
}

QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const Core::IWizard *wizard)
{
267
    if (!m_context) {
con's avatar
con committed
268
        m_context = new ProjectWizardContext;
269
270
271
    } else {
        m_context->clear();
    }
con's avatar
con committed
272
273
    // Init context with page and projects
    m_context->page = new ProjectWizardPage;
274
275
276
277
278
279
280
281
    // Project list remains the same over duration of wizard execution
    // Note that projects cannot be added to projects.
    initProjectChoices(wizard->kind() != Core::IWizard::ProjectWizard);
    return QList<QWizardPage *>() << m_context->page;
}

void ProjectFileWizardExtension::initProjectChoices(bool enabled)
{
con's avatar
con committed
282
    // Set up project list which remains the same over duration of wizard execution
283
284
    // As tooltip, set the directory for disambiguation (should someone have
    // duplicate base names in differing directories).
285
286
    //: No project selected
    QStringList projectChoices(tr("<None>"));
287
    QStringList projectToolTips( QString::null ); // Do not use QString() - gcc-bug.
288
289
290
291
292
293
294
295
296
297
298
299
    if (enabled) {
        typedef QMap<ProjectEntry, bool> ProjectEntryMap;
        // Sort by base name and purge duplicated entries (resulting from dependencies)
        // via Map.
        ProjectEntryMap entryMap;
        foreach(ProjectNode *n, AllProjectNodesVisitor::allProjects())
            entryMap.insert(ProjectEntry(n), true);
        // Collect names
        const ProjectEntryMap::const_iterator cend = entryMap.constEnd();
        for (ProjectEntryMap::const_iterator it = entryMap.constBegin(); it != cend; ++it) {
            m_context->projects.push_back(it.key());
            projectChoices.push_back(it.key().fileName);
300
            projectToolTips.push_back(it.key().nativeDirectory);
301
        }
con's avatar
con committed
302
    }
303
    m_context->page->setProjects(projectChoices);
304
    m_context->page->setProjectToolTips(projectToolTips);
con's avatar
con committed
305
306
307
}

bool ProjectFileWizardExtension::process(const QList<Core::GeneratedFile> &files, QString *errorMessage)
308
309
310
311
312
313
314
{
    return processProject(files, errorMessage) &&
           processVersionControl(files, errorMessage);
}

// Add files to project && version control
bool ProjectFileWizardExtension::processProject(const QList<Core::GeneratedFile> &files, QString *errorMessage)
con's avatar
con committed
315
316
{
    typedef QMultiMap<FileType, QString> TypeFileMap;
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

    // Add files to  project (Entry at 0 is 'None').
    const int projectIndex = m_context->page->currentProjectIndex() - 1;
    if (projectIndex < 0 || projectIndex >= m_context->projects.size())
        return true;
    ProjectNode *project = m_context->projects.at(projectIndex).node;
    // Split into lists by file type and bulk-add them.
    TypeFileMap typeFileMap;
    const Core::MimeDatabase *mdb = Core::ICore::instance()->mimeDatabase();
    foreach (const Core::GeneratedFile &generatedFile, files) {
        const QString path = generatedFile.path();
        typeFileMap.insert(typeForFileName(mdb, path), path);
    }
    foreach (FileType type, typeFileMap.uniqueKeys()) {
        const QStringList files = typeFileMap.values(type);
        if (!project->addFiles(type, files)) {
            *errorMessage = tr("Failed to add one or more files to project\n'%1' (%2).").
                            arg(project->path(), files.join(QString(QLatin1Char(','))));
            return false;
con's avatar
con committed
336
        }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
    }
    return true;
}

bool ProjectFileWizardExtension::processVersionControl(const QList<Core::GeneratedFile> &files, QString *errorMessage)
{
    // Add files to  version control (Entry at 0 is 'None').
    const int vcsIndex = m_context->page->versionControlIndex() - 1;
    if (vcsIndex < 0 || vcsIndex >= m_context->versionControls.size())
        return true;
    QTC_ASSERT(!m_context->commonDirectory.isEmpty(), return false);
    Core::IVersionControl *versionControl = m_context->versionControls.at(vcsIndex);
    // Create repository?
    if (!m_context->repositoryExists) {
        QTC_ASSERT(versionControl->supportsOperation(Core::IVersionControl::CreateRepositoryOperation), return false);
        if (!versionControl->vcsCreateRepository(m_context->commonDirectory)) {
            *errorMessage = tr("A version control system repository could not be created in '%1'.").arg(m_context->commonDirectory);
            return false;
con's avatar
con committed
355
356
        }
    }
357
358
    // Add files if supported.
    if (versionControl->supportsOperation(Core::IVersionControl::AddOperation)) {
con's avatar
con committed
359
        foreach (const Core::GeneratedFile &generatedFile, files) {
360
            if (!versionControl->vcsAdd(generatedFile.path())) {
con's avatar
con committed
361
362
363
364
365
366
367
368
                *errorMessage = tr("Failed to add '%1' to the version control system.").arg(generatedFile.path());
                return false;
            }
        }
    }
    return true;
}

hjk's avatar
hjk committed
369
370
} // namespace Internal
} // namespace ProjectExplorer