From 8ce6cfc99957fe048604bbdb48784287cf5f7ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 17 Aug 2008 14:32:33 +0000 Subject: [PATCH] * Added two more public functions: get_named_icon(). * Implemented B_GET_ICON_NAME in get_device_icon(). * Comments welcome. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27004 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/storage/Mime.h | 4 ++ src/kits/storage/Mime.cpp | 108 +++++++++++++++++++++++++++++++++++--- 2 files changed, 104 insertions(+), 8 deletions(-) diff --git a/headers/os/storage/Mime.h b/headers/os/storage/Mime.h index 2793145de5..20eac6de35 100644 --- a/headers/os/storage/Mime.h +++ b/headers/os/storage/Mime.h @@ -50,6 +50,10 @@ status_t get_device_icon(const char* device, BBitmap* icon, icon_size which); status_t get_device_icon(const char* device, uint8** _data, size_t* _size, type_code* _type); +status_t get_named_icon(const char* name, BBitmap* icon, icon_size which); +status_t get_named_icon(const char* name, uint8** _data, size_t* _size, + type_code* _type); + // include MimeType.h for convenience # include #endif // __cplusplus diff --git a/src/kits/storage/Mime.cpp b/src/kits/storage/Mime.cpp index 3c01f34560..bd80c8714f 100644 --- a/src/kits/storage/Mime.cpp +++ b/src/kits/storage/Mime.cpp @@ -20,16 +20,19 @@ #include #include -#include -#include #include #include #include +#include +#include +#include +#include #include #include #include #include #include +#include #include #include #include @@ -266,14 +269,18 @@ get_device_icon(const char *device, uint8** _data, size_t* _size, if (fd < 0) return errno; - // TODO: support B_GET_ICON_NAME! -#if 0 + // Try to get the icon by name first + char name[B_FILE_NAME_LENGTH]; - if (ioctl(fd, B_GET_ICON_NAME, name) == 0) { - close(fd); - return B_OK; + if (ioctl(fd, B_GET_ICON_NAME, name) >= 0) { + status_t status = get_named_icon(name, _data, _size, _type); + if (status == B_OK) { + close(fd); + return B_OK; + } } -#endif + + // Getting the named icon failed, try vector icon next uint8 data[8192]; device_icon iconData = {sizeof(data), data}; @@ -302,6 +309,91 @@ get_device_icon(const char *device, uint8** _data, size_t* _size, *_type = B_VECTOR_ICON_TYPE; } + // TODO: also support getting the old icon? close(fd); return status; } + + +status_t +get_named_icon(const char* name, BBitmap* icon, icon_size which) +{ + // check parameters + if (name == NULL || icon == NULL) + return B_BAD_VALUE; + + BRect rect; + if (which == B_MINI_ICON) + rect.Set(0, 0, 15, 15); + else if (which == B_LARGE_ICON) + rect.Set(0, 0, 31, 31); + else + return B_BAD_VALUE; + + if (icon->Bounds() != rect) + return B_BAD_VALUE; + + uint8* data; + size_t size; + type_code type; + status_t status = get_named_icon(name, &data, &size, &type); + if (status == B_OK) + status = BIconUtils::GetVectorIcon(data, size, icon); + + delete[] data; + return status; +} + + +status_t +get_named_icon(const char* name, uint8** _data, size_t* _size, type_code* _type) +{ + if (name == NULL || _data == NULL || _size == NULL || _type == NULL) + return B_BAD_VALUE; + + directory_which kWhich[] = { + B_USER_DATA_DIRECTORY, + B_COMMON_DATA_DIRECTORY, + B_BEOS_DATA_DIRECTORY, + }; + + status_t status = B_ENTRY_NOT_FOUND; + BFile file; + off_t size; + + for (uint32 i = 0; i < sizeof(kWhich) / sizeof(kWhich[0]); i++) { + BPath path; + if (find_directory(kWhich[i], &path) != B_OK) + continue; + + path.Append("icons"); + path.Append(name); + + status = file.SetTo(path.Path(), B_READ_ONLY); + if (status == B_OK) { + status = file.GetSize(&size); + if (size > 1024 * 1024) + status = B_ERROR; + } + if (status == B_OK) + break; + } + + if (status != B_OK) + return status; + + *_data = new(std::nothrow) uint8[size]; + if (*_data == NULL) + return B_NO_MEMORY; + + if (file.Read(*_data, size) != size) { + delete[] *_data; + return B_ERROR; + } + + *_size = size; + *_type = B_VECTOR_ICON_TYPE; + // TODO: for now + + return B_OK; +}