Skip to content
Newer
Older
100644 233 lines (184 sloc) 7.15 KB
012f7d4 @vilhelmk creds
authored May 20, 2011
1 /*
2 Copyright (c) 2011 Vilhelm K. Vardøy, vilhelmkv@gmail.com
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation
6 files (the "Software"), to deal in the Software without
7 restriction, including without limitation the rights to use,
8 copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following
11 conditions:
12
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 */
25
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
26 #include "php_spotify.h"
27
28 zend_class_entry *spotifytrack_ce;
29
30 PHP_METHOD(SpotifyTrack, __construct)
31 {
32 zval *object = getThis();
33 zval *parent;
34 sp_track *track;
35
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
36 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz", &parent, spotify_ce, &track) == FAILURE) {
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
37 return;
38 }
39
40 spotify_object *p = (spotify_object*)zend_object_store_get_object((parent) TSRMLS_CC);
41 spotifytrack_object *obj = (spotifytrack_object*)zend_object_store_get_object(object TSRMLS_CC);
42 obj->session = p->session;
f820737 @vilhelmk add support for adding a track to a playlist
authored May 17, 2011
43 obj->track = track;
b1c84c4 @vilhelmk add and release
authored May 20, 2011
44
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
45 zend_update_property(spotifytrack_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC);
46
b1c84c4 @vilhelmk add and release
authored May 20, 2011
47 sp_track_add_ref(obj->track);
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
48 }
49
50668c7 @vilhelmk more cleanup of memory
authored May 19, 2011
50 PHP_METHOD(SpotifyTrack, __destruct)
51 {
52 spotifytrack_object *obj = (spotifytrack_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
53 sp_track_release(obj->track);
54 }
55
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
56 PHP_METHOD(SpotifyTrack, getName)
57 {
58 zval *object = getThis();
59 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(object TSRMLS_CC);
60
61 RETURN_STRING(sp_track_name(p->track), 1);
62 }
63
f820737 @vilhelmk add support for adding a track to a playlist
authored May 17, 2011
64 PHP_METHOD(SpotifyTrack, getURI)
65 {
66 char uri[256];
67 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
68
69 // TODO add support for offset in the track link
70 sp_link *link = sp_link_create_from_track(p->track, 0);
71 sp_link_as_string(link, uri, 256);
b1c84c4 @vilhelmk add and release
authored May 20, 2011
72 sp_link_release(link);
f820737 @vilhelmk add support for adding a track to a playlist
authored May 17, 2011
73
74 RETURN_STRING(uri, 1);
75 }
76
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
77 PHP_METHOD(SpotifyTrack, getAlbum)
78 {
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
79 zval *object = getThis(), temp, *spotifyobject;
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
80 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(object TSRMLS_CC);
265fe34 @vilhelmk adding album support
authored May 17, 2011
81
82 sp_album *album = sp_track_album(p->track);
83 if (NULL == album) {
84 RETURN_FALSE;
85 }
86
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
87 spotifyobject = GET_THIS_PROPERTY(spotifytrack_ce, "spotify");
88
265fe34 @vilhelmk adding album support
authored May 17, 2011
89 object_init_ex(return_value, spotifyalbum_ce);
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
90 SPOTIFY_METHOD2(SpotifyAlbum, __construct, &temp, return_value, spotifyobject, album);
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
91 }
92
93 PHP_METHOD(SpotifyTrack, getArtist)
94 {
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
95 zval *object = getThis(), temp, *spotifyobject;
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
96 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(object TSRMLS_CC);
97
98 int num_artists = sp_track_num_artists(p->track);
99 if (0 == num_artists) {
100 RETURN_FALSE;
101 }
102
103 // FIXME add support for multiple artists
104
105 sp_artist *artist = sp_track_artist(p->track, 0);
106 if (NULL == artist) {
107 RETURN_FALSE;
108 }
109
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
110 spotifyobject = GET_THIS_PROPERTY(spotifytrack_ce, "spotify");
111
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
112 object_init_ex(return_value, spotifyartist_ce);
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
113 SPOTIFY_METHOD2(SpotifyArtist, __construct, &temp, return_value, spotifyobject, artist);
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
114 }
115
6668300 @vilhelmk SpotifyTrack::getDuration()
authored May 17, 2011
116 PHP_METHOD(SpotifyTrack, getDuration)
117 {
118 zval *object = getThis();
119 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(object TSRMLS_CC);
120
121 int duration_ms = sp_track_duration(p->track);
122 RETURN_DOUBLE((float)duration_ms / 1000.0f);
123 }
124
fbb032c @vilhelmk SpotifyTrack::getPopularity(), SpotifyTrack::isStarred(), SpotifyTrac…
authored May 17, 2011
125 PHP_METHOD(SpotifyTrack, getPopularity)
126 {
127 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
128 RETURN_LONG(sp_track_popularity(p->track));
129 }
130
2a8b2f1 @vilhelmk SpotifyTrack::getIndex()
authored May 17, 2011
131 PHP_METHOD(SpotifyTrack, getIndex)
132 {
133 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
134 RETURN_LONG(sp_track_index(p->track));
135 }
136
fbb032c @vilhelmk SpotifyTrack::getPopularity(), SpotifyTrack::isStarred(), SpotifyTrac…
authored May 17, 2011
137 PHP_METHOD(SpotifyTrack, isStarred)
138 {
139 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
140
141 if (sp_track_is_starred(p->session, p->track)) {
142 RETURN_TRUE;
143 } else {
144 RETURN_FALSE;
145 }
146 }
147
0318f07 @Hexxeh Add toplist support for tracks, artist and albums. Add track isAvaila…
Hexxeh authored Feb 20, 2012
148 PHP_METHOD(SpotifyTrack, isAvailable)
149 {
150 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
151
152 sp_track_availability availability = sp_track_get_availability(p->session, p->track);
153
154 if (availability == SP_TRACK_AVAILABILITY_AVAILABLE) {
155 RETURN_TRUE;
156 } else {
157 RETURN_FALSE;
158 }
159 }
160
fbb032c @vilhelmk SpotifyTrack::getPopularity(), SpotifyTrack::isStarred(), SpotifyTrac…
authored May 17, 2011
161 PHP_METHOD(SpotifyTrack, setStarred)
162 {
163 bool starred;
164 zval *object = getThis();
165 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(object TSRMLS_CC);
166
167 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &starred) == FAILURE) {
168 return;
169 }
170
171 sp_track_set_starred(p->session, &p->track, 1, starred);
172 }
173
e2bfc2d @vilhelmk user subsystem
authored May 17, 2011
174 PHP_METHOD(SpotifyTrack, __toString)
175 {
176 spotifytrack_object *p = (spotifytrack_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
177 RETURN_STRING(sp_track_name(p->track), 1);
178 }
179
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
180 function_entry spotifytrack_methods[] = {
e2bfc2d @vilhelmk user subsystem
authored May 17, 2011
181 PHP_ME(SpotifyTrack, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
50668c7 @vilhelmk more cleanup of memory
authored May 19, 2011
182 PHP_ME(SpotifyTrack, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
183 PHP_ME(SpotifyTrack, getName, NULL, ZEND_ACC_PUBLIC)
f820737 @vilhelmk add support for adding a track to a playlist
authored May 17, 2011
184 PHP_ME(SpotifyTrack, getURI, NULL, ZEND_ACC_PUBLIC)
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
185 PHP_ME(SpotifyTrack, getAlbum, NULL, ZEND_ACC_PUBLIC)
186 PHP_ME(SpotifyTrack, getArtist, NULL, ZEND_ACC_PUBLIC)
6668300 @vilhelmk SpotifyTrack::getDuration()
authored May 17, 2011
187 PHP_ME(SpotifyTrack, getDuration, NULL, ZEND_ACC_PUBLIC)
fbb032c @vilhelmk SpotifyTrack::getPopularity(), SpotifyTrack::isStarred(), SpotifyTrac…
authored May 17, 2011
188 PHP_ME(SpotifyTrack, getPopularity, NULL, ZEND_ACC_PUBLIC)
2a8b2f1 @vilhelmk SpotifyTrack::getIndex()
authored May 17, 2011
189 PHP_ME(SpotifyTrack, getIndex, NULL, ZEND_ACC_PUBLIC)
0318f07 @Hexxeh Add toplist support for tracks, artist and albums. Add track isAvaila…
Hexxeh authored Feb 19, 2012
190 PHP_ME(SpotifyTrack, isAvailable, NULL, ZEND_ACC_PUBLIC)
fbb032c @vilhelmk SpotifyTrack::getPopularity(), SpotifyTrack::isStarred(), SpotifyTrac…
authored May 17, 2011
191 PHP_ME(SpotifyTrack, isStarred, NULL, ZEND_ACC_PUBLIC)
192 PHP_ME(SpotifyTrack, setStarred, NULL, ZEND_ACC_PUBLIC)
e2bfc2d @vilhelmk user subsystem
authored May 17, 2011
193 PHP_ME(SpotifyTrack, __toString, NULL, ZEND_ACC_PUBLIC)
194 {NULL, NULL, NULL}
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
195 };
196
197 void spotifytrack_free_storage(void *object TSRMLS_DC)
198 {
199 spotifytrack_object *obj = (spotifytrack_object*)object;
200 zend_hash_destroy(obj->std.properties);
201 FREE_HASHTABLE(obj->std.properties);
202 efree(obj);
203 }
204
205 zend_object_value spotifytrack_create_handler(zend_class_entry *type TSRMLS_DC)
206 {
207 zval *tmp;
208 zend_object_value retval;
209
210 spotifytrack_object *obj = (spotifytrack_object *)emalloc(sizeof(spotifytrack_object));
211 memset(obj, 0, sizeof(spotifytrack_object));
212
213 zend_object_std_init(&obj->std, type TSRMLS_CC);
214 zend_hash_copy(obj->std.properties, &type->default_properties,
215 (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
216
217 retval.handle = zend_objects_store_put(obj, NULL,
218 spotifytrack_free_storage, NULL TSRMLS_CC);
219 retval.handlers = &spotify_object_handlers;
220
221 return retval;
222 }
223
224 void spotify_init_track(TSRMLS_D)
225 {
226 zend_class_entry ce;
227 INIT_CLASS_ENTRY(ce, "SpotifyTrack", spotifytrack_methods);
228 spotifytrack_ce = zend_register_internal_class(&ce TSRMLS_CC);
229 spotifytrack_ce->create_object = spotifytrack_create_handler;
ddad05e @vilhelmk keep the parent session class as a property in all classes so the gc …
authored Oct 25, 2011
230
231 zend_declare_property_null(spotifytrack_ce, "spotify", strlen("spotify"), ZEND_ACC_PROTECTED TSRMLS_CC);
307888b @vilhelmk more functions and classes, artists, tracks
authored May 17, 2011
232 }
Something went wrong with that request. Please try again.