File indexing completed on 2024-05-12 05:47:29

0001 /*
0002  * SPDX-FileCopyrightText: 2011 Janardhan Reddy <annapareddyjanardhanreddy@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KFILEITEMMODELFILTER_H
0008 #define KFILEITEMMODELFILTER_H
0009 
0010 #include "dolphin_export.h"
0011 
0012 #include <QStringList>
0013 
0014 class KFileItem;
0015 class QRegularExpression;
0016 
0017 /**
0018  * @brief Allows to check whether an item of the KFileItemModel
0019  *        matches with a set filter-string.
0020  *
0021  * Currently the filter is only checked for the KFileItem::text()
0022  * property of the KFileItem, but this might get extended in
0023  * future.
0024  */
0025 class DOLPHIN_EXPORT KFileItemModelFilter
0026 {
0027 public:
0028     KFileItemModelFilter();
0029     virtual ~KFileItemModelFilter();
0030 
0031     /**
0032      * Sets the pattern that is used for a comparison with the item
0033      * in KFileItemModelFilter::matches(). Per default the pattern
0034      * defines a sub-string. As soon as the pattern contains at least
0035      * a '*', '?' or '[' the pattern represents a regular expression.
0036      */
0037     void setPattern(const QString &pattern);
0038     QString pattern() const;
0039 
0040     /**
0041      * Set the list of mimetypes that are used for comparison with the
0042      * item in KFileItemModelFilter::matchesMimeType.
0043      */
0044     void setMimeTypes(const QStringList &types);
0045     QStringList mimeTypes() const;
0046 
0047     /**
0048      * Set the list of mimetypes that are used for comparison and excluded with the
0049      * item in KFileItemModelFilter::matchesMimeType.
0050      */
0051     void setExcludeMimeTypes(const QStringList &types);
0052     QStringList excludeMimeTypes() const;
0053 
0054     /**
0055      * @return True if either the pattern or mimetype filters has been set.
0056      */
0057     bool hasSetFilters() const;
0058 
0059     /**
0060      * @return True if the item matches with the pattern defined by
0061      *         @ref setPattern() or @ref setMimeTypes
0062      */
0063     bool matches(const KFileItem &item) const;
0064 
0065 private:
0066     /**
0067      * @return True if item matches pattern set by @ref setPattern.
0068      */
0069     bool matchesPattern(const KFileItem &item) const;
0070 
0071     /**
0072      * @return True if item matches mimetypes set by @ref setMimeTypes.
0073      */
0074     bool matchesType(const KFileItem &item) const;
0075 
0076     bool m_useRegExp; // If true, m_regExp is used for filtering,
0077                       // otherwise m_lowerCaseFilter is used.
0078     QRegularExpression *m_regExp;
0079     QString m_lowerCasePattern; // Lowercase version of m_filter for
0080                                 // faster comparison in matches().
0081     QString m_pattern; // Property set by setPattern().
0082     QStringList m_mimeTypes; // Property set by setMimeTypes()
0083     QStringList m_excludeMimeTypes; // Property set by setExcludeMimeTypes()
0084 };
0085 #endif