summaryrefslogtreecommitdiffstats
path: root/source/kde/kde/patch/kio/c801571f.patch
blob: 1045ac4be41a780d63aedd808db32835b163604c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
From c801571f78136692873ea3d2d22e35ac7e5fa530 Mon Sep 17 00:00:00 2001
From: Nicolas Fella <nicolas.fella@gmx.de>
Date: Wed, 28 Dec 2022 21:32:14 +0100
Subject: [PATCH] Restore old behavior for KFileFilterCombo::setFilter

fb2a2268dbb2033bb8483d9bee63cb11f914573e refactored it to use the newly introduced KFileFilter
class to parse the input. However, it actually allows input that is not a valid filter string expression.

For example K3B uses filters like "audio/x-wav |Wave Sound Files", which are now parsed incorrectly.

Also, contrary to KFileWidget's filter strings slashes were allowed unescapted, which broke Krita.

K3B's usage of KFileFilterCombo for this is somewhat questionable, but we should not break it regardless.

Longer-term setFilter should be replaced with API that takes a KFileFilter object to avoid such confusion

Revert the implementation of setFilter to the original one. For setMimeFilter the usage of KFileFilter is fine.
That means we now have separate codepaths for when setFilter and setMimeFilter was called. Longer-term there will
be only one using KFileFilter

BUG: 463309
---
 autotests/kfilefiltercombotest.cpp   | 20 +++++++++
 src/filewidgets/kfilefiltercombo.cpp | 64 +++++++++++++++++++---------
 2 files changed, 65 insertions(+), 19 deletions(-)

diff --git a/autotests/kfilefiltercombotest.cpp b/autotests/kfilefiltercombotest.cpp
index bb155f2de..22a042020 100644
--- a/autotests/kfilefiltercombotest.cpp
+++ b/autotests/kfilefiltercombotest.cpp
@@ -65,6 +65,15 @@ void KFileFilterComboTest::testSetFilter_data()
     QTest::addRow("mutiple_extension_multiple_filter")
         << "*.cpp *.cc *.C|C++ Source Files\n*.h *.H|Header files" << QStringList{"C++ Source Files", "Header files"};
     QTest::addRow("pattern_only") << "*.cpp" << QStringList{"*.cpp"};
+
+    // must handle an unescaped slash https://bugs.kde.org/show_bug.cgi?id=463309
+    QTest::addRow("slash") << "*.c *.cpp|C/C++ Files" << QStringList{"C/C++ Files"};
+
+    QString k3bFilter =
+        "*|All Files\naudio/x-mp3 audio/x-wav application/x-ogg |Sound Files\naudio/x-wav |Wave Sound Files\naudio/x-mp3 |MP3 Sound Files\napplication/x-ogg "
+        "|Ogg Vorbis Sound Files\nvideo/mpeg |MPEG Video Files";
+    QTest::addRow("k3b") << k3bFilter
+                         << QStringList{"All Files", "Sound Files", "Wave Sound Files", "MP3 Sound Files", "Ogg Vorbis Sound Files", "MPEG Video Files"};
 }
 
 void KFileFilterComboTest::testDefaultFilter()
@@ -177,6 +186,17 @@ void KFileFilterComboTest::testFilters_data()
     QTest::addRow("mutiple_extension_multiple_filter")
         << "*.cpp *.cc *.C|C++ Source Files\n*.h *.H|Header files" << QStringList{"*.cpp *.cc *.C|C++ Source Files", "*.h *.H|Header files"};
     QTest::addRow("pattern_only") << "*.cpp" << QStringList{"*.cpp"};
+
+    QString k3bFilter =
+        "*|All Files\naudio/x-mp3 audio/x-wav application/x-ogg |Sound Files\naudio/x-wav |Wave Sound Files\naudio/x-mp3 |MP3 Sound Files\napplication/x-ogg "
+        "|Ogg Vorbis Sound Files\nvideo/mpeg |MPEG Video Files";
+    QTest::addRow("k3b") << k3bFilter
+                         << QStringList{"*|All Files",
+                                        "audio/x-mp3 audio/x-wav application/x-ogg |Sound Files",
+                                        "audio/x-wav |Wave Sound Files",
+                                        "audio/x-mp3 |MP3 Sound Files",
+                                        "application/x-ogg |Ogg Vorbis Sound Files",
+                                        "video/mpeg |MPEG Video Files"};
 }
 
 void KFileFilterComboTest::testFilters()
diff --git a/src/filewidgets/kfilefiltercombo.cpp b/src/filewidgets/kfilefiltercombo.cpp
index 3d93af643..e751f0c25 100644
--- a/src/filewidgets/kfilefiltercombo.cpp
+++ b/src/filewidgets/kfilefiltercombo.cpp
@@ -41,7 +41,8 @@ public:
     QString m_lastFilter;
     QString m_defaultFilter = i18nc("Default mime type filter that shows all file types", "*|All Files");
 
-    QVector<KFileFilter> m_filters;
+    QVector<KFileFilter> m_fileFilters;
+    QStringList m_filters;
     bool m_allTypes;
 };
 
@@ -62,22 +63,31 @@ KFileFilterCombo::KFileFilterCombo(QWidget *parent)
 
 KFileFilterCombo::~KFileFilterCombo() = default;
 
-void KFileFilterCombo::setFilter(const QString &filterString)
+void KFileFilterCombo::setFilter(const QString &filter)
 {
     clear();
     d->m_filters.clear();
+    d->m_fileFilters.clear();
     d->m_hasAllSupportedFiles = false;
 
-    const QVector<KFileFilter> filters = KFileFilter::fromFilterString(filterString);
-
-    if (!filters.isEmpty()) {
-        d->m_filters = filters;
+    if (!filter.isEmpty()) {
+        QString tmp = filter;
+        int index = tmp.indexOf(QLatin1Char('\n'));
+        while (index > 0) {
+            d->m_filters.append(tmp.left(index));
+            tmp.remove(0, index + 1);
+            index = tmp.indexOf(QLatin1Char('\n'));
+        }
+        d->m_filters.append(tmp);
     } else {
-        d->m_filters = KFileFilter::fromFilterString(d->m_defaultFilter);
+        d->m_filters.append(d->m_defaultFilter);
     }
 
-    for (const KFileFilter &filter : std::as_const(d->m_filters)) {
-        addItem(filter.label());
+    QStringList::ConstIterator it;
+    QStringList::ConstIterator end(d->m_filters.constEnd());
+    for (it = d->m_filters.constBegin(); it != end; ++it) {
+        int tab = (*it).indexOf(QLatin1Char('|'));
+        addItem((tab < 0) ? *it : (*it).mid(tab + 1));
     }
 
     d->m_lastFilter = currentText();
@@ -88,7 +98,13 @@ QString KFileFilterCombo::currentFilter() const
 {
     QString f = currentText();
     if (f == itemText(currentIndex())) { // user didn't edit the text
-        f = d->m_filters.value(currentIndex()).toFilterString();
+
+        if (!d->m_filters.isEmpty()) {
+            f = d->m_filters.value(currentIndex());
+        } else {
+            f = d->m_fileFilters.value(currentIndex()).toFilterString();
+        }
+
         if (d->m_isMimeFilter || (currentIndex() == 0 && d->m_hasAllSupportedFiles)) {
             return f; // we have a MIME type as filter
         }
@@ -109,9 +125,13 @@ bool KFileFilterCombo::showsAllTypes() const
 
 QStringList KFileFilterCombo::filters() const
 {
+    if (!d->m_filters.isEmpty()) {
+        return d->m_filters;
+    }
+
     QStringList result;
 
-    for (const KFileFilter &filter : std::as_const(d->m_filters)) {
+    for (const KFileFilter &filter : std::as_const(d->m_fileFilters)) {
         result << filter.toFilterString();
     }
 
@@ -120,18 +140,23 @@ QStringList KFileFilterCombo::filters() const
 
 void KFileFilterCombo::setCurrentFilter(const QString &filterString)
 {
-    auto it = std::find_if(d->m_filters.cbegin(), d->m_filters.cend(), [filterString](const KFileFilter &filter) {
+    if (!d->m_filters.isEmpty()) {
+        setCurrentIndex(d->m_filters.indexOf(filterString));
+        return;
+    }
+
+    auto it = std::find_if(d->m_fileFilters.cbegin(), d->m_fileFilters.cend(), [filterString](const KFileFilter &filter) {
         return filterString == filter.toFilterString();
     });
 
-    if (it == d->m_filters.cend()) {
+    if (it == d->m_fileFilters.cend()) {
         qCWarning(KIO_KFILEWIDGETS_KFILEFILTERCOMBO) << "Could not find filter" << filterString;
         setCurrentIndex(-1);
         Q_EMIT filterChanged();
         return;
     }
 
-    setCurrentIndex(std::distance(d->m_filters.cbegin(), it));
+    setCurrentIndex(std::distance(d->m_fileFilters.cbegin(), it));
     Q_EMIT filterChanged();
 }
 
@@ -139,6 +164,7 @@ void KFileFilterCombo::setMimeFilter(const QStringList &types, const QString &de
 {
     clear();
     d->m_filters.clear();
+    d->m_fileFilters.clear();
     QString delim = QStringLiteral(", ");
     d->m_hasAllSupportedFiles = false;
     bool hasAllFilesFilter = false;
@@ -181,7 +207,7 @@ void KFileFilterCombo::setMimeFilter(const QStringList &types, const QString &de
             filter = KFileFilter::fromMimeType(*it);
         }
 
-        d->m_filters.append(filter);
+        d->m_fileFilters.append(filter);
         addItem(filter.label());
 
         if (type.name() == defaultType) {
@@ -195,7 +221,7 @@ void KFileFilterCombo::setMimeFilter(const QStringList &types, const QString &de
 
     if (d->m_allTypes) {
         QStringList allTypes;
-        for (const KFileFilter &filter : std::as_const(d->m_filters)) {
+        for (const KFileFilter &filter : std::as_const(d->m_fileFilters)) {
             allTypes << filter.mimePatterns().join(QLatin1Char(' '));
         }
 
@@ -203,7 +229,7 @@ void KFileFilterCombo::setMimeFilter(const QStringList &types, const QString &de
 
         if (count() <= 3) { // show the MIME type comments of at max 3 types
             QStringList allComments;
-            for (const KFileFilter &filter : std::as_const(d->m_filters)) {
+            for (const KFileFilter &filter : std::as_const(d->m_fileFilters)) {
                 allComments << filter.label();
             }
 
@@ -214,13 +240,13 @@ void KFileFilterCombo::setMimeFilter(const QStringList &types, const QString &de
         }
 
         insertItem(0, allSupportedFilesFilter.label());
-        d->m_filters.prepend(allSupportedFilesFilter);
+        d->m_fileFilters.prepend(allSupportedFilesFilter);
         setCurrentIndex(0);
     }
 
     if (hasAllFilesFilter) {
         addItem(i18n("All Files"));
-        d->m_filters.append(KFileFilter(i18n("All Files"), {}, {QStringLiteral("application/octet-stream")}));
+        d->m_fileFilters.append(KFileFilter(i18n("All Files"), {}, {QStringLiteral("application/octet-stream")}));
     }
 
     d->m_lastFilter = currentText();
-- 
GitLab