Skip to content

Commit

Permalink
Replace deprecated functions
Browse files Browse the repository at this point in the history
This replaces the deprecated Reader and Favicon functions from libkiwix

kiwix::Reader functions are converted to use zim::Archive
Favicon functions are converted to use Illustration
Fix #805
  • Loading branch information
juuz0 committed Apr 13, 2022
1 parent 46b3398 commit 88243fd
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 40 deletions.
26 changes: 23 additions & 3 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,33 @@ QStringList ContentManager::getBookInfos(QString id, const QStringList &keys)
ADD_V("url", getUrl);
ADD_V("name", getName);
ADD_V("origId", getOrigId);
ADD_V("faviconMimeType", getFaviconMimeType);
ADD_V("downloadId", getDownloadId);
ADD_V("faviconUrl", getFaviconUrl);
if (key == "favicon") {
auto s = b->getFavicon();
auto s = b->getIllustration(48)->getData();
values.append(QByteArray::fromStdString(s).toBase64());
}
if (key == "faviconMimeType") {
std::string mimeType;
try {
auto item = b->getIllustration(48);
mimeType = item->mimeType;
} catch (...) {
const kiwix::Book::Illustration tempIllustration;
mimeType = tempIllustration.mimeType;
}
values.append(QString::fromStdString(mimeType));
}
if (key == "faviconUrl") {
std::string url;
try {
auto item = b->getIllustration(48);
url = item->url;
} catch (...) {
const kiwix::Book::Illustration tempIllustration;
url = tempIllustration.url;
}
values.append(QString::fromStdString(url));
}
if (key == "size") {
values.append(QString::number(b->getSize()));
}
Expand Down
6 changes: 3 additions & 3 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,16 @@ void KiwixApp::openRandomUrl(bool newTab)
if (zimId.isEmpty()) {
return;
}
auto reader = m_library.getReader(zimId);
auto archive = m_library.getArchive(zimId);
try {
auto entry = reader->getRandomPage();
auto entry = archive->getRandomEntry();

QUrl url;
url.setScheme("zim");
url.setHost(zimId + ".zim");
url.setPath("/" + QString::fromStdString(entry.getPath()));
openUrl(url, newTab);
} catch ( const kiwix::NoEntry& ) {
} catch (const zim::EntryNotFound& e) {
showMessage(gt("random-article-error"), gt("error-title"), QMessageBox::Information);
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,27 @@ QString Library::openBookFromPath(const QString &zimPath)
return QString::fromStdString(id);
}

std::shared_ptr<kiwix::Reader> Library::getReader(const QString &zimId)
std::shared_ptr<zim::Archive> Library::getArchive(const QString &zimId)
{
try {
return m_library.getReaderById(zimId.toStdString());
return m_library.getArchiveById(zimId.toStdString());
} catch (std::out_of_range& e) {
return nullptr;
}
}

std::shared_ptr<kiwix::Searcher> Library::getSearcher(const QString &zimId)
std::shared_ptr<zim::Searcher> Library::getSearcher(const QString &zimId)
{
auto searcher = std::make_shared<kiwix::Searcher>();
std::shared_ptr<zim::Searcher> searcher;
try {
searcher->add_reader(m_library.getReaderById(zimId.toStdString()).get());
auto archive = getArchive(zimId);
if (archive) {
if (!searcher) {
searcher = std::make_shared<zim::Searcher>(*archive);
} else {
searcher->addArchive(*archive);
}
}
} catch(std::out_of_range& e) {
return nullptr;
}
Expand Down
6 changes: 4 additions & 2 deletions src/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <kiwix/library.h>
#include <kiwix/reader.h>
#include <kiwix/searcher.h>
#include <zim/archive.h>
#include <zim/search.h>
#include <qstring.h>
#include <memory>

Expand All @@ -28,8 +30,8 @@ class Library : public QObject
Library(const QString& libraryDirectory);
virtual ~Library();
QString openBookFromPath(const QString& zimPath);
std::shared_ptr<kiwix::Reader> getReader(const QString& zimId);
std::shared_ptr<kiwix::Searcher> getSearcher(const QString& zimId);
std::shared_ptr<zim::Archive> getArchive(const QString& zimId);
std::shared_ptr<zim::Searcher> getSearcher(const QString& zimId);
QStringList getBookIds() const;
QStringList listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const;
const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = false) const { return m_library.getBookmarks(onlyValidBookmarks); }
Expand Down
10 changes: 5 additions & 5 deletions src/readinglistbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ void ReadingListBar::setupList()
auto listWidget = ui->listWidget;
listWidget->clear();
for(auto& bookmark:bookmarks) {
auto reader = library->getReader(QString::fromStdString(bookmark.getBookId()));
if (reader == nullptr)
auto archive = library->getArchive(QString::fromStdString(bookmark.getBookId()));
if (archive == nullptr)
continue;
std::string content;
std::string mimeType;
reader->getFavicon(content, mimeType);
auto illustration = archive->getIllustrationItem(48);
std::string content = illustration.getData();
std::string mimeType = illustration.getMimetype();
QPixmap pixmap;
pixmap.loadFromData(reinterpret_cast<const uchar*>(content.data()), content.size());
auto icon = QIcon(pixmap);
Expand Down
35 changes: 30 additions & 5 deletions src/suggestionlistworker.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "suggestionlistworker.h"
#include "kiwixapp.h"
#include <zim/suggestion.h>

SuggestionListWorker::SuggestionListWorker(const QString& text, int token, QObject *parent)
: QThread(parent),
Expand All @@ -8,6 +9,30 @@ SuggestionListWorker::SuggestionListWorker(const QString& text, int token, QObje
{
}

std::string lcAll(std::string title)
{
QString word = QString::fromStdString(title);
return word.toLower().toStdString();
}

bool searchSuggestionsSmart(std::shared_ptr<zim::Archive> archive,
std::string prefix,
unsigned int suggestionsCount,
kiwix::SuggestionsList_t& results)
{
auto suggestionSearcher = zim::SuggestionSearcher(*archive);
if (archive->hasTitleIndex()) {
auto suggestionSearch = suggestionSearcher.suggest(prefix);
const auto suggestions = suggestionSearch.getResults(0, suggestionsCount);
for (auto current : suggestions) {
kiwix::SuggestionItem suggestion(current.getTitle(), lcAll(current.getTitle()),
current.getPath(), current.getSnippet());
results.push_back(suggestion);
}
}
return results.size() > 0;
}

void SuggestionListWorker::run()
{
QStringList suggestionList;
Expand All @@ -18,13 +43,13 @@ void SuggestionListWorker::run()
return;
auto qurl = current->url();
auto currentZimId = qurl.host().split(".")[0];
auto reader = KiwixApp::instance()->getLibrary()->getReader(currentZimId);
auto archive = KiwixApp::instance()->getLibrary()->getArchive(currentZimId);
QUrl url;
url.setScheme("zim");
if (reader) {
if (archive) {
url.setHost(currentZimId + ".zim");
kiwix::SuggestionsList_t suggestions;
reader->searchSuggestionsSmart(m_text.toStdString(), 15, suggestions);
searchSuggestionsSmart(archive, m_text.toStdString(), 15, suggestions);
for (auto& suggestion: suggestions) {
QString path = QString("/") + QString::fromStdString(suggestion.getPath());
url.setPath(path);
Expand All @@ -35,8 +60,8 @@ void SuggestionListWorker::run()

// Propose fulltext search
url.setPath("");
if (reader) {
if (reader->hasFulltextIndex()) {
if (archive) {
if (archive->hasFulltextIndex()) {
// The host is used to determine the currentZimId
// The content query item is used to know in which zim search (as for kiwix-serve)
url.setHost(currentZimId + ".search");
Expand Down
58 changes: 44 additions & 14 deletions src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,41 @@

#include <kiwix/search_renderer.h>
#include <kiwix/name_mapper.h>
#include <zim/search.h>
#include <zim/entry.h>
#include <zim/error.h>


UrlSchemeHandler::UrlSchemeHandler()
{
}

zim::Entry getEntryFromPath(const zim::Archive& archive, const std::string& path)
{
try {
return archive.getEntryByPath(path);
} catch (zim::EntryNotFound& e) {
if (path.empty() || path == "/") {
return archive.getMainEntry();
}
}
throw zim::EntryNotFound("Cannot find entry for non empty path");
}

zim::Entry getFinalEntry(zim::Entry entry)
{
int loopCounter = 42;
auto final_entry = entry;
while (final_entry.isRedirect() && loopCounter--) {
final_entry = final_entry.getRedirectEntry();
}
// Prevent infinite loops.
if (final_entry.isRedirect()) {
throw zim::EntryNotFound("bad entry");
}
return final_entry;
}

void
UrlSchemeHandler::handleContentRequest(QWebEngineUrlRequestJob *request)
{
Expand All @@ -23,27 +53,27 @@ UrlSchemeHandler::handleContentRequest(QWebEngineUrlRequestJob *request)
auto library = KiwixApp::instance()->getLibrary();
auto zim_id = qurl.host();
zim_id.resize(zim_id.length()-4);
auto reader = library->getReader(zim_id);
if ( reader == nullptr) {
auto archive = library->getArchive(zim_id);
if ( archive == nullptr) {
request->fail(QWebEngineUrlRequestJob::UrlNotFound);
return;
}
try {
kiwix::Entry entry = reader->getEntryFromPath(url);
auto entry = getEntryFromPath(*archive, url);
if (entry.isRedirect()) {
entry = entry.getFinalEntry();
entry = getFinalEntry(entry);
auto path = QString("/") + QString::fromStdString(entry.getPath());
qurl.setPath(path);
request->redirect(qurl);
return;
}

BlobBuffer* buffer = new BlobBuffer(entry.getBlob());
auto mimeType = QByteArray::fromStdString(entry.getMimetype());
BlobBuffer* buffer = new BlobBuffer(entry.getItem().getData(0));
auto mimeType = QByteArray::fromStdString(entry.getItem(true).getMimetype());
mimeType = mimeType.split(';')[0];
connect(request, &QObject::destroyed, buffer, &QObject::deleteLater);
request->reply(mimeType, buffer);
} catch (kiwix::NoEntry&) {
} catch (zim::EntryNotFound&) {
request->fail(QWebEngineUrlRequestJob::UrlNotFound);
}
}
Expand All @@ -61,8 +91,9 @@ UrlSchemeHandler::handleMetaRequest(QWebEngineUrlRequestJob* request)
try {
auto library = KiwixApp::instance()->getLibrary();
auto book = library->getBookById(zimId);
std::string content= book.getFavicon();
std::string mimeType = book.getFaviconMimeType();
auto illustration = book.getIllustration(48);
std::string content = illustration->getData();
std::string mimeType = illustration->mimeType;
QBuffer* buffer = new QBuffer;
buffer->setData(content.data(), content.size());
connect(request, &QObject::destroyed, buffer, &QObject::deleteLater);
Expand Down Expand Up @@ -103,18 +134,17 @@ UrlSchemeHandler::handleSearchRequest(QWebEngineUrlRequestJob* request)
if (ok)
pageLength = temp;

auto end = start + pageLength;

auto searcher = app->getLibrary()->getSearcher(bookId);
std::shared_ptr<zim::Search> search;
try {
searcher->search(searchQuery, start, end);
search = make_shared<zim::Search>(searcher->search(searchQuery));
} catch(std::runtime_error&) {
request->fail(QWebEngineUrlRequestJob::UrlInvalid);
return;
}

IdNameMapper nameMapper;
kiwix::SearchRenderer renderer(searcher.get(), &nameMapper);
kiwix::SearchRenderer renderer(search->getResults(start, pageLength), &nameMapper, search->getEstimatedMatches(),
start);
renderer.setSearchPattern(searchQuery);
renderer.setSearchContent(bookId.toStdString());
renderer.setProtocolPrefix("zim://");
Expand Down
8 changes: 5 additions & 3 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,14 @@ void WebView::onUrlChanged(const QUrl& url) {
m_currentZimId = zimId;
emit zimIdChanged(m_currentZimId);
auto app = KiwixApp::instance();
auto reader = app->getLibrary()->getReader(m_currentZimId);
if (!reader) {
auto archive = app->getLibrary()->getArchive(m_currentZimId);
if (!archive) {
return;
}
std::string favicon, _mimetype;
reader->getFavicon(favicon, _mimetype);
auto item = archive->getIllustrationItem(48);
favicon = item.getData();
_mimetype = item.getMimetype();
QPixmap pixmap;
pixmap.loadFromData((const uchar*)favicon.data(), favicon.size());
m_icon = QIcon(pixmap);
Expand Down

0 comments on commit 88243fd

Please sign in to comment.