summaryrefslogtreecommitdiff
path: root/src/plugins/boostbuildprojectmanager/b2projectnode.cpp
blob: 0d67a291aaf8149ba08418af29d3bed8a3d02781 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// Copyright (C) 2013 Mateusz Łoskot <mateusz@loskot.net>
// Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
//
// This file is part of Qt Creator Boost.Build plugin project.
//
// This is free software; you can redistribute and/or modify it under
// the terms of the  GNU Lesser General Public License, Version 2.1
// as published by the Free Software Foundation.
// See accompanying file LICENSE.txt or copy at
// http://www.gnu.org/licenses/lgpl-2.1-standalone.html.
//
#include "b2projectnode.h"
#include "b2project.h"
#include "b2utility.h"

#include <coreplugin/idocument.h>
#include <projectexplorer/projectnodes.h>
#include <utils/qtcassert.h>

#include <QFutureInterface>
#include <QHash>
#include <QList>
#include <QSet>
#include <QString>
#include <QStringList>

namespace BoostBuildProjectManager {
namespace Internal {

ProjectNode::ProjectNode(Project* project, Core::IDocument* projectFile)
    : ProjectExplorer::ProjectNode(projectFile->filePath())
    , m_project(project)
    , m_projectFile(projectFile)
{
    // TODO: setDisplayName(QFileInfo(projectFile->filePath()).completeBaseName());
}

bool ProjectNode::hasBuildTargets() const
{
    return false;
}

QList<ProjectExplorer::ProjectAction>
ProjectNode::supportedActions(Node* node) const
{
    Q_UNUSED(node);

    // TODO: Jamfiles (auto)editing not supported, does it make sense to manage files?
    return QList<ProjectExplorer::ProjectAction>();
}

bool ProjectNode::canAddSubProject(QString const& filePath) const
{
    Q_UNUSED(filePath)
    return false;
}

bool ProjectNode::addSubProjects(QStringList const& filePaths)
{
    Q_UNUSED(filePaths)
    return false;
}

bool ProjectNode::removeSubProjects(QStringList const& filePath)
{
    Q_UNUSED(filePath)
    return false;
}

bool ProjectNode::addFiles(QStringList const& filePaths, QStringList* notAdded = 0)
{
    Q_UNUSED(filePaths);
    Q_UNUSED(notAdded);
    return false;
}

bool ProjectNode::removeFiles(QStringList const& filePaths, QStringList* notRemoved = 0)
{
    Q_UNUSED(filePaths);
    Q_UNUSED(notRemoved);
    return false;
}

bool ProjectNode::deleteFiles(QStringList const& filePaths)
{
    Q_UNUSED(filePaths);
    return false;
}

bool ProjectNode::renameFile(QString const& filePath, QString const& newFilePath)
{
    Q_UNUSED(filePath);
    Q_UNUSED(newFilePath);
    return false;
}

QList<ProjectExplorer::RunConfiguration*> ProjectNode::runConfigurationsFor(Node* node)
{
    Q_UNUSED(node);
    return QList<ProjectExplorer::RunConfiguration*>();
}

void ProjectNode::refresh(QSet<QString> oldFileList)
{
    // The idea of refreshing files in project explorer taken from GenericProjectManager

    // Only do this once, at first run.
    if (oldFileList.isEmpty()) {
        using ProjectExplorer::FileNode;
        FileNode* projectFileNode = new FileNode(m_project->projectFilePath()
                                               , ProjectExplorer::ProjectFileType
                                               , Constants::FileNotGenerated);

        FileNode* filesFileNode = new FileNode(Utils::FileName::fromString(m_project->filesFilePath())
                                             , ProjectExplorer::ProjectFileType
                                             , Constants::FileNotGenerated);

        FileNode* includesFileNode = new FileNode(Utils::FileName::fromString(m_project->includesFilePath())
                                             , ProjectExplorer::ProjectFileType
                                             , Constants::FileNotGenerated);

        addFileNodes(QList<FileNode*>()
            << projectFileNode << filesFileNode << includesFileNode);
    }

    oldFileList.remove(m_project->filesFilePath());
    oldFileList.remove(m_project->includesFilePath());

    QSet<QString> newFileList = m_project->files().toSet();
    newFileList.remove(m_project->filesFilePath());
    newFileList.remove(m_project->includesFilePath());

    // Calculate set of added and removed files
    QSet<QString> removed = oldFileList;
    removed.subtract(newFileList);
    QSet<QString> added = newFileList;
    added.subtract(oldFileList);

    typedef QHash<QString, QStringList> FilesInPaths;
    typedef FilesInPaths::ConstIterator FilesInPathsIterator;
    using ProjectExplorer::FileNode;
    using ProjectExplorer::FileType;
    QString const baseDir = QFileInfo(path().toString()).absolutePath();

    // Process all added files
    FilesInPaths filesInPaths = Utility::sortFilesIntoPaths(baseDir, added);
    for (FilesInPathsIterator it = filesInPaths.constBegin(),
         cend = filesInPaths.constEnd(); it != cend; ++it) {
        // Create node
        QString const& filePath = it.key();
        QStringList parts;
        if (!filePath.isEmpty())
            parts = filePath.split(QLatin1Char('/'));

        FolderNode* folder = findFolderByName(parts, parts.size());
        if (!folder)
            folder = createFolderByName(parts, parts.size());

        // Attach content to node
        QList<FileNode*> fileNodes;
        foreach (QString const& file, it.value()) {
            // TODO: handle various types, mime to FileType
            FileType fileType = ProjectExplorer::SourceType;
            FileNode* fileNode = new FileNode(Utils::FileName::fromString(file), fileType, Constants::FileNotGenerated);
            fileNodes.append(fileNode);
        }

        addFileNodes(fileNodes);
    }

    // Process all removed files
    filesInPaths = Utility::sortFilesIntoPaths(baseDir, removed);
    for (FilesInPathsIterator it = filesInPaths.constBegin(),
         cend = filesInPaths.constEnd(); it != cend; ++it) {
        // Create node
        QString const& filePath = it.key();
        QStringList parts;
        if (!filePath.isEmpty())
            parts = filePath.split(QLatin1Char('/'));
        FolderNode* folder = findFolderByName(parts, parts.size());

        QList<FileNode*> fileNodes;
        foreach (QString const& file, it.value()) {
            foreach (FileNode* fn, folder->fileNodes())
                if (fn->path() == Utils::FileName::fromString(file))
                    fileNodes.append(fn);
        }

        removeFileNodes(fileNodes);
    }

    // Clean up
    foreach (FolderNode* fn, subFolderNodes())
        removeEmptySubFolders(this, fn);

}

void ProjectNode::removeEmptySubFolders(FolderNode* parent, FolderNode* subParent)
{
    foreach (FolderNode* fn, subParent->subFolderNodes())
        removeEmptySubFolders(subParent, fn);

    if (subParent->subFolderNodes().isEmpty() && subParent->fileNodes().isEmpty())
        removeFolderNodes(QList<FolderNode*>() << subParent);
}

QString appendPathComponents(QStringList const& components, int const end)
{
    QString folderName;
    for (int i = 0; i < end; ++i) {
        folderName.append(components.at(i));
        folderName += QLatin1Char('/');
    }
    return folderName;
}

ProjectExplorer::FolderNode*
ProjectNode::createFolderByName(QStringList const& components, int const end)
{
    if (end == 0)
        return this;

    using ProjectExplorer::FolderNode;
    QString const baseDir = QFileInfo(path().toString()).path();
    QString const folderName = appendPathComponents(components, end);
    FolderNode* folder = new FolderNode(Utils::FileName::fromString(baseDir + QLatin1Char('/') + folderName));
    folder->setDisplayName(components.at(end - 1));

    FolderNode* parent = findFolderByName(components, end - 1);
    if (!parent)
        parent = createFolderByName(components, end - 1);
    addFolderNodes(QList<FolderNode*>() << folder);

    return folder;
}

ProjectExplorer::FolderNode*
ProjectNode::findFolderByName(QStringList const& components, int const end) const
{
    if (end == 0)
        return const_cast<ProjectNode*>(this);

    using ProjectExplorer::FolderNode;
    FolderNode *parent = findFolderByName(components, end - 1);
    if (!parent)
        return 0;

    QString const folderName = appendPathComponents(components, end);
    QString const baseDir = QFileInfo(path().toString()).path();
    foreach (FolderNode* fn, parent->subFolderNodes()) {
        if (fn->path() == Utils::FileName::fromString(baseDir + QLatin1Char('/') + folderName))
            return fn;
    }

    return 0;
}

} // namespace Internal
} // namespace BoostBuildProjectManager