-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMacAuthTest.cpp
More file actions
233 lines (193 loc) · 4.49 KB
/
Copy pathMacAuthTest.cpp
File metadata and controls
233 lines (193 loc) · 4.49 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "MacAuth.h"
#include "MacAuthTest.h"
#include "Keys.h"
#include <gason/gason.hpp>
#include <stdio.h>
#include <vector>
#include "MacAuthUtils.h"
MacWifiLib _wifiLib;
MacAuth _macAuth(&_wifiLib);
DialogPtr _window;
using namespace gason;
using namespace std;
int main()
{
InitToolBox();
ShowMainWindow();
EventLoop();
}
void InitToolBox()
{
InitGraf(&qd.thePort);
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(NULL);
InitCursor();
AEInstallEventHandler(
kCoreEventClass,
kAEAnswer,
(AEEventHandlerUPP)ProcessResponseEvent,
0L,
false);
}
void ShowMainWindow()
{
_window = GetNewDialog(128, 0, (WindowPtr)-1);
MacSetPort(_window);
UpdateDialog(_window, _window->visRgn);
}
void EventLoop()
{
EventRecord event;
while (_run)
{
if (WaitNextEvent(everyEvent, &event, 0, NULL))
{
WindowPtr windowPtr;
FindWindow(event.where, &windowPtr);
switch (event.what)
{
case kHighLevelEvent:
AEProcessAppleEvent(&event);
break;
case mouseDown:
if (windowPtr == _window)
{
HandleMouseDown(&event);
}
break;
case updateEvt:
HandleUpdate(&event);
break;
}
}
}
}
void HandleMouseDown(EventRecord *eventPtr)
{
WindowPtr window;
short int part;
part = FindWindow(eventPtr->where, &window);
switch (part)
{
case inContent:
if (window == FrontWindow())
HandleInContent(eventPtr);
break;
case inDrag:
if (window == FrontWindow())
DragWindow(window, eventPtr->where, &qd.screenBits.bounds);
break;
case inGoAway:
if (TrackGoAway(window, eventPtr->where))
{
_run = false;
}
break;
}
}
void HandleInContent(EventRecord *eventPtr)
{
short int item;
DialogRef dialogRef;
if (DialogSelect(eventPtr, &dialogRef, &item))
{
switch (item)
{
case 1:
{
AuthRequest authRequest;
authRequest.ClientId = Keys::MacAuthClientId;
authRequest.Provider = "spotify";
authRequest.Params.insert(pair<string, string>("client_id", Keys::SpotifyClientId));
authRequest.Params.insert(pair<string, string>("response_type", "code"));
AuthResponse authResponse = _macAuth.Authenticate(authRequest);
if (authResponse.Success)
{
_wifiLib.Post(
"https://accounts.spotify.com/api/token",
"client_id=" + Keys::SpotifyClientId +
"&client_secret=" + Keys::SpotifyClientSecret +
"&grant_type=authorization_code&code=" + authResponse.Code +
"&redirect_uri=https://68k.io/login/callback",
TokenResponse);
}
}
}
}
}
void TokenResponse(MacWifiResponse& response)
{
if (response.Success)
{
JsonAllocator allocator;
JsonValue root;
JsonParseStatus status = jsonParse((char*)response.Content.c_str(), root, allocator);
if (status == JSON_PARSE_OK)
{
string accessToken = root("access_token").toString();
_wifiLib.SetAuthorization("Bearer " + accessToken);
_wifiLib.Get("https://api.spotify.com/v1/browse/new-releases?country=AU&limit=1", BrowseResponse);
}
}
}
void BrowseResponse(MacWifiResponse& response)
{
if (response.Success)
{
JsonAllocator allocator;
JsonValue root;
JsonParseStatus status = jsonParse((char*)response.Content.c_str(), root, allocator);
if (status == JSON_PARSE_OK)
{
JsonValue albums = root("albums");
JsonValue topAlbum = albums("items")[0];
JsonValue artist = topAlbum("artists")[0];
JsonValue image = topAlbum("images")[0];
_albumName = topAlbum("name").toString();
_artistName = artist("name").toString();
string imageUrl = image("url").toString();
_wifiLib.SetAuthorization("");
_wifiLib.Utf8ToMacRoman(false);
_wifiLib.Get(
"https://68k.io/image?ma_client_id=" + Keys::MacAuthClientId +
"&source_url=" + imageUrl +
"&dest_width=300&dest_height=300",
ImageResponse);
}
}
}
void ImageResponse(MacWifiResponse& response)
{
if (response.Success)
{
vector<char> v(response.Content.begin(), response.Content.end());
_image = &v[512]; // Skip 512-byte PICT1 header
DoUpdate();
}
}
void HandleUpdate(EventRecord *eventPtr)
{
WindowPtr windowPtr = (WindowPtr)eventPtr->message;
BeginUpdate(windowPtr);
DoUpdate();
EndUpdate(windowPtr);
}
void DoUpdate()
{
MacSetPort(_window);
if (_image > 0)
{
PicHandle imageHandle = (PicHandle)&_image;
Rect pictRect;
MacSetRect(&pictRect, 190, 10, 490, 310);
DrawPicture(imageHandle, &pictRect);
}
UpdateDialog(_window, _window->visRgn);
}
pascal OSErr ProcessResponseEvent(AppleEvent* appleEvent, AppleEvent* reply, long refCon)
{
_wifiLib.ProcessReply(appleEvent);
}