Skip to content

Commit 50b97d9

Browse files
committed
Downloading thumbnails is working
* You can double click one of the icons, and a message is send to the window containing all need information. Nothing happens then yet, though.
1 parent 8b98144 commit 50b97d9

File tree

5 files changed

+270
-183
lines changed

5 files changed

+270
-183
lines changed

CoverWindow.cpp

+144-39
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,37 @@
1515
#include <TextControl.h>
1616
#include <TranslationUtils.h>
1717

18+
#include "Query.h"
19+
#include "Utility.h"
20+
1821

1922
static const uint32 kIconSize = 64;
23+
static const uint32 kMaxColumns = 4;
2024

2125
static const uint32 kMsgAddImage = 'adIm';
2226
static const uint32 kMsgQuery = 'quer';
27+
static const uint32 kMsgSelect = 'sele';
28+
29+
30+
class ImageLoaderResultListener : public ResultListener {
31+
public:
32+
ImageLoaderResultListener(
33+
const BMessenger& target);
34+
virtual ~ImageLoaderResultListener();
35+
36+
virtual void AddResult(const BString& id,
37+
const char* info, UrlMap urls);
38+
39+
private:
40+
BMessenger fTarget;
41+
};
2342

2443

2544
class IconView : public BView {
2645
public:
27-
IconView(const char* id, BBitmap* bitmap);
46+
IconView(const char* identifier,
47+
const char* info, const char* url,
48+
const char* imageUrl, BBitmap* bitmap);
2849
virtual ~IconView();
2950

3051
void SetSelected(bool selected);
@@ -35,26 +56,93 @@ class IconView : public BView {
3556

3657
private:
3758
BString fIdentifier;
59+
BString fInfo;
60+
BString fUrl;
61+
BString fImageUrl;
3862
BBitmap* fBitmap;
3963
bool fSelected;
4064
float fDescent;
4165
};
4266

4367

68+
// #pragma mark - ImageLoaderResultListener
69+
70+
71+
ImageLoaderResultListener::ImageLoaderResultListener(const BMessenger& target)
72+
:
73+
fTarget(target)
74+
{
75+
}
76+
77+
78+
ImageLoaderResultListener::~ImageLoaderResultListener()
79+
{
80+
}
81+
82+
83+
void
84+
ImageLoaderResultListener::AddResult(const BString& id, const char* info,
85+
UrlMap urls)
86+
{
87+
BBitmap* bitmap = NULL;
88+
89+
UrlType thumbTypes[] = {kThumbImage, kLargeImage, kOriginalImage};
90+
for (int index = 0; index < 3; index++) {
91+
UrlMap::const_iterator found = urls.find(thumbTypes[index]);
92+
if (found == urls.end())
93+
continue;
94+
95+
const BUrl& url = found->second;
96+
printf("LOAD: %s\n", url.UrlString().String());
97+
98+
bitmap = FetchImage(url);
99+
if (bitmap != NULL)
100+
break;
101+
102+
fprintf(stderr, "Could not load image: %s\n",
103+
url.UrlString().String());
104+
}
105+
if (bitmap == NULL)
106+
return;
107+
108+
BMessage add(kMsgAddImage);
109+
add.AddPointer("image", bitmap);
110+
add.AddString("id", id);
111+
add.AddString("info", info);
112+
add.AddString("url", urls[kSource].UrlString().String());
113+
114+
UrlType types[] = {kOriginalImage, kLargeImage};
115+
for (int index = 0; index < 2; index++) {
116+
UrlMap::const_iterator found = urls.find(types[index]);
117+
if (found == urls.end())
118+
continue;
119+
120+
add.AddString("image_url", found->second.UrlString().String());
121+
break;
122+
}
123+
124+
fTarget.SendMessage(&add);
125+
}
126+
127+
44128
// #pragma mark - IconView
45129

46130

47-
IconView::IconView(const char* identifier, BBitmap* bitmap)
131+
IconView::IconView(const char* identifier, const char* info, const char* url,
132+
const char* imageUrl, BBitmap* bitmap)
48133
:
49134
BView("image", B_WILL_DRAW),
50135
fIdentifier(identifier),
136+
fInfo(info),
137+
fUrl(url),
138+
fImageUrl(imageUrl),
51139
fBitmap(bitmap),
52140
fSelected(false)
53141
{
54142
font_height fontHeight;
55143
GetFontHeight(&fontHeight);
56144

57-
float width = StringWidth("WWWWWWWWWWWW");
145+
float width = StringWidth("WWWWWWWWWWWWWWWWWWW");
58146
float height = kIconSize + be_plain_font->Size() / 12.0f * 3.0f
59147
+ ceilf(fontHeight.ascent + fontHeight.descent);
60148

@@ -91,7 +179,15 @@ IconView::IsSelected()
91179
void
92180
IconView::MouseDown(BPoint where)
93181
{
94-
SetSelected(!IsSelected());
182+
int32 count = Window()->CurrentMessage()->GetInt32("clicks", 1);
183+
if (count == 1)
184+
SetSelected(!IsSelected());
185+
else if (count == 2) {
186+
BMessage select(kMsgSelect);
187+
select.AddString("id", fIdentifier);
188+
select.AddString("image_url", fImageUrl);
189+
Window()->PostMessage(&select);
190+
}
95191
}
96192

97193

@@ -107,9 +203,11 @@ IconView::Draw(BRect updateRect)
107203

108204
DrawBitmap(fBitmap, imageBounds);
109205

110-
BString text = fIdentifier;
111-
text << " " << fBitmap->Bounds().IntegerWidth() << " x "
112-
<< fBitmap->Bounds().IntegerHeight();
206+
BString text = fInfo;
207+
if (text.IsEmpty())
208+
text = fIdentifier;
209+
else
210+
text << " (" << fIdentifier << ")";
113211

114212
float width = StringWidth(text.String());
115213
if (width > bounds.Width()) {
@@ -164,30 +262,12 @@ CoverWindow::CoverWindow()
164262
.Add(fTitleControl)
165263
.End()
166264
.Add(scrollView);
167-
168-
// TODO: for testing purposes only!
169-
const char* files[] = {"/Media/Kram/Pics/band.jpg",
170-
"/boot/home/Desktop/CD Cover/Antemasque - dto.jpg",
171-
"/boot/home/Desktop/CD Cover/Danzig - dto.jpg",
172-
"/boot/home/Desktop/CD Cover/Kate Bush - Never For Ever.jpg",
173-
"/boot/home/Desktop/animation.gif",
174-
"/boot/home/develop/haiku/haiku/data/artwork/HAIKU logo - white on black - normal.png",
175-
"/boot/home/develop/haiku/haiku/data/artwork/HAIKU square - white on blue.png", NULL};
176-
for (int i = 0; files[i] != NULL; i++) {
177-
BBitmap* bitmap = BTranslationUtils::GetBitmapFile(files[i]);
178-
if (bitmap == NULL)
179-
continue;
180-
181-
BMessage add(kMsgAddImage);
182-
add.AddString("amazon_id", "BX348938");
183-
add.AddPointer("image", bitmap);
184-
PostMessage(&add);
185-
}
186265
}
187266

188267

189268
CoverWindow::~CoverWindow()
190269
{
270+
_AbortQuery();
191271
}
192272

193273

@@ -199,32 +279,57 @@ CoverWindow::MessageReceived(BMessage* message)
199279
{
200280
BBitmap* bitmap = (BBitmap*)message->GetPointer("image");
201281
if (bitmap != NULL) {
202-
const char* identifier = message->GetString("amazon_id");
203-
fMainView->AddChild(new IconView(identifier, bitmap));
204-
// fMainView->GridLayout()->AddView(new IconView(bitmap), 0, 0);
282+
const char* identifier = message->GetString("id");
283+
const char* info = message->GetString("info");
284+
const char* url = message->GetString("url");
285+
const char* imageUrl = message->GetString("image_url");
286+
287+
IconView* view = new IconView(identifier, info, url, imageUrl,
288+
bitmap);
289+
fMainView->GridLayout()->AddView(view,
290+
fCount % kMaxColumns, fCount / kMaxColumns);
291+
fCount++;
205292
}
206293
break;
207294
}
295+
case kMsgSelect:
296+
{
297+
message->PrintToStream();
298+
break;
299+
}
208300
case kMsgQuery:
209301
{
210-
if (fQuery != NULL) {
211-
// Abort
212-
213-
// Remove previous results
214-
while (BView* view = fMainView->ChildAt(0)) {
215-
fMainView->RemoveChild(view);
216-
delete view;
217-
}
218-
}
302+
_AbortQuery();
219303

220304
// Start new query
221305
BString artist = fArtistControl->Text();
222306
BString title = fTitleControl->Text();
223-
printf("A: %s, T: %s\n", artist.String(), title.String());
307+
308+
fQuery = new Query(artist, title);
309+
fQuery->AddListener(new ImageLoaderResultListener(this));
310+
fQuery->Run();
224311
break;
225312
}
226313
default:
227314
BWindow::MessageReceived(message);
228315
break;
229316
}
230317
}
318+
319+
320+
void
321+
CoverWindow::_AbortQuery()
322+
{
323+
if (fQuery != NULL) {
324+
// TODO: abort asynchronously in message loop!
325+
fQuery->Abort();
326+
delete fQuery;
327+
328+
// Remove previous results
329+
while (BView* view = fMainView->ChildAt(0)) {
330+
fMainView->RemoveChild(view);
331+
delete view;
332+
}
333+
fCount++;
334+
}
335+
}

CoverWindow.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
class BGridView;
1212
class BTextControl;
1313

14+
class Query;
15+
1416

1517
class CoverWindow : public BWindow {
1618
public:
@@ -19,11 +21,15 @@ class CoverWindow : public BWindow {
1921

2022
virtual void MessageReceived(BMessage* message);
2123

24+
private:
25+
void _AbortQuery();
26+
2227
private:
2328
BGridView* fMainView;
2429
BTextControl* fArtistControl;
2530
BTextControl* fTitleControl;
26-
void* fQuery;
31+
Query* fQuery;
32+
int32 fCount;
2733
};
2834

2935

0 commit comments

Comments
 (0)