From 2e19e8fc639dd64b12c103d6c826982b70e92ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sat, 27 Sep 2014 21:28:54 +0200 Subject: [PATCH] HaikuDepot: Extracted SharedBitmap into its own files --- src/apps/haikudepot/Jamfile | 1 + src/apps/haikudepot/PackageInfo.cpp | 265 -------------------------- src/apps/haikudepot/PackageInfo.h | 49 +---- src/apps/haikudepot/RatingView.cpp | 2 + src/apps/haikudepot/RatingView.h | 2 +- src/apps/haikudepot/SharedBitmap.cpp | 274 +++++++++++++++++++++++++++ src/apps/haikudepot/SharedBitmap.h | 63 ++++++ 7 files changed, 342 insertions(+), 314 deletions(-) create mode 100644 src/apps/haikudepot/SharedBitmap.cpp create mode 100644 src/apps/haikudepot/SharedBitmap.h diff --git a/src/apps/haikudepot/Jamfile b/src/apps/haikudepot/Jamfile index 03e5ff3638..34421b8f7a 100644 --- a/src/apps/haikudepot/Jamfile +++ b/src/apps/haikudepot/Jamfile @@ -54,6 +54,7 @@ Application HaikuDepot : RatePackageWindow.cpp RatingView.cpp support.cpp + SharedBitmap.cpp UserLoginWindow.cpp WebAppInterface.cpp diff --git a/src/apps/haikudepot/PackageInfo.cpp b/src/apps/haikudepot/PackageInfo.cpp index 762d900b51..9bb7cca367 100644 --- a/src/apps/haikudepot/PackageInfo.cpp +++ b/src/apps/haikudepot/PackageInfo.cpp @@ -8,272 +8,7 @@ #include -#include -#include -#include -#include -#include #include -#include -#include - -#include "support.h" - - -// #pragma mark - SharedBitmap - - -SharedBitmap::SharedBitmap(BBitmap* bitmap) - : - BReferenceable(), - fResourceID(-1), - fBuffer(NULL), - fSize(0), - fMimeType() -{ - fBitmap[0] = bitmap; - fBitmap[1] = NULL; - fBitmap[2] = NULL; -} - - -SharedBitmap::SharedBitmap(int32 resourceID) - : - BReferenceable(), - fResourceID(resourceID), - fBuffer(NULL), - fSize(0), - fMimeType() -{ - fBitmap[0] = NULL; - fBitmap[1] = NULL; - fBitmap[2] = NULL; -} - - -SharedBitmap::SharedBitmap(const char* mimeType) - : - BReferenceable(), - fResourceID(-1), - fBuffer(NULL), - fSize(0), - fMimeType(mimeType) -{ - fBitmap[0] = NULL; - fBitmap[1] = NULL; - fBitmap[2] = NULL; -} - - -SharedBitmap::SharedBitmap(BPositionIO& data) - : - BReferenceable(), - fResourceID(-1), - fBuffer(NULL), - fSize(0), - fMimeType() -{ - status_t status = data.GetSize(&fSize); - const off_t kMaxSize = 128 * 1024; - if (status == B_OK && fSize > 0 && fSize <= kMaxSize) { - fBuffer = new(std::nothrow) uint8[fSize]; - if (fBuffer != NULL) { - data.Seek(0, SEEK_SET); - - off_t bytesRead = 0; - size_t chunkSize = std::min((off_t)4096, fSize); - while (bytesRead < fSize) { - ssize_t read = data.Read(fBuffer + bytesRead, chunkSize); - if (read > 0) - bytesRead += read; - else - break; - } - - if (bytesRead != fSize) { - delete[] fBuffer; - fBuffer = NULL; - fSize = 0; - } - } else - fSize = 0; - } else { - fprintf(stderr, "SharedBitmap(): Stream too large: %" B_PRIi64 - ", max: %" B_PRIi64 "\n", fSize, kMaxSize); - } - - fBitmap[0] = NULL; - fBitmap[1] = NULL; - fBitmap[2] = NULL; -} - - -SharedBitmap::~SharedBitmap() -{ - delete fBitmap[0]; - delete fBitmap[1]; - delete fBitmap[2]; - delete[] fBuffer; -} - - -const BBitmap* -SharedBitmap::Bitmap(Size which) -{ - if (fResourceID == -1 && fMimeType.Length() == 0 && fBuffer == NULL) - return fBitmap[0]; - - int32 index = 0; - int32 size = 16; - - switch (which) { - default: - case SIZE_16: - break; - - case SIZE_32: - index = 1; - size = 32; - break; - case SIZE_64: - index = 2; - size = 64; - break; - } - - if (fBitmap[index] == NULL) { - if (fResourceID >= 0) - fBitmap[index] = _CreateBitmapFromResource(size); - else if (fBuffer != NULL) - fBitmap[index] = _CreateBitmapFromBuffer(size); - else if (fMimeType.Length() > 0) - fBitmap[index] = _CreateBitmapFromMimeType(size); - } - - return fBitmap[index]; -} - - -BBitmap* -SharedBitmap::_CreateBitmapFromResource(int32 size) const -{ - BResources resources; - status_t status = get_app_resources(resources); - if (status != B_OK) - return NULL; - - size_t dataSize; - const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE, fResourceID, - &dataSize); - if (data != NULL) - return _LoadIconFromBuffer(data, dataSize, size); - - data = resources.LoadResource(B_MESSAGE_TYPE, fResourceID, &dataSize); - if (data != NULL) - return _LoadBitmapFromBuffer(data, dataSize); - - return NULL; -} - - -BBitmap* -SharedBitmap::_CreateBitmapFromBuffer(int32 size) const -{ - BBitmap* bitmap = _LoadIconFromBuffer(fBuffer, fSize, size); - - if (bitmap == NULL) - bitmap = _LoadBitmapFromBuffer(fBuffer, fSize); - - return bitmap; -} - - -BBitmap* -SharedBitmap::_CreateBitmapFromMimeType(int32 size) const -{ - BMimeType mimeType(fMimeType.String()); - status_t status = mimeType.InitCheck(); - if (status != B_OK) - return NULL; - - BBitmap* bitmap = new BBitmap(BRect(0, 0, size - 1, size - 1), 0, B_RGBA32); - status = bitmap->InitCheck(); - if (status == B_OK) - status = mimeType.GetIcon(bitmap, B_MINI_ICON); - - if (status != B_OK) { - delete bitmap; - bitmap = NULL; - } - - return bitmap; -} - - -BBitmap* -SharedBitmap::_LoadBitmapFromBuffer(const void* buffer, size_t size) const -{ - BMemoryIO stream(buffer, size); - - // Try to read as an archived bitmap. - BBitmap* bitmap = _LoadArchivedBitmapFromStream(stream); - - if (bitmap == NULL) { - // Try to read as a translator bitmap - stream.Seek(0, SEEK_SET); - bitmap = _LoadTranslatorBitmapFromStream(stream); - } - - if (bitmap != NULL) { - status_t status = bitmap->InitCheck(); - if (status != B_OK) { - delete bitmap; - bitmap = NULL; - } - } - - return bitmap; -} - - -BBitmap* -SharedBitmap::_LoadArchivedBitmapFromStream(BPositionIO& stream) const -{ - BMessage archive; - status_t status = archive.Unflatten(&stream); - if (status != B_OK) - return NULL; - - return new BBitmap(&archive); -} - - -BBitmap* -SharedBitmap::_LoadTranslatorBitmapFromStream(BPositionIO& stream) const -{ - return BTranslationUtils::GetBitmap(&stream); -} - - -BBitmap* -SharedBitmap::_LoadIconFromBuffer(const void* data, size_t dataSize, - int32 size) const -{ - BBitmap* bitmap = new BBitmap(BRect(0, 0, size - 1, size - 1), 0, - B_RGBA32); - status_t status = bitmap->InitCheck(); - if (status == B_OK) { - status = BIconUtils::GetVectorIcon( - reinterpret_cast(data), dataSize, bitmap); - }; - - if (status != B_OK) { - delete bitmap; - bitmap = NULL; - } - - return bitmap; -} // #pragma mark - UserInfo diff --git a/src/apps/haikudepot/PackageInfo.h b/src/apps/haikudepot/PackageInfo.h index e903851772..64bc150722 100644 --- a/src/apps/haikudepot/PackageInfo.h +++ b/src/apps/haikudepot/PackageInfo.h @@ -13,54 +13,7 @@ #include "List.h" #include "PackageInfoListener.h" - - -class BBitmap; -class BPositionIO; - - -class SharedBitmap : public BReferenceable { -public: - enum Size { - SIZE_ANY = -1, - SIZE_16 = 0, - SIZE_32 = 1, - SIZE_64 = 2 - }; - - SharedBitmap(BBitmap* bitmap); - SharedBitmap(int32 resourceID); - SharedBitmap(const char* mimeType); - SharedBitmap(BPositionIO& data); - ~SharedBitmap(); - - const BBitmap* Bitmap(Size which); - -private: - BBitmap* _CreateBitmapFromResource(int32 size) const; - BBitmap* _CreateBitmapFromBuffer(int32 size) const; - BBitmap* _CreateBitmapFromMimeType(int32 size) const; - - BBitmap* _LoadBitmapFromBuffer(const void* buffer, - size_t dataSize) const; - BBitmap* _LoadArchivedBitmapFromStream( - BPositionIO& stream) const; - BBitmap* _LoadTranslatorBitmapFromStream( - BPositionIO& stream) const; - BBitmap* _LoadIconFromBuffer(const void* buffer, - size_t dataSize, int32 size) const; - -private: - int32 fResourceID; - uint8* fBuffer; - off_t fSize; - BString fMimeType; - BBitmap* fBitmap[3]; -}; - - -typedef BReference BitmapRef; -typedef List BitmapList; +#include "SharedBitmap.h" class UserInfo { diff --git a/src/apps/haikudepot/RatingView.cpp b/src/apps/haikudepot/RatingView.cpp index af62126c6e..ab97cb1fa0 100644 --- a/src/apps/haikudepot/RatingView.cpp +++ b/src/apps/haikudepot/RatingView.cpp @@ -6,6 +6,8 @@ #include "RatingView.h" +#include + RatingView::RatingView(const char* name) : diff --git a/src/apps/haikudepot/RatingView.h b/src/apps/haikudepot/RatingView.h index 73eb0137ad..3f439ead1e 100644 --- a/src/apps/haikudepot/RatingView.h +++ b/src/apps/haikudepot/RatingView.h @@ -8,7 +8,7 @@ #include -#include "PackageInfo.h" +#include "SharedBitmap.h" class RatingView : public BView { diff --git a/src/apps/haikudepot/SharedBitmap.cpp b/src/apps/haikudepot/SharedBitmap.cpp new file mode 100644 index 0000000000..70374ff226 --- /dev/null +++ b/src/apps/haikudepot/SharedBitmap.cpp @@ -0,0 +1,274 @@ +/* + * Copyright 2013-2014, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ + +#include "SharedBitmap.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "support.h" + + +SharedBitmap::SharedBitmap(BBitmap* bitmap) + : + BReferenceable(), + fResourceID(-1), + fBuffer(NULL), + fSize(0), + fMimeType() +{ + fBitmap[0] = bitmap; + fBitmap[1] = NULL; + fBitmap[2] = NULL; +} + + +SharedBitmap::SharedBitmap(int32 resourceID) + : + BReferenceable(), + fResourceID(resourceID), + fBuffer(NULL), + fSize(0), + fMimeType() +{ + fBitmap[0] = NULL; + fBitmap[1] = NULL; + fBitmap[2] = NULL; +} + + +SharedBitmap::SharedBitmap(const char* mimeType) + : + BReferenceable(), + fResourceID(-1), + fBuffer(NULL), + fSize(0), + fMimeType(mimeType) +{ + fBitmap[0] = NULL; + fBitmap[1] = NULL; + fBitmap[2] = NULL; +} + + +SharedBitmap::SharedBitmap(BPositionIO& data) + : + BReferenceable(), + fResourceID(-1), + fBuffer(NULL), + fSize(0), + fMimeType() +{ + status_t status = data.GetSize(&fSize); + const off_t kMaxSize = 128 * 1024; + if (status == B_OK && fSize > 0 && fSize <= kMaxSize) { + fBuffer = new(std::nothrow) uint8[fSize]; + if (fBuffer != NULL) { + data.Seek(0, SEEK_SET); + + off_t bytesRead = 0; + size_t chunkSize = std::min((off_t)4096, fSize); + while (bytesRead < fSize) { + ssize_t read = data.Read(fBuffer + bytesRead, chunkSize); + if (read > 0) + bytesRead += read; + else + break; + } + + if (bytesRead != fSize) { + delete[] fBuffer; + fBuffer = NULL; + fSize = 0; + } + } else + fSize = 0; + } else { + fprintf(stderr, "SharedBitmap(): Stream too large: %" B_PRIi64 + ", max: %" B_PRIi64 "\n", fSize, kMaxSize); + } + + fBitmap[0] = NULL; + fBitmap[1] = NULL; + fBitmap[2] = NULL; +} + + +SharedBitmap::~SharedBitmap() +{ + delete fBitmap[0]; + delete fBitmap[1]; + delete fBitmap[2]; + delete[] fBuffer; +} + + +const BBitmap* +SharedBitmap::Bitmap(Size which) +{ + if (fResourceID == -1 && fMimeType.Length() == 0 && fBuffer == NULL) + return fBitmap[0]; + + int32 index = 0; + int32 size = 16; + + switch (which) { + default: + case SIZE_16: + break; + + case SIZE_32: + index = 1; + size = 32; + break; + case SIZE_64: + index = 2; + size = 64; + break; + } + + if (fBitmap[index] == NULL) { + if (fResourceID >= 0) + fBitmap[index] = _CreateBitmapFromResource(size); + else if (fBuffer != NULL) + fBitmap[index] = _CreateBitmapFromBuffer(size); + else if (fMimeType.Length() > 0) + fBitmap[index] = _CreateBitmapFromMimeType(size); + } + + return fBitmap[index]; +} + + +BBitmap* +SharedBitmap::_CreateBitmapFromResource(int32 size) const +{ + BResources resources; + status_t status = get_app_resources(resources); + if (status != B_OK) + return NULL; + + size_t dataSize; + const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE, fResourceID, + &dataSize); + if (data != NULL) + return _LoadIconFromBuffer(data, dataSize, size); + + data = resources.LoadResource(B_MESSAGE_TYPE, fResourceID, &dataSize); + if (data != NULL) + return _LoadBitmapFromBuffer(data, dataSize); + + return NULL; +} + + +BBitmap* +SharedBitmap::_CreateBitmapFromBuffer(int32 size) const +{ + BBitmap* bitmap = _LoadIconFromBuffer(fBuffer, fSize, size); + + if (bitmap == NULL) + bitmap = _LoadBitmapFromBuffer(fBuffer, fSize); + + return bitmap; +} + + +BBitmap* +SharedBitmap::_CreateBitmapFromMimeType(int32 size) const +{ + BMimeType mimeType(fMimeType.String()); + status_t status = mimeType.InitCheck(); + if (status != B_OK) + return NULL; + + BBitmap* bitmap = new BBitmap(BRect(0, 0, size - 1, size - 1), 0, B_RGBA32); + status = bitmap->InitCheck(); + if (status == B_OK) + status = mimeType.GetIcon(bitmap, B_MINI_ICON); + + if (status != B_OK) { + delete bitmap; + bitmap = NULL; + } + + return bitmap; +} + + +BBitmap* +SharedBitmap::_LoadBitmapFromBuffer(const void* buffer, size_t size) const +{ + BMemoryIO stream(buffer, size); + + // Try to read as an archived bitmap. + BBitmap* bitmap = _LoadArchivedBitmapFromStream(stream); + + if (bitmap == NULL) { + // Try to read as a translator bitmap + stream.Seek(0, SEEK_SET); + bitmap = _LoadTranslatorBitmapFromStream(stream); + } + + if (bitmap != NULL) { + status_t status = bitmap->InitCheck(); + if (status != B_OK) { + delete bitmap; + bitmap = NULL; + } + } + + return bitmap; +} + + +BBitmap* +SharedBitmap::_LoadArchivedBitmapFromStream(BPositionIO& stream) const +{ + BMessage archive; + status_t status = archive.Unflatten(&stream); + if (status != B_OK) + return NULL; + + return new BBitmap(&archive); +} + + +BBitmap* +SharedBitmap::_LoadTranslatorBitmapFromStream(BPositionIO& stream) const +{ + return BTranslationUtils::GetBitmap(&stream); +} + + +BBitmap* +SharedBitmap::_LoadIconFromBuffer(const void* data, size_t dataSize, + int32 size) const +{ + BBitmap* bitmap = new BBitmap(BRect(0, 0, size - 1, size - 1), 0, + B_RGBA32); + status_t status = bitmap->InitCheck(); + if (status == B_OK) { + status = BIconUtils::GetVectorIcon( + reinterpret_cast(data), dataSize, bitmap); + }; + + if (status != B_OK) { + delete bitmap; + bitmap = NULL; + } + + return bitmap; +} + diff --git a/src/apps/haikudepot/SharedBitmap.h b/src/apps/haikudepot/SharedBitmap.h new file mode 100644 index 0000000000..72694665fc --- /dev/null +++ b/src/apps/haikudepot/SharedBitmap.h @@ -0,0 +1,63 @@ +/* + * Copyright 2013-2014, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ +#ifndef SHARED_BITMAP_H +#define SHARED_BITMAP_H + + +#include +#include + +#include "List.h" + + +class BBitmap; +class BPositionIO; + + +class SharedBitmap : public BReferenceable { +public: + enum Size { + SIZE_ANY = -1, + SIZE_16 = 0, + SIZE_32 = 1, + SIZE_64 = 2 + }; + + SharedBitmap(BBitmap* bitmap); + SharedBitmap(int32 resourceID); + SharedBitmap(const char* mimeType); + SharedBitmap(BPositionIO& data); + ~SharedBitmap(); + + const BBitmap* Bitmap(Size which); + +private: + BBitmap* _CreateBitmapFromResource(int32 size) const; + BBitmap* _CreateBitmapFromBuffer(int32 size) const; + BBitmap* _CreateBitmapFromMimeType(int32 size) const; + + BBitmap* _LoadBitmapFromBuffer(const void* buffer, + size_t dataSize) const; + BBitmap* _LoadArchivedBitmapFromStream( + BPositionIO& stream) const; + BBitmap* _LoadTranslatorBitmapFromStream( + BPositionIO& stream) const; + BBitmap* _LoadIconFromBuffer(const void* buffer, + size_t dataSize, int32 size) const; + +private: + int32 fResourceID; + uint8* fBuffer; + off_t fSize; + BString fMimeType; + BBitmap* fBitmap[3]; +}; + + +typedef BReference BitmapRef; +typedef List BitmapList; + + +#endif // SHARED_BITMAP_H