Clean up outdated TODO comments that are no longer relevant and improve comment clarity in activity classes and parsers.
400 lines
13 KiB
C++
400 lines
13 KiB
C++
#include "OpdsBookBrowserActivity.h"
|
|
|
|
#include <GfxRenderer.h>
|
|
#include <FreeInkUI.h>
|
|
#include <I18n.h>
|
|
#include <Logging.h>
|
|
#include <OpdsStream.h>
|
|
#include <WiFi.h>
|
|
|
|
#include "MappedInputManager.h"
|
|
#include "SilentRestart.h"
|
|
#include "activities/network/WifiSelectionActivity.h"
|
|
#include "activities/util/KeyboardEntryActivity.h"
|
|
#include "components/TouchRegistry.h"
|
|
#include "components/UITheme.h"
|
|
#include "fontIds.h"
|
|
#include "network/HttpDownloader.h"
|
|
#include "util/BookCacheUtils.h"
|
|
#include "util/StringUtils.h"
|
|
#include "util/UrlUtils.h"
|
|
|
|
namespace {
|
|
constexpr int PAGE_ITEMS = 23;
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::onEnter() {
|
|
Activity::onEnter();
|
|
|
|
state = BrowserState::CHECK_WIFI;
|
|
entries.clear();
|
|
navigationHistory.clear();
|
|
searchTemplate = "";
|
|
currentPath = "";
|
|
selectorIndex = 0;
|
|
consumeConfirm = false;
|
|
errorMessage.clear();
|
|
statusMessage = tr(STR_CHECKING_WIFI);
|
|
requestUpdate();
|
|
|
|
checkAndConnectWifi();
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::onExit() {
|
|
Activity::onExit();
|
|
entries.clear();
|
|
navigationHistory.clear();
|
|
|
|
if (WiFi.getMode() != WIFI_MODE_NULL) {
|
|
WiFi.disconnect(false);
|
|
delay(30);
|
|
silentRestart();
|
|
}
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::loop() {
|
|
if (state == BrowserState::WIFI_SELECTION || state == BrowserState::SEARCH_INPUT) {
|
|
return;
|
|
}
|
|
|
|
if (consumeConfirm && mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
|
consumeConfirm = false;
|
|
return;
|
|
}
|
|
|
|
if (state == BrowserState::ERROR) {
|
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
|
if (WiFi.status() == WL_CONNECTED && WiFi.localIP() != IPAddress(0, 0, 0, 0)) {
|
|
state = BrowserState::LOADING;
|
|
statusMessage = tr(STR_LOADING);
|
|
requestUpdate();
|
|
fetchFeed(currentPath);
|
|
} else {
|
|
launchWifiSelection();
|
|
}
|
|
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
|
navigateBack();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (state == BrowserState::CHECK_WIFI || state == BrowserState::LOADING) {
|
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
|
state == BrowserState::CHECK_WIFI ? onGoHome() : navigateBack();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (state == BrowserState::DOWNLOADING) return;
|
|
|
|
if (state == BrowserState::BROWSING) {
|
|
if (!entries.empty()) {
|
|
if (mappedInput.wasListScroll(selectorIndex, static_cast<int>(entries.size()), PAGE_ITEMS)) {
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
|
|
int downId = -1;
|
|
if (mappedInput.wasItemTouchedDown(downId) &&
|
|
freeink::ui::listSelectIndex(selectorIndex, downId, static_cast<int>(entries.size()))) {
|
|
requestUpdate();
|
|
}
|
|
|
|
int tappedId = -1;
|
|
if (mappedInput.wasItemTapped(tappedId) && tappedId >= 0 && tappedId < static_cast<int>(entries.size())) {
|
|
selectorIndex = tappedId;
|
|
activateSelectedEntry();
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
|
activateSelectedEntry();
|
|
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
|
navigateBack();
|
|
} else if (mappedInput.wasReleased(MappedInputManager::Button::Left)) {
|
|
if (!searchTemplate.empty() && selectorIndex == 0) launchSearch();
|
|
}
|
|
|
|
if (!entries.empty()) {
|
|
buttonNavigator.onNextRelease([this] {
|
|
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, entries.size());
|
|
requestUpdate();
|
|
});
|
|
buttonNavigator.onPreviousRelease([this] {
|
|
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, entries.size());
|
|
requestUpdate();
|
|
});
|
|
buttonNavigator.onNextContinuous([this] {
|
|
selectorIndex = ButtonNavigator::nextPageIndex(selectorIndex, entries.size(), PAGE_ITEMS);
|
|
requestUpdate();
|
|
});
|
|
buttonNavigator.onPreviousContinuous([this] {
|
|
selectorIndex = ButtonNavigator::previousPageIndex(selectorIndex, entries.size(), PAGE_ITEMS);
|
|
requestUpdate();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::render(RenderLock&&) {
|
|
renderer.clearScreen();
|
|
const auto pageWidth = renderer.getScreenWidth();
|
|
const auto pageHeight = renderer.getScreenHeight();
|
|
|
|
// Show server name in header if available, otherwise generic title
|
|
const char* headerTitle = server.name.empty() ? tr(STR_OPDS_BROWSER) : server.name.c_str();
|
|
renderer.drawCenteredText(UI_12_FONT_ID, 15, headerTitle, true, EpdFontFamily::BOLD);
|
|
|
|
if (state == BrowserState::CHECK_WIFI || state == BrowserState::LOADING) {
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2, statusMessage.c_str());
|
|
const auto labels = mappedInput.mapLabels(tr(STR_BACK), "", "", "");
|
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
renderer.displayBuffer();
|
|
return;
|
|
}
|
|
|
|
if (state == BrowserState::ERROR) {
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 - 20, tr(STR_ERROR_MSG));
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 + 10, errorMessage.c_str());
|
|
const auto labels = mappedInput.mapLabels(tr(STR_BACK), tr(STR_RETRY), "", "");
|
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
renderer.displayBuffer();
|
|
return;
|
|
}
|
|
|
|
if (state == BrowserState::DOWNLOADING) {
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 - 40, tr(STR_DOWNLOADING));
|
|
auto title = renderer.truncatedText(UI_10_FONT_ID, statusMessage.c_str(), pageWidth - 40);
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 - 10, title.c_str());
|
|
if (downloadTotal > 0) {
|
|
GUI.drawProgressBar(renderer, Rect{50, pageHeight / 2 + 20, pageWidth - 100, 20}, downloadProgress,
|
|
downloadTotal);
|
|
}
|
|
renderer.displayBuffer();
|
|
return;
|
|
}
|
|
|
|
const char* confirmLabel =
|
|
(!entries.empty() && entries[selectorIndex].type == OpdsEntryType::BOOK) ? tr(STR_DOWNLOAD) : tr(STR_OPEN);
|
|
const char* searchLabel = (!searchTemplate.empty() && selectorIndex == 0) ? tr(STR_SEARCH) : tr(STR_DIR_UP);
|
|
const auto labels = mappedInput.mapLabels(tr(STR_BACK), confirmLabel, searchLabel, tr(STR_DIR_DOWN));
|
|
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
|
|
if (entries.empty()) {
|
|
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2, tr(STR_NO_ENTRIES));
|
|
} else {
|
|
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
|
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 - 2, pageWidth - 1, 30);
|
|
|
|
for (size_t i = pageStartIndex; i < entries.size() && i < static_cast<size_t>(pageStartIndex + PAGE_ITEMS); i++) {
|
|
const auto& entry = entries[i];
|
|
std::string displayText = (entry.type == OpdsEntryType::NAVIGATION) ? "> " + entry.title : entry.title;
|
|
if (entry.type == OpdsEntryType::BOOK && !entry.author.empty()) displayText += " - " + entry.author;
|
|
auto item = renderer.truncatedText(UI_10_FONT_ID, displayText.c_str(), pageWidth - 40);
|
|
const int itemY = 60 + (i % PAGE_ITEMS) * 30;
|
|
renderer.drawText(UI_10_FONT_ID, 20, itemY, item.c_str(), i != static_cast<size_t>(selectorIndex));
|
|
TouchRegistry::getInstance().add(Rect{0, itemY - 2, pageWidth, 30}, static_cast<int>(i), TouchRegistry::Item);
|
|
}
|
|
}
|
|
renderer.displayBuffer();
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::activateSelectedEntry() {
|
|
if (entries.empty() || selectorIndex < 0 || selectorIndex >= static_cast<int>(entries.size())) return;
|
|
const auto& entry = entries[selectorIndex];
|
|
entry.type == OpdsEntryType::BOOK ? downloadBook(entry) : navigateToEntry(entry);
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::fetchFeed(const std::string& path) {
|
|
if (server.url.empty()) {
|
|
state = BrowserState::ERROR;
|
|
errorMessage = tr(STR_NO_SERVER_URL);
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
|
|
std::string url = (path.find("http") == 0) ? path : UrlUtils::buildUrl(server.url, path);
|
|
LOG_DBG("OPDS", "Fetching: %s", url.c_str());
|
|
OpdsParser parser;
|
|
{
|
|
OpdsParserStream stream{parser};
|
|
if (!HttpDownloader::fetchUrl(url, stream, server.username, server.password)) {
|
|
state = BrowserState::ERROR;
|
|
errorMessage = tr(STR_FETCH_FEED_FAILED);
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!parser) {
|
|
state = BrowserState::ERROR;
|
|
errorMessage = tr(STR_PARSE_FEED_FAILED);
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
|
|
searchTemplate = parser.getSearchTemplate();
|
|
const auto& nextUrl = parser.getNextPageUrl();
|
|
const auto& prevUrl = parser.getPrevPageUrl();
|
|
entries = std::move(parser).getEntries();
|
|
|
|
if (!prevUrl.empty()) {
|
|
entries.insert(entries.begin(), OpdsEntry{OpdsEntryType::NAVIGATION, tr(STR_PREV_PAGE), "", prevUrl, ""});
|
|
}
|
|
if (!nextUrl.empty()) {
|
|
entries.push_back(OpdsEntry{OpdsEntryType::NAVIGATION, tr(STR_NEXT_PAGE), "", nextUrl, ""});
|
|
}
|
|
|
|
selectorIndex = 0;
|
|
state = entries.empty() ? BrowserState::ERROR : BrowserState::BROWSING;
|
|
if (entries.empty()) errorMessage = tr(STR_NO_ENTRIES);
|
|
requestUpdate();
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::navigateToEntry(const OpdsEntry& entry) {
|
|
navigationHistory.push_back(currentPath);
|
|
// Resolve to a full URL so sub-sub-navigation retains parent path context
|
|
const std::string feedUrl = UrlUtils::buildUrl(server.url, currentPath);
|
|
currentPath = UrlUtils::buildUrl(feedUrl, entry.href);
|
|
|
|
state = BrowserState::LOADING;
|
|
statusMessage = tr(STR_LOADING);
|
|
entries.clear();
|
|
selectorIndex = 0;
|
|
requestUpdate(true);
|
|
fetchFeed(currentPath);
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::navigateBack() {
|
|
if (navigationHistory.empty()) {
|
|
onGoHome();
|
|
} else {
|
|
currentPath = navigationHistory.back();
|
|
navigationHistory.pop_back();
|
|
state = BrowserState::LOADING;
|
|
statusMessage = tr(STR_LOADING);
|
|
entries.clear();
|
|
selectorIndex = 0;
|
|
requestUpdate();
|
|
fetchFeed(currentPath);
|
|
}
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::downloadBook(const OpdsEntry& book) {
|
|
state = BrowserState::DOWNLOADING;
|
|
statusMessage = book.title;
|
|
downloadProgress = downloadTotal = 0;
|
|
requestUpdate(true);
|
|
|
|
// Build full download URL relative to the current feed, not the root server URL
|
|
const std::string feedUrl = UrlUtils::buildUrl(server.url, currentPath);
|
|
std::string downloadUrl = UrlUtils::buildUrl(feedUrl, book.href);
|
|
std::string filename =
|
|
"/" + StringUtils::sanitizeFilename((book.author.empty() ? "" : book.author + " - ") + book.title) + ".epub";
|
|
LOG_DBG("OPDS", "Downloading: %s -> %s", downloadUrl.c_str(), filename.c_str());
|
|
|
|
const auto result = HttpDownloader::downloadToFile(
|
|
downloadUrl, filename,
|
|
[this](const size_t downloaded, const size_t total) {
|
|
downloadProgress = downloaded;
|
|
downloadTotal = total;
|
|
requestUpdate(true);
|
|
},
|
|
nullptr, server.username, server.password);
|
|
|
|
if (result == HttpDownloader::OK) {
|
|
clearBookCache(filename);
|
|
state = BrowserState::BROWSING;
|
|
} else {
|
|
state = BrowserState::ERROR;
|
|
errorMessage = tr(STR_DOWNLOAD_FAILED);
|
|
}
|
|
requestUpdate();
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::launchSearch() {
|
|
consumeConfirm = true;
|
|
state = BrowserState::SEARCH_INPUT;
|
|
requestUpdate();
|
|
|
|
auto keyboard = std::make_unique<KeyboardEntryActivity>(renderer, mappedInput, tr(STR_SEARCH));
|
|
startActivityForResult(std::move(keyboard), [this](const ActivityResult& result) {
|
|
state = BrowserState::BROWSING;
|
|
if (!result.isCancelled) {
|
|
performSearch(std::get<KeyboardResult>(result.data).text);
|
|
} else {
|
|
requestUpdate();
|
|
}
|
|
});
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::performSearch(const std::string& query) {
|
|
if (query.empty() || searchTemplate.empty()) {
|
|
state = BrowserState::BROWSING;
|
|
requestUpdate();
|
|
return;
|
|
}
|
|
|
|
auto urlEncode = [](const std::string& s) {
|
|
std::string out;
|
|
out.reserve(s.size() * 3);
|
|
for (unsigned char c : s) {
|
|
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
|
|
out += static_cast<char>(c);
|
|
else {
|
|
char buf[4];
|
|
snprintf(buf, sizeof(buf), "%%%02X", c);
|
|
out += buf;
|
|
}
|
|
}
|
|
return out;
|
|
};
|
|
|
|
std::string url = searchTemplate;
|
|
const std::string placeholder = "{searchTerms}";
|
|
const size_t pos = url.find(placeholder);
|
|
if (pos != std::string::npos) url.replace(pos, placeholder.length(), urlEncode(query));
|
|
|
|
navigationHistory.push_back(currentPath); // <-- add this
|
|
currentPath = url; // <-- add this
|
|
|
|
state = BrowserState::LOADING;
|
|
statusMessage = tr(STR_LOADING);
|
|
requestUpdate(true);
|
|
fetchFeed(url);
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::checkAndConnectWifi() {
|
|
if (WiFi.status() == WL_CONNECTED && WiFi.localIP() != IPAddress(0, 0, 0, 0)) {
|
|
state = BrowserState::LOADING;
|
|
statusMessage = tr(STR_LOADING);
|
|
requestUpdate();
|
|
fetchFeed(currentPath);
|
|
return;
|
|
}
|
|
launchWifiSelection();
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::launchWifiSelection() {
|
|
state = BrowserState::WIFI_SELECTION;
|
|
requestUpdate();
|
|
|
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
|
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
|
|
}
|
|
|
|
void OpdsBookBrowserActivity::onWifiSelectionComplete(const bool connected) {
|
|
if (connected) {
|
|
state = BrowserState::LOADING;
|
|
statusMessage = tr(STR_LOADING);
|
|
requestUpdate(true);
|
|
fetchFeed(currentPath);
|
|
} else {
|
|
// Leave WiFi up; onExit's silent reboot handles teardown without fragmenting.
|
|
state = BrowserState::ERROR;
|
|
errorMessage = tr(STR_WIFI_CONN_FAILED);
|
|
requestUpdate();
|
|
}
|
|
}
|