genericprojectnodes.cpp 8.64 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Roberto Raggi's avatar
Roberto Raggi committed
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
Roberto Raggi's avatar
Roberto Raggi committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Roberto Raggi's avatar
Roberto Raggi committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
Roberto Raggi's avatar
Roberto Raggi committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
Roberto Raggi's avatar
Roberto Raggi committed
30 31

#include "genericprojectnodes.h"
Roberto Raggi's avatar
Roberto Raggi committed
32 33
#include "genericproject.h"

34
#include <utils/fileutils.h>
35
#include <coreplugin/idocument.h>
36 37
#include <projectexplorer/projectexplorer.h>

38
#include <QFileInfo>
Roberto Raggi's avatar
Roberto Raggi committed
39

hjk's avatar
hjk committed
40 41 42 43
using namespace ProjectExplorer;

namespace GenericProjectManager {
namespace Internal {
Roberto Raggi's avatar
Roberto Raggi committed
44

45
GenericProjectNode::GenericProjectNode(GenericProject *project, Core::IDocument *projectFile)
46
    : ProjectNode(projectFile->filePath())
47 48
    , m_project(project)
    , m_projectFile(projectFile)
49
{
50
    setDisplayName(projectFile->filePath().toFileInfo().completeBaseName());
51
}
Roberto Raggi's avatar
Roberto Raggi committed
52

53
Core::IDocument *GenericProjectNode::projectFile() const
hjk's avatar
hjk committed
54 55 56
{
    return m_projectFile;
}
Roberto Raggi's avatar
Roberto Raggi committed
57 58

QString GenericProjectNode::projectFilePath() const
hjk's avatar
hjk committed
59
{
60
    return m_projectFile->filePath().toString();
hjk's avatar
hjk committed
61
}
Roberto Raggi's avatar
Roberto Raggi committed
62

63
QHash<QString, QStringList> sortFilesIntoPaths(const QString &base, const QSet<QString> &files)
64 65
{
    QHash<QString, QStringList> filesInPath;
66
    const QDir baseDir(base);
67

68
    foreach (const QString &absoluteFileName, files) {
69
        QFileInfo fileInfo(absoluteFileName);
70
        Utils::FileName absoluteFilePath = Utils::FileName::fromString(fileInfo.path());
71
        QString relativeFilePath;
72

73 74
        if (absoluteFilePath.isChildOf(baseDir)) {
            relativeFilePath = absoluteFilePath.relativeChildPath(Utils::FileName::fromString(base)).toString();
75 76
        } else {
            // `file' is not part of the project.
77
            relativeFilePath = baseDir.relativeFilePath(absoluteFilePath.toString());
78 79
            if (relativeFilePath.endsWith(QLatin1Char('/')))
                relativeFilePath.chop(1);
80
        }
81 82 83

        filesInPath[relativeFilePath].append(absoluteFileName);
    }
84 85 86 87 88
    return filesInPath;
}

void GenericProjectNode::refresh(QSet<QString> oldFileList)
{
89 90 91
    typedef QHash<QString, QStringList> FilesInPathHash;
    typedef FilesInPathHash::ConstIterator FilesInPathHashConstIt;

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    // Do those separately
    oldFileList.remove(m_project->filesFileName());
    oldFileList.remove(m_project->includesFileName());
    oldFileList.remove(m_project->configFileName());

    QSet<QString> newFileList = m_project->files().toSet();
    newFileList.remove(m_project->filesFileName());
    newFileList.remove(m_project->includesFileName());
    newFileList.remove(m_project->configFileName());

    QSet<QString> removed = oldFileList;
    removed.subtract(newFileList);
    QSet<QString> added = newFileList;
    added.subtract(oldFileList);

107
    QString baseDir = path().toFileInfo().absolutePath();
108 109 110 111 112
    FilesInPathHash filesInPaths = sortFilesIntoPaths(baseDir, added);

    FilesInPathHashConstIt cend = filesInPaths.constEnd();
    for (FilesInPathHashConstIt it = filesInPaths.constBegin(); it != cend; ++it) {
        const QString &filePath = it.key();
113 114 115
        QStringList components;
        if (!filePath.isEmpty())
            components = filePath.split(QLatin1Char('/'));
116 117 118
        FolderNode *folder = findFolderByName(components, components.size());
        if (!folder)
            folder = createFolderByName(components, components.size());
119 120

        QList<FileNode *> fileNodes;
121
        foreach (const QString &file, it.value()) {
122
            FileType fileType = SourceType; // ### FIXME
123 124
            FileNode *fileNode = new FileNode(Utils::FileName::fromString(file),
                                              fileType, /*generated = */ false);
125 126 127
            fileNodes.append(fileNode);
        }

128
        folder->addFileNodes(fileNodes);
129
    }
130 131

    filesInPaths = sortFilesIntoPaths(baseDir, removed);
132 133 134
    cend = filesInPaths.constEnd();
    for (FilesInPathHashConstIt it = filesInPaths.constBegin(); it != cend; ++it) {
        const QString &filePath = it.key();
135 136 137
        QStringList components;
        if (!filePath.isEmpty())
            components = filePath.split(QLatin1Char('/'));
138 139 140
        FolderNode *folder = findFolderByName(components, components.size());

        QList<FileNode *> fileNodes;
141
        foreach (const QString &file, it.value()) {
142 143
            foreach (FileNode *fn, folder->fileNodes()) {
                if (fn->path().toString() == file)
144
                    fileNodes.append(fn);
145
            }
146 147
        }

148
        folder->removeFileNodes(fileNodes);
149 150 151 152 153
    }

    foreach (FolderNode *fn, subFolderNodes())
        removeEmptySubFolders(this, fn);

154 155
}

156
void GenericProjectNode::removeEmptySubFolders(FolderNode *gparent, FolderNode *parent)
157
{
158 159 160 161
    foreach (FolderNode *fn, parent->subFolderNodes())
        removeEmptySubFolders(parent, fn);

    if (parent->subFolderNodes().isEmpty() && parent->fileNodes().isEmpty())
162
        gparent->removeFolderNodes(QList<FolderNode*>() << parent);
163 164 165 166 167 168
}

FolderNode *GenericProjectNode::createFolderByName(const QStringList &components, int end)
{
    if (end == 0)
        return this;
169 170 171 172

    QString folderName;
    for (int i = 0; i < end; ++i) {
        folderName.append(components.at(i));
173
        folderName += QLatin1Char('/');
174 175
    }

Roberto Raggi's avatar
Roberto Raggi committed
176 177
    const QString component = components.at(end - 1);

178 179
    const Utils::FileName folderPath = path().parentDir().appendPath(folderName);
    FolderNode *folder = new FolderNode(folderPath);
180
    folder->setDisplayName(component);
Roberto Raggi's avatar
Roberto Raggi committed
181

182
    FolderNode *parent = findFolderByName(components, end - 1);
183
    if (!parent)
184
        parent = createFolderByName(components, end - 1);
185
    parent->addFolderNodes(QList<FolderNode*>() << folder);
186 187 188 189

    return folder;
}

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
FolderNode *GenericProjectNode::findFolderByName(const QStringList &components, int end)
{
    if (end == 0)
        return this;

    QString folderName;
    for (int i = 0; i < end; ++i) {
        folderName.append(components.at(i));
        folderName += QLatin1Char('/');
    }

    FolderNode *parent = findFolderByName(components, end - 1);

    if (!parent)
        return 0;

206 207 208
    const QString baseDir = path().toFileInfo().path();
    foreach (FolderNode *fn, parent->subFolderNodes()) {
        if (fn->path().toString() == baseDir + QLatin1Char('/') + folderName)
209
            return fn;
210
    }
211 212 213
    return 0;
}

214
bool GenericProjectNode::showInSimpleTree() const
Roberto Raggi's avatar
Roberto Raggi committed
215 216 217 218
{
    return true;
}

219
QList<ProjectAction> GenericProjectNode::supportedActions(Node *node) const
Roberto Raggi's avatar
Roberto Raggi committed
220
{
dt's avatar
dt committed
221
    Q_UNUSED(node);
222
    return QList<ProjectAction>()
dt's avatar
dt committed
223 224
        << AddNewFile
        << AddExistingFile
225
        << AddExistingDirectory
226 227
        << RemoveFile
        << Rename;
Roberto Raggi's avatar
Roberto Raggi committed
228 229
}

230 231 232 233 234 235
bool GenericProjectNode::canAddSubProject(const QString &proFilePath) const
{
    Q_UNUSED(proFilePath)
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
236 237
bool GenericProjectNode::addSubProjects(const QStringList &proFilePaths)
{
238
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
239 240 241 242 243
    return false;
}

bool GenericProjectNode::removeSubProjects(const QStringList &proFilePaths)
{
244
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
245 246 247
    return false;
}

248
bool GenericProjectNode::addFiles(const QStringList &filePaths, QStringList *notAdded)
Roberto Raggi's avatar
Roberto Raggi committed
249
{
250
    Q_UNUSED(notAdded)
251 252

    return m_project->addFiles(filePaths);
Roberto Raggi's avatar
Roberto Raggi committed
253 254
}

255
bool GenericProjectNode::removeFiles(const QStringList &filePaths, QStringList *notRemoved)
Roberto Raggi's avatar
Roberto Raggi committed
256
{
257
    Q_UNUSED(notRemoved)
258 259

    return m_project->removeFiles(filePaths);
Roberto Raggi's avatar
Roberto Raggi committed
260 261
}

262
bool GenericProjectNode::deleteFiles(const QStringList &filePaths)
263 264 265 266 267
{
    Q_UNUSED(filePaths)
    return false;
}

268
bool GenericProjectNode::renameFile(const QString &filePath, const QString &newFilePath)
Roberto Raggi's avatar
Roberto Raggi committed
269
{
270
    return m_project->renameFile(filePath, newFilePath);
Roberto Raggi's avatar
Roberto Raggi committed
271
}
dt's avatar
dt committed
272

hjk's avatar
hjk committed
273 274
} // namespace Internal
} // namespace GenericProjectManager