genericprojectnodes.cpp 7.55 KB
Newer Older
Roberto Raggi's avatar
Roberto Raggi committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 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
**
con's avatar
con committed
9
** No Commercial Usage
Roberto Raggi's avatar
Roberto Raggi committed
10
**
con's avatar
con committed
11 12 13 14
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
Roberto Raggi's avatar
Roberto Raggi committed
15 16 17 18 19 20 21 22 23 24
**
** GNU Lesser General Public License Usage
**
** 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.
**
con's avatar
con committed
25 26 27 28 29 30
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
Roberto Raggi's avatar
Roberto Raggi committed
31 32 33 34
**
**************************************************************************/

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

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

40
#include <QtCore/QFileInfo>
Roberto Raggi's avatar
Roberto Raggi committed
41 42 43 44

using namespace GenericProjectManager;
using namespace GenericProjectManager::Internal;

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

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

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

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

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

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

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

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

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

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

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

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

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

106 107 108 109 110 111
        if (absoluteFilePath.startsWith(base)) {
            relativeFilePath = absoluteFilePath.mid(base.length() + 1);
        } else {
            // `file' is not part of the project.
            relativeFilePath = baseDir.relativeFilePath(absoluteFilePath);
        }
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

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

        filesInPath[relativeFilePath].append(absoluteFileName);
    }

    foreach (const QString &filePath, filePaths) {
        FolderNode *folder = findOrCreateFolderByName(filePath);

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

132
    m_folderByName.clear();
133 134 135 136 137 138 139 140 141 142 143 144 145
}

ProjectExplorer::FolderNode *GenericProjectNode::findOrCreateFolderByName(const QStringList &components, int end)
{
    if (! end)
        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 = m_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
    m_folderByName.insert(folderName, folder);
Roberto Raggi's avatar
Roberto Raggi committed
158 159 160 161 162

    FolderNode *parent = findOrCreateFolderByName(components, end - 1);
    if (! parent)
        parent = this;
    addFolderNodes(QList<FolderNode*>() << folder, parent);
163 164 165 166 167 168 169 170 171 172

    return folder;
}

ProjectExplorer::FolderNode *GenericProjectNode::findOrCreateFolderByName(const QString &filePath)
{
    QStringList components = filePath.split(QLatin1Char('/'));
    return findOrCreateFolderByName(components, components.length());
}

173
bool GenericProjectNode::hasBuildTargets() const
Roberto Raggi's avatar
Roberto Raggi committed
174 175 176 177
{
    return true;
}

dt's avatar
dt committed
178
QList<ProjectExplorer::ProjectNode::ProjectAction> GenericProjectNode::supportedActions(Node *node) const
Roberto Raggi's avatar
Roberto Raggi committed
179
{
dt's avatar
dt committed
180
    Q_UNUSED(node);
181
    return QList<ProjectAction>()
dt's avatar
dt committed
182 183
        << AddNewFile
        << AddExistingFile
184
        << RemoveFile;
Roberto Raggi's avatar
Roberto Raggi committed
185 186
}

187 188 189 190 191 192
bool GenericProjectNode::canAddSubProject(const QString &proFilePath) const
{
    Q_UNUSED(proFilePath)
    return false;
}

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

bool GenericProjectNode::removeSubProjects(const QStringList &proFilePaths)
{
201
    Q_UNUSED(proFilePaths)
Roberto Raggi's avatar
Roberto Raggi committed
202 203 204 205 206 207
    return false;
}

bool GenericProjectNode::addFiles(const ProjectExplorer::FileType fileType,
                                  const QStringList &filePaths, QStringList *notAdded)
{
208 209
    Q_UNUSED(fileType)
    Q_UNUSED(notAdded)
210 211

    return m_project->addFiles(filePaths);
Roberto Raggi's avatar
Roberto Raggi committed
212 213 214 215 216
}

bool GenericProjectNode::removeFiles(const ProjectExplorer::FileType fileType,
                                     const QStringList &filePaths, QStringList *notRemoved)
{
217 218
    Q_UNUSED(fileType)
    Q_UNUSED(notRemoved)
219 220

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

223 224 225 226 227 228 229 230
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
231 232 233
bool GenericProjectNode::renameFile(const ProjectExplorer::FileType fileType,
                                    const QString &filePath, const QString &newFilePath)
{
234 235 236
    Q_UNUSED(fileType)
    Q_UNUSED(filePath)
    Q_UNUSED(newFilePath)
Roberto Raggi's avatar
Roberto Raggi committed
237 238
    return false;
}
dt's avatar
dt committed
239 240 241 242 243 244

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