/// <summary> /// Go through all database records along with all book files in the library root folder and remove any database entries which point to non-existing files, /// then attempt to generate database entries for all book files, which don't have them. /// </summary> public static void SyncDbWithFileTree() { MainWindow.Busy(true); Task.Factory.StartNew(() => { GenerateFileTree(); Tools.RemoveEmptyDirectories(Properties.Settings.Default.BooksDir); var fileTree = GetFileTree(); const string sql = "SELECT Path FROM books"; const string sqlDelete = "DELETE FROM books WHERE Path = @Path"; var query = Db.Query(sql); var pathList = new List <string>(); MainWindow.Busy(UiLang.Get("BusyCleaningDb")); //Delete rows pointing to non-existing files while (query.Read()) { if (File.Exists(BookKeeper.GetAbsoluteBookFilePath(query["Path"].ToString()))) { pathList.Add(query["Path"].ToString()); } else { Db.NonQuery(sqlDelete, new[] { new SQLiteParameter("Path", query["Path"].ToString()) }); } } MainWindow.BusyMax(fileTree.Count(bookFile => !pathList.Contains(BookKeeper.GetRelativeBookFilePath(bookFile)))); var i = 0; //Generate rows for any books missing them foreach ( var bookFile in fileTree.Where(bookFile => !pathList.Contains(BookKeeper.GetRelativeBookFilePath(bookFile)))) { MainWindow.Busy(BookKeeper.GetRelativeBookFilePath(bookFile)); MainWindow.Busy(i++); try { BookKeeper.GetData(bookFile); } catch (Exception e) { DebugConsole.WriteLine( "Library structure: I found a book file without any entry in the database (" + bookFile + "), but an error occurred during attempted adding: " + e); } } MainWindow.MW.Dispatcher.Invoke(() => MainWindow.MW.BookGridReload()); MainWindow.Busy(false); }); }
//Change value in existing .dat file for a book private static void BookInfoSet(string key, object value, string bookFile) { try { var bookData = BookKeeper.GetData(bookFile); if ((typeof(BookData)).GetField("Sync") == null) { return; } typeof(BookData).GetField(key).SetValue(bookData, value); BookKeeper.SaveData(bookData); } catch (Exception e) { DebugConsole.WriteLine("Edit book: It was not possible to save the provided book data: " + e.Message); } }
/// <summary> /// Fetch data to fill the form fields, from the BookInfo object based on the key /// </summary> /// <param name="key">Name of the BookData field from which to get the data</param> /// <returns>Value from the BookData field specified by the given key</returns> private object BookInfoGet(string key) { if (_bookData == null) //Singleton, so we don't have to reopen the DB with saved info, after every form field loads and its load event handler calls BookInfoGet { try { _bookData = BookKeeper.GetData(_bookFile); } catch (Exception) { MainWindow.Info(UiLang.Get("BookInfoNotAvailable"), 1); IsEnabled = false; Close(); _bookData = new BookData(); return(null); } } return(typeof(BookData).GetField(key) != null ? typeof(BookData).GetField(key).GetValue(_bookData) : null); }