Skip to content

Commit 5096465

Browse files
committed
CoversWindow is almost done
* At least using the imperfectly suited GridLayout * Moved image loading functionality into Utility.cpp/h
1 parent 50b97d9 commit 5096465

File tree

5 files changed

+131
-137
lines changed

5 files changed

+131
-137
lines changed

CoverWindow.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77

88
#include <stdio.h>
99

10+
#include <Application.h>
1011
#include <Bitmap.h>
12+
#include <Button.h>
1113
#include <GridView.h>
1214
#include <LayoutBuilder.h>
1315
#include <ScrollView.h>
1416
#include <String.h>
1517
#include <TextControl.h>
1618
#include <TranslationUtils.h>
1719

20+
#include "covers.h"
1821
#include "Query.h"
1922
#include "Utility.h"
2023

@@ -24,7 +27,6 @@ static const uint32 kMaxColumns = 4;
2427

2528
static const uint32 kMsgAddImage = 'adIm';
2629
static const uint32 kMsgQuery = 'quer';
27-
static const uint32 kMsgSelect = 'sele';
2830

2931

3032
class ImageLoaderResultListener : public ResultListener {
@@ -183,7 +185,7 @@ IconView::MouseDown(BPoint where)
183185
if (count == 1)
184186
SetSelected(!IsSelected());
185187
else if (count == 2) {
186-
BMessage select(kMsgSelect);
188+
BMessage select(kMsgSelected);
187189
select.AddString("id", fIdentifier);
188190
select.AddString("image_url", fImageUrl);
189191
Window()->PostMessage(&select);
@@ -247,6 +249,7 @@ CoverWindow::CoverWindow()
247249
{
248250
fArtistControl = new BTextControl("Artist", "", new BMessage(kMsgQuery));
249251
fTitleControl = new BTextControl("Title", "", new BMessage(kMsgQuery));
252+
BButton* skipButton = new BButton("Skip", new BMessage(kMsgSkip));
250253

251254
fMainView = new BGridView("main");
252255
fMainView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
@@ -260,8 +263,11 @@ CoverWindow::CoverWindow()
260263
.SetInsets(B_USE_DEFAULT_SPACING)
261264
.Add(fArtistControl)
262265
.Add(fTitleControl)
266+
.Add(skipButton)
263267
.End()
264268
.Add(scrollView);
269+
270+
fArtistControl->MakeFocus(true);
265271
}
266272

267273

@@ -292,9 +298,11 @@ CoverWindow::MessageReceived(BMessage* message)
292298
}
293299
break;
294300
}
295-
case kMsgSelect:
301+
case kMsgSelected:
296302
{
297303
message->PrintToStream();
304+
be_app->PostMessage(kMsgSelected);
305+
PostMessage(B_QUIT_REQUESTED);
298306
break;
299307
}
300308
case kMsgQuery:
@@ -310,6 +318,10 @@ CoverWindow::MessageReceived(BMessage* message)
310318
fQuery->Run();
311319
break;
312320
}
321+
case kMsgSkip:
322+
be_app->PostMessage(kMsgSkip);
323+
PostMessage(B_QUIT_REQUESTED);
324+
break;
313325
default:
314326
BWindow::MessageReceived(message);
315327
break;

Utility.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2019 pinc Software. All Rights Reserved.
3+
*/
4+
5+
6+
#include "Utility.h"
7+
8+
#include <Bitmap.h>
9+
#include <DataIO.h>
10+
#include <TranslationUtils.h>
11+
12+
#include <stdio.h>
13+
14+
15+
BBitmap*
16+
FetchImage(const BUrl& url)
17+
{
18+
BHttpRequest request(url);
19+
20+
DataListener listener;
21+
request.SetListener(&listener);
22+
request.Run();
23+
while (request.IsRunning()) {
24+
snooze(50000);
25+
putchar('.');
26+
fflush(stdout);
27+
}
28+
29+
const BHttpResult& result = dynamic_cast<const BHttpResult&>(
30+
request.Result());
31+
printf("Status: %" B_PRId32 " d - %s\n", result.StatusCode(),
32+
result.StatusText().String());
33+
34+
listener.IO().Seek(0, SEEK_SET);
35+
return BTranslationUtils::GetBitmap(&listener.IO());
36+
}

Utility.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2019 pinc Software. All Rights Reserved.
3+
*/
4+
#ifndef UTILITY_H
5+
#define UTILITY_H
6+
7+
8+
#include <HttpRequest.h>
9+
10+
11+
class BBitmap;
12+
13+
14+
class DataListener : public BUrlProtocolListener {
15+
public:
16+
DataListener()
17+
{
18+
}
19+
20+
virtual ~DataListener()
21+
{
22+
}
23+
24+
virtual void DataReceived(BUrlRequest* request, const char* data,
25+
off_t offset, ssize_t size)
26+
{
27+
fResult.Write(data, size);
28+
}
29+
30+
BMallocIO& IO() { return fResult; }
31+
32+
private:
33+
BMallocIO fResult;
34+
};
35+
36+
37+
BBitmap* FetchImage(const BUrl& url);
38+
39+
40+
#endif // UTILITY_H

covers.cpp

+25-134
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66

7+
#include "covers.h"
8+
79
#include <Application.h>
810
#include <Bitmap.h>
911
#include <DataIO.h>
@@ -14,163 +16,52 @@
1416
#include <stdio.h>
1517

1618
#include "CoverWindow.h"
19+
#include "Utility.h"
1720

1821

19-
#define MAX_GROUPS 10
20-
#define MAX_MATCHES 10
21-
22-
#define COUNTRY_GERMANY 3
23-
24-
#define TYPE_ORIGINAL "SCRM"
25-
#define TYPE_LARGE "SCL"
26-
#define TYPE_THUMB "THUMB"
27-
28-
29-
class DataListener : public BUrlProtocolListener {
22+
class CoversApplication : public BApplication {
3023
public:
31-
DataListener()
32-
{
33-
}
24+
CoversApplication();
25+
virtual ~CoversApplication();
3426

35-
virtual ~DataListener()
36-
{
37-
}
38-
39-
virtual void DataReceived(BUrlRequest* request, const char* data,
40-
off_t offset, ssize_t size)
41-
{
42-
fResult.Write(data, size);
43-
}
44-
45-
BMallocIO& IO() { return fResult; }
46-
47-
private:
48-
BMallocIO fResult;
27+
virtual void MessageReceived(BMessage* message);
4928
};
5029

5130

52-
BBitmap*
53-
loadImage(int countryCode, const BString& id, const char* type)
31+
CoversApplication::CoversApplication()
32+
:
33+
BApplication("application/x-vnd.pinc-covers")
5434
{
55-
BString urlString;
56-
urlString.SetToFormat("http://ecx.images-amazon.com/images/P/%s.%02d._%s_",
57-
id.String(), countryCode, type);
58-
59-
BUrl url(urlString.String());
60-
BHttpRequest request(url);
61-
62-
DataListener listener;
63-
request.SetListener(&listener);
64-
request.Run();
65-
while (request.IsRunning()) {
66-
snooze(50000);
67-
putchar('.');
68-
fflush(stdout);
69-
}
35+
}
7036

71-
const BHttpResult& result = dynamic_cast<const BHttpResult&>(
72-
request.Result());
73-
printf("Status: %" B_PRId32 " d - %s\n", result.StatusCode(),
74-
result.StatusText().String());
7537

76-
listener.IO().Seek(0, SEEK_SET);
77-
return BTranslationUtils::GetBitmap(&listener.IO());
38+
CoversApplication::~CoversApplication()
39+
{
7840
}
7941

8042

8143
void
82-
getImageFromAmazon(const char* country, int countryCode, const BString& artist,
83-
const BString& title)
44+
CoversApplication::MessageReceived(BMessage* message)
8445
{
85-
BString pattern("<a[^>]*title\\s*=\\s*\"([^\"]+)\"[^>]*"
86-
"href\\s*=\\s*\"([^\"]+?/dp/([^/]+)/)[^>]+><h2.*?>([^<]+)(</a>)?</span></div></div>");
87-
//");
88-
regex_t patternCompiled;
89-
regmatch_t groups[MAX_GROUPS];
90-
int patternResult = regcomp(&patternCompiled, pattern.String(),
91-
REG_EXTENDED | REG_ICASE);
92-
if (patternResult != 0) {
93-
printf("bad pattern: %d!\n", patternResult);
94-
char text[256];
95-
regerror(patternResult, &patternCompiled, text, sizeof(text));
96-
printf(" --> %s\n", text);
97-
return;
98-
}
99-
100-
BString urlString("http://www.amazon.%COUNTRY%/gp/search/"
101-
"?search-alias=popular&sort=relevancerank");
102-
if (!artist.IsEmpty())
103-
urlString += "&field-artist=%ARTIST%";
104-
if (!title.IsEmpty())
105-
urlString += "&field-title=%TITLE%";
106-
urlString.ReplaceAll("%COUNTRY%", country);
107-
urlString.ReplaceAll("%ARTIST%", BUrl::UrlEncode(artist));
108-
urlString.ReplaceAll("%TITLE%", BUrl::UrlEncode(title));
109-
printf("URL: %s\n", urlString.String());
110-
111-
BUrl url(urlString);
112-
BHttpRequest request(url);
113-
114-
DataListener listener;
115-
request.SetListener(&listener);
116-
request.Run();
117-
while (request.IsRunning()) {
118-
snooze(50000);
119-
putchar('.');
120-
fflush(stdout);
121-
}
122-
123-
const BHttpResult& result = dynamic_cast<const BHttpResult&>(
124-
request.Result());
125-
printf("Status: %" B_PRId32 " - %s\n", result.StatusCode(),
126-
result.StatusText().String());
127-
128-
BString data((const char*)listener.IO().Buffer(),
129-
listener.IO().BufferLength());
130-
printf("length: %d\n", (int)listener.IO().BufferLength());
131-
const char* cursor = data.String();
132-
int offset = 0;
133-
134-
for (int index = 0; index < MAX_MATCHES; index++) {
135-
if (regexec(&patternCompiled, cursor, MAX_GROUPS, groups, 0))
46+
switch (message->what) {
47+
case kMsgSelected:
48+
case kMsgSkip:
49+
message->PrintToStream();
50+
break;
51+
default:
52+
BApplication::MessageReceived(message);
13653
break;
137-
138-
BString id;
139-
140-
for (int groupIndex = 0; groupIndex < MAX_GROUPS; groupIndex++) {
141-
if (groups[groupIndex].rm_so == -1)
142-
break;
143-
if (groupIndex == 0)
144-
offset = groups[0].rm_eo;
145-
146-
int length = groups[groupIndex].rm_eo - groups[groupIndex].rm_so;
147-
BString match(cursor + groups[groupIndex].rm_so, length);
148-
if (groupIndex)
149-
printf("%d: match? %s\n", groupIndex, match.String());
150-
if (groupIndex == 3)
151-
id = match;
152-
}
153-
154-
if (!id.IsEmpty()) {
155-
BBitmap* bitmap = loadImage(COUNTRY_GERMANY, id, TYPE_ORIGINAL);
156-
if (bitmap != NULL)
157-
puts("got image!");
158-
else
159-
puts("nay!");
160-
}
161-
162-
printf("offset = %d\n", offset);
163-
cursor += offset;
16454
}
165-
regfree(&patternCompiled);
16655
}
16756

16857

58+
// #pragma mark -
59+
60+
16961
int
17062
main()
17163
{
172-
BApplication application("application/x-vnd.pinc-covers");
173-
// getImageFromAmazon("de", COUNTRY_GERMANY, "toundra", "");
64+
CoversApplication application;
17465

17566
BWindow* window = new CoverWindow();
17667
window->CenterOnScreen();

covers.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2019 pinc Software. All Rights Reserved.
3+
*/
4+
#ifndef COVERS_H
5+
#define COVERS_H
6+
7+
8+
#include <SupportDefs.h>
9+
10+
11+
static const uint32 kMsgSelected = 'sele';
12+
static const uint32 kMsgSkip = 'skip';
13+
14+
15+
#endif // COVERS_H

0 commit comments

Comments
 (0)