Ejemplo n.º 1
0
static PyObject *
AlbumBrowser_FromSpotify(sp_albumbrowse * browse)
{
    AlbumBrowser *b = (AlbumBrowser *)AlbumBrowserType.tp_alloc(&AlbumBrowserType, 0);

    b->_browser = browse;
    sp_albumbrowse_add_ref(browse);

    return b;
}
Ejemplo n.º 2
0
jobject createJAlbumInstance(JNIEnv *env, sp_album *album)
{
    jclass albumJClass;
    jobject albumInstance;

    albumJClass = (*env)->FindClass(env, "jahspotify/media/Album");
    if (albumJClass == NULL)
    {
        fprintf(stderr,"jahspotify::createJAlbumInstance: could not load jahnotify.media.Album\n");
        return NULL;
    }

    albumInstance = (*env)->AllocObject(env,albumJClass);
    if (!albumInstance)
    {
        fprintf(stderr,"jahspotify::createJAlbumInstance: could not create instance of jahspotify.media.Album\n");
        return NULL;
    }

    sp_albumbrowse *albumBrowse = sp_albumbrowse_create(g_sess,album,albumBrowseCompleteCallback,NULL);

    if (albumBrowse)
    {
      sp_albumbrowse_add_ref(albumBrowse);
      
      int count = 0;
      
      while (!sp_albumbrowse_is_loaded(albumBrowse) && count < 5)
      {
	fprintf(stderr,"jahspotify::createJAlbumInstance: waiting for album browse load to complete\n");
	sleep(1);
	count++;
      }
      
      if (count == 5)
      {
	sp_albumbrowse_release(albumBrowse);
	return NULL;
      }
  
  
      // By now it looks like the album will also be loaded 
      sp_link *albumLink = sp_link_create_from_album(album);

      if (albumLink)
      {
	  sp_link_add_ref(albumLink);

	  jobject albumJLink = createJLinkInstance(env, albumLink);
	  setObjectObjectField(env,albumInstance,"id","Ljahspotify/media/Link;",albumJLink);

	  setObjectStringField(env,albumInstance,"name",sp_album_name(album));
	  setObjectIntField(env,albumInstance,"year",sp_album_year(album));

	  sp_albumtype albumType = sp_album_type(album);

	  jclass albumTypeJClass = (*env)->FindClass(env, "jahspotify/media/AlbumType");
	  jmethodID jMethod = (*env)->GetStaticMethodID(env,albumTypeJClass,"fromOrdinal","(I)Ljahspotify/media/AlbumType;");
	  jobject albumTypeEnum = (jobjectArray)(*env)->CallStaticObjectMethod(env, albumTypeJClass, jMethod,(int)albumType);
	  setObjectObjectField(env,albumInstance,"type","Ljahspotify/media/AlbumType;",albumTypeEnum);

	  sp_link *albumCoverLink = sp_link_create_from_album_cover(album);
	  if (albumCoverLink)
	  {
	      sp_link_add_ref(albumCoverLink);

	      jobject albumCoverJLink = createJLinkInstance(env, albumCoverLink);
	      setObjectObjectField(env,albumInstance,"cover","Ljahspotify/media/Link;",albumCoverJLink);

	      sp_image *albumCoverImage = sp_image_create_from_link(g_sess,albumCoverLink);
	      if (albumCoverImage)
	      {
		sp_image_add_ref(albumCoverImage);
		sp_image_add_load_callback(albumCoverImage,imageLoadedCallback,NULL);
	      }
	      
	      sp_link_release(albumCoverLink);

	  }

	  sp_artist *artist = sp_album_artist(album);
	  if (artist)
	  {
	      sp_artist_add_ref(artist);

	      sp_link *artistLink = sp_link_create_from_artist(artist);
	      
	      if (artistLink)
	      {
		sp_link_add_ref(artistLink);
		
	        jobject artistJLink = createJLinkInstance(env,artistLink);
		
		setObjectObjectField(env,albumInstance,"artist","Ljahspotify/media/Link;",artistJLink);

		sp_link_release(artistLink);
	      }

	      sp_artist_release(artist);
	  }
	  
	  sp_link_release(albumLink);
      }
      
      int numTracks = sp_albumbrowse_num_tracks(albumBrowse);
      if (numTracks > 0)
      {
	// Add each track to the album - also pass in the disk as need be
	jmethodID addTrackJMethodID = (*env)->GetMethodID(env,albumJClass,"addTrack","(ILjahspotify/media/Link;)V");
 	int i = 0;
	for (i = 0; i < numTracks; i++)
	{
	  sp_track *track = sp_albumbrowse_track(albumBrowse,i);
	  
	  if (track)
	  {
	    sp_track_add_ref(track);
	  
	    sp_link *trackLink = sp_link_create_from_track(track,0);
	    if (trackLink)
	    {
	      sp_link_add_ref(trackLink);
	      jobject trackJLink = createJLinkInstance(env,trackLink);
	      (*env)->CallVoidMethod(env, albumInstance, addTrackJMethodID,sp_track_disc(track),trackJLink);
	      sp_link_release(trackLink);
	    }
	  }
	}
      }
      
      int numCopyrights = sp_albumbrowse_num_copyrights(albumBrowse);
      if (numCopyrights > 0)
      {
	// Add copyrights to album
	jmethodID addCopyrightMethodID = (*env)->GetMethodID(env,albumJClass,"addCopyright","(Ljava/lang/String;)V");
	int i = 0;
	for (i = 0; i < numCopyrights; i++)
	{
	  const char *copyright = sp_albumbrowse_copyright(albumBrowse,i);
	  if (copyright)
	  {
	    jstring str = (*env)->NewStringUTF(env, copyright);
	    (*env)->CallVoidMethod(env, albumInstance, addCopyrightMethodID,str);
	  }
	}
      }

      const char *review = sp_albumbrowse_review(albumBrowse);
      if (review)
      {
        setObjectStringField(env,albumInstance,"review",review);
      }
      sp_albumbrowse_release(albumBrowse);
    }
    return albumInstance;

}