genericprojectnodes.cpp 9.78 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Roberto Raggi's avatar
Roberto Raggi committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
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 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Roberto Raggi's avatar
Roberto Raggi committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Roberto Raggi's avatar
Roberto Raggi committed
29 30

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

33
#include <coreplugin/idocument.h>
34 35
#include <projectexplorer/projectexplorer.h>

36
#include <QFileInfo>
Roberto Raggi's avatar
Roberto Raggi committed
37

hjk's avatar
hjk committed
38 39 40 41
using namespace ProjectExplorer;

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

43
GenericProjectNode::GenericProjectNode(GenericProject *project, Core::IDocument *projectFile)
hjk's avatar
hjk committed
44
    : ProjectNode(projectFile->fileName()), m_project(project), m_projectFile(projectFile)
45
{
46
    setDisplayName(QFileInfo(projectFile->fileName()).completeBaseName());
47
}
Roberto Raggi's avatar
Roberto Raggi committed
48

49
Core::IDocument *GenericProjectNode::projectFile() const
hjk's avatar
hjk committed
50 51 52
{
    return m_projectFile;
}
Roberto Raggi's avatar
Roberto Raggi committed
53 54

QString GenericProjectNode::projectFilePath() const
hjk's avatar
hjk committed
55 56 57
{
    return m_projectFile->fileName();
}
Roberto Raggi's avatar
Roberto Raggi committed
58

59
QHash<QString, QStringList> sortFilesIntoPaths(const QString &base, const QSet<QString> files)
60 61
{
    QHash<QString, QStringList> filesInPath;
62
    const QDir baseDir(base);
63

64
    foreach (const QString &absoluteFileName, files) {
65
        QFileInfo fileInfo(absoluteFileName);
66
        Utils::FileName absoluteFilePath = Utils::FileName::fromString(fileInfo.path());
67
        QString relativeFilePath;
68

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

        filesInPath[relativeFilePath].append(absoluteFileName);
    }
80 81 82 83 84
    return filesInPath;
}

void GenericProjectNode::refresh(QSet<QString> oldFileList)
{
85 86 87
    typedef QHash<QString, QStringList> FilesInPathHash;
    typedef FilesInPathHash::ConstIterator FilesInPathHashConstIt;

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    if (oldFileList.isEmpty()) {
        // Only do this once
        FileNode *projectFilesNode = new FileNode(m_project->filesFileName(),
                                                  ProjectFileType,
                                                  /* generated = */ false);

        FileNode *projectIncludesNode = new FileNode(m_project->includesFileName(),
                                                     ProjectFileType,
                                                     /* generated = */ false);

        FileNode *projectConfigNode = new FileNode(m_project->configFileName(),
                                                   ProjectFileType,
                                                   /* generated = */ false);

        addFileNodes(QList<FileNode *>()
                     << projectFilesNode
                     << projectIncludesNode
                     << projectConfigNode,
                     this);
    }
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    // 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);

    QString baseDir = QFileInfo(path()).absolutePath();
125 126 127 128 129
    FilesInPathHash filesInPaths = sortFilesIntoPaths(baseDir, added);

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

        QList<FileNode *> fileNodes;
138
        foreach (const QString &file, it.value()) {
139 140 141 142 143 144 145
            FileType fileType = SourceType; // ### FIXME
            FileNode *fileNode = new FileNode(file, fileType, /*generated = */ false);
            fileNodes.append(fileNode);
        }

        addFileNodes(fileNodes, folder);
    }
146 147

    filesInPaths = sortFilesIntoPaths(baseDir, removed);
148 149 150
    cend = filesInPaths.constEnd();
    for (FilesInPathHashConstIt it = filesInPaths.constBegin(); it != cend; ++it) {
        const QString &filePath = it.key();
151 152 153
        QStringList components;
        if (!filePath.isEmpty())
            components = filePath.split(QLatin1Char('/'));
154 155 156
        FolderNode *folder = findFolderByName(components, components.size());

        QList<FileNode *> fileNodes;
157
        foreach (const QString &file, it.value()) {
158 159 160 161 162 163 164 165 166 167 168
            foreach (FileNode *fn, folder->fileNodes())
                if (fn->path() == file)
                    fileNodes.append(fn);
        }

        removeFileNodes(fileNodes, folder);
    }

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

169 170
}

171
void GenericProjectNode::removeEmptySubFolders(FolderNode *gparent, FolderNode *parent)
172
{
173 174 175 176 177 178 179 180 181 182 183
    foreach (FolderNode *fn, parent->subFolderNodes())
        removeEmptySubFolders(parent, fn);

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

FolderNode *GenericProjectNode::createFolderByName(const QStringList &components, int end)
{
    if (end == 0)
        return this;
184 185 186 187

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

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

193
    const QString baseDir = QFileInfo(path()).path();
194
    FolderNode *folder = new FolderNode(baseDir + QLatin1Char('/') + folderName);
195
    folder->setDisplayName(component);
Roberto Raggi's avatar
Roberto Raggi committed
196

197
    FolderNode *parent = findFolderByName(components, end - 1);
198
    if (!parent)
199
        parent = createFolderByName(components, end - 1);
Roberto Raggi's avatar
Roberto Raggi committed
200
    addFolderNodes(QList<FolderNode*>() << folder, parent);
201 202 203 204

    return folder;
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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;

    const QString baseDir = QFileInfo(path()).path();
    foreach (FolderNode *fn, parent->subFolderNodes())
        if (fn->path() == baseDir + QLatin1Char('/') + folderName)
            return fn;
    return 0;
}

228
bool GenericProjectNode::hasBuildTargets() const
Roberto Raggi's avatar
Roberto Raggi committed
229 230 231 232
{
    return true;
}

hjk's avatar
hjk committed
233
QList<ProjectNode::ProjectAction> GenericProjectNode::supportedActions(Node *node) const
Roberto Raggi's avatar
Roberto Raggi committed
234
{
dt's avatar
dt committed
235
    Q_UNUSED(node);
236
    return QList<ProjectAction>()
dt's avatar
dt committed
237 238
        << AddNewFile
        << AddExistingFile
239 240
        << RemoveFile
        << Rename;
Roberto Raggi's avatar
Roberto Raggi committed
241 242
}

243 244 245 246 247 248
bool GenericProjectNode::canAddSubProject(const QString &proFilePath) const
{
    Q_UNUSED(proFilePath)
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
249 250
bool GenericProjectNode::addSubProjects(const QStringList &proFilePaths)
{
251
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
252 253 254 255 256
    return false;
}

bool GenericProjectNode::removeSubProjects(const QStringList &proFilePaths)
{
257
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
258 259 260
    return false;
}

hjk's avatar
hjk committed
261
bool GenericProjectNode::addFiles(const FileType fileType,
Roberto Raggi's avatar
Roberto Raggi committed
262 263
                                  const QStringList &filePaths, QStringList *notAdded)
{
264 265
    Q_UNUSED(fileType)
    Q_UNUSED(notAdded)
266 267

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

hjk's avatar
hjk committed
270
bool GenericProjectNode::removeFiles(const FileType fileType,
Roberto Raggi's avatar
Roberto Raggi committed
271 272
                                     const QStringList &filePaths, QStringList *notRemoved)
{
273 274
    Q_UNUSED(fileType)
    Q_UNUSED(notRemoved)
275 276

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

hjk's avatar
hjk committed
279
bool GenericProjectNode::deleteFiles(const FileType fileType,
280 281 282 283 284 285 286
                                     const QStringList &filePaths)
{
    Q_UNUSED(fileType)
    Q_UNUSED(filePaths)
    return false;
}

hjk's avatar
hjk committed
287
bool GenericProjectNode::renameFile(const FileType fileType,
Roberto Raggi's avatar
Roberto Raggi committed
288 289
                                    const QString &filePath, const QString &newFilePath)
{
290
    Q_UNUSED(fileType)
291 292

    return m_project->renameFile(filePath, newFilePath);
Roberto Raggi's avatar
Roberto Raggi committed
293
}
dt's avatar
dt committed
294

hjk's avatar
hjk committed
295
QList<RunConfiguration *> GenericProjectNode::runConfigurationsFor(Node *node)
dt's avatar
dt committed
296 297
{
    Q_UNUSED(node)
hjk's avatar
hjk committed
298
    return QList<RunConfiguration *>();
dt's avatar
dt committed
299
}
hjk's avatar
hjk committed
300 301 302

} // namespace Internal
} // namespace GenericProjectManager