private void renameButton_Click(object sender, EventArgs e) { try { NamePaser paser = new NamePaser(); paser.Address = Path.GetDirectoryName(openFileDialog.FileName); paser.Ext = Path.GetExtension(openFileDialog.FileName); paser.Speaker = this.speakerTextBox.Text; paser.Category = this.categoryTextBox.Text; paser.Word = this.wordTextBox.Text; paser.Label = this.labelTextBox.Text; File.Move(openFileDialog.FileName, paser.SingleFile); if (File.Exists(paser.SingleFile)) { this.filenameTextBox.Text = paser.FullName; this.openFileDialog.FileName = paser.SingleFile; } } catch (Exception exp) { if (exp.GetType() == typeof(FileNotFoundException)) { MessageBox.Show(exp.Message, "No such file!"); } else if (exp.GetType() == typeof(IOException)) { MessageBox.Show(exp.Message, "Destination file already exists!"); } } }
private void toLocalButton_Click(object sender, EventArgs e) { try { var DBContext = MainForm.self.DBModel; for (int i = onDBListBox.SelectedItems.Count - 1; i >= 0; i--) { SingleFile sf = onDBListBox.SelectedItems[i] as MPAid.Models.SingleFile; Recording rd = null; NamePaser paser = new NamePaser(); paser.FullName = sf.Name; if (paser.MediaFormat == "audio") { rd = sf.Audio; } else if (paser.MediaFormat == "video") { rd = sf.Video; } Speaker spk = rd.Speaker; Word word = rd.Word; Category cty = word.Category; string existingFile = sf.Address + "\\" + sf.Name; if (File.Exists(existingFile)) { File.Delete(existingFile); } DBContext.SingleFile.Remove(sf); if (rd.Audios.Count == 0 && rd.Video == null) { DBContext.Recording.Remove(rd); } if (spk.Recordings.Count == 0) { DBContext.Speaker.Remove(spk); } if (word.Recordings.Count == 0) { DBContext.Word.Remove(word); } if (cty.Words.Count == 0) { DBContext.Category.Remove(cty); } } MainForm.self.DBModel.SaveChanges(); } catch (Exception exp) { MessageBox.Show(exp.Message, "Fail to delete!"); } }
private void toDBButton_Click(object sender, EventArgs e) { try { var DBContext = MainForm.self.DBModel; foreach (KeyValuePair <string, string> item in mediaLocalListBox.SelectedItems) { String filename = item.Key.ToString(); NamePaser paser = new NamePaser(); paser.FullName = filename; if (paser.MediaFormat == "audio") { paser.Address = Properties.Settings.Default.AudioFolder; } else if (paser.MediaFormat == "video") { paser.Address = Properties.Settings.Default.VideoFolder; } DBContext.AddOrUpdateRecordingFile(paser.SingleFile); string existingFile = item.Value + "\\" + item.Key; string newFile = paser.Address + "\\" + item.Key; //avoid writing itslef if (!existingFile.Equals(newFile)) { File.Copy(existingFile, newFile, true); } } } catch (Exception exp) { Console.WriteLine(exp); MessageBox.Show("Fail to update!"); } }
public void AddOrUpdateRecordingFile(String recordingFile) { try { NamePaser paser = new NamePaser(); paser.SingleFile = recordingFile; Speaker spk = this.Speaker.SingleOrDefault(x => x.Name == paser.Speaker); if (spk == null) { spk = new Speaker() { Name = paser.Speaker }; this.Speaker.AddOrUpdate(x => x.Name, spk); this.SaveChanges(); } Category cty = this.Category.SingleOrDefault(x => x.Name == paser.Category); if (cty == null) { cty = new Category() { Name = paser.Category }; this.Category.AddOrUpdate(x => x.Name, cty); this.SaveChanges(); } Word word = this.Word.SingleOrDefault(x => x.Name == paser.Word); if (word == null) { word = new Word() { Name = paser.Word, CategoryId = cty.CategoryId }; this.Word.AddOrUpdate(x => x.Name, word); this.SaveChanges(); } Recording rd = this.Recording.SingleOrDefault(x => x.Name == paser.Recording);; if (rd == null) { rd = new Recording() { Name = paser.Recording, SpeakerId = spk.SpeakerId, WordId = word.WordId }; this.Recording.AddOrUpdate(x => x.Name, rd); this.SaveChanges(); } SingleFile sf = this.SingleFile.SingleOrDefault(x => x.Name == paser.FullName); if (sf == null) { sf = new SingleFile() { Name = paser.FullName, Address = paser.Address, }; if (paser.MediaFormat == "audio") { sf.Audio = rd; } else if (paser.MediaFormat == "video") { sf.Video = rd; } this.SingleFile.AddOrUpdate(x => x.Name, sf); this.SaveChanges(); } } catch (Exception exp) { Console.WriteLine(exp); } }