File indexing completed on 2024-04-28 04:37:35
0001 /* 0002 SPDX-FileCopyrightText: 2010 Niko Sams <niko.sams@gmail.com> 0003 SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "testproject.h" 0009 0010 #include <project/projectmodel.h> 0011 #include <interfaces/icore.h> 0012 0013 #include <QDir> 0014 #include <QDirIterator> 0015 0016 using namespace KDevelop; 0017 0018 TestProject::TestProject(const Path& path, QObject* parent) 0019 : IProject(parent) 0020 , m_projectConfiguration(KSharedConfig::openConfig()) 0021 { 0022 m_path = path.isValid() ? path : Path(QDir::tempPath() + QLatin1String("/kdev-testproject/")); 0023 m_root = new ProjectFolderItem(this, m_path); 0024 ICore::self()->projectController()->projectModel()->appendRow(m_root); 0025 } 0026 0027 void TestProject::setPath(const Path& path) 0028 { 0029 m_path = path; 0030 if (m_root) { 0031 m_root->setPath(path); 0032 } 0033 } 0034 0035 TestProject::~TestProject() 0036 { 0037 if (m_root) { 0038 delete m_root; 0039 } 0040 } 0041 0042 ProjectFolderItem* TestProject::projectItem() const 0043 { 0044 return m_root; 0045 } 0046 0047 void TestProject::setProjectItem(ProjectFolderItem* item) 0048 { 0049 if (m_root) { 0050 ICore::self()->projectController()->projectModel()->removeRow(m_root->row()); 0051 m_root = nullptr; 0052 m_path = Path{}; 0053 } 0054 if (item) { 0055 m_root = item; 0056 m_path = item->path(); 0057 ICore::self()->projectController()->projectModel()->appendRow(m_root); 0058 } 0059 } 0060 0061 Path TestProject::projectFile() const 0062 { 0063 return Path(m_path, m_path.lastPathSegment() + QLatin1String(".kdev4")); 0064 } 0065 0066 Path TestProject::path() const 0067 { 0068 return m_path; 0069 } 0070 0071 bool TestProject::inProject(const IndexedString& path) const 0072 { 0073 return m_path.isParentOf(Path(path.str())); 0074 } 0075 0076 void findFileItems(ProjectBaseItem* root, QList<ProjectFileItem*>& items, const Path& path = {}) 0077 { 0078 const auto children = root->children(); 0079 for (ProjectBaseItem* item : children) { 0080 if (item->file() && (path.isEmpty() || item->path() == path)) { 0081 items << item->file(); 0082 } 0083 if (item->rowCount()) { 0084 findFileItems(item, items, path); 0085 } 0086 } 0087 } 0088 0089 QList<ProjectFileItem*> TestProject::files() const 0090 { 0091 QList<ProjectFileItem*> ret; 0092 findFileItems(m_root, ret); 0093 return ret; 0094 } 0095 0096 QList<ProjectFileItem*> TestProject::filesForPath(const IndexedString& path) const 0097 { 0098 QList<ProjectFileItem*> ret; 0099 findFileItems(m_root, ret, Path(path.toUrl())); 0100 return ret; 0101 } 0102 0103 void TestProject::addToFileSet(ProjectFileItem* file) 0104 { 0105 if (!m_fileSet.contains(file->indexedPath())) { 0106 m_fileSet.insert(file->indexedPath()); 0107 emit fileAddedToSet(file); 0108 } 0109 } 0110 0111 void TestProject::removeFromFileSet(ProjectFileItem* file) 0112 { 0113 if (m_fileSet.remove(file->indexedPath())) { 0114 emit fileRemovedFromSet(file); 0115 } 0116 } 0117 0118 void TestProjectController::initialize() 0119 { 0120 } 0121 0122 void TestProjectUtils::addChildrenFromFileSystem(ProjectFolderItem* parent) 0123 { 0124 constexpr QDir::Filters entryFilter = QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden; 0125 QDirIterator it(parent->path().path(), {QStringLiteral("*")}, entryFilter); 0126 while (it.hasNext()) { 0127 it.next(); 0128 const auto info = it.fileInfo(); 0129 if (info.isDir()) { 0130 const auto child = createChild<ProjectFolderItem>(parent, info.fileName()); 0131 addChildrenFromFileSystem(child); 0132 } else { 0133 createChild<ProjectFileItem>(parent, info.fileName()); 0134 } 0135 } 0136 } 0137 0138 #include "moc_testproject.cpp"