Main.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace MusicPlayer
  9. {
  10. public class Main
  11. {
  12. public APIHandler api;
  13. public MainForm form;
  14. public NetworkHandler nw;
  15. public PlaylistHandler pl;
  16. public AudioHandler audio;
  17. public SongsTable table;
  18. private List<string> genres;
  19. private List<string> artists;
  20. public List<Song> currentPlayingList;
  21. public Main(NetworkHandler nw, APIHandler api, MainForm form, PlaylistHandler pl)
  22. {
  23. this.nw = nw;
  24. this.api = api;
  25. this.form = form;
  26. form.main = this;
  27. pl.main = this;
  28. pl.Populate();
  29. this.pl = pl;
  30. audio = new AudioHandler(this);
  31. table = new SongsTable();
  32. form.SongsTableView.DataSource = table;
  33. form.SongsTableView.Columns[5].Visible = false;
  34. genres = new List<string>();
  35. artists = new List<string>();
  36. currentPlayingList = new List<Song>();
  37. Populate();
  38. }
  39. public void SwitchServer(string server)
  40. {
  41. Clear();
  42. nw.ip = server;
  43. Populate();
  44. }
  45. private void Clear()
  46. {
  47. form.GenreListBox.Items.Clear();
  48. form.ArtistListBox.Items.Clear();
  49. form.AlbumListView.Items.Clear();
  50. form.PlaylistBox.Items.Clear();
  51. table.Clear();
  52. genres = new List<string>();
  53. artists = new List<string>();
  54. currentPlayingList = new List<Song>();
  55. }
  56. private void Populate()
  57. {
  58. this.api.GetAlbums().ForEach(a => { form.AlbumListView.Items.Add(a.albumnaam);});
  59. this.api.GetArtists().ForEach(a => { artists.Add(a.naam); form.ArtistListBox.Items.Add(a.naam); });
  60. this.api.GetGenres().ForEach(g => { genres.Add(g.name); form.GenreListBox.Items.Add(g.name); });
  61. this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name));
  62. BackgroundWorker bw = new BackgroundWorker();
  63. bw.DoWork += new DoWorkEventHandler(
  64. delegate (object o, DoWorkEventArgs args)
  65. {
  66. BackgroundWorker b = o as BackgroundWorker;
  67. ImageList imagelist = new ImageList();
  68. List<string> templist = new List<string>();
  69. Action action = () =>
  70. {
  71. foreach (ListViewItem item in form.AlbumListView.Items)
  72. {
  73. templist.Add(item.Text);
  74. Console.WriteLine(item.Text);
  75. }
  76. };
  77. form.Invoke(action);
  78. foreach (string item in templist)
  79. {
  80. imagelist.Images.Add(item, api.getAlbumCover(item));
  81. }
  82. action = () => {
  83. form.AlbumListView.LargeImageList = imagelist;
  84. foreach (ListViewItem item in form.AlbumListView.Items)
  85. {
  86. item.ImageKey = item.Text;
  87. }
  88. };
  89. form.Invoke(action);
  90. });
  91. bw.RunWorkerAsync();
  92. }
  93. public void Repopulate()
  94. {
  95. form.AlbumListView.Items.Clear();
  96. form.ArtistListBox.Items.Clear();
  97. form.GenreListBox.Items.Clear();
  98. form.PlaylistBox.Items.Clear();
  99. this.api.GetAlbums().ForEach(a => form.AlbumListView.Items.Add(a.albumnaam));
  100. this.api.GetArtists().ForEach(a => form.ArtistListBox.Items.Add(a.naam));
  101. this.api.GetGenres().ForEach(g => form.GenreListBox.Items.Add(g.name));
  102. this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name));
  103. }
  104. public void ArtistFilter(string artist)
  105. {
  106. table.Clear();
  107. api.GetSongsByArtist(artist).ForEach(s =>
  108. {
  109. table.Add(s);
  110. });
  111. }
  112. public void SearchFilter(string search)
  113. {
  114. table.Clear();
  115. api.GetSongsBySearch(search).ForEach(s =>
  116. {
  117. table.Add(s);
  118. });
  119. }
  120. public void FilterCurrentPlaying()
  121. {
  122. table.Clear();
  123. currentPlayingList.ForEach(s =>
  124. {
  125. table.Add(s);
  126. });
  127. form.GenreListBox.ClearSelected();
  128. form.ArtistListBox.ClearSelected();
  129. form.PlaylistBox.ClearSelected();
  130. form.AlbumListView.SelectedIndices.Clear();
  131. }
  132. public void SearchArtist(string search)
  133. {
  134. form.ArtistListBox.Items.Clear();
  135. if (search.Length > 1)
  136. {
  137. string sPattern = search;
  138. foreach (string s in artists)
  139. {
  140. if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
  141. {
  142. form.ArtistListBox.Items.Add(s);
  143. }
  144. }
  145. }
  146. else
  147. {
  148. artists.ForEach(a => form.ArtistListBox.Items.Add(a));
  149. }
  150. }
  151. public void SearchGenre(string search)
  152. {
  153. form.GenreListBox.Items.Clear();
  154. if (search.Length > 1)
  155. {
  156. string sPattern = search;
  157. foreach (string s in genres)
  158. {
  159. if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
  160. {
  161. form.GenreListBox.Items.Add(s);
  162. }
  163. }
  164. }
  165. else
  166. {
  167. genres.ForEach(g => form.GenreListBox.Items.Add(g));
  168. }
  169. }
  170. public void GenreFilter(string genre)
  171. {
  172. table.Clear();
  173. api.GetSongsByGenre(genre).ForEach(s =>
  174. {
  175. table.Add(s);
  176. });
  177. }
  178. public void PlaylistFilter(string name)
  179. {
  180. table.Clear();
  181. pl.GetPlaylistByName(name).GetSongs().ForEach(s => table.Add(s));
  182. }
  183. public void AlbumFilter(string album)
  184. {
  185. table.Clear();
  186. api.GetSongsByAlbum(album).ForEach(s =>
  187. {
  188. table.Add(s);
  189. });
  190. }
  191. public static string SecondsToTimestamp(int seconds)
  192. {
  193. string str = "";
  194. //Hours
  195. str += (seconds / 3600).ToString("D2") + ":";
  196. seconds %= 3600;
  197. //Minutes
  198. str += (seconds / 60).ToString("D2") + ":";
  199. seconds %= 60;
  200. //Seconds
  201. str += seconds.ToString("D2");
  202. return str;
  203. }
  204. public static bool CheckURLValid(string source)
  205. {
  206. Uri uriResult;
  207. return Uri.TryCreate(source, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
  208. }
  209. public static string GetDomain(string url)
  210. {
  211. Uri uri = new Uri(url);
  212. return uri.Host;
  213. }
  214. }
  215. }