Main.cs 8.4 KB

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