* All functions returning a message will now return an empty message in case there is

none yet, instead of returning an error.
* Cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21152 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2007-05-16 10:07:21 +00:00
parent 90c18b97c9
commit 5d2e4044c7

View File

@ -1,7 +1,12 @@
//---------------------------------------------------------------------- /*
// This software is part of the OpenBeOS distribution and is covered * Copyright 2002-2007, Haiku.
// by the OpenBeOS license. * Distributed under the terms of the MIT License.
//--------------------------------------------------------------------- *
* Authors:
* Tyler Dauwalder
*/
/*! /*!
\file database_access.cpp \file database_access.cpp
Mime database atomic read functions Mime database atomic read functions
@ -35,13 +40,10 @@ namespace BPrivate {
namespace Storage { namespace Storage {
namespace Mime { namespace Mime {
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
// get_app_hint /*! \brief Fetches the application hint for the given MIME type.
//! Fetches the application hint for the given MIME type.
/*! The entry_ref pointed to by \c ref must be pre-allocated. The entry_ref pointed to by \c ref must be pre-allocated.
\param type The MIME type of interest \param type The MIME type of interest
\param ref Pointer to a pre-allocated \c entry_ref struct into \param ref Pointer to a pre-allocated \c entry_ref struct into
@ -71,7 +73,7 @@ get_app_hint(const char *type, entry_ref *ref)
return status; return status;
} }
// get_attr_info
/*! \brief Fetches from the MIME database a BMessage describing the attributes /*! \brief Fetches from the MIME database a BMessage describing the attributes
typically associated with files of the given MIME type typically associated with files of the given MIME type
@ -90,16 +92,23 @@ status_t
get_attr_info(const char *type, BMessage *info) get_attr_info(const char *type, BMessage *info)
{ {
status_t err = read_mime_attr_message(type, kAttrInfoAttr, info); status_t err = read_mime_attr_message(type, kAttrInfoAttr, info);
if (!err) { if (err == B_ENTRY_NOT_FOUND) {
info->what = 233; // Don't know why, but that's what R5 does. // return an empty message
info->MakeEmpty();
err = B_OK;
}
if (err == B_OK) {
info->what = 233;
// Don't know why, but that's what R5 does.
err = info->AddString("type", type); err = info->AddString("type", type);
} }
return err; return err;
} }
// get_short_description
//! Fetches the short description for the given MIME type. /*! \brief Fetches the short description for the given MIME type.
/*! The string pointed to by \c description must be long enough to
The string pointed to by \c description must be long enough to
hold the short description; a length of \c B_MIME_TYPE_LENGTH is hold the short description; a length of \c B_MIME_TYPE_LENGTH is
recommended. recommended.
@ -117,7 +126,8 @@ status_t
get_short_description(const char *type, char *description) get_short_description(const char *type, char *description)
{ {
/// DBG(OUT("Mime::Database::get_short_description()\n")); /// DBG(OUT("Mime::Database::get_short_description()\n"));
ssize_t err = read_mime_attr(type, kShortDescriptionAttr, description, B_MIME_TYPE_LENGTH, kShortDescriptionType); ssize_t err = read_mime_attr(type, kShortDescriptionAttr, description,
B_MIME_TYPE_LENGTH, kShortDescriptionType);
return err >= 0 ? B_OK : err ; return err >= 0 ? B_OK : err ;
} }
@ -141,16 +151,19 @@ status_t
get_long_description(const char *type, char *description) get_long_description(const char *type, char *description)
{ {
// DBG(OUT("Mime::Database::get_long_description()\n")); // DBG(OUT("Mime::Database::get_long_description()\n"));
ssize_t err = read_mime_attr(type, kLongDescriptionAttr, description, B_MIME_TYPE_LENGTH, kLongDescriptionType); ssize_t err = read_mime_attr(type, kLongDescriptionAttr, description,
B_MIME_TYPE_LENGTH, kLongDescriptionType);
return err >= 0 ? B_OK : err ; return err >= 0 ? B_OK : err ;
} }
// get_file_extensions
//! Fetches a BMessage describing the MIME type's associated filename extensions /*! \brief Fetches a BMessage describing the MIME type's associated filename
/*! The list of extensions is returned in a pre-allocated BMessage pointed to by extensions
the \c extensions parameter (note that the any prior contents of the message
will be destroyed). Please see BMimeType::GetFileExtensions() for a description The list of extensions is returned in a pre-allocated BMessage pointed to
of the message format. by the \c extensions parameter (note that the any prior contents of the
message will be destroyed). Please see BMimeType::GetFileExtensions() for
a description of the message format.
\param extensions Pointer to a pre-allocated BMessage into which the MIME \param extensions Pointer to a pre-allocated BMessage into which the MIME
type's associated file extensions will be stored. type's associated file extensions will be stored.
@ -162,16 +175,22 @@ status_t
get_file_extensions(const char *type, BMessage *extensions) get_file_extensions(const char *type, BMessage *extensions)
{ {
status_t err = read_mime_attr_message(type, kFileExtensionsAttr, extensions); status_t err = read_mime_attr_message(type, kFileExtensionsAttr, extensions);
if (!err) { if (err == B_ENTRY_NOT_FOUND) {
// return an empty message
extensions->MakeEmpty();
err = B_OK;
}
if (err == B_OK) {
extensions->what = 234; // Don't know why, but that's what R5 does. extensions->what = 234; // Don't know why, but that's what R5 does.
err = extensions->AddString("type", type); err = extensions->AddString("type", type);
} }
return err; return err;
} }
// get_icon
//! Fetches the icon of given size associated with the given MIME type /*! \brief Fetches the icon of given size associated with the given MIME type
/*! The bitmap pointed to by \c icon must be of the proper size (\c 32x32
The bitmap pointed to by \c icon must be of the proper size (\c 32x32
for \c B_LARGE_ICON, \c 16x16 for \c B_MINI_ICON) and color depth for \c B_LARGE_ICON, \c 16x16 for \c B_MINI_ICON) and color depth
(\c B_CMAP8). (\c B_CMAP8).
@ -412,18 +431,20 @@ get_icon_for_type(const char* type, const char* fileType, uint8** data,
return err; return err;
} }
// get_preferred_app
//! Fetches signature of the MIME type's preferred application for the given action. /*! \brief Fetches signature of the MIME type's preferred application for the
/*! The string pointed to by \c signature must be long enough to given action.
The string pointed to by \c signature must be long enough to
hold the short description; a length of \c B_MIME_TYPE_LENGTH is hold the short description; a length of \c B_MIME_TYPE_LENGTH is
recommended. recommended.
Currently, the only supported app verb is \c B_OPEN. Currently, the only supported app verb is \c B_OPEN.
\param type The MIME type of interest \param type The MIME type of interest
\param description Pointer to a pre-allocated string into which the preferred \param description Pointer to a pre-allocated string into which the
application's signature is copied. If the function fails, the preferred application's signature is copied. If the function fails,
contents of the string are undefined. the contents of the string are undefined.
\param verb \c The action of interest \param verb \c The action of interest
\return \return
@ -435,11 +456,12 @@ status_t
get_preferred_app(const char *type, char *signature, app_verb verb = B_OPEN) get_preferred_app(const char *type, char *signature, app_verb verb = B_OPEN)
{ {
// Since B_OPEN is the currently the only app_verb, it is essentially ignored // Since B_OPEN is the currently the only app_verb, it is essentially ignored
ssize_t err = read_mime_attr(type, kPreferredAppAttr, signature, B_MIME_TYPE_LENGTH, kPreferredAppType); ssize_t err = read_mime_attr(type, kPreferredAppAttr, signature,
B_MIME_TYPE_LENGTH, kPreferredAppType);
return err >= 0 ? B_OK : err ; return err >= 0 ? B_OK : err ;
} }
// get_sniffer_rule
/*! \brief Fetches the sniffer rule for the given MIME type. /*! \brief Fetches the sniffer rule for the given MIME type.
\param type The MIME type of interest \param type The MIME type of interest
\param result Pointer to a pre-allocated BString into which the type's \param result Pointer to a pre-allocated BString into which the type's
@ -455,19 +477,24 @@ get_sniffer_rule(const char *type, BString *result)
return read_mime_attr_string(type, kSnifferRuleAttr, result); return read_mime_attr_string(type, kSnifferRuleAttr, result);
} }
// get_supported_types
status_t status_t
get_supported_types(const char *type, BMessage *types) get_supported_types(const char *type, BMessage *types)
{ {
status_t err = read_mime_attr_message(type, kSupportedTypesAttr, types); status_t err = read_mime_attr_message(type, kSupportedTypesAttr, types);
if (!err) { if (err == B_ENTRY_NOT_FOUND) {
// return an empty message
types->MakeEmpty();
err = B_OK;
}
if (err == B_OK) {
types->what = 0; types->what = 0;
err = types->AddString("type", type); err = types->AddString("type", type);
} }
return err; return err;
} }
// is_installed
//! Checks if the given MIME type is present in the database //! Checks if the given MIME type is present in the database
bool bool
is_installed(const char *type) is_installed(const char *type)
@ -476,22 +503,25 @@ is_installed(const char *type)
return open_type(type, &node) == B_OK; return open_type(type, &node) == B_OK;
} }
// get_icon_data
/*! \brief Returns properly formatted raw bitmap data, ready to be shipped off to the hacked
up 4-parameter version of Database::SetIcon()
This function exists as something of a hack until an OBOS::BBitmap implementation is /*! \brief Returns properly formatted raw bitmap data, ready to be shipped off
available. It takes the given bitmap, converts it to the B_CMAP8 color space if necessary to the hacked up 4-parameter version of Database::SetIcon()
and able, and returns said bitmap data in a newly allocated array pointed to by the
pointer that's pointed to by \c data. The length of the array is stored in the integer
pointed to by \c dataSize. The array is allocated with \c new[], and it's your responsibility
to \c delete[] it when you're finished.
This function exists as something of a hack until an OBOS::BBitmap
implementation is available. It takes the given bitmap, converts it to the
B_CMAP8 color space if necessary and able, and returns said bitmap data in
a newly allocated array pointed to by the pointer that's pointed to by
\c data. The length of the array is stored in the integer pointed to by
\c dataSize. The array is allocated with \c new[], and it's your
responsibility to \c delete[] it when you're finished.
*/ */
status_t status_t
get_icon_data(const BBitmap *icon, icon_size which, void **data, int32 *dataSize) get_icon_data(const BBitmap *icon, icon_size which, void **data,
int32 *dataSize)
{ {
ssize_t err = (icon && data && dataSize && icon->InitCheck() == B_OK) ? B_OK : B_BAD_VALUE; if (icon == NULL || data == NULL || dataSize == 0
|| icon->InitCheck() != B_OK)
return B_BAD_VALUE;
BRect bounds; BRect bounds;
BBitmap *icon8 = NULL; BBitmap *icon8 = NULL;
@ -499,7 +529,6 @@ get_icon_data(const BBitmap *icon, icon_size which, void **data, int32 *dataSize
bool otherColorSpace = false; bool otherColorSpace = false;
// Figure out what kind of data we *should* have // Figure out what kind of data we *should* have
if (!err) {
switch (which) { switch (which) {
case B_MINI_ICON: case B_MINI_ICON:
bounds.Set(0, 0, 15, 15); bounds.Set(0, 0, 15, 15);
@ -508,13 +537,12 @@ get_icon_data(const BBitmap *icon, icon_size which, void **data, int32 *dataSize
bounds.Set(0, 0, 31, 31); bounds.Set(0, 0, 31, 31);
break; break;
default: default:
err = B_BAD_VALUE; return B_BAD_VALUE;
break;
}
} }
// Check the icon // Check the icon
if (!err) status_t err = icon->Bounds() == bounds ? B_OK : B_BAD_VALUE;
err = (icon->Bounds() == bounds) ? B_OK : B_BAD_VALUE;
// Convert to B_CMAP8 if necessary // Convert to B_CMAP8 if necessary
if (!err) { if (!err) {
otherColorSpace = (icon->ColorSpace() != B_CMAP8); otherColorSpace = (icon->ColorSpace() != B_CMAP8);
@ -534,12 +562,14 @@ get_icon_data(const BBitmap *icon, icon_size which, void **data, int32 *dataSize
*dataSize = icon->BitsLength(); *dataSize = icon->BitsLength();
} }
} }
// Alloc a new data buffer // Alloc a new data buffer
if (!err) { if (!err) {
*data = new(std::nothrow) char[*dataSize]; *data = new(std::nothrow) char[*dataSize];
if (!*data) if (!*data)
err = B_NO_MEMORY; err = B_NO_MEMORY;
} }
// Copy the data into it. // Copy the data into it.
if (!err) if (!err)
memcpy(*data, srcData, *dataSize); memcpy(*data, srcData, *dataSize);