From 1c56a03c51fe268636ec1f48d067fa82f7082b1e Mon Sep 17 00:00:00 2001 From: John Scipione Date: Fri, 24 Jan 2014 03:44:09 -0500 Subject: [PATCH] Icons Screensaver: Get icons from MIME db again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous method only queried application icons, not document icons. Unfortunately, the app icons in the MIME DB are broken/not present. IconsSaver only displays document icons for the time being. Because of better error checking the garbled icons are filtered out though at least. Next step is to fix the application icons in the MIME DB. Some other changes: * Don't draw if the BBitmap was not filled out correctly * Add Vincent Duvert to authors list, he's already in copyright * Convert fVectorIcons from a BList to a BObjectList * Put vector_icon struct in it’s own header (needed for above) * Remove type param from vector_icon struct * Bump max icon count to 300, hopefully this should be enough, 128 is too few for app and document icons in default install. --- .../screen_savers/icons/IconDisplay.cpp | 30 +++++--- src/add-ons/screen_savers/icons/IconDisplay.h | 10 +-- .../screen_savers/icons/IconsSaver.cpp | 77 +++++++------------ src/add-ons/screen_savers/icons/IconsSaver.h | 8 +- src/add-ons/screen_savers/icons/VectorIcon.h | 21 +++++ src/preferences/filetypes/IconView.cpp | 15 ++-- 6 files changed, 85 insertions(+), 76 deletions(-) create mode 100644 src/add-ons/screen_savers/icons/VectorIcon.h diff --git a/src/add-ons/screen_savers/icons/IconDisplay.cpp b/src/add-ons/screen_savers/icons/IconDisplay.cpp index 3c3ca0b9f7..cc32a85aa7 100644 --- a/src/add-ons/screen_savers/icons/IconDisplay.cpp +++ b/src/add-ons/screen_savers/icons/IconDisplay.cpp @@ -5,17 +5,20 @@ * Distributed under the terms of the MIT License. * * Authors: + * Vincent Duvert, vincent.duvert@free.fr * John Scipione, jscipione@gmail.com */ #include "IconDisplay.h" -#include #include #include #include +#include + +#include "VectorIcon.h" #define RAND_BETWEEN(a, b) ((rand() % ((b) - (a) + 1) + (a))) @@ -48,7 +51,8 @@ IconDisplay::Run(vector_icon* icon, BRect frame) fBitmap = new BBitmap(BRect(0, 0, frame.Width(), frame.Height()), 0, B_RGBA32); - BIconUtils::GetVectorIcon(icon->data, icon->size, fBitmap); + if (BIconUtils::GetVectorIcon(icon->data, icon->size, fBitmap) != B_OK) + return; fState = 0; fTicks = 0; @@ -80,26 +84,28 @@ IconDisplay::DrawOn(BView* view, uint32 delta) rgb_color backColor = view->HighColor(); switch (fState) { - case 0: - // Progressive showing + case 0: + // progressive showing if (fTicks < fDelay) backColor.alpha = (fTicks * 255) / fDelay; else fState++; + break; - case 1: - // Completed showing + case 1: + // completed showing backColor.alpha = 255; fTicks = 0; fDelay = RAND_BETWEEN(STAY_TICKS_MIN, STAY_TICKS_MAX); fState++; break; - case 2: - // Waiting + case 2: + // waiting if (fTicks < fDelay) return; + fTicks = 0; backColor.alpha = 255; fDelay = RAND_BETWEEN(HIDE_TICKS_MIN, HIDE_TICKS_MAX); @@ -107,8 +113,8 @@ IconDisplay::DrawOn(BView* view, uint32 delta) return; break; - case 3: - // Progressive hiding + case 3: + // progressive hiding if (fTicks < fDelay) { backColor.alpha = 255 - (fTicks * 255) / fDelay; } else { @@ -117,8 +123,8 @@ IconDisplay::DrawOn(BView* view, uint32 delta) } break; - default: - // Finished + default: + // finished fIsRunning = false; return; break; diff --git a/src/add-ons/screen_savers/icons/IconDisplay.h b/src/add-ons/screen_savers/icons/IconDisplay.h index d077753603..17bec7095b 100644 --- a/src/add-ons/screen_savers/icons/IconDisplay.h +++ b/src/add-ons/screen_savers/icons/IconDisplay.h @@ -5,6 +5,7 @@ * Distributed under the terms of the MIT License. * * Authors: + * Vincent Duvert, vincent.duvert@free.fr * John Scipione, jscipione@gmail.com */ #ifndef ICON_DISPLAY_H @@ -12,18 +13,13 @@ #include -#include -#include -struct vector_icon { - uint8* data; - size_t size; - type_code type; -}; +struct vector_icon; class BBitmap; +class BView; class IconDisplay { diff --git a/src/add-ons/screen_savers/icons/IconsSaver.cpp b/src/add-ons/screen_savers/icons/IconsSaver.cpp index 8f23d45130..87cb7f074c 100644 --- a/src/add-ons/screen_savers/icons/IconsSaver.cpp +++ b/src/add-ons/screen_savers/icons/IconsSaver.cpp @@ -5,29 +5,24 @@ * Distributed under the terms of the MIT License. * * Authors: + * Vincent Duvert, vincent.duvert@free.fr * John Scipione, jscipione@gmail.com */ #include "IconsSaver.h" -#include #include #include #include -#include #include -#include -#include -#include -#include -#include -#include + #include #include "IconDisplay.h" +#include "VectorIcon.h" #undef B_TRANSLATION_CONTEXT @@ -43,13 +38,12 @@ static const int32 kMinIconWidthPercentage = 5; static const int32 kMaxIconWidthPercentage = 20; // same here static const int32 kMinIconCount = 20; -static const int32 kMaxIconCount = 128; +static const int32 kMaxIconCount = 300; const rgb_color kBackgroundColor = ui_color(B_DESKTOP_COLOR); - BScreenSaver* instantiate_screen_saver(BMessage* msg, image_id image) { return new IconsSaver(msg, image); @@ -172,7 +166,7 @@ IconsSaver::Draw(BView* view, int32 frame) } int32 index = RAND_BETWEEN(0, fVectorIcons.CountItems() - 1); - fIcons[i].Run((vector_icon*)fVectorIcons.ItemAt(index), iconFrame); + fIcons[i].Run(fVectorIcons.ItemAt(index), iconFrame); return; } } @@ -193,49 +187,32 @@ IconsSaver::StartConfig(BView* view) void IconsSaver::_GetVectorIcons() { - BVolumeRoster volumeRoster; - BVolume volume; - while (volumeRoster.GetNextVolume(&volume) == B_OK) { - if (!volume.KnowsAttr() || !volume.KnowsMime() || !volume.KnowsQuery()) + // Load vector icons from the MIME type database + BMessage types; + if (BMimeType::GetInstalledTypes(&types) != B_OK) + return; + + const char* type; + for (int32 i = 0; types.FindString("types", i, &type) == B_OK; i++) { + BMimeType mimeType(type); + if (mimeType.InitCheck() != B_OK) continue; - BQuery query; - query.SetVolume(&volume); - query.SetPredicate("BEOS:APP_SIG=*"); - query.Fetch(); + vector_icon* icon = (vector_icon*)malloc(sizeof(vector_icon)); + if (icon == NULL) + continue; - entry_ref ref; - while (query.GetNextRef(&ref) == B_OK) { - BFile file(&ref, B_READ_ONLY); - if (file.InitCheck() != B_OK) - continue; + if (mimeType.GetIcon(&icon->data, &icon->size) != B_OK) { + // didn't find an icon, delete the icon container + delete icon; + continue; + } - struct vector_icon* icon - = (struct vector_icon*)malloc(sizeof(struct vector_icon)); - if (icon == NULL) - continue; - - BNode node(&ref); - BNodeInfo nodeInfo(&node); - if (nodeInfo.InitCheck() != B_OK - || nodeInfo.GetIcon(&icon->data, &icon->size, &icon->type) - != B_OK) { - // didn't find an icon - continue; - } - - if (icon->type != B_VECTOR_ICON_TYPE) { - // found an icon, but it's not a vector icon - delete icon; - continue; - } - - // found a vector icon, add it to the list - fVectorIcons.AddItem(icon); - if (fVectorIcons.CountItems() >= kMaxIconCount) { - // this is enough to choose from, stop eating memory... - return; - } + // found a vector icon, add it to the list + fVectorIcons.AddItem(icon); + if (fVectorIcons.CountItems() >= kMaxIconCount) { + // this is enough to choose from, stop eating memory... + return; } } } diff --git a/src/add-ons/screen_savers/icons/IconsSaver.h b/src/add-ons/screen_savers/icons/IconsSaver.h index 6def51b8d6..d569faf7a0 100644 --- a/src/add-ons/screen_savers/icons/IconsSaver.h +++ b/src/add-ons/screen_savers/icons/IconsSaver.h @@ -5,16 +5,20 @@ * Distributed under the terms of the MIT License. * * Authors: + * Vincent Duvert, vincent.duvert@free.fr * John Scipione, jscipione@gmail.com */ #ifndef ICONS_SAVER_H #define ICONS_SAVER_H -#include +#include #include +struct vector_icon; + + class IconDisplay; @@ -33,7 +37,7 @@ public: private: void _GetVectorIcons(); - BList fVectorIcons; + BObjectList fVectorIcons; IconDisplay* fIcons; BBitmap* fBackBitmap; diff --git a/src/add-ons/screen_savers/icons/VectorIcon.h b/src/add-ons/screen_savers/icons/VectorIcon.h new file mode 100644 index 0000000000..18215804e0 --- /dev/null +++ b/src/add-ons/screen_savers/icons/VectorIcon.h @@ -0,0 +1,21 @@ +/* + * Copyright 2009 Vincent Duvert, vincent.duvert@free.fr + * Copyright 2014 Haiku, Inc. All rights reserved. + * + * Distributed under the terms of the MIT License. + * + * Authors: + * Vincent Duvert, vincent.duvert@free.fr + * John Scipione, jscipione@gmail.com + */ +#ifndef VECTOR_ICON_H +#define VECTOR_ICON_H + + +struct vector_icon { + uint8* data; + size_t size; +}; + + +#endif // VECTOR_ICON_H diff --git a/src/preferences/filetypes/IconView.cpp b/src/preferences/filetypes/IconView.cpp index f6850d58a6..577529fff5 100644 --- a/src/preferences/filetypes/IconView.cpp +++ b/src/preferences/filetypes/IconView.cpp @@ -4,9 +4,11 @@ */ -#include "FileTypes.h" #include "IconView.h" -#include "MimeTypeListView.h" + +#include +#include +#include #include #include @@ -24,9 +26,8 @@ #include #include -#include -#include -#include +#include "FileTypes.h" +#include "MimeTypeListView.h" #undef B_TRANSLATION_CONTEXT @@ -182,6 +183,7 @@ Icon::SetTo(const BAppFileInfo& info, const char* type) uint8* data; size_t size; + if (info.GetIconForType(type, &data, &size) == B_OK) { // we have the vector icon, no need to get the rest AdoptData(data, size); @@ -253,6 +255,7 @@ Icon::CopyTo(BAppFileInfo& info, const char* type, bool force) const status = info.SetIconForType(type, fMini, B_MINI_ICON); if (fData != NULL || force) status = info.SetIconForType(type, fData, fSize); + return status; } @@ -285,6 +288,7 @@ Icon::CopyTo(BMimeType& type, bool force) const status = type.SetIcon(fMini, B_MINI_ICON); if (fData != NULL || force) status = type.SetIcon(fData, fSize); + return status; } @@ -1264,6 +1268,7 @@ IconView::_SetIcon(BBitmap* large, BBitmap* mini, const uint8* data, fType.SetIcon(mini, B_MINI_ICON); if (data != NULL || force) fType.SetIcon(data, size); + // the icon shown will be updated automatically - we're watching // any changes to the MIME database } else if (fIconData != NULL) {