qt4nodes.cpp 92.9 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 35
#include "qt4nodes.h"
#include "qt4project.h"
#include "qt4projectmanager.h"
36
#include "qt4projectmanagerconstants.h"
dt's avatar
dt committed
37
#include "qtuicodemodelsupport.h"
38
#include "qmakestep.h"
39
#include "qt4buildconfiguration.h"
Tobias Hunger's avatar
Tobias Hunger committed
40
#include "qmakerunconfigurationfactory.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
#include <projectexplorer/buildmanager.h>
Tobias Hunger's avatar
Tobias Hunger committed
57 58
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/target.h>
59
#include <qtsupport/profilereader.h>
Tobias Hunger's avatar
Tobias Hunger committed
60 61
#include <qtsupport/qtprofileinformation.h>
#include <qtsupport/qtsupportconstants.h>
con's avatar
con committed
62

hjk's avatar
hjk committed
63
#include <utils/qtcassert.h>
64
#include <utils/stringutils.h>
65
#include <utils/fileutils.h>
66
#include <proparser/prowriter.h>
67
#include <algorithm>
hjk's avatar
hjk committed
68

69 70 71 72 73 74
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QCoreApplication>
#include <QXmlStreamReader>
hjk's avatar
hjk committed
75

76 77 78 79
#include <QPainter>
#include <QMainWindow>
#include <QMessageBox>
#include <QPushButton>
80
#include <utils/QtConcurrentTools>
con's avatar
con committed
81

82 83 84 85 86 87 88 89 90 91 92 93
// 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
94
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Headers"),
95 96
      ":/qt4projectmanager/images/headers.png" },
    { ProjectExplorer::SourceType,
Daniel Teske's avatar
Daniel Teske committed
97
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Sources"),
98 99
      ":/qt4projectmanager/images/sources.png" },
    { ProjectExplorer::FormType,
Daniel Teske's avatar
Daniel Teske committed
100
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Forms"),
101 102
      ":/qt4projectmanager/images/forms.png" },
    { ProjectExplorer::ResourceType,
Daniel Teske's avatar
Daniel Teske committed
103
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Resources"),
104
      ":/qt4projectmanager/images/qt_qrc.png" },
105
    { ProjectExplorer::QMLType,
Daniel Teske's avatar
Daniel Teske committed
106
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "QML"),
107
      ":/qt4projectmanager/images/qml.ico" },
108
    { ProjectExplorer::UnknownFileType,
Daniel Teske's avatar
Daniel Teske committed
109
      QT_TRANSLATE_NOOP("Qt4ProjectManager::Qt4PriFileNode", "Other files"),
110 111 112
      ":/qt4projectmanager/images/unknown.png" }
};

113 114 115 116 117
bool sortNodesByPath(ProjectExplorer::Node *a, ProjectExplorer::Node *b)
{
    return a->path() < b->path();
}

118 119 120 121
class Qt4NodeStaticData {
public:
    class FileTypeData {
    public:
122 123 124 125
        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
126

127 128 129 130 131
        ProjectExplorer::FileType type;
        QString typeName;
        QIcon icon;
    };

132 133
    Qt4NodeStaticData();

134 135 136 137 138 139
    QVector<FileTypeData> fileTypeData;
    QIcon projectIcon;
};

static void clearQt4NodeStaticData();

140 141
Qt4NodeStaticData::Qt4NodeStaticData()
{
142 143
    // File type data
    const unsigned count = sizeof(fileTypeDataStorage)/sizeof(FileTypeDataStorage);
144
    fileTypeData.reserve(count);
145 146 147 148

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

Friedemann Kleint's avatar
Friedemann Kleint committed
149
    for (unsigned i = 0 ; i < count; ++i) {
150 151 152 153 154 155
        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
156
        const QString desc = Qt4ProjectManager::Qt4PriFileNode::tr(fileTypeDataStorage[i].typeName);
157 158
        fileTypeData.push_back(Qt4NodeStaticData::FileTypeData(fileTypeDataStorage[i].type,
                                                               desc, folderIcon));
159 160 161 162 163 164
    }
    // Project icon
    const QIcon projectBaseIcon(QLatin1String(":/qt4projectmanager/images/qt_project.png"));
    const QPixmap projectPixmap = Core::FileIconProvider::overlayIcon(QStyle::SP_DirIcon,
                                                                      projectBaseIcon,
                                                                      desiredSize);
165
    projectIcon.addPixmap(projectPixmap);
166 167

    qAddPostRoutine(clearQt4NodeStaticData);
168 169 170
}

Q_GLOBAL_STATIC(Qt4NodeStaticData, qt4NodeStaticData)
171 172 173 174 175

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

178 179
enum { debug = 0 };

Daniel Teske's avatar
Daniel Teske committed
180 181
using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
182

183
Qt4PriFile::Qt4PriFile(Qt4ProjectManager::Qt4PriFileNode *qt4PriFile)
184
    : IDocument(qt4PriFile), m_priFile(qt4PriFile)
185 186 187 188
{

}

189
bool Qt4PriFile::save(QString *errorString, const QString &fileName, bool autoSave)
190
{
191
    Q_UNUSED(errorString);
192
    Q_UNUSED(fileName);
193
    Q_UNUSED(autoSave);
194 195 196
    return false;
}

dt's avatar
dt committed
197 198 199 200 201 202 203
void Qt4PriFile::rename(const QString &newName)
{
    // Can't happen
    Q_ASSERT(false);
    Q_UNUSED(newName);
}

204 205 206 207 208 209 210
QString Qt4PriFile::fileName() const
{
    return m_priFile->path();
}

QString Qt4PriFile::defaultPath() const
{
211
    return QString();
212 213 214 215
}

QString Qt4PriFile::suggestedFileName() const
{
216
    return QString();
217 218 219 220
}

QString Qt4PriFile::mimeType() const
{
221
    return QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE);
222 223 224 225 226 227 228 229 230 231 232 233
}

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

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

234
Core::IDocument::ReloadBehavior Qt4PriFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
235
{
236 237 238
    Q_UNUSED(state)
    Q_UNUSED(type)
    return BehaviorSilent;
239 240
}

241
bool Qt4PriFile::reload(QString *errorString, ReloadFlag flag, ChangeType type)
242
{
243
    Q_UNUSED(errorString)
244 245
    Q_UNUSED(flag)
    if (type == TypePermissions)
246
        return true;
247
    m_priFile->scheduleUpdate();
248
    return true;
249
}
250

con's avatar
con committed
251 252 253 254 255
/*!
  \class Qt4PriFileNode
  Implements abstract ProjectNode class
  */

256 257
namespace Qt4ProjectManager {

258
Qt4PriFileNode::Qt4PriFileNode(Qt4Project *project, Qt4ProFileNode* qt4ProFileNode, const QString &filePath)
con's avatar
con committed
259 260
        : ProjectNode(filePath),
          m_project(project),
261
          m_qt4ProFileNode(qt4ProFileNode),
262
          m_projectFilePath(QDir::fromNativeSeparators(filePath)),
263 264
          m_projectDir(QFileInfo(filePath).absolutePath()),
          m_includedInExactParse(true)
con's avatar
con committed
265
{
dt's avatar
dt committed
266
    Q_ASSERT(project);
267
    m_qt4PriFile = new Qt4PriFile(this);
268
    Core::DocumentManager::addDocument(m_qt4PriFile);
269

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

272
    setIcon(qt4NodeStaticData()->projectIcon);
273 274
}

275 276 277 278 279
Qt4PriFileNode::~Qt4PriFileNode()
{
    watchFolders(QSet<QString>());
}

280 281
void Qt4PriFileNode::scheduleUpdate()
{
282
    QtSupport::ProFileCacheManager::instance()->discardFile(m_projectFilePath);
283
    m_qt4ProFileNode->scheduleUpdate();
con's avatar
con committed
284 285
}

Daniel Teske's avatar
Daniel Teske committed
286
namespace Internal {
287
struct InternalNode
288
{
289 290
    QList<InternalNode *> virtualfolders;
    QMap<QString, InternalNode *> subnodes;
291 292
    QStringList files;
    ProjectExplorer::FileType type;
293
    QString displayName;
294 295
    QString typeName;
    int priority;
296 297
    QString fullPath;
    QIcon icon;
298

299 300 301
    InternalNode()
    {
        type = ProjectExplorer::UnknownFileType;
302
        priority = 0;
303
    }
304

305 306
    ~InternalNode()
    {
307
        qDeleteAll(virtualfolders);
308 309
        qDeleteAll(subnodes);
    }
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    // 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
329
    void create(const QString &projectDir, const QSet<Utils::FileName> &newFilePaths, ProjectExplorer::FileType type)
330
    {
331
        static const QChar separator = QLatin1Char('/');
332
        const Utils::FileName projectDirFileName = Utils::FileName::fromString(projectDir);
333 334
        foreach (const Utils::FileName &file, newFilePaths) {
            Utils::FileName fileWithoutPrefix;
335
            bool isRelative;
336
            if (file.isChildOf(projectDirFileName)) {
337
                isRelative = true;
338
                fileWithoutPrefix = file.relativeChildPath(projectDirFileName);
339 340 341 342
            } else {
                isRelative = false;
                fileWithoutPrefix = file;
            }
343
            QStringList parts = fileWithoutPrefix.toString().split(separator, QString::SkipEmptyParts);
344
#ifndef Q_OS_WIN
345 346
            if (!isRelative && parts.count() > 0)
                parts[0].prepend(separator);
347
#endif
348 349
            QStringListIterator it(parts);
            InternalNode *currentNode = this;
350
            QString path = (isRelative ? (projectDirFileName.toString() + QLatin1Char('/')) : QString());
351 352 353 354
            while (it.hasNext()) {
                const QString &key = it.next();
                if (it.hasNext()) { // key is directory
                    path += key;
355
                    if (!currentNode->subnodes.contains(path)) {
356 357 358
                        InternalNode *val = new InternalNode;
                        val->type = type;
                        val->fullPath = path;
359 360
                        val->displayName = key;
                        currentNode->subnodes.insert(path, val);
361 362
                        currentNode = val;
                    } else {
363
                        currentNode = currentNode->subnodes.value(path);
364
                    }
365 366
                    path += separator;
                } else { // key is filename
367
                    currentNode->files.append(file.toString());
368 369 370
                }
            }
        }
371 372
        this->compress();
    }
373

374 375 376 377 378 379 380 381 382
    // 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) {
383
                // replace i.value() by i.value()->subnodes.begin()
384
                QString key = i.value()->subnodes.begin().key();
385
                InternalNode *keep = i.value()->subnodes.value(key);
386
                keep->displayName = i.value()->displayName + QLatin1Char('/') + keep->displayName;
387
                newSubnodes.insert(key, keep);
388 389 390 391
                i.value()->subnodes.clear();
                delete i.value();
            } else {
                newSubnodes.insert(i.key(), i.value());
392 393
            }
        }
394 395
        subnodes = newSubnodes;
    }
396

397 398 399 400 401 402 403 404 405 406 407 408 409 410
    FolderNode *createFolderNode(InternalNode *node)
    {
        FolderNode *newNode = 0;
        if (node->typeName.isEmpty())
            newNode = new FolderNode(node->fullPath);
        else
            newNode = new ProVirtualFolderNode(node->fullPath, node->priority, node->typeName);

        newNode->setDisplayName(node->displayName);
        if (!node->icon.isNull())
            newNode->setIcon(node->icon);
        return newNode;
    }

411
    // Makes the projectNode's subtree below the given folder match this internal node's subtree
412
    void updateSubFolders(Qt4ProjectManager::Qt4PriFileNode *projectNode, ProjectExplorer::FolderNode *folder)
413 414
    {
        updateFiles(projectNode, folder, type);
415

416 417 418
        // updateFolders
        QMultiMap<QString, FolderNode *> existingFolderNodes;
        foreach (FolderNode *node, folder->subFolderNodes())
419
            if (node->nodeType() != ProjectNodeType)
420
                existingFolderNodes.insert(node->path(), node);
421

422 423 424 425 426
        QList<FolderNode *> foldersToRemove;
        QList<FolderNode *> foldersToAdd;
        typedef QPair<InternalNode *, FolderNode *> NodePair;
        QList<NodePair> nodesToUpdate;

427 428 429 430 431 432 433 434 435
        // Check virtual
        {
            QList<InternalNode *>::const_iterator it = virtualfolders.constBegin();
            QList<InternalNode *>::const_iterator end = virtualfolders.constEnd();
            for ( ; it != end; ++it) {
                bool found = false;
                QString path = (*it)->fullPath;
                QMultiMap<QString, FolderNode *>::const_iterator oldit
                        = existingFolderNodes.constFind(path);
436
                while (oldit != existingFolderNodes.constEnd() && oldit.key() == path) {
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
                    if (oldit.value()->nodeType() == ProjectExplorer::VirtualFolderNodeType) {
                        ProjectExplorer::VirtualFolderNode *vfn
                                = qobject_cast<ProjectExplorer::VirtualFolderNode *>(oldit.value());
                        if (vfn->priority() == (*it)->priority) {
                            found = true;
                            break;
                        }
                    }
                    ++oldit;
                }
                if (found) {
                    nodesToUpdate << NodePair(*it, *oldit);
                } else {
                    FolderNode *newNode = createFolderNode(*it);
                    foldersToAdd << newNode;
                    nodesToUpdate << NodePair(*it, newNode);
                }
454
            }
455
        }
456 457 458 459 460 461 462 463 464 465
        // Check subnodes
        {
            QMap<QString, InternalNode *>::const_iterator it = subnodes.constBegin();
            QMap<QString, InternalNode *>::const_iterator end = subnodes.constEnd();

            for ( ; it != end; ++it) {
                bool found = false;
                QString path = it.value()->fullPath;
                QMultiMap<QString, FolderNode *>::const_iterator oldit
                        = existingFolderNodes.constFind(path);
466
                while (oldit != existingFolderNodes.constEnd() && oldit.key() == path) {
467 468 469 470 471 472 473 474 475 476 477 478 479 480
                    if (oldit.value()->nodeType() == ProjectExplorer::FolderNodeType) {
                        found = true;
                        break;
                    }
                    ++oldit;
                }
                if (found) {
                    nodesToUpdate << NodePair(it.value(), *oldit);
                } else {
                    FolderNode *newNode = createFolderNode(it.value());
                    foldersToAdd << newNode;
                    nodesToUpdate << NodePair(it.value(), newNode);
                }
            }
481
        }
482

483 484 485 486 487 488 489 490 491 492
        QSet<FolderNode *> toKeep;
        foreach (const NodePair &np, nodesToUpdate)
            toKeep << np.second;

        QMultiMap<QString, FolderNode *>::const_iterator jit = existingFolderNodes.constBegin();
        QMultiMap<QString, FolderNode *>::const_iterator jend = existingFolderNodes.constEnd();
        for ( ; jit != jend; ++jit)
            if (!toKeep.contains(jit.value()))
                foldersToRemove << jit.value();

493 494 495 496
        if (!foldersToRemove.isEmpty())
            projectNode->removeFolderNodes(foldersToRemove, folder);
        if (!foldersToAdd.isEmpty())
            projectNode->addFolderNodes(foldersToAdd, folder);
497

498 499 500 501 502
        foreach (const NodePair &np, nodesToUpdate)
            np.first->updateSubFolders(projectNode, np.second);
    }

    // Makes the folder's files match this internal node's file list
503
    void updateFiles(Qt4ProjectManager::Qt4PriFileNode *projectNode, FolderNode *folder, FileType type)
504 505 506 507 508
    {
        QList<FileNode*> existingFileNodes;
        foreach (FileNode *fileNode, folder->fileNodes()) {
            if (fileNode->fileType() == type && !fileNode->isGenerated())
                existingFileNodes << fileNode;
509 510
        }

511 512
        QList<FileNode*> filesToRemove;
        QList<FileNode*> filesToAdd;
513

514
        qSort(files);
515
        qSort(existingFileNodes.begin(), existingFileNodes.end(), sortNodesByPath);
516 517 518 519 520 521

        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) {
522 523
                filesToRemove << *existingNodeIter;
                ++existingNodeIter;
524
            } else if ((*existingNodeIter)->path() > *newPathIter) {
525
                filesToAdd << new ProjectExplorer::FileNode(*newPathIter, type, false);
526
                ++newPathIter;
527 528 529
            } else { // *existingNodeIter->path() == *newPathIter
                ++existingNodeIter;
                ++newPathIter;
530 531
            }
        }
532 533 534 535 536
        while (existingNodeIter != existingFileNodes.constEnd()) {
            filesToRemove << *existingNodeIter;
            ++existingNodeIter;
        }
        while (newPathIter != files.constEnd()) {
537
            filesToAdd << new ProjectExplorer::FileNode(*newPathIter, type, false);
538 539 540 541 542 543 544 545 546
            ++newPathIter;
        }

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

549
QStringList Qt4PriFileNode::baseVPaths(QtSupport::ProFileReader *reader, const QString &projectDir) const
con's avatar
con committed
550
{
dt's avatar
dt committed
551 552 553
    QStringList result;
    if (!reader)
        return result;
554
    result += reader->absolutePathValues(QLatin1String("VPATH"), projectDir);
dt's avatar
dt committed
555
    result << projectDir; // QMAKE_ABSOLUTE_SOURCE_PATH
556
    result += reader->absolutePathValues(QLatin1String("DEPENDPATH"), projectDir);
dt's avatar
dt committed
557 558 559
    result.removeDuplicates();
    return result;
}
con's avatar
con committed
560

561 562
QStringList Qt4PriFileNode::fullVPaths(const QStringList &baseVPaths, QtSupport::ProFileReader *reader,
                                       FileType type, const QString &qmakeVariable, const QString &projectDir) const
dt's avatar
dt committed
563 564 565 566 567
{
    QStringList vPaths;
    if (!reader)
        return vPaths;
    if (type == ProjectExplorer::SourceType)
568
        vPaths = reader->absolutePathValues(QLatin1String("VPATH_") + qmakeVariable, projectDir);
dt's avatar
dt committed
569 570
    vPaths += baseVPaths;
    if (type == ProjectExplorer::HeaderType)
571
        vPaths += reader->absolutePathValues(QLatin1String("INCLUDEPATH"), projectDir);
dt's avatar
dt committed
572 573 574 575
    vPaths.removeDuplicates();
    return vPaths;
}

576
static QSet<Utils::FileName> recursiveEnumerate(const QString &folder)
577
{
578
    QSet<Utils::FileName> result;
579 580 581 582 583 584
    QFileInfo fi(folder);
    if (fi.isDir()) {
        QDir dir(folder);
        dir.setFilter(dir.filter() | QDir::NoDotAndDotDot);

        foreach (const QFileInfo &file, dir.entryInfoList()) {
585
            if (file.isDir() && !file.isSymLink())
586 587
                result += recursiveEnumerate(file.absoluteFilePath());
            else
588
                result += Utils::FileName(file);
589 590
        }
    } else if (fi.exists()) {
591
        result << Utils::FileName(fi);
592 593 594
    }
    return result;
}
dt's avatar
dt committed
595

596
void Qt4PriFileNode::update(ProFile *includeFileExact, QtSupport::ProFileReader *readerExact, ProFile *includeFileCumlative, QtSupport::ProFileReader *readerCumulative)
dt's avatar
dt committed
597
{
con's avatar
con committed
598 599
    // add project file node
    if (m_fileNodes.isEmpty())
600
        addFileNodes(QList<FileNode*>() << new ProjectExplorer::FileNode(m_projectFilePath, ProjectExplorer::ProjectFileType, false), this);
con's avatar
con committed
601

602 603
    const QString &projectDir = m_qt4ProFileNode->m_projectDir;

604 605
    InternalNode contents;

606 607 608
    // Figure out DEPLOYMENT and INSTALL folders
    QStringList folders;
    QStringList dynamicVariables = dynamicVarNames(readerExact, readerCumulative);
609 610 611 612 613 614 615 616
    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
        }
617 618 619


    for (int i=0; i < folders.size(); ++i) {
620
        const QFileInfo fi(folders.at(i));
621
        if (fi.isRelative())
622
            folders[i] = QDir::cleanPath(projectDir + QLatin1Char('/') + folders.at(i));
623 624
    }

dt's avatar
dt committed
625 626

    m_recursiveEnumerateFiles.clear();
627
    // Remove non existing items and non folders
dt's avatar
dt committed
628 629 630
    QStringList::iterator it = folders.begin();
    while (it != folders.end()) {
        QFileInfo fi(*it);
dt's avatar
dt committed
631 632 633 634 635 636
        if (fi.exists()) {
            if (fi.isDir()) {
                // keep directories
                ++it;
            } else {
                // move files directly to m_recursiveEnumerateFiles
637
                m_recursiveEnumerateFiles << Utils::FileName::fromString(*it);
dt's avatar
dt committed
638 639 640 641
                it = folders.erase(it);
            }
        } else {
            // do remove non exsting stuff
dt's avatar
dt committed
642
            it = folders.erase(it);
dt's avatar
dt committed
643
        }
dt's avatar
dt committed
644 645
    }

646 647 648 649 650 651
    folders.removeDuplicates();
    watchFolders(folders.toSet());

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

654 655 656 657 658 659 660 661 662
    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
663
    // update files
664
    for (int i = 0; i < fileTypes.size(); ++i) {
665
        FileType type = fileTypes.at(i).type;
666
        QStringList qmakeVariables = varNames(type);
con's avatar
con committed
667

668
        QSet<Utils::FileName> newFilePaths;
669
        foreach (const QString &qmakeVariable, qmakeVariables) {
670 671
            if (includeFileExact) {
                QStringList vPathsExact = fullVPaths(baseVPathsExact, readerExact, type, qmakeVariable, projectDir);
672 673 674
                QStringList tmp = readerExact->absoluteFileValues(qmakeVariable, projectDir, vPathsExact, includeFileExact);
                foreach (const QString &t, tmp)
                    newFilePaths += Utils::FileName::fromString(t);
675 676 677
            }
            if (includeFileCumlative) {
                QStringList vPathsCumulative = fullVPaths(baseVPathsCumulative, readerCumulative, type, qmakeVariable, projectDir);
678 679 680
                QStringList tmp = readerCumulative->absoluteFileValues(qmakeVariable, projectDir, vPathsCumulative, includeFileCumlative);
                foreach (const QString &t, tmp)
                    newFilePaths += Utils::FileName::fromString(t);
681
            }
682
        }
con's avatar
con committed
683

dt's avatar
dt committed
684 685 686 687 688 689
        foundFiles[type] = newFilePaths;
        m_recursiveEnumerateFiles.subtract(newFilePaths);
    }

    for (int i = 0; i < fileTypes.size(); ++i) {
        FileType type = fileTypes.at(i).type;
690
        QSet<Utils::FileName> newFilePaths = filterFilesProVariables(type, foundFiles[type]);
691
        newFilePaths += filterFilesRecursiveEnumerata(type, m_recursiveEnumerateFiles);
692 693 694 695 696 697 698 699

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

700 701 702
        if (!newFilePaths.isEmpty()) {
            InternalNode *subfolder = new InternalNode;
            subfolder->type = type;
703
            subfolder->icon = fileTypes.at(i).icon;
704 705 706
            subfolder->fullPath = m_projectDir;
            subfolder->typeName = fileTypes.at(i).typeName;
            subfolder->priority = -i;
707
            subfolder->displayName = fileTypes.at(i).typeName;
708
            contents.virtualfolders.append(subfolder);
709
            // create the hierarchy with subdirectories
710
            subfolder->create(m_projectDir, newFilePaths, type);
con's avatar
con committed
711 712
        }
    }
713 714 715 716 717 718 719 720 721 722 723 724 725

    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())
726
        m_project->unwatchFolders(toUnwatch.toList(), this);
727
    if (!toWatch.isEmpty())
728
        m_project->watchFolders(toWatch.toList(), this);
729 730 731 732

    m_watchedFolders = folders;
}

733
void Qt4PriFileNode::folderChanged(const QString &folder)
734 735 736 737
{
    //qDebug()<<"########## Qt4PriFileNode::folderChanged";
    // So, we need to figure out which files changed.

738 739 740 741
    QString changedFolder = folder;
    if (!changedFolder.endsWith(QLatin1Char('/')))
        changedFolder.append(QLatin1Char('/'));

742
    // Collect all the files
743
    QSet<Utils::FileName> newFiles;
744 745
    newFiles += recursiveEnumerate(changedFolder);

746
    foreach (const Utils::FileName &file, m_recursiveEnumerateFiles) {
747
        if (!file.isChildOf(Utils::FileName::fromString(changedFolder)))
748
            newFiles.insert(file);
749 750
    }

751
    QSet<Utils::FileName> addedFiles = newFiles;
752 753
    addedFiles.subtract(m_recursiveEnumerateFiles);

754
    QSet<Utils::FileName> removedFiles = m_recursiveEnumerateFiles;
755 756 757 758 759 760 761 762 763 764 765 766
    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;
767 768
        QSet<Utils::FileName> add = filterFilesRecursiveEnumerata(type, addedFiles);
        QSet<Utils::FileName> remove = filterFilesRecursiveEnumerata(type, removedFiles);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

        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;
789 790 791
            subfolder->fullPath = m_projectDir;
            subfolder->typeName = fileTypes.at(i).typeName;
            subfolder->priority = -i;
792
            subfolder->displayName = fileTypes.at(i).typeName;
793
            contents.virtualfolders.append(subfolder);
794
            // create the hierarchy with subdirectories
795
            subfolder->create(m_projectDir, m_files[type], type);
796 797 798
        }
    }

799
    contents.updateSubFolders(this, this);
800
    m_project->updateFileList();
801
    m_project->updateCodeModels();
802 803 804 805 806

    // 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
Tobias Hunger's avatar
Tobias Hunger committed
807 808 809
    foreach (const ProjectExplorer::Target *target, m_project->targets()) {
        QtSupport::BaseQtVersion *version = QtSupport::QtProfileInformation::qtVersion(target->profile());
        if (version && version->type() == QtSupport::Constants::SYMBIANQT) {
810 811 812 813 814 815 816 817 818 819 820
            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
821 822
}

dt's avatar
dt committed
823 824 825
bool Qt4PriFileNode::deploysFolder(const QString &folder) const
{
    QString f = folder;
826 827 828
    const QChar slash = QLatin1Char('/');
    if (!f.endsWith(slash))
        f.append(slash);
dt's avatar
dt committed
829 830
    foreach (const QString &wf, m_watchedFolders) {
        if (f.startsWith(wf)
831 832
            && (wf.endsWith(slash)
                || (wf.length() < f.length() && f.at(wf.length()) == slash)))
dt's avatar
dt committed
833 834 835 836 837
            return true;
    }
    return false;
}

dt's avatar
dt committed
838 839
QList<ProjectExplorer::RunConfiguration *> Qt4PriFileNode::runConfigurationsFor(Node *node)
{
Tobias Hunger's avatar
Tobias Hunger committed
840 841 842
    QmakeRunConfigurationFactory *factory = QmakeRunConfigurationFactory::find(m_project->activeTarget());
    if (factory)
        return factory->runConfigurationsForNode(m_project->activeTarget(), node);
843
    return QList<ProjectExplorer::RunConfiguration *>();
dt's avatar
dt committed
844 845
}

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
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
867
QList<ProjectNode::ProjectAction> Qt4PriFileNode::supportedActions(Node *node) const
con's avatar
con committed
868 869
{
    QList<ProjectAction> actions;
870 871 872 873 874

    const FolderNode *folderNode = this;
    const Qt4ProFileNode *proFileNode;
    while (!(proFileNode = qobject_cast<const Qt4ProFileNode*>(folderNode)))
        folderNode = folderNode->parentFolderNode();
dt's avatar
dt committed
875
    Q_ASSERT(proFileNode);
876 877 878

    switch (proFileNode->projectType()) {
    case ApplicationTemplate:
879 880 881 882 883 884
    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