Icons Screensaver: Get icons from MIME db again

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.
This commit is contained in:
John Scipione
2014-01-24 16:01:35 -05:00
parent 82bcd89b92
commit 1c56a03c51
6 changed files with 85 additions and 76 deletions
@@ -5,17 +5,20 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Vincent Duvert, [email protected]
* John Scipione, [email protected] * John Scipione, [email protected]
*/ */
#include "IconDisplay.h" #include "IconDisplay.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <IconUtils.h> #include <IconUtils.h>
#include <View.h>
#include "VectorIcon.h"
#define RAND_BETWEEN(a, b) ((rand() % ((b) - (a) + 1) + (a))) #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, fBitmap = new BBitmap(BRect(0, 0, frame.Width(), frame.Height()), 0,
B_RGBA32); B_RGBA32);
BIconUtils::GetVectorIcon(icon->data, icon->size, fBitmap); if (BIconUtils::GetVectorIcon(icon->data, icon->size, fBitmap) != B_OK)
return;
fState = 0; fState = 0;
fTicks = 0; fTicks = 0;
@@ -81,15 +85,16 @@ IconDisplay::DrawOn(BView* view, uint32 delta)
switch (fState) { switch (fState) {
case 0: case 0:
// Progressive showing // progressive showing
if (fTicks < fDelay) if (fTicks < fDelay)
backColor.alpha = (fTicks * 255) / fDelay; backColor.alpha = (fTicks * 255) / fDelay;
else else
fState++; fState++;
break; break;
case 1: case 1:
// Completed showing // completed showing
backColor.alpha = 255; backColor.alpha = 255;
fTicks = 0; fTicks = 0;
fDelay = RAND_BETWEEN(STAY_TICKS_MIN, STAY_TICKS_MAX); fDelay = RAND_BETWEEN(STAY_TICKS_MIN, STAY_TICKS_MAX);
@@ -97,9 +102,10 @@ IconDisplay::DrawOn(BView* view, uint32 delta)
break; break;
case 2: case 2:
// Waiting // waiting
if (fTicks < fDelay) if (fTicks < fDelay)
return; return;
fTicks = 0; fTicks = 0;
backColor.alpha = 255; backColor.alpha = 255;
fDelay = RAND_BETWEEN(HIDE_TICKS_MIN, HIDE_TICKS_MAX); fDelay = RAND_BETWEEN(HIDE_TICKS_MIN, HIDE_TICKS_MAX);
@@ -108,7 +114,7 @@ IconDisplay::DrawOn(BView* view, uint32 delta)
break; break;
case 3: case 3:
// Progressive hiding // progressive hiding
if (fTicks < fDelay) { if (fTicks < fDelay) {
backColor.alpha = 255 - (fTicks * 255) / fDelay; backColor.alpha = 255 - (fTicks * 255) / fDelay;
} else { } else {
@@ -118,7 +124,7 @@ IconDisplay::DrawOn(BView* view, uint32 delta)
break; break;
default: default:
// Finished // finished
fIsRunning = false; fIsRunning = false;
return; return;
break; break;
@@ -5,6 +5,7 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Vincent Duvert, [email protected]
* John Scipione, [email protected] * John Scipione, [email protected]
*/ */
#ifndef ICON_DISPLAY_H #ifndef ICON_DISPLAY_H
@@ -12,18 +13,13 @@
#include <Rect.h> #include <Rect.h>
#include <SupportDefs.h>
#include <View.h>
struct vector_icon { struct vector_icon;
uint8* data;
size_t size;
type_code type;
};
class BBitmap; class BBitmap;
class BView;
class IconDisplay { class IconDisplay {
+27 -50
View File
@@ -5,29 +5,24 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Vincent Duvert, [email protected]
* John Scipione, [email protected] * John Scipione, [email protected]
*/ */
#include "IconsSaver.h" #include "IconsSaver.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <Catalog.h> #include <Catalog.h>
#include <Entry.h>
#include <MimeType.h> #include <MimeType.h>
#include <Node.h>
#include <NodeInfo.h>
#include <StringView.h>
#include <Query.h>
#include <Volume.h>
#include <VolumeRoster.h>
#include <BuildScreenSaverDefaultSettingsView.h> #include <BuildScreenSaverDefaultSettingsView.h>
#include "IconDisplay.h" #include "IconDisplay.h"
#include "VectorIcon.h"
#undef B_TRANSLATION_CONTEXT #undef B_TRANSLATION_CONTEXT
@@ -43,13 +38,12 @@ static const int32 kMinIconWidthPercentage = 5;
static const int32 kMaxIconWidthPercentage = 20; static const int32 kMaxIconWidthPercentage = 20;
// same here // same here
static const int32 kMinIconCount = 20; static const int32 kMinIconCount = 20;
static const int32 kMaxIconCount = 128; static const int32 kMaxIconCount = 300;
const rgb_color kBackgroundColor = ui_color(B_DESKTOP_COLOR); const rgb_color kBackgroundColor = ui_color(B_DESKTOP_COLOR);
BScreenSaver* instantiate_screen_saver(BMessage* msg, image_id image) BScreenSaver* instantiate_screen_saver(BMessage* msg, image_id image)
{ {
return new IconsSaver(msg, image); return new IconsSaver(msg, image);
@@ -172,7 +166,7 @@ IconsSaver::Draw(BView* view, int32 frame)
} }
int32 index = RAND_BETWEEN(0, fVectorIcons.CountItems() - 1); 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; return;
} }
} }
@@ -193,49 +187,32 @@ IconsSaver::StartConfig(BView* view)
void void
IconsSaver::_GetVectorIcons() IconsSaver::_GetVectorIcons()
{ {
BVolumeRoster volumeRoster; // Load vector icons from the MIME type database
BVolume volume; BMessage types;
while (volumeRoster.GetNextVolume(&volume) == B_OK) { if (BMimeType::GetInstalledTypes(&types) != B_OK)
if (!volume.KnowsAttr() || !volume.KnowsMime() || !volume.KnowsQuery()) 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; continue;
BQuery query; vector_icon* icon = (vector_icon*)malloc(sizeof(vector_icon));
query.SetVolume(&volume); if (icon == NULL)
query.SetPredicate("BEOS:APP_SIG=*"); continue;
query.Fetch();
entry_ref ref; if (mimeType.GetIcon(&icon->data, &icon->size) != B_OK) {
while (query.GetNextRef(&ref) == B_OK) { // didn't find an icon, delete the icon container
BFile file(&ref, B_READ_ONLY); delete icon;
if (file.InitCheck() != B_OK) continue;
continue; }
struct vector_icon* icon // found a vector icon, add it to the list
= (struct vector_icon*)malloc(sizeof(struct vector_icon)); fVectorIcons.AddItem(icon);
if (icon == NULL) if (fVectorIcons.CountItems() >= kMaxIconCount) {
continue; // this is enough to choose from, stop eating memory...
return;
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;
}
} }
} }
} }
+6 -2
View File
@@ -5,16 +5,20 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Vincent Duvert, [email protected]
* John Scipione, [email protected] * John Scipione, [email protected]
*/ */
#ifndef ICONS_SAVER_H #ifndef ICONS_SAVER_H
#define ICONS_SAVER_H #define ICONS_SAVER_H
#include <List.h> #include <ObjectList.h>
#include <ScreenSaver.h> #include <ScreenSaver.h>
struct vector_icon;
class IconDisplay; class IconDisplay;
@@ -33,7 +37,7 @@ public:
private: private:
void _GetVectorIcons(); void _GetVectorIcons();
BList fVectorIcons; BObjectList<vector_icon> fVectorIcons;
IconDisplay* fIcons; IconDisplay* fIcons;
BBitmap* fBackBitmap; BBitmap* fBackBitmap;
@@ -0,0 +1,21 @@
/*
* Copyright 2009 Vincent Duvert, [email protected]
* Copyright 2014 Haiku, Inc. All rights reserved.
*
* Distributed under the terms of the MIT License.
*
* Authors:
* Vincent Duvert, [email protected]
* John Scipione, [email protected]
*/
#ifndef VECTOR_ICON_H
#define VECTOR_ICON_H
struct vector_icon {
uint8* data;
size_t size;
};
#endif // VECTOR_ICON_H
+10 -5
View File
@@ -4,9 +4,11 @@
*/ */
#include "FileTypes.h"
#include "IconView.h" #include "IconView.h"
#include "MimeTypeListView.h"
#include <new>
#include <stdlib.h>
#include <string.h>
#include <Application.h> #include <Application.h>
#include <AppFileInfo.h> #include <AppFileInfo.h>
@@ -24,9 +26,8 @@
#include <Roster.h> #include <Roster.h>
#include <Size.h> #include <Size.h>
#include <new> #include "FileTypes.h"
#include <stdlib.h> #include "MimeTypeListView.h"
#include <string.h>
#undef B_TRANSLATION_CONTEXT #undef B_TRANSLATION_CONTEXT
@@ -182,6 +183,7 @@ Icon::SetTo(const BAppFileInfo& info, const char* type)
uint8* data; uint8* data;
size_t size; size_t size;
if (info.GetIconForType(type, &data, &size) == B_OK) { if (info.GetIconForType(type, &data, &size) == B_OK) {
// we have the vector icon, no need to get the rest // we have the vector icon, no need to get the rest
AdoptData(data, size); 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); status = info.SetIconForType(type, fMini, B_MINI_ICON);
if (fData != NULL || force) if (fData != NULL || force)
status = info.SetIconForType(type, fData, fSize); status = info.SetIconForType(type, fData, fSize);
return status; return status;
} }
@@ -285,6 +288,7 @@ Icon::CopyTo(BMimeType& type, bool force) const
status = type.SetIcon(fMini, B_MINI_ICON); status = type.SetIcon(fMini, B_MINI_ICON);
if (fData != NULL || force) if (fData != NULL || force)
status = type.SetIcon(fData, fSize); status = type.SetIcon(fData, fSize);
return status; return status;
} }
@@ -1264,6 +1268,7 @@ IconView::_SetIcon(BBitmap* large, BBitmap* mini, const uint8* data,
fType.SetIcon(mini, B_MINI_ICON); fType.SetIcon(mini, B_MINI_ICON);
if (data != NULL || force) if (data != NULL || force)
fType.SetIcon(data, size); fType.SetIcon(data, size);
// the icon shown will be updated automatically - we're watching // the icon shown will be updated automatically - we're watching
// any changes to the MIME database // any changes to the MIME database
} else if (fIconData != NULL) { } else if (fIconData != NULL) {