Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt kiwix-desktop to new libkiwix API. #894

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent),
mp_library(library),
mp_remoteLibrary(std::make_shared<kiwix::Library>()),
mp_downloader(downloader),
m_remoteLibraryManager()
{
Expand Down Expand Up @@ -56,7 +57,7 @@ QStringList ContentManager::getBookInfos(QString id, const QStringList &keys)
return &mp_library->getBookById(id);
} catch (...) {
try {
return &m_remoteLibrary.getBookById(id.toStdString());
return &mp_remoteLibrary->getBookById(id.toStdString());
} catch(...) { return nullptr; }
}
}();
Expand Down Expand Up @@ -179,7 +180,7 @@ QStringList ContentManager::updateDownloadInfos(QString id, const QStringList &k
} catch(...) {
kiwix::Book bCopy(b);
bCopy.setDownloadId("");
mp_library->getKiwixLibrary().addOrUpdateBook(bCopy);
mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy);
mp_library->save();
emit(mp_library->booksChanged());
return values;
Expand All @@ -194,7 +195,7 @@ QStringList ContentManager::updateDownloadInfos(QString id, const QStringList &k
bCopy.setPathValid(true);
// removing book url so that download link in kiwix-serve is not displayed.
bCopy.setUrl("");
mp_library->getKiwixLibrary().addOrUpdateBook(bCopy);
mp_library->getKiwixLibrary()->addOrUpdateBook(bCopy);
mp_library->save();
mp_library->bookmarksChanged();
if (!m_local) {
Expand Down Expand Up @@ -255,7 +256,7 @@ QString ContentManager::downloadBook(const QString &id)
return "";
const auto& book = [&]()->const kiwix::Book& {
try {
return m_remoteLibrary.getBookById(id.toStdString());
return mp_remoteLibrary->getBookById(id.toStdString());
} catch (...) {
return mp_library->getBookById(id);
}
Expand Down Expand Up @@ -407,8 +408,8 @@ void ContentManager::updateLibrary() {

#define CATALOG_URL "library.kiwix.org"
void ContentManager::updateRemoteLibrary(const QString& content) {
m_remoteLibrary = kiwix::Library();
kiwix::Manager manager(&m_remoteLibrary);
mp_remoteLibrary = std::make_shared<kiwix::Library>();
kiwix::Manager manager(mp_remoteLibrary);
manager.readOpds(content.toStdString(), CATALOG_URL);
emit(this->booksChanged());
emit(this->pendingRequest(false));
Expand Down Expand Up @@ -457,8 +458,8 @@ QStringList ContentManager::getBookIds()
return mp_library->listBookIds(filter, m_sortBy, m_sortOrderAsc);
} else {
filter.remote(true);
auto bookIds = m_remoteLibrary.filter(filter);
m_remoteLibrary.sort(bookIds, m_sortBy, m_sortOrderAsc);
auto bookIds = mp_remoteLibrary->filter(filter);
mp_remoteLibrary->sort(bookIds, m_sortBy, m_sortOrderAsc);
QStringList list;
for(auto& bookId:bookIds) {
list.append(QString::fromStdString(bookId));
Expand Down
2 changes: 1 addition & 1 deletion src/contentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ContentManager : public QObject

private:
Library* mp_library;
kiwix::Library m_remoteLibrary;
std::shared_ptr<kiwix::Library> mp_remoteLibrary;
kiwix::Downloader* mp_downloader;
OpdsRequestManager m_remoteLibraryManager;
ContentManagerView* mp_view;
Expand Down
29 changes: 25 additions & 4 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
mp_downloader(nullptr),
mp_manager(nullptr),
mp_mainWindow(nullptr),
m_nameMapper(m_library.getKiwixLibrary(), false),
m_server(&m_library.getKiwixLibrary(), &m_nameMapper)
mp_nameMapper(std::make_shared<kiwix::UpdatableNameMapper>(m_library.getKiwixLibrary(), false)),
mp_server(nullptr)
{
try {
m_translation.setTranslation(QLocale());
Expand Down Expand Up @@ -114,7 +114,9 @@ void KiwixApp::init()

KiwixApp::~KiwixApp()
{
m_server.stop();
if (mp_server) {
mp_server->stop();
}
if (mp_downloader) {
mp_downloader->close();
delete mp_downloader;
Expand Down Expand Up @@ -453,7 +455,26 @@ void KiwixApp::disableItemsOnLibraryPage(bool libraryDisplayed)

void KiwixApp::updateNameMapper()
{
m_nameMapper.update();
mp_nameMapper->update();
}

bool KiwixApp::runServer(const QString& ipAddress, int port) {
auto settings = getSettingsManager();
if (mp_server) {
mp_server->stop();
}
kiwix::Server::Configuration configuration(m_library.getKiwixLibrary(), mp_nameMapper);
configuration.setPort(port);
configuration.setAddress(ipAddress.toStdString());
settings->setKiwixServerPort(port);
settings->setKiwixServerIpAddress(ipAddress);
mp_server.reset(new kiwix::Server(configuration));
return mp_server->start();
}

void KiwixApp::stopServer() {
mp_server->stop();
mp_server.reset();
}

void KiwixApp::printVersions(std::ostream& out) {
Expand Down
8 changes: 5 additions & 3 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ class KiwixApp : public QtSingleApplication
TabBar* getTabWidget() { return getMainWindow()->getTabBar(); }
QAction* getAction(Actions action);
QString getLibraryDirectory() { return m_libraryDirectory; };
kiwix::Server* getLocalServer() { return &m_server; }
SettingsManager* getSettingsManager() { return &m_settingsManager; };
QString getText(const QString &key) { return m_translation.getText(key); };
void setMonitorDir(const QString &dir);
bool isCurrentArticleBookmarked();

bool runServer(const QString& ipAddress, int port);
void stopServer();

public slots:
void openZimFile(const QString& zimfile="");
void openUrl(const QString& url, bool newTab=true);
Expand All @@ -108,8 +110,8 @@ public slots:
ContentManager* mp_manager;
MainWindow* mp_mainWindow;
QErrorMessage* mp_errorDialog;
kiwix::UpdatableNameMapper m_nameMapper;
kiwix::Server m_server;
std::shared_ptr<kiwix::UpdatableNameMapper> mp_nameMapper;
std::unique_ptr<kiwix::Server> mp_server;
Translation m_translation;
QFileSystemWatcher m_watcher;

Expand Down
39 changes: 20 additions & 19 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@
class LibraryManipulator: public kiwix::LibraryManipulator {
public:
LibraryManipulator(Library* p_library)
: kiwix::LibraryManipulator(&p_library->getKiwixLibrary())
: kiwix::LibraryManipulator(p_library->getKiwixLibrary())
, mp_library(p_library)
{}
virtual ~LibraryManipulator() {}
bool addBookToLibrary(kiwix::Book book) {
auto ret = mp_library->m_library.addBook(book);
auto ret = mp_library->mp_library->addBook(book);
emit(mp_library->booksChanged());
return ret;
}
void addBookmarkToLibrary(kiwix::Bookmark bookmark) {
mp_library->m_library.addBookmark(bookmark);
mp_library->mp_library->addBookmark(bookmark);
}
Library* mp_library;
};

Library::Library(const QString& libraryDirectory)
: m_libraryDirectory(libraryDirectory)
: mp_library(std::make_shared<kiwix::Library>()),
m_libraryDirectory(libraryDirectory)
{
auto manipulator = LibraryManipulator(this);
auto manager = kiwix::Manager(&manipulator);
Expand All @@ -44,11 +45,11 @@ Library::~Library()
QString Library::openBookFromPath(const QString &zimPath)
{
try {
auto& book = m_library.getBookByPath(zimPath.toStdString());
auto& book = mp_library->getBookByPath(zimPath.toStdString());
return QString::fromStdString(book.getId());
} catch(std::out_of_range& e) { }

kiwix::Manager manager(&m_library);
kiwix::Manager manager(mp_library);
auto id = manager.addBookFromPathAndGetId(zimPath.toStdString());
if (id == "") {
throw std::invalid_argument("invalid zim file");
Expand All @@ -60,18 +61,18 @@ QString Library::openBookFromPath(const QString &zimPath)

std::shared_ptr<zim::Archive> Library::getArchive(const QString &zimId)
{
return m_library.getArchiveById(zimId.toStdString());
return mp_library->getArchiveById(zimId.toStdString());
}

std::shared_ptr<zim::Searcher> Library::getSearcher(const QString &zimId)
{
return m_library.getSearcherById(zimId.toStdString());
return mp_library->getSearcherById(zimId.toStdString());
}

QStringList Library::getBookIds() const
{
QStringList list;
for(auto& id: m_library.getBooksIds()) {
for(auto& id: mp_library->getBooksIds()) {
list.append(QString::fromStdString(id));
}
return list;
Expand All @@ -80,8 +81,8 @@ QStringList Library::getBookIds() const
QStringList Library::listBookIds(const kiwix::Filter& filter, kiwix::supportedListSortBy sortBy, bool ascending) const
{
QStringList list;
auto bookIds = m_library.filter(filter);
m_library.sort(bookIds, sortBy, ascending);
auto bookIds = mp_library->filter(filter);
mp_library->sort(bookIds, sortBy, ascending);
for(auto& id: bookIds) {
list.append(QString::fromStdString(id));
}
Expand All @@ -90,29 +91,29 @@ QStringList Library::listBookIds(const kiwix::Filter& filter, kiwix::supportedLi

void Library::addBookToLibrary(kiwix::Book &book)
{
m_library.addBook(book);
mp_library->addBook(book);
}

void Library::removeBookFromLibraryById(const QString& id) {
m_library.removeBookById(id.toStdString());
mp_library->removeBookById(id.toStdString());
}

void Library::addBookmark(kiwix::Bookmark &bookmark)
{
m_library.addBookmark(bookmark);
mp_library->addBookmark(bookmark);
emit bookmarksChanged();
}

void Library::removeBookmark(const QString &zimId, const QString &url)
{
m_library.removeBookmark(zimId.toStdString(), url.toStdString());
mp_library->removeBookmark(zimId.toStdString(), url.toStdString());
emit bookmarksChanged();
}

void Library::save()
{
m_library.writeToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"));
m_library.writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
mp_library->writeToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(),"library.xml"));
mp_library->writeBookmarksToFile(kiwix::appendToDirectory(m_libraryDirectory.toStdString(), "library.bookmarks.xml"));
}

void Library::setMonitorDirZims(QStringList zimList)
Expand Down Expand Up @@ -160,7 +161,7 @@ void Library::loadMonitorDir(QString monitorDir)
needsRefresh |= manager.addBookFromPath(book.toStdString());
}
for (auto bookPath : removedZims) {
removeBookFromLibraryById(QString::fromStdString(m_library.getBookByPath(bookPath.toStdString()).getId()));
removeBookFromLibraryById(QString::fromStdString(mp_library->getBookByPath(bookPath.toStdString()).getId()));
}
if (needsRefresh) {
setMonitorDirZims(newDir.values());
Expand All @@ -177,5 +178,5 @@ void Library::asyncLoadMonitorDir(QString dir)

const kiwix::Book &Library::getBookById(QString id) const
{
return m_library.getBookById(id.toStdString());
return mp_library->getBookById(id.toStdString());
}
6 changes: 3 additions & 3 deletions src/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Library : public QObject
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); }
const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = false) const { return mp_library->getBookmarks(onlyValidBookmarks); }
QStringList getLibraryZimsFromDir(QString dir) const;
void setMonitorDirZims(QStringList zimList);
void addBookToLibrary(kiwix::Book& book);
Expand All @@ -42,7 +42,7 @@ class Library : public QObject
void save();
void loadMonitorDir(QString dir);
void asyncLoadMonitorDir(QString dir);
kiwix::Library& getKiwixLibrary() { return m_library; }
std::shared_ptr<kiwix::Library> getKiwixLibrary() { return mp_library; }
public slots:
const kiwix::Book& getBookById(QString id) const;

Expand All @@ -51,7 +51,7 @@ public slots:
void bookmarksChanged();

private:
kiwix::Library m_library;
std::shared_ptr<kiwix::Library> mp_library;
QString m_libraryDirectory;
QStringList m_monitorDirZims;
friend class LibraryManipulator;
Expand Down
27 changes: 13 additions & 14 deletions src/localkiwixserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ LocalKiwixServer::LocalKiwixServer(QWidget *parent) :
QString style(byteContent);
setStyleSheet(style);

mp_server = KiwixApp::instance()->getLocalServer();
m_port = KiwixApp::instance()->getSettingsManager()->getKiwixServerPort();

connect(ui->KiwixServerButton, SIGNAL(clicked()), this, SLOT(runOrStopServer()));
Expand Down Expand Up @@ -81,28 +80,28 @@ void LocalKiwixServer::openInBrowser()
void LocalKiwixServer::runOrStopServer()
{
if (!m_active) {
auto settingsManager = KiwixApp::instance()->getSettingsManager();
m_port = ui->PortChooser->text().toInt();
mp_server->setPort(m_port);
m_ipAddress = (ui->IpChooser->currentText() != gt("all")) ? ui->IpChooser->currentText() : "0.0.0.0";
settingsManager->setKiwixServerPort(m_port);
settingsManager->setKiwixServerIpAddress(m_ipAddress);
mp_server->setAddress(m_ipAddress.toStdString());
m_ipAddress = (m_ipAddress != "0.0.0.0") ? m_ipAddress : QString::fromStdString(kiwix::getBestPublicIp());
ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port));
ui->IpAddress->setReadOnly(true);
if (!mp_server->start()) {
m_ipAddress = ui->IpChooser->currentText();

m_active = KiwixApp::instance()->runServer(
(m_ipAddress != gt("all") ? m_ipAddress : "0.0.0.0"),
m_port
);

if (!m_active) {
QMessageBox messageBox;
messageBox.critical(0,gt("error-title"),gt("error-launch-server-message"));
return;
}
m_active = true;
} else {
mp_server->stop();
KiwixApp::instance()->stopServer();
m_active = false;
}

if (m_active) {
// Update UI to display how to acces the server
m_ipAddress = m_ipAddress != gt("all") ? m_ipAddress : QString::fromStdString(kiwix::getBestPublicIp());
ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port));
ui->IpAddress->setReadOnly(true);
ui->KiwixServerButton->setText(gt("stop-kiwix-server"));
ui->KiwixServerText->setText(gt("kiwix-server-running-message"));
ui->stackedWidget->setCurrentIndex(1);
Expand Down
1 change: 0 additions & 1 deletion src/localkiwixserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public slots:

private:
Ui::LocalKiwixServer *ui;
kiwix::Server* mp_server;
bool m_active = false;
QString m_ipAddress;
int m_port;
Expand Down
9 changes: 6 additions & 3 deletions src/urlschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ UrlSchemeHandler::handleSearchRequest(QWebEngineUrlRequestJob* request)
request->fail(QWebEngineUrlRequestJob::UrlInvalid);
return;
}
IdNameMapper nameMapper;
kiwix::SearchRenderer renderer(search->getResults(start, pageLength), &nameMapper, search->getEstimatedMatches(),
start);
auto nameMapper = std::make_shared<IdNameMapper>();
kiwix::SearchRenderer renderer(
search->getResults(start, pageLength),
nameMapper,
search->getEstimatedMatches(),
start);
renderer.setSearchPattern(searchQuery);
renderer.setSearchBookQuery("content="+bookId.toStdString());
renderer.setProtocolPrefix("zim://");
Expand Down