2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 Alexander Drozdov.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2016-12-04 03:36:12 +10:00
|
|
|
|
|
|
|
|
#include "treescanner.h"
|
2021-06-16 11:33:35 +02:00
|
|
|
|
2019-03-14 15:34:14 +01:00
|
|
|
#include "projectexplorerconstants.h"
|
2021-06-16 11:33:35 +02:00
|
|
|
#include "projectnodeshelper.h"
|
2021-06-21 16:14:20 +02:00
|
|
|
#include "projecttree.h"
|
2016-12-04 03:36:12 +10:00
|
|
|
|
2017-06-19 16:07:25 +02:00
|
|
|
#include <coreplugin/iversioncontrol.h>
|
|
|
|
|
#include <coreplugin/vcsmanager.h>
|
2016-12-04 03:36:12 +10:00
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/cppeditorconstants.h>
|
2016-12-04 03:36:12 +10:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/runextensions.h>
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
2019-03-14 15:34:14 +01:00
|
|
|
namespace ProjectExplorer {
|
2016-12-04 03:36:12 +10:00
|
|
|
|
|
|
|
|
TreeScanner::TreeScanner(QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
m_factory = TreeScanner::genericFileType;
|
2019-05-28 13:49:26 +02:00
|
|
|
m_filter = [](const Utils::MimeType &mimeType, const Utils::FilePath &fn) {
|
2016-12-04 03:36:12 +10:00
|
|
|
return isWellKnownBinary(mimeType, fn) && isMimeBinary(mimeType, fn);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connect(&m_futureWatcher, &FutureWatcher::finished, this, &TreeScanner::finished);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-14 16:34:22 +10:00
|
|
|
TreeScanner::~TreeScanner()
|
|
|
|
|
{
|
2020-03-30 14:56:40 +02:00
|
|
|
disconnect(&m_futureWatcher, nullptr, nullptr, nullptr); // Do not trigger signals anymore!
|
|
|
|
|
|
2017-02-14 16:34:22 +10:00
|
|
|
if (!m_futureWatcher.isFinished()) {
|
|
|
|
|
m_futureWatcher.cancel();
|
|
|
|
|
m_futureWatcher.waitForFinished();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool TreeScanner::asyncScanForFiles(const Utils::FilePath &directory)
|
2016-12-04 03:36:12 +10:00
|
|
|
{
|
|
|
|
|
if (!m_futureWatcher.isFinished())
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-11-02 09:33:10 +01:00
|
|
|
m_scanFuture = Utils::runAsync(
|
|
|
|
|
[directory, filter = m_filter, factory = m_factory] (FutureInterface &fi) {
|
|
|
|
|
TreeScanner::scanForFiles(fi, directory, filter, factory);
|
2020-11-11 11:39:57 +01:00
|
|
|
});
|
2016-12-04 03:36:12 +10:00
|
|
|
m_futureWatcher.setFuture(m_scanFuture);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TreeScanner::setFilter(TreeScanner::FileFilter filter)
|
|
|
|
|
{
|
|
|
|
|
if (isFinished())
|
|
|
|
|
m_filter = filter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TreeScanner::setTypeFactory(TreeScanner::FileTypeFactory factory)
|
|
|
|
|
{
|
|
|
|
|
if (isFinished())
|
|
|
|
|
m_factory = factory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TreeScanner::Future TreeScanner::future() const
|
|
|
|
|
{
|
|
|
|
|
return m_scanFuture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TreeScanner::isFinished() const
|
|
|
|
|
{
|
|
|
|
|
return m_futureWatcher.isFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TreeScanner::Result TreeScanner::result() const
|
|
|
|
|
{
|
|
|
|
|
if (isFinished())
|
|
|
|
|
return m_scanFuture.result();
|
|
|
|
|
return Result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TreeScanner::Result TreeScanner::release()
|
|
|
|
|
{
|
2020-11-26 22:46:31 +02:00
|
|
|
if (isFinished() && m_scanFuture.resultCount() > 0) {
|
2016-12-04 03:36:12 +10:00
|
|
|
auto result = m_scanFuture.result();
|
|
|
|
|
m_scanFuture = Future();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2020-11-26 22:46:31 +02:00
|
|
|
m_scanFuture = Future();
|
2016-12-04 03:36:12 +10:00
|
|
|
return Result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TreeScanner::reset()
|
|
|
|
|
{
|
|
|
|
|
if (isFinished())
|
|
|
|
|
m_scanFuture = Future();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool TreeScanner::isWellKnownBinary(const Utils::MimeType & /*mdb*/, const Utils::FilePath &fn)
|
2016-12-04 03:36:12 +10:00
|
|
|
{
|
|
|
|
|
return fn.endsWith(QLatin1String(".a")) ||
|
|
|
|
|
fn.endsWith(QLatin1String(".o")) ||
|
|
|
|
|
fn.endsWith(QLatin1String(".d")) ||
|
|
|
|
|
fn.endsWith(QLatin1String(".exe")) ||
|
|
|
|
|
fn.endsWith(QLatin1String(".dll")) ||
|
|
|
|
|
fn.endsWith(QLatin1String(".obj")) ||
|
|
|
|
|
fn.endsWith(QLatin1String(".elf"));
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool TreeScanner::isMimeBinary(const Utils::MimeType &mimeType, const Utils::FilePath &/*fn*/)
|
2016-12-04 03:36:12 +10:00
|
|
|
{
|
|
|
|
|
bool isBinary = false;
|
|
|
|
|
if (mimeType.isValid()) {
|
|
|
|
|
QStringList mimes;
|
|
|
|
|
mimes << mimeType.name() << mimeType.allAncestors();
|
|
|
|
|
isBinary = !mimes.contains(QLatin1String("text/plain"));
|
|
|
|
|
}
|
|
|
|
|
return isBinary;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FileType TreeScanner::genericFileType(const Utils::MimeType &mimeType, const Utils::FilePath &/*fn*/)
|
2016-12-04 03:36:12 +10:00
|
|
|
{
|
2017-03-10 17:03:51 +01:00
|
|
|
return Node::fileTypeForMimeType(mimeType);
|
2016-12-04 03:36:12 +10:00
|
|
|
}
|
|
|
|
|
|
2021-06-16 11:33:35 +02:00
|
|
|
static std::unique_ptr<FolderNode> createFolderNode(const Utils::FilePath &directory,
|
|
|
|
|
const QList<FileNode *> &allFiles)
|
|
|
|
|
{
|
|
|
|
|
auto fileSystemNode = std::make_unique<FolderNode>(directory);
|
|
|
|
|
for (const FileNode *fn : allFiles) {
|
|
|
|
|
if (!fn->filePath().isChildOf(directory))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<FileNode> node(fn->clone());
|
|
|
|
|
fileSystemNode->addNestedNode(std::move(node));
|
|
|
|
|
}
|
2021-10-14 10:27:45 +02:00
|
|
|
ProjectTree::applyTreeManager(fileSystemNode.get(), ProjectTree::AsyncPhase); // QRC nodes
|
2021-06-16 11:33:35 +02:00
|
|
|
return fileSystemNode;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 11:39:57 +01:00
|
|
|
void TreeScanner::scanForFiles(FutureInterface &fi, const Utils::FilePath& directory,
|
2018-09-30 08:55:19 +03:00
|
|
|
const FileFilter &filter, const FileTypeFactory &factory)
|
2016-12-04 03:36:12 +10:00
|
|
|
{
|
2021-06-16 11:33:35 +02:00
|
|
|
QList<FileNode *> nodes = ProjectExplorer::scanForFiles(fi, directory,
|
|
|
|
|
[&filter, &factory](const Utils::FilePath &fn) -> FileNode * {
|
2021-05-18 14:35:07 +02:00
|
|
|
const Utils::MimeType mimeType = Utils::mimeTypeForFile(fn);
|
2016-12-04 03:36:12 +10:00
|
|
|
|
|
|
|
|
// Skip some files during scan.
|
|
|
|
|
if (filter && filter(mimeType, fn))
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
// Type detection
|
|
|
|
|
FileType type = FileType::Unknown;
|
|
|
|
|
if (factory)
|
|
|
|
|
type = factory(mimeType, fn);
|
|
|
|
|
|
2019-02-25 12:08:58 +01:00
|
|
|
return new FileNode(fn, type);
|
2020-11-11 11:39:57 +01:00
|
|
|
});
|
2016-12-04 03:36:12 +10:00
|
|
|
|
2017-05-21 08:27:58 +03:00
|
|
|
Utils::sort(nodes, ProjectExplorer::Node::sortByPath);
|
2016-12-04 03:36:12 +10:00
|
|
|
|
2020-11-11 11:39:57 +01:00
|
|
|
fi.setProgressValue(fi.progressMaximum());
|
2021-06-16 11:33:35 +02:00
|
|
|
Result result{createFolderNode(directory, nodes), nodes};
|
|
|
|
|
|
|
|
|
|
fi.reportResult(result);
|
2016-12-04 03:36:12 +10:00
|
|
|
}
|
|
|
|
|
|
2019-03-14 15:34:14 +01:00
|
|
|
} // namespace ProjectExplorer
|