/// <summary> /// Occurs when the update library process background worker needs to do its job. /// </summary> /// <param name='sender'>Object sender</param> /// <param name='e'>Event arguments</param> protected void workerUpdateLibrary_DoWork(object sender, DoWorkEventArgs e) { List<string> filePaths = new List<string>(); UpdateLibraryArgument arg = (UpdateLibraryArgument)e.Argument; filePaths.AddRange(arg.FilePaths); try { // Remove broken songs from the library OnRaiseRefreshStatusEvent(new UpdateLibraryEntity() { Title = "Checking for broken file paths", Subtitle = "Checking if songs have been deleted on your hard disk but not removed from the library", PercentageDone = 0 }); _libraryService.RemoveAudioFilesWithBrokenFilePaths(); if (_cancelUpdateLibrary) throw new UpdateLibraryException(); filePaths = SearchMediaFilesInFolders(arg.FolderPaths); if (_cancelUpdateLibrary) throw new UpdateLibraryException(); // Get the list of audio files from the database var filePathsDatabase = _libraryService.SelectFilePaths(); var filePathsToUpdate = filePaths.Except(filePathsDatabase); var audioFiles = new List<AudioFile>(); for(int a = 0; a < filePathsToUpdate.Count(); a++) { if (_cancelUpdateLibrary) throw new UpdateLibraryException(); // Get current file path and calculate stats string filePath = filePathsToUpdate.ElementAt(a); float percentCompleted = ((float)a / (float)filePathsToUpdate.Count()); try { // Check if this is a playlist file if (filePath.ToUpper().Contains(".M3U") || filePath.ToUpper().Contains(".M3U8") || filePath.ToUpper().Contains(".PLS") || filePath.ToUpper().Contains(".XSPF")) { var playlistFile = new PlaylistFile(filePath); _libraryService.InsertPlaylistFile(playlistFile); } else { var audioFile = new AudioFile(filePath, Guid.NewGuid(), true); //_libraryService.InsertAudioFile(audioFile); audioFiles.Add(audioFile); if (audioFiles.Count >= 50) { //Console.WriteLine("UpdateLibraryService - Inserting 20 audio files into database..."); _libraryService.InsertAudioFiles(audioFiles); audioFiles.Clear(); } } OnRaiseRefreshStatusEvent(new UpdateLibraryEntity() { Title = "Adding audio files to the library", Subtitle = "Adding " + Path.GetFileName(filePath), FilePath = filePath, PercentageDone = percentCompleted, FileIndex = a, FileCount = filePathsToUpdate.Count() }); } catch (Exception ex) { Console.WriteLine("UpdateLibraryService - Failed to add {0}: {1}", filePath, ex); OnRaiseRefreshStatusEvent(new UpdateLibraryEntity() { Title = "Adding audio files to the library", Subtitle = "Adding " + filePath, FilePath = filePath, PercentageDone = percentCompleted, FileIndex = a, FileCount = filePathsToUpdate.Count(), Exception = ex }); } } if(audioFiles.Count > 0) _libraryService.InsertAudioFiles(audioFiles); // Cancel thread if necessary if (_cancelUpdateLibrary) throw new UpdateLibraryException(); // // Compact database // TODO: Lags like hell on iOS, completely blocks the UI thread even though it is done in another thread... // OnRaiseRefreshStatusEvent(new UpdateLibraryEntity() { // Title = "Compacting database", // Subtitle = "Compacting database...", // PercentageDone = 1 // }); // Task.Factory.StartNew(() => { // _libraryService.CompactDatabase(); // }); } catch (UpdateLibraryException ex) { OnRaiseRefreshStatusEvent(new UpdateLibraryEntity() { Title = "Update process canceled", Subtitle = "The update process was canceled by the user." }); e.Cancel = true; } catch (Exception ex) { OnRaiseRefreshStatusEvent(new UpdateLibraryEntity() { Title = "Error updating library", Subtitle = "An error has occured: " + ex.Message + "\n" + ex.StackTrace }); } }
public void InsertPlaylistFile(PlaylistFile playlistFile) { _gateway.InsertPlaylistFile(playlistFile); }