Torc  0.1
torcmime.cpp
Go to the documentation of this file.
1 /* Class TorcMime
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2013-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 #include <QtGlobal>
24 #include <QMimeDatabase>
25 
26 //Torc
27 #include "torcmime.h"
28 
40 QString TorcMime::MimeTypeForData(const QByteArray &Data)
42 {
43  return QMimeDatabase().mimeTypeForData(Data).name();
44 }
45 
47 QString TorcMime::MimeTypeForData(QIODevice *Device)
48 {
49  if (Device)
50  return QMimeDatabase().mimeTypeForData(Device).name();
51  return QString();
52 }
53 
55 QString TorcMime::MimeTypeForFileNameAndData(const QString &FileName, QIODevice *Device)
56 {
57  if (FileName.isEmpty() || !Device)
58  return QString();
59 
60  return QMimeDatabase().mimeTypeForFileNameAndData(FileName, Device).name();
61 }
62 
64 QString TorcMime::MimeTypeForFileNameAndData(const QString &FileName, const QByteArray &Data)
65 {
66  if (FileName.isEmpty() || Data.isEmpty())
67  return QString();
68 
69  return QMimeDatabase().mimeTypeForFileNameAndData(FileName, Data).name();
70 }
71 
73 QString TorcMime::MimeTypeForName(const QString &Name)
74 {
75  return QMimeDatabase().mimeTypeForName(Name).name();
76 }
77 
79 QString TorcMime::MimeTypeForUrl(const QUrl &Url)
80 {
81  return QMimeDatabase().mimeTypeForUrl(Url).name();
82 }
83 
85 QStringList TorcMime::MimeTypeForFileName(const QString &FileName)
86 {
87  QStringList result;
88 
89  QList<QMimeType> types = QMimeDatabase().mimeTypesForFileName(FileName);
90  foreach (const QMimeType &type, types)
91  result << type.name();
92 
93  (void)result.removeDuplicates();
94  return result;
95 }
96 
101 QStringList TorcMime::ExtensionsForType(const QString &Type)
102 {
103  QStringList extensions;
104 
105  QList<QMimeType> mimes = QMimeDatabase().allMimeTypes();
106  foreach (const QMimeType &mime, mimes)
107  if (mime.name().startsWith(Type, Qt::CaseInsensitive))
108  extensions.append(mime.suffixes());
109 
110  (void)extensions.removeDuplicates();
111  return extensions;
112 }
static QStringList MimeTypeForFileName(const QString &FileName)
Return a list of possible MIME types for the file named Name.
Definition: torcmime.cpp:85
static QString MimeTypeForFileNameAndData(const QString &FileName, QIODevice *Device)
Return a MIME type for the media described by FileName and Device.
Definition: torcmime.cpp:55
static QString MimeTypeForData(const QByteArray &Data)
Return a MIME type for the media described by Data.
Definition: torcmime.cpp:41
static QString MimeTypeForUrl(const QUrl &Url)
Return a MIME type for the media pointed to by Url.
Definition: torcmime.cpp:79
static QStringList ExtensionsForType(const QString &Type)
Returns a list of known file extensions for a given top level MIME type.
Definition: torcmime.cpp:101
static QString MimeTypeForName(const QString &Name)
Return a MIME type for the file named Name.
Definition: torcmime.cpp:73