aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/vcprojectmanager/vcprojectmodel/folder.cpp
blob: a5b9cc85d3241f5b3ceaf690c6be9843ef5ba6c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**************************************************************************
**
** Copyright (c) 2013 Bojan Petrovic
** Copyright (c) 2013 Radovan Zivkovic
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "folder.h"

#include <QFileInfo>

#include "vcprojectdocument.h"
#include "generalattributecontainer.h"
#include "../vcprojectmanagerconstants.h"

namespace VcProjectManager {
namespace Internal {

Folder::Folder(IVisualStudioProject *parentProjectDoc)
    : m_parentProjectDoc(parentProjectDoc)
{
    m_attributeContainer = new GeneralAttributeContainer;
}

Folder::Folder(const Folder &folder)
{
    m_parentProjectDoc = folder.m_parentProjectDoc;
    m_name = folder.m_name;
    m_attributeContainer = new GeneralAttributeContainer;
    *m_attributeContainer = *(folder.m_attributeContainer);

    foreach (IFile *file, folder.m_files)
        m_files.append(file->clone());

    foreach (IFileContainer *filter, folder.m_fileContainers)
        m_fileContainers.append(filter->clone());
}

Folder &Folder::operator =(const Folder &folder)
{
    if (this != &folder) {
        m_parentProjectDoc = folder.m_parentProjectDoc;
        m_name = folder.m_name;
        *m_attributeContainer = *(folder.m_attributeContainer);

        qDeleteAll(m_files);
        qDeleteAll(m_fileContainers);
        m_files.clear();
        m_fileContainers.clear();

        foreach (IFile *file, folder.m_files)
            m_files.append(file->clone());

        foreach (IFileContainer *filter, folder.m_fileContainers)
            m_fileContainers.append(filter->clone());
    }

    return *this;
}

Folder::~Folder()
{
}

QString Folder::containerType() const
{
    return QLatin1String(Constants::VC_PROJECT_FILE_CONTAINER_FOLDER);
}

void Folder::processNode(const QDomNode &node)
{
    if (node.isNull())
        return;

    if (node.nodeType() == QDomNode::ElementNode)
        processNodeAttributes(node.toElement());

    if (node.hasChildNodes()) {
        QDomNode firstChild = node.firstChild();
        if (!firstChild.isNull()) {
            if (firstChild.nodeName() == QLatin1String("Filter"))
                processFilter(firstChild);
            else if (firstChild.nodeName() == QLatin1String("File"))
                processFile(firstChild);
            else if (firstChild.nodeName() == QLatin1String("Folder"))
                processFolder(firstChild);
        }
    }
}

VcNodeWidget *Folder::createSettingsWidget()
{
    return 0;
}

QDomNode Folder::toXMLDomNode(QDomDocument &domXMLDocument) const
{
    QDomElement fileNode = domXMLDocument.createElement(QLatin1String("Folder"));

    fileNode.setAttribute(QLatin1String("Name"), m_name);

    m_attributeContainer->appendToXMLNode(fileNode);

    foreach (IFile *file, m_files)
        fileNode.appendChild(file->toXMLDomNode(domXMLDocument));

    foreach (IFileContainer *filter, m_fileContainers)
        fileNode.appendChild(filter->toXMLDomNode(domXMLDocument));

    return fileNode;
}

void Folder::addFile(IFile *file)
{
    if (m_files.contains(file))
        return;

    foreach (IFile *f, m_files) {
        if (f->relativePath() == file->relativePath())
            return;
    }
    m_files.append(file);
}

void Folder::removeFile(IFile *file)
{
    m_files.removeAll(file);
}

void Folder::removeFile(const QString &relativeFilePath)
{
    foreach (IFile *file, m_files) {
        if (file->relativePath() == relativeFilePath) {
            removeFile(file);
            return;
        }
    }
}

IFile *Folder::file(const QString &relativeFilePath) const
{
    foreach (IFile *file, m_files) {
        if (file->relativePath() == relativeFilePath)
            return file;
    }
    return 0;
}

IFile *Folder::file(int index) const
{
    if (0 <= index && index < m_files.size())
        return m_files[index];
    return 0;
}

int Folder::fileCount() const
{
    return m_files.size();
}

void Folder::addFileContainer(IFileContainer *fileContainer)
{
    if (!fileContainer && m_fileContainers.contains(fileContainer))
        return;

    m_fileContainers.append(fileContainer);
}

int Folder::childCount() const
{
    return m_fileContainers.size();
}

IFileContainer *Folder::fileContainer(int index) const
{
    if (0 <= index && index < m_fileContainers.size())
        return m_fileContainers[index];
    return 0;
}

void Folder::removeFileContainer(IFileContainer *fileContainer)
{
    m_fileContainers.removeAll(fileContainer);
}

IAttributeContainer *Folder::attributeContainer() const
{
    return m_attributeContainer;
}

bool Folder::fileExists(const QString &relativeFilePath) const
{
    foreach (IFile *filePtr, m_files) {
        if (filePtr->relativePath() == relativeFilePath)
            return true;
    }

    foreach (IFileContainer *filterPtr, m_fileContainers) {
        if (filterPtr->fileExists(relativeFilePath))
            return true;
    }

    return false;
}

QString Folder::name() const
{
    return m_name;
}

void Folder::setName(const QString &name)
{
    m_name = name;
}

void Folder::allFiles(QStringList &sl) const
{
    foreach (IFileContainer *filter, m_fileContainers)
        filter->allFiles(sl);

    foreach (IFile *file, m_files)
        sl.append(file->canonicalPath());
}

IFileContainer *Folder::clone() const
{
    return new Folder(*this);
}

void Folder::processFile(const QDomNode &fileNode)
{
    IFile *file = new File(m_parentProjectDoc);
    file->processNode(fileNode);
    m_files.append(file);

    // process next sibling
    QDomNode nextSibling = fileNode.nextSibling();
    if (!nextSibling.isNull()) {
        if (nextSibling.nodeName() == QLatin1String("File"))
            processFile(nextSibling);
        else if (nextSibling.nodeName() == QLatin1String("Folder"))
            processFolder(nextSibling);
        else
            processFilter(nextSibling);
    }
}

void Folder::processFilter(const QDomNode &filterNode)
{
    IFileContainer *filter = new Filter(m_parentProjectDoc);
    filter->processNode(filterNode);
    m_fileContainers.append(filter);

    // process next sibling
    QDomNode nextSibling = filterNode.nextSibling();
    if (!nextSibling.isNull()) {
        if (nextSibling.nodeName() == QLatin1String("File"))
            processFile(nextSibling);
        else if (nextSibling.nodeName() == QLatin1String("Folder"))
            processFolder(nextSibling);
        else
            processFilter(nextSibling);
    }
}

void Folder::processFolder(const QDomNode &folderNode)
{
    IFileContainer *folder = new Folder(m_parentProjectDoc);
    folder->processNode(folderNode);
    m_fileContainers.append(folder);

    // process next sibling
    QDomNode nextSibling = folderNode.nextSibling();
    if (!nextSibling.isNull()) {
        if (nextSibling.nodeName() == QLatin1String("File"))
            processFile(nextSibling);
        else if (nextSibling.nodeName() == QLatin1String("Folder"))
            processFolder(nextSibling);
        else
            processFilter(nextSibling);
    }
}

void Folder::processNodeAttributes(const QDomElement &element)
{
    QDomNamedNodeMap namedNodeMap = element.attributes();

    for (int i = 0; i < namedNodeMap.size(); ++i) {
        QDomNode domNode = namedNodeMap.item(i);

        if (domNode.nodeType() == QDomNode::AttributeNode) {
            QDomAttr domElement = domNode.toAttr();

            if (domElement.name() == QLatin1String("Name"))
                setName(domElement.value());

            else
                m_attributeContainer->setAttribute(domElement.name(), domElement.value());
        }
    }
}

} // namespace Internal
} // namespace VcProjectManager