File indexing completed on 2024-04-28 15:29:20
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2012 Dawit Alemayehu <adawit@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KPARTS_LISTINGFILTEREXTENSION_H 0009 #define KPARTS_LISTINGFILTEREXTENSION_H 0010 0011 #include <kparts/kparts_export.h> 0012 0013 #include <QObject> 0014 #include <memory> 0015 0016 class KFileItemList; 0017 0018 namespace KParts 0019 { 0020 class ReadOnlyPart; 0021 class ListingFilterExtensionPrivate; 0022 0023 /** 0024 * @class ListingFilterExtension listingfilterextension.h <KParts/ListingFilterExtension> 0025 * 0026 * @short An extension for filtering listings. 0027 * 0028 * This extension is intended to be implemented by parts that provide listing 0029 * services, e.g. file management parts and is intended to provide a generic 0030 * API for filtering any listing through keywords, wildcard characters and/or 0031 * content-type. 0032 * 0033 * Examples: 0034 * 0035 * To show items that only match the term "kde" 0036 * \code 0037 * KParts::ListingFilterExtension* ext = KParts::ListingFilterExtension::childObject(part); 0038 * if (ext && (ext->supportedFilterModes() & KParts::ListingFilterExtension::SubString)) { 0039 * ext->setFilter(KParts::ListingFilterExtension::SubString, QLatin1String("kde")); 0040 * } 0041 * \endcode 0042 * 0043 * To show items that only match "text/html" 0044 * \code 0045 * KParts::ListingFilterExtension* ext = KParts::ListingFilterExtension::childObject(part); 0046 * if (ext && (ext->supportedFilterModes() & KParts::ListingFilterExtension::MimeType)) { 0047 * ext->setFilter(KParts::ListingFilterExtension::MimeType, QLatin1String("text/html")); 0048 * } 0049 * \endcode 0050 * 0051 * To show items that only match the wildcard string "*.txt" 0052 * \code 0053 * KParts::ListingFilterExtension* ext = KParts::ListingFilterExtension::childObject(part); 0054 * if (ext && (ext->supportedFilterModes() & KParts::ListingFilterExtension::WildCard)) { 0055 * ext->setFilter(KParts::ListingFilterExtension::WildCard, QLatin1String("*.txt")); 0056 * } 0057 * \endcode 0058 * 0059 * To show items that match multiple mime types, e.g. text/html & application/xml: 0060 * 0061 * \code 0062 * KParts::ListingFilterExtension* ext = KParts::ListingFilterExtension::childObject(part); 0063 * if (ext && 0064 * (ext->supportedFilterModes() & KParts::ListingFilterExtension::MimeType) && 0065 * ext->supportsMultipleFilters(KParts::ListingFilterExtension::MimeType)) { 0066 * QStringList mimeTypes = ext->filter(KParts::ListingFilterExtension::MimeType).toStringList(); 0067 * mimeTypes << QLatin1String("text/html") << QLatin1String("application/xml"); 0068 * ext->setFilter(KParts::ListingFilterExtension::MimeType, mimeTypes); 0069 * } 0070 * \endcode 0071 * 0072 * @since 4.9.2 0073 */ 0074 class KPARTS_EXPORT ListingFilterExtension : public QObject 0075 { 0076 Q_OBJECT 0077 0078 public: 0079 /** 0080 * Supported file filtering modes modes. 0081 * @FilterModes 0082 */ 0083 enum FilterMode { 0084 None = 0x00, 0085 MimeType = 0x01, /*!< Filter by mime type, e.g. "text/plain". */ 0086 SubString = 0x02, /*!< Filter by matching any part of a file or directory name, e.g. "Documents" */ 0087 WildCard = 0x04, /*!< Filter by using wildcard matches, e.g. "*.txt" */ 0088 }; 0089 0090 /** 0091 * Stores a combination of #FilterMode values. 0092 */ 0093 Q_DECLARE_FLAGS(FilterModes, FilterMode) 0094 0095 /*! Constructor */ 0096 explicit ListingFilterExtension(KParts::ReadOnlyPart *parent); 0097 0098 /*! Destructor */ 0099 ~ListingFilterExtension() override; 0100 0101 /** 0102 * Queries @p obj for a child object which inherits from this class. 0103 */ 0104 static ListingFilterExtension *childObject(QObject *obj); 0105 0106 /** 0107 * Returns the OR'ed value of the file filter modes supported by the part 0108 * that implements this extension. 0109 * 0110 * By default this function returns None. 0111 */ 0112 virtual FilterModes supportedFilterModes() const; 0113 0114 /** 0115 * Returns true if the part that implements this extension allows 0116 * the use of multiple filters for the given filtering @p mode. 0117 * 0118 * By default this function returns false. 0119 */ 0120 virtual bool supportsMultipleFilters(FilterMode mode) const; 0121 0122 /** 0123 * Returns the currently set filters for the given @p mode. 0124 * 0125 * @param mode the desired filter mode as specified in @ref FilterMode. 0126 */ 0127 virtual QVariant filter(FilterMode mode) const = 0; 0128 0129 /** 0130 * Sets the file @p filter that should be applied by the part that 0131 * implements this extension for the given filtering @p mode. 0132 * 0133 * To remove a filter for a given filter mode, simply call this function with 0134 * the desired mode and the @p filter parameter set to a NULL variant. 0135 * 0136 * The second parameter can be 0137 * 0138 * @param mode the desired filter mode as specified in @ref FilterMode. 0139 * @param filter a list of filter texts based on the selected mode. 0140 */ 0141 virtual void setFilter(FilterMode mode, const QVariant &filter) = 0; 0142 0143 private: 0144 std::unique_ptr<ListingFilterExtension> const d; 0145 }; 0146 0147 Q_DECLARE_OPERATORS_FOR_FLAGS(ListingFilterExtension::FilterModes) 0148 0149 } 0150 0151 #endif /* KPARTS_LISTINGFILTEREXTENSION_H */