<commit_before>/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (<EMAIL>)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/

#include "projectfilewizardextension.h"
#include "projectexplorer.h"
#include "projectnodes.h"
#include "nodesvisitor.h"
#include "projectwizardpage.h"

#include <coreplugin/basefilewizard.h>
#include <coreplugin/dialogs/iwizard.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>

#include <QtCore/QVariant>
#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtCore/QMultiMap>

enum { debugExtension = 0 };

namespace ProjectExplorer {

typedef QList<ProjectNode *> ProjectNodeList;

namespace Internal {

// --------- AllProjectNodesVisitor. Figure out all projects.
// No sooner said then done.
class AllProjectNodesVisitor : public NodesVisitor
{
    AllProjectNodesVisitor(ProjectNodeList &l) : m_projectNodes(l) {}
public:

    static ProjectNodeList allProjects(const ProjectExplorerPlugin *pe);

    virtual void visitProjectNode(ProjectNode *node);

private:
    ProjectNodeList &m_projectNodes;
};

ProjectNodeList AllProjectNodesVisitor::allProjects(const ProjectExplorerPlugin *pe)
{
    ProjectNodeList rc;
    AllProjectNodesVisitor visitor(rc);
    pe->session()->sessionNode()->accept(&visitor);
    return rc;
}

void AllProjectNodesVisitor::visitProjectNode(ProjectNode *node)
{
    if (node->supportedActions().contains(ProjectNode::AddFile))
        m_projectNodes << node;
}

// --------- ProjectWizardContext
struct ProjectWizardContext
{
    Core::IVersionControl *versionControl;
    ProjectNodeList projects;
    ProjectWizardPage *page;
};

// ---- ProjectFileWizardExtension
ProjectFileWizardExtension::ProjectFileWizardExtension()
  : m_context(0)
{
}

ProjectFileWizardExtension::~ProjectFileWizardExtension()
{
    delete m_context;
}

void ProjectFileWizardExtension::firstExtensionPageShown(const QList<Core::GeneratedFile> &files)
{
    if (debugExtension)
        qDebug() << Q_FUNC_INFO << files.size();
    // Setup files display and version control depending on path
    QStringList fileNames;
    foreach (const Core::GeneratedFile &f, files)
        fileNames.push_back(f.path());

    const QString directory = QFileInfo(fileNames.front()).absolutePath();
    m_context->versionControl = Core::ICore::instance()->vcsManager()->findVersionControlForDirectory(directory);

    m_context->page->setFilesDisplay(fileNames);

    const bool canAddToVCS = m_context->versionControl && m_context->versionControl->supportsOperation(Core::IVersionControl::AddOperation);
    if (m_context->versionControl)
         m_context->page->setVCSDisplay(m_context->versionControl->name());
    m_context->page->setAddToVersionControlEnabled(canAddToVCS);
}

static ProjectNode *currentProject()
{
    if (Node *currentNode = ProjectExplorerPlugin::instance()->currentNode())
        if (ProjectNode *currentProjectNode = qobject_cast<ProjectNode *>(currentNode))
            return currentProjectNode;
    return 0;
}

QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const Core::IWizard *wizard)
{
    if (!m_context)
        m_context = new ProjectWizardContext;
    // Init context with page and projects
    m_context->page = new ProjectWizardPage;
    m_context->versionControl = 0;
    m_context->projects = AllProjectNodesVisitor::allProjects(ProjectExplorerPlugin::instance());
    // Set up project list which remains the same over duration of wizard execution
    // Disable "add project to project"
    const bool hasProjects = !m_context->projects.empty();
    if (hasProjects) {
        // Compile list of names and find current project if there is one
        QStringList projectNames;
        ProjectNode *current = currentProject();
        int currentIndex = -1;
        const int count = m_context->projects.size();
        for (int i = 0; i < count; i++) {
            ProjectNode *pn = m_context->projects.at(i);
            projectNames.push_back(pn->name());
            if (current == pn)
                currentIndex = i;
        }
        m_context->page->setProjects(projectNames);
        if (currentIndex != -1)
            m_context->page->setCurrentProjectIndex(currentIndex);
    }
    m_context->page->setAddToProjectEnabled(hasProjects && wizard->kind() != Core::IWizard::ProjectWizard);

    return QList<QWizardPage *>() << m_context->page;
}

bool ProjectFileWizardExtension::process(const QList<Core::GeneratedFile> &files, QString *errorMessage)
{
    typedef QMultiMap<FileType, QString> TypeFileMap;
    // Add files to project && version control
    if (m_context->page->addToProject()) {
        ProjectNode *project = m_context->projects.at(m_context->page->currentProjectIndex());
        // Split into lists by file type and add
        TypeFileMap typeFileMap;
        foreach (const Core::GeneratedFile &generatedFile, files) {
            const QString path = generatedFile.path();
            typeFileMap.insert(typeForFileName(Core::ICore::instance()->mimeDatabase(), path), path);
        }
        foreach (FileType type, typeFileMap.uniqueKeys()) {
            const QStringList files = typeFileMap.values(type);
            if (!project->addFiles(type, files)) {<commit_msg>Fixes:    - Show extension of projects to add files to<commit_after>/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (<EMAIL>)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/

#include "projectfilewizardextension.h"
#include "projectexplorer.h"
#include "projectnodes.h"
#include "nodesvisitor.h"
#include "projectwizardpage.h"

#include <coreplugin/basefilewizard.h>
#include <coreplugin/dialogs/iwizard.h>
#include <coreplugin/filemanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/iversioncontrol.h>
#include <coreplugin/vcsmanager.h>

#include <QtCore/QVariant>
#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtCore/QMultiMap>

enum { debugExtension = 0 };

namespace ProjectExplorer {

typedef QList<ProjectNode *> ProjectNodeList;

namespace Internal {

// --------- AllProjectNodesVisitor. Figure out all projects.
// No sooner said then done.
class AllProjectNodesVisitor : public NodesVisitor
{
    AllProjectNodesVisitor(ProjectNodeList &l) : m_projectNodes(l) {}
public:

    static ProjectNodeList allProjects(const ProjectExplorerPlugin *pe);

    virtual void visitProjectNode(ProjectNode *node);

private:
    ProjectNodeList &m_projectNodes;
};

ProjectNodeList AllProjectNodesVisitor::allProjects(const ProjectExplorerPlugin *pe)
{
    ProjectNodeList rc;
    AllProjectNodesVisitor visitor(rc);
    pe->session()->sessionNode()->accept(&visitor);
    return rc;
}

void AllProjectNodesVisitor::visitProjectNode(ProjectNode *node)
{
    if (node->supportedActions().contains(ProjectNode::AddFile))
        m_projectNodes << node;
}

// --------- ProjectWizardContext
struct ProjectWizardContext
{
    Core::IVersionControl *versionControl;
    ProjectNodeList projects;
    ProjectWizardPage *page;
};

// ---- ProjectFileWizardExtension
ProjectFileWizardExtension::ProjectFileWizardExtension()
  : m_context(0)
{
}

ProjectFileWizardExtension::~ProjectFileWizardExtension()
{
    delete m_context;
}

void ProjectFileWizardExtension::firstExtensionPageShown(const QList<Core::GeneratedFile> &files)
{
    if (debugExtension)
        qDebug() << Q_FUNC_INFO << files.size();
    // Setup files display and version control depending on path
    QStringList fileNames;
    foreach (const Core::GeneratedFile &f, files)
        fileNames.push_back(f.path());

    const QString directory = QFileInfo(fileNames.front()).absolutePath();
    m_context->versionControl = Core::ICore::instance()->vcsManager()->findVersionControlForDirectory(directory);

    m_context->page->setFilesDisplay(fileNames);

    const bool canAddToVCS = m_context->versionControl && m_context->versionControl->supportsOperation(Core::IVersionControl::AddOperation);
    if (m_context->versionControl)
         m_context->page->setVCSDisplay(m_context->versionControl->name());
    m_context->page->setAddToVersionControlEnabled(canAddToVCS);
}

static ProjectNode *currentProject()
{
    if (Node *currentNode = ProjectExplorerPlugin::instance()->currentNode())
        if (ProjectNode *currentProjectNode = qobject_cast<ProjectNode *>(currentNode))
            return currentProjectNode;
    return 0;
}

QList<QWizardPage *> ProjectFileWizardExtension::extensionPages(const Core::IWizard *wizard)
{
    if (!m_context)
        m_context = new ProjectWizardContext;
    // Init context with page and projects
    m_context->page = new ProjectWizardPage;
    m_context->versionControl = 0;
    m_context->projects = AllProjectNodesVisitor::allProjects(ProjectExplorerPlugin::instance());
    // Set up project list which remains the same over duration of wizard execution
    // Disable "add project to project"
    const bool hasProjects = !m_context->projects.empty();
    if (hasProjects) {
        // Compile list of names and find current project if there is one
        QStringList projectNames;
        ProjectNode *current = currentProject();
        int currentIndex = -1;
        const int count = m_context->projects.size();
        for (int i = 0; i < count; i++) {
            ProjectNode *pn = m_context->projects.at(i);
            projectNames.push_back(QFileInfo(pn->path()).fileName());
            if (current == pn)
                currentIndex = i;
        }
        m_context->page->setProjects(projectNames);
        if (currentIndex != -1)
            m_context->page->setCurrentProjectIndex(currentIndex);
    }
    m_context->page->setAddToProjectEnabled(hasProjects && wizard->kind() != Core::IWizard::ProjectWizard);

    return QList<QWizardPage *>() << m_context->page;
}

bool ProjectFileWizardExtension::process(const QList<Core::GeneratedFile> &files, QString *errorMessage)
{
    typedef QMultiMap<FileType, QString> TypeFileMap;
    // Add files to project && version control
    if (m_context->page->addToProject()) {
        ProjectNode *project = m_context->projects.at(m_context->page->currentProjectIndex());
        // Split into lists by file type and add
        TypeFileMap typeFileMap;
        foreach (const Core::GeneratedFile &generatedFile, files) {
            const QString path = generatedFile.path();
            typeFileMap.insert(typeForFileName(Core::ICore::instance()->mimeDatabase(), path), path);
        }
        foreach (FileType type, typeFileMap.uniqueKeys()) {
            const QStringList files = typeFileMap.values(type);
            if (!project->addFiles(type, files)) {
{'fraction_non_alphanumeric': 0.1333995871875239, 'fraction_numerical': 0.0036694442320923475, 'mean_word_length': 4.311408850994722, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4247573', 'n_tokens_mistral': 3673, 'n_tokens_neox': 3539, 'n_words': 942}