bool QSpotifyUser::createPlaylistInFolder(const QString &name, QSpotifyPlaylist *folder) { if (!folder || folder->type() != QSpotifyPlaylist::Folder) return createPlaylist(name); if (name.trimmed().isEmpty()) return false; QString n = name; if (n.size() > 255) n.resize(255); sp_playlist *pl = sp_playlistcontainer_add_new_playlist(m_playlistContainer->m_container, n.toUtf8().constData()); if (!pl) return false; int i = m_playlistContainer->m_playlists.indexOf(folder); if (i == -1) return true; sp_uint64 folderId = sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, i); int count = sp_playlistcontainer_num_playlists(m_playlistContainer->m_container); for (int j = i + 1; j < count; ++j) { if (folderId == sp_playlistcontainer_playlist_folder_id(m_playlistContainer->m_container, j)) i = j; } sp_playlistcontainer_move_playlist(m_playlistContainer->m_container, count - 1, i, false); return true; }
/** * 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; }
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; }