Documentation | StorageKit | MimeType
SYNOPSIS
Perl
use HaikuR1::MimeType; my $type = HaikuR1::MimeType->new($mime_string); $type->Install();
Python
from HaikuR1.StorageKit import MimeType type = MimeType(mime_string) type.Install()
DESCRIPTION
Exposes the BMimeType
object.
For more information on MimeType, see the Be Book class description, the Be Book overview, and the Haiku Book class description.
For more information on mime types in general, see Media types on Wikipedia.
METHODS
Constructor
Creates a MimeType.
Perl
HaikuR1::MimeType->new($mimeType); HaikuR1::MimeType->new();
Python
MimeType(mimeType) MimeType()
mimeType
A mime type string; can be a full type or just a supertype.
App hint
GetAppHint
SetAppHint
Gets or sets the application hint for the mime type. This is a path on the file system, not an application signature, so this may break if the file is moved.
Perl
$mimetype->GetAppHint(); $mimetype->SetAppHint($ref);
Python
mimetype.GetAppHint() mimetype.SetAppHint(ref)
ref
A string, the path to the desired application.
Attribute info
GetAttrInfo
SetAttrInfo
Gets or sets a native list of native maps containing the attributes that files of this type will normally have. The maps will have the following fields:
name
(string) - the internal name (the name you use to get or set the attribute)public_name
(string) - the human-readable name (the name that appears in Trackertype
(int32) - a type constantpublic
(boolean)editable
(boolean)
Note: The C++ version of this method passes the information in a Message.
Perl
$mimetype->GetAttrInfo(); $mimetype->SetAttrInfo($info);
Python
mimetype.GetAttrInfo() mimetype.SetAttrInfo(info)
info
A native list of native maps.
Descriptions
GetShortDescription
SetShortDescription
GetLongDescription
SetLongDescription
Gets or sets a description for the mime type.
The suggested size difference between the names is not enforced; both values have the same length limit - B_MIME_TYPE_LENGTH (which is a limit on the number of bytes, not on the number of characters).
Perl
$mimetype->GetShortDescription(); $mimetype->SetShortDescription($description); $mimetype->GetLongDescription(); $mimetype->SetLongDescription($description);
Python
mimetype.GetShortDescription() mimetype.SetShortDescription(description) mimetype.GetLongDescription() mimetype.SetLongDescription(description)
description
A string.
File extensions
GetFileExtensions
SetFileExtensions
Gets or sets a list of file extensions that this type would usually have, as a native list of strings.
Note: The C++ version of this method passes the information in a Message.
Perl
$mimetype->GetFileExtensions(); $mimetype->SetFileExtensions($extensions);
Python
mimetype.GetFileExtensions() mimetype.SetFileExtensions(extensions)
extensions
A native list of strings.
GetSupportingApps
Returns a list of applications that handle this file type, as a native map with three fields:
applications
- a native list of strings, the signatures of supporting applicationssub
- the number of applications that handle the full file type; will be0
if the MimeType represents a supertypesuper
- the number of applications that support the supertype.
The sub
applications come before the super
applications in the list.
Note: The C++ version of this method passes the information in a Message.
Perl
$mimetype->GetSupportingApps();
Python
mimetype.GetSupportingApps()
Icons
GetIcon
SetIcon
GetIconForType
SetIconForType
Gets the mime type's icon. If you pass a size, then it returns a Bitmap; otherwise, it the raw icon data.
The Type
versions are for applications, and work on the icon that the
application will use for the given type. (The MimeType object is the
application, and type
is the desired type.)
Perl
$mimetype->GetIcon($size, $colorSpace); $mimetype->GetIcon(); $mimetype->SetIcon($icon); $mimetype->SetIcon($data); $mimetype->GetIconForType($type, $size, $colorSpace); $mimetype->GetIconForType(); $mimetype->SetIconForType($type, $icon); $mimetype->SetIconForType($type, $data);
Python
mimetype.GetIcon(size, colorSpace) mimetype.GetIcon() mimetype.SetIcon(icon) mimetype.SetIcon(data) mimetype.GetIconForType(type, size) mimetype.GetIconForType(type, size, colorSpace) mimetype.SetIconForType(type, icon) mimetype.SetIconForType(type, data)
size
An integer icon size constant. The version with this parameter returns a Bitmap object; the other version returns a byte string.
colorSpace
An integer color space constant, the color space of the returned Bitmap; defaults to
B_RGBA32
, but also acceptsB_RGBA32
andB_CMAP8
.icon
A Bitmap, the new icon; should be a
32x32
or16x16
image.data
A byte string containing icon data.
type
A mime type string.
Install
Delete
IsInstalled
These methods operate on the system mime type database. (You can examine the
contents of this databse via the FiletTypes
preflet.)
Perl
$mimetype->Install(); $mimetype->Delete(); $mimetype->IsInstalled();
Python
mimetype.Install() mimetype.Delete() mimetype.IsInstalled()
Preferred App
GetPreferredApp
SetPreferredApp
Gets or sets the preferred application for the mime type.
Perl
$nodeinfo->GetPreferredApp($verb); $nodeinfo->SetPreferredApp($signature, $verb);
Python
nodeinfo.GetPreferredApp(verb) nodeinfo.SetPreferredApp(signature, verb)
verb
An app verb constant; the default is B_OPEN (which is also the only verb currently defined).
signature
An application signature.
SetTo
Unset
Updates the MimeType object to refer to a different mime type, or to no mime type.
Perl
$mimetype->SetTo($mimeType); $mimetype->Unset();
Python
mimetype.SetTo(mimeType) mimetype.Unset()
mimeType
A mime type string; can be a full type or just a supertype.
Sniffer rules
GetSnifferRule
SetSnifferRule
Sniffer rules are used by the system to guess the mime type a file or byte string.
Perl
$mimetype->GetSnifferRule(); $mimetype->SetSnifferRule($rule);
Python
mimetype.GetSnifferRule() mimetype.SetSnifferRule(rule)
rule
A rule string.
Supertype
IsSupertypeOnly
GetSupertype
Contains
Methods for working with supertypes.
Note: Contains
will return true not only when the current MimeType contains
the given MimeType, but also when both MimeTypes reprsent the same mime type.
Perl
$mimetype->IsSupertypeOnly(); $mimetype->GetSupertype(); $mimetype->Contains($type);
Python
mimetype.IsSupertypeOnly() mimetype.GetSupertype() mimetype.Contains(type)
type
A MimeType object
Type
Returns the mime string for the type represented by the MimeType object.
Perl
$mimetype->Type();
Python
mimetype.Type()
CLASS METHODS
CheckSnifferRule
Determines whether a sniffer rule is valid.
- The rules, and the elements of an individual rule, should be separated by whitespace. Recognized whitespace characters are space, tab, and linefeed.
- Each rule begins with a weight, a floating point number from
0
to1
. (The rule grammar does not specify this limit, but the rule parsing enforces it.) - The rest of the rule is one or more lists of one or more patterns, each list
is enclosed in a single pair of parentheses (curved brackets for those of
you who do not speak American English), and patterns in the list are
separated by pipes (
|
); each list may be preceded by a range. - Each pattern may be preceded by flags or a range, or both. Currently, there
is only one flag:
-i
for case-insensitive matching. Although the grammar does not specify this, whenever any pattern in a list contains this flag, the flag is applied to every pattern in the list. - A range looks like
[ pos ]
or[ start : end ]
(whitespace optional), indicating a position or range within the data where the pattern should start. Omitting the range is equivalent to using the range[0]
. - Note that the pattern should start somehwere in the given range; it does not need to fit in the range.
- A pattern string may be designated by a hex string, a quoted string, or an unquoted string; each string represents a sequence of byes to be found at the desired position or range.
- A hex string is
0x
followed by an even number of hex digits. - A quoted string may use either single quotes or double quotes; it may not
contain its own quote or a backslash, unless escaped by a backslash (that
is,
\\
,\'
, and\"
are accepted). - An unquoted string may not contain whitespace or punctuation unless escaped by a backslash, or escaped as octal or hexadecimal sequences.
- An octal escape is a backslash followed by one to three octal digits.
- A hexdecimal escape is
\x
followed by two hexadecimal digits. (Each character must be individually escaped; hex strings are not allowed here.) - The following are considered punctuation:
( ) [ ] | & :
. - Each pattern string may optional be followed by a bitmask, separated from
the string by an ampersand
&
. In theory this mask follows the same grammar as the string itself, but all the examples I have seen are hex strings consisting of a mixture offf
and00
, to indicate optional bytes in the pattern string. Although not specified in the grammar, this mask must be the same length (in bytes) as the pattern string.
Here are some explanatory examples from
haiku/headers/private/storage/sniffer/Parser.h
(in the git repo) or
...
(if you have the development headers on your installation of Haiku). (In the same file you will
also find the grammar from which I pulled the rules above.)
1.0 ('ABCD')
The file must start with the string "ABCD". The priority of the rule is 1.0 (maximal).
0.8 [0:3] ('ABCD' | 'abcd')
The file must contain the string "ABCD" or "abcd" starting somewhere in the first four bytes. The rule priority is 0.8.
0.5 ([0:3] 'ABCD' | [0:3] 'abcd' | [13] 'EFGH')
The file must contain the string "ABCD" or "abcd" starting somewhere in the first four bytes or the string "EFGH" at position 13. The rule priority is 0.5.
0.8 [0:3] ('ABCD' & 0xff00ffff | 'abcd' & 0xffff00ff)
The file must contain the string "A.CD" or "ab.d" (where "." is an arbitrary character) starting somewhere in the first four bytes. The rule priority is 0.8.
0.3 [10] ('mnop') ('abc') [20] ('xyz')
The file must contain the string 'abc' at the beginning of the file, the string 'mnop' starting at position 10, and the string 'xyz' starting at position 20. The rule priority is 0.3.
200e-3 (-i 'ab')
The file must contain the string 'ab', 'aB', 'Ab', or 'AB' at the beginning of the file. The rule priority is 0.2.
Here are some real examples, from sniffers included with Haiku:
text/x-source-code
0.20 ([0]"//" | [0]"/\*" | [0:32]"#include" | [0:32]"#ifndef" | [0:32]"#ifdef")
image/x-photoshop
0.70 ("8BPS \000\000\000\000" & 0xffffffff0000ffffffff )
text/html
0.40 [0:64]( -i "<HTML" | "<HEAD" | "<TITLE" | "<BODY" | "<TABLE" | "<!--" | "<META" | "<CENTER")
Perl
HaikuR1::MimeType->CheckSnifferRule($rule);
Python
MimeType.CheckSnifferRule(rule)
rule
A rule string.
GetWildcardApps
Acts like GetSupportingApps, but returns a list of
signatures for applications that support any file type. (The super
value
will always be 0
.)
Perl
HaikuR1::MimeType->GetWildcardApps();
Python
MimeType.GetWildcardApps()
GuessMimeType
Guesses the mime type for a file or a byte string.
Perl
HaikuR1::MimeType->GuessMimeType(path => $path); HaikuR1::MimeType->GuessMimeType(data => $data);
Python
MimeType.GuessMimeType(path = path) MimeType.GuessMimeType(data = data)
path
A string, the path to a file to check
data
A string, the data to check
Installed types
GetInstalledTypes
GetInstalledSupertypes
Gets or sets a list of installed types, as a native list of strings.
Note: The C++ version of this method passes the information in a Message.
Perl
HaikuR1::MimeType->GetInstalledTypeForSupertype($supertype); HaikuR1::MimeType->GetInstalledSupertypes();
Python
MimeType.GetInstalledTypeForSupertype(supertype) MimeType.GetInstalledSupertypes()
supertype
A string; if omitted, will get all installed types.
IsValid
Returns true if the represented type is valid, or false if it is not.
The algorithm to determne validity is simple:
- The type may consist of either a supertype only, or a supertype and a
subtype, separated by a slash (or solidus, or whatever you prefer to call
the character
/
). - The supertype and subtype (if present) may not be empty.
- Neither to supertype nor the subtype may contain the following characters:
/ < > @ , ; : " ( ) [ ] ? = \
orDEL
(the control character at code point 127).
Perl
HaikuR1::MimeType->IsMimeTypeValid($mimeType); $mimetype->IsValid();
Python
MimeType.IsMimeTypeValid(mimeType)
mimeType
A string or a MimeType
Perl
The Perl version functions as both a class method and an instance method, so you call the method on an object instead of passing it as the first parameter.
MIME Monitor
StartWatching
StopWatching
These methods track changes to the MIME database.
Notifications will come in the form of a Message with a what
value
of B_META_MIME_CHANGED
and a field called opcode
, with the following
fields:
be:which
(int32) - one or more of the MIME change constantsbe:type
(string) - the MIME type that has changedbe:extra_type
(string) - sent with some notifications; currently, it is only sent for IconForType operations;type
is the application type that made the change;extra_type
is the file type for which the icon changedbe:large_icon
(bool) - true is the large icon has changed; false otherwise (only present when an icon has changed)be:action
(int32) - one of the MIME action constants
Perl
HaikuR1::MimeType->StartWatching($target); HaikuR1::MimeType->StopWatching($target);
Python
MimeType.StartWatching(target) MimeType.StopWatching(target)
OPERATORS
==
Returns true if the MimeType objects refer to the same mime type. Can also be used to compare a MimeType object to a mime type string.
CONSTANTS
App verbs
Perl
use HaikuR1::MimeType qw(:app_verb)
Python
Python does not support export tags.
- B_OPEN
MIME actions
Perl
use HaikuR1::MimeType qw(:action)
Python
Python does not support export tags.
- B_META_MIME_MODIFIED
- B_META_MIME_DELETED
MIME changes
The first item in the list is the what
value in a MIME change notification
Message. The remaining items are the change indicators.
Perl
use HaikuR1::MimeType qw(:change)
Python
Python does not support export tags.
- B_META_MIME_CHANGED
- B_ICON_CHANGED
- B_PREFERRED_APP_CHANGED
- B_ATTR_INFO_CHANGED
- B_FILE_EXTENSIONS_CHANGED
- B_SHORT_DESCRIPTION_CHANGED
- B_LONG_DESCRIPTION_CHANGED
- B_ICON_FOR_TYPE_CHANGED
- B_APP_HINT_CHANGED
- B_MIME_TYPE_CREATED
- B_MIME_TYPE_DELETED
- B_SNIFFER_RULE_CHANGED
- B_SUPPORTED_TYPES_CHANGED
- B_EVERYTHING_CHANGED
MIME types
Perl
use HaikuR1::MimeType qw(:types)
Python
Python does not support export tags.
- B_APP_MIME_TYPE
- B_PEF_APP_MIME_TYPE
- B_PE_APP_MIME_TYPE
- B_ELF_APP_MIME_TYPE
- B_RESOURCE_MIME_TYPE
- B_FILE_MIME_TYPE