| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 |
|
| 6 |
#include <QtCore/QMimeDatabase> |
| 7 |
#include <QtCore/QMimeType> |
| 8 |
#include <QtCore/QCoreApplication> |
| 9 |
#include <QtCore/QCommandLineParser> |
| 10 |
#include <QtCore/QCommandLineOption> |
| 11 |
#include <QtCore/QDebug> |
| 12 |
|
| 13 |
#include <iostream> |
| 14 |
#include <algorithm> |
| 15 |
#include <iterator> |
| 16 |
|
| 17 |
std::wostream &operator<<(std::wostream &str, const QString &s) |
| 18 |
{ |
| 19 |
str << s.toStdWString(); |
| 20 |
return str; |
| 21 |
} |
| 22 |
|
| 23 |
template <class T> |
| 24 |
std::wostream &operator<<(std::wostream &str, const QList<T> &l) |
| 25 |
{ |
| 26 |
std::copy(l.constBegin(), l.constEnd(), |
| 27 |
std::ostream_iterator<T, wchar_t>(str, L" ")); |
| 28 |
return str; |
| 29 |
} |
| 30 |
|
| 31 |
std::wostream &operator<<(std::wostream &str, const QMimeType &type) |
| 32 |
{ |
| 33 |
str << "Type : " << type.name(); |
| 34 |
const QStringList aliases = type.aliases(); |
| 35 |
if (!aliases.isEmpty()) |
| 36 |
str << " (" << aliases << ')'; |
| 37 |
str << '\n'; |
| 38 |
|
| 39 |
const QStringList parentMimeTypes = type.parentMimeTypes(); |
| 40 |
if (!parentMimeTypes.isEmpty()) |
| 41 |
str << "Inherits: " << parentMimeTypes << '\n'; |
| 42 |
|
| 43 |
if (!type.comment().isEmpty()) |
| 44 |
str << "Comment : " << type.comment() << '\n'; |
| 45 |
|
| 46 |
const QStringList globPatterns = type.globPatterns(); |
| 47 |
if (!globPatterns.isEmpty()) |
| 48 |
str << "Patterns: " << globPatterns << '\n'; |
| 49 |
|
| 50 |
if (!type.preferredSuffix().isEmpty()) { |
| 51 |
str << "Suffix : " << type.preferredSuffix(); |
| 52 |
const QStringList suffixes = type.suffixes(); |
| 53 |
if (suffixes.size() > 1) |
| 54 |
str << " (" << suffixes << ')'; |
| 55 |
str << '\n'; |
| 56 |
} |
| 57 |
|
| 58 |
if (!type.iconName().isEmpty()) |
| 59 |
str << "Icon : " << type.iconName() << '\n'; |
| 60 |
|
| 61 |
return str; |
| 62 |
} |
| 63 |
|
| 64 |
bool operator<(const QMimeType &t1, const QMimeType &t2) |
| 65 |
{ |
| 66 |
return t1.name() < t2.name(); |
| 67 |
} |
| 68 |
|
| 69 |
int main(int argc, char *argv[]) |
| 70 |
{ |
| 71 |
QCoreApplication app(argc, argv); |
| 72 |
QCoreApplication::setApplicationVersion(QT_VERSION_STR); |
| 73 |
|
| 74 |
QCommandLineParser parser; |
| 75 |
parser.addHelpOption(); |
| 76 |
parser.addVersionOption(); |
| 77 |
QCommandLineOption matchContentOnlyOption(QStringList() << "c" << "content", |
| 78 |
"Use only the file content for determining the mimetype."); |
| 79 |
parser.addOption(matchContentOnlyOption); |
| 80 |
QCommandLineOption matchFileOnlyOption(QStringList() << "f" << "filename-only", |
| 81 |
"Whether use the file name only for determining the mimetype. Not used if -c is specified."); |
| 82 |
parser.addOption(matchFileOnlyOption); |
| 83 |
QCommandLineOption dumpAllOption("a", "Dump all mime types."); |
| 84 |
parser.addOption(dumpAllOption); |
| 85 |
parser.addPositionalArgument("file", "The file(s) to analyze."); |
| 86 |
parser.process(app); |
| 87 |
|
| 88 |
QMimeDatabase::MatchMode matchMode = QMimeDatabase::MatchDefault; |
| 89 |
if (parser.isSet(matchContentOnlyOption)) |
| 90 |
matchMode = QMimeDatabase::MatchContent; |
| 91 |
else if (parser.isSet(matchFileOnlyOption)) |
| 92 |
matchMode = QMimeDatabase::MatchExtension; |
| 93 |
|
| 94 |
const bool dumpAll = parser.isSet(dumpAllOption); |
| 95 |
|
| 96 |
QMimeDatabase mimeDatabase; |
| 97 |
|
| 98 |
if (dumpAll) { |
| 99 |
QList<QMimeType> mimeTypes = mimeDatabase.allMimeTypes(); |
| 100 |
std::stable_sort(mimeTypes.begin(), mimeTypes.end()); |
| 101 |
std::wcout << mimeTypes.size() << " mime types found.\n\n"; |
| 102 |
foreach (const QMimeType &type, mimeTypes) |
| 103 |
std::wcout << type << '\n'; |
| 104 |
} else { |
| 105 |
foreach (const QString &fileName, parser.positionalArguments()) { |
| 106 |
QMimeType data = mimeDatabase.mimeTypeForFile(fileName, matchMode); |
| 107 |
std::wcout << "File : " << fileName << '\n' << data << '\n'; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |