feat: add OPDS search support

- Parse OpenSearch template URL from OPDS feed
- Launch keyboard entry on Left button when search is available
- URL-encode search query and fetch results feed
- Guard against stale Confirm press auto-downloading after search
- Handle absolute search result URLs in fetchFeed
This commit is contained in:
kira
2026-04-12 12:15:48 -04:00
parent 5c12f2f01e
commit 3d0fcd297d
4 changed files with 111 additions and 45 deletions
+31 -38
View File
@@ -33,7 +33,6 @@ size_t OpdsParser::write(const uint8_t* xmlData, const size_t length) {
XML_SetElementHandler(parser, startElement, endElement);
XML_SetCharacterDataHandler(parser, characterData);
// Parse in chunks to avoid large buffer allocations
const char* currentPos = reinterpret_cast<const char*>(xmlData);
size_t remaining = length;
constexpr size_t chunkSize = 1024;
@@ -78,6 +77,7 @@ bool OpdsParser::error() const { return errorOccured; }
void OpdsParser::clear() {
entries.clear();
searchTemplate.clear(); // Reset search template on clear
currentEntry = OpdsEntry{};
currentText.clear();
inEntry = false;
@@ -109,7 +109,36 @@ const char* OpdsParser::findAttribute(const XML_Char** atts, const char* name) {
void XMLCALL OpdsParser::startElement(void* userData, const XML_Char* name, const XML_Char** atts) {
auto* self = static_cast<OpdsParser*>(userData);
// Check for entry element (with or without namespace prefix)
// Handle feed-level or entry-level links
if (strcmp(name, "link") == 0 || strstr(name, ":link") != nullptr) {
const char* rel = findAttribute(atts, "rel");
const char* type = findAttribute(atts, "type");
const char* href = findAttribute(atts, "href");
if (href) {
// 1. Search Template: Look for search relation
if (rel && strcmp(rel, "search") == 0) {
self->searchTemplate = href;
}
if (self->inEntry) {
// 2. Book: acquisition link with epub type
if (rel && type && strstr(rel, "opds-spec.org/acquisition") != nullptr &&
strcmp(type, "application/epub+zip") == 0) {
self->currentEntry.type = OpdsEntryType::BOOK;
self->currentEntry.href = href;
}
// 3. Navigation: atom+xml type
else if (type && strstr(type, "application/atom+xml") != nullptr) {
if (self->currentEntry.type != OpdsEntryType::BOOK) {
self->currentEntry.type = OpdsEntryType::NAVIGATION;
self->currentEntry.href = href;
}
}
}
}
}
if (strcmp(name, "entry") == 0 || strstr(name, ":entry") != nullptr) {
self->inEntry = true;
self->currentEntry = OpdsEntry{};
@@ -118,64 +147,34 @@ void XMLCALL OpdsParser::startElement(void* userData, const XML_Char* name, cons
if (!self->inEntry) return;
// Check for title element
if (strcmp(name, "title") == 0 || strstr(name, ":title") != nullptr) {
self->inTitle = true;
self->currentText.clear();
return;
}
// Check for author element
if (strcmp(name, "author") == 0 || strstr(name, ":author") != nullptr) {
self->inAuthor = true;
return;
}
// Check for author name element
if (self->inAuthor && (strcmp(name, "name") == 0 || strstr(name, ":name") != nullptr)) {
self->inAuthorName = true;
self->currentText.clear();
return;
}
// Check for id element
if (strcmp(name, "id") == 0 || strstr(name, ":id") != nullptr) {
self->inId = true;
self->currentText.clear();
return;
}
// Check for link element
if (strcmp(name, "link") == 0 || strstr(name, ":link") != nullptr) {
const char* rel = findAttribute(atts, "rel");
const char* type = findAttribute(atts, "type");
const char* href = findAttribute(atts, "href");
if (href) {
// Check for acquisition link with epub type (this is a downloadable book)
if (rel && type && strstr(rel, "opds-spec.org/acquisition") != nullptr &&
strcmp(type, "application/epub+zip") == 0) {
self->currentEntry.type = OpdsEntryType::BOOK;
self->currentEntry.href = href;
}
// Check for navigation link (subsection or no rel specified with atom+xml type)
else if (type && strstr(type, "application/atom+xml") != nullptr) {
// Only set navigation link if we don't already have an epub link
if (self->currentEntry.type != OpdsEntryType::BOOK) {
self->currentEntry.type = OpdsEntryType::NAVIGATION;
self->currentEntry.href = href;
}
}
}
}
}
void XMLCALL OpdsParser::endElement(void* userData, const XML_Char* name) {
auto* self = static_cast<OpdsParser*>(userData);
// Check for entry end
if (strcmp(name, "entry") == 0 || strstr(name, ":entry") != nullptr) {
// Only add entry if it has required fields (title and href)
if (!self->currentEntry.title.empty() && !self->currentEntry.href.empty()) {
self->entries.push_back(self->currentEntry);
}
@@ -186,7 +185,6 @@ void XMLCALL OpdsParser::endElement(void* userData, const XML_Char* name) {
if (!self->inEntry) return;
// Check for title end
if (strcmp(name, "title") == 0 || strstr(name, ":title") != nullptr) {
if (self->inTitle) {
self->currentEntry.title = self->currentText;
@@ -195,13 +193,11 @@ void XMLCALL OpdsParser::endElement(void* userData, const XML_Char* name) {
return;
}
// Check for author end
if (strcmp(name, "author") == 0 || strstr(name, ":author") != nullptr) {
self->inAuthor = false;
return;
}
// Check for author name end
if (self->inAuthor && (strcmp(name, "name") == 0 || strstr(name, ":name") != nullptr)) {
if (self->inAuthorName) {
self->currentEntry.author = self->currentText;
@@ -210,7 +206,6 @@ void XMLCALL OpdsParser::endElement(void* userData, const XML_Char* name) {
return;
}
// Check for id end
if (strcmp(name, "id") == 0 || strstr(name, ":id") != nullptr) {
if (self->inId) {
self->currentEntry.id = self->currentText;
@@ -222,8 +217,6 @@ void XMLCALL OpdsParser::endElement(void* userData, const XML_Char* name) {
void XMLCALL OpdsParser::characterData(void* userData, const XML_Char* s, const int len) {
auto* self = static_cast<OpdsParser*>(userData);
// Only accumulate text when in a text element
if (self->inTitle || self->inAuthorName || self->inId) {
self->currentText.append(s, len);
}
+2
View File
@@ -49,6 +49,7 @@ class OpdsParser final : public Print {
~OpdsParser();
// Disable copy
const std::string& getSearchTemplate() const { return searchTemplate; }
OpdsParser(const OpdsParser&) = delete;
OpdsParser& operator=(const OpdsParser&) = delete;
@@ -85,6 +86,7 @@ class OpdsParser final : public Print {
static void XMLCALL endElement(void* userData, const XML_Char* name);
static void XMLCALL characterData(void* userData, const XML_Char* s, int len);
std::string searchTemplate;
// Helper to find attribute value
static const char* findAttribute(const XML_Char** atts, const char* name);