genericprojectnodes.cpp 7.14 KB
Newer Older
Roberto Raggi's avatar
Roberto Raggi committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
Roberto Raggi's avatar
Roberto Raggi committed
6
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
Roberto Raggi's avatar
Roberto Raggi committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Roberto Raggi's avatar
Roberto Raggi committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
Roberto Raggi's avatar
Roberto Raggi committed
28 29 30 31
**
**************************************************************************/

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

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

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

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

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

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

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

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

60 61 62 63 64 65
void GenericProjectNode::refresh()
{
    // remove the existing nodes.
    removeFileNodes(fileNodes(), this);
    removeFolderNodes(subFolderNodes(), this);

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

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

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

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

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

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

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

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

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

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

        filesInPath[relativeFilePath].append(absoluteFileName);
    }

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

        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
130
FolderNode *GenericProjectNode::findOrCreateFolderByName
131
    (FolderByName *folderByName, const QStringList &components, int end)
132
{
133
    if (!end)
134 135 136 137 138 139 140 141
        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
142 143 144 145 146
    const QString component = components.at(end - 1);

    if (component.isEmpty())
        return this;

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

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

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

    return folder;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

} // namespace Internal
} // namespace GenericProjectManager