Skip to content

Commit

Permalink
Merge pull request #831 from kiwix/removeLibkiwixDeprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Jun 16, 2022
2 parents 5037219 + b6934d7 commit c3eace1
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 89 deletions.
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Build-Depends: debhelper-compat (= 13),
pkg-config,
qtbase5-dev,
qtwebengine5-dev,
libkiwix-dev (>= 10.0.0~)
libkiwix-dev (>= 11.0.0~)
Standards-Version: 4.5.0
Homepage: https://www.kiwix.org/
Rules-Requires-Root: no
Expand Down
4 changes: 2 additions & 2 deletions kiwix-desktop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ unix {
INSTALLS += mime_file
}

PKGCONFIG_CFLAGS = $$system(pkg-config --cflags $$PKGCONFIG_OPTION \"kiwix >= 10.0.0 libzim >= 7.2.0\")
PKGCONFIG_CFLAGS = $$system(pkg-config --cflags $$PKGCONFIG_OPTION \"kiwix >= 11.0.0 libzim >= 7.2.0\")

QMAKE_CXXFLAGS += $$PKGCONFIG_CFLAGS
QMAKE_CFLAGS += $$PKGCONFIG_CFLAGS

LIBS += $$system(pkg-config --libs $$PKGCONFIG_OPTION \"kiwix >= 10.0.0 libzim >= 7.2.0\")
LIBS += $$system(pkg-config --libs $$PKGCONFIG_OPTION \"kiwix >= 11.0.0 libzim >= 7.2.0\")

RESOURCES += \
resources/kiwix.qrc \
Expand Down
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
40 changes: 34 additions & 6 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ QStringList ContentManager::getBookInfos(QString id, const QStringList &keys)

if (nullptr == b){
for(auto& key:keys) {
(void) key;
values.append("");
}
return values;
Expand All @@ -80,12 +81,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 Expand Up @@ -141,8 +166,10 @@ QStringList ContentManager::updateDownloadInfos(QString id, const QStringList &k
{
QStringList values;
if (!mp_downloader) {
for(auto& key: keys)
for(auto& key: keys) {
(void) key;
values.append("");
}
return values;
}
auto& b = mp_library->getBookById(id);
Expand Down Expand Up @@ -235,7 +262,8 @@ QString ContentManager::downloadBook(const QString &id)
}();
auto downloadPath = KiwixApp::instance()->getSettingsManager()->getDownloadDir();
QStorageInfo storage(downloadPath);
if (book.getSize() > storage.bytesAvailable()) {
auto bytesAvailable = storage.bytesAvailable();
if (bytesAvailable == -1 || book.getSize() > (unsigned) bytesAvailable) {
return "storage_error";
}
auto booksList = mp_library->getBookIds();
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
Loading

0 comments on commit c3eace1

Please sign in to comment.