public static TrackViewModel AddFile(string file) { try { var d = new TrackViewModel(); //Build tags TrackViewModel track = TaglibCore.BuildTrack(file); try { //Save Track track.SaveTrack(); } catch (Exception exception) { Console.WriteLine(exception); } //============== //Artists //============== //Do the operations for artists ArtistViewModel art = ArtistQuery.GetArtistViewModel(track.Artist); art = art ?? new ArtistViewModel(); art.Name = track.Artist; art.Duration += track.Duration; art.SongCount++; art.SaveArtist(); //============== //Genre //============== ViewModels.GenreViewModel gens = GenreQuery.GetGenreViewModel(track.Genre); gens = gens ?? new ViewModels.GenreViewModel(); gens.Name = track.Genre; gens.Duration += track.Duration; gens.SongCount++; gens.SaveGenre(); //============== //Albums //============== AlbumViewModel albs = AlbumQuery.GetAlbumViewModel(track.Album); albs = albs ?? new AlbumViewModel(); albs.Name = track.Album; albs.Artist = track.Artist; albs.Duration += track.Duration; albs.SongCount++; albs.ID = track.Album; albs.SaveAlbum(); if (track.Album.ToLower() != "unknown album") { //Global.SaveAlbumArt(album + " - " + Artists, tgl.getArtStream()); //opt.SaveThumb("My Library\\AlbumArt\\" + album + " - " + Artists + ".jpg.thumb"); } Tracks.Add(track); Artists.Add(art); Albums.Add(albs); Genres.Add(gens); App.Current.Dispatcher.Invoke(new Action(() => { var mw = App.Current.MainWindow as MainWindow; if (mw != null) { if (mw.hLibrary.grdDD.Visibility == Visibility.Visible) { mw.hLibrary.grdDD.FadeOut(); } } if (SOFilter.ValidGenre(Results.TrackSearch, gens)) { Results.Genres.Add(gens); } if (SOFilter.ValidArtist(Results.TrackSearch, art)) { Results.Artists.Add(art); } if (SOFilter.ValidAlbum(Results.TrackSearch, albs)) { Results.Albums.Add(albs); } if (SOFilter.ValidTrack(Results.TrackSearch, track)) { Results.Tracks.Add(track); } })); return(track); } catch { } return(null); }
/// <summary> /// Workers load /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void worker_load(object sender, DoWorkEventArgs e) { //Load lists //InitializeLists(); if (Results.Tracks == null) { Results.Tracks = new ObservableCollection <TrackViewModel>(); } if (Results.Artists == null) { Results.Artists = new ObservableCollection <ArtistViewModel>(); } if (Results.Albums == null) { Results.Albums = new ObservableCollection <AlbumViewModel>(); } if (Results.Genres == null) { Results.Genres = new ObservableCollection <ViewModels.GenreViewModel>(); } DB_InUse = true; try { NotifyPropertyChanged("Initialized"); App.Current.Dispatcher.BeginInvoke(new Action(() => { if (ListenToChanges != null) { ListenToChanges(new TrackViewModel() { Title = "Searching" }, SearchGuid); } })); files = Search(@path, "*.*") .Where( s => s.ToLower().EndsWith(".mp3") || s.ToLower().EndsWith(".wma") || s.ToLower().EndsWith(".flac") || s.ToLower().EndsWith(".ogg") || s.ToLower().EndsWith(".wav") || s.ToLower().EndsWith(".mp2") || s.ToLower().EndsWith(".mp1") || s.ToLower().EndsWith(".m4a")); ; max = files.Count(); NotifyPropertyChanged("New Count"); } catch { } //NotifyPropertyChanged("New Count"); //tag data holding variables int mval = 0; if (files == null) // if there is nothin in files, return { //Settings.Default.LibSearchPath = ""; //Settings.Default.LibSearchCompleted = true; Settings.Default.Save(); return; } var artists = new Dictionary <string, ArtistViewModel>(); var albums = new Dictionary <string, AlbumViewModel>(); var genres = new Dictionary <string, ViewModels.GenreViewModel>(); Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = 1 }, file => { if (GlobalSearchCancel) { return; } try { var d = new TrackViewModel(); //Build tags TrackViewModel track = TaglibCore.BuildTrack(file); try { //Save Track if (!string.IsNullOrEmpty(track.Title) && track.SaveTrack() == "success") { App.Current.Dispatcher.BeginInvoke(new Action(() => { if (SOFilter.ValidTrack(Results.TrackSearch, track)) { if (!Results.Tracks.Contains(track)) { Results.Tracks.Add(track); } } })); } } catch (Exception exception) { Console.WriteLine(exception); } //============== //Artists //============== //Add the artist to dictionary from DB if the dictionary doesnt have already if (!artists.ContainsKey(track.Artist)) { ArtistViewModel arts = ArtistQuery.GetArtistViewModel(track.Artist); if (arts != null) { artists.Add(track.Artist, arts); } else { artists.Add(track.Artist, new ArtistViewModel()); } } //Do the operations for artists ArtistViewModel art = artists[track.Artist]; art.Name = track.Artist; art.Duration += track.Duration; art.SongCount++; art.SaveArtist(); App.Current.Dispatcher.BeginInvoke(new Action(() => { if (SOFilter.ValidArtist(Results.TrackSearch, art)) { ArtistViewModel exar; try { exar = Results.Artists.Single(c => c.Name == art.Name); } catch (Exception exception) { exar = null; } if (exar == null) { Results.Artists.Add(art); } else { exar.Listens = art.Listens; exar.Duration = art.Duration; exar.SongCount = art.SongCount; exar.Name = art.Name; } } })); //============== //Genre //============== //Add the album to dictionary from DB if the dictionary doesnt have already if (!genres.ContainsKey(track.Genre)) { ViewModels.GenreViewModel al = GenreQuery.GetGenreViewModel(track.Genre); if (al != null) { genres.Add(track.Genre, al); } else { genres.Add(track.Genre, new ViewModels.GenreViewModel()); } } ViewModels.GenreViewModel gens = genres[track.Genre]; gens.Name = track.Genre; gens.Duration += track.Duration; gens.SongCount++; gens.SaveGenre(); App.Current.Dispatcher.BeginInvoke(new Action(() => { if (SOFilter.ValidGenre(Results.TrackSearch, gens)) { ViewModels.GenreViewModel exar; try { exar = Results.Genres.Single(c => c.Name == gens.Name); } catch (Exception exception) { exar = null; } if (exar == null) { Results.Genres.Add(gens); } else { exar.Listens = art.Listens; exar.Duration = art.Duration; exar.SongCount = art.SongCount; exar.Name = art.Name; } } })); //============== //Albums //============== //Add the genre to dictionary from DB if the dictionary doesnt have already if (!albums.ContainsKey(track.Album)) { AlbumViewModel al = AlbumQuery.GetAlbumViewModel(track.Album); if (al != null) { albums.Add(track.Album, al); } else { albums.Add(track.Album, new AlbumViewModel()); } } AlbumViewModel albs = albums[track.Album]; albs.Name = track.Album; albs.Artist = track.Artist; albs.Duration += track.Duration; albs.SongCount++; albs.ID = track.Album; albs.SaveAlbum(); App.Current.Dispatcher.BeginInvoke(new Action(() => { if (SOFilter.ValidAlbum(Results.TrackSearch, albs)) { AlbumViewModel exar; try { exar = Results.Albums.Single(c => c.ID == albs.ID); } catch (Exception exception) { exar = null; } if (exar == null) { Results.Albums.Add(albs); } else { exar.Listens = art.Listens; exar.Duration = art.Duration; exar.SongCount = art.SongCount; exar.Name = art.Name; } } })); if (track.Album.ToLower() != "unknown album") { //Global.SaveAlbumArt(album + " - " + Artists, tgl.getArtStream()); //opt.SaveThumb("My Library\\AlbumArt\\" + album + " - " + Artists + ".jpg.thumb"); } if (!Tracks.Any(c => c.Path == track.Path)) { Tracks.Add(track); } if (!Artists.Any(c => c.Name == art.Name)) { Artists.Add(art); } if (!Albums.Any(c => c.Name == albs.Name)) { Albums.Add(albs); } if (!Genres.Any(c => c.Name == gens.Name)) { Genres.Add(gens); } //Playlist callbacks triggerCallback(track, SearchGuid); ListenToChanges(track, SearchGuid); } catch { } mval += 1; cworker.ReportProgress(mval); }); }