forked from HaikuArchives/Weather
/
SelectionWindow.cpp
164 lines (134 loc) · 3.77 KB
/
SelectionWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2015 Przemysław Buczkowski <przemub@przemub.pl>
* Copyright 2014 George White
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include <Button.h>
#include <Catalog.h>
#include <ControlLook.h>
#include <GroupLayout.h>
#include <GroupView.h>
#include <ListView.h>
#include <StringView.h>
#include <Url.h>
#include <UrlProtocolRoster.h>
#include <UrlRequest.h>
#include <Window.h>
#include "CitiesListSelectionWindow.h"
#include "MainWindow.h"
#include "WSOpenMeteo.h"
#include "SelectionWindow.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "SelectionWindow"
SelectionWindow::SelectionWindow(BRect rect, MainWindow* parent, BString city, int32 cityId):
BWindow(rect, B_TRANSLATE("Change location"), B_TITLED_WINDOW,
B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS | B_CLOSE_ON_ESCAPE
| B_AUTO_UPDATE_SIZE_LIMITS),
fDownloadThread(-1)
{
fParent = parent;
fCity = city;
fCityId = cityId;
BGroupLayout *root = new BGroupLayout(B_VERTICAL);
this->SetLayout(root);
BGroupView *view = new BGroupView(B_HORIZONTAL);
BGroupLayout *layout = view->GroupLayout();
static const float spacing = be_control_look->DefaultItemSpacing();
layout->SetInsets(spacing / 2);
this->AddChild(view);
fCityControl = new BTextControl(NULL, B_TRANSLATE("City:"), fCity, NULL);
fCityControl->SetToolTip(B_TRANSLATE("Select city: city, country, region"));
layout->AddView(fCityControl);
BButton* button;
layout->AddView(button = new BButton("search", B_TRANSLATE("OK"),
new BMessage(kSearchMessage)));
fCityControl->MakeFocus(true);
button->MakeDefault(true);
}
void
SelectionWindow::MessageReceived(BMessage *msg)
{
switch (msg->what) {
case kSearchMessage:
_StartSearch();
Hide();
break;
case kCitiesListMessage: {
BRect frame(Frame().LeftTop(),BSize(400,200));
frame.OffsetBy(30,30);
(new CitiesListSelectionWindow(frame, fParent, msg))->Show();
break;
}
case kDataMessage:
msg->FindInt32("id", &fCityId);
_UpdateCity();
QuitRequested();
Close();
break;
case kFailureMessage:
// TODO add a message to the window
Show();
break;
default:
BWindow::MessageReceived(msg);
}
}
bool
SelectionWindow::QuitRequested()
{
BMessenger messenger(fParent);
BMessage* message = new BMessage(kCloseCitySelectionWindowMessage);
messenger.SendMessage(message);
return true;
}
void
SelectionWindow::_UpdateCity()
{
BMessenger messenger(fParent);
BMessage* message = new BMessage(kUpdateCityMessage);
message->AddString("city", fCityControl->Text());
message->AddInt32("id", fCityId);
messenger.SendMessage(message);
}
void
SelectionWindow::_StartSearch()
{
_StopSearch();
fDownloadThread = spawn_thread(&_FindIdFunc,
"Download Data", B_NORMAL_PRIORITY, this);
if (fDownloadThread >= 0)
resume_thread(fDownloadThread);
}
void
SelectionWindow::_StopSearch()
{
if (fDownloadThread < 0)
return;
wait_for_thread(fDownloadThread, NULL);
fDownloadThread = -1;
}
int32
SelectionWindow::_FindIdFunc(void *cookie)
{
SelectionWindow* selectionWindow = static_cast<SelectionWindow*>(cookie);
selectionWindow->_FindId();
return 0;
}
void
SelectionWindow::_FindId()
{
BString urlString("https://geocoding-api.open-meteo.com/v1/search?name=");
urlString << fCityControl->Text();
// Filter out characters that trip up BUrl
urlString.ReplaceAll(" ", "+");
urlString.ReplaceAll("<", "");
urlString.ReplaceAll(">", "");
urlString.ReplaceAll("\"", "");
WSOpenMeteo listener(this, CITY_REQUEST);
BUrlRequest* request =
BUrlProtocolRoster::MakeRequest(BUrl(urlString.String()),
&listener);
thread_id thread = request->Run();
wait_for_thread(thread, NULL);
delete request;
}