// This could delete multiple selections at once, but it doesn't private void deleteSong_Click(object sender, EventArgs e) { api.OnDeleteFromPlaylistComplete = OnChangePlaylist; GMusicSong selectedSong = (GMusicSong)playlistSongsBox.SelectedItem; GMusicPlaylist selectedPlaylist = (GMusicPlaylist)playlistListBox.SelectedItem; GMusicPlaylistEntry songEntry = selectedPlaylist.Songs.First(s => s.TrackID == selectedSong.ID); api.DeleteFromPlaylist(new List <GMusicPlaylistEntry> { songEntry }); }
private void playlistListBox_SelectedIndexChanged(object sender, EventArgs e) { playlistSongsBox.Items.Clear(); GMusicPlaylist selectedPlaylist = (GMusicPlaylist)playlistListBox.SelectedItem; foreach (GMusicPlaylistEntry song in selectedPlaylist.Songs) { if (!song.Deleted) { GMusicSong thisSong = AllSongs.FirstOrDefault(s => s.ID == song.TrackID); playlistSongsBox.Items.Add(thisSong); } } }
// Go through the selected playlists from GMusic, // delete the correspondingly named MusicBee playlist // Create a new playlist with the GMusic playlist contents public void SyncPlaylistsToMusicBee(List <GMusicPlaylist> playlists, List <GMusicSong> allGMusicSongs) { // The API doesn't give us a directory for playlists, // We need to guess by finding the root directory of the first playlist /* Apparently playlistDir = "" is "root" playlist dir, so this is unneeded. * MbPlaylist useForDir = localPlaylists.First(); * String playlistDir = new FileInfo(useForDir.mbName).DirectoryName; * if (useForDir.Name.Contains('\\')) * { * String folder = useForDir.Name.Split('\\')[0]; * playlistDir = playlistDir.Replace(folder, ""); * }*/ List <MbPlaylist> localPlaylists = GetMbPlaylists(); List <MbSong> allMbSongs = GetMbSongs(); // Go through each playlist we want to sync in turn foreach (GMusicPlaylist playlist in playlists) { // Create an empty list for this playlist's local songs List <MbSong> mbPlaylistSongs = new List <MbSong>(); // For each entry in the playlist we're syncing, get the song from the GMusic library we've downloaded, // Get the song Title and Artist and then look it up in the list of local songs. // If we find it, add it to the list of local songs foreach (GMusicPlaylistEntry entry in playlist.Songs) { GMusicSong thisSong = allGMusicSongs.FirstOrDefault(s => s.ID == entry.TrackID); if (thisSong != null) { MbSong thisMbSong = allMbSongs.FirstOrDefault(s => s.Artist == thisSong.Artist && s.Title == thisSong.Title); if (thisMbSong != null) { mbPlaylistSongs.Add(thisMbSong); } } } //mbAPI expects a string array of song filenames to create a playlist string[] mbPlaylistSongFiles = new string[mbPlaylistSongs.Count]; int i = 0; foreach (MbSong song in mbPlaylistSongs) { mbPlaylistSongFiles[i] = song.Filename; i++; } // Now we need to either clear (by deleting and recreating the file) or create the playlist MbPlaylist localPlaylist = localPlaylists.FirstOrDefault(p => p.Name == playlist.Name); if (localPlaylist != null) { string playlistPath = localPlaylist.mbName; // delete the local playlist File.Delete(playlistPath); // And create a new empty file in its place File.Create(playlistPath).Dispose(); // Set all our new files into the playlist _mbApiInterface.Playlist_SetFiles(localPlaylist.mbName, mbPlaylistSongFiles); } else { // Create the playlist _mbApiInterface.Playlist_CreatePlaylist("", playlist.Name, mbPlaylistSongFiles); // I haven't been able to get a playlist to be created in a directory yet // For now, don't give that option // _mbApiInterface.Playlist_CreatePlaylist(_settings.PlaylistDirectory, playlist.Name, mbPlaylistSongFiles); } } // Get the local playlists again // Call the delegate if (OnSyncComplete != null) { OnSyncComplete(this, new EventArgs()); } }
// Synchronise the playlists defined in the settings file to Google Music public void SyncPlaylistsToGMusic(List <MbPlaylist> mbPlaylistsToSync) { _syncRunning = true; AutoResetEvent waitForEvent = new AutoResetEvent(false); if (_dataFetched) { // Get the MusicBee playlists foreach (MbPlaylist playlist in mbPlaylistsToSync) { // Use LINQ to check for a playlist with the same name // If there is one, clear it's contents, otherwise create one // Unless it's been deleted, in which case pretend it doesn't exist. // I'm not sure how to undelete a playlist, or even if you can GMusicPlaylist thisPlaylist = _allPlaylists.FirstOrDefault(p => p.Name == playlist.Name && p.Deleted == false); String thisPlaylistID = ""; if (thisPlaylist != null) { List <GMusicPlaylistEntry> allPlsSongs = thisPlaylist.Songs; // This simply signals the wait handle when done api.OnDeleteFromPlaylistComplete = delegate(MutatePlaylistResponse response) { waitForEvent.Set(); }; api.DeleteFromPlaylist(allPlsSongs); // Wait until the deletion is done waitForEvent.WaitOne(); thisPlaylistID = thisPlaylist.ID; } else { // Set the callback api.OnCreatePlaylistComplete = delegate(MutateResponse response) { thisPlaylistID = response.ID; waitForEvent.Set(); }; // Create the playlist api.CreatePlaylist(playlist.Name); // Wait until creation is done waitForEvent.WaitOne(); } // Create a list of files based on the MB Playlist string[] playlistFiles = null; if (_mbApiInterface.Playlist_QueryFiles(playlist.mbName)) { // Old method: // playlistFiles = _mbApiInterface.Playlist_QueryGetAllFiles().Split(filesSeparators, StringSplitOptions.RemoveEmptyEntries); bool success = _mbApiInterface.Playlist_QueryFilesEx(playlist.mbName, ref playlistFiles); if (!success) { throw new Exception("Couldn't get playlist files"); } } else { playlistFiles = new string[0]; } List <GMusicSong> songsToAdd = new List <GMusicSong>(); // And get the title and artist of each file, and add it to the GMusic playlist foreach (string file in playlistFiles) { string title = _mbApiInterface.Library_GetFileTag(file, Plugin.MetaDataType.TrackTitle); string artist = _mbApiInterface.Library_GetFileTag(file, Plugin.MetaDataType.Artist); GMusicSong gSong = _allSongs.FirstOrDefault(item => (item.Artist == artist && item.Title == title)); if (gSong != null) { songsToAdd.Add(gSong); } } api.OnAddToPlaylistComplete = delegate(MutatePlaylistResponse response) { waitForEvent.Set(); }; api.AddToPlaylist(thisPlaylistID, songsToAdd); waitForEvent.WaitOne(); } _syncRunning = false; // Signal to anyone calling that we're done if (OnSyncComplete != null) { OnSyncComplete(this, new EventArgs()); } } else { throw new Exception("Not fetched data yet"); } }