chore: Refactor stores to use PersistableStore CRTP template (#2464)
This commit is contained in:
+29
-107
@@ -3,23 +3,42 @@
|
||||
#include <Epub.h>
|
||||
#include <FsHelpers.h>
|
||||
#include <HalStorage.h>
|
||||
#include <JsonSettingsIO.h>
|
||||
#include <Logging.h>
|
||||
#include <Serialization.h>
|
||||
#include <Xtc.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
namespace {
|
||||
constexpr uint8_t RECENT_BOOKS_FILE_VERSION = 3;
|
||||
constexpr char RECENT_BOOKS_FILE_BIN[] = "/.crosspoint/recent.bin";
|
||||
constexpr char RECENT_BOOKS_FILE_JSON[] = "/.crosspoint/recent.json";
|
||||
constexpr char RECENT_BOOKS_FILE_BAK[] = "/.crosspoint/recent.bin.bak";
|
||||
constexpr int MAX_RECENT_BOOKS = 10;
|
||||
} // namespace
|
||||
void RecentBooksStore::toJson(JsonDocument& doc) const {
|
||||
JsonArray arr = doc["books"].to<JsonArray>();
|
||||
for (const auto& book : recentBooks) {
|
||||
JsonObject obj = arr.add<JsonObject>();
|
||||
obj["path"] = book.path;
|
||||
obj["title"] = book.title;
|
||||
obj["author"] = book.author;
|
||||
obj["coverBmpPath"] = book.coverBmpPath;
|
||||
}
|
||||
}
|
||||
|
||||
RecentBooksStore RecentBooksStore::instance;
|
||||
bool RecentBooksStore::fromJson(JsonVariantConst doc) {
|
||||
// Tolerate a missing/invalid 'books' key (treat as empty list); only a
|
||||
// JSON parse error is fatal. A null JsonArray iterates zero times.
|
||||
recentBooks.clear();
|
||||
JsonArrayConst arr = doc["books"].as<JsonArrayConst>();
|
||||
recentBooks.reserve(std::min(arr.size(), static_cast<size_t>(MAX_RECENT_BOOKS)));
|
||||
for (JsonObjectConst obj : arr) {
|
||||
if (getCount() >= MAX_RECENT_BOOKS) break;
|
||||
RecentBook book;
|
||||
book.path = obj["path"] | "";
|
||||
book.title = obj["title"] | "";
|
||||
book.author = obj["author"] | "";
|
||||
book.coverBmpPath = obj["coverBmpPath"] | "";
|
||||
recentBooks.push_back(book);
|
||||
}
|
||||
|
||||
LOG_DBG("RBS", "Recent books loaded from file (%d entries)", getCount());
|
||||
return true;
|
||||
}
|
||||
|
||||
void RecentBooksStore::addBook(const std::string& path, const std::string& title, const std::string& author,
|
||||
const std::string& coverBmpPath) {
|
||||
@@ -92,11 +111,6 @@ bool RecentBooksStore::pruneMissing() {
|
||||
return recentBooks.size() != before;
|
||||
}
|
||||
|
||||
bool RecentBooksStore::saveToFile() const {
|
||||
Storage.mkdir("/.crosspoint");
|
||||
return JsonSettingsIO::saveRecentBooks(*this, RECENT_BOOKS_FILE_JSON);
|
||||
}
|
||||
|
||||
RecentBook RecentBooksStore::getDataFromBook(std::string path) const {
|
||||
std::string lastBookFileName = "";
|
||||
const size_t lastSlash = path.find_last_of('/');
|
||||
@@ -124,95 +138,3 @@ RecentBook RecentBooksStore::getDataFromBook(std::string path) const {
|
||||
}
|
||||
return RecentBook{path, "", "", ""};
|
||||
}
|
||||
|
||||
bool RecentBooksStore::loadFromFile() {
|
||||
// Try JSON first
|
||||
if (Storage.exists(RECENT_BOOKS_FILE_JSON)) {
|
||||
String json = Storage.readFile(RECENT_BOOKS_FILE_JSON);
|
||||
if (!json.isEmpty()) {
|
||||
return JsonSettingsIO::loadRecentBooks(*this, json.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to binary migration
|
||||
if (Storage.exists(RECENT_BOOKS_FILE_BIN)) {
|
||||
if (loadFromBinaryFile()) {
|
||||
saveToFile();
|
||||
Storage.rename(RECENT_BOOKS_FILE_BIN, RECENT_BOOKS_FILE_BAK);
|
||||
LOG_DBG("RBS", "Migrated recent.bin to recent.json");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RecentBooksStore::loadFromBinaryFile() {
|
||||
HalFile inputFile;
|
||||
if (!Storage.openFileForRead("RBS", RECENT_BOOKS_FILE_BIN, inputFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t version;
|
||||
serialization::readPod(inputFile, version);
|
||||
if (version == 1 || version == 2) {
|
||||
// Old version, just read paths
|
||||
uint8_t count;
|
||||
serialization::readPod(inputFile, count);
|
||||
recentBooks.clear();
|
||||
recentBooks.reserve(count);
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
std::string path;
|
||||
serialization::readString(inputFile, path);
|
||||
|
||||
// load book to get missing data
|
||||
RecentBook book = getDataFromBook(path);
|
||||
if (book.title.empty() && book.author.empty() && version == 2) {
|
||||
// Fall back to loading what we can from the store
|
||||
std::string title, author;
|
||||
serialization::readString(inputFile, title);
|
||||
serialization::readString(inputFile, author);
|
||||
recentBooks.push_back({path, title, author, ""});
|
||||
} else {
|
||||
recentBooks.push_back(book);
|
||||
}
|
||||
}
|
||||
} else if (version == 3) {
|
||||
uint8_t count;
|
||||
serialization::readPod(inputFile, count);
|
||||
|
||||
recentBooks.clear();
|
||||
recentBooks.reserve(count);
|
||||
uint8_t omitted = 0;
|
||||
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
std::string path, title, author, coverBmpPath;
|
||||
serialization::readString(inputFile, path);
|
||||
serialization::readString(inputFile, title);
|
||||
serialization::readString(inputFile, author);
|
||||
serialization::readString(inputFile, coverBmpPath);
|
||||
|
||||
// Omit books with missing title (e.g. saved before metadata was available)
|
||||
if (title.empty()) {
|
||||
omitted++;
|
||||
continue;
|
||||
}
|
||||
|
||||
recentBooks.push_back({path, title, author, coverBmpPath});
|
||||
}
|
||||
|
||||
if (omitted > 0) {
|
||||
// Explicitly close() file before saveToFile() rewrites the same file
|
||||
inputFile.close();
|
||||
saveToFile();
|
||||
LOG_DBG("RBS", "Omitted %u recent book(s) with missing title", omitted);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
LOG_ERR("RBS", "Deserialization failed: Unknown version %u", version);
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_DBG("RBS", "Recent books loaded from binary file (%d entries)", static_cast<int>(recentBooks.size()));
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user