genericprojectnodes.cpp 7.37 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
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
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.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
Roberto Raggi's avatar
Roberto Raggi committed
30 31 32 33
**
**************************************************************************/

#include "genericprojectnodes.h"
Roberto Raggi's avatar
Roberto Raggi committed
34 35
#include "genericproject.h"

36
#include <coreplugin/ifile.h>
37 38
#include <projectexplorer/projectexplorer.h>

39
#include <QFileInfo>
Roberto Raggi's avatar
Roberto Raggi committed
40 41 42 43

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;

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

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

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

62 63 64 65 66 67 68 69
void GenericProjectNode::refresh()
{
    using namespace ProjectExplorer;

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

70
    //ProjectExplorerPlugin::instance()->setCurrentNode(0); // ### remove me
71

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

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

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

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

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

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

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

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

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

        filesInPath[relativeFilePath].append(absoluteFileName);
    }

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

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

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

    if (component.isEmpty())
        return this;

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

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

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

    return folder;
}

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

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

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

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

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

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

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

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

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

218 219 220 221 222 223 224 225
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
226 227 228
bool GenericProjectNode::renameFile(const ProjectExplorer::FileType fileType,
                                    const QString &filePath, const QString &newFilePath)
{
229
    Q_UNUSED(fileType)
230 231

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

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