Qt 6.x
The Qt SDK
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#include "qmimeprovider_p.h"
10
11#include "qmimeglobpattern_p.h"
12
13#include <QtCore/QDebug>
14#include <QtCore/QLocale>
15#include <QtCore/QHashFunctions>
16
17#include <memory>
18
20
21using namespace Qt::StringLiterals;
22
24 : loaded(false), fromCache(false)
25{}
26
28 : loaded(other.d->loaded),
29 name(other.d->name),
30 localeComments(other.d->localeComments),
31 genericIconName(other.d->genericIconName),
32 iconName(other.d->iconName),
33 globPatterns(other.d->globPatterns)
34{}
35
37{
38 name.clear();
42 globPatterns.clear();
43}
44
46{
47 globPatterns.append(pattern);
48}
49
89 d(new QMimeTypePrivate())
90{
91}
92
98 d(other.d)
99{
100}
101
107{
108 if (d != other.d)
109 d = other.d;
110 return *this;
111}
112
119 d(new QMimeTypePrivate(dd))
120{
121}
122
140{
141}
142
150{
151 return d == other.d || d->name == other.d->name;
152}
153
161size_t qHash(const QMimeType &key, size_t seed) noexcept
162{
163 return qHash(key.d->name, seed);
164}
165
182{
183 return !d->name.isEmpty();
184}
185
195{
197}
198
207{
208 return d->name;
209}
210
221{
223
224 QStringList languageList;
225 languageList << QLocale().name();
226 languageList << QLocale().uiLanguages();
227 languageList << u"default"_s; // use the default locale if possible.
228 for (const QString &language : std::as_const(languageList)) {
229 const QString lang = language == "C"_L1 ? u"en_US"_s : language;
230 const QString comm = d->localeComments.value(lang);
231 if (!comm.isEmpty())
232 return comm;
233 const qsizetype pos = lang.indexOf(u'_');
234 if (pos != -1) {
235 // "pt_BR" not found? try just "pt"
236 const QString shortLang = lang.left(pos);
237 const QString commShort = d->localeComments.value(shortLang);
238 if (!commShort.isEmpty())
239 return commShort;
240 }
241 }
242
243 // Use the mimetype name as fallback
244 return d->name;
245}
246
262{
264 if (d->genericIconName.isEmpty()) {
265 // From the spec:
266 // If the generic icon name is empty (not specified by the mimetype definition)
267 // then the mimetype is used to generate the generic icon by using the top-level
268 // media type (e.g. "video" in "video/ogg") and appending "-x-generic"
269 // (i.e. "video-x-generic" in the previous example).
270 const QString group = name();
271 QStringView groupRef(group);
272 const qsizetype slashindex = groupRef.indexOf(u'/');
273 if (slashindex != -1)
274 groupRef = groupRef.left(slashindex);
275 return groupRef + "-x-generic"_L1;
276 }
277 return d->genericIconName;
278}
279
281{
282 const qsizetype slashindex = iconName.indexOf(u'/');
283 if (slashindex != -1)
284 iconName[slashindex] = u'-';
285 return iconName;
286}
287
298{
300 if (d->iconName.isEmpty()) {
302 }
303 return d->iconName;
304}
305
314{
316 return d->globPatterns;
317}
318
338{
340}
341
342static void collectParentMimeTypes(const QString &mime, QStringList &allParents)
343{
345 for (const QString &parent : parents) {
346 // I would use QSet, but since order matters I better not
347 if (!allParents.contains(parent))
348 allParents.append(parent);
349 }
350 // We want a breadth-first search, so that the least-specific parent (octet-stream) is last
351 // This means iterating twice, unfortunately.
352 for (const QString &parent : parents)
353 collectParentMimeTypes(parent, allParents);
354}
355
373{
374 QStringList allParents;
375 collectParentMimeTypes(d->name, allParents);
376 return allParents;
377}
378
395{
397}
398
409{
411
413 for (const QString &pattern : std::as_const(d->globPatterns)) {
414 // Not a simple suffix if it looks like: README or *. or *.* or *.JP*G or *.JP?
415 if (pattern.startsWith("*."_L1) &&
416 pattern.size() > 2 &&
417 pattern.indexOf(u'*', 2) < 0 && pattern.indexOf(u'?', 2) < 0) {
418 const QString suffix = pattern.mid(2);
419 result.append(suffix);
420 }
421 }
422
423 return result;
424}
425
437{
438 if (isDefault()) // workaround for unwanted *.bin suffix for octet-stream, https://bugs.freedesktop.org/show_bug.cgi?id=101667, fixed upstream in 1.10
439 return QString();
440 const QStringList suffixList = suffixes();
441 return suffixList.isEmpty() ? QString() : suffixList.at(0);
442}
443
452{
455
456 if (!d->globPatterns.empty()) {
457 filter += comment() + " ("_L1;
458 for (int i = 0; i < d->globPatterns.size(); ++i) {
459 if (i != 0)
460 filter += u' ';
461 filter += d->globPatterns.at(i);
462 }
463 filter += u')';
464 }
465
466 return filter;
467}
468
477bool QMimeType::inherits(const QString &mimeTypeName) const
478{
479 if (d->name == mimeTypeName)
480 return true;
481 return QMimeDatabasePrivate::instance()->mimeInherits(d->name, mimeTypeName);
482}
483
484#ifndef QT_NO_DEBUG_STREAM
486{
487 QDebugStateSaver saver(debug);
488 if (!mime.isValid()) {
489 debug.nospace() << "QMimeType(invalid)";
490 } else {
491 debug.nospace() << "QMimeType(" << mime.name() << ")";
492 }
493 return debug;
494}
495#endif
496
498
499#include "moc_qmimetype.cpp"
\inmodule QtCore
\inmodule QtCore
T value(const Key &key) const noexcept
Definition qhash.h:1044
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:949
QStringList uiLanguages() const
List of locale names for use in selecting translations.
Definition qlocale.cpp:4580
QString name() const
The short name of this locale.
Definition qlocale.cpp:1340
QStringList listAliases(const QString &mimeName)
bool mimeInherits(const QString &mime, const QString &parent)
void loadIcon(QMimeTypePrivate &mimePrivate)
void loadGenericIcon(QMimeTypePrivate &mimePrivate)
QStringList mimeParents(const QString &mimeName)
const QString & defaultMimeType() const
void loadMimeTypePrivate(QMimeTypePrivate &mimePrivate)
static QMimeDatabasePrivate * instance()
LocaleHash localeComments
Definition qmimetype_p.h:44
QString genericIconName
Definition qmimetype_p.h:45
QStringList globPatterns
Definition qmimetype_p.h:47
void addGlobPattern(const QString &pattern)
Definition qmimetype.cpp:45
\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
bool operator==(const QMimeType &other) const
Returns true if other equals this QMimeType object, otherwise returns false.
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.
QMimeType()
Constructs this QMimeType object initialized with default property values that indicate an invalid MI...
Definition qmimetype.cpp:88
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:76
constexpr QStringView left(qsizetype n) const noexcept
qsizetype indexOf(QChar c, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1107
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
Definition qstring.cpp:5204
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QString & append(QChar c)
Definition qstring.cpp:3227
QString left(qsizetype n) const
Returns a substring that contains the n leftmost characters of the string.
Definition qstring.cpp:5161
static QString static QString qsizetype indexOf(QChar c, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4420
Combined button and popup list for selecting options.
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
QDebug operator<<(QDebug debug, const QMimeType &mime)
static QString make_default_icon_name_from_mimetype_name(QString iconName)
static void collectParentMimeTypes(const QString &mime, QStringList &allParents)
GLuint64 key
GLboolean GLuint group
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLuint name
GLuint64EXT * result
[6]
GLubyte * pattern
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
ptrdiff_t qsizetype
Definition qtypes.h:70
application x qt windows mime
[2]
QSharedPointer< T > other(t)
[5]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent