KDE 4.1 PyKDE API Reference
  • KDE's Python API
  • Overview
  • PyKDE Home
  • Sitemap
  • Contact Us
 

KMimeType Class Reference

from PyKDE4.kdecore import *

Inherits: KServiceType → KSycocaEntry

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


Enumerations

FindByNameOption { DontResolveAlias, ResolveAliases }

Methods

 __init__ (self, QDataStream str, int offset)
 __init__ (self, QString fullpath, QString name, QString comment)
 __init__ (self, KMimeTypePrivate dd)
 __init__ (self, KMimeTypePrivate dd, QString name, QString comment)
QStringList allParentMimeTypes (self)
QString comment (self, KUrl url=KUrl())
QString iconName (self, KUrl url=KUrl())
 internalClearData (self)
bool is_ (self, QString mimeTypeName)
bool isDefault (self)
QString parentMimeType (self)
QStringList parentMimeTypes (self)
QStringList patterns (self)
 setParentMimeType (self, QString parent)
 setPatterns (self, QStringList patterns)

Static Methods

KMimeType.List allMimeTypes ()
QString defaultMimeType ()
KMimeType.Ptr defaultMimeTypePtr ()
QString extractKnownExtension (QString fileName)
QString favIconForUrl (KUrl url)
KMimeType.Ptr, int (accuracy) findByContent (QByteArray data, )
KMimeType.Ptr, int (accuracy) findByFileContent (QString fileName, )
KMimeType.Ptr, int (accuracy) findByNameAndContent (QString name, QByteArray data, mode_t mode=0, )
KMimeType.Ptr, int (accuracy) findByPath (QString path, mode_t mode=0, bool fast_mode=0, )
KMimeType.Ptr, int (accuracy) findByUrl (KUrl url, mode_t mode=0, bool is_local_file=0, bool fast_mode=0, )
QString iconNameForUrl (KUrl url, mode_t mode=0)
bool isBinaryData (QString fileName)
bool isBufferBinaryData (QByteArray data)
KMimeType.Ptr mimeType (QString name, KMimeType.FindByNameOption options=KMimeType.DontResolveAlias)

Method Documentation

__init__ (  self,
QDataStream  str,
int  offset
)

Internal:
Construct a service from a stream.

The stream must already be positionned at the correct offset

__init__ (  self,
QString  fullpath,
QString  name,
QString  comment
)

Construct a mimetype and take all information from an XML file.

Parameters:
fullpath  the path to the xml that describes the mime type
name  the name of the mimetype (usually the end of the path)
comment  the comment associated with the mimetype

__init__ (  self,
KMimeTypePrivate  dd
)

Internal:
Construct a service from a stream.

The stream must already be positioned at the correct offset

__init__ (  self,
KMimeTypePrivate  dd,
QString  name,
QString  comment
)

Construct a mimetype and take all information from an XML file.

Parameters:
fullpath  the path to the xml that describes the mime type
name  the name of the mimetype (usually the end of the path)
comment  the comment associated with the mimetype

KMimeType.List allMimeTypes (   )

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

QStringList allParentMimeTypes (   self )

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

QString comment (  self,
KUrl  url=KUrl()
)

Returns the descriptive comment associated with the MIME type. The url argument is unused, but provided so that KMimeType derived classes can use it.

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

QString defaultMimeType (   )

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"

KMimeType.Ptr defaultMimeTypePtr (   )

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.

QString extractKnownExtension ( QString  fileName
)

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

QString favIconForUrl ( KUrl  url
)

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:
url  the URL of the favicon

Returns:
the name of the favicon, or QString()

KMimeType.Ptr, int (accuracy) findByContent ( QByteArray  data,
)

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. 0 if the type can not be found this way.

KMimeType.Ptr, int (accuracy) findByFileContent ( QString  fileName,
)

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, or the default mimetype (application/octet-stream) if the file cannot be opened.

KMimeType.Ptr, int (accuracy) findByNameAndContent ( QString  name,
QByteArray  data,
mode_t  mode=0,
)

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:
name  the filename or url representing this data. Only used for the extension, not used as a local filename.
data  the data to examine when the extension isn't conclusive in itself
mode  the mode of the file (used, for example, to identify executables)
accuracy  If not a null pointer, *accuracy is set to the accuracy of the match (which is in the range 0..100)

KMimeType.Ptr, int (accuracy) findByPath ( QString  path,
mode_t  mode=0,
bool  fast_mode=0,
)

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;
 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.
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 matching mimetype. 0 is never returned.

KMimeType.Ptr, int (accuracy) findByUrl ( KUrl  url,
mode_t  mode=0,
bool  is_local_file=0,
bool  fast_mode=0,
)

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.

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; false if not, or if you don't know.
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.
accuracy  if 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* !

QString iconName (  self,
KUrl  url=KUrl()
)

Return the filename of the icon associated with the mimetype. Use KIconLoader.loadMimeTypeIcon to load the icon.

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

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

QString iconNameForUrl ( KUrl  url,
mode_t  mode=0
)

Return the filename of the icon associated with the mimetype, for a given url. Use KIconLoader.loadMimeTypeIcon to load the icon.

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

internalClearData (   self )

Internal:
for kbuildsycoca. Don't ever use this, or else...

bool is_ (  self,
QString  mimeTypeName
)

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! is() also supports mimetype aliases.

bool isBinaryData ( QString  fileName
)

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.

bool isBufferBinaryData ( QByteArray  data
)

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.

bool isDefault (   self )

Return true if this mimetype is the default mimetype

KMimeType.Ptr mimeType ( QString  name,
KMimeType.FindByNameOption  options=KMimeType.DontResolveAlias
)

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:
name  the name of the mime type

Returns:
the pointer to the KMimeType with the given name, or 0 if not found
See also:
KServiceType.serviceType

QString parentMimeType (   self )

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()

QStringList parentMimeTypes (   self )

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

QStringList patterns (   self )

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

setParentMimeType (  self,
QString  parent
)

Internal:
for kbuildsycoca. Don't ever use this, or else...

setPatterns (  self,
QStringList  patterns
)

Internal:
for kbuildsycoca. Don't ever use this, or else...


Enumeration Documentation

FindByNameOption
Enumerator:
DontResolveAlias 
ResolveAliases = 1

  • Full Index

Modules

  • akonadi
  • dnssd
  • kdecore
  • kdeui
  • khtml
  • kio
  • knewstuff
  • kparts
  • kutils
  • nepomuk
  • phonon
  • solid
  • soprano
This documentation is maintained by Simon Edwards.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal