/* * Adds a media to the database */ private void AddMediaButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = fileTypeFilter; if (openFileDialog.ShowDialog() == true) { try { FileStream file = File.Open(openFileDialog.FileName, FileMode.Open); using (var db = new mainEntities()) { string fileExt = Path.GetExtension(file.Name).Remove(0, 1); string fileType = extensions[fileExt]; Medias newMedia = new Medias { Name = Path.GetFileNameWithoutExtension(file.Name), Ext = fileExt, ExtID = (from ext in db.MediaExtensions where ext.Extension == fileExt select ext).Single <MediaExtensions>().Id, Path = file.Name, Type = fileType, TypeID = (from type in db.MediaTypes where type.Name == fileType select type).Single <MediaTypes>().Id }; db.Medias.Add(newMedia); db.SaveChanges(); Populate(); file.Close(); db.Dispose(); } } catch (IOException) { } } }
/* * Loads saved medias from database */ private void Populate() { List <Medias> mediaList = new List <Medias>(); using (var db = new mainEntities()) { Display((from media in db.Medias select media).ToList()); db.Dispose(); } }
/* * Removes every medias from the database */ private void RemoveAllButton_Click(object sender, RoutedEventArgs e) { using (var db = new mainEntities()) { db.Medias.RemoveRange(from media in db.Medias select media); db.SaveChanges(); selectionList.Clear(); Populate(); db.Dispose(); } }
/* * Removes the selected medias from the database */ private void RemoveButton_Click(object sender, RoutedEventArgs e) { using (var db = new mainEntities()) { foreach (Medias m in selectionList) { db.Medias.RemoveRange(from media in db.Medias where media.Id == m.Id select media); } db.SaveChanges(); selectionList.Clear(); Populate(); db.Dispose(); } }
/* * Loads supported extensions from database */ private void LoadExtensions() { using (var db = new mainEntities()) { foreach (MediaTypes mediaType in ((from type in db.MediaTypes select type).ToList())) { var extString = ""; foreach (MediaExtensions mediaExt in ((from ext in db.MediaExtensions where ext.MediaTypeID == mediaType.Id select ext).ToList())) { extString += "*." + mediaExt.Extension + ";"; extensions.Add(mediaExt.Extension, mediaType.Name); } extString = extString.Remove(extString.Length - 1); fileTypeFilter += "|" + mediaType.Name + " files(" + extString + ")|" + extString; } if (fileTypeFilter.Length > 0) { fileTypeFilter = fileTypeFilter.Remove(0, 1); } db.Dispose(); } }