private void LoadPLFromFile(string path) { if (!File.Exists(path)) { return; } var pl = new PlayList(Path.GetFileNameWithoutExtension(path)); using (var reader = new StreamReader(path)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(','); pl.Items.Add(new MediaItem(values[0], values[1])); } } Catalog.Lists.Add(pl); }
private void DeletePLItem(object sender, EventArgs e) { var pl = new PlayList(null); if (PlaylistMediaLbl.Text != "Playlist media") { pl = Catalog.Lists.Where(l => l.Name == PlaylistMediaLbl.Text).FirstOrDefault(); } foreach (ListViewItem lvi in PlaylistMediaLV.SelectedItems) { PlaylistMediaLV.Items.Remove(lvi); pl?.Items.Remove(pl.Items.Where(i => i.Name == lvi.SubItems[0].Text).FirstOrDefault()); } TimeSpan totalDuration = default; foreach (ListViewItem item in PlaylistMediaLV.Items) { totalDuration += TimeSpan.Parse(item.SubItems[1].Text); } PlaylistMediaDurationLbl.Text = $"{(int)totalDuration.TotalHours}:{(int)totalDuration.Minutes}:{totalDuration.Seconds:00}"; LoadListViews(); IsToSave = true; }
private void SavePB_Click(object sender, EventArgs e) { Settings.DefPLs(); if (PlaylistMediaLbl.Text == "Playlist media" && PlaylistMediaLV.Items.Count != 0) { Form prompt = new Form() { Width = 380, Height = 100, FormBorderStyle = FormBorderStyle.FixedDialog, Text = "Create a new playlist", StartPosition = FormStartPosition.CenterScreen, MaximizeBox = false, MinimizeBox = false }; Label textLabel = new Label() { Left = 20, Top = 23, Text = "Name:" }; TextBox textBox = new TextBox() { Left = 55, Top = 20, Width = 190, MaxLength = 15 }; Button confirmation = new Button() { Text = "Ok", Left = 255, Width = 80, Top = 19, DialogResult = DialogResult.OK }; confirmation.Click += (s, a) => { if (string.IsNullOrEmpty(textBox.Text)) { MessageBox.Show("Name is required!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); prompt.DialogResult = DialogResult.Retry; } else { if (!Catalog.Lists.Select(x => x.Name).Contains(textBox.Text)) { var fileName = Settings.LocalPLFolder + "\\" + textBox.Text + ".csv"; //var fs = File.Create(fileName); //fs.Close(); var pl = new PlayList(textBox.Text); var sb = new StringBuilder(); foreach (ListViewItem item in PlaylistMediaLV.Items) { var mediaItem = new MediaItem(item.SubItems[0].Text, item.SubItems[1].Text); pl.Items.Add(mediaItem); sb.AppendLine(mediaItem.ToCSVLine()); } File.WriteAllText(fileName, sb.ToString()); Catalog.Lists.Add(pl); ListViewItem listViewItem = new ListViewItem(textBox.Text); listViewItem.Selected = true; listViewItem.Focused = true; LocalPLsLV.Items.Add(listViewItem); PlaylistMediaLbl.Text = textBox.Text; prompt.Close(); } else { MessageBox.Show(textBox.Text + " playlist already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); prompt.DialogResult = DialogResult.Retry; } } }; prompt.Controls.Add(textBox); prompt.Controls.Add(confirmation); prompt.Controls.Add(textLabel); prompt.AcceptButton = confirmation; var res = DialogResult.Retry; do { res = prompt.ShowDialog(); } while (res == DialogResult.Retry); if (res == DialogResult.OK) { UpdateAllMediaLVContext(); } else if (res == DialogResult.Cancel) { PlaylistMediaLV.Items.Clear(); } } // there are some pls to delete else if (PlaylistMediaLbl.Text == "Playlist media" && PlaylistMediaLV.Items.Count == 0) { foreach (var file in Directory.GetFiles(Settings.LocalPLFolder, "*.csv")) { if (!Catalog.Lists.Select(l => l.Name).Contains(file)) { GC.Collect(); GC.WaitForPendingFinalizers(); File.Delete(file); } else { var pl = Catalog.Lists.Where(l => l.Name == Path.GetFileNameWithoutExtension(file)).FirstOrDefault(); File.WriteAllText(file, string.Empty); var sb = new StringBuilder(); foreach (var item in pl.Items) { sb.AppendLine(item.ToCSVLine()); } File.WriteAllText(file, sb.ToString()); } } } else // it is an old playlist { WriteToFiles(); } UpdateAllMediaLVContext(); IsToSave = false; deletePB.Enabled = false; }