public void UpdateCurrentPlaylist() { List <KeyValuePair <string, string> > result; if (PreviousStatus == null) { result = s.SendCommand("playlistinfo"); } else { Console.WriteLine("Playlist updated! old_version is " + PreviousStatus.PlaylistVersion); Console.WriteLine("Length diff: {0} -> {1}", PreviousStatus.PlaylistLength, CurrentStatus.PlaylistLength); //result = SendCommand("plchanges", old_status.Playlist.ToString()); int new_pos = -1; foreach (var item in s.SendCommand("plchangesposid", PreviousStatus.PlaylistVersion.ToString())) { if (item.Key == "cpos") { new_pos = int.Parse(item.Value); } else { int id = int.Parse(item.Value); int old_pos = -1; for (int i = 0; i < CurrentPlaylist.Count; i++) { if (CurrentPlaylist[i].ID == id) { old_pos = i; var tmp = CurrentPlaylist[new_pos]; CurrentPlaylist[new_pos] = CurrentPlaylist[old_pos]; CurrentPlaylist[old_pos] = tmp; break; } } if (old_pos == -1) { var data = s.SendCommand("playlistinfo", new_pos.ToString()); CurrentPlaylist.Insert(new_pos, new PlaylistEntry(data)); } Console.WriteLine("Item: old_pos: {0}; cpos: {1}; id: {2}", old_pos, new_pos, id); } } if (CurrentStatus.PlaylistLength < PreviousStatus.PlaylistLength) { while (CurrentPlaylist.Count > CurrentStatus.PlaylistLength) { CurrentPlaylist.RemoveAt(CurrentPlaylist.Count - 1); } Console.WriteLine("Remove end"); } result = null; } if (result != null) { PlaylistEntry entry; var buffer = new List <KeyValuePair <string, string> >(); foreach (var item in result) { if (buffer.Count > 0 && item.Key == "file") { entry = new PlaylistEntry(buffer); if (entry.Pos >= 0) { CurrentPlaylist.Insert(entry.Pos, entry); } else { CurrentPlaylist.Add(entry); } buffer.Clear(); } buffer.Add(item); } if (buffer.Count > 0) { entry = new PlaylistEntry(buffer); if (entry.Pos >= 0) { CurrentPlaylist.Insert(entry.Pos, entry); } else { CurrentPlaylist.Add(entry); } } } }
public void UpdateCurrentPlaylist() { List<KeyValuePair<string, string>> result; if (PreviousStatus == null) { result = s.SendCommand("playlistinfo"); } else { Console.WriteLine("Playlist updated! old_version is " + PreviousStatus.PlaylistVersion); Console.WriteLine("Length diff: {0} -> {1}", PreviousStatus.PlaylistLength, CurrentStatus.PlaylistLength); //result = SendCommand("plchanges", old_status.Playlist.ToString()); int new_pos = -1; foreach (var item in s.SendCommand("plchangesposid", PreviousStatus.PlaylistVersion.ToString())) { if (item.Key == "cpos") new_pos = int.Parse(item.Value); else { int id = int.Parse(item.Value); int old_pos = -1; for (int i = 0; i < CurrentPlaylist.Count; i++) { if (CurrentPlaylist[i].ID == id) { old_pos = i; var tmp = CurrentPlaylist[new_pos]; CurrentPlaylist[new_pos] = CurrentPlaylist[old_pos]; CurrentPlaylist[old_pos] = tmp; break; } } if (old_pos == -1) { var data = s.SendCommand("playlistinfo", new_pos.ToString()); CurrentPlaylist.Insert(new_pos, new PlaylistEntry(data)); } Console.WriteLine("Item: old_pos: {0}; cpos: {1}; id: {2}", old_pos, new_pos, id); } } if (CurrentStatus.PlaylistLength < PreviousStatus.PlaylistLength) { while (CurrentPlaylist.Count > CurrentStatus.PlaylistLength) { CurrentPlaylist.RemoveAt(CurrentPlaylist.Count - 1); } Console.WriteLine("Remove end"); } result = null; } if (result != null) { PlaylistEntry entry; var buffer = new List<KeyValuePair<string, string>>(); foreach (var item in result) { if (buffer.Count > 0 && item.Key == "file") { entry = new PlaylistEntry(buffer); if (entry.Pos >= 0) CurrentPlaylist.Insert(entry.Pos, entry); else CurrentPlaylist.Add(entry); buffer.Clear(); } buffer.Add(item); } if (buffer.Count > 0) { entry = new PlaylistEntry(buffer); if (entry.Pos >= 0) CurrentPlaylist.Insert(entry.Pos, entry); else CurrentPlaylist.Add(entry); } } }