PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/playlist.c

https://github.com/GazNicoll/libspotify-php
C | 322 lines | 230 code | 65 blank | 27 comment | 33 complexity | c46c2abcc94a942e8b170bda3f08607e MD5 | raw file
  1. /*
  2. Copyright (c) 2011 Vilhelm K. Vardøy, vilhelmkv@gmail.com
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "php_spotify.h"
  23. zend_class_entry *spotifyplaylist_ce;
  24. PHP_METHOD(SpotifyPlaylist, __construct)
  25. {
  26. zval *object = getThis();
  27. zval *parent;
  28. sp_playlist *playlist;
  29. int timeout = 0;
  30. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz", &parent, spotify_ce, &playlist) == FAILURE) {
  31. return;
  32. }
  33. spotify_object *p = (spotify_object*)zend_object_store_get_object((parent) TSRMLS_CC);
  34. spotifyplaylist_object *obj = (spotifyplaylist_object*)zend_object_store_get_object(object TSRMLS_CC);
  35. obj->session = p->session;
  36. obj->playlist = playlist;
  37. while (!sp_playlist_is_loaded(playlist)) {
  38. sp_session_process_events(p->session, &timeout);
  39. }
  40. sp_playlist_add_ref(obj->playlist);
  41. }
  42. PHP_METHOD(SpotifyPlaylist, __destruct)
  43. {
  44. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  45. sp_playlist_release(p->playlist);
  46. }
  47. PHP_METHOD(SpotifyPlaylist, getName)
  48. {
  49. zval *object = getThis();
  50. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(object TSRMLS_CC);
  51. RETURN_STRING(sp_playlist_name(p->playlist), 1);
  52. }
  53. PHP_METHOD(SpotifyPlaylist, getTracks)
  54. {
  55. int i, num_tracks, timeout = 0;
  56. zval *thisptr = getThis(), tempretval;
  57. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
  58. SPOTIFY_METHOD(SpotifyPlaylist, browse, &tempretval, thisptr);
  59. array_init(return_value);
  60. num_tracks = sp_playlist_num_tracks(p->playlist);
  61. for (i=0; i<num_tracks; i++) {
  62. sp_track *track = sp_playlist_track(p->playlist, i);
  63. while (!sp_track_is_loaded(track)) {
  64. sp_session_process_events(p->session, &timeout);
  65. }
  66. zval *z_track;
  67. ALLOC_INIT_ZVAL(z_track);
  68. object_init_ex(z_track, spotifytrack_ce);
  69. SPOTIFY_METHOD2(SpotifyTrack, __construct, &tempretval, z_track, thisptr, track);
  70. add_next_index_zval(return_value, z_track);
  71. }
  72. }
  73. PHP_METHOD(SpotifyPlaylist, getOwner)
  74. {
  75. sp_user *user;
  76. zval temp;
  77. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  78. user = sp_playlist_owner(p->playlist);
  79. object_init_ex(return_value, spotifyuser_ce);
  80. SPOTIFY_METHOD2(SpotifyUser, __construct, &temp, return_value, getThis(), user);
  81. }
  82. PHP_METHOD(SpotifyPlaylist, getDescription)
  83. {
  84. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  85. RETURN_STRING(sp_playlist_get_description(p->playlist), 1);
  86. }
  87. PHP_METHOD(SpotifyPlaylist, getNumTracks)
  88. {
  89. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  90. RETURN_LONG(sp_playlist_num_tracks(p->playlist));
  91. }
  92. PHP_METHOD(SpotifyPlaylist, getTrackCreateTime)
  93. {
  94. int index;
  95. zval *z_index;
  96. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z_index) == FAILURE) {
  97. return;
  98. }
  99. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  100. RETURN_LONG(sp_playlist_track_create_time(p->playlist, Z_LVAL_P(z_index)));
  101. }
  102. PHP_METHOD(SpotifyPlaylist, getTrackCreator)
  103. {
  104. int index;
  105. zval *thisptr = getThis(), tempretval;
  106. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &index) == FAILURE) {
  107. return;
  108. }
  109. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(thisptr TSRMLS_CC);
  110. sp_user *user = sp_playlist_track_creator(p->playlist, index);
  111. if (!user) {
  112. RETURN_FALSE;
  113. }
  114. object_init_ex(return_value, spotifyuser_ce);
  115. SPOTIFY_METHOD2(SpotifyUser, __construct, &tempretval, return_value, thisptr, user);
  116. }
  117. PHP_METHOD(SpotifyPlaylist, isCollaborative)
  118. {
  119. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  120. if (sp_playlist_is_collaborative(p->playlist)) {
  121. RETURN_TRUE;
  122. } else {
  123. RETURN_FALSE;
  124. }
  125. }
  126. PHP_METHOD(SpotifyPlaylist, setCollaborative)
  127. {
  128. bool collab;
  129. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  130. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &collab) == FAILURE) {
  131. return;
  132. }
  133. sp_playlist_set_collaborative(p->playlist, collab);
  134. }
  135. PHP_METHOD(SpotifyPlaylist, rename)
  136. {
  137. zval *object = getThis(), *z_name;
  138. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(object TSRMLS_CC);
  139. sp_error error;
  140. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z_name) == FAILURE) {
  141. return;
  142. }
  143. error = sp_playlist_rename(p->playlist, Z_STRVAL_P(z_name));
  144. if (SP_ERROR_OK != error) {
  145. RETURN_FALSE;
  146. }
  147. RETURN_TRUE;
  148. }
  149. PHP_METHOD(SpotifyPlaylist, addTrack)
  150. {
  151. zval *track, retval, *z_position, *object = getThis();
  152. sp_error error;
  153. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|z", &track, spotifytrack_ce, &z_position) == FAILURE) {
  154. return;
  155. }
  156. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(object TSRMLS_CC);
  157. sp_track **tracks = (sp_track**)emalloc(sizeof(sp_track*) * 1);
  158. spotifytrack_object *track_obj = (spotifytrack_object*)zend_object_store_get_object(track TSRMLS_CC);
  159. tracks[0] = track_obj->track;
  160. sp_track_add_ref(track_obj->track);
  161. int position;
  162. if (Z_TYPE_P(z_position) == IS_NULL || Z_LVAL_P(z_position) < 0) {
  163. position = sp_playlist_num_tracks(p->playlist);
  164. } else {
  165. position = Z_LVAL_P(z_position);
  166. }
  167. error = sp_playlist_add_tracks(p->playlist, tracks, 1, position, p->session);
  168. efree(tracks);
  169. if (SP_ERROR_OK == error) {
  170. RETURN_TRUE;
  171. } else {
  172. RETURN_FALSE;
  173. }
  174. }
  175. PHP_METHOD(SpotifyPlaylist, removeTrack)
  176. {
  177. zval *z_index;
  178. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &z_index) == FAILURE) {
  179. return;
  180. }
  181. int *tracks = (int*)emalloc(sizeof(int) * 1);
  182. tracks[0] = Z_LVAL_P(z_index);
  183. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  184. sp_error error = sp_playlist_remove_tracks(p->playlist, tracks, 1);
  185. efree(tracks);
  186. if (SP_ERROR_OK == error) {
  187. RETURN_TRUE;
  188. } else {
  189. RETURN_FALSE;
  190. }
  191. }
  192. PHP_METHOD(SpotifyPlaylist, browse)
  193. {
  194. int timeout = 0;
  195. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  196. while (!sp_playlist_is_loaded(p->playlist)) {
  197. sp_session_process_events(p->session, &timeout);
  198. }
  199. }
  200. PHP_METHOD(SpotifyPlaylist, __toString)
  201. {
  202. spotifyplaylist_object *p = (spotifyplaylist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  203. RETURN_STRING(sp_playlist_name(p->playlist), 1);
  204. }
  205. function_entry spotifyplaylist_methods[] = {
  206. PHP_ME(SpotifyPlaylist, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
  207. PHP_ME(SpotifyPlaylist, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
  208. PHP_ME(SpotifyPlaylist, getName, NULL, ZEND_ACC_PUBLIC)
  209. PHP_ME(SpotifyPlaylist, getTracks, NULL, ZEND_ACC_PUBLIC)
  210. PHP_ME(SpotifyPlaylist, getOwner, NULL, ZEND_ACC_PUBLIC)
  211. PHP_ME(SpotifyPlaylist, getDescription, NULL, ZEND_ACC_PUBLIC)
  212. PHP_ME(SpotifyPlaylist, getNumTracks, NULL, ZEND_ACC_PUBLIC)
  213. PHP_ME(SpotifyPlaylist, getTrackCreateTime, NULL, ZEND_ACC_PUBLIC)
  214. PHP_ME(SpotifyPlaylist, getTrackCreator, NULL, ZEND_ACC_PUBLIC)
  215. PHP_ME(SpotifyPlaylist, isCollaborative, NULL, ZEND_ACC_PUBLIC)
  216. PHP_ME(SpotifyPlaylist, setCollaborative, NULL, ZEND_ACC_PUBLIC)
  217. PHP_ME(SpotifyPlaylist, rename, NULL, ZEND_ACC_PUBLIC)
  218. PHP_ME(SpotifyPlaylist, addTrack, NULL, ZEND_ACC_PUBLIC)
  219. PHP_ME(SpotifyPlaylist, removeTrack, NULL, ZEND_ACC_PUBLIC)
  220. PHP_ME(SpotifyPlaylist, browse, NULL, ZEND_ACC_PRIVATE)
  221. PHP_ME(SpotifyPlaylist, __toString, NULL, ZEND_ACC_PUBLIC)
  222. {NULL, NULL, NULL}
  223. };
  224. void spotifyplaylist_free_storage(void *object TSRMLS_DC)
  225. {
  226. spotifyplaylist_object *obj = (spotifyplaylist_object*)object;
  227. zend_hash_destroy(obj->std.properties);
  228. FREE_HASHTABLE(obj->std.properties);
  229. efree(obj);
  230. }
  231. zend_object_value spotifyplaylist_create_handler(zend_class_entry *type TSRMLS_DC)
  232. {
  233. zval *tmp;
  234. zend_object_value retval;
  235. spotifyplaylist_object *obj = (spotifyplaylist_object *)emalloc(sizeof(spotifyplaylist_object));
  236. memset(obj, 0, sizeof(spotifyplaylist_object));
  237. // obj->std.ce = type;
  238. zend_object_std_init(&obj->std, type TSRMLS_CC);
  239. zend_hash_copy(obj->std.properties, &type->default_properties,
  240. (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
  241. retval.handle = zend_objects_store_put(obj, NULL,
  242. spotifyplaylist_free_storage, NULL TSRMLS_CC);
  243. retval.handlers = &spotify_object_handlers;
  244. return retval;
  245. }
  246. void spotify_init_playlist(TSRMLS_D)
  247. {
  248. zend_class_entry ce;
  249. INIT_CLASS_ENTRY(ce, "SpotifyPlaylist", spotifyplaylist_methods);
  250. spotifyplaylist_ce = zend_register_internal_class(&ce TSRMLS_CC);
  251. spotifyplaylist_ce->create_object = spotifyplaylist_create_handler;
  252. //memcpy(&spotify_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  253. //spotify_object_handlers.clone_obj = NULL;
  254. }