feat: port crossink 'read book move' feature to crosspoint (#2032)

This commit is contained in:
KemoNine
2026-05-18 12:39:50 -04:00
committed by GitHub
parent 8a11f44571
commit 06d28d6ffa
28 changed files with 130 additions and 1 deletions
+2
View File
@@ -220,6 +220,8 @@ class CrossPointSettings {
char sdFontFamilyName[32] = "";
// Show hidden files/directories (starting with '.') in the file browser (0 = hidden, 1 = show)
uint8_t showHiddenFiles = 0;
// Move epub to /Read/ folder on SD card when finished (0 = disabled, 1 = enabled)
uint8_t moveFinishedToReadFolder = 0;
// Image rendering mode in EPUB reader
uint8_t imageRendering = IMAGES_DISPLAY;
// Tilt-based page turning (X3 only — requires QMI8658 IMU)
+14
View File
@@ -56,6 +56,20 @@ void RecentBooksStore::updateBook(const std::string& path, const std::string& ti
}
}
void RecentBooksStore::updatePath(const std::string& oldPath, const std::string& newPath,
const std::string& oldCachePath, const std::string& newCachePath) {
auto it = std::find_if(recentBooks.begin(), recentBooks.end(),
[&](const RecentBook& book) { return book.path == oldPath; });
if (it == recentBooks.end()) {
return;
}
it->path = newPath;
if (!oldCachePath.empty() && !it->coverBmpPath.empty() && it->coverBmpPath.rfind(oldCachePath, 0) == 0) {
it->coverBmpPath = newCachePath + it->coverBmpPath.substr(oldCachePath.size());
}
saveToFile();
}
bool RecentBooksStore::isMissing(const RecentBook& book) { return !Storage.exists(book.path.c_str()); }
bool RecentBooksStore::pruneMissing() {
+6
View File
@@ -37,6 +37,12 @@ class RecentBooksStore {
void updateBook(const std::string& path, const std::string& title, const std::string& author,
const std::string& coverBmpPath);
// Repoint an entry's path (and coverBmpPath, if it lived under the old cache dir) after the
// backing file and cache dir were moved on disk. No-op if no entry matches oldPath.
// Persists on success. Keeps the entry's list position (does not reorder).
void updatePath(const std::string& oldPath, const std::string& newPath, const std::string& oldCachePath,
const std::string& newCachePath);
// True if the book's backing file is no longer present on the SD card.
static bool isMissing(const RecentBook& book);
+2
View File
@@ -177,6 +177,8 @@ inline std::vector<SettingInfo> getSettingsList(const SdCardFontRegistry* regist
"sleepTimeout", StrId::STR_CAT_SYSTEM),
SettingInfo::Toggle(StrId::STR_SHOW_HIDDEN_FILES, &CrossPointSettings::showHiddenFiles, "showHiddenFiles",
StrId::STR_CAT_SYSTEM),
SettingInfo::Toggle(StrId::STR_MOVE_FINISHED_TO_READ, &CrossPointSettings::moveFinishedToReadFolder,
"moveFinishedToReadFolder", StrId::STR_CAT_SYSTEM),
// --- KOReader Sync (web-only, uses KOReaderCredentialStore) ---
SettingInfo::DynamicString(
+81 -1
View File
@@ -10,6 +10,7 @@
#include <Logging.h>
#include <esp_system.h>
#include <functional>
#include <iterator>
#include <limits>
@@ -45,6 +46,68 @@ int clampPercent(int percent) {
return percent;
}
// SD card folder finished books are moved into. Single source of truth for the path.
// constexpr ⇒ lives in flash .rodata, no DRAM cost.
constexpr char READ_FOLDER[] = "/read";
// True if path is inside READ_FOLDER (starts with "<READ_FOLDER>/"). Non-allocating so
// it is cheap to call from loop(), and avoids reintroducing a separate "/Read/" literal.
bool isInReadFolder(const std::string& path) {
constexpr size_t n = sizeof(READ_FOLDER) - 1; // length of "/Read" (excludes NUL)
return path.size() > n && path.compare(0, n, READ_FOLDER) == 0 && path[n] == '/';
}
// Pick a non-colliding destination path inside /Read/ for a finished book.
// Mirrors the suffixing scheme used elsewhere: "name.epub" -> "name (2).epub", etc.
std::string buildReadFolderDestination(const std::string& srcPath) {
const size_t lastSlash = srcPath.rfind('/');
const std::string filename = (lastSlash != std::string::npos) ? srcPath.substr(lastSlash + 1) : srcPath;
Storage.mkdir(READ_FOLDER);
std::string dstPath = std::string(READ_FOLDER) + "/" + filename;
if (!Storage.exists(dstPath.c_str())) {
return dstPath;
}
const size_t dotPos = filename.rfind('.');
const std::string base = (dotPos != std::string::npos) ? filename.substr(0, dotPos) : filename;
const std::string ext = (dotPos != std::string::npos) ? filename.substr(dotPos) : "";
int suffix = 2;
do {
dstPath = std::string(READ_FOLDER) + "/" + base + " (" + std::to_string(suffix) + ")" + ext;
suffix++;
} while (Storage.exists(dstPath.c_str()) && suffix < 100);
return dstPath;
}
// Relocate a finished book and its cache dir into /read/, keep it in recents by
// repointing its entry to the new path, and repoint the resume pointer too.
// On rename failure: LOG_ERR and leave everything in place (no UI alert subsystem here).
void moveFinishedBookToReadFolder(const std::string& srcPath, const std::string& dstPath,
const std::string& oldCachePath) {
LOG_INF("ERS", "Moving finished epub: %s -> %s", srcPath.c_str(), dstPath.c_str());
if (!Storage.rename(srcPath.c_str(), dstPath.c_str())) {
LOG_ERR("ERS", "Failed to move finished book to '/Read' folder");
return;
}
// Cache dir is keyed by hash of the epub path (see Epub ctor), so it must be re-keyed.
const std::string newCachePath = "/.crosspoint/epub_" + std::to_string(std::hash<std::string>{}(dstPath));
if (!oldCachePath.empty() && Storage.exists(oldCachePath.c_str())) {
if (!Storage.rename(oldCachePath.c_str(), newCachePath.c_str())) {
LOG_ERR("ERS", "Failed to rename cache dir %s -> %s (non-fatal)", oldCachePath.c_str(), newCachePath.c_str());
}
}
// Keep the book in recents (crossink behavior): repoint the entry to its new
// location instead of dropping it. updatePath persists on success.
RECENT_BOOKS.updatePath(srcPath, dstPath, oldCachePath, newCachePath);
if (APP_STATE.openEpubPath == srcPath) {
APP_STATE.openEpubPath = dstPath;
APP_STATE.saveToFile();
}
}
} // namespace
void EpubReaderActivity::onEnter() {
@@ -109,7 +172,15 @@ void EpubReaderActivity::onExit() {
APP_STATE.readerActivityLoadCount = 0;
APP_STATE.saveToFile();
section.reset();
epub.reset();
if (pendingReadFolderMove && epub) {
const std::string srcPath = epub->getPath();
const std::string oldCachePath = epub->getCachePath();
const std::string dstPath = buildReadFolderDestination(srcPath);
epub.reset(); // release the Epub (and any open handles) before renaming on the SD card
moveFinishedBookToReadFolder(srcPath, dstPath, oldCachePath);
} else {
epub.reset();
}
}
void EpubReaderActivity::loop() {
@@ -119,6 +190,15 @@ void EpubReaderActivity::loop() {
return;
}
// Being on the "End of Book" screen (currentSpineIndex == spine count) means the book is
// finished. Arm the move here so ANY exit path (Back, Home, file browser) relocates the
// book in onExit(); paging back off the end screen disarms it (book not actually finished).
if (currentSpineIndex > 0 && currentSpineIndex >= epub->getSpineItemsCount()) {
pendingReadFolderMove = SETTINGS.moveFinishedToReadFolder && !isInReadFolder(epub->getPath());
} else {
pendingReadFolderMove = false;
}
if (automaticPageTurnActive) {
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm) ||
mappedInput.wasReleased(MappedInputManager::Button::Back)) {
@@ -31,6 +31,9 @@ class EpubReaderActivity final : public Activity {
bool pendingSyncSaveError = false;
bool skipNextButtonCheck = false; // Skip button processing for one frame after subactivity exit
bool automaticPageTurnActive = false;
// Set when the reader is left at end-of-book and SETTINGS.moveFinishedToReadFolder is on.
// Consumed in onExit() to relocate the finished book into /Read/.
bool pendingReadFolderMove = false;
// Footnote support
std::vector<FootnoteEntry> currentPageFootnotes;