Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qmimetype.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2015 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author David Faure <david.faure@kdab.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qmimetype.h"
6
7#include "qmimetype_p.h"
8#include "qmimedatabase_p.h"
9
10#include <QtCore/QDebug>
11#include <QtCore/QLocale>
12#include <QtCore/QHashFunctions>
13
14#include <memory>
15
17
18using namespace Qt::StringLiterals;
19
63
72
78{
79 if (d != other.d)
80 d = other.d;
81 return *this;
82}
83
93
113
121bool comparesEqual(const QMimeType &lhs, const QMimeType &rhs) noexcept
122{
123 return lhs.d == rhs.d || lhs.d->name == rhs.d->name;
124}
125
133size_t qHash(const QMimeType &key, size_t seed) noexcept
134{
135 return qHash(key.d->name, seed);
136}
137
155{
156 return !d->name.isEmpty();
157}
158
168{
169 return d->name == QMimeDatabasePrivate::instance()->defaultMimeType();
170}
171
180{
181 return d->name;
182}
183
194{
195 const auto localeComments = QMimeDatabasePrivate::instance()->localeComments(d->name);
196
198 qsizetype defaultIndex = languageList.indexOf(u"en_US"_s);
199
200 // Include the default locale as fall-back.
201 if (defaultIndex >= 0) {
202 // en_US is generally the default, and may be omitted from the
203 // overtly-named locales in the MIME type's data (QTBUG-105007).
204 ++defaultIndex; // Skip over en_US.
205 // That's typically followed by en_Latn_US and en (in that order):
206 if (defaultIndex < languageList.size() && languageList.at(defaultIndex) == u"en_Latn_US")
207 ++defaultIndex;
208 if (defaultIndex < languageList.size() && languageList.at(defaultIndex) == u"en")
209 ++defaultIndex;
210 } else {
211 // Absent en-US, just append it:
212 defaultIndex = languageList.size();
213 }
214 languageList.insert(defaultIndex, u"default"_s);
215
216 for (const QString &language : std::as_const(languageList)) {
217 const QString lang = language == "C"_L1 ? u"en_US"_s : language;
218 QString comm = localeComments.value(lang);
219 if (!comm.isEmpty())
220 return comm;
221 const qsizetype cut = lang.indexOf(u'_');
222 // If "de_CH" is missing, check for "de" (and similar):
223 if (cut != -1) {
224 comm = localeComments.value(lang.left(cut));
225 if (!comm.isEmpty())
226 return comm;
227 }
228 }
229
230 // Use the mimetype name as fallback
231 return d->name;
232}
233
249{
251 if (genericIconName.isEmpty()) {
252 // From the spec:
253 // If the generic icon name is empty (not specified by the mimetype definition)
254 // then the mimetype is used to generate the generic icon by using the top-level
255 // media type (e.g. "video" in "video/ogg") and appending "-x-generic"
256 // (i.e. "video-x-generic" in the previous example).
257 const QString group = name();
258 QStringView groupRef(group);
259 const qsizetype slashindex = groupRef.indexOf(u'/');
260 if (slashindex != -1)
261 groupRef = groupRef.left(slashindex);
262 return groupRef + "-x-generic"_L1;
263 }
264 return genericIconName;
265}
266
268{
269 const qsizetype slashindex = iconName.indexOf(u'/');
270 if (slashindex != -1)
271 iconName[slashindex] = u'-';
272 return iconName;
273}
274
292
301{
302 return QMimeDatabasePrivate::instance()->globPatterns(d->name);
303}
304
324{
325 return QMimeDatabasePrivate::instance()->mimeParents(d->name);
326}
327
328static void collectParentMimeTypes(const QString &mime, QStringList &allParents)
329{
330 const QStringList parents = QMimeDatabasePrivate::instance()->mimeParents(mime);
331 QStringList newParents;
332 for (const QString &parent : parents) {
333 // I would use QSet, but since order matters I better not
334 if (!allParents.contains(parent)) {
335 allParents.append(parent);
336 newParents.append(parent);
337 }
338 }
339 // We want a breadth-first search, so that the least-specific parent (octet-stream) is last
340 // This means iterating twice, unfortunately.
341 for (const QString &parent : newParents)
342 collectParentMimeTypes(parent, allParents);
343}
344
362{
363 QStringList allParents;
364 collectParentMimeTypes(d->name, allParents);
365 return allParents;
366}
367
384{
385 return QMimeDatabasePrivate::instance()->listAliases(d->name);
386}
387
398{
399 const QStringList patterns = globPatterns();
400
402 result.reserve(patterns.size());
403 for (const QString &pattern : patterns) {
404 // Not a simple suffix if it looks like: README or *. or *.* or *.JP*G or *.JP?
405 if (pattern.startsWith("*."_L1) &&
406 pattern.size() > 2 &&
407 pattern.indexOf(u'*', 2) < 0 && pattern.indexOf(u'?', 2) < 0) {
408 const QString suffix = pattern.mid(2);
409 result.append(suffix);
410 }
411 }
412
413 return result;
414}
415
427{
428 if (isDefault()) // workaround for unwanted *.bin suffix for octet-stream, https://bugs.freedesktop.org/show_bug.cgi?id=101667, fixed upstream in 1.10
429 return QString();
430 const QStringList suffixList = suffixes();
431 return suffixList.isEmpty() ? QString() : suffixList.at(0);
432}
433
442{
443 const QStringList patterns = globPatterns();
445
446 if (!patterns.isEmpty()) {
447 filter = comment() + " ("_L1 + patterns.join(u' ') + u')';
448 }
449
450 return filter;
451}
452
461bool QMimeType::inherits(const QString &mimeTypeName) const
462{
463 if (d->name == mimeTypeName)
464 return true;
465 return QMimeDatabasePrivate::instance()->mimeInherits(d->name, mimeTypeName);
466}
467
468#ifndef QT_NO_DEBUG_STREAM
470{
471 QDebugStateSaver saver(debug);
472 if (!mime.isValid()) {
473 debug.nospace() << "QMimeType(invalid)";
474 } else {
475 debug.nospace() << "QMimeType(" << mime.name() << ")";
476 }
477 return debug;
478}
479#endif
480
482
483#include "moc_qmimetype.cpp"
\inmodule QtCore
\inmodule QtCore
QStringList uiLanguages(TagSeparator separator=TagSeparator::Dash) const
List of locale names for use in selecting translations.
Definition qlocale.cpp:4748
static QMimeDatabasePrivate * instance()
\inmodule QtCore
Definition qmimetype.h:25
Q_INVOKABLE bool inherits(const QString &mimeTypeName) const
Returns true if this mimetype is mimeTypeName, or inherits mimeTypeName (see parentMimeTypes()),...
bool isValid() const
QStringList globPatterns
the list of glob matching patterns
Definition qmimetype.h:33
QStringList parentMimeTypes
the names of parent MIME types
Definition qmimetype.h:34
QStringList allAncestors
the names of direct and indirect parent MIME types
Definition qmimetype.h:35
QStringList aliases
the list of aliases of this mimetype
Definition qmimetype.h:36
QStringList suffixes
the known suffixes for the MIME type
Definition qmimetype.h:37
QExplicitlySharedDataPointer< QMimeTypePrivate > d
Definition qmimetype.h:88
QString comment
the description of the MIME type to be displayed on user interfaces
Definition qmimetype.h:30
QMimeType & operator=(const QMimeType &other)
Move-assigns other to this QMimeType instance.
Definition qmimetype.cpp:77
QMimeType()
Constructs this QMimeType object initialized with default property values that indicate an invalid MI...
Definition qmimetype.cpp:59
QString preferredSuffix
the preferred suffix for the MIME type
Definition qmimetype.h:38
QString iconName
the file name of an icon image that represents the MIME type
Definition qmimetype.h:32
bool isDefault
true if this MIME type is the default MIME type which applies to all files: application/octet-stream.
Definition qmimetype.h:28
QString name
the name of the MIME type
Definition qmimetype.h:29
~QMimeType()
Destroys the QMimeType object, and releases the d pointer.
QString genericIconName
the file name of a generic icon that represents the MIME type
Definition qmimetype.h:31
QString filterString
a filter string usable for a file dialog
Definition qmimetype.h:39
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString left(qsizetype n) const &
Definition qstring.h:363
qsizetype indexOf(QLatin1StringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4517
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QString & append(QChar c)
Definition qstring.cpp:3252
Combined button and popup list for selecting options.
static jboolean cut(JNIEnv *, jobject)
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
static qsizetype defaultIndex()
Definition qlocale.cpp:840
QDebug operator<<(QDebug debug, const QMimeType &mime)
static QString make_default_icon_name_from_mimetype_name(QString iconName)
bool comparesEqual(const QMimeType &lhs, const QMimeType &rhs) noexcept
static void collectParentMimeTypes(const QString &mime, QStringList &allParents)
GLuint64 key
GLboolean GLuint group
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLuint64EXT * result
[6]
GLubyte * pattern
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
ptrdiff_t qsizetype
Definition qtypes.h:165
application x qt windows mime
[2]
QSharedPointer< T > other(t)
[5]