Ticket #12983: SelectionWindow.cpp

File SelectionWindow.cpp, 3.8 KB (added by khyati-agarwalss, 5 years ago)

I think the angle brackets are not a problem. The actual problem was with the backslash. I replaced it with %5C because it is being used on most of the sites on the internet. Please correct me if I'm wrong.

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