genericprojectnodes.cpp 7.27 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 39 40 41

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;

42
GenericProjectNode::GenericProjectNode(GenericProject *project, Core::IDocument *projectFile)
43
    : ProjectExplorer::ProjectNode(projectFile->fileName()),
44 45
      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 66 67
void GenericProjectNode::refresh()
{
    using namespace ProjectExplorer;

    // remove the existing nodes.
    removeFileNodes(fileNodes(), this);
    removeFolderNodes(subFolderNodes(), this);

68
    //ProjectExplorerPlugin::instance()->setCurrentNode(0); // ### remove me
69

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

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

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

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

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

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

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

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

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

        filesInPath[relativeFilePath].append(absoluteFileName);
    }

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

        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);
    }
}

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

    if (component.isEmpty())
        return this;

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

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

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

    return folder;
}

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

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

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

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

bool GenericProjectNode::removeSubProjects(const QStringList &proFilePaths)
{
194
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
195 196 197 198 199 200
    return false;
}

bool GenericProjectNode::addFiles(const ProjectExplorer::FileType fileType,
                                  const QStringList &filePaths, QStringList *notAdded)
{
201 202
    Q_UNUSED(fileType)
    Q_UNUSED(notAdded)
203 204

    return m_project->addFiles(filePaths);
Roberto Raggi's avatar
Roberto Raggi committed
205 206 207 208 209
}

bool GenericProjectNode::removeFiles(const ProjectExplorer::FileType fileType,
                                     const QStringList &filePaths, QStringList *notRemoved)
{
210 211
    Q_UNUSED(fileType)
    Q_UNUSED(notRemoved)
212 213

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

216 217 218 219 220 221 222 223
bool GenericProjectNode::deleteFiles(const ProjectExplorer::FileType fileType,
                                     const QStringList &filePaths)
{
    Q_UNUSED(fileType)
    Q_UNUSED(filePaths)
    return false;
}

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

    return m_project->renameFile(filePath, newFilePath);
Roberto Raggi's avatar
Roberto Raggi committed
230
}
dt's avatar
dt committed
231 232 233 234 235 236

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