void QSpotifyPlaylistContainer::addPlaylist(sp_playlist *playlist, int pos)
{
    if (playlist != sp_playlistcontainer_playlist(m_container, pos)) {
        int count = sp_playlistcontainer_num_playlists(m_container);
        for (int i = 0; i < count; ++i) {
            if (playlist == sp_playlistcontainer_playlist(m_container, i)) {
                pos = i;
                break;
            }
        }
    }

    sp_playlist_type type = sp_playlistcontainer_playlist_type(m_container, pos);
    QSpotifyPlaylist *pl = new QSpotifyPlaylist(QSpotifyPlaylist::Type(type), playlist);
    if (pos == -1)
        m_playlists.append(pl);
    else
        m_playlists.insert(pos, pl);
    if (type == SP_PLAYLIST_TYPE_START_FOLDER) {
        char buffer[200];
        sp_playlistcontainer_playlist_folder_name(m_container, pos, buffer, 200);
        pl->m_name = QString::fromUtf8(buffer);
    }
    connect(pl, SIGNAL(nameChanged()), this, SIGNAL(playlistsNameChanged()));
}
Example #2
0
/**
 * This callback is called when an attempt to login has succeeded or failed.
 *
 * @sa sp_session_callbacks#logged_in
 */
static void logged_in(sp_session *sess, sp_error error)
{
	sp_playlistcontainer *pc = sp_session_playlistcontainer(sess);
	int i;

	if (SP_ERROR_OK != error) {
		fprintf(stderr, "jukebox: Login failed: %s\n",
			sp_error_message(error));
		exit(2);
	}

	sp_playlistcontainer_add_callbacks(
		pc,
		&pc_callbacks,
		NULL);

	printf("jukebox: Looking at %d playlists\n", sp_playlistcontainer_num_playlists(pc));

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

		sp_playlist_add_callbacks(pl, &pl_callbacks, NULL);

		if (!strcasecmp(sp_playlist_name(pl), g_listname)) {
			g_jukeboxlist = pl;
			try_jukebox_start();
		}
	}

	if (!g_jukeboxlist) {
		printf("jukebox: No such playlist. Waiting for one to pop up...\n");
		fflush(stdout);
	}
}
Example #3
0
void SP_CALLCONV playlist_renamed(sp_playlist *pl, void *userdata) {
	sp_playlistcontainer *pc = sp_session_playlistcontainer(session);
	int num = sp_playlistcontainer_num_playlists(pc);
	int pos;
	MLTREEITEMINFO treeItemInfo;
	UINT_PTR treeId, treeHandle;


	/* Find position of renamed playlist in the playlist container */
	for(pos = 0; pos < num; pos++)
		if(sp_playlistcontainer_playlist(pc, pos) == pl)
			break;

	if(pos == num) {
		DSFYDEBUG("WTF, did not find playlist %p in container!\n", pl);
		return;
	}


	/* Get tree ID so we can get the handle */
	treeId = tpGetTreeId(pos);
	treeHandle = SendMessage(hwndLibraryParent, WM_ML_IPC, (WPARAM)treeId, ML_IPC_TREEITEM_GETHANDLE);

	/* Create request to update the item */
	memset(&treeItemInfo, 0, sizeof(MLTREEITEMINFO));
	treeItemInfo.handle = treeHandle;
	treeItemInfo.mask = MLTI_TEXT;
	treeItemInfo.item.size = sizeof(MLTREEITEM);
	treeItemInfo.item.title = (char *)sp_playlist_name(pl);

	/* Submit request */
	SendMessage(hwndLibraryParent, WM_ML_IPC, (WPARAM)&treeItemInfo, ML_IPC_TREEITEM_SETINFO);
}
Example #4
0
int cmd_playlist(int argc, char **argv)
{
	int index, i;
	sp_track *track;
	sp_playlist *playlist;
	sp_playlistcontainer *pc = sp_session_playlistcontainer(g_session);

	if (argc < 1) {
		printf("playlist [playlist index]\n");
		return 0;
	}

	index = atoi(argv[1]);
	if (index < 0 || index > sp_playlistcontainer_num_playlists(pc)) {
		printf("invalid index\n");
		return 0;
	}
	playlist = sp_playlistcontainer_playlist(pc, index);
	printf("Playlist %s by %s%s%s\n",
		   sp_playlist_name(playlist),
		   sp_user_display_name(sp_playlist_owner(playlist)),
		   sp_playlist_is_collaborative(playlist) ? " (collaborative)" : "",
		   sp_playlist_has_pending_changes(playlist) ? " with pending changes" : ""
		   );
	for (i = 0; i < sp_playlist_num_tracks(playlist); ++i) {
		track = sp_playlist_track(playlist, i);
		printf("%d. %c %s%s %s\n", i,
			   sp_track_is_starred(g_session, track) ? '*' : ' ',
			   sp_track_is_local(g_session, track) ? "local" : "     ",
			   sp_track_is_autolinked(g_session, track) ? "autolinked" : "          ",
			   sp_track_name(track));
	}
	return 1;
}
ClientResponse* SpotifyPlaylistContainer::playPlaylist(PlaylistTask* task) {
	StatusResponse* response = new StatusResponse(false,
			"Playlist Id is out of range!");

	int playlistNum = task->getCommandInfo().playlist;
	if (playlistNum >= 0
			&& playlistNum
					< sp_playlistcontainer_num_playlists(playlistContainer)) {
		sp_playlist* playlist = sp_playlistcontainer_playlist(playlistContainer,
				playlistNum);
		if (sp_playlist_num_tracks(playlist) > 0) {
			sp_track* track = sp_playlist_track(playlist, 0);
			spotifySession->getSpotifyPlayer()->clearPlayQueue();
			spotifySession->getSpotifyPlayer()->playTrack(track);
			for (int i = 1; i < sp_playlist_num_tracks(playlist); i++) {
				spotifySession->getSpotifyPlayer()->addTrackToQueue(
						sp_playlist_track(playlist, i));
			}
			response->setSuccess(true);
			response->setMessage(nullptr);
		} else {
			response->setMessage("Playlist does not have any tracks");
		}
	}

	return response;
}
ListResponse<TrackInfo*>* SpotifyPlaylistContainer::listTracks(
		PlaylistTask* task) {
	ListResponse<TrackInfo*>* response = new ListResponse<TrackInfo*>();
	response->setListType(ListTypeSong);
	ListPlaylistTrackInfo info = task->getCommandInfo().listPlaylistTrackInfo;
	int playlistId = info.playlist;
	if (playlistId > -1
			&& playlistId
					< sp_playlistcontainer_num_playlists(playlistContainer)) {
		bool sendName = ((info.trackInfoFlags & TrackInfoName) == TrackInfoName);
		bool sendArtist = ((info.trackInfoFlags & TrackInfoArtists)
				== TrackInfoArtists);
		bool sendAlbum = ((info.trackInfoFlags & TrackInfoAlbum)
				== TrackInfoAlbum);
		bool sendDuration = ((info.trackInfoFlags & TrackInfoDuration)
				== TrackInfoDuration);
		bool sendArtwork = ((info.trackInfoFlags & TrackInfoArtwork)
				== TrackInfoArtwork);

		sp_playlist* playlist = sp_playlistcontainer_playlist(playlistContainer,
				playlistId);
		int numTracks = sp_playlist_num_tracks(playlist);
		for (int i = 0; i < numTracks; i++) {
			TrackInfo* trackInfo = new TrackInfo();
			trackInfo->id = i;

			sp_track* track = sp_playlist_track(playlist, i);

			if (sendName) {
				trackInfo->name = sp_track_name(track);
			}
			if (sendArtist) {
				trackInfo->numArtists = sp_track_num_artists(track);
				trackInfo->artists = new const char*[trackInfo->numArtists];
				for (int j = 0; j < trackInfo->numArtists; j++) {
					trackInfo->artists[j] = sp_artist_name(
							sp_track_artist(track, j));
				}
			}
			if (sendAlbum) {
				trackInfo->album = sp_album_name(sp_track_album(track));
			}
			if (sendDuration) {
				trackInfo->duration = sp_track_duration(track);
			}
			if (sendArtwork) {
				trackInfo->artwork = sp_image_create(session,
						sp_album_cover(sp_track_album(track),
								SP_IMAGE_SIZE_NORMAL));
			}
			response->addMember(trackInfo);
		}

		return response;
	}

	delete response;
	return nullptr;
}
Example #7
0
sp_playlist* playlist_get(int nb) {
    if (nb == 0) {
        if (g_starred_playlist == NULL)
            g_starred_playlist = sp_session_starred_create(g_session);
        return g_starred_playlist;
    }
    else
        return sp_playlistcontainer_playlist(g_container, nb-1);
}
Example #8
0
/**
 * Callback from libspotify, telling us the rootlist is fully synchronized
 *
 * @param  pc            The playlist container handle
 * @param  userdata      The opaque pointer
 */
static void container_loaded ( sp_playlistcontainer *pc, void *userdata )
{
    char *folderName = malloc ( sizeof ( char ) * ( MAX_LENGTH_FOLDER_NAME ) );
    int i;


    if ( folderName == NULL )
    {
        fprintf ( stderr, "jahspotify: Could not allocate folder name variable)\n" );
        return;
    }

    // fprintf ( stderr, "jahspotify: Rootlist synchronized (%d playlists)\n",sp_playlistcontainer_num_playlists ( pc ) );
    signalSynchStarting(sp_playlistcontainer_num_playlists (pc));

    for ( i = 0; i < sp_playlistcontainer_num_playlists ( pc ); ++i )
    {
        sp_playlist *pl = sp_playlistcontainer_playlist ( pc, i );
        sp_playlist_add_callbacks ( pl, &pl_callbacks, NULL );

        sp_link *link = sp_link_create_from_playlist(pl);

        char *linkStr = malloc(sizeof(char) * 100);
        if (link)
        {
            sp_link_add_ref(link);
            sp_link_as_string(link,linkStr,100);
        }
        else
        {
            strcpy(linkStr,"N/A\0");
        }
        switch ( sp_playlistcontainer_playlist_type ( pc,i ) )
        {
          case SP_PLAYLIST_TYPE_PLAYLIST:
            signalPlaylistSeen(sp_playlist_name ( pl ),linkStr);
            break;
          case SP_PLAYLIST_TYPE_START_FOLDER:
            sp_playlistcontainer_playlist_folder_name ( pc,i,folderName, MAX_LENGTH_FOLDER_NAME);
            signalStartFolderSeen(folderName, sp_playlistcontainer_playlist_folder_id(pc,i));
            break;
          case SP_PLAYLIST_TYPE_END_FOLDER:
            sp_playlistcontainer_playlist_folder_name ( pc,i,folderName,MAX_LENGTH_FOLDER_NAME);
            signalEndFolderSeen();
            break;
          case SP_PLAYLIST_TYPE_PLACEHOLDER:
            fprintf ( stderr,"jahspotify: placeholder\n");
        }

        if (link)
            sp_link_release(link);
        free(linkStr);
    }
    signalSynchCompleted();
    free ( folderName );
}
Example #9
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;
}
Example #10
0
// ----------------------------------------------------------------------------
//
PlaylistArray SpotifyEngine::getPlaylists( void ) {
    PlaylistArray playlists;

    sp_playlistcontainer *pc = sp_session_playlistcontainer( m_spotify_session );

    for ( int i=0; i < sp_playlistcontainer_num_playlists(pc); i++ ) {
        sp_playlist *pl = sp_playlistcontainer_playlist(pc, i);
        if ( sp_playlist_num_tracks( pl ) > 0 )
            playlists.push_back( pl );
    }

    return playlists;
}
void Spotify::compileNewListOfPlaylists()
{
    sp_playlistcontainer * pc = sp_session_playlistcontainer(sp);
    int numPlaylists = sp_playlistcontainer_num_playlists(pc);

    QStringList names;
    for (int idx = 0; idx < numPlaylists; ++idx) {
        sp_playlist *pl = sp_playlistcontainer_playlist(pc, idx);
        names << QString::fromLocal8Bit(sp_playlist_name(pl));
    }

    emit playlistsUpdated(names);
}
void Spotify::changeCurrentPlaylist()
{
    sp_error err;
    sp_playlist * playlist;

    if (currentPlaylistIdx < 0) {
        return;
    }

    sp_playlistcontainer * pc = sp_session_playlistcontainer(sp);
    if (currentPlaylistIdx >= sp_playlistcontainer_num_playlists(pc)) {
        return;
    }

    switch (sp_playlistcontainer_playlist_type(pc, currentPlaylistIdx)) {
    case SP_PLAYLIST_TYPE_PLAYLIST:
        playlist = sp_playlistcontainer_playlist(pc, currentPlaylistIdx);
        if (!playlist) {
            fprintf(stderr, "Spotify: failed to get playlist\n");
            break;
        }

        err = sp_playlist_add_callbacks(playlist, Spotify_Wrapper::playlistCallbacks(), NULL);
        if (err != SP_ERROR_OK) {
            fprintf(stderr, "Spotify: failed to add callbacks to playlist: %s\n",
                    sp_error_message(err));
        }

        if (currentPlaylist) {
            sp_playlist_remove_callbacks(currentPlaylist, Spotify_Wrapper::playlistCallbacks(), NULL);
            sp_playlist_release(currentPlaylist);
        }
        currentPlaylist = playlist;
        sp_playlist_add_ref(currentPlaylist);

        fprintf(stderr, "Spotify: switched to playlist %s\n",
                sp_playlist_name(currentPlaylist));

        tryLoadPlaylist();
        break;

    default:
        fprintf(stderr, "Spotify: Tried to load a playlist that wasn't a playlist\n");
        currentPlaylistIdx = -1;
        break;
    }
}
bool QSpotifyPlaylistContainer::updateData()
{
    bool updated = false;

    if (m_playlists.isEmpty()) {
        int count = sp_playlistcontainer_num_playlists(m_container);
        for (int i = 0; i < count; ++i) {
            addPlaylist(sp_playlistcontainer_playlist(m_container, i), i);
            if(sp_playlistcontainer_playlist_type(m_container, i) == SP_PLAYLIST_TYPE_PLACEHOLDER)
                sp_playlistcontainer_remove_playlist(m_container, i);
        }
        updated = true;
        updatePlaylists();
    }

    return updated;
}
Example #14
0
int cmd_update_subscriptions(int argc, char **argv)
{
	int i;
	sp_playlistcontainer *pc = sp_session_playlistcontainer(g_session);
	sp_playlist *pl;
	subscriptions_updated = 1;
	for (i = 0; i < sp_playlistcontainer_num_playlists(pc); ++i) {
		switch (sp_playlistcontainer_playlist_type(pc, i)) {
		case SP_PLAYLIST_TYPE_PLAYLIST:
			pl = sp_playlistcontainer_playlist(pc, i);
			sp_playlist_update_subscribers(g_session, pl);
			break;
		default:
			break;
		}
	}
	return 1;
}
/* UTILITY */
static sp_playlistcontainer_playlist_t *
sp_mock_playlistcontainer_find_playlist(sp_playlistcontainer *pc, sp_playlist *_playlist)
{
  sp_playlistcontainer_playlist_t *playlist = NULL;
  sp_playlist *tmp = NULL;
  int i;

  for (i = 0; i < sp_playlistcontainer_num_playlists(pc); ++i)
  {
    tmp = sp_playlistcontainer_playlist(pc, i);
    if (tmp != NULL && _playlist == tmp)
    {
      playlist = &pc->playlists[i];
    }
  }

  return playlist;
}
void SpotifyGuiController::RefreshPlaylistContainer()
{
	SpotifyUserData *data = (SpotifyUserData *)spotify_userdata;

	if ( !data->container) return ;

	if ( sp_playlistcontainer_is_loaded(data->container) )
	{
		int n_plist = sp_playlistcontainer_num_playlists(data->container);

		for ( int np = 0 ; np < n_plist ; np++ )
		{
			sp_playlist *plist = sp_playlistcontainer_playlist(data->container, np);

			AddPlaylist(plist);
		}
	}
}
Example #17
0
static sp_playlist *find_album_playlist(sp_playlistcontainer *list_container, int *spiffify_start_index, int spiffify_end_index, sp_album *al) 
{
	if (al == NULL) {
		return NULL;
	}
	int j=*spiffify_start_index;
	sp_artist *a = sp_album_artist(al);
	const char *artist = sp_artist_name(a);
	int artist_end = 0;
	sp_uint64 artist_folder_id = 0;
	for(; j < spiffify_end_index; j++) {
		sp_playlist *p = sp_playlistcontainer_playlist(list_container, j);
		if (p == NULL) {
			continue;
		}
		sp_playlist_type type = sp_playlistcontainer_playlist_type(list_container,j);
		if (type == SP_PLAYLIST_TYPE_START_FOLDER) {
			char folder_name[256];
			sp_error error = sp_playlistcontainer_playlist_folder_name(list_container, j, folder_name, 255);
			if (strcmp(folder_name, artist) == 0) {
				artist_folder_id = sp_playlistcontainer_playlist_folder_id(list_container, j);
			}
		}
		else if (type == SP_PLAYLIST_TYPE_END_FOLDER) {
			if (artist_folder_id == sp_playlistcontainer_playlist_folder_id(list_container, j)) {
				artist_end = j;
			}
		}
		const char *plname = sp_playlist_name(p);
		if (plname != NULL) {
			const char *alname = sp_album_name(al);
			if (strcmp(plname, alname) == 0) {
				return p;
			}
		}
	}
	// in the case where we don't yet have the playlist for this album and we are about to create it, send back the end of this artist folder
	if (artist_end != 0) {
		*spiffify_start_index = artist_end-1;
	}
	return NULL;
}
Example #18
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);

  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);
}
Example #19
0
int cmd_playlists(int argc, char **argv)
{
	sp_playlistcontainer *pc = sp_session_playlistcontainer(g_session);
	int i, j, level = 0;
	sp_playlist *pl;
	char name[200];

	printf("%d entries in the container\n", sp_playlistcontainer_num_playlists(pc));

	for (i = 0; i < sp_playlistcontainer_num_playlists(pc); ++i) {
		switch (sp_playlistcontainer_playlist_type(pc, i)) {
			case SP_PLAYLIST_TYPE_PLAYLIST:
				printf("%d. ", i);
				for (j = level; j; --j) printf("\t");
				pl = sp_playlistcontainer_playlist(pc, i);
				printf("%s", sp_playlist_name(pl));
				if(subscriptions_updated)
					printf(" (%d subscribers)", sp_playlist_num_subscribers(pl));
				printf("\n");
				break;
			case SP_PLAYLIST_TYPE_START_FOLDER:
				printf("%d. ", i);
				for (j = level; j; --j) printf("\t");
				sp_playlistcontainer_playlist_folder_name(pc, i, name, sizeof(name));
				printf("Folder: %s with id %llu\n", name,
					   sp_playlistcontainer_playlist_folder_id(pc, i));
				level++;
				break;
			case SP_PLAYLIST_TYPE_END_FOLDER:
				level--;
 				printf("%d. ", i);
				for (j = level; j; --j) printf("\t");
				printf("End folder with id %llu\n",	sp_playlistcontainer_playlist_folder_id(pc, i));
				break;
			case SP_PLAYLIST_TYPE_PLACEHOLDER:
				printf("%d. Placeholder", i);
				break;
		}
	}
	return 1;
}
Example #20
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);
	}
    }
}
Example #21
0
/**
 * This callback is called when an attempt to login has succeeded or failed.
 *
 * @sa sp_session_callbacks#logged_in
 */
static void logged_in ( sp_session *sess, sp_error error )
{
    sp_playlistcontainer *pc = sp_session_playlistcontainer ( sess );
    int i;

    if ( SP_ERROR_OK != error )
    {
        fprintf ( stderr, "jahspotify: Login failed: %s\n", sp_error_message ( error ) );
        exit ( 2 );
    }

    fprintf ( stderr, "jahspotify: Login Success: %d\n", sp_playlistcontainer_num_playlists(pc));

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

        sp_playlist_add_callbacks(pl, &pl_callbacks, NULL);
    }

    signalLoggedIn();

}
Example #22
0
PHP_METHOD(Spotify, getPlaylists)
{
	zval *thisptr = getThis(), tempretval;
	int i, timeout = 0, num_playlists;

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

	SPOTIFY_METHOD(Spotify, initPlaylistContainer, &tempretval, thisptr);

	array_init(return_value);

	num_playlists = sp_playlistcontainer_num_playlists(p->playlistcontainer);
	for (i=0; i<num_playlists; i++) {
		sp_playlist *playlist = sp_playlistcontainer_playlist(p->playlistcontainer, i);

		zval *z_playlist;
		ALLOC_INIT_ZVAL(z_playlist);
		object_init_ex(z_playlist, spotifyplaylist_ce);
		SPOTIFY_METHOD2(SpotifyPlaylist, __construct, &tempretval, z_playlist, thisptr, playlist);
		add_next_index_zval(return_value, z_playlist);
	}
}
void PlayListContainer::OnContainerLoaded() {
    LOG4CPLUS_DEBUG(logger, "OnContainerLoaded");

    int num_playlists = sp_playlistcontainer_num_playlists(container_);

    boost::shared_ptr<PlayListElement> it_container = shared_from_this();

    for (int i = 0; i < num_playlists; i++) {
        sp_playlist_type type = sp_playlistcontainer_playlist_type(container_, i);

        switch (type) {
            case SP_PLAYLIST_TYPE_PLAYLIST: {
                sp_playlist *p = sp_playlistcontainer_playlist(container_, i);

                boost::shared_ptr<PlayList> playlist = session_->CreatePlayList();
                playlist->Load(p);

                it_container->AddPlayList(playlist);
            }   break;
            case SP_PLAYLIST_TYPE_START_FOLDER: {
                boost::shared_ptr<PlayListFolder> folder = session_->CreatePlayListFolder();
                folder->Load(container_, i);

                it_container->AddPlayList(folder);
                it_container = folder;
            }   break;
            case SP_PLAYLIST_TYPE_END_FOLDER:
                it_container = it_container->GetParent();
                break;
            case SP_PLAYLIST_TYPE_PLACEHOLDER:
            default:
                LOG4CPLUS_WARN(logger, "Unrecognized playlist type");
                break;
        }
    }

    BOOST_ASSERT(it_container == shared_from_this());
}
Example #24
0
int cmd_set_autolink(int argc, char **argv)
{
	int index; 
	bool autolink;
	sp_playlist *playlist;
	sp_playlistcontainer *pc = sp_session_playlistcontainer(g_session);

	if (argc < 2) {
		printf("set autolink [playlist index] [0/1]\n");
		return 0;
	}

	index = atoi(argv[1]);
	autolink = atoi(argv[2]);
	if (index < 0 || index > sp_playlistcontainer_num_playlists(pc)) {
		printf("invalid index\n");
		return 0;
	}
	playlist = sp_playlistcontainer_playlist(pc, index);
	sp_playlist_set_autolink_tracks(playlist, !!autolink);
	printf("Set autolinking to %s on playlist %s\n", autolink ? "true": "false", sp_playlist_name(playlist));
	return 1;
}
ListResponse<PlaylistInfo*>* SpotifyPlaylistContainer::listPlaylists(
		PlaylistTask* task) {
	ListResponse<PlaylistInfo*>* response = new ListResponse<PlaylistInfo*>();
	response->setListType(ListTypePlaylist);
	CommandInfo info = task->getCommandInfo();
	bool sendName = ((info.ListFlags & Name) == Name);
	bool sendNumTracks = ((info.ListFlags & NumTracks) == NumTracks);
	bool sendDescription = ((info.ListFlags & Description) == Description);

	int numPlaylists = sp_playlistcontainer_num_playlists(playlistContainer);
	for (int i = 0; i < numPlaylists; i++) {
		if (sp_playlistcontainer_playlist_type(playlistContainer, i)
				== SP_PLAYLIST_TYPE_PLAYLIST) {
			sp_playlist* playlist = sp_playlistcontainer_playlist(
					playlistContainer, i);
			const char* name = sp_playlist_name(playlist);

			if (!(name[0] == '_' && name[1] == '_')) {
				PlaylistInfo* playlistInfo = new PlaylistInfo();
				playlistInfo->id = i;

				if (sendName) {
					playlistInfo->name = name;
				}
				if (sendNumTracks) {
					playlistInfo->numTracks = sp_playlist_num_tracks(playlist);
				}
				if (sendDescription) {
					playlistInfo->description = sp_playlist_get_description(
							playlist);
				}
				response->addMember(playlistInfo);
			}
		}
	}
	return response;
}
ClientResponse* SpotifyPlaylistContainer::playTrack(PlaylistTask* task) {
	StatusResponse* response = new StatusResponse(false,
			"Playlist Id is out of range!");

	PlayTrackCommandInfo info = task->getCommandInfo().playTrackCommandInfo;
	if (info.playlistId > -1
			&& info.playlistId
					< sp_playlistcontainer_num_playlists(playlistContainer)) {
		sp_playlist* playlist = sp_playlistcontainer_playlist(playlistContainer,
				info.playlistId);
		if (info.trackId > -1
				&& info.trackId < sp_playlist_num_tracks(playlist)) {
			spotifySession->getSpotifyPlayer()->clearPlayQueue();
			for (int i = 0; i < info.trackId; i++) {
				spotifySession->getSpotifyPlayer()->addTrackToPlayedQueue(
						sp_playlist_track(playlist, i));
			}

			sp_track* track = sp_playlist_track(playlist, info.trackId);
			spotifySession->getSpotifyPlayer()->playTrack(track);
			for (int i = info.trackId + 1; i < sp_playlist_num_tracks(playlist);
					i++) {
				spotifySession->getSpotifyPlayer()->addTrackToQueue(
						sp_playlist_track(playlist, i));
			}
            if(spotifySession->getSpotifyPlayer()->isShuffled()) {
                spotifySession->getSpotifyPlayer()->setShuffle(true);
            }
			response->setSuccess(true);
			response->setMessage(nullptr);
		} else {
			response->setMessage("Track does not exist!");
		}
	}

	return response;
}
Example #27
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);
}
Example #28
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;
}
Example #29
0
static bool spiffify() {
	if (!validate_complete_load()) {
		return false;
	}
	sp_playlistcontainer *list_container = g_list_container;	

	// check to see if all playlists have been loaded
	int pl_count = sp_playlistcontainer_num_playlists(list_container);
	int i=0;
	sp_playlist *source_list = NULL;
	sp_playlist *spiffify_list = NULL;
	char folder_name[256];
	int spiffify_start_index = -1;
	int spiffify_end_index = -1;
	sp_uint64 spiffify_folder_id = -1;
	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;
		}
		sp_playlist_type type = sp_playlistcontainer_playlist_type(list_container, i);
		if (type == SP_PLAYLIST_TYPE_START_FOLDER) {
			sp_error error = sp_playlistcontainer_playlist_folder_name(list_container, i, folder_name, 256);
			if (error == SP_ERROR_OK) {
				if (strcmp(folder_name, SPIFFIFY_PLAYLIST_NAME) == 0) {
					spiffify_list = l;
					spiffify_start_index = i;
					spiffify_folder_id = sp_playlistcontainer_playlist_folder_id(list_container, i);
				}
			}
		}
		else if (type == SP_PLAYLIST_TYPE_END_FOLDER) {
			sp_uint64 id = sp_playlistcontainer_playlist_folder_id(list_container, i);
			if (id == spiffify_folder_id) {
				spiffify_end_index = i;
			}
		}
	}

	if (source_list == NULL) {
		fprintf(stderr, "Cannot find source list: %s\n", playlist_name);
		exit(1);
	}

	if (spiffify_list != NULL && spiffify_start_index != -1) {
		// smoke the list and start over every time.. it's just not worth the effort of inserting changes.
		int x=spiffify_end_index;
		for(; x >= spiffify_start_index; x--) {
			sp_playlistcontainer_remove_playlist(list_container, x);
		}
		spiffify_list = NULL;
		spiffify_start_index = -1;
		spiffify_end_index = -1;
	}
	
	pl_count = sp_playlistcontainer_num_playlists(list_container); // reset count.

	if (spiffify_list == NULL) {
		// make the Spiffify list for this user;
		sp_error error = sp_playlistcontainer_add_folder(list_container, pl_count, SPIFFIFY_PLAYLIST_NAME);
		if (error == SP_ERROR_OK) {
			spiffify_list = sp_playlistcontainer_playlist(list_container, pl_count);
			spiffify_start_index = pl_count;
			spiffify_end_index = spiffify_start_index+1;
		}
		if (spiffify_list == NULL) {
			fprintf(stderr, "Cannot create '%s' playlist\n", SPIFFIFY_PLAYLIST_NAME);
			exit(1);
		}
	}
	
	// iterate the source playlist
	int nt = sp_playlist_num_tracks(source_list);

	// allocate storage for tracks
	sp_track **tracks = (sp_track **) malloc(nt * sizeof(sp_track *));
	i=0;
	for(; i < nt; i++) {
		sp_track *t = sp_playlist_track(source_list, i);
		tracks[i] = t; // store in the array
	}
	// :)
	mergesort(tracks, nt, sizeof(sp_track *), &compare_tracks);

	i=0;
	for(; i < nt; i++) {
		sp_track *t = tracks[i]; //sp_playlist_track(source_list, i);
		sp_artist *a = sp_track_artist(t,0); // get first artist on track because I am lazy
		// find artist folder inside our spiffify list
		bool haveartist = false;
		int j=spiffify_start_index;
		char artist[256];
		
		if (a != NULL) {
			strcpy(artist, sp_artist_name(a));
		}
		else {
			strcpy(artist, "Unknown Artist");
		}

		for(; j < spiffify_end_index; j++) {
			sp_error error = sp_playlistcontainer_playlist_folder_name(list_container, j, folder_name, 256);
			if (strcmp(artist, folder_name) == 0) {
				haveartist = true;
				break;
			}
		}
		sp_album *al = sp_track_album(t);
		while (al == NULL) {
			printf("Cannot find album or it's not loaded.. forcing an event loop\n");
			return false;
		}
		if (!haveartist) {
			int artist_pos = spiffify_end_index;
			sp_playlistcontainer_add_folder(list_container, artist_pos, artist);
			spiffify_end_index+=2; // a folder adds 2 indexes.
			pl_count = sp_playlistcontainer_num_playlists(list_container);
			sp_playlist* album = sp_playlistcontainer_add_new_playlist(list_container, sp_album_name(al));
			sp_playlist_add_tracks(album, &t, 1, 0, session);
			sp_playlistcontainer_move_playlist(list_container, pl_count, artist_pos+1, false);
			spiffify_end_index++; // one more for the album.
		}
		else {
			int artist_pos = j;
			sp_playlist *albumpl = find_album_playlist(list_container, &artist_pos, spiffify_end_index, al);
			if (albumpl == NULL) {
				pl_count = sp_playlistcontainer_num_playlists(list_container);
				albumpl = sp_playlistcontainer_add_new_playlist(list_container, sp_album_name(al));
				sp_playlistcontainer_move_playlist(list_container, pl_count, artist_pos+1, false);
				spiffify_end_index++; // one more for the album.
			}
			sp_playlist_add_tracks(albumpl, &t, 1, 0, session);
		}
	}
	free(tracks);
	return true;
}