-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsearch.cc
More file actions
225 lines (182 loc) · 6.82 KB
/
search.cc
File metadata and controls
225 lines (182 loc) · 6.82 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
/*
* =====================================================================================
*
* Filename: search.cc
*
* Description: bindings to the spotify search submodule
*
* Version: 1.0
* Created: 23/12/2012 16:59:00
* Revision: none
* Compiler: gcc
*
* Author: Florent Jaby (FJ), florent.jaby@gmail.com
* Company: Florent Jaby
*
* =====================================================================================
*/
#include "common.h"
using namespace v8;
using namespace nsp;
/**
* Spotify callback when a search query completed
*/
static void on_search_complete(sp_search* result, void* userdata) {
ObjectHandle<sp_search>* search = static_cast<ObjectHandle<sp_search>* >(userdata);
Handle<Value> cbv = search->object->Get(String::New("on_search_complete"));
if(!cbv->IsFunction()) {
return;
}
Handle<Function> cb = Local<Function>(Function::Cast(*cbv));
const unsigned int argc = 2;
sp_error error = sp_search_error(result);
Handle<Value> err = Null();
if(error != SP_ERROR_OK) {
err = Exception::Error(String::New(sp_error_message(error)));
}
Local<Value> argv[argc] = { Local<Value>::New(err), Local<Object>::New(search->object) };
cb->Call(Context::GetCurrent()->Global(), argc, argv);
return;
}
/**
* JS search_create implementation. calls sp_search_create
*/
static Handle<Value> Search_Create(const Arguments& args) {
HandleScope scope;
// check arguments sanity
assert(args.Length() == 10);
assert(args[0]->IsObject());
assert(args[1]->IsString());
assert(args[2]->IsNumber());
assert(args[3]->IsNumber());
assert(args[4]->IsNumber());
assert(args[5]->IsNumber());
assert(args[6]->IsNumber());
assert(args[7]->IsNumber());
assert(args[8]->IsNumber());
assert(args[9]->IsNumber());
// unwrap the session handle from the given object
ObjectHandle<sp_session>* session = ObjectHandle<sp_session>::Unwrap(args[0]);
// create the new handle for the sp_search we are creating
ObjectHandle<sp_search>* search = new ObjectHandle<sp_search>("sp_search");
String::Utf8Value query(args[1]);
// actually call sp_search_create
search->pointer = sp_search_create(
session->pointer,
*query,
args[2]->ToNumber()->Int32Value(),
args[3]->ToNumber()->Int32Value(),
args[4]->ToNumber()->Int32Value(),
args[5]->ToNumber()->Int32Value(),
args[6]->ToNumber()->Int32Value(),
args[7]->ToNumber()->Int32Value(),
args[8]->ToNumber()->Int32Value(),
args[9]->ToNumber()->Int32Value(),
SP_SEARCH_STANDARD,
&on_search_complete,
search
);
return scope.Close(search->object);
}
/**
* JS search_num_tracks implementation.
*/
static Handle<Value> Search_Num_Tracks(const Arguments& args) {
HandleScope scope;
assert(args.Length() == 1);
assert(args[0]->IsObject());
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int num = sp_search_num_tracks(search->pointer);
return scope.Close(Number::New(num));
}
/**
* JS search_num_albums implementation.
*/
static Handle<Value> Search_Num_Albums(const Arguments& args) {
HandleScope scope;
assert(args.Length() == 1);
assert(args[0]->IsObject());
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int num = sp_search_num_albums(search->pointer);
return scope.Close(Number::New(num));
}
/**
* JS search_num_artists implementation.
*/
static Handle<Value> Search_Num_Artists(const Arguments& args) {
HandleScope scope;
assert(args.Length() == 1);
assert(args[0]->IsObject());
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int num = sp_search_num_artists(search->pointer);
return scope.Close(Number::New(num));
}
/**
* JS search_track implementation. gets a track a the given index in a search result
*/
static Handle<Value> Search_Track(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 2);
assert(args[0]->IsObject());
assert(args[1]->IsNumber());
// gets sp_search pointer from given object
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int index = args[1]->ToNumber()->Int32Value();
// check index is within search results range
assert(index >= 0);
assert(index < sp_search_num_tracks(search->pointer));
// create new handle for this track
ObjectHandle<sp_track>* track = new ObjectHandle<sp_track>("sp_track");
track->pointer = sp_search_track(search->pointer, index);
return scope.Close(track->object);
}
/**
* JS search_album implementation. gets a album a the given index in a search result
*/
static Handle<Value> Search_Album(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 2);
assert(args[0]->IsObject());
assert(args[1]->IsNumber());
// gets sp_search pointer from given object
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int index = args[1]->ToNumber()->Int32Value();
// check index is within search results range
assert(index >= 0);
assert(index < sp_search_num_albums(search->pointer));
// create new handle for this album
ObjectHandle<sp_album>* album = new ObjectHandle<sp_album>("sp_album");
album->pointer = sp_search_album(search->pointer, index);
return scope.Close(album->object);
}
/**
* JS search_artist implementation. gets a artist a the given index in a search result
*/
static Handle<Value> Search_Artist(const Arguments& args) {
HandleScope scope;
// test arguments sanity
assert(args.Length() == 2);
assert(args[0]->IsObject());
assert(args[1]->IsNumber());
// gets sp_search pointer from given object
ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
int index = args[1]->ToNumber()->Int32Value();
// check index is within search results range
assert(index >= 0);
assert(index < sp_search_num_artists(search->pointer));
// create new handle for this artist
ObjectHandle<sp_artist>* artist = new ObjectHandle<sp_artist>("sp_artist");
artist->pointer = sp_search_artist(search->pointer, index);
return scope.Close(artist->object);
}
void nsp::init_search(Handle<Object> target) {
NODE_SET_METHOD(target, "search_create", Search_Create);
NODE_SET_METHOD(target, "search_num_tracks", Search_Num_Tracks);
NODE_SET_METHOD(target, "search_num_albums", Search_Num_Albums);
NODE_SET_METHOD(target, "search_num_artists", Search_Num_Artists);
NODE_SET_METHOD(target, "search_track", Search_Track);
NODE_SET_METHOD(target, "search_album", Search_Album);
NODE_SET_METHOD(target, "search_artist", Search_Artist);
}