Implement search in the contentManager.

This commit is contained in:
Matthieu Gautier 2018-12-02 17:09:39 +01:00
parent b1b0c1d2c3
commit 0b1296cc86
5 changed files with 29 additions and 5 deletions

View File

@ -168,7 +168,7 @@ button {
<div id="app">
<div id="searchBar">
<form>
<input id="searchInput" type="text" placeholder="Search files" readonly/>
<input id="searchInput" type="text" placeholder="Search files" oninput="contentManager.setSearch(this.value)"/>
</form>
</div>
<div id="bookList">

View File

@ -23,7 +23,6 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
setCurrentLanguage(QLocale().name().split("_").at(0));
connect(mp_library, &Library::booksChanged, this, [=]() {emit(this->booksChanged());});
connect(this, &ContentManager::remoteParamsChanged, this, &ContentManager::updateRemoteLibrary);
connect(this, &ContentManager::booksChanged, this, [=]() {if (!m_local) this->updateRemoteLibrary(); });
}
@ -218,6 +217,8 @@ void ContentManager::updateRemoteLibrary() {
QUrlQuery query;
query.addQueryItem("lang", m_currentLanguage);
query.addQueryItem("count", QString::number(0));
if (not m_searchQuery.isEmpty())
query.addQueryItem("q", m_searchQuery);
QUrl url;
url.setScheme("http");
url.setHost(CATALOG_HOST);
@ -225,17 +226,27 @@ void ContentManager::updateRemoteLibrary() {
url.setPath("/catalog/search");
url.setQuery(query);
qInfo() << "Downloading" << url;
m_remoteLibrary = kiwix::Library();
kiwix::Manager manager(&m_remoteLibrary);
try {
auto allContent = kiwix::download(url.toString().toStdString());
manager.readOpds(allContent, CATALOG_HOST);
m_totalBooks = manager.m_totalBooks;
} catch (runtime_error&) {}
emit(booksChanged());
}
void ContentManager::setSearch(const QString &search)
{
m_searchQuery = search;
if (m_local)
emit(booksChanged());
else
emit(remoteParamsChanged());
}
QStringList ContentManager::getBookIds() {
if (m_local) {
return mp_library->getBookIds();
return mp_library->listBookIds(m_searchQuery);
} else {
auto bookIds = m_remoteLibrary.getBooksIds();
QStringList list;

View File

@ -27,9 +27,9 @@ private:
kiwix::Library m_remoteLibrary;
kiwix::Downloader* mp_downloader;
ContentManagerView* mp_view;
int m_totalBooks = 0;
bool m_local = true;
QString m_currentLanguage;
QString m_searchQuery;
void setCurrentLanguage(QString language);
QStringList getBookIds();
@ -46,6 +46,7 @@ public slots:
QStringList updateDownloadInfos(QString id, const QStringList& keys);
QString downloadBook(const QString& id);
void updateRemoteLibrary();
void setSearch(const QString& search);
};
#endif // CONTENTMANAGER_H

View File

@ -93,6 +93,17 @@ QStringList Library::getBookIds()
return list;
}
QStringList Library::listBookIds(const QString &query)
{
QStringList list;
for(auto& id: m_library.listBooksIds(kiwix::VALID|kiwix::LOCAL,
kiwix::UNSORTED,
query.toStdString())) {
list.append(QString::fromStdString(id));
}
return list;
}
void Library::addBookToLibrary(kiwix::Book &book)
{
m_library.addBook(book);

View File

@ -29,6 +29,7 @@ public:
QString openBookFromPath(const QString& zimPath);
std::shared_ptr<kiwix::Reader> getReader(const QString& zimId);
QStringList getBookIds();
QStringList listBookIds(const QString& query);
const std::vector<kiwix::Bookmark>& getBookmarks() { return m_library.getBookmarks(); }
void addBookToLibrary(kiwix::Book& book);
void addBookmark(kiwix::Bookmark& bookmark);