genericprojectnodes.cpp 7.41 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Roberto Raggi's avatar
Roberto Raggi committed
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** 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 60 61 62 63 64
void GenericProjectNode::refresh()
{
    // remove the existing nodes.
    removeFileNodes(fileNodes(), this);
    removeFolderNodes(subFolderNodes(), this);

65
    //ProjectExplorerPlugin::instance()->setCurrentNode(0); // ### remove me
66

67
    FileNode *projectFilesNode = new FileNode(m_project->filesFileName(),
68 69 70
                                              ProjectFileType,
                                              /* generated = */ false);

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

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

79 80 81 82
    QStringList files = m_project->files();
    files.removeAll(m_project->filesFileName());
    files.removeAll(m_project->includesFileName());
    files.removeAll(m_project->configFileName());
83

84 85 86 87 88
    addFileNodes(QList<FileNode *>()
                 << projectFilesNode
                 << projectIncludesNode
                 << projectConfigNode,
                 this);
89 90 91

    QStringList filePaths;
    QHash<QString, QStringList> filesInPath;
92 93
    const QString base = QFileInfo(path()).absolutePath();
    const QDir baseDir(base);
94

95
    foreach (const QString &absoluteFileName, files) {
96 97
        QFileInfo fileInfo(absoluteFileName);
        const QString absoluteFilePath = fileInfo.path();
98
        QString relativeFilePath;
99

100 101 102 103 104 105
        if (absoluteFilePath.startsWith(base)) {
            relativeFilePath = absoluteFilePath.mid(base.length() + 1);
        } else {
            // `file' is not part of the project.
            relativeFilePath = baseDir.relativeFilePath(absoluteFilePath);
        }
106 107 108 109 110 111 112

        if (! filePaths.contains(relativeFilePath))
            filePaths.append(relativeFilePath);

        filesInPath[relativeFilePath].append(absoluteFileName);
    }

113
    FolderByName folderByName;
114
    foreach (const QString &filePath, filePaths) {
115 116
        QStringList components = filePath.split(QLatin1Char('/'));
        FolderNode *folder = findOrCreateFolderByName(&folderByName, components, components.size());
117 118 119 120 121 122 123 124 125 126 127 128

        QList<FileNode *> fileNodes;
        foreach (const QString &file, filesInPath.value(filePath)) {
            FileType fileType = SourceType; // ### FIXME
            FileNode *fileNode = new FileNode(file, fileType, /*generated = */ false);
            fileNodes.append(fileNode);
        }

        addFileNodes(fileNodes, folder);
    }
}

hjk's avatar
hjk committed
129
FolderNode *GenericProjectNode::findOrCreateFolderByName
130
    (FolderByName *folderByName, const QStringList &components, int end)
131
{
132
    if (!end)
133 134 135 136 137 138 139 140
        return 0;

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

Roberto Raggi's avatar
Roberto Raggi committed
141 142 143 144 145
    const QString component = components.at(end - 1);

    if (component.isEmpty())
        return this;

146
    else if (FolderNode *folder = folderByName->value(folderName))
Roberto Raggi's avatar
Roberto Raggi committed
147 148
        return folder;

149
    const QString baseDir = QFileInfo(path()).path();
150
    FolderNode *folder = new FolderNode(baseDir + QLatin1Char('/') + folderName);
151
    folder->setDisplayName(component);
152
    folderByName->insert(folderName, folder);
Roberto Raggi's avatar
Roberto Raggi committed
153

154 155
    FolderNode *parent = findOrCreateFolderByName(folderByName, components, end - 1);
    if (!parent)
Roberto Raggi's avatar
Roberto Raggi committed
156 157
        parent = this;
    addFolderNodes(QList<FolderNode*>() << folder, parent);
158 159 160 161

    return folder;
}

162
bool GenericProjectNode::hasBuildTargets() const
Roberto Raggi's avatar
Roberto Raggi committed
163 164 165 166
{
    return true;
}

hjk's avatar
hjk committed
167
QList<ProjectNode::ProjectAction> GenericProjectNode::supportedActions(Node *node) const
Roberto Raggi's avatar
Roberto Raggi committed
168
{
dt's avatar
dt committed
169
    Q_UNUSED(node);
170
    return QList<ProjectAction>()
dt's avatar
dt committed
171 172
        << AddNewFile
        << AddExistingFile
173 174
        << RemoveFile
        << Rename;
Roberto Raggi's avatar
Roberto Raggi committed
175 176
}

177 178 179 180 181 182
bool GenericProjectNode::canAddSubProject(const QString &proFilePath) const
{
    Q_UNUSED(proFilePath)
    return false;
}

Roberto Raggi's avatar
Roberto Raggi committed
183 184
bool GenericProjectNode::addSubProjects(const QStringList &proFilePaths)
{
185
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
186 187 188 189 190
    return false;
}

bool GenericProjectNode::removeSubProjects(const QStringList &proFilePaths)
{
191
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
192 193 194
    return false;
}

hjk's avatar
hjk committed
195
bool GenericProjectNode::addFiles(const FileType fileType,
Roberto Raggi's avatar
Roberto Raggi committed
196 197
                                  const QStringList &filePaths, QStringList *notAdded)
{
198 199
    Q_UNUSED(fileType)
    Q_UNUSED(notAdded)
200 201

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

hjk's avatar
hjk committed
204
bool GenericProjectNode::removeFiles(const FileType fileType,
Roberto Raggi's avatar
Roberto Raggi committed
205 206
                                     const QStringList &filePaths, QStringList *notRemoved)
{
207 208
    Q_UNUSED(fileType)
    Q_UNUSED(notRemoved)
209 210

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

hjk's avatar
hjk committed
213
bool GenericProjectNode::deleteFiles(const FileType fileType,
214 215 216 217 218 219 220
                                     const QStringList &filePaths)
{
    Q_UNUSED(fileType)
    Q_UNUSED(filePaths)
    return false;
}

hjk's avatar
hjk committed
221
bool GenericProjectNode::renameFile(const FileType fileType,
Roberto Raggi's avatar
Roberto Raggi committed
222 223
                                    const QString &filePath, const QString &newFilePath)
{
224
    Q_UNUSED(fileType)
225 226

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

hjk's avatar
hjk committed
229
QList<RunConfiguration *> GenericProjectNode::runConfigurationsFor(Node *node)
dt's avatar
dt committed
230 231
{
    Q_UNUSED(node)
hjk's avatar
hjk committed
232
    return QList<RunConfiguration *>();
dt's avatar
dt committed
233
}
hjk's avatar
hjk committed
234 235 236

} // namespace Internal
} // namespace GenericProjectManager