PlaylistHandler.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace MusicPlayer
  6. {
  7. public class PlaylistHandler
  8. {
  9. private List<Playlist> playlists;
  10. public Main main
  11. {
  12. get; set;
  13. }
  14. private readonly string basedir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\.mpplaylists\\";
  15. public PlaylistHandler()
  16. {
  17. this.playlists = new List<Playlist>();
  18. }
  19. public void Populate()
  20. {
  21. playlists.Clear();
  22. try {
  23. Directory.GetFiles(basedir).ToList().ForEach(f =>
  24. {
  25. if (f.EndsWith(".txt"))
  26. {
  27. playlists.Add(new Playlist(Path.GetFileName(f.Replace(".txt","")) ,basedir, main.nw.ip, main.api));
  28. }
  29. });
  30. }
  31. catch (DirectoryNotFoundException)
  32. {
  33. Directory.CreateDirectory(basedir);
  34. Populate();
  35. }
  36. }
  37. public void MakeNewPlaylistByName(string name)
  38. {
  39. playlists.Add(new Playlist(name, basedir, main.nw.ip, main.api));
  40. }
  41. public Playlist GetPlaylistByName(string name)
  42. {
  43. Playlist toFind = null;
  44. playlists.ForEach(p =>
  45. {
  46. if (p.name == name) { toFind = p; }
  47. });
  48. return toFind;
  49. }
  50. public List<Playlist> GetPlaylists()
  51. {
  52. List<Playlist> currentPlaylists = new List<Playlist>();
  53. foreach(Playlist pl in playlists)
  54. {
  55. if (pl.server == main.nw.ip)
  56. currentPlaylists.Add(pl);
  57. }
  58. return currentPlaylists;
  59. }
  60. internal void RemovePlaylistByName(string name)
  61. {
  62. Playlist toRemove = null;
  63. playlists.ForEach(p =>
  64. {
  65. if (p.name == name) { toRemove = p; }
  66. });
  67. if(toRemove != null)
  68. {
  69. toRemove.Delete();
  70. playlists.Remove(toRemove);
  71. }
  72. }
  73. }
  74. }