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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2010-12-16
* Description : Filter for filter combobox
*
* SPDX-FileCopyrightText: 2010-2011 by Petri Damstén <petri dot damsten at iki dot fi>
* SPDX-FileCopyrightText: 2014 by Teemu Rytilahti <tpr@iki.fi>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "importfilter.h"
// Qt includes
#include <QMimeType>
#include <QMimeDatabase>
// Local includes
#include "digikam_debug.h"
namespace Digikam
{
QString Filter::toString()
{
return (
QString::fromUtf8("%1|%2|%3|%4|%5")
.arg(name)
.arg(onlyNew ? QLatin1String("true") : QLatin1String("false"))
.arg(fileFilter.join(QLatin1Char(';')))
.arg(pathFilter.join(QLatin1Char(';')))
.arg(mimeFilter)
);
}
void Filter::fromString(const QString& filter)
{
QStringList s = filter.split(QLatin1Char('|'));
name = s.value(0);
onlyNew = (s.value(1) == QLatin1String("true"));
if (!s.value(2).isEmpty())
{
fileFilter = s.value(2).split(QLatin1Char(';'));
}
if (!s.value(3).isEmpty())
{
pathFilter = s.value(3).split(QLatin1Char(';'));
}
if (!s.value(4).isEmpty())
{
mimeFilter = s.value(4);
}
if (!s.value(5).isEmpty())
{
ignoreNames = s.value(5).split(QLatin1Char(' '), Qt::SkipEmptyParts);
}
if (!s.value(6).isEmpty())
{
ignoreExtensions = s.value(6).split(QLatin1Char(' '), Qt::SkipEmptyParts);
}
}
const QRegularExpression& Filter::regexp(const QString& wildcard)
{
if (!filterHash.contains(wildcard))
{
QString pattern = QRegularExpression::wildcardToRegularExpression(wildcard);
pattern.replace(QLatin1String("[^/\\\\]"), QLatin1String(".")); // Windows
pattern.replace(QLatin1String("[^/]"), QLatin1String(".")); // Linux
QRegularExpression rx(pattern, QRegularExpression::CaseInsensitiveOption);
filterHash[wildcard] = rx;
}
return filterHash[wildcard];
}
bool Filter::match(const QStringList& wildcards, const QString& name)
{
bool match = false;
for (const QString& wildcard : std::as_const(wildcards))
{
match = regexp(wildcard).match(name).hasMatch();
/*
qCDebug(DIGIKAM_IMPORTUI_LOG) << "**" << wildcard << name << match;
*/
if (match)
{
break;
}
}
return match;
}
const QStringList& Filter::mimeWildcards(const QString& mime)
{
if (!mimeHash.contains(mime))
{
QStringList& wc = mimeHash[mime];
QStringList list = mime.split(QLatin1Char(';'));
for (const QString& m : std::as_const(list))
{
QMimeType mimet = QMimeDatabase().mimeTypeForName(m);
if (!mimet.isValid())
{
continue;
}
const auto pats = mimet.globPatterns();
for (const QString& pattern : pats)
{
wc.append(pattern);
}
}
}
return mimeHash[mime];
}
bool Filter::matchesCurrentFilter(const CamItemInfo& item)
{
if (onlyNew)
{
if (item.downloaded == CamItemInfo::DownloadedYes)
{
return false;
}
}
QString folder = item.folder.toLower();
QString fname = item.name.toLower();
if (!fileFilter.isEmpty())
{
if (!match(fileFilter, fname))
{
return false;
}
}
if (!pathFilter.isEmpty())
{
if (!match(pathFilter, folder))
{
folder.replace(QLatin1Char('/'),
QLatin1Char(' '));
if (!match(pathFilter, folder))
{
return false;
}
}
}
if (!mimeFilter.isEmpty())
{
if (!match(mimeWildcards(mimeFilter), fname))
{
return false;
}
}
if (!ignoreNames.isEmpty())
{
if (match(ignoreNames, fname))
{
return false;
}
}
if (!ignoreExtensions.isEmpty())
{
for (const QString& ext : std::as_const(ignoreExtensions))<--- Consider using std::all_of or std::none_of algorithm instead of a raw loop.
{
if (fname.endsWith(QLatin1Char('.') + ext))
{
return false;
}
}
}
return true;
}
} // namespace Digikam
|