KMimeType

#include <kmimetype.h>

Inheritance diagram for KMimeType:

Public Types

enum  FindByNameOption { ResolveAliases = 1 }
 
typedef QList< Ptr > List
 

Public Member Functions

 KMimeType (const QMimeType &mime)
 
QStringList allParentMimeTypes () const
 
QString comment () const
 
QString iconName () const
 
bool is (const QString &mimeTypeName) const
 
bool isDefault () const
 
QString mainExtension () const
 
QString name () const
 
QString parentMimeType () const
 
QStringList parentMimeTypes () const
 
QStringList patterns () const
 
QString userSpecifiedIconName () const
 
- Public Member Functions inherited from QSharedData
 QSharedData (const QSharedData &)
 

Static Public Member Functions

static List allMimeTypes ()
 
static QString defaultMimeType ()
 
static KMimeType::Ptr defaultMimeTypePtr ()
 
static QString extractKnownExtension (const QString &fileName)
 
static QString favIconForUrl (const QUrl &url)
 
static Ptr findByContent (const QByteArray &data, int *accuracy=nullptr)
 
static Ptr findByContent (QIODevice *device, int *accuracy=nullptr)
 
static Ptr findByFileContent (const QString &fileName, int *accuracy=nullptr)
 
static Ptr findByNameAndContent (const QString &name, const QByteArray &data, mode_t mode=0, int *accuracy=nullptr)
 
static Ptr findByNameAndContent (const QString &name, QIODevice *device, mode_t mode=0, int *accuracy=nullptr)
 
static Ptr findByPath (const QString &path, mode_t mode=0, bool fast_mode=false, int *accuracy=nullptr)
 
static Ptr findByUrl (const QUrl &url, mode_t mode=0, bool is_local_file=false, bool fast_mode=false, int *accuracy=nullptr)
 
static QString iconNameForUrl (const QUrl &url, mode_t mode=0)
 
static bool isBinaryData (const QString &fileName)
 
static bool isBufferBinaryData (const QByteArray &data)
 
static bool matchFileName (const QString &filename, const QString &pattern)
 
static Ptr mimeType (const QString &name, FindByNameOption options=ResolveAliases)
 
static int sharedMimeInfoVersion ()
 

Detailed Description

Represent a mime type, like "text/plain", and the data that is associated with it.

The starting point you need is often the static methods.

KMimeType inherits KServiceType because "text/plain" can be used to find services (apps and components) "which can open text/plain".

See also
KServiceType
Deprecated:
use QMimeType, see https://community.kde.org/Frameworks/Porting_Notes#Mime_Types

Definition at line 50 of file kmimetype.h.

Constructor & Destructor Documentation

KMimeType::KMimeType ( const QMimeType mime)

Construct a KMimeType from a QMimeType.

This is mostly useful as a temporary measure during porting.

Definition at line 279 of file kmimetype.cpp.

Member Function Documentation

KMimeType::List KMimeType::allMimeTypes ( )
static

Get all the mimetypes.

Useful for showing the list of available mimetypes. More memory consuming than the ones above, don't use unless really necessary.

Returns
the list of all existing KMimeTypes

Definition at line 69 of file kmimetype.cpp.

QStringList KMimeType::allParentMimeTypes ( ) const

Return all parent mimetypes of this mimetype, direct or indirect.

This includes the parent(s) of its parent(s), etc. If this mimetype is an alias, the list also contains the canonical name for this mimetype.

The usual reason to use this method is to look for a setting which is stored per mimetype (like PreviewJob does).

Since
4.1

Definition at line 326 of file kmimetype.cpp.

QString KMimeType::comment ( ) const

Returns the descriptive comment associated with the MIME type.

Returns
The descriptive comment associated with the MIME type, if any.

Definition at line 300 of file kmimetype.cpp.

QString KMimeType::defaultMimeType ( )
static

Returns the name of the default mimetype.

Always application/octet-stream, but this method exists for performance purposes.

Returns
the name of the default mime type, always "application/octet-stream"

Definition at line 331 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::defaultMimeTypePtr ( )
static

Returns the default mimetype.

Always application/octet-stream. This can be used to check the result of mimeType(name).

Returns
the "application/octet-stream" mimetype pointer.

Definition at line 47 of file kmimetype.cpp.

QString KMimeType::extractKnownExtension ( const QString fileName)
static

Determines the extension from a filename (or full path) using the mimetype database.

This allows to extract "tar.bz2" for foo.tar.bz2 but still return "txt" for my.doc.with.dots.txt

Deprecated:
use QMimeDatabase::suffixForFileName

Definition at line 224 of file kmimetype.cpp.

QString KMimeType::favIconForUrl ( const QUrl url)
static

Return the "favicon" (see http://www.favicon.com) for the given url, if available.

Does NOT attempt to download the favicon, it only returns one that is already available.

If unavailable, returns QString().

Parameters
urlthe URL of the favicon
Returns
the name of the favicon, or QString()
Deprecated:
use KIO::favIconForUrl

Definition at line 289 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::findByContent ( const QByteArray data,
int *  accuracy = nullptr 
)
static

Tries to find out the MIME type of a data chunk by looking for certain magic numbers and characteristic strings in it.

Parameters
datathe data to examine
accuracyIf not a null pointer, *accuracy is set to the accuracy of the match (which is in the range 0..100)
Returns
a pointer to the KMimeType. "application/octet-stream" is returned if the type can not be found this way.

Definition at line 230 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::findByContent ( QIODevice device,
int *  accuracy = nullptr 
)
static

Tries to find out the MIME type of a data chunk by looking for certain magic numbers and characteristic strings in it.

Parameters
devicethe IO device providing the data to examine
accuracyIf not a null pointer, *accuracy is set to the accuracy of the match (which is in the range 0..100)
Returns
a pointer to the KMimeType. "application/octet-stream" is returned if the type can not be found this way.
Since
4.4

Definition at line 239 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::findByFileContent ( const QString fileName,
int *  accuracy = nullptr 
)
static

Tries to find out the MIME type of a file by looking for certain magic numbers and characteristic strings in it.

This function is similar to the previous one. Note that the file name is not used for determining the file type, it is just used for loading the file's contents.

Parameters
fileNamethe path to the file
accuracyIf not a null pointer, *accuracy is set to the accuracy of the match (which is in the range 0..100)
Returns
a pointer to the KMimeType, or the default mimetype (application/octet-stream) if the file cannot be opened.

Definition at line 248 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::findByNameAndContent ( const QString name,
const QByteArray data,
mode_t  mode = 0,
int *  accuracy = nullptr 
)
static

Tries to find out the MIME type of filename/url and a data chunk.

Whether to trust the extension or the data depends on the results of both approaches, and is determined automatically.

This method is useful for instance in the get() method of kioslaves, and anywhere else where a filename is associated with some data which is available immediately.

Parameters
namethe filename or url representing this data. Only used for the extension, not used as a local filename.
datathe data to examine when the extension isn't conclusive in itself
modethe mode of the file (used, for example, to identify executables)
accuracyIf not a null pointer, *accuracy is set to the accuracy of the match (which is in the range 0..100)

Definition at line 202 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::findByNameAndContent ( const QString name,
QIODevice device,
mode_t  mode = 0,
int *  accuracy = nullptr 
)
static

Tries to find out the MIME type of filename/url and a data chunk.

Whether to trust the extension or the data depends on the results of both approaches, and is determined automatically.

This method is useful for instance in the get() method of kioslaves, and anywhere else where a filename is associated with some data which is available immediately.

Parameters
namethe filename or url representing this data. Only used for the extension, not used as a local filename.
devicethe IO device providing the data to examine when the extension isn't conclusive in itself
modethe mode of the file (used, for example, to identify executables)
accuracyIf not a null pointer, *accuracy is set to the accuracy of the match (which is in the range 0..100)
Returns
a pointer to the KMimeType. "application/octet-stream" is returned if the type can not be found this way.
Since
4.4

Definition at line 213 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::findByPath ( const QString path,
mode_t  mode = 0,
bool  fast_mode = false,
int *  accuracy = nullptr 
)
static

Finds a KMimeType with the given url.

This function looks at mode_t first. If that does not help it looks at the extension. This is fine for FTP, FILE, TAR and friends, but is not for HTTP ( cgi scripts! ). You should use KRun instead, but this function returns immediately while KRun is async. If no extension matches, then the file contents will be examined if the URL is a local file, or "application/octet-stream" is returned otherwise.

Equivalent to

KUrl u(path);
return findByUrl( u, mode, true, fast_mode );
Parameters
paththe path to the file (a file name is enough, in fast mode)
modethe mode of the file (used, for example, to identify executables)
fast_modeIf set to true no disk access is allowed to find out the mimetype. The result may be suboptimal, but it is fast.
accuracyIf not a null pointer, *accuracy is set to the accuracy of the match (which is in the range 0..100)
Returns
A pointer to the matching mimetype. 0 is never returned.

Definition at line 188 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::findByUrl ( const QUrl url,
mode_t  mode = 0,
bool  is_local_file = false,
bool  fast_mode = false,
int *  accuracy = nullptr 
)
static

Finds a KMimeType with the given url.

This function looks at mode_t first. If that does not help it looks at the extension (and the contents, for local files). This method is fine for many protocols like ftp, file, fish, zip etc., but is not for http (e.g. cgi scripts make extension-based checking unreliable). For HTTP you should use KRun instead (to open the URL, in an app or internally), or a KIO::mimetype() job (to find the mimetype without downloading), or a KIO::get() job (to find the mimetype and then download). In fact KRun is the most complete solution, but deriving from it just for this is a bit cumbersome.

If no extension matches, then the file contents will be examined if the URL is a local file, or "application/octet-stream" is returned otherwise.

Parameters
urlIs the right most URL with a filesystem protocol. It is up to you to find out about that if you have a nested URL. For example "http://localhost/mist.gz#gzip:/decompress" would have to pass the "http://..." URL part, while "file:/tmp/x.tar#tar:/src/test.gz#gzip:/decompress" would have to pass the "tar:/..." part of the URL, since gzip is a filter protocol and not a filesystem protocol.
modethe mode of the file (used, for example, to identify executables)
is_local_filetrue if the file is local; false if not, or if you don't know.
fast_modeIf set to true no disk access is allowed to find out the mimetype. The result may be suboptimal, but it is fast.
accuracyif set, the accuracy of the result, between 0 and 100. For instance, when the extension was used to determine the mimetype, the accuracy is set to 80, as per the shared-mime spec. Some 'magic' rules (used when !fast_mode) have an accuracy > 80 (and have priority over the filename, others are < 80).
Returns
A pointer to the matching mimetype. 0 is never returned. Very Important: Don't store the result in a KMimeType* !

Definition at line 172 of file kmimetype.cpp.

QString KMimeType::iconName ( ) const

Return the filename of the icon associated with the mimetype.

Use KIconLoader::loadMimeTypeIcon to load the icon.

Returns
The name of the icon associated with this MIME type.

Definition at line 336 of file kmimetype.cpp.

QString KMimeType::iconNameForUrl ( const QUrl url,
mode_t  mode = 0 
)
static

Return the filename of the icon associated with the mimetype, for a given url.

Use KIconLoader::loadMimeTypeIcon to load the icon.

Parameters
urlURL for the file
modethe mode of the file. The mode may modify the icon with overlays that show special properties of the icon. Use 0 for default
Returns
the name of the icon. The name of a default icon if there is no icon for the mime type
Deprecated:
use KIO::iconNameForUrl

Definition at line 294 of file kmimetype.cpp.

bool KMimeType::is ( const QString mimeTypeName) const

Do not use name()=="somename" anymore, to check for a given mimetype.

For mimetype inheritance to work, use is("somename") instead. is() also supports mimetype aliases.

Deprecated:
since KF5, use QMimeType::inherits

Definition at line 316 of file kmimetype.cpp.

bool KMimeType::isBinaryData ( const QString fileName)
static

Returns whether a file has an internal format that is not human readable.

This is much more generic than "not mime->is(text/plain)". Many application file formats (like rtf and postscript) are based on text, but text that the user should rarely ever see.

Definition at line 269 of file kmimetype.cpp.

bool KMimeType::isBufferBinaryData ( const QByteArray data)
static

Returns whether a buffer has an internal format that is not human readable.

This is much more generic than "not mime->is(text/plain)". Many application file formats (like rtf and postscript) are based on text, but text that the user should rarely ever see.

Definition at line 82 of file kmimetype.cpp.

bool KMimeType::isDefault ( ) const

Return true if this mimetype is the default mimetype.

Definition at line 52 of file kmimetype.cpp.

QString KMimeType::mainExtension ( ) const

Return the primary extension associated with this mimetype, if any.

If patterns() returns (*.jpg, *.jpeg) then mainExtension will return ".jpg". Note that the dot is included.

If none of the patterns is in *.foo format (for instance *.jp? or *.* or callgrind.out* ) then mainExtension() returns an empty string.

Since
4.3
Deprecated:
use QMimeType::suffixes(), but note that it doesn't return the leading dot.

Definition at line 359 of file kmimetype.cpp.

bool KMimeType::matchFileName ( const QString filename,
const QString pattern 
)
static

Returns true if the given filename matches the given pattern.

Since
4.6.1
Deprecated:
use QRegExp:
QRegExp rx(pattern);
rx.setPatternSyntax(QRegExp::Wildcard);
return rx.exactMatch(filename);

Definition at line 394 of file kmimetype.cpp.

KMimeType::Ptr KMimeType::mimeType ( const QString name,
FindByNameOption  options = ResolveAliases 
)
static

Retrieve a pointer to the mime type name.

Very important: Don't store the result in a KMimeType* !

Also note that you get a new KMimeType pointer every time you call this. Don't ever write code that compares mimetype pointers, compare names instead.

Parameters
namethe name of the mime type
optionscontrols how the mime type is searched for
Returns
the pointer to the KMimeType with the given name, or 0 if not found
See also
KServiceType::serviceType

Definition at line 57 of file kmimetype.cpp.

QString KMimeType::name ( ) const
Returns
the name of this entry

Definition at line 406 of file kmimetype.cpp.

QString KMimeType::parentMimeType ( ) const

If this mimetype is a subclass of another mimetype, return the name of the parent.

Returns
the parent mime type, or QString() if not set
Deprecated:
this method does not support multiple inheritance, which is actually part of the shared-mime-info standard. Use is(), parentMimeTypes(), or allParentMimeTypes() instead of parentMimeType()

Definition at line 306 of file kmimetype.cpp.

QStringList KMimeType::parentMimeTypes ( ) const

If this mimetype is a subclass of one or more other mimetypes, return the list of those mimetypes.

For instance a application/javascript is a special kind of text/plain, so the definition of application/javascript says sub-class-of type="text/plain"

Another example: application/x-shellscript is a subclass of two other mimetypes, application/x-executable and text/plain.

(Note that this notion doesn't map to the servicetype inheritance mechanism, since an application that handles the specific type doesn't necessarily handle the base type. The opposite is true though.)

Returns
the list of parent mimetypes
Since
4.1

Definition at line 321 of file kmimetype.cpp.

QStringList KMimeType::patterns ( ) const

Retrieve the list of patterns associated with the MIME Type.

Returns
a list of file globs that describe the file names (or, usually, the extensions) of files with this mime type

Definition at line 341 of file kmimetype.cpp.

int KMimeType::sharedMimeInfoVersion ( )
static

Returns the version of the installed update-mime-database program (from freedesktop.org shared-mime-info).

This is used by unit tests and by the code that writes out icon definitions.

Since
4.3
Returns
-1 on error, otherwise a version number to use like this:
if (version >= KDE_MAKE_VERSION(0, 40, 0)) { ... }

Definition at line 354 of file kmimetype.cpp.

QString KMimeType::userSpecifiedIconName ( ) const

Returns the user-specified icon for this mimetype.

This is empty most of the time, you probably want to use iconName() instead. This method is for the mimetype editor.

Since
4.3

Definition at line 347 of file kmimetype.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:59:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.