File indexing completed on 2024-04-28 04:39:08

0001 /*
0002     SPDX-FileCopyrightText: 2013 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "projectfilter.h"
0008 
0009 #include <interfaces/iproject.h>
0010 
0011 #include <QFile>
0012 
0013 using namespace KDevelop;
0014 
0015 ProjectFilter::ProjectFilter( const IProject* const project, const QVector<Filter>& filters )
0016     : m_filters( filters )
0017     , m_projectFile( project->projectFile() )
0018     , m_project( project->path() )
0019 {
0020 
0021 }
0022 
0023 ProjectFilter::~ProjectFilter()
0024 {
0025 
0026 }
0027 
0028 bool ProjectFilter::isValid( const Path &path, const bool isFolder ) const
0029 {
0030     if (!isFolder && path == m_projectFile) {
0031         // do not show the project file
0032         ///TODO: enable again once the project page is ready for consumption
0033         return false;
0034     } else if (isFolder && path == m_project) {
0035         // always show the project root
0036         return true;
0037     }
0038 
0039     if (isFolder && path.isLocalFile() && QFile::exists(path.toLocalFile() + QLatin1String("/.kdev_ignore"))) {
0040         return false;
0041     }
0042 
0043     // from here on the user can configure what he wants to see or not.
0044 
0045     // we operate on the path relative to the project base
0046     // by prepending a slash we can filter hidden files with the pattern "*/.*"
0047 
0048     const QString relativePath = makeRelative(path);
0049 
0050     if (isFolder && relativePath.endsWith(QLatin1String("/.kdev4"))) {
0051         return false;
0052     }
0053 
0054     bool isValid = true;
0055     for (const Filter& filter : m_filters) {
0056         if (isFolder && !(filter.targets & Filter::Folders)) {
0057             continue;
0058         } else if (!isFolder && !(filter.targets & Filter::Files)) {
0059             continue;
0060         }
0061         if ((!isValid && filter.type == Filter::Inclusive) || (isValid && filter.type == Filter::Exclusive)) {
0062             const bool match = filter.pattern.exactMatch( relativePath );
0063             if (filter.type == Filter::Inclusive) {
0064                 isValid = match;
0065             } else {
0066                 isValid = !match;
0067             }
0068         }
0069     }
0070     return isValid;
0071 }
0072 
0073 QString ProjectFilter::makeRelative(const Path& path) const
0074 {
0075     if (!m_project.isParentOf(path)) {
0076         return path.path();
0077     }
0078 
0079     return QLatin1Char('/') + m_project.relativePath(path);
0080 }