2025-12-17 23:32:18 +11:00
|
|
|
#include "ReaderActivity.h"
|
|
|
|
|
|
|
|
|
|
#include "Epub.h"
|
|
|
|
|
#include "EpubReaderActivity.h"
|
|
|
|
|
#include "FileSelectionActivity.h"
|
2025-12-28 23:56:05 +09:00
|
|
|
#include "Xtc.h"
|
|
|
|
|
#include "XtcReaderActivity.h"
|
2025-12-17 23:32:18 +11:00
|
|
|
#include "activities/util/FullScreenMessageActivity.h"
|
2026-01-07 20:07:23 +10:00
|
|
|
#include "util/StringUtils.h"
|
2025-12-17 23:32:18 +11:00
|
|
|
|
2025-12-26 09:55:23 +09:00
|
|
|
std::string ReaderActivity::extractFolderPath(const std::string& filePath) {
|
|
|
|
|
const auto lastSlash = filePath.find_last_of('/');
|
|
|
|
|
if (lastSlash == std::string::npos || lastSlash == 0) {
|
|
|
|
|
return "/";
|
|
|
|
|
}
|
|
|
|
|
return filePath.substr(0, lastSlash);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
bool ReaderActivity::isXtcFile(const std::string& path) {
|
2026-01-07 20:07:23 +10:00
|
|
|
return StringUtils::checkFileExtension(path, ".xtc") || StringUtils::checkFileExtension(path, ".xtch");
|
2025-12-28 23:56:05 +09:00
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
std::unique_ptr<Epub> ReaderActivity::loadEpub(const std::string& path) {
|
2025-12-30 15:09:30 +10:00
|
|
|
if (!SdMan.exists(path.c_str())) {
|
2025-12-17 23:32:18 +11:00
|
|
|
Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto epub = std::unique_ptr<Epub>(new Epub(path, "/.crosspoint"));
|
|
|
|
|
if (epub->load()) {
|
|
|
|
|
return epub;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.printf("[%lu] [ ] Failed to load epub\n", millis());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
std::unique_ptr<Xtc> ReaderActivity::loadXtc(const std::string& path) {
|
2025-12-30 15:09:30 +10:00
|
|
|
if (!SdMan.exists(path.c_str())) {
|
2025-12-28 23:56:05 +09:00
|
|
|
Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto xtc = std::unique_ptr<Xtc>(new Xtc(path, "/.crosspoint"));
|
|
|
|
|
if (xtc->load()) {
|
|
|
|
|
return xtc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.printf("[%lu] [ ] Failed to load XTC\n", millis());
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReaderActivity::onSelectBookFile(const std::string& path) {
|
|
|
|
|
currentBookPath = path; // Track current book path
|
2025-12-17 23:32:18 +11:00
|
|
|
exitActivity();
|
2025-12-28 21:59:14 -06:00
|
|
|
enterNewActivity(new FullScreenMessageActivity(renderer, mappedInput, "Loading..."));
|
2025-12-17 23:32:18 +11:00
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
if (isXtcFile(path)) {
|
|
|
|
|
// Load XTC file
|
|
|
|
|
auto xtc = loadXtc(path);
|
|
|
|
|
if (xtc) {
|
|
|
|
|
onGoToXtcReader(std::move(xtc));
|
|
|
|
|
} else {
|
|
|
|
|
exitActivity();
|
2025-12-31 12:11:36 +10:00
|
|
|
enterNewActivity(new FullScreenMessageActivity(renderer, mappedInput, "Failed to load XTC",
|
|
|
|
|
EpdFontFamily::REGULAR, EInkDisplay::HALF_REFRESH));
|
2025-12-28 23:56:05 +09:00
|
|
|
delay(2000);
|
|
|
|
|
onGoToFileSelection();
|
|
|
|
|
}
|
2025-12-17 23:32:18 +11:00
|
|
|
} else {
|
2025-12-28 23:56:05 +09:00
|
|
|
// Load EPUB file
|
|
|
|
|
auto epub = loadEpub(path);
|
|
|
|
|
if (epub) {
|
|
|
|
|
onGoToEpubReader(std::move(epub));
|
|
|
|
|
} else {
|
|
|
|
|
exitActivity();
|
2025-12-31 12:11:36 +10:00
|
|
|
enterNewActivity(new FullScreenMessageActivity(renderer, mappedInput, "Failed to load epub",
|
|
|
|
|
EpdFontFamily::REGULAR, EInkDisplay::HALF_REFRESH));
|
2025-12-28 23:56:05 +09:00
|
|
|
delay(2000);
|
|
|
|
|
onGoToFileSelection();
|
|
|
|
|
}
|
2025-12-17 23:32:18 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
void ReaderActivity::onGoToFileSelection(const std::string& fromBookPath) {
|
2025-12-17 23:32:18 +11:00
|
|
|
exitActivity();
|
2025-12-26 09:55:23 +09:00
|
|
|
// If coming from a book, start in that book's folder; otherwise start from root
|
2025-12-28 23:56:05 +09:00
|
|
|
const auto initialPath = fromBookPath.empty() ? "/" : extractFolderPath(fromBookPath);
|
2025-12-17 23:32:18 +11:00
|
|
|
enterNewActivity(new FileSelectionActivity(
|
2025-12-28 21:59:14 -06:00
|
|
|
renderer, mappedInput, [this](const std::string& path) { onSelectBookFile(path); }, onGoBack, initialPath));
|
2025-12-17 23:32:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ReaderActivity::onGoToEpubReader(std::unique_ptr<Epub> epub) {
|
2025-12-26 09:55:23 +09:00
|
|
|
const auto epubPath = epub->getPath();
|
2025-12-28 23:56:05 +09:00
|
|
|
currentBookPath = epubPath;
|
2025-12-17 23:32:18 +11:00
|
|
|
exitActivity();
|
2025-12-26 09:55:23 +09:00
|
|
|
enterNewActivity(new EpubReaderActivity(
|
2025-12-28 21:59:14 -06:00
|
|
|
renderer, mappedInput, std::move(epub), [this, epubPath] { onGoToFileSelection(epubPath); },
|
2025-12-26 09:55:23 +09:00
|
|
|
[this] { onGoBack(); }));
|
2025-12-17 23:32:18 +11:00
|
|
|
}
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
void ReaderActivity::onGoToXtcReader(std::unique_ptr<Xtc> xtc) {
|
|
|
|
|
const auto xtcPath = xtc->getPath();
|
|
|
|
|
currentBookPath = xtcPath;
|
|
|
|
|
exitActivity();
|
|
|
|
|
enterNewActivity(new XtcReaderActivity(
|
2025-12-28 21:59:14 -06:00
|
|
|
renderer, mappedInput, std::move(xtc), [this, xtcPath] { onGoToFileSelection(xtcPath); },
|
2025-12-28 23:56:05 +09:00
|
|
|
[this] { onGoBack(); }));
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 23:32:18 +11:00
|
|
|
void ReaderActivity::onEnter() {
|
2025-12-21 21:17:00 +11:00
|
|
|
ActivityWithSubactivity::onEnter();
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
if (initialBookPath.empty()) {
|
2025-12-26 09:55:23 +09:00
|
|
|
onGoToFileSelection(); // Start from root when entering via Browse
|
2025-12-17 23:32:18 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
currentBookPath = initialBookPath;
|
2025-12-17 23:32:18 +11:00
|
|
|
|
2025-12-28 23:56:05 +09:00
|
|
|
if (isXtcFile(initialBookPath)) {
|
|
|
|
|
auto xtc = loadXtc(initialBookPath);
|
|
|
|
|
if (!xtc) {
|
|
|
|
|
onGoBack();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onGoToXtcReader(std::move(xtc));
|
|
|
|
|
} else {
|
|
|
|
|
auto epub = loadEpub(initialBookPath);
|
|
|
|
|
if (!epub) {
|
|
|
|
|
onGoBack();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
onGoToEpubReader(std::move(epub));
|
|
|
|
|
}
|
2025-12-17 23:32:18 +11:00
|
|
|
}
|