/// <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); }); }
/// <summary> /// Generate a list of BookData objects, where each of them contains information about a book /// </summary> public static List <BookData> List() { var bookData = new List <BookData>(); const string sql = "SELECT * FROM books"; var query = Db.Query(sql); while (query.Read()) { if (!File.Exists(BookKeeper.GetAbsoluteBookFilePath(query["Path"].ToString()))) { continue; } bookData.Add(BookKeeper.CastSqlBookRowToBookData(query)); } query.Dispose(); return(bookData); }
/// <summary> /// Take all books files marked for sync and copy them onto the reader device, if one is found. /// If any book files are found on the reader device, which are not marked for sync in the local library, they will be deleted from the device. /// </summary> private static void SyncBookFiles() { var localBookList = LibraryStructure.List(); var localBookListForSync = (from bookData in localBookList where bookData.Sync select BookKeeper.GetAbsoluteBookFilePath(bookData.Path)).ToList(); var bookList = GetFileList(); if (bookList == null) //The reader is not connected, or the specified storage folder on it doesn't exist, no point to continue { MainWindow.Busy(false); return; } var filesToDelete = (from file in bookList let fileName = Path.GetFileName(file) where !localBookListForSync.Select(Path.GetFileName).Contains(fileName) select file).ToArray(); var filesToCopy = localBookListForSync.Where( file => File.Exists(file) && !bookList.Select(Path.GetFileName).Contains(Path.GetFileName(file))).ToArray(); MainWindow.BusyMax(filesToDelete.Length + filesToCopy.Length); var busyCount = 0; foreach (var file in filesToDelete) //Delete files from the reader which don't exist in the local Sync list { MainWindow.Busy(busyCount++); MainWindow.Busy(file); try { File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } catch (Exception e) { DebugConsole.WriteLine("Usb sync: Failed to delete " + file + ": " + e); } } foreach (var file in filesToCopy) //Copy files (which don't exist in the reader) into the reader, from the local Sync list { DebugConsole.WriteLine("Copying " + file); MainWindow.Busy(busyCount++); MainWindow.Busy(file); try { if (file != null) { File.Copy(file, Path.Combine(_deviceDir, Path.GetFileName(file))); } } catch (Exception e) { MainWindow.Info(String.Format(UiLang.Get("SyncFileCopyFailed"), file)); DebugConsole.WriteLine("Usb sync: Error while copying " + file + ": " + e); } } MainWindow.Info(UiLang.Get("SyncFinished")); MainWindow.Busy(false); }