1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KFILEFILTER_H
9#define KFILEFILTER_H
10
11#include "kiocore_export.h"
12
13#include <QSharedDataPointer>
14#include <QString>
15#include <QStringList>
16
17class KFileFilterPrivate;
18
19/**
20 * Encapsulates rules to filter a list of files.
21 * Files can be filtered based on name patterns (e.g. *.cpp), MIME types, or both.
22 * Filters also optionally have a user-facing label.
23 *
24 * @since 5.101
25 */
26class KIOCORE_EXPORT KFileFilter
27{
28public:
29 /**
30 * Creates an empty filter.
31 */
32 explicit KFileFilter();
33
34 /**
35 * Creates a filter with a given label, name patterns, and MIME types.
36 *
37 * @param label The user-facing label for this filter.
38 * @param filePatterns A list of file name patterns that should be included, e.g. ("*.cpp", "*.cxx").
39 * @param mimePatterns A list of MIME types that should be included, e.g. ("text/plain", "image/png").
40 *
41 */
42 explicit KFileFilter(const QString &label, const QStringList &filePatterns, const QStringList &mimePatterns);
43
44 KFileFilter(const KFileFilter &other);
45 KFileFilter &operator=(const KFileFilter &other);
46 ~KFileFilter();
47 bool operator==(const KFileFilter &other) const;
48
49 /**
50 * The user-facing label for this filter.
51 *
52 * If no label is passed on creation one is created based on the patterns.
53 */
54 QString label() const;
55
56 /**
57 * List of file name patterns that are included by this filter.
58 */
59 QStringList filePatterns() const;
60
61 /**
62 * List of MIME types that are included by this filter;
63 */
64 QStringList mimePatterns() const;
65
66 /**
67 * Converts this filter to a string representation
68 */
69 QString toFilterString() const;
70
71 /**
72 * Whether the filer is empty, i.e. matches all files.
73 */
74 bool isEmpty() const;
75
76 /**
77 * Whether the filter is valid.
78 *
79 * Creating a filter from an invalid/unkown MIME type will result in an invalid filter.
80 *
81 * @since 6.0
82 */
83 bool isValid() const;
84
85 /*
86 * Creates a filter for one MIME type.
87 * The user-facing label is automatically determined from the MIME type.
88 */
89 static KFileFilter fromMimeType(const QString &mimeType);
90
91 /**
92 * Creates filters from a list of MIME types.
93 * The user-facing label is automatically determined from the MIME type.
94 *
95 * @since 6.0
96 */
97 static QList<KFileFilter> fromMimeTypes(const QStringList &mimeTypes);
98
99private:
100 /**
101 * Convert a filter string understood by KFileWidget to a list of KFileFilters.
102 */
103 static QList<KFileFilter> fromFilterString(const QString &filterString);
104 friend class KFileFilterCombo;
105 friend class KFileFilterTest;
106 friend class KFileFilterComboPrivate;
107 friend class KFileWidgetTest;
108 friend class KFileFilterComboTest;
109 friend class KDEPlatformFileDialog;
110 friend class KDEPlatformFileDialogHelper;
111 friend class KEncodingFileDialog;
112
113 QSharedDataPointer<KFileFilterPrivate> d;
114};
115
116KIOCORE_EXPORT QDebug operator<<(QDebug dbg, const KFileFilter &filter);
117
118#endif
119

source code of kio/src/core/kfilefilter.h