diff --git a/MusicPlayer/MusicPlayer/APIHandler.cs b/MusicPlayer/MusicPlayer/APIHandler.cs index af89f97..88202d7 100755 --- a/MusicPlayer/MusicPlayer/APIHandler.cs +++ b/MusicPlayer/MusicPlayer/APIHandler.cs @@ -46,6 +46,22 @@ namespace MusicPlayer return GetSongsByArgs("album=" + year); } + public List GetAllSongs() + { + List allsongslist = new List(); + JObject o = nw.SendString("getallsongs?"); + if (o["result"].ToString() == "OK") + { + dynamic songs = o["songs"]; + for (int i = 0; i < songs.Count; i++) + { + allsongslist.Add(new Song(songs[i][0].ToString(), songs[i][3].ToString(), songs[i][5].ToString(), songs[i][4].ToString(), songs[i][1].ToString(), (int)songs[i][9], this)); + } + } + return allsongslist; + + } + public List GetSongsByArgs(string args) { List songslist = new List(); diff --git a/MusicPlayer/MusicPlayer/Main.cs b/MusicPlayer/MusicPlayer/Main.cs index c1fc010..f2c2989 100755 --- a/MusicPlayer/MusicPlayer/Main.cs +++ b/MusicPlayer/MusicPlayer/Main.cs @@ -11,16 +11,18 @@ namespace MusicPlayer public APIHandler api; public MainForm form; public NetworkHandler nw; + public PlaylistHandler pl; public AudioHandler audio; private SongsTable table; - public Main(NetworkHandler nw, APIHandler api, MainForm form) + public Main(NetworkHandler nw, APIHandler api, MainForm form, PlaylistHandler pl) { this.nw = nw; this.api = api; this.form = form; form.main = this; + this.pl = pl; audio = new AudioHandler(); table = new SongsTable(); @@ -34,6 +36,19 @@ namespace MusicPlayer this.api.GetAlbums().ForEach(a => form.AlbumListView.Items.Add(a.albumnaam)); this.api.GetArtists().ForEach(a => form.ArtistListBox.Items.Add(a.naam)); this.api.GetGenres().ForEach(g => form.GenreListBox.Items.Add(g.name)); + this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name)); + } + + public void Repopulate() + { + form.AlbumListView.Items.Clear(); + form.ArtistListBox.Items.Clear(); + form.GenreListBox.Items.Clear(); + form.PlaylistBox.Items.Clear(); + this.api.GetAlbums().ForEach(a => form.AlbumListView.Items.Add(a.albumnaam)); + this.api.GetArtists().ForEach(a => form.ArtistListBox.Items.Add(a.naam)); + this.api.GetGenres().ForEach(g => form.GenreListBox.Items.Add(g.name)); + this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name)); } public void ArtistFilter(string artist) @@ -54,6 +69,13 @@ namespace MusicPlayer }); } + public void PlaylistFilter(string name) + { + table.Clear(); + pl.GetPlaylistByName(name).GetSongs().ForEach(s => table.Add(s)); + + } + public void AlbumFilter(string album) { table.Clear(); diff --git a/MusicPlayer/MusicPlayer/MainForm.Designer.cs b/MusicPlayer/MusicPlayer/MainForm.Designer.cs index 221b0d9..0c2fe88 100644 --- a/MusicPlayer/MusicPlayer/MainForm.Designer.cs +++ b/MusicPlayer/MusicPlayer/MainForm.Designer.cs @@ -51,6 +51,7 @@ this.playlistToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.loadToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.makeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.ControlsPanel = new System.Windows.Forms.Panel(); this.CurrentSongLabel = new System.Windows.Forms.Label(); this.SeperatorLabel = new System.Windows.Forms.Label(); @@ -195,6 +196,7 @@ this.PlaylistBox.Size = new System.Drawing.Size(760, 131); this.PlaylistBox.TabIndex = 4; this.PlaylistBox.Visible = false; + this.PlaylistBox.SelectedIndexChanged += new System.EventHandler(this.PlaylistBox_SelectedIndexChanged); // // PlaylistListLabel // @@ -294,7 +296,8 @@ // this.playlistToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.saveToolStripMenuItem1, - this.loadToolStripMenuItem}); + this.loadToolStripMenuItem, + this.makeToolStripMenuItem}); this.playlistToolStripMenuItem.Name = "playlistToolStripMenuItem"; this.playlistToolStripMenuItem.Size = new System.Drawing.Size(56, 20); this.playlistToolStripMenuItem.Text = "Playlist"; @@ -302,15 +305,22 @@ // saveToolStripMenuItem1 // this.saveToolStripMenuItem1.Name = "saveToolStripMenuItem1"; - this.saveToolStripMenuItem1.Size = new System.Drawing.Size(100, 22); + this.saveToolStripMenuItem1.Size = new System.Drawing.Size(103, 22); this.saveToolStripMenuItem1.Text = "Save"; // // loadToolStripMenuItem // this.loadToolStripMenuItem.Name = "loadToolStripMenuItem"; - this.loadToolStripMenuItem.Size = new System.Drawing.Size(100, 22); + this.loadToolStripMenuItem.Size = new System.Drawing.Size(103, 22); this.loadToolStripMenuItem.Text = "Load"; // + // makeToolStripMenuItem + // + this.makeToolStripMenuItem.Name = "makeToolStripMenuItem"; + this.makeToolStripMenuItem.Size = new System.Drawing.Size(103, 22); + this.makeToolStripMenuItem.Text = "Make"; + this.makeToolStripMenuItem.Click += new System.EventHandler(this.makeToolStripMenuItem_Click); + // // ControlsPanel // this.ControlsPanel.BackColor = System.Drawing.SystemColors.WindowFrame; @@ -558,7 +568,7 @@ private System.Windows.Forms.Label GenreListLabel; private System.Windows.Forms.ToolStripMenuItem playlistsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem overviewToolStripMenuItem; - private System.Windows.Forms.ListBox PlaylistBox; + public System.Windows.Forms.ListBox PlaylistBox; private System.Windows.Forms.Label PlaylistListLabel; private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem playlistToolStripMenuItem; @@ -573,6 +583,7 @@ private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; private System.Windows.Forms.ToolStripMenuItem NotifyMenuStripPlayingSongLabel; private System.Windows.Forms.SplitContainer SplitContainer; + private System.Windows.Forms.ToolStripMenuItem makeToolStripMenuItem; } } diff --git a/MusicPlayer/MusicPlayer/MainForm.cs b/MusicPlayer/MusicPlayer/MainForm.cs index c906936..dd114e0 100644 --- a/MusicPlayer/MusicPlayer/MainForm.cs +++ b/MusicPlayer/MusicPlayer/MainForm.cs @@ -147,6 +147,18 @@ namespace MusicPlayer { main.GenreFilter(GenreListBox.SelectedItems[0].ToString()); ArtistListBox.ClearSelected(); + PlaylistBox.ClearSelected(); + AlbumListView.SelectedIndices.Clear(); + } + } + + private void PlaylistBox_SelectedIndexChanged(object sender, EventArgs e) + { + if (PlaylistBox.SelectedItems.Count != 0) + { + main.PlaylistFilter(PlaylistBox.SelectedItems[0].ToString()); + GenreListBox.ClearSelected(); + ArtistListBox.ClearSelected(); AlbumListView.SelectedIndices.Clear(); } } @@ -157,6 +169,7 @@ namespace MusicPlayer { main.ArtistFilter(ArtistListBox.SelectedItems[0].ToString()); GenreListBox.ClearSelected(); + PlaylistBox.ClearSelected(); AlbumListView.SelectedIndices.Clear(); } } @@ -166,6 +179,7 @@ namespace MusicPlayer if (AlbumListView.SelectedItems.Count != 0) { main.AlbumFilter(AlbumListView.SelectedItems[0].Text); + PlaylistBox.ClearSelected(); ArtistListBox.ClearSelected(); GenreListBox.ClearSelected(); } @@ -264,5 +278,12 @@ namespace MusicPlayer { StopButton_Click(sender, e); } + + private void makeToolStripMenuItem_Click(object sender, EventArgs e) + { + new PlaylistMaker(main.pl, main.api).Show(); + + + } } } diff --git a/MusicPlayer/MusicPlayer/MusicPlayer.csproj b/MusicPlayer/MusicPlayer/MusicPlayer.csproj index 0f3f9af..a2dc57f 100755 --- a/MusicPlayer/MusicPlayer/MusicPlayer.csproj +++ b/MusicPlayer/MusicPlayer/MusicPlayer.csproj @@ -73,6 +73,14 @@ NotificationPopup.cs + + + + Form + + + PlaylistMaker.cs + @@ -86,6 +94,9 @@ NotificationPopup.cs + + PlaylistMaker.cs + ResXFileCodeGenerator Resources.Designer.cs diff --git a/MusicPlayer/MusicPlayer/Playlist.cs b/MusicPlayer/MusicPlayer/Playlist.cs new file mode 100644 index 0000000..97665dd --- /dev/null +++ b/MusicPlayer/MusicPlayer/Playlist.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace MusicPlayer +{ + public class Playlist + { + public string name { get; } + private string basedir; + public List songs; + + private APIHandler api; + public Playlist(string name, string basedir, APIHandler api) + { + this.songs = new List(); + this.name = name; + this.api = api; + this.basedir = basedir; + this.ReadFromFile(); + } + + public void AddSong(Song s) + { + this.songs.Add(s); + } + + public List GetSongs() + { + return songs; + } + + public void ReadFromFile() + { + try { + using (StreamReader str = new StreamReader(basedir + name + ".txt")) + { + string readline; + while ((readline = str.ReadLine()) != null) + { + string[] songsvalues = readline.Split('|'); + + songs.Add(new Song(songsvalues[0], songsvalues[1], songsvalues[2], songsvalues[3], songsvalues[4], Int32.Parse(songsvalues[5]), api)); + } + } + } + catch (FileNotFoundException) + { + FileStream fs = new FileStream(basedir + name + ".txt", FileMode.CreateNew); + fs.Close(); + ReadFromFile(); + } + + } + + public void WriteToFile() + { + using (StreamWriter stw = new StreamWriter(basedir + name + ".txt")) + { + this.songs.ForEach(s => + { + stw.WriteLine(s.ToString()); + }); + stw.Flush(); + } + } + } +} \ No newline at end of file diff --git a/MusicPlayer/MusicPlayer/PlaylistHandler.cs b/MusicPlayer/MusicPlayer/PlaylistHandler.cs new file mode 100644 index 0000000..b2acf9c --- /dev/null +++ b/MusicPlayer/MusicPlayer/PlaylistHandler.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace MusicPlayer +{ + public class PlaylistHandler + { + private List playlists; + private APIHandler api; + private readonly string basedir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\.mpplaylists\\"; + public PlaylistHandler(APIHandler api) + { + this.playlists = new List(); + this.api = api; + Populate(); + } + + private void Populate() + { + try { + Directory.GetFiles(basedir).ToList().ForEach(f => + { + if (f.EndsWith(".txt")) + { + playlists.Add(new Playlist(Path.GetFileName(f.Replace(".txt","")) ,basedir, api)); + } + }); + } + catch (DirectoryNotFoundException) + { + Directory.CreateDirectory(basedir); + Populate(); + } + + + } + + public void MakeNewPlaylistByName(string name) + { + playlists.Add(new Playlist(name, basedir, api)); + } + + public Playlist GetPlaylistByName(string name) + { + Playlist toFind = null; + playlists.ForEach(p => + { + if (p.name == name) { toFind = p; } + }); + return toFind; + } + + public List GetPlaylists() + { + return playlists; + } + } +} \ No newline at end of file diff --git a/MusicPlayer/MusicPlayer/PlaylistMaker.Designer.cs b/MusicPlayer/MusicPlayer/PlaylistMaker.Designer.cs new file mode 100644 index 0000000..dbfd640 --- /dev/null +++ b/MusicPlayer/MusicPlayer/PlaylistMaker.Designer.cs @@ -0,0 +1,121 @@ +using System.Windows.Forms; + +namespace MusicPlayer +{ + partial class PlaylistMaker + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.PlaylistSelectBox = new System.Windows.Forms.ComboBox(); + this.PlaylistSongSelector = new System.Windows.Forms.ListBox(); + this.PlaylistAddSongsButton = new System.Windows.Forms.Button(); + this.PlaylistSongContainer = new System.Windows.Forms.ListBox(); + this.PlaylistNewButton = new System.Windows.Forms.Button(); + this.PlaylistNewInputfield = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // PlaylistSelectBox + // + this.PlaylistSelectBox.FormattingEnabled = true; + this.PlaylistSelectBox.Location = new System.Drawing.Point(12, 12); + this.PlaylistSelectBox.Name = "PlaylistSelectBox"; + this.PlaylistSelectBox.Size = new System.Drawing.Size(354, 21); + this.PlaylistSelectBox.TabIndex = 0; + this.PlaylistSelectBox.SelectedIndexChanged += new System.EventHandler(this.PlaylistSelectBox_SelectedIndexChanged); + // + // PlaylistSongSelector + // + this.PlaylistSongSelector.FormattingEnabled = true; + this.PlaylistSongSelector.Location = new System.Drawing.Point(13, 39); + this.PlaylistSongSelector.Name = "PlaylistSongSelector"; + this.PlaylistSongSelector.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; + this.PlaylistSongSelector.Size = new System.Drawing.Size(353, 160); + this.PlaylistSongSelector.TabIndex = 1; + // + // PlaylistAddSongsButton + // + this.PlaylistAddSongsButton.Location = new System.Drawing.Point(12, 293); + this.PlaylistAddSongsButton.Name = "PlaylistAddSongsButton"; + this.PlaylistAddSongsButton.Size = new System.Drawing.Size(354, 23); + this.PlaylistAddSongsButton.TabIndex = 2; + this.PlaylistAddSongsButton.Text = "Add"; + this.PlaylistAddSongsButton.UseVisualStyleBackColor = true; + this.PlaylistAddSongsButton.Click += new System.EventHandler(this.PlaylistAddSongsButton_Click); + // + // PlaylistSongContainer + // + this.PlaylistSongContainer.FormattingEnabled = true; + this.PlaylistSongContainer.Location = new System.Drawing.Point(12, 205); + this.PlaylistSongContainer.Name = "PlaylistSongContainer"; + this.PlaylistSongContainer.Size = new System.Drawing.Size(354, 82); + this.PlaylistSongContainer.TabIndex = 3; + // + // PlaylistNewButton + // + this.PlaylistNewButton.Location = new System.Drawing.Point(118, 342); + this.PlaylistNewButton.Name = "PlaylistNewButton"; + this.PlaylistNewButton.Size = new System.Drawing.Size(75, 23); + this.PlaylistNewButton.TabIndex = 4; + this.PlaylistNewButton.Text = "New"; + this.PlaylistNewButton.UseVisualStyleBackColor = true; + this.PlaylistNewButton.Click += new System.EventHandler(this.PlaylistNewButton_Click); + // + // PlaylistNewInputfield + // + this.PlaylistNewInputfield.Location = new System.Drawing.Point(12, 342); + this.PlaylistNewInputfield.Name = "PlaylistNewInputfield"; + this.PlaylistNewInputfield.Size = new System.Drawing.Size(100, 20); + this.PlaylistNewInputfield.TabIndex = 5; + // + // PlaylistMaker + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(382, 400); + this.Controls.Add(this.PlaylistNewInputfield); + this.Controls.Add(this.PlaylistNewButton); + this.Controls.Add(this.PlaylistSongContainer); + this.Controls.Add(this.PlaylistAddSongsButton); + this.Controls.Add(this.PlaylistSongSelector); + this.Controls.Add(this.PlaylistSelectBox); + this.Name = "PlaylistMaker"; + this.Text = "PlaylistMaker"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.ComboBox PlaylistSelectBox; + private System.Windows.Forms.ListBox PlaylistSongSelector; + private System.Windows.Forms.Button PlaylistAddSongsButton; + private System.Windows.Forms.ListBox PlaylistSongContainer; + private Button PlaylistNewButton; + private TextBox PlaylistNewInputfield; + } +} \ No newline at end of file diff --git a/MusicPlayer/MusicPlayer/PlaylistMaker.cs b/MusicPlayer/MusicPlayer/PlaylistMaker.cs new file mode 100644 index 0000000..9b25b97 --- /dev/null +++ b/MusicPlayer/MusicPlayer/PlaylistMaker.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace MusicPlayer +{ + public partial class PlaylistMaker : Form + { + private PlaylistHandler pl; + private APIHandler api; + + private List allPlaylistSongs; + private List allsongs; + + public PlaylistMaker(PlaylistHandler pl, APIHandler api) + { + this.pl = pl; + this.api = api; + this.allPlaylistSongs = new List(); + this.allsongs = new List(); + InitializeComponent(); + Populate(); + } + + public void PlaylistSelectBox_SelectedIndexChanged(object sender, EventArgs e) + { + if (PlaylistSelectBox.SelectedItem != null) + { + PlaylistSongContainer.Items.Clear(); + allPlaylistSongs = pl.GetPlaylistByName(PlaylistSelectBox.SelectedItem.ToString()).GetSongs(); + allPlaylistSongs.ForEach(s => PlaylistSongContainer.Items.Add(s.Name)); + } + } + + public void Repopulate() + { + PlaylistSelectBox.Items.Clear(); + pl.GetPlaylists().ForEach(p => PlaylistSelectBox.Items.Add(p.name)); + if (PlaylistSelectBox.SelectedItem != null) + { + PlaylistSongContainer.Items.Clear(); + allPlaylistSongs = pl.GetPlaylistByName(PlaylistSelectBox.SelectedItem.ToString()).GetSongs(); + allPlaylistSongs.ForEach(s => PlaylistSongContainer.Items.Add(s.Name)); + } + } + + public void Populate() + { + pl.GetPlaylists().ForEach(p => PlaylistSelectBox.Items.Add(p.name)); + allsongs = api.GetAllSongs(); + allsongs.ForEach(s => PlaylistSongSelector.Items.Add(s.Name)); + } + + private void PlaylistAddSongsButton_Click(object sender, EventArgs e) + { + if (PlaylistSelectBox.SelectedItem!=null) + { + Playlist currentPlaylist = pl.GetPlaylistByName(PlaylistSelectBox.SelectedItem.ToString()); + foreach (string song in PlaylistSongSelector.SelectedItems) + { + foreach (Song s in allsongs) { + if (s.Name == song) + { + currentPlaylist.AddSong(s); + break; + } + } + } + currentPlaylist.WriteToFile(); + } + Repopulate(); + } + + private void PlaylistNewButton_Click(object sender, EventArgs e) + { + pl.MakeNewPlaylistByName(PlaylistNewInputfield.Text); + Repopulate(); + } + } +} diff --git a/MusicPlayer/MusicPlayer/PlaylistMaker.resx b/MusicPlayer/MusicPlayer/PlaylistMaker.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/MusicPlayer/MusicPlayer/PlaylistMaker.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/MusicPlayer/MusicPlayer/Program.cs b/MusicPlayer/MusicPlayer/Program.cs index 8e609c8..4b3b523 100755 --- a/MusicPlayer/MusicPlayer/Program.cs +++ b/MusicPlayer/MusicPlayer/Program.cs @@ -19,24 +19,9 @@ namespace MusicPlayer NetworkHandler nw = new NetworkHandler("http://www.imegumii.nl"); APIHandler api = new APIHandler(nw); -// api.GetSongsByArtist("Amon Amarth").ForEach(s => -// { -// Console.WriteLine(s.SongID); -// }); -// api.GetSongsByYear("2009").ForEach(s => -// { -// Console.WriteLine(s.Name); -// }); -// api.GetSongsByGenre("Melodic Death Metal").ForEach(s => -// { -// Console.WriteLine(s.Name); -// }); -// api.GetSongsByAlbum("Stronger").ForEach(s => -// { -// Console.WriteLine(s.Name); -// }); MainForm form = new MainForm(); - new Main(nw, api, form); + PlaylistHandler pl = new PlaylistHandler(api); + new Main(nw, api, form,pl); Application.Run(form); } diff --git a/MusicPlayer/MusicPlayer/Song.cs b/MusicPlayer/MusicPlayer/Song.cs index 18a3bf4..d698b3f 100755 --- a/MusicPlayer/MusicPlayer/Song.cs +++ b/MusicPlayer/MusicPlayer/Song.cs @@ -48,5 +48,10 @@ namespace MusicPlayer { url = str; } + + public override string ToString() + { + return $"{this.SongID}|{this.Name}|{this.Album}|{this.Artist}|{this.Genre}|{this.Seconds}"; + } } }