File indexing completed on 2024-11-10 05:02:44
0001 /* 0002 SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "suffixcheck.h" 0008 0009 #include <mutex> 0010 0011 #include <QImageReader> 0012 #include <QMimeDatabase> 0013 #include <QSet> 0014 0015 namespace 0016 { 0017 constinit QStringList s_suffixes; 0018 constinit std::once_flag s_onceFlag; 0019 0020 void fillSuffixes() 0021 { 0022 Q_ASSERT(s_suffixes.empty()); 0023 0024 QSet<QString> suffixeSet; 0025 QMimeDatabase db; 0026 const auto supportedMimeTypes = QImageReader::supportedMimeTypes(); 0027 0028 for (const QByteArray &mimeType : supportedMimeTypes) { 0029 QMimeType mime(db.mimeTypeForName(QString::fromLatin1(mimeType))); 0030 const QStringList globPatterns = mime.globPatterns(); 0031 0032 for (const QString &pattern : globPatterns) { 0033 suffixeSet.insert(pattern); 0034 } 0035 } 0036 0037 s_suffixes = suffixeSet.values(); 0038 } 0039 } 0040 0041 const QStringList &suffixes() 0042 { 0043 std::call_once(s_onceFlag, &fillSuffixes); 0044 return s_suffixes; 0045 } 0046 0047 bool isAcceptableSuffix(QString &&suffix) 0048 { 0049 // Despite its name, suffixes() returns a list of glob patterns. 0050 // Therefore the file suffix check needs to include the "*." prefix. 0051 std::call_once(s_onceFlag, &fillSuffixes); 0052 return s_suffixes.contains(QLatin1String("*.%1").arg(std::move(suffix).toLower())); 0053 }