Exemple #1
0
SxPlaylist::SxPlaylist(sp_playlist* spPlaylist, int index, bool isFolder) {
    m_spPlaylist = spPlaylist;
    m_isValid = true;
    m_isFolder = isFolder;
    m_index = index;
    m_thumb = NULL;

    if (!isFolder) {
        if (sp_playlist_is_loaded(spPlaylist)) reLoad();
        m_plCallbacks.description_changed = 0;
        m_plCallbacks.image_changed = 0;
        m_plCallbacks.playlist_metadata_updated = &cb_playlist_metadata_updated;
        m_plCallbacks.playlist_renamed = &cb_playlist_renamed;
        m_plCallbacks.playlist_state_changed = &cb_state_change;
        m_plCallbacks.playlist_update_in_progress = 0;
        m_plCallbacks.subscribers_changed = 0;
        m_plCallbacks.track_created_changed = 0;
        m_plCallbacks.track_message_changed = 0;
        m_plCallbacks.track_seen_changed = 0;
        m_plCallbacks.tracks_added = &cb_tracks_added;
        m_plCallbacks.tracks_moved = &cb_tracks_moved;
        m_plCallbacks.tracks_removed = &cb_tracks_removed;

        sp_playlist_add_callbacks(spPlaylist, &m_plCallbacks, this);
    }
}
Exemple #2
0
PHP_METHOD(Spotify, getPlaylistByURI)
{
	zval *uri, temp, *object = getThis();
	int timeout = 0;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &uri) == FAILURE) {
		return;
	}

	spotify_object *p = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC);

	sp_link *link = sp_link_create_from_string(Z_STRVAL_P(uri));
	if (NULL == link) {
		RETURN_FALSE;
	}

	if (SP_LINKTYPE_PLAYLIST != sp_link_type(link)) {
		RETURN_FALSE;
	}

	sp_playlist *playlist = sp_playlist_create(p->session, link);
	if (NULL == playlist) {
		RETURN_FALSE;
	}

	while (!sp_playlist_is_loaded(playlist)) {	
		sp_session_process_events(p->session, &timeout);
	}

	object_init_ex(return_value, spotifyplaylist_ce);
	SPOTIFY_METHOD2(SpotifyPlaylist, __construct, &temp, return_value, object, playlist);

	sp_link_release(link);
}
Exemple #3
0
static void playlist_browse_try(void)
{
	int i, tracks;

	metadata_updated_fn = playlist_browse_try;
	if(!sp_playlist_is_loaded(playlist_browse))
		return;

	tracks = sp_playlist_num_tracks(playlist_browse);
	for(i = 0; i < tracks; i++) {
		sp_track *t = sp_playlist_track(playlist_browse, i);
		if (!sp_track_is_loaded(t))
			return;
	}

	printf("\tPlaylist and metadata loaded\n");

	for(i = 0; i < tracks; i++) {
		sp_track *t = sp_playlist_track(playlist_browse, i);
		
		printf(" %5d: ", i + 1);
		print_track(t);
	}
	sp_playlist_remove_callbacks(playlist_browse, &pl_callbacks, NULL);

	sp_playlist_release(playlist_browse);
	playlist_browse = NULL;
	metadata_updated_fn = NULL;
	cmd_done();
}
Exemple #4
0
JNIEXPORT jobject JNICALL Java_jahspotify_impl_JahSpotifyImpl_retrievePlaylist ( JNIEnv *env, jobject obj, jstring uri)
{
    jobject playlistInstance;
    uint8_t *nativeUri = NULL;

    nativeUri = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, uri, NULL );

    sp_link *link = sp_link_create_from_string(nativeUri);
    if (!link)
    {
        // hmm
        fprintf ( stderr, "jahspotify::Java_jahspotify_impl_JahSpotifyImpl_retrievePlaylist: Could not create link!\n" );
        return JNI_FALSE;
    }

    sp_playlist *playlist = sp_playlist_create(g_sess,link);

    while (!sp_playlist_is_loaded(playlist))
    {
        fprintf ( stderr, "jahspotify::Java_jahspotify_impl_JahSpotifyImpl_retrievePlaylist: Waiting for playlist to be loaded ...\n" );
        sleep(1);
    }

    playlistInstance = createJPlaylist(env, playlist);

    if (playlist)
        sp_playlist_release(playlist);
    if (link)
        sp_link_release(link);
    if (nativeUri)
        free(nativeUri);

    return playlistInstance;

}
Exemple #5
0
static void get_playlist_collaborative(sp_playlist *playlist,
                                       struct evhttp_request *request,
                                       void *userdata) {
  assert(sp_playlist_is_loaded(playlist));
  json_t *json = json_object();
  playlist_to_json_set_collaborative(playlist, json);
  send_reply_json(request, HTTP_OK, "OK", json);
}
Exemple #6
0
static void get_playlist_subscribers(sp_playlist *playlist,
                                     struct evhttp_request *request,
                                     void *userdata) {
  assert(sp_playlist_is_loaded(playlist));
  sp_session *session = userdata;
  register_playlist_callbacks(playlist, request,
                              &get_playlist_subscribers_callback,
                              &playlist_subscribers_changed_callbacks,
                              userdata);
  sp_playlist_update_subscribers(session, playlist);
}
Exemple #7
0
static void handle_user_request(struct evhttp_request *request,
                                char *action,
                                const char *canonical_username,
                                sp_session *session) {
  if (action == NULL) {
    evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request");
    return;
  }

  int http_method = evhttp_request_get_command(request);

  switch (http_method) {
    case EVHTTP_REQ_GET:
      if (strncmp(action, "playlists", 9) == 0) {
        sp_playlistcontainer *pc = sp_session_publishedcontainer_for_user_create(
            session, canonical_username);

        if (sp_playlistcontainer_is_loaded(pc)) {
          get_user_playlists(pc, request, session);
        } else {
          register_playlistcontainer_callbacks(pc, request,
              &get_user_playlists,
              &playlistcontainer_loaded_callbacks,
              session);
        }
      } else if (strncmp(action, "starred", 7) == 0) {
        sp_playlist *playlist = sp_session_starred_for_user_create(session,
            canonical_username);

        if (sp_playlist_is_loaded(playlist)) {
          get_playlist(playlist, request, session);
        } else {
          register_playlist_callbacks(playlist, request, &get_playlist,
              &playlist_state_changed_callbacks,
              session);
        }
      }
      break;

    case EVHTTP_REQ_PUT:
    case EVHTTP_REQ_POST:
      if (strncmp(action, "inbox", 5) == 0) {
        put_user_inbox(canonical_username, request, session);
      }
      break;

    default:
      evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request");
      break;
  }
}
static void get_playlist_subscribers(sp_playlist *playlist,
                                     struct evhttp_request *request,
                                     void *userdata) {
  assert(sp_playlist_is_loaded(playlist));
  sp_subscribers *subscribers = sp_playlist_subscribers(playlist);
  json_t *array = json_array();

  for (int i = 0; i < subscribers->count; i++) {
    char *subscriber = subscribers->subscribers[i];
    json_array_append_new(array, json_string(subscriber));
  }

  send_reply_json(request, HTTP_OK, "OK", array);
}
void
SpotifySearch::addSearchedTrack( sp_search *result, void *userdata)
{
    qDebug() << Q_FUNC_INFO;
    sp_playlist *playlist = reinterpret_cast<sp_playlist*>(userdata);
    if(!sp_playlist_is_loaded( playlist ) )
    {
        qDebug() << "Search Playlist is not loaded";
        return;
    }

    // Need to pass a ** to add_tracks
    sp_track **tracks = static_cast<sp_track **>(malloc(sizeof(sp_track*)));

    if( sp_search_num_tracks( result ) > 0 )
    {
        int num = qMin( sp_search_num_tracks( result ), 1 );
        for( int i = 0; i < num; i++ )
        {
            sp_track *const tr = sp_search_track( result, i );
            if( !tr || !sp_track_is_loaded( tr ) ) {
                qDebug() << "Got still loading track, skipping";
                continue;
            }

            qDebug() << "Adding track to playlist" << sp_track_name( tr );
            *(tracks++) = tr;
            sp_error err = sp_playlist_add_tracks(playlist, tracks, 1, sp_playlist_num_tracks( playlist ), SpotifySession::getInstance()->Session());

            switch(err) {
                case SP_ERROR_OK:
                    qDebug() << "Added tracks to pos" << sp_playlist_num_tracks( playlist )-1;
                    break;
                case SP_ERROR_INVALID_INDATA:
                    qDebug() << "Invalid position";
                    break;

                case SP_ERROR_PERMISSION_DENIED:
                    qDebug() << "Access denied";
                    break;
                default:
                    qDebug() << "Other error (should not happen)";
                    break;
                }
        }

    }
    delete []tracks;

}
void SpotifyGuiController::AddPlaylist( sp_playlist *plist)
{
	if (plist )
	{
		//aggiungo
		if (sp_playlist_is_loaded(plist) && playlistTree)
		{
			TCHAR plist_name[1001] = { 0 };
			_sntprintf_s(plist_name, 1000, TEXT("%hs"), sp_playlist_name(plist));

			playlistTree->addItemAsLast(NULL, plist_name, plist, 0);

			//sp_playlist_add_ref(plist);
		}
	}
}
Exemple #11
0
bool XplodifyPlaylist::load(sp_playlist * pl) {
    if(!pl) {
        return false;
    }

    m_playlist = pl;
    sp_playlist_add_callbacks(pl, const_cast<sp_playlist_callbacks *>(&cbs), this);

    if(!sp_playlist_is_loaded(pl))
    {
        m_loading = true;
    } else {
        m_loading = false; //already loaded.
        load_tracks();
    }

    return true;
}
Exemple #12
0
static bool validate_complete_load() 
{
	if (g_list_container == NULL) {
		return false;
	}
	sp_playlistcontainer *list_container = g_list_container;	
	int pl_count = sp_playlistcontainer_num_playlists(list_container);
	int i=0;
	for(; i < pl_count; i++) {
		sp_playlist *l = sp_playlistcontainer_playlist(list_container, i);
		sp_playlist_type type = sp_playlistcontainer_playlist_type(list_container, i);
		if (type == SP_PLAYLIST_TYPE_PLAYLIST && !sp_playlist_is_loaded(l)) {
			return false;
		}
	}
	sp_playlist *source_list = NULL;
	i=0;
	for(; i < pl_count; i++) {
		sp_playlist *l = sp_playlistcontainer_playlist(list_container, i);
		const char* pname = sp_playlist_name(l);
		if (strcmp(pname, playlist_name) == 0) {
			source_list = l;
			break;
		}
	}
	
	if (source_list == NULL) {
		fprintf(stderr, "Cannot find source list: %s\n", playlist_name);
		exit(1);
	}
		
	int nt = sp_playlist_num_tracks(source_list);
	i=0;
	for(; i < nt; i++) {
		sp_track *t = sp_playlist_track(source_list, i);
		sp_artist *a = sp_track_artist(t,0); // get first artist on track because I am lazy
		sp_album *al = sp_track_album(t);
		if (al == NULL || a == NULL) {
			return false;
		}
	}
	return true;
}
Exemple #13
0
void search_for_tracks_at(sp_session *session, char *buf, size_t len, size_t i, bool (*f)(sp_track *))
{
	if(search == NULL)
	{
		return;
	}
	buf[0] = '\0';
	if(search_is_track(i) && sp_track_is_loaded(sp_search_track(search, i)))
	{
		f(sp_search_track(search, i));
		if(buf != NULL)
		{
			track_to_str(buf, len, sp_search_track(search, i));
		}
	}
	if(search_is_album(i) && sp_album_is_loaded(sp_search_album(search, i-sp_search_num_tracks(search))))
	{
		sp_albumbrowse_create(session, sp_search_album(search, i-sp_search_num_tracks(search))
				,on_albumbrowse_complete, f);
		if(buf != NULL)
		{
			album_to_str(buf, len, sp_search_album(search, i-sp_search_num_tracks(search)));
		}
	}
	if(search_is_playlist(i))
	{
		sp_playlist *pl = sp_search_playlist(search, 
					i-sp_search_num_tracks(search)-sp_search_num_albums(search));
		if(sp_playlist_is_loaded(pl))
		{
			int j;
			for(j = 0; j < sp_playlist_num_tracks(pl); ++j)
			{
				f(sp_playlist_track(pl, j));
			}
			if(buf != NULL)
			{
				playlist_to_str(buf, len, pl);
			}
		}
	}
}
static void get_user_playlists(sp_playlistcontainer *pc,
                               struct evhttp_request *request,
                               void *userdata) {
  json_t *json = json_object();
  json_t *playlists = json_array();
  json_object_set_new(json, "playlists", playlists);

  for (int i = 0; i < sp_playlistcontainer_num_playlists(pc); i++) {
    sp_playlist *playlist = sp_playlistcontainer_playlist(pc, i);

    if (!sp_playlist_is_loaded(playlist)) // TODO(liesen): Wait for it to load?
      continue;

    json_t *playlist_json = json_object();
    playlist_to_json(playlist, playlist_json);
    json_array_append_new(playlists, playlist_json);
  }

  send_reply_json(request, HTTP_OK, "OK", json);
}
Exemple #15
0
/*********************
 * Tracks management *
 *********************/
GArray* tracks_get_playlist(sp_playlist* pl) {
    GArray* tracks;
    sp_track* tr;
    int i, n;

    if (!sp_playlist_is_loaded(pl))
        return NULL;

    n = sp_playlist_num_tracks(pl);
    tracks = g_array_sized_new(FALSE, FALSE, sizeof(sp_track*), n);
    if (!tracks)
        g_error("Can't allocate array of %d tracks.", n);

    for (i=0; i < n; i++) {
        tr = sp_playlist_track(pl, i);
        sp_track_add_ref(tr);
        g_array_append_val(tracks, tr);
    }

    return tracks;
}
void Spotify::tryLoadPlaylist()
{
    if ((currentPlaylistIdx < 0) || (currentPlaylist == 0)) {
        return;
    }

    if (sp_playlist_is_loaded(currentPlaylist)) {
        QList<SpotifyTrackInfo> tracks;

        int numTracks = sp_playlist_num_tracks(currentPlaylist);
        for (int idx = 0; idx < numTracks; ++idx) {
            sp_track * track = sp_playlist_track(currentPlaylist, idx);
            if (!track) {
                fprintf(stderr, "Spotify: failed to get track #%d\n", idx + 1);
                continue;
            }

            QString name(sp_track_name(track));
            if (name.isEmpty()) {
                fprintf(stderr, "Spotify: got empty track name\n");
                continue;
            }

            SpotifyTrackInfo info;
            info.name = name;
            info.URI = uriFromTrack(track);
            if (info.URI.isEmpty()) {
                continue;
            }

            tracks.append(info);
        }

        emit currentPlaylistUpdated(tracks);

        fprintf(stderr, "Spotify: loaded playlist %s (%d tracks)\n",
                sp_playlist_name(currentPlaylist),
                tracks.size());
    }
}
Exemple #17
0
void SpotWorker::emitLoggedInSignal(sp_session *session, sp_error error)
{
    if (SP_ERROR_OK != error) {
        fprintf(stderr, "SpotWorker: Login failed: %s\n",
                sp_error_message(error));
    }
    else{
        printf("SpotWorker: Successfully logged in\n");

        emit loggedIn(session, error);

        sp_playlistcontainer *playlists = sp_session_playlistcontainer(session);
        void * userdata = NULL;
        sp_playlistcontainer_add_callbacks(playlists,
                                           &pc_callbacks,
                                           userdata);

        int listCount = sp_playlistcontainer_num_playlists(playlists);
        printf("%d playlists discovered\n", sp_playlistcontainer_num_playlists(playlists));

        if(listCount > 0)
            emit playlistAdded(playlists);

        for (int i = 0; i < listCount && i < 10; ++i) {
            sp_playlist *pl = sp_playlistcontainer_playlist(playlists, i);

            //TODO: register playback callback
            //sp_playlist_add_callbacks(pl, &pl_callbacks, NULL);

            if (sp_playlist_is_loaded(pl)){
                DEBUG printf("Playlist found: %s (%i tracks)\n",
                             sp_playlist_name(pl), sp_playlist_num_tracks(pl));
            }
        }
	DEBUG {
	    if (listCount > 10)
		printf("...and %d more.\n", listCount-10);
	}
    }
}
void SpotifyGuiController::RefreshPlaylistTracks ( sp_playlist *plist )
{
	if ( plist && plist != selected_playlist )
	{

		if ( trackTree ) trackTree->removeAllItem();

		SpotifyUserData *sp_data = (SpotifyUserData *)spotify_userdata;

		//ho la playlist, carico le tracce
		sp_playlist_add_callbacks(plist, sp_data->playlist_cb, this);

		if ( sp_playlist_is_loaded(plist) )
		{
			//aggiungo tracce
			int n_tracks = sp_playlist_num_tracks(plist);

			for ( int nt = 0 ; nt < n_tracks ; nt++ )
			{
				sp_track *track = sp_playlist_track(plist, nt);

				if ( track && sp_track_is_loaded(track) )
				{
					if ( trackTree )
					{
						TCHAR trackname[1001] = {0};
						memset(trackname, 0, sizeof(TCHAR)*1001);

						_sntprintf_s(trackname, 1000, TEXT("%hs"), sp_track_name(track));
						trackTree->addItemAsLast(NULL, trackname, track, 0);
					}
				}
			}
		}

	}

}
Exemple #19
0
static int apply_changes(sp_playlist *pl, struct pl_update_work *puw)
{
	sp_link *l;
	sp_error err;

	if(!sp_playlist_is_loaded(pl))
		return 1;


	l = sp_link_create_from_playlist(pl);
	if(l == NULL)
		return 1;
	sp_link_release(l);

	fprintf(stderr, "Playlist loaded, applying changes ... ");

	err = sp_playlist_add_tracks(pl, puw->tracks,
	    puw->num_tracks, puw->position, g_session);

	switch(err) {
	case SP_ERROR_OK:
		fprintf(stderr, "OK\n");
		break;
	case SP_ERROR_INVALID_INDATA:
		fprintf(stderr, "Invalid position\n");
		break;

	case SP_ERROR_PERMISSION_DENIED:
		fprintf(stderr, "Access denied\n");
		break;
	default:
		fprintf(stderr, "Other error (should not happen)\n");
		break;
	}
	return 0;
}
Exemple #20
0
// session->mutex must be locked
static int wait_for_playlist_loaded(php_spotify_playlist *playlist) {
	struct timespec ts;
	int err = 0;
	php_spotify_session *session;

	assert(playlist != NULL);
	session = playlist->session;

	DEBUG_PRINT("wait_for_playlist_loaded start\n");

	// Block for a max of SPOTIFY_TIMEOUT seconds
	clock_gettime(CLOCK_REALTIME, &ts);
	ts.tv_sec += SPOTIFY_TIMEOUT;

	request_lock();

	while(err == 0) {
		DEBUG_PRINT("wait_for_playlist_loaded loop\n");
		int loaded;

		session_lock(session);
		loaded = sp_playlist_is_loaded(playlist->playlist);
		session_unlock(session);

		if (loaded)
			break;

		// Wait until a callback is fired
		err = request_sleep_lock(&ts);
	}

	request_unlock();

	DEBUG_PRINT("wait_for_playlist_loaded end(%d)\n", err);
	return err;
}
Exemple #21
0
static void get_user_playlists(sp_playlistcontainer *pc,
                               struct evhttp_request *request,
                               void *userdata) {
  json_t *json = json_object();
  json_t *playlists = json_array();
  json_object_set_new(json, "playlists", playlists);
  int status = HTTP_OK;

  for (int i = 0; i < sp_playlistcontainer_num_playlists(pc); i++) {
    sp_playlist *playlist = sp_playlistcontainer_playlist(pc, i);

    if (!sp_playlist_is_loaded(playlist)) {
      status = HTTP_PARTIAL;
      continue;
    }

    json_t *playlist_json = json_object();
    playlist_to_json(playlist, playlist_json);
    json_array_append_new(playlists, playlist_json);
  }

  send_reply_json(request, status, status == HTTP_OK ? "OK" : "Partial Content",
                  json);
}
Exemple #22
0
PHP_METHOD(SpotifyPlaylist, __construct)
{
	zval *object = getThis();
	zval *parent;
	sp_playlist *playlist;
	int timeout = 0;
	
	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oz", &parent, spotify_ce, &playlist) == FAILURE) {
		return;
	}

	spotify_object *p = (spotify_object*)zend_object_store_get_object((parent) TSRMLS_CC);
	spotifyplaylist_object *obj = (spotifyplaylist_object*)zend_object_store_get_object(object TSRMLS_CC);
	obj->session = p->session;
	obj->playlist = playlist;

	zend_update_property(spotifyplaylist_ce, getThis(), "spotify", strlen("spotify"), parent TSRMLS_CC);

	while (!sp_playlist_is_loaded(playlist)) {
		sp_session_process_events(p->session, &timeout);
	}

	sp_playlist_add_ref(obj->playlist);
}
Exemple #23
0
void XplodifyPlaylist::playlist_state_changed(){

    boost::shared_ptr<XplodifySession> sess(m_session.lock());

    //Has the state changed cause we're done loading?
#ifdef _DEBUG
    std::cout << "Playlist state changed" << std::endl;
#endif
    if(sp_playlist_is_loaded(m_playlist))
    {
        if(m_loading) {
            m_loading = false;
            //we should now create shared_ptr and insert into cache.
            sess->get_pl_container()->add_playlist(this);
        }
        load_tracks();
#ifdef _DEBUG
        std::cout << "Playlist " << get_name() << " completed loading." << std::endl;
#endif
    }

    sess->update_state_ts();
    return;
}
Exemple #24
0
static void playlist_dispatch_if_loaded(sp_playlist *playlist, void *userdata) {
  if (sp_playlist_is_loaded(playlist))
    playlist_dispatch(playlist, userdata);
}
Exemple #25
0
// Request dispatcher
static void handle_request(struct evhttp_request *request,
                            void *userdata) {
  evhttp_connection_set_timeout(request->evcon, 1);
  evhttp_add_header(evhttp_request_get_output_headers(request),
                    "Server", "[email protected]/spotify-api-server");

  // Check request method
  int http_method = evhttp_request_get_command(request);

  switch (http_method) {
    case EVHTTP_REQ_GET:
    case EVHTTP_REQ_PUT:
    case EVHTTP_REQ_POST:
      break;

    default:
      evhttp_send_error(request, HTTP_NOTIMPL, "Not Implemented");
      return;
  }

  struct state *state = userdata;
  sp_session *session = state->session;
  char *uri = evhttp_decode_uri(evhttp_request_get_uri(request));

  char *entity = strtok(uri, "/");

  if (entity == NULL) {
    evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request");
    free(uri);
    return;
  }

  // Handle requests to /user/<user_name>/inbox
  if (strncmp(entity, "user", 4) == 0) {
    char *username = strtok(NULL, "/");

    if (username == NULL) {
      evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request");
      free(uri);
      return;
    }

    char *action = strtok(NULL, "/");
    handle_user_request(request, action, username, session);
    free(uri);
    return;
  }

  // Handle requests to /playlist/<playlist_uri>/<action>
  if (strncmp(entity, "playlist", 8) != 0) {
    evhttp_send_error(request, HTTP_BADREQUEST, "Bad Request");
    free(uri);
    return;
  }

  char *playlist_uri = strtok(NULL, "/");

  if (playlist_uri == NULL) {
    switch (http_method) {
      case EVHTTP_REQ_PUT:
      case EVHTTP_REQ_POST:
        put_playlist(NULL, request, session);
        break;

      default:
        send_error(request, HTTP_BADREQUEST, "Bad Request");
        break;
    }

    free(uri);
    return;
  }

  sp_link *playlist_link = sp_link_create_from_string(playlist_uri);

  if (playlist_link == NULL) {
    send_error(request, HTTP_NOTFOUND, "Playlist link not found");
    free(uri);
    return;
  }

  if (sp_link_type(playlist_link) != SP_LINKTYPE_PLAYLIST) {
    sp_link_release(playlist_link);
    send_error(request, HTTP_BADREQUEST, "Not a playlist link");
    free(uri);
    return;
  }

  sp_playlist *playlist = sp_playlist_create(session, playlist_link);
  sp_link_release(playlist_link);

  if (playlist == NULL) {
    send_error(request, HTTP_NOTFOUND, "Playlist not found");
    free(uri);
    return;
  }

  sp_playlist_add_ref(playlist);

  // Dispatch request
  char *action = strtok(NULL, "/");

  // Default request handler
  handle_playlist_fn request_callback = &not_implemented;
  void *callback_userdata = session;

  switch (http_method) {
  case EVHTTP_REQ_GET:
    {
      if (action == NULL) {
        // Send entire playlist
        request_callback = &get_playlist;
      } else if (strncmp(action, "collaborative", 13) == 0) {
        request_callback = &get_playlist_collaborative;
      } else if (strncmp(action, "subscribers", 11) == 0) {
        request_callback = &get_playlist_subscribers;
      }
    }
    break;

  case EVHTTP_REQ_PUT:
  case EVHTTP_REQ_POST:
    {
      if (strncmp(action, "add", 3) == 0) {
        request_callback = &put_playlist_add_tracks;
      } else if (strncmp(action, "remove", 6) == 0) {
        request_callback = &put_playlist_remove_tracks;
      } else if (strncmp(action, "patch", 5) == 0) {
        callback_userdata = state;
        request_callback = &put_playlist_patch;
      }
    }
    break;
  }

  if (sp_playlist_is_loaded(playlist)) {
    request_callback(playlist, request, callback_userdata);
  } else {
    // Wait for playlist to load
    register_playlist_callbacks(playlist, request, request_callback,
                                &playlist_state_changed_callbacks,
                                callback_userdata);
  }

  free(uri);
}
json_t *playlist_to_json(sp_playlist *playlist, json_t *object) {
  assert(sp_playlist_is_loaded(playlist));

  // Owner
  sp_user *owner = sp_playlist_owner(playlist);
  const char *username = sp_user_display_name(owner);
  sp_user_release(owner);
  json_object_set_new_nocheck(object, "creator",
                              json_string_nocheck(username));

  // URI
  size_t playlist_uri_len = strlen("spotify:user:"******":playlist:") +
                            strlen("284on3DVWeAxWkgVuzZKGt");
  char *playlist_uri = malloc(playlist_uri_len);

  if (playlist_uri == NULL) {
    return NULL;
  }

  sp_link *playlist_link = sp_link_create_from_playlist(playlist);
  sp_link_as_string(playlist_link, playlist_uri, playlist_uri_len);
  sp_link_release(playlist_link);
  json_object_set_new(object, "uri", 
                      json_string_nocheck(playlist_uri));
  free(playlist_uri);

  // Title
  const char *title = sp_playlist_name(playlist);
  json_object_set_new(object, "title",
                      json_string_nocheck(title));

  // Collaborative
  playlist_to_json_set_collaborative(playlist, object);

  // Description
  const char *description = sp_playlist_get_description(playlist);

  if (description != NULL) {
    json_object_set_new(object, "description",
                        json_string_nocheck(description));
  }

  // Number of subscribers
  int num_subscribers = sp_playlist_num_subscribers(playlist);
  json_object_set_new(object, "subscriberCount",
                      json_integer(num_subscribers));

  // Tracks
  json_t *tracks = json_array();
  json_object_set_new(object, "tracks", tracks);
  char track_uri[kTrackLinkLength];

  for (int i = 0; i < sp_playlist_num_tracks(playlist); i++) {
    sp_track *track = sp_playlist_track(playlist, i);
    sp_link *track_link = sp_link_create_from_track(track, 0);
    sp_link_as_string(track_link, track_uri, kTrackLinkLength);
    json_array_append(tracks, json_string_nocheck(track_uri));
    sp_link_release(track_link);
  }

  return object;
}
Exemple #27
0
void foo(sp_playlist *bar, void *baz) {
    printf("playlist is loaded? %d\n", sp_playlist_is_loaded(bar));
}
Exemple #28
0
static void playlist_state_changed(sp_playlist *playlist, void *userdata) {
  if (!sp_playlist_is_loaded(playlist))
    return;

  playlist_dispatch(playlist, userdata);
}
Exemple #29
0
/**
 * Move playlists
 */
int sort_playlists(sp_session *session)
{
	sp_playlistcontainer *pc = sp_session_playlistcontainer(session);
	sp_playlist_type playlist_type;
	int i, not_loaded = 0, num_playlists = 0;
	int *reorder;
	sp_playlist *pl;
	node *items, *parent, *previous;
	
	
#ifdef TESTING
	playlist_item *faux_playlist;
#endif
	
	num_playlists = sp_playlistcontainer_num_playlists(pc);
	items = previous = parent = NULL;
	
#ifdef TESTING
	faux_playlist = (playlist_item*) malloc(sizeof(playlist_item) * num_playlists);
#endif

	printf("Reordering %d playlists and playlist folders\n", num_playlists);
	
	for (i = 0; i < num_playlists; ++i) {
		playlist_type = sp_playlistcontainer_playlist_type(pc, i);
		
		switch (playlist_type) {
				
			case SP_PLAYLIST_TYPE_PLAYLIST:
				
				pl = sp_playlistcontainer_playlist(pc, i);
				if (!sp_playlist_is_loaded(pl)) {
					not_loaded++;
				} else {
					previous = create_node(previous, parent, create_playlist_item(i, sp_playlist_name(pl)));
					if (items == NULL) {
						items = previous;
					}
				}
				
#ifdef TESTING
				faux_playlist[i].index = -1;
				faux_playlist[i].name = strdup(sp_playlist_name(pl));
#endif
				
				break;
			case SP_PLAYLIST_TYPE_START_FOLDER:
				
				parent = create_node(previous, parent, create_playlist_item(i, sp_playlistcontainer_playlist_folder_name(pc, i)));
				previous = NULL;
				if (items == NULL) {
					items = parent;
				}
				
#ifdef TESTING
				faux_playlist[i].index = sp_playlistcontainer_playlist_folder_id(pc, i);
				faux_playlist[i].name = strdup(sp_playlistcontainer_playlist_folder_name(pc, i));
#endif
				
				break;
			case SP_PLAYLIST_TYPE_END_FOLDER:
				
				previous = parent;
				previous->item->end_index = i;
				parent = parent->parent;
				
#ifdef TESTING
				faux_playlist[i].index = sp_playlistcontainer_playlist_folder_id(pc, i);
				faux_playlist[i].name = NULL;
#endif
				
				break;
			case SP_PLAYLIST_TYPE_PLACEHOLDER:

#ifdef TESTING
				printf("%d. Placeholder", i);
				faux_playlist[i].index = -1;
				faux_playlist[i].name = NULL;
#endif
				
				break;
		}
	}
	
	if(not_loaded > 0) {
		printf("ERROR: %d playlists could not be loaded\n", not_loaded);
		return 1;
	}
	
	if(items != NULL) {
		items = sort_list(items);

#ifdef TESTING
		print_list(items);
#endif
		
		reorder = (int *) malloc(sizeof(int) * num_playlists);
		flatten_list(items, reorder);
		
		for(i = 0; i < num_playlists; ++i) {
			printf(".");
			if(i != reorder[i]) {
#ifdef TESTING
				printf("Moving item at %d -> %d\n", reorder[i], i);
				move_playlist(faux_playlist, num_playlists, reorder[i], i);
#else			
				sp_playlistcontainer_move_playlist(pc, reorder[i], i);
#endif
				recalculate_indexes(reorder, num_playlists, i);
			}
		}
		printf("\ndone\n");
		
		free(reorder);
		free_list(items);
	}
	
	
#ifdef TESTING
	for(i = 0; i < num_playlists; ++i) {
		if(faux_playlist[i].name != NULL) {
			printf(" -- %s (%d)\n", faux_playlist[i].name, faux_playlist[i].index);
			free((void*)faux_playlist[i].name);
		} else {
			printf(" -- %d\n", faux_playlist[i].index);
		}
	}
	free(faux_playlist);
#endif
	
	return 1;
}
Exemple #30
0
static PyObject *Playlist_is_loaded(Playlist *self) {
    return Py_BuildValue("i", sp_playlist_is_loaded(self->_playlist));
}