Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sp_albumbrowse added to Album #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion spotify/libspotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ void SP_CALLCONV cb_toplistbrowse_complete(sp_toplistbrowse *toplist, void *user
go_toplistbrowse_complete(toplist, userdata);
}

sp_albumbrowse* albumbrowse_create(sp_session *session, sp_album *album, void *userdata)
{
return sp_albumbrowse_create(
session, album, cb_albumbrowse_complete, userdata);
}

void SP_CALLCONV cb_albumbrowse_complete(sp_albumbrowse *result, void *userdata)
{
go_albumbrowse_complete(result, userdata);
}

void set_playlistcontainer_callbacks(sp_playlistcontainer_callbacks *callbacks)
{
callbacks->playlist_added = cb_playlistcontainer_playlist_added;
Expand Down Expand Up @@ -210,4 +221,4 @@ void set_image_callback(sp_image *image, void *userdata)
void SP_CALLCONV cb_image_complete(sp_image *image, void *userdata)
{
go_image_complete(image, userdata);
}
}
68 changes: 64 additions & 4 deletions spotify/libspotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,12 @@ func go_toplistbrowse_complete(sp_toplistsearch unsafe.Pointer, userdata unsafe.
t.cbComplete()
}

//export go_albumbrowse_complete
func go_albumbrowse_complete(sp_albumbrowse unsafe.Pointer, userdata unsafe.Pointer) {
a := (*Album)(userdata)
a.cbAlbumbrowseComplete()
}

//export go_image_complete
func go_image_complete(spImage unsafe.Pointer, userdata unsafe.Pointer) {
i := (*Image)(userdata)
Expand Down Expand Up @@ -1569,7 +1575,9 @@ func (l *Link) Album() (*Album, error) {
if l.Type() != LinkTypeAlbum {
return nil, errors.New("spotify: link is not an album")
}
return newAlbum(l.session, C.sp_link_as_album(l.sp_link)), nil

album := newAlbum(l.session, C.sp_link_as_album(l.sp_link))
return album, nil
}

func (l *Link) Artist() (*Artist, error) {
Expand Down Expand Up @@ -1981,9 +1989,10 @@ const (
)

type Album struct {
session *Session
sp_album *C.sp_album
wg sync.WaitGroup
session *Session
sp_album *C.sp_album
wg sync.WaitGroup
sp_albumbrowse *C.sp_albumbrowse
}

type AlbumType C.sp_albumtype
Expand All @@ -2007,13 +2016,22 @@ func newAlbum(s *Session, sp_album *C.sp_album) *Album {
if s.listenForMetadataUpdates(album.isLoaded, album) {
album.wg.Add(1)
}

album.wg.Add(1)
album.sp_albumbrowse = C.albumbrowse_create(s.sp_session, sp_album, unsafe.Pointer(album))
C.sp_albumbrowse_add_ref(album.sp_albumbrowse)

return album
}

func (a *Album) release() {
if a.sp_album == nil {
panic("spotify: album object has no sp_album object")
}

C.sp_albumbrowse_release(a.sp_albumbrowse)
a.sp_albumbrowse = nil

C.sp_album_release(a.sp_album)
a.sp_album = nil
}
Expand All @@ -2024,6 +2042,10 @@ func (a *Album) cbUpdated() {
}
}

func (a *Album) cbAlbumbrowseComplete() {
a.wg.Done()
}

func (a *Album) Wait() {
a.wg.Wait()
if !a.isLoaded() {
Expand Down Expand Up @@ -2089,6 +2111,44 @@ func (a *Album) isLoaded() bool {
return C.sp_album_is_loaded(a.sp_album) == 1
}

// Error checks if browsing returned an error code.
func (a *Album) Error() error {
return spError(C.sp_albumbrowse_error(a.sp_albumbrowse))
}

// Copyrights returns the number of copyright strings.
func (a *Album) Copyrights() int {
return int(C.sp_albumbrowse_num_copyrights(a.sp_albumbrowse))
}

// Copyright returns a copyright string for index n.
func (a *Album) Copyright(n int) string {
if n < 0 || n > a.Copyrights() {
panic("spotify: albumbrowse copyright out of range")
}

return C.GoString(C.sp_albumbrowse_copyright(a.sp_albumbrowse, C.int(n)))
}

// Tracks returns the number of tracks.
func (a *Album) Tracks() int {
return int(C.sp_albumbrowse_num_tracks(a.sp_albumbrowse))
}

// Track returns a track for index n.
func (a *Album) Track(n int) *Track {
if n < 0 || n > a.Tracks() {
panic("spotify: albumbrowse track out of range")
}
sp_track := C.sp_albumbrowse_track(a.sp_albumbrowse, C.int(n))
return newTrack(a.session, sp_track)
}

// Review returns an album review.
func (a *Album) Review() string {
return C.GoString(C.sp_albumbrowse_review(a.sp_albumbrowse))
}

type Artist struct {
session *Session
sp_artist *C.sp_artist
Expand Down
3 changes: 3 additions & 0 deletions spotify/libspotify.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void SP_CALLCONV cb_search_complete(sp_search *search, void *userdata);
sp_toplistbrowse* toplistbrowse_create(sp_session *session, sp_toplisttype type, sp_toplistregion region, const char *username, void *userdata);
void SP_CALLCONV cb_toplistbrowse_complete(sp_toplistbrowse *toplist, void *userdata);

sp_albumbrowse* albumbrowse_create(sp_session *session, sp_album *album, void *userdata);
void SP_CALLCONV cb_albumbrowse_complete(sp_albumbrowse *result, void *userdata);

void set_playlistcontainer_callbacks(sp_playlistcontainer_callbacks*);
void SP_CALLCONV cb_playlistcontainer_playlist_added(sp_playlistcontainer *pc, sp_playlist *playlist, int position, void *userdata);
void SP_CALLCONV cb_playlistcontainer_loaded(sp_playlistcontainer *pc, void *userdata);
Expand Down