# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# KMimeType Class Reference

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

`#include <kmimetype.h>`

Inheritance diagram for KMimeType:

[legend]
Collaboration diagram for KMimeType:

[legend]
List of all members.

## Public Types

typedef KSharedPtr< KMimeType > | Ptr
typedef QValueList< Ptr > | List

## Public Member Functions

| KMimeType (const QString &_fullpath, const QString &_type, const QString &_icon, const QString &_comment, const QStringList &_patterns)
| KMimeType (const QString &_fullpath)
| KMimeType (KDesktopFile *config)
| KMimeType (QDataStream &_str, int offset)
virtual QString | icon (const QString &, bool) const
virtual QString | icon (const KURL &, bool) const
virtual QPixmap | pixmap (KIcon::Group group, int force_size=0, int state=0, QString *path=0L) const
virtual QPixmap | pixmap (const KURL &_url, KIcon::Group _group, int _force_size=0, int _state=0, QString *_path=0L) const
QString | comment () const
virtual QString | comment (const QString &, bool) const
virtual QString | comment (const KURL &, bool) const
const QStringList & | patterns () const
virtual void | load (QDataStream &qs)
virtual void | save (QDataStream &qs)
virtual QVariant | property (const QString &_name) const
virtual QStringList | propertyNames () const
QString | parentMimeType () const
bool | is (const QString &mimeTypeName) const

## Static Public Member Functions

static QPixmap | pixmapForURL (const KURL &_url, mode_t _mode=0, KIcon::Group _group=KIcon::Desktop, int _force_size=0, int _state=0, QString *_path=0L)
static QString | iconForURL (const KURL &_url, mode_t _mode=0)
static QString | favIconForURL (const KURL &url)
static Ptr | mimeType (const QString &_name)
static Ptr | findByURL (const KURL &_url, mode_t _mode=0, bool _is_local_file=false, bool _fast_mode=false)
static Ptr | findByURL (const KURL &_url, mode_t _mode, bool _is_local_file, bool _fast_mode, bool *accurate)
static Ptr | findByPath (const QString &path, mode_t mode=0, bool fast_mode=false)
static Ptr | findByContent (const QByteArray &data, int *accuracy=0)
static Ptr | findByFileContent (const QString &fileName, int *accuracy=0)
static Format | findFormatByFileContent (const QString &fileName)
static List | allMimeTypes ()
static const QString & | defaultMimeType ()
static KMimeType::Ptr | defaultMimeTypePtr ()
static KMimeType::Ptr | diagnoseFileName (const QString &file, QString &pattern)

## Protected Member Functions

void | loadInternal (QDataStream &)
void | init (KDesktopFile *)
int | patternsAccuracy () const
virtual void | virtual_hook (int id, void *data)

## Static Protected Member Functions

static void | errorMissingMimeType (const QString &_type)
static void | buildDefaultType ()
static void | checkEssentialMimeTypes ()

## Protected Attributes

QStringList | m_lstPatterns

## Static Protected Attributes

static bool | s_bChecked = false
static Ptr | s_pDefaultType = 0L

## Friends

class | KServiceTypeFactory

## 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

Definition at line 47 of file kmimetype.h.

## Constructor & Destructor Documentation

KMimeType::KMimeType | ( | const QString & | _fullpath,
|  | const QString & | _type,
|  | const QString & | _icon,
|  | const QString & | _comment,
|  | const QStringList & | _patterns
| ) |

Constructor.

You may pass in arguments to create a mimetype with specific properties.

Parameters:

  | _fullpath | the path to the configuration file (.desktop)
  | _type | the mime type itself
  | _icon | the name of the icon that represens the mime type
  | _comment | a comment describing the mime type
  | _patterns | a list of file globs that describes the names (or extensions) of the files with this mime type

Definition at line 361 of file kmimetype.cpp.

Referenced by buildDefaultType().

KMimeType::KMimeType | ( | const QString & | _fullpath | ) |

Construct a mimetype and take all information from a config file.

Parameters:

  | _fullpath | the path to the configuration file (.desktop)

Definition at line 368 of file kmimetype.cpp.

References endl(), KServiceType::isValid(), and kdWarning().

KMimeType::KMimeType | ( | KDesktopFile * | config | ) |

Construct a mimetype and take all information from a desktop file.

Parameters:

  | config | the desktop configuration file that describes the mime type

Definition at line 377 of file kmimetype.cpp.

References endl(), KServiceType::isValid(), and kdWarning().

## Member Function Documentation

virtual QString KMimeType::icon | ( | const QString & | ,
|  | bool |
| ) | const`[inline, virtual]`

Return the filename of the icon associated with the mimetype.

The arguments are unused, but provided so that KMimeType-derived classes can use them (e.g. KFolderType uses the URL to return one out of 2 icons)

Returns:
  The path to the icon associated with this MIME type.

Reimplemented in KFolderType, and KDEDesktopMimeType.

Definition at line 100 of file kmimetype.h.

virtual QString KMimeType::icon | ( | const KURL & | ,
|  | bool |
| ) | const`[inline, virtual]`

Return the filename of the icon associated with the mimetype.

The arguments are unused, but provided so that KMimeType-derived classes can use them (e.g. KFolderType uses the URL to return one out of 2 icons)

Returns:
  The path to the icon associated with this MIME type.

Reimplemented in KFolderType, and KDEDesktopMimeType.

Definition at line 110 of file kmimetype.h.

QPixmap KMimeType::pixmap | ( | KIcon::Group | group,
|  | int | force_size = `0`,
|  | int | state = `0`,
|  | QString * | path = `0L`
| ) | const`[virtual]`

Use this function only if you don't have a special URL for which you search a pixmap.

This function is useful to find out, which icon is usually chosen for a certain mime type. Since no URL is passed, it is impossible to obey icon hints in desktop entries for example.

Parameters:

  | group | The icon group where the icon is going to be used.
  | force_size | Override globallly configured icon size. Use 0 for the default size
  | state | The icon state, one of: `KIcon::DefaultState`, `KIcon::ActiveState` or `KIcon::DisabledState`.
  | path | Output parameter to get the full path. Seldom needed. Ignored if 0

Returns:
  the pixmap of the mime type, can be a default icon if not found

Reimplemented in KDEDesktopMimeType.

Definition at line 454 of file kmimetype.cpp.

References KIconLoader::addExtraDesktopThemes(), KIconLoader::extraDesktopThemesAdded(), KServiceType::icon(), KGlobal::iconLoader(), QPixmap::isNull(), KIconLoader::loadIcon(), and pixmap().

Referenced by KDEDesktopMimeType::pixmap(), pixmap(), and pixmapForURL().

QPixmap KMimeType::pixmap | ( | const KURL & | _url,
|  | KIcon::Group | _group,
|  | int | _force_size = `0`,
|  | int | _state = `0`,
|  | QString * | _path = `0L`
| ) | const`[virtual]`

Find the pixmap for a given file of this mimetype.

Convenience method that uses icon(), but also locates and load the pixmap.

Parameters:

  | _url | URL for the file.
  | _group | The icon group where the icon is going to be used.
  | _force_size | Override globallly configured icon size. Use 0 for the default size
  | _state | The icon state, one of: KIcon::DefaultState, KIcon::ActiveState or KIcon::DisabledState.
  | _path | Output parameter to get the full path. Seldom needed. Ignored if 0

Returns:
  the pixmap of the URL, can be a default icon if not found

Reimplemented in KDEDesktopMimeType.

Definition at line 470 of file kmimetype.cpp.

References KIconLoader::addExtraDesktopThemes(), KIconLoader::extraDesktopThemesAdded(), KServiceType::icon(), KGlobal::iconLoader(), QPixmap::isNull(), KIconLoader::loadIcon(), and pixmap().

QPixmap KMimeType::pixmapForURL | ( | const KURL & | _url,
|  | mode_t | _mode = `0`,
|  | KIcon::Group | _group = `KIcon::Desktop`,
|  | int | _force_size = `0`,
|  | int | _state = `0`,
|  | QString * | _path = `0L`
| ) | `[static]`

Convenience method to find the pixmap for a URL.

Call this one when you don't know the mimetype.

Parameters:

  | _url | URL for the file.
  | _mode | the mode of the file. The mode may modify the icon with overlays that show special properties of the icon. Use 0 for default
  | _group | The icon group where the icon is going to be used.
  | _force_size | Override globally configured icon size. Use 0 for the default size
  | _state | The icon state, one of: KIcon::DefaultState, KIcon::ActiveState or KIcon::DisabledState.
  | _path | Output parameter to get the full path. Seldom needed. Ignored if 0

Returns:
  the pixmap of the URL, can be a default icon if not found

Definition at line 486 of file kmimetype.cpp.

References KIconLoader::addExtraDesktopThemes(), KIconLoader::extraDesktopThemesAdded(), iconForURL(), KGlobal::iconLoader(), QPixmap::isNull(), KIconLoader::loadIcon(), and pixmap().

Referenced by KURLComboBox::getPixmap(), KURLPixmapProvider::pixmapFor(), and KURLBarItem::setIcon().

QString KMimeType::iconForURL | ( | const KURL & | _url,
|  | mode_t | _mode = `0`
| ) | `[static]`

The same functionality as pixmapForURL(), but this method returns the name of the icon to load.

You'll have to use KIconLoader to load the pixmap for it. The advantage of this method is that you can store the result, and then use it later on for any kind of size.

Parameters:

  | _url | URL for the file
  | _mode | the 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

Definition at line 504 of file kmimetype.cpp.

References defaultMimeTypePtr(), favIconForURL(), findByURL(), KProtocolInfo::icon(), QString::isEmpty(), and KGlobal::staticQString().

Referenced by KBookmarkGroup::addBookmark(), KURIFilterData::iconName(), KFilePropsPlugin::KFilePropsPlugin(), KURLBarItemDialog::KURLBarItemDialog(), and pixmapForURL().

QString KMimeType::favIconForURL | ( | const KURL & | 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::null.

Parameters:

  | url | the URL of the favicon

Returns:
  the name of the favicon, or QString::null

Definition at line 529 of file kmimetype.cpp.

References DCOPRef::call(), KGlobal::config(), DCOPReply::isValid(), and KConfigBase::readBoolEntry().

Referenced by iconForURL().

QString KMimeType::comment | ( |  | ) | const`[inline]`

Returns the descriptive comment associated with the MIME type.

Returns:
  the descriptive comment associated with the MIME type

Reimplemented from KServiceType.

Definition at line 202 of file kmimetype.h.

Referenced by KDEDesktopMimeType::comment(), and KFolderType::comment().

virtual QString KMimeType::comment | ( | const QString & | ,
|  | bool |
| ) | const`[inline, virtual]`

Returns the descriptive comment associated with the MIME type.

The arguments are unused, but provided so that KMimeType derived classes can use them.

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

Reimplemented in KFolderType, and KDEDesktopMimeType.

Definition at line 211 of file kmimetype.h.

virtual QString KMimeType::comment | ( | const KURL & | ,
|  | bool |
| ) | const`[inline, virtual]`

Returns the descriptive comment associated with the MIME type.

The arguments are unused, but provided so that KMimeType derived classes can use them.

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

Reimplemented in KFolderType, and KDEDesktopMimeType.

Definition at line 220 of file kmimetype.h.

const QStringList& KMimeType::patterns | ( |  | ) | const`[inline]`

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 227 of file kmimetype.h.

void KMimeType::load | ( | QDataStream & | qs | ) | `[virtual]`

Load the mimetype from a stream.

Parameters:

  | qs | the stream to load from

Reimplemented from KServiceType.

Definition at line 414 of file kmimetype.cpp.

References KSycocaEntry::load().

void KMimeType::save | ( | QDataStream & | qs | ) | `[virtual]`

Save the mimetype to a stream.

Parameters:

  | qs | the stream to save to

Reimplemented from KServiceType.

Definition at line 426 of file kmimetype.cpp.

References KSycocaEntry::save().

QVariant KMimeType::property | ( | const QString & | _name | ) | const`[virtual]`

Returns the property with the given `_name`.

Parameters:

  | _name | the name of the property

Returns:
  the value of the property

See also:
  propertyNames()

Reimplemented from KServiceType.

Definition at line 434 of file kmimetype.cpp.

References KServiceType::property().

Referenced by parentMimeType().

QStringList KMimeType::propertyNames | ( |  | ) | const`[virtual]`

Retrieves a list of all properties associated with this KMimeType.

Returns:
  a list of all property names

See also:
  property()

Reimplemented from KServiceType.

Definition at line 442 of file kmimetype.cpp.

References KServiceType::propertyNames().

KMimeType::Ptr KMimeType::mimeType | ( | const QString & | _name | ) | `[static]`

Retrieve a pointer to the mime type `_name` or a pointer to the default mime type "application/octet-stream".

0L is never returned.

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

Parameters:

  | _name | the name of the mime type

Returns:
  the pointer to the KMimeType with the given `_name`, or a pointer to the application/octet-stream KMimeType if not found

See also:
  KServiceType::serviceType

Definition at line 141 of file kmimetype.cpp.

References buildDefaultType(), and KSycocaEntry::isType().

Referenced by KFileTreeView::addBranch(), checkEssentialMimeTypes(), KFileDialog::currentFilterMimeType(), KDirLister::doMimeFilter(), findByContent(), findByFileContent(), findByURL(), KService::hasServiceType(), KService::initialPreferenceForMimeType(), is(), KRun::isExecutableFile(), KDesktopPropsPlugin::KDesktopPropsPlugin(), KFileItem::KFileItem(), KMimeTypeChooser::patterns(), KFileItem::pixmap(), KFileItem::readUDSEntry(), and KFileDialog::updateAutoSelectExtension().

KMimeType::Ptr KMimeType::findByURL | ( | const KURL & | _url,
|  | mode_t | _mode = `0`,
|  | bool | _is_local_file = `false`,
|  | bool | _fast_mode = `false`
| ) | `[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 will be examined if the URL a local file or "application/octet-stream" is returned otherwise.

Parameters:

  | _url | Is 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.
  | _mode | the mode of the file (used, for example, to identify executables)
  | _is_local_file | true if the file is local
  | _fast_mode | If set to true no disk access is allowed to find out the mimetype. The result may be suboptimal, but it is fast.

Returns:
  A pointer to the matching mimetype. 0L is never returned. Very Important: Don't store the result in a KMimeType* !

Definition at line 165 of file kmimetype.cpp.

References checkEssentialMimeTypes(), defaultMimeType(), KProtocolInfo::defaultMimetype(), defaultMimeTypePtr(), KProtocolInfo::determineMimetypeFromExtension(), QString::endsWith(), KMimeMagic::findFileType(), QString::isEmpty(), KMimeMagicResult::isValid(), KMimeMagicResult::mimeType(), mimeType(), KMimeMagic::self(), and KGlobal::staticQString().

Referenced by KFileItem::determineMimeType(), findByPath(), iconForURL(), KFileItem::init(), KFilePropsPlugin::KFilePropsPlugin(), KImageIO::mimeType(), KFileItem::pixmap(), KIO::ForwardingSlaveBase::prepareUDSEntry(), KOpenWithDlg::setServiceType(), and KIO::ListJob::slotFinished().

KMimeType::Ptr KMimeType::findByPath | ( | const QString & | path,
|  | mode_t | mode = `0`,
|  | bool | fast_mode = `false`
| ) | `[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 will be examined if the URL a local file or "application/octet-stream" is returned otherwise.

Equivalent to

     KURL u;
     u.setPath(path);
     return findByURL( u, mode, true, fast_mode );

Parameters:

  | path | the path to the file
  | mode | the mode of the file (used, for example, to identify executables)
  | fast_mode | If set to true no disk access is allowed to find out the mimetype. The result may be suboptimal, but it is fast.

Returns:
  A pointer to the matching mimetype. 0L is never returned.

Definition at line 304 of file kmimetype.cpp.

References findByURL(), and KURL::setPath().

Referenced by KFilterBase::findFilterByFileName(), findFormatByFileContent(), and KTar::KTar().

KMimeType::Ptr KMimeType::findByContent | ( | const QByteArray & | data,
|  | int * | accuracy = `0`
| ) | `[static]`

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

Parameters:

  | data | the data to examine
  | accuracy | If 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's KMimeType of the type can not be found this way.

Definition at line 311 of file kmimetype.cpp.

References KMimeMagicResult::accuracy(), defaultMimeType(), KMimeMagic::findBufferType(), KMimeMagicResult::isValid(), mimeType(), KMimeMagicResult::mimeType(), and KMimeMagic::self().

KMimeType::Ptr KMimeType::findByFileContent | ( | const QString & | fileName,
|  | int * | accuracy = `0`
| ) | `[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:

  | fileName | the path to the file
  | accuracy | If 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's KMimeType of the type can not be found this way.

Definition at line 321 of file kmimetype.cpp.

References KMimeMagicResult::accuracy(), defaultMimeType(), KMimeMagic::findFileType(), KMimeMagicResult::isValid(), mimeType(), KMimeMagicResult::mimeType(), and KMimeMagic::self().

Referenced by findFormatByFileContent(), KFilePropsPlugin::KFilePropsPlugin(), and KTar::KTar().

KMimeType::Format KMimeType::findFormatByFileContent | ( | const QString & | fileName | ) | `[static]`

Returns whether a file has an internal format that is human readable, or that would be human readable after decompression.

Since:
  3.2

Definition at line 334 of file kmimetype.cpp.

References findByFileContent(), findByPath(), QVariant::isValid(), QFile::open(), and QVariant::toBool().

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 160 of file kmimetype.cpp.

Referenced by KApplicationPropsPlugin::KApplicationPropsPlugin().

const 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 1123 of file kmimetype.cpp.

References KGlobal::staticQString().

Referenced by buildDefaultType(), findByContent(), findByFileContent(), findByURL(), KFilePropsPlugin::KFilePropsPlugin(), and KMimeTypeChooser::patterns().

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.

Since:
  3.2

Definition at line 89 of file kmimetype.cpp.

References buildDefaultType().

Referenced by findByURL(), iconForURL(), and KDesktopPropsPlugin::KDesktopPropsPlugin().

QString KMimeType::parentMimeType | ( |  | ) | const

If this mimetype inherits from ("is also") another mimetype, return the name of the parent.

For instance a text/x-log is a special kind of text/plain, so the definition of text/x-log can say "X-KDE-IsAlso=text/plain". Or an smb-workgroup is a special kind of inode/directory, etc. This mechanism can also be used to rename mimetypes and preserve compat.

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 parent mime type, or QString::null if not set

Since:
  3.2

Definition at line 554 of file kmimetype.cpp.

References property(), and QVariant::toString().

Referenced by is().

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. Warning, do not use inherits(), that's the servicetype inheritance concept!

Since:
  3.2

Definition at line 560 of file kmimetype.cpp.

References QString::isEmpty(), mimeType(), KServiceType::name(), and parentMimeType().

void KMimeType::errorMissingMimeType | ( | const QString & | _type | ) | `[static, protected]`

Signal a missing mime type.

Parameters:

  | _type | the missinf mime type

Definition at line 134 of file kmimetype.cpp.

Referenced by buildDefaultType(), and checkEssentialMimeTypes().

void KMimeType::buildDefaultType | ( |  | ) | `[static, protected]`

| This function makes sure that the default mime type exists. Definition at line 68 of file kmimetype.cpp. References defaultMimeType(), errorMissingMimeType(), KSycocaEntry::isType(), KMimeType(), and KStandardDirs::resourceDirs(). Referenced by checkEssentialMimeTypes(), defaultMimeTypePtr(), and mimeType().

void KMimeType::checkEssentialMimeTypes | ( |  | ) | `[static, protected]`

| This function makes sure that vital mime types are installed. Definition at line 97 of file kmimetype.cpp. References buildDefaultType(), errorMissingMimeType(), mimeType(), and s_bChecked. Referenced by findByURL().

## Member Data Documentation

bool KMimeType::s_bChecked = false`[static, protected]`

| true if check for vital mime types has been done. Definition at line 66 of file kmimetype.cpp. Referenced by checkEssentialMimeTypes().

The documentation for this class was generated from the following files:

- kmimetype.h
- kmimetype.cpp

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:24:58 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003