Derived from: none
Declared in: be/nustorage/Mime.h
Library: libbe.so
The BMimeType class provides three services:
It can parse a MIME string. It can tell you whether the string is valid, what it's supertype component is, and whether it has a subtype component. (The MIME string format is described in "Valid MIME Strings."
| ||
It gives you access to the File Type database. Given a MIME type, it can look in the database and retrieve that type's icon(s), "preferred handler" application, the filename extensions that correpond to it, and so on.
| ||
It can regard a MIME string as an application signature, and so get and set the executable file, the file types, and the document icons that correspond to that signature. |
All three services operate on MIME strings. In other words, they answer questions such as "Does this string have a supertype?", "Is this string installed in the database?", and so on. You can get the MIME strings from anywhere: from a file's file type attribute, from and application's signature, from the header of an e-mail message, you can even make them up.
A valid MIME string takes the form...
supertype/[subtype] |
...where supertype is one of the seven "media" strings:
...and (the optional) subtype can be just about anything...Except it can't include spaces or any of these forbidden characters:
/ \\ < > @ , ; : " ( ) [ ] ? = |
When you initialize a BMimeType object (through the constructor or SetTo() function), you have to tell it what MIME string you want it to represent:
The string can be supertype-only, or it can be supertype/subtype.
| ||
Currently, the supertype is not restricted to the seven types listed above, but you're probably making a mistake if you make up a new, unrecognized supertype.
| ||
Neither the supertype nor the subtype can include any of the forbidden characters.
| ||
The entire string must be no longer than B_MIME_TYPE_LENGTH characters long. (That's about 240 characters. More than enough.) |
You can check the validity of a MIME string without constructing a BMimeType object by calling the static IsValid() function:
BMimeType::IsValid("text/qwerty");
BMimeType(void) BMimeType(const char *MIME_string)
Constructs a new BMimeType object and initializes its MIME type to a copy of MIME_string (if the argument is given). The rules of validity apply (see "Valid MIME Strings"). To see if the initialization was successful, call InitCheck() after you construct a new BMimeType object.
You can also set the MIME type through the SetTo() function.
virtual ~MimeType(void)
Frees the object's MIME string and destroys the object.
These functions set the object's MIME type, and return information about it.
const char *Type(void) const status_t GetSupertype(BMime *super) const
Type() returns a pointer to the object's MIME string. If the object isn't initialized, this returns a pointer to NULL.
GetSupertype() initializes the argument with this object's supertype. (You can then call GetType() on the argument to see the supertype.) super must be allocated before it's passed in. If this object isn't initialized, super is uninitialized.
RETURN CODES
The errors apply to GetSupertype() only.
status_t InitCheck(void) const
Returns the status of the most recent construction or SetTo() call.
RETURN CODES
static bool IsValid(const char *MIME_string) const bool IsValid(void) const bool IsSupertypeOnly(void) const
The static IsValid() tests its argument for MIME validity. See "Valid MIME Strings" for the rules. The non-static version checks the validity of the object's MIME string.
IsSupertypeOnly() returns true if the object's MIME string doesn't include a subtype.
status_t SetTo(const char *MIME_string)
Initializes this BMimeType object to represent MIME_string. The object's previous MIME string is freed; the argument is then copied.
The argument can be a full supertype/subtype string, or simply a supertype. In any case, it must pass the validity test described in "Valid MIME Strings."
RETURN CODES
These return codes are also returned by the InitCheck() function.
void Unset(void)
Frees the object's current MIME string, and sets the object's status to B_NO_INIT.
These functions query and manipulate the File Type database. Keep in mind that the user also has access through to the database through the FileTypes preferences app.
status_t GetFileExtensions(BMessage *msg) const status_t SetFileExtensions(const BMessage *msg)
The database associates a list of file extensions (".xxx..." filename appendages) with each file type. If a file is otherwise untyped, clients of the database can figure out its type by matching the file's extension to the lists in the database.
These functions get and set the file extensions that are associated with the object's MIME type.
The BMessage's what field is unimportant. |
For example, to retrieve all the extensions that correspond to this object's MIME type, you would do the following:
BMessage msg(); uint32 i=0; char *ptr; if (mime.GetFileExtensions(&msg) != B_NO_ERROR) /* Handle the error. */ while (true) { if (msg.FindString("extensions", i++, &ptr) != B_NO_ERROR) break; printf("> Extension: %s\\n", ptr); }
A given extension can be associated with more than one MIME type.
A NULL msg to SetFileExtensions() clears the type's extension list.
SetFileExtensions() clobbers the existing set of extensions. If you want to augment a type's extensions, you should retrieve the existing set, add the new ones, and then call SetFileExtensions().
| ||
Currently, there's no way to ask the database to give you a set of file types that map to a given extension. To find a type for an extension, you have to get all the installed types (GetInstalledTypes()) and ask each one for its set of extensions. |
RETURN CODES
virtual status_t GetIcon(BBitmap *icon, icon_size which) const virtual status_t SetIcon(const BBitmap *icon, icon_size which)
GetIcon() and SetIcon() get and set the icons that are associated (in the database) with this object's MIME type. You specify which icon you want (large or small) by passing B_LARGE_ICON or B_SMALL_ICON as the which argument. The icon is passed in or returned through the icon argument. The icon data is copied out of or into the BBitmap object.
If you're setting the icon, the bitmap must be the proper size: 32x32 for the large icon, 16x16 for the small one. If you want to erase the node's icon, pass NULL as the icon argument to SetIcon().
RETURN CODES
static status_t GetInstalledTypes(BMessage *types) static status_t GetInstalledTypes(const char *supertype, BMessage *subtypes) static status_t GetInstalledSupertypes(BMessage *supertypes)
These static functions retrieve all the file types that are currently installed in the database, all the installed subtypes for a given supertype, and all the installed supertypes. The types are copied into the "types" field of the passed-in BMessage (which must be allocated).
RETURN CODES
status_t GetLongDescription(char *description) const status_t SetLongDescription(const char *description) status_t GetShortDescription(char *description) const status_t SetShortDescription(const char *description)
Each file type has a couple of human-readable description strings associated with it. Neither description string may be longer than B_MIME_TYPE_LENGTH characters.
These functions get and set the long and short description strings. The Get functions copy the string into the argument (which must be allocated). The Set functions copy the string that the argument points to.
RETURN CODES
status_t GetPreferredApp(char *signature, app_verb verb = B_OPEN) const status_t SetPreferredApp(const char *signature, app_verb verb = B_OPEN)
These functions get and set the "preferred app" for this object's MIME type. The preferred app is the application that's used to access a file when, for example, the user double-clicks the file in a Tracker window: Unless the file identifies (in its attributes) a "custom" preferred app, the Tracker will ask the File Type database for the preferred app that's associated with the file's type.
RETURN CODES
status_t GetSupportingApps(BMessage *msg) const status_t SetSupportingApps(const BMessage *msg)
These functions get and set the list of the MIME type's "supporting apps." These are the applications that know how to deal with the type. The apps are identified by their signatures.
The BMessage's what field is unimportant. |
For example, to retrieve all the application (signatures) that support this object's MIME type, you would do the following:
BMessage msg(); uint32 i=0; char *ptr; if (mime.GetSupportingApps(&msg) != B_NO_ERROR) /* Handle the error. */ while (true) { if (msg.FindString("applications", i++, &ptr) != B_NO_ERROR) break; printf("> Supporting App: %s\\n", ptr); }
A given application can be associated with more than one MIME type.
A NULL msg to SetSupportingApps() clears the type's application signature list.
SetSupportingApps() clobbers the existing set of signatures. If you want to augment a type's signatures, you should retrieve the existing set, add the new ones, and then call SetSupportingApps(). |
RETURN CODES
status_t Install(void) status_t Delete(void) bool IsInstalled(void) const
Install() adds the object's MIME type to the File Type database. Delete() removes the type from the database. IsInstalled() tells you if the type is currently installed.
None of these functions affect the object's copy of the MIME type; for instance, deleting a MIME type from the databse doesn't uninitialize the object.
RETURN CODES
Currently, Install() may return a random value if the object is already installed. To avoid confusion, you should call IsInstalled() first: |
if (!mime.IsInstalled()) mime.Install();
These functions, which query and modify the database, only apply to MIME types that represent application signatures. The supertype of an application signature is always "application"; the subtype can be anything (within the boundaries of MIME validity).
status_t GetAppHint(entry_ref *app_ref) const status_t SetAppHint(const entry_ref *app_ref)
These functions get and set the "app hint" for the object's application signature. The app hint is an entry_ref that identifies the executable that should be used when launching an application that has this signature. For example, when the Tracker needs to launch an app of type "application/YourAppHere", it asks the database for the entry_ref hint. Of course, the entry_ref may not point to an application, or it might point to an application with the wrong signature (and so on)--that's why this is merely a hint.
GetAppHint() function initializes the entry_ref to the hint recorded in the database; the argument must be allocated before it's passed in.
SetAppHint() copies the entry_ref into the database. app_ref should point to an executable file that has the same signature as this object's MIME type.
Keep in mind that entry_refs aren't guaranteed to be persistent. |
RETURN CODES
status_t GetIconForType(const char *file_type, BBitmap *icon, icon_size which) const status_t SetIconForType(const char *file_type, const BBitmap *icon, icon_size which)
These functions get and set the icons that an application that has this object's MIME type as a signature uses to display the given file type. file_type must be a valid MIME string.
The icon is passed in or returned through the icon argument:
RETURN CODES
The Be Book, in lovely HTML, for the BeOS Preview Release.
Copyright © 1997 Be, Inc. All rights reserved.
Be is a registered trademark; BeOS, BeBox, BeWare, GeekPort, the Be logo, and the BeOS logo are trademarks of Be, Inc.
Last modified July 17, 1997.