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 authored and mgautierfr committed Jun 16, 2022
1 parent 5037219 commit b05daa1
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 83 deletions.
1 change: 1 addition & 0 deletions resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"error-downloader-window-title":"Cannot create downloader",
"error-downloader-launch-message":"Impossible to launch downloader, Kiwix-desktop will start but all download functions will not working!",
"error-launch-server-message":"An error has occured!",
"error-archive":"Cannot get the archive",
"open-zim":"Open Zim",
"local-kiwix-server":"Local Kiwix Server",
"random-article":"Random Article",
Expand Down
32 changes: 28 additions & 4 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,36 @@ 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();
values.append(QByteArray::fromStdString(s).toBase64());
try {
auto s = b->getIllustration(48)->getData();
values.append(QByteArray::fromStdString(s).toBase64());
} catch(...) {
values.append(QByteArray());
}
}
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
9 changes: 6 additions & 3 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,20 @@ void KiwixApp::openRandomUrl(bool newTab)
if (zimId.isEmpty()) {
return;
}
auto reader = m_library.getReader(zimId);

try {
auto entry = reader->getRandomPage();
auto archive = m_library.getArchive(zimId);
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);
} catch (std::out_of_range& e) {
showMessage(gt("error-archive"), gt("error-title"), QMessageBox::Information);
}
}

Expand Down
18 changes: 4 additions & 14 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,14 @@ 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());
} catch (std::out_of_range& e) {
return nullptr;
}
return m_library.getArchiveById(zimId.toStdString());
}

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>();
try {
searcher->add_reader(m_library.getReaderById(zimId.toStdString()));
} catch(std::out_of_range& e) {
return nullptr;
}
return searcher;
return m_library.getSearcherById(zimId.toStdString());
}

QStringList Library::getBookIds() const
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
37 changes: 24 additions & 13 deletions src/readinglistbar.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "readinglistbar.h"
#include "ui_readinglistbar.h"
#include "kiwixapp.h"
#include "zim/error.h"

#include <QListWidgetItem>

Expand Down Expand Up @@ -31,20 +32,30 @@ void ReadingListBar::setupList()
auto listWidget = ui->listWidget;
listWidget->clear();
for(auto& bookmark:bookmarks) {
auto reader = library->getReader(QString::fromStdString(bookmark.getBookId()));
if (reader == nullptr)
std::shared_ptr<zim::Archive> archive;
try {
archive = library->getArchive(QString::fromStdString(bookmark.getBookId()));
} catch (std::out_of_range& e) {
continue;
std::string content;
std::string mimeType;
reader->getFavicon(content, mimeType);
QPixmap pixmap;
pixmap.loadFromData(reinterpret_cast<const uchar*>(content.data()), content.size());
auto icon = QIcon(pixmap);
auto item = new QListWidgetItem(
icon,
QString::fromStdString(bookmark.getTitle()),
listWidget);
item->setTextAlignment(Qt::TextWordWrap);
}
try {
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);
auto item = new QListWidgetItem(
icon,
QString::fromStdString(bookmark.getTitle()),
listWidget);
item->setTextAlignment(Qt::TextWordWrap);
} catch (zim::EntryNotFound& e) {
auto item = new QListWidgetItem(
QString::fromStdString(bookmark.getTitle()),
listWidget);
item->setTextAlignment(Qt::TextWordWrap);
}
}
}

Expand Down
41 changes: 22 additions & 19 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 @@ -18,25 +19,26 @@ void SuggestionListWorker::run()
return;
auto qurl = current->url();
auto currentZimId = qurl.host().split(".")[0];
auto reader = KiwixApp::instance()->getLibrary()->getReader(currentZimId);
QUrl url;
url.setScheme("zim");
if (reader) {
try {
auto archive = KiwixApp::instance()->getLibrary()->getArchive(currentZimId);
QUrl url;
url.setScheme("zim");
url.setHost(currentZimId + ".zim");
kiwix::SuggestionsList_t suggestions;
reader->searchSuggestionsSmart(m_text.toStdString(), 15, suggestions);
for (auto& suggestion: suggestions) {
QString path = QString("/") + QString::fromStdString(suggestion.getPath());
int suggestionsCount = 15;
auto prefix = m_text.toStdString();
auto suggestionSearcher = zim::SuggestionSearcher(*archive);
auto suggestionSearch = suggestionSearcher.suggest(prefix);
const auto suggestions = suggestionSearch.getResults(0, suggestionsCount);
for (auto current : suggestions) {
QString path = QString("/") + QString::fromStdString(current.getPath());
url.setPath(path);
suggestionList.append(QString::fromStdString(suggestion.getTitle()));
suggestionList.append(QString::fromStdString(current.getTitle()));
urlList.append(url);
}
}

// Propose fulltext search
url.setPath("");
if (reader) {
if (reader->hasFulltextIndex()) {
// Propose fulltext search
url.setPath("");
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 All @@ -47,11 +49,12 @@ void SuggestionListWorker::run()
suggestionList.append(m_text + " (" + gt("fulltext-search") + ")");
urlList.append(url);
}
} else {
// We do not allow multi zim search for now.
// We don't have a correct UI to select on which zim search,
// how to display results, ...
//url.setHost("library.search");
} catch (std::out_of_range& e) {
// Impossible to find the requested archive (bug ?)
// We could propose a suggestion to do multi-zim search with:
// url.setHost("library.search");
// but we don't have a correct UI to select on which zim search, how to display results, ...
// So do nothing for now
}
emit(searchFinished(suggestionList, urlList, m_token));
}
56 changes: 37 additions & 19 deletions src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,27 @@

#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");
}

void
UrlSchemeHandler::handleContentRequest(QWebEngineUrlRequestJob *request)
{
Expand All @@ -23,27 +39,29 @@ 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) {
request->fail(QWebEngineUrlRequestJob::UrlNotFound);
return;
std::shared_ptr<zim::Archive> archive;
try {
archive = library->getArchive(zim_id);
} catch (std::out_of_range& e) {
request->fail(QWebEngineUrlRequestJob::UrlNotFound);
return;
}
try {
kiwix::Entry entry = reader->getEntryFromPath(url);
auto entry = getEntryFromPath(*archive, url);
auto item = entry.getItem(true);
if (entry.isRedirect()) {
entry = entry.getFinalEntry();
auto path = QString("/") + QString::fromStdString(entry.getPath());
auto path = QString("/") + QString::fromStdString(item.getPath());
qurl.setPath(path);
request->redirect(qurl);
return;
}

BlobBuffer* buffer = new BlobBuffer(entry.getBlob());
auto mimeType = QByteArray::fromStdString(entry.getMimetype());
BlobBuffer* buffer = new BlobBuffer(item.getData(0));
auto mimeType = QByteArray::fromStdString(item.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 +79,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 +122,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);
} catch(std::runtime_error&) {
auto searcher = app->getLibrary()->getSearcher(bookId);
search = make_shared<zim::Search>(searcher->search(searchQuery));
} catch(...) {
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.setSearchBookQuery("content="+bookId.toStdString());
renderer.setProtocolPrefix("zim://");
Expand Down
24 changes: 15 additions & 9 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <QWebEngineSettings>
#include <QWebEngineHistory>
#include <QVBoxLayout>

#include <zim/error.h>

void WebViewBackMenu::showEvent(QShowEvent *)
{
Expand Down Expand Up @@ -167,18 +167,24 @@ 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) {
std::shared_ptr<zim::Archive> archive;
try {
archive = app->getLibrary()->getArchive(m_currentZimId);
} catch (std::out_of_range& e) {
return;
}
std::string favicon, _mimetype;
reader->getFavicon(favicon, _mimetype);
QPixmap pixmap;
pixmap.loadFromData((const uchar*)favicon.data(), favicon.size());
m_icon = QIcon(pixmap);
emit iconChanged(m_icon);
auto zoomFactor = app->getSettingsManager()->getZoomFactorByZimId(zimId);
this->setZoomFactor(zoomFactor);
try {
std::string 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);
emit iconChanged(m_icon);
} catch (zim::EntryNotFound& e) {}
}

void WebView::wheelEvent(QWheelEvent *event) {
Expand Down

0 comments on commit b05daa1

Please sign in to comment.