qt4nodes.cpp 87.7 KB
Newer Older
hjk's avatar
hjk committed
1
/**************************************************************************
con's avatar
con committed
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).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
hjk's avatar
hjk committed
10
** GNU Lesser General Public License Usage
11
**
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.
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.
con's avatar
con committed
30
**
hjk's avatar
hjk committed
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33 34
#include "qt4nodes.h"
#include "qt4project.h"
35
#include "qt4target.h"
con's avatar
con committed
36
#include "qt4projectmanager.h"
37
#include "qt4projectmanagerconstants.h"
dt's avatar
dt committed
38
#include "qtuicodemodelsupport.h"
39
#include "qmakestep.h"
40
#include "qt4buildconfiguration.h"
con's avatar
con committed
41 42

#include <projectexplorer/nodesvisitor.h>
43
#include <projectexplorer/runconfiguration.h>
con's avatar
con committed
44 45

#include <coreplugin/editormanager/editormanager.h>
46
#include <coreplugin/editormanager/ieditor.h>
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
47
#include <coreplugin/fileiconprovider.h>
48
#include <coreplugin/documentmanager.h>
con's avatar
con committed
49 50 51 52
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>

53
#include <cpptools/ModelManagerInterface.h>
54
#include <cplusplus/CppDocument.h>
55
#include <extensionsystem/pluginmanager.h>
56 57
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/buildmanager.h>
58
#include <qtsupport/profilereader.h>
con's avatar
con committed
59

hjk's avatar
hjk committed
60
#include <utils/qtcassert.h>
61
#include <utils/stringutils.h>
62
#include <utils/fileutils.h>
63
#include <proparser/prowriter.h>
64
#include <algorithm>
hjk's avatar
hjk committed
65

66 67 68 69 70 71
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QCoreApplication>
#include <QXmlStreamReader>
hjk's avatar
hjk committed
72

73 74 75 76
#include <QPainter>
#include <QMainWindow>
#include <QMessageBox>
#include <QPushButton>
77
#include <utils/QtConcurrentTools>
con's avatar
con committed
78

79 80 81 82 83 84 85 86 87 88 89 90
// Static cached data in struct Qt4NodeStaticData providing information and icons
// for file types and the project. Do some magic via qAddPostRoutine()
// to make sure the icons do not outlive QApplication, triggering warnings on X11.

struct FileTypeDataStorage {
    ProjectExplorer::FileType type;
    const char *typeName;
    const char *icon;
};

static const FileTypeDataStorage fileTypeDataStorage[] = {
    { ProjectExplorer::HeaderType,
Daniel Teske's avatar
Daniel Teske committed
91
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Headers"),
92 93
      ":/qt4projectmanager/images/headers.png" },
    { ProjectExplorer::SourceType,
Daniel Teske's avatar
Daniel Teske committed
94
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Sources"),
95 96
      ":/qt4projectmanager/images/sources.png" },
    { ProjectExplorer::FormType,
Daniel Teske's avatar
Daniel Teske committed
97
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Forms"),
98 99
      ":/qt4projectmanager/images/forms.png" },
    { ProjectExplorer::ResourceType,
Daniel Teske's avatar
Daniel Teske committed
100
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Resources"),
101
      ":/qt4projectmanager/images/qt_qrc.png" },
102
    { ProjectExplorer::QMLType,
Daniel Teske's avatar
Daniel Teske committed
103
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "QML"),
dt's avatar
dt committed
104
      ":/qt4projectmanager/images/qml.ico" }, // TODO icon
105
    { ProjectExplorer::UnknownFileType,
Daniel Teske's avatar
Daniel Teske committed
106
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Other files"),
107 108 109
      ":/qt4projectmanager/images/unknown.png" }
};

110 111 112 113
class Qt4NodeStaticData {
public:
    class FileTypeData {
    public:
114 115 116 117
        FileTypeData(ProjectExplorer::FileType t = ProjectExplorer::UnknownFileType,
                     const QString &tN = QString(),
                     const QIcon &i = QIcon()) :
        type(t), typeName(tN), icon(i) { }
con's avatar
con committed
118

119 120 121 122 123
        ProjectExplorer::FileType type;
        QString typeName;
        QIcon icon;
    };

124 125
    Qt4NodeStaticData();

126 127 128 129 130 131
    QVector<FileTypeData> fileTypeData;
    QIcon projectIcon;
};

static void clearQt4NodeStaticData();

132 133
Qt4NodeStaticData::Qt4NodeStaticData()
{
134 135
    // File type data
    const unsigned count = sizeof(fileTypeDataStorage)/sizeof(FileTypeDataStorage);
136
    fileTypeData.reserve(count);
137 138 139 140

    // Overlay the SP_DirIcon with the custom icons
    const QSize desiredSize = QSize(16, 16);

Friedemann Kleint's avatar
Friedemann Kleint committed
141
    for (unsigned i = 0 ; i < count; ++i) {
142 143 144 145 146 147
        const QIcon overlayIcon = QIcon(QLatin1String(fileTypeDataStorage[i].icon));
        const QPixmap folderPixmap =
                Core::FileIconProvider::overlayIcon(QStyle::SP_DirIcon,
                                                    overlayIcon, desiredSize);
        QIcon folderIcon;
        folderIcon.addPixmap(folderPixmap);
Daniel Teske's avatar
Daniel Teske committed
148
        const QString desc = Qt4ProjectManager::Qt4PriFileNode::tr(fileTypeDataStorage[i].typeName);
149 150
        fileTypeData.push_back(Qt4NodeStaticData::FileTypeData(fileTypeDataStorage[i].type,
                                                               desc, folderIcon));
151 152 153 154 155 156
    }
    // Project icon
    const QIcon projectBaseIcon(QLatin1String(":/qt4projectmanager/images/qt_project.png"));
    const QPixmap projectPixmap = Core::FileIconProvider::overlayIcon(QStyle::SP_DirIcon,
                                                                      projectBaseIcon,
                                                                      desiredSize);
157
    projectIcon.addPixmap(projectPixmap);
158 159

    qAddPostRoutine(clearQt4NodeStaticData);
160 161 162
}

Q_GLOBAL_STATIC(Qt4NodeStaticData, qt4NodeStaticData)
163 164 165 166 167

static void clearQt4NodeStaticData()
{
    qt4NodeStaticData()->fileTypeData.clear();
    qt4NodeStaticData()->projectIcon = QIcon();
con's avatar
con committed
168 169
}

170 171
enum { debug = 0 };

Daniel Teske's avatar
Daniel Teske committed
172 173
using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
174

175
Qt4PriFile::Qt4PriFile(Qt4ProjectManager::Qt4PriFileNode *qt4PriFile)
176
    : IDocument(qt4PriFile), m_priFile(qt4PriFile)
177 178 179 180
{

}

181
bool Qt4PriFile::save(QString *errorString, const QString &fileName, bool autoSave)
182
{
183
    Q_UNUSED(errorString);
184
    Q_UNUSED(fileName);
185
    Q_UNUSED(autoSave);
186 187 188
    return false;
}

dt's avatar
dt committed
189 190 191 192 193 194 195
void Qt4PriFile::rename(const QString &newName)
{
    // Can't happen
    Q_ASSERT(false);
    Q_UNUSED(newName);
}

196 197 198 199 200 201 202
QString Qt4PriFile::fileName() const
{
    return m_priFile->path();
}

QString Qt4PriFile::defaultPath() const
{
203
    return QString();
204 205 206 207
}

QString Qt4PriFile::suggestedFileName() const
{
208
    return QString();
209 210 211 212
}

QString Qt4PriFile::mimeType() const
{
213
    return QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE);
214 215 216 217 218 219 220 221 222 223 224 225
}

bool Qt4PriFile::isModified() const
{
    return false;
}

bool Qt4PriFile::isSaveAsAllowed() const
{
    return false;
}

226
Core::IDocument::ReloadBehavior Qt4PriFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
227
{
228 229 230
    Q_UNUSED(state)
    Q_UNUSED(type)
    return BehaviorSilent;
231 232
}

233
bool Qt4PriFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
234
{
235
    Q_UNUSED(errorString)
236 237
    Q_UNUSED(flag)
    if (type == TypePermissions)
238
        return true;
239
    m_priFile->scheduleUpdate();
240
    return true;
241
}
242

con's avatar
con committed
243 244 245 246 247
/*!
  \class Qt4PriFileNode
  Implements abstract ProjectNode class
  */

248 249
namespace Qt4ProjectManager {

250
Qt4PriFileNode::Qt4PriFileNode(Qt4Project *project, Qt4ProFileNode* qt4ProFileNode, const QString &filePath)
con's avatar
con committed
251 252
        : ProjectNode(filePath),
          m_project(project),
253
          m_qt4ProFileNode(qt4ProFileNode),
254
          m_projectFilePath(QDir::fromNativeSeparators(filePath)),
255 256
          m_projectDir(QFileInfo(filePath).absolutePath()),
          m_includedInExactParse(true)
con's avatar
con committed
257
{
dt's avatar
dt committed
258
    Q_ASSERT(project);
259
    m_qt4PriFile = new Qt4PriFile(this);
260
    Core::DocumentManager::addDocument(m_qt4PriFile);
261

262
    setDisplayName(QFileInfo(filePath).completeBaseName());
Jens Bache-Wiig's avatar
Jens Bache-Wiig committed
263

264
    setIcon(qt4NodeStaticData()->projectIcon);
265 266
}

267 268 269 270 271
Qt4PriFileNode::~Qt4PriFileNode()
{
    watchFolders(QSet<QString>());
}

272 273
void Qt4PriFileNode::scheduleUpdate()
{
274
    QtSupport::ProFileCacheManager::instance()->discardFile(m_projectFilePath);
275
    m_qt4ProFileNode->scheduleUpdate();
con's avatar
con committed
276 277
}

Daniel Teske's avatar
Daniel Teske committed
278
namespace Internal {
279
struct InternalNode
280 281 282 283
{
    QMap<QString, InternalNode*> subnodes;
    QStringList files;
    ProjectExplorer::FileType type;
284
    QString displayName;
285 286
    QString fullPath;
    QIcon icon;
287

288 289 290 291
    InternalNode()
    {
        type = ProjectExplorer::UnknownFileType;
    }
292

293 294 295 296
    ~InternalNode()
    {
        qDeleteAll(subnodes);
    }
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    // Creates a tree structure from a list of absolute file paths.
    // Empty directories are compressed into a single entry with a longer path.
    // * project
    //    * /absolute/path
    //       * file1
    //    * relative
    //       * path1
    //          * file1
    //          * file2
    //       * path2
    //          * file1
    // The method first creates a tree that looks like the directory structure, i.e.
    //    * /
    //       * absolute
    //          * path
    // ...
    // and afterwards calls compress() which merges directory nodes with single children, i.e. to
    //    * /absolute/path
316
    void create(const QString &projectDir, const QSet<Utils::FileName> &newFilePaths, ProjectExplorer::FileType type)
317
    {
318
        static const QChar separator = QLatin1Char('/');
319
        const Utils::FileName projectDirFileName = Utils::FileName::fromString(projectDir);
320 321
        foreach (const Utils::FileName &file, newFilePaths) {
            Utils::FileName fileWithoutPrefix;
322
            bool isRelative;
323
            if (file.isChildOf(projectDirFileName)) {
324
                isRelative = true;
325
                fileWithoutPrefix = file.relativeChildPath(projectDirFileName);
326 327 328 329
            } else {
                isRelative = false;
                fileWithoutPrefix = file;
            }
330
            QStringList parts = fileWithoutPrefix.toString().split(separator, QString::SkipEmptyParts);
331
#ifndef Q_OS_WIN
332 333
            if (!isRelative && parts.count() > 0)
                parts[0].prepend(separator);
334
#endif
335 336
            QStringListIterator it(parts);
            InternalNode *currentNode = this;
337
            QString path = (isRelative ? (projectDirFileName.toString() + QLatin1Char('/')) : QString());
338 339 340 341
            while (it.hasNext()) {
                const QString &key = it.next();
                if (it.hasNext()) { // key is directory
                    path += key;
342
                    if (!currentNode->subnodes.contains(path)) {
343 344 345
                        InternalNode *val = new InternalNode;
                        val->type = type;
                        val->fullPath = path;
346 347
                        val->displayName = key;
                        currentNode->subnodes.insert(path, val);
348 349
                        currentNode = val;
                    } else {
350
                        currentNode = currentNode->subnodes.value(path);
351
                    }
352 353
                    path += separator;
                } else { // key is filename
354
                    currentNode->files.append(file.toString());
355 356 357
                }
            }
        }
358 359
        this->compress();
    }
360

361 362 363 364 365 366 367 368 369
    // Removes folder nodes with only a single sub folder in it
    void compress()
    {
        QMap<QString, InternalNode*> newSubnodes;
        QMapIterator<QString, InternalNode*> i(subnodes);
        while (i.hasNext()) {
            i.next();
            i.value()->compress();
            if (i.value()->files.isEmpty() && i.value()->subnodes.size() == 1) {
370
                // replace i.value() by i.value()->subnodes.begin()
371
                QString key = i.value()->subnodes.begin().key();
372
                InternalNode *keep = i.value()->subnodes.value(key);
373
                keep->displayName = i.value()->displayName + QLatin1Char('/') + keep->displayName;
374
                newSubnodes.insert(key, keep);
375 376 377 378
                i.value()->subnodes.clear();
                delete i.value();
            } else {
                newSubnodes.insert(i.key(), i.value());
379 380
            }
        }
381 382
        subnodes = newSubnodes;
    }
383

384
    // Makes the projectNode's subtree below the given folder match this internal node's subtree
385
    void updateSubFolders(Qt4ProjectManager::Qt4PriFileNode *projectNode, ProjectExplorer::FolderNode *folder)
386 387
    {
        updateFiles(projectNode, folder, type);
388

389 390 391 392 393 394
        // update folders
        QList<FolderNode *> existingFolderNodes;
        foreach (FolderNode *node, folder->subFolderNodes()) {
            if (node->nodeType() != ProjectNodeType)
                existingFolderNodes << node;
        }
395

396 397
        qSort(existingFolderNodes.begin(), existingFolderNodes.end(), ProjectNode::sortNodesByPath);

398 399 400 401 402
        QList<FolderNode *> foldersToRemove;
        QList<FolderNode *> foldersToAdd;
        typedef QPair<InternalNode *, FolderNode *> NodePair;
        QList<NodePair> nodesToUpdate;

403
        // Both lists should be already sorted...
404 405 406 407
        QList<FolderNode*>::const_iterator existingNodeIter = existingFolderNodes.constBegin();
        QMap<QString, InternalNode*>::const_iterator newNodeIter = subnodes.constBegin();;
        while (existingNodeIter != existingFolderNodes.constEnd()
               && newNodeIter != subnodes.constEnd()) {
408
            if ((*existingNodeIter)->path() < newNodeIter.value()->fullPath) {
409 410
                foldersToRemove << *existingNodeIter;
                ++existingNodeIter;
411
            } else if ((*existingNodeIter)->path() > newNodeIter.value()->fullPath) {
412
                FolderNode *newNode = new FolderNode(newNodeIter.value()->fullPath);
413
                newNode->setDisplayName(newNodeIter.value()->displayName);
414 415 416
                if (!newNodeIter.value()->icon.isNull())
                    newNode->setIcon(newNodeIter.value()->icon);
                foldersToAdd << newNode;
417
                nodesToUpdate << NodePair(newNodeIter.value(), newNode);
418
                ++newNodeIter;
419 420 421 422
            } else { // *existingNodeIter->path() == *newPathIter
                nodesToUpdate << NodePair(newNodeIter.value(), *existingNodeIter);
                ++existingNodeIter;
                ++newNodeIter;
423
            }
424 425 426 427 428 429 430 431
        }

        while (existingNodeIter != existingFolderNodes.constEnd()) {
            foldersToRemove << *existingNodeIter;
            ++existingNodeIter;
        }
        while (newNodeIter != subnodes.constEnd()) {
            FolderNode *newNode = new FolderNode(newNodeIter.value()->fullPath);
432
            newNode->setDisplayName(newNodeIter.value()->displayName);
433 434 435 436 437 438
            if (!newNodeIter.value()->icon.isNull())
                newNode->setIcon(newNodeIter.value()->icon);
            foldersToAdd << newNode;
            nodesToUpdate << NodePair(newNodeIter.value(), newNode);
            ++newNodeIter;
        }
439

440 441 442 443
        if (!foldersToRemove.isEmpty())
            projectNode->removeFolderNodes(foldersToRemove, folder);
        if (!foldersToAdd.isEmpty())
            projectNode->addFolderNodes(foldersToAdd, folder);
444

445 446 447 448 449
        foreach (const NodePair &np, nodesToUpdate)
            np.first->updateSubFolders(projectNode, np.second);
    }

    // Makes the folder's files match this internal node's file list
450
    void updateFiles(Qt4ProjectManager::Qt4PriFileNode *projectNode, FolderNode *folder, FileType type)
451 452 453 454 455
    {
        QList<FileNode*> existingFileNodes;
        foreach (FileNode *fileNode, folder->fileNodes()) {
            if (fileNode->fileType() == type && !fileNode->isGenerated())
                existingFileNodes << fileNode;
456 457
        }

458 459
        QList<FileNode*> filesToRemove;
        QList<FileNode*> filesToAdd;
460

461 462 463 464 465 466 467 468
        qSort(files);
        qSort(existingFileNodes.begin(), existingFileNodes.end(), ProjectNode::sortNodesByPath);

        QList<FileNode*>::const_iterator existingNodeIter = existingFileNodes.constBegin();
        QList<QString>::const_iterator newPathIter = files.constBegin();
        while (existingNodeIter != existingFileNodes.constEnd()
               && newPathIter != files.constEnd()) {
            if ((*existingNodeIter)->path() < *newPathIter) {
469 470
                filesToRemove << *existingNodeIter;
                ++existingNodeIter;
471
            } else if ((*existingNodeIter)->path() > *newPathIter) {
472
                filesToAdd << new ProjectExplorer::FileNode(*newPathIter, type, false);
473
                ++newPathIter;
474 475 476
            } else { // *existingNodeIter->path() == *newPathIter
                ++existingNodeIter;
                ++newPathIter;
477 478
            }
        }
479 480 481 482 483
        while (existingNodeIter != existingFileNodes.constEnd()) {
            filesToRemove << *existingNodeIter;
            ++existingNodeIter;
        }
        while (newPathIter != files.constEnd()) {
484
            filesToAdd << new ProjectExplorer::FileNode(*newPathIter, type, false);
485 486 487 488 489 490 491 492 493
            ++newPathIter;
        }

        if (!filesToRemove.isEmpty())
            projectNode->removeFileNodes(filesToRemove, folder);
        if (!filesToAdd.isEmpty())
            projectNode->addFileNodes(filesToAdd, folder);
    }
};
Daniel Teske's avatar
Daniel Teske committed
494
}
dt's avatar
dt committed
495

496
QStringList Qt4PriFileNode::baseVPaths(QtSupport::ProFileReader *reader, const QString &projectDir)
con's avatar
con committed
497
{
dt's avatar
dt committed
498 499 500
    QStringList result;
    if (!reader)
        return result;
501
    result += reader->absolutePathValues(QLatin1String("VPATH"), projectDir);
dt's avatar
dt committed
502
    result << projectDir; // QMAKE_ABSOLUTE_SOURCE_PATH
503
    result += reader->absolutePathValues(QLatin1String("DEPENDPATH"), projectDir);
dt's avatar
dt committed
504 505 506
    result.removeDuplicates();
    return result;
}
con's avatar
con committed
507

508
QStringList Qt4PriFileNode::fullVPaths(const QStringList &baseVPaths, QtSupport::ProFileReader *reader, FileType type, const QString &qmakeVariable, const QString &projectDir)
dt's avatar
dt committed
509 510 511 512 513
{
    QStringList vPaths;
    if (!reader)
        return vPaths;
    if (type == ProjectExplorer::SourceType)
514
        vPaths = reader->absolutePathValues(QLatin1String("VPATH_") + qmakeVariable, projectDir);
dt's avatar
dt committed
515 516
    vPaths += baseVPaths;
    if (type == ProjectExplorer::HeaderType)
517
        vPaths += reader->absolutePathValues(QLatin1String("INCLUDEPATH"), projectDir);
dt's avatar
dt committed
518 519 520 521
    vPaths.removeDuplicates();
    return vPaths;
}

522
static QSet<Utils::FileName> recursiveEnumerate(const QString &folder)
523
{
524
    QSet<Utils::FileName> result;
525 526 527 528 529 530
    QFileInfo fi(folder);
    if (fi.isDir()) {
        QDir dir(folder);
        dir.setFilter(dir.filter() | QDir::NoDotAndDotDot);

        foreach (const QFileInfo &file, dir.entryInfoList()) {
531
            if (file.isDir() && !file.isSymLink())
532 533
                result += recursiveEnumerate(file.absoluteFilePath());
            else
534
                result += Utils::FileName(file);
535 536
        }
    } else if (fi.exists()) {
537
        result << Utils::FileName(fi);
538 539 540
    }
    return result;
}
dt's avatar
dt committed
541

542
void Qt4PriFileNode::update(ProFile *includeFileExact, QtSupport::ProFileReader *readerExact, ProFile *includeFileCumlative, QtSupport::ProFileReader *readerCumulative)
dt's avatar
dt committed
543
{
con's avatar
con committed
544 545
    // add project file node
    if (m_fileNodes.isEmpty())
546
        addFileNodes(QList<FileNode*>() << new ProjectExplorer::FileNode(m_projectFilePath, ProjectExplorer::ProjectFileType, false), this);
con's avatar
con committed
547

548 549
    const QString &projectDir = m_qt4ProFileNode->m_projectDir;

550 551
    InternalNode contents;

552 553 554
    // Figure out DEPLOYMENT and INSTALL folders
    QStringList folders;
    QStringList dynamicVariables = dynamicVarNames(readerExact, readerCumulative);
555 556 557 558 559 560 561 562
    if (includeFileExact)
        foreach (const QString &dynamicVar, dynamicVariables) {
            folders += readerExact->values(dynamicVar, includeFileExact);
            // Ignore stuff from cumulative parse
            // we are recursively enumerating all the files from those folders
            // and add watchers for them, that's too dangerous if we get the foldrs
            // wrong and enumerate the whole project tree multiple times
        }
563 564 565


    for (int i=0; i < folders.size(); ++i) {
566
        const QFileInfo fi(folders.at(i));
567
        if (fi.isRelative())
568
            folders[i] = QDir::cleanPath(projectDir + QLatin1Char('/') + folders.at(i));
569 570
    }

dt's avatar
dt committed
571 572

    m_recursiveEnumerateFiles.clear();
573 574
    // Remove non existing items and non folders
    // todo fix files in INSTALL rules
dt's avatar
dt committed
575 576 577
    QStringList::iterator it = folders.begin();
    while (it != folders.end()) {
        QFileInfo fi(*it);
dt's avatar
dt committed
578 579 580 581 582 583
        if (fi.exists()) {
            if (fi.isDir()) {
                // keep directories
                ++it;
            } else {
                // move files directly to m_recursiveEnumerateFiles
584
                m_recursiveEnumerateFiles << Utils::FileName::fromString(*it);
dt's avatar
dt committed
585 586 587 588
                it = folders.erase(it);
            }
        } else {
            // do remove non exsting stuff
dt's avatar
dt committed
589
            it = folders.erase(it);
dt's avatar
dt committed
590
        }
dt's avatar
dt committed
591 592
    }

593 594 595 596 597 598
    folders.removeDuplicates();
    watchFolders(folders.toSet());

    foreach (const QString &folder, folders) {
        m_recursiveEnumerateFiles += recursiveEnumerate(folder);
    }
599
    QMap<FileType, QSet<Utils::FileName> > foundFiles;
dt's avatar
dt committed
600

601 602 603 604 605 606 607 608 609
    QStringList baseVPathsExact;
    if (includeFileExact)
        baseVPathsExact = baseVPaths(readerExact, projectDir);
    QStringList baseVPathsCumulative;
    if (includeFileCumlative)
        baseVPathsCumulative = baseVPaths(readerCumulative, projectDir);

    const QVector<Qt4NodeStaticData::FileTypeData> &fileTypes = qt4NodeStaticData()->fileTypeData;

con's avatar
con committed
610
    // update files
611
    for (int i = 0; i < fileTypes.size(); ++i) {
612
        FileType type = fileTypes.at(i).type;
613
        QStringList qmakeVariables = varNames(type);
con's avatar
con committed
614

615
        QSet<Utils::FileName> newFilePaths;
616
        foreach (const QString &qmakeVariable, qmakeVariables) {
617 618
            if (includeFileExact) {
                QStringList vPathsExact = fullVPaths(baseVPathsExact, readerExact, type, qmakeVariable, projectDir);
619 620 621
                QStringList tmp = readerExact->absoluteFileValues(qmakeVariable, projectDir, vPathsExact, includeFileExact);
                foreach (const QString &t, tmp)
                    newFilePaths += Utils::FileName::fromString(t);
622 623 624
            }
            if (includeFileCumlative) {
                QStringList vPathsCumulative = fullVPaths(baseVPathsCumulative, readerCumulative, type, qmakeVariable, projectDir);
625 626 627
                QStringList tmp = readerCumulative->absoluteFileValues(qmakeVariable, projectDir, vPathsCumulative, includeFileCumlative);
                foreach (const QString &t, tmp)
                    newFilePaths += Utils::FileName::fromString(t);
628
            }
629
        }
con's avatar
con committed
630

dt's avatar
dt committed
631 632 633 634 635 636
        foundFiles[type] = newFilePaths;
        m_recursiveEnumerateFiles.subtract(newFilePaths);
    }

    for (int i = 0; i < fileTypes.size(); ++i) {
        FileType type = fileTypes.at(i).type;
637
        QSet<Utils::FileName> newFilePaths = filterFilesProVariables(type, foundFiles[type]);
638
        newFilePaths += filterFilesRecursiveEnumerata(type, m_recursiveEnumerateFiles);
639 640 641 642 643 644 645 646

        // We only need to save this information if
        // we are watching folders
        if (!folders.isEmpty())
            m_files[type] = newFilePaths;
        else
            m_files[type].clear();

647 648 649
        if (!newFilePaths.isEmpty()) {
            InternalNode *subfolder = new InternalNode;
            subfolder->type = type;
650
            subfolder->icon = fileTypes.at(i).icon;
651 652
            subfolder->fullPath = m_projectDir + QLatin1String("/#")
                                  + QString::number(i) + fileTypes.at(i).typeName;
653 654
            subfolder->displayName = fileTypes.at(i).typeName;
            contents.subnodes.insert(subfolder->fullPath, subfolder);
655
            // create the hierarchy with subdirectories
656
            subfolder->create(m_projectDir, newFilePaths, type);
con's avatar
con committed
657 658
        }
    }
659 660 661 662 663 664 665 666 667 668 669 670 671

    contents.updateSubFolders(this, this);
}

void Qt4PriFileNode::watchFolders(const QSet<QString> &folders)
{
    QSet<QString> toUnwatch = m_watchedFolders;
    toUnwatch.subtract(folders);

    QSet<QString> toWatch = folders;
    toWatch.subtract(m_watchedFolders);

    if (!toUnwatch.isEmpty())
672
        m_project->unwatchFolders(toUnwatch.toList(), this);
673
    if (!toWatch.isEmpty())
674
        m_project->watchFolders(toWatch.toList(), this);
675 676 677 678

    m_watchedFolders = folders;
}

679
void Qt4PriFileNode::folderChanged(const QString &folder)
680 681 682 683
{
    //qDebug()<<"########## Qt4PriFileNode::folderChanged";
    // So, we need to figure out which files changed.

684 685 686 687
    QString changedFolder = folder;
    if (!changedFolder.endsWith(QLatin1Char('/')))
        changedFolder.append(QLatin1Char('/'));

688
    // Collect all the files
689
    QSet<Utils::FileName> newFiles;
690 691
    newFiles += recursiveEnumerate(changedFolder);

692
    foreach (const Utils::FileName &file, m_recursiveEnumerateFiles) {
693
        if (!file.isChildOf(Utils::FileName::fromString(changedFolder)))
694
            newFiles.insert(file);
695 696
    }

697
    QSet<Utils::FileName> addedFiles = newFiles;
698 699
    addedFiles.subtract(m_recursiveEnumerateFiles);

700
    QSet<Utils::FileName> removedFiles = m_recursiveEnumerateFiles;
701 702 703 704 705 706 707 708 709 710 711 712
    removedFiles.subtract(newFiles);

    if (addedFiles.isEmpty() && removedFiles.isEmpty())
        return;

    m_recursiveEnumerateFiles = newFiles;

    // Apply the differences
    // per file type
    const QVector<Qt4NodeStaticData::FileTypeData> &fileTypes = qt4NodeStaticData()->fileTypeData;
    for (int i = 0; i < fileTypes.size(); ++i) {
        FileType type = fileTypes.at(i).type;
713 714
        QSet<Utils::FileName> add = filterFilesRecursiveEnumerata(type, addedFiles);
        QSet<Utils::FileName> remove = filterFilesRecursiveEnumerata(type, removedFiles);
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734

        if (!add.isEmpty() || !remove.isEmpty()) {
            // Scream :)
//            qDebug()<<"For type"<<fileTypes.at(i).typeName<<"\n"
//                    <<"added files"<<add<<"\n"
//                    <<"removed files"<<remove;

            m_files[type].unite(add);
            m_files[type].subtract(remove);
        }
    }

    // Now apply stuff
    InternalNode contents;
    for (int i = 0; i < fileTypes.size(); ++i) {
        FileType type = fileTypes.at(i).type;
        if (!m_files[type].isEmpty()) {
            InternalNode *subfolder = new InternalNode;
            subfolder->type = type;
            subfolder->icon = fileTypes.at(i).icon;
735 736
            subfolder->fullPath = m_projectDir + QLatin1String("/#")
                                  + QString::number(i) + fileTypes.at(i).typeName;
737 738 739
            subfolder->displayName = fileTypes.at(i).typeName;
            contents.subnodes.insert(subfolder->fullPath, subfolder);
            // create the hierarchy with subdirectories
740
            subfolder->create(m_projectDir, m_files[type], type);
741 742 743
        }
    }

744
    contents.updateSubFolders(this, this);
745
    m_project->updateFileList();
746
    m_project->updateCodeModels();
747 748 749 750 751 752

    // The files to be packaged are listed inside the symbian build system.
    // We need to regenerate that list by running qmake
    // Other platforms do not have a explicit list of files to package, but package
    // directories
    foreach (ProjectExplorer::Target *target, m_project->targets()) {
753
        if (target->id() == QLatin1String(Constants::S60_DEVICE_TARGET_ID)) {
754 755 756 757 758 759 760 761 762 763 764
            foreach (ProjectExplorer::BuildConfiguration *bc, target->buildConfigurations()) {
                Qt4BuildConfiguration *qt4bc = qobject_cast<Qt4BuildConfiguration *>(bc);
                if (qt4bc) {
                    QMakeStep *qmakeStep = qt4bc->qmakeStep();
                    if (qmakeStep)
                        qmakeStep->setForced(true);
                }
            }
        }
    }

con's avatar
con committed
765 766
}

dt's avatar
dt committed
767 768 769
bool Qt4PriFileNode::deploysFolder(const QString &folder) const
{
    QString f = folder;
770 771 772
    const QChar slash = QLatin1Char('/');
    if (!f.endsWith(slash))
        f.append(slash);
dt's avatar
dt committed
773 774
    foreach (const QString &wf, m_watchedFolders) {
        if (f.startsWith(wf)
775 776
            && (wf.endsWith(slash)
                || (wf.length() < f.length() && f.at(wf.length()) == slash)))
dt's avatar
dt committed
777 778 779 780 781
            return true;
    }
    return false;
}

dt's avatar
dt committed
782 783
QList<ProjectExplorer::RunConfiguration *> Qt4PriFileNode::runConfigurationsFor(Node *node)
{
784 785 786 787
    Qt4BaseTarget *target = m_project->activeTarget();
    if (target)
        return target->runConfigurationsForNode(node);
    return QList<ProjectExplorer::RunConfiguration *>();
dt's avatar
dt committed
788 789
}

790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
QList<Qt4PriFileNode *> Qt4PriFileNode::subProjectNodesExact() const
{
    QList<Qt4PriFileNode *> nodes;
    foreach (ProjectNode *node, subProjectNodes()) {
        Qt4PriFileNode *n = qobject_cast<Qt4PriFileNode *>(node);
        if (n && n->includedInExactParse())
            nodes << n;
    }
    return nodes;
}

bool Qt4PriFileNode::includedInExactParse() const
{
    return m_includedInExactParse;
}

void Qt4PriFileNode::setIncludedInExactParse(bool b)
{
    m_includedInExactParse = b;
}

dt's avatar
dt committed
811
QList<ProjectNode::ProjectAction> Qt4PriFileNode::supportedActions(Node *node) const
con's avatar
con committed
812 813
{
    QList<ProjectAction> actions;
814 815 816 817 818

    const FolderNode *folderNode = this;
    const Qt4ProFileNode *proFileNode;
    while (!(proFileNode = qobject_cast<const Qt4ProFileNode*>(folderNode)))
        folderNode = folderNode->parentFolderNode();
dt's avatar
dt committed
819
    Q_ASSERT(proFileNode);
820 821 822

    switch (proFileNode->projectType()) {
    case ApplicationTemplate:
823 824 825 826 827 828
    case LibraryTemplate:
    case AuxTemplate: {
        // TODO: Some of the file types don't make much sense for aux
        // projects (e.g. cpp). It'd be nice if the "add" action could
        // work on a subset of the file types according to project type.

dt's avatar
dt committed
829
        actions << AddNewFile;
830
        if (m_recursiveEnumerateFiles.contains(Utils::FileName::fromString(node->path()))) {
dt's avatar
dt committed
831
            actions << EraseFile;
dt's avatar
dt committed
832
        } else {
833
            actions << RemoveFile;
dt's avatar
dt committed
834 835
        }

836
        bool addExistingFiles = true;
837
        if (node->path().contains(QLatin1Char('#'))) {
838 839 840 841 842
            // A virtual folder, we do what the projectexplorer does
            FolderNode *folder = qobject_cast<FolderNode *>(node);
            if (folder) {
                QStringList list;
                foreach (FolderNode *f, folder->subFolderNodes())
843
                    list << f->path() + QLatin1Char('/');
844 845 846 847 848
                if (deploysFolder(Utils::commonPath(list)))
                    addExistingFiles = false;
            }
        }

dt's avatar
dt committed
849
        addExistingFiles = addExistingFiles && !deploysFolder(node->path());
850 851

        if (addExistingFiles)
dt's avatar
dt committed
852 853
            actions << AddExistingFile;

854
        break;
855
    }
856 857 858 859 860
    case SubDirsTemplate:
        actions << AddSubProject << RemoveSubProject;
        break;
    default:
        break;
con's avatar
con committed
861
    }
dt's avatar
dt committed
862

863
    ProjectExplorer::FileNode *fileNode = qobject_cast<FileNode *>(node);
dt's avatar
dt committed