genericprojectnodes.cpp 7.41 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>

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

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 54 55 56

GenericProjectNode::~GenericProjectNode()
{ }

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

QString GenericProjectNode::projectFilePath() const
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 118 119 120 121 122 123 124 125 126 127 128 129

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

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

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
145 146 147 148 149
    const QString component = components.at(end - 1);

    if (component.isEmpty())
        return this;

150
    else if (FolderNode *folder = m_folderByName.value(folderName))
Roberto Raggi's avatar
Roberto Raggi committed
151 152
        return folder;

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

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

    return folder;
}

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

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

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

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

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

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

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

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

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

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

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