5#include <qplatformdefs.h>
13#include <private/qfilesystementry_p.h>
16#include <QtCore/QFile>
17#include <QtCore/QFileInfo>
18#include <QtCore/QStandardPaths>
19#include <QtCore/QBuffer>
21#include <QtCore/QDebug>
44 return staticQMimeDatabase();
57#ifdef QT_BUILD_INTERNAL
64bool QMimeDatabasePrivate::shouldCheck()
77#if defined(Q_OS_UNIX) && !defined(Q_OS_NACL) && !defined(Q_OS_INTEGRITY)
81void QMimeDatabasePrivate::loadProviders()
85 const auto fdoIterator = std::find_if(mimeDirs.constBegin(), mimeDirs.constEnd(), [](
const QString &mimeDir) ->
bool {
86 return QFileInfo::exists(mimeDir +
"/packages/freedesktop.org.xml"_L1); }
91 Providers currentProviders;
92 std::swap(m_providers, currentProviders);
94 m_providers.reserve(mimeDirs.size() + (needInternalDB ? 1 : 0));
96 for (
const QString &mimeDir : mimeDirs) {
97 const QString cacheFile = mimeDir +
"/mime.cache"_L1;
99 const auto predicate = [mimeDir](
const std::unique_ptr<QMimeProviderBase> &prov)
101 return prov && prov->directory() == mimeDir;
103 const auto it = std::find_if(currentProviders.begin(), currentProviders.end(),
predicate);
104 if (
it == currentProviders.
end()) {
105 std::unique_ptr<QMimeProviderBase> provider;
106#if defined(QT_USE_MMAP)
110 if (!provider->isValid()) {
119 m_providers.push_back(std::move(provider));
121 auto provider = std::move(*
it);
122 provider->ensureLoaded();
123 if (!provider->isValid()) {
127 m_providers.push_back(std::move(provider));
132 if (needInternalDB) {
134 const auto isInternal = [](
const std::unique_ptr<QMimeProviderBase> &prov)
136 return prov && prov->isInternalDatabase();
138 const auto it = std::find_if(currentProviders.begin(), currentProviders.end(), isInternal);
139 if (
it == currentProviders.
end()) {
142 m_providers.push_back(std::move(*
it));
147 auto it = m_providers.begin();
148 const auto end = m_providers.end();
154 auto nextIt =
it + 1;
155 for (; nextIt !=
end; ++nextIt)
156 (*nextIt)->excludeMimeTypeGlobs(
list);
160const QMimeDatabasePrivate::Providers &QMimeDatabasePrivate::providers()
165 if (m_providers.empty()) {
177 for (
const auto &provider : providers()) {
178 const QString ret = provider->resolveAlias(nameOrAlias);
192 for (
const auto &provider : providers()) {
207 matchingMimeTypes.sort();
208 return matchingMimeTypes;
215 for (
const auto &provider : providers())
216 provider->addFileNameMatches(fileNameExcludingPath,
result);
225 if (!mimePrivate.
loaded) {
228 for (
const auto &provider : providers()) {
229 if (provider->loadMimeTypePrivate(mimePrivate)) {
236 qWarning() <<
"No file found for" <<
file <<
", even though update-mime-info said it would exist.\n"
237 "Either it was just removed, or the directory doesn't have executable permission..."
240 mimePrivate.
loaded =
true;
249 for (
const auto &provider : providers()) {
250 provider->loadGenericIcon(mimePrivate);
262 for (
const auto &provider : providers()) {
263 provider->loadIcon(mimePrivate);
270QString QMimeDatabasePrivate::fallbackParent(
const QString &mimeTypeName)
const
277 if (myGroup !=
"inode"_L1 &&
279 myGroup !=
"all"_L1 && myGroup !=
"fonts"_L1 && myGroup !=
"print"_L1 && myGroup !=
"uri"_L1
296 for (
const auto &provider : providers())
297 provider->addParents(mimeName,
result);
310 for (
const auto &provider : providers())
311 provider->addAliases(mimeName,
result);
324 static const char bigEndianBOM[] =
"\xFE\xFF";
325 static const char littleEndianBOM[] =
"\xFF\xFE";
326 if (
data.startsWith(bigEndianBOM) ||
data.startsWith(littleEndianBOM))
330 const char *
p =
data.constData();
332 for ( ;
p <
e; ++
p) {
333 if (
static_cast<unsigned char>(*
p) < 32 && *
p != 9 && *
p !=10 && *
p != 13)
342 if (
data.isEmpty()) {
349 for (
const auto &provider : providers())
350 provider->findByMagic(
data, accuracyPtr, candidate);
377 candidatesByName = {};
382 const auto matchOnContent = [
this, &candidatesByName](
QIODevice *
device) {
392 int magicAccuracy = 0;
396 if (candidateByData.
isValid() && magicAccuracy > 0) {
397 const QString sniffedMime = candidateByData.
name();
400 return candidateByData;
410 return candidateByData;
426 return matchOnContent(
device);
429 return matchOnContent(&fallbackFile);
469 QT_STATBUF statBuffer;
470 if (QT_STAT(nativeFilePath.
constData(), &statBuffer) == 0) {
471 if (S_ISDIR(statBuffer.st_mode))
473 if (S_ISCHR(statBuffer.st_mode))
475 if (S_ISBLK(statBuffer.st_mode))
477 if (S_ISFIFO(statBuffer.st_mode))
479 if (S_ISSOCK(statBuffer.st_mode))
483 }
else if (fileInfo.
isDir()) {
504 for (
const auto &provider : providers())
505 provider->addAllMimeTypes(
result);
512 std::stack<QString, QStringList> toCheck;
514 while (!toCheck.empty()) {
515 if (toCheck.top() == resolvedParent)
517 const QString mimeName = toCheck.top();
519 const auto parentList =
parents(mimeName);
520 for (
const QString &par : parentList)
576 d(staticQMimeDatabase())
685 return fileName.right(suffixLength);
737 if (scheme.
startsWith(
"http"_L1) || scheme ==
"mailto"_L1)
IOBluetoothDevice * device
\inmodule QtCore \reentrant
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
qint64 elapsed() const noexcept
Returns the number of milliseconds since this QElapsedTimer was last started.
void start() noexcept
Starts this timer.
bool isValid() const noexcept
Returns false if the timer has never been started or invalidated by a call to invalidate().
\inmodule QtCore \reentrant
bool isNativePath() const
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory.
QString filePath() const
Returns the file name, including the path (which may be absolute or relative).
bool exists() const
Returns true if the file exists; otherwise returns false.
Q_AUTOTEST_EXPORT QString fileName() const
static QByteArray encodeName(const QString &fileName)
Converts fileName to an 8-bit encoding that you can use in native APIs.
\inmodule QtCore \reentrant
bool isEmpty() const noexcept
void reserve(qsizetype size)
void append(parameter_type t)
QString resolveAlias(const QString &nameOrAlias)
QStringList listAliases(const QString &mimeName)
QList< QMimeType > allMimeTypes()
bool mimeInherits(const QString &mime, const QString &parent)
QMimeType mimeTypeForFileExtension(const QString &fileName)
QMimeType mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device)
void loadIcon(QMimeTypePrivate &mimePrivate)
QMimeType mimeTypeForData(QIODevice *device)
QStringList mimeTypeForFileName(const QString &fileName)
bool inherits(const QString &mime, const QString &parent)
QMimeType mimeTypeForName(const QString &nameOrAlias)
void loadGenericIcon(QMimeTypePrivate &mimePrivate)
QStringList mimeParents(const QString &mimeName)
const QString & defaultMimeType() const
void loadMimeTypePrivate(QMimeTypePrivate &mimePrivate)
QMimeType mimeTypeForFile(const QString &fileName, const QFileInfo &fileInfo, QMimeDatabase::MatchMode mode)
QStringList parents(const QString &mimeName)
QMimeGlobMatchResult findByFileName(const QString &fileName)
QMimeType findByData(const QByteArray &data, int *priorityPtr)
QMimeType mimeTypeForName(const QString &nameOrAlias) const
Returns a MIME type for nameOrAlias or an invalid one if none found.
~QMimeDatabase()
Destroys the QMimeDatabase object.
QMimeType mimeTypeForFileNameAndData(const QString &fileName, QIODevice *device) const
Returns a MIME type for the given fileName and device data.
QList< QMimeType > mimeTypesForFileName(const QString &fileName) const
Returns the MIME types for the file name fileName.
QMimeDatabase()
Constructs a QMimeDatabase object.
QMimeType mimeTypeForUrl(const QUrl &url) const
Returns a MIME type for url.
QMimeType mimeTypeForData(const QByteArray &data) const
Returns a MIME type for data.
QString suffixForFileName(const QString &fileName) const
Returns the suffix for the file fileName, as known by the MIME database.
MatchMode
This enum specifies how matching a file to a MIME type is performed.
QList< QMimeType > allMimeTypes() const
Returns the list of all available MIME types.
QMimeType mimeTypeForFile(const QString &fileName, MatchMode mode=MatchDefault) const
Returns a MIME type for the file named fileName using mode.
QString name
the name of the MIME type
@ InternalDatabaseAvailable
bool tryLock(int timeout=0) noexcept
Attempts to lock the mutex.
static QStringList locateAll(StandardLocation type, const QString &fileName, LocateOptions options=LocateFile)
[0]
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
void clear()
Clears the contents of the string and makes it null.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
QString left(qsizetype n) const
Returns a substring that contains the n leftmost characters of the string.
static QString static QString qsizetype indexOf(QChar c, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
QString scheme() const
Returns the scheme of the URL.
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
QSet< QString >::iterator it
Combined button and popup list for selecting options.
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
static Q_CONSTINIT const int qmime_secondsBetweenChecks
static bool isTextFile(const QByteArray &data)
static QStringList locateMimeDirectories()
static QString directoryMimeType()
static QString plainTextMimeType()
constexpr const T & qMin(const T &a, const T &b)
static bool matches(const QJsonObject &object, const QString &osName, const QVersionNumber &kernelVersion, const QString &osRelease, const QOpenGLConfig::Gpu &gpu)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define QStringLiteral(str)
Q_CORE_EXPORT bool qEnvironmentVariableIsEmpty(const char *varName) noexcept
QUrl url("example.com")
[constructor-url-reference]
application x qt windows mime
[2]
The QMimeGlobMatchResult class accumulates results from glob matching.
QStringList m_allMatchingMimeTypes
qsizetype m_knownSuffixLength
QStringList m_matchingMimeTypes
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent