Permalink
Browse files

implement albumiterator: show all albums from an artist.

  • Loading branch information...
1 parent e554f20 commit 3714c752e2ec02390d88e6ee8db030eea18bc0c5 @vilhelmk committed Oct 25, 2011
Showing with 190 additions and 1 deletion.
  1. +164 −0 albumiterator.c
  2. +9 −0 artist.c
  3. +1 −1 config.m4
  4. +14 −0 php_spotify.h
  5. +2 −0 spotify.c
View
@@ -0,0 +1,164 @@
+/*
+Copyright (c) 2011 Vilhelm K. Vardøy, vilhelmkv@gmail.com
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "php_spotify.h"
+#include <zend_interfaces.h>
+
+zend_class_entry *spotifyalbumiterator_ce;
+
+void spotify_artistbrowse_complete(sp_artistbrowse *result, void *userdata) {
+
+}
+
+PHP_METHOD(SpotifyAlbumIterator, __construct)
+{
+ zval *thisptr = getThis(), *parent;
+ spotifyartist_object *artistobj;
+ int timeout = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &parent, spotifyartist_ce) == FAILURE) {
+ return;
+ }
+
+ zval *spotifyobject = GET_PROPERTY(spotifyartist_ce, parent, "spotify");
+ spotify_object *p = (spotify_object*)zend_object_store_get_object(spotifyobject TSRMLS_CC);
+
+ artistobj = (spotifyartist_object*)zend_object_store_get_object(parent TSRMLS_CC);
+
+ spotifyalbumiterator_object *obj = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ obj->session = p->session;
+ obj->artist = artistobj->artist;
+ obj->artistbrowse = sp_artistbrowse_create(p->session, artistobj->artist, spotify_artistbrowse_complete, obj);
+
+ while (!sp_artistbrowse_is_loaded(obj->artistbrowse)) {
+ sp_session_process_events(p->session, &timeout);
+ }
+
+ obj->position = 0;
+ obj->length = sp_artistbrowse_num_albums(obj->artistbrowse);
+
+ zend_update_property(spotifyalbumiterator_ce, getThis(), "spotify", strlen("spotify"), spotifyobject TSRMLS_CC);
+
+ sp_artistbrowse_add_ref(obj->artistbrowse);
+}
+
+PHP_METHOD(SpotifyAlbumIterator, __destruct)
+{
+ spotifyalbumiterator_object *p = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ sp_artistbrowse_release(p->artistbrowse);
+}
+
+PHP_METHOD(SpotifyAlbumIterator, current)
+{
+ spotifyalbumiterator_object *p;
+ sp_album *album;
+ zval temp, *spotifyobject;
+
+ p = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ album = sp_artistbrowse_album(p->artistbrowse, p->position);
+
+ spotifyobject = GET_THIS_PROPERTY(spotifyalbumiterator_ce, "spotify");
+
+ object_init_ex(return_value, spotifyalbum_ce);
+ SPOTIFY_METHOD2(SpotifyAlbum, __construct, &temp, return_value, spotifyobject, album);
+}
+
+PHP_METHOD(SpotifyAlbumIterator, key)
+{
+ spotifyalbumiterator_object *p = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ RETURN_LONG(p->position);
+}
+
+PHP_METHOD(SpotifyAlbumIterator, next)
+{
+ spotifyalbumiterator_object *p = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ p->position++;
+}
+
+PHP_METHOD(SpotifyAlbumIterator, rewind)
+{
+ spotifyalbumiterator_object *p = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ p->position = 0;
+}
+
+PHP_METHOD(SpotifyAlbumIterator, valid)
+{
+ spotifyalbumiterator_object *p = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ if (p->position >= p->length) {
+ RETURN_FALSE;
+ } else {
+ RETURN_TRUE;
+ }
+}
+
+function_entry spotifyalbumiterator_methods[] = {
+ PHP_ME(SpotifyAlbumIterator, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
+ PHP_ME(SpotifyAlbumIterator, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
+ PHP_ME(SpotifyAlbumIterator, current, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(SpotifyAlbumIterator, key, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(SpotifyAlbumIterator, next, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(SpotifyAlbumIterator, rewind, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(SpotifyAlbumIterator, valid, NULL, ZEND_ACC_PUBLIC)
+ {NULL, NULL, NULL}
+};
+
+void spotifyalbumiterator_free_storage(void *object TSRMLS_DC)
+{
+ spotifyalbumiterator_object *obj = (spotifyalbumiterator_object*)object;
+ zend_hash_destroy(obj->std.properties);
+ FREE_HASHTABLE(obj->std.properties);
+ efree(obj);
+}
+
+zend_object_value spotifyalbumiterator_create_handler(zend_class_entry *type TSRMLS_DC)
+{
+ zval *tmp;
+ zend_object_value retval;
+
+ spotifyalbumiterator_object *obj = (spotifyalbumiterator_object *)emalloc(sizeof(spotifyalbumiterator_object));
+ memset(obj, 0, sizeof(spotifyalbumiterator_object));
+
+ zend_object_std_init(&obj->std, type TSRMLS_CC);
+ zend_hash_copy(obj->std.properties, &type->default_properties,
+ (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
+
+ retval.handle = zend_objects_store_put(obj, NULL,
+ spotifyalbumiterator_free_storage, NULL TSRMLS_CC);
+ retval.handlers = &spotify_object_handlers;
+
+ return retval;
+}
+
+void spotify_init_albumiterator(TSRMLS_D)
+{
+ zend_class_entry ce;
+ INIT_CLASS_ENTRY(ce, "SpotifyAlbumIterator", spotifyalbumiterator_methods);
+ spotifyalbumiterator_ce = zend_register_internal_class(&ce TSRMLS_CC);
+ spotifyalbumiterator_ce->create_object = spotifyalbumiterator_create_handler;
+
+ zend_class_implements(spotifyalbumiterator_ce TSRMLS_CC, 1, zend_ce_iterator);
+
+ zend_declare_property_null(spotifyalbumiterator_ce, "spotify", strlen("spotify"), ZEND_ACC_PROTECTED TSRMLS_CC);
+}
View
@@ -74,6 +74,14 @@ PHP_METHOD(SpotifyArtist, getURI)
RETURN_STRING(uri, 1);
}
+PHP_METHOD(SpotifyArtist, getAlbums)
+{
+ zval tempretval;
+
+ object_init_ex(return_value, spotifyalbumiterator_ce);
+ SPOTIFY_METHOD1(SpotifyAlbumIterator, __construct, &tempretval, return_value, getThis());
+}
+
PHP_METHOD(SpotifyArtist, __toString)
{
spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
@@ -85,6 +93,7 @@ function_entry spotifyartist_methods[] = {
PHP_ME(SpotifyArtist, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_ME(SpotifyArtist, getName, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyArtist, getURI, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(SpotifyArtist, getAlbums, NULL, ZEND_ACC_PUBLIC)
PHP_ME(SpotifyArtist, __toString, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
View
@@ -4,5 +4,5 @@ if test "$PHP_SPOTIFY" = "yes"; then
AC_DEFINE(HAVE_SPOTIFY, 1, [Whether you have Spotify])
LDFLAGS="$LDFLAGS -lspotify"
PHP_ADD_LIBRARY_WITH_PATH(spotify, "", SPOTIFY_SHARED_PATH)
- PHP_NEW_EXTENSION(spotify, spotify.c playlist.c track.c artist.c album.c user.c, $ext_shared)
+ PHP_NEW_EXTENSION(spotify, spotify.c playlist.c track.c artist.c album.c user.c albumiterator.c, $ext_shared)
fi
View
@@ -78,6 +78,15 @@ typedef struct {
typedef struct {
zend_object std;
sp_session *session;
+ sp_artist *artist;
+ sp_artistbrowse *artistbrowse;
+ int length;
+ int position;
+} spotifyalbumiterator_object;
+
+typedef struct {
+ zend_object std;
+ sp_session *session;
sp_user *user;
} spotifyuser_object;
@@ -100,6 +109,8 @@ extern zend_class_entry *spotifyartist_ce;
extern zend_class_entry *spotifyalbum_ce;
extern zend_class_entry *spotifyuser_ce;
+extern zend_class_entry *spotifyalbumiterator_ce;
+
extern void get_playlistcontainer_playlists(zval *return_value, container_browse_data *p, sp_playlistcontainer *pc);
#define NOISY 0
@@ -135,6 +146,9 @@ extern void get_playlistcontainer_playlists(zval *return_value, container_browse
#define GET_THIS_PROPERTY(ce, name) \
zend_read_property(ce, getThis(), name, strlen(name), NOISY TSRMLS_CC);
+#define GET_PROPERTY(ce, thisptr, name) \
+ zend_read_property(ce, thisptr, name, strlen(name), NOISY TSRMLS_CC);
+
/* push parameters, call function, pop parameters */
#define SPOTIFY_METHOD(classname, name, retval, thisptr) \
SPOTIFY_METHOD_BASE(classname, name)(0, retval, NULL, thisptr, 0 TSRMLS_CC);
View
@@ -417,6 +417,8 @@ PHP_MINIT_FUNCTION(spotify)
spotify_init_album(TSRMLS_C);
spotify_init_user(TSRMLS_C);
+ spotify_init_albumiterator(TSRMLS_C);
+
return SUCCESS;
}

0 comments on commit 3714c75

Please sign in to comment.