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

OPDS based languages and categories #960

Merged
merged 6 commits into from
Aug 2, 2023
Merged
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
2 changes: 0 additions & 2 deletions kiwix-desktop.pro
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ SOURCES += \
src/fullscreenwindow.cpp \
src/fullscreennotification.cpp \
src/zimview.cpp \
src/static_content.cpp

HEADERS += \
src/contentmanagerdelegate.h \
Expand Down Expand Up @@ -115,7 +114,6 @@ HEADERS += \
src/fullscreennotification.h \
src/menuproxystyle.h \
src/zimview.h \
src/static_content.h

FORMS += \
src/contentmanagerview.ui \
Expand Down
74 changes: 70 additions & 4 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "contentmanager.h"

#include "kiwixapp.h"
#include "static_content.h"
#include <kiwix/manager.h>
#include <kiwix/tools.h>

Expand Down Expand Up @@ -68,6 +67,10 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
connect(mp_view->getView(), SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenu(const QPoint &)));
connect(this, &ContentManager::pendingRequest, mp_view, &ContentManagerView::showLoader);
connect(treeView, &QTreeView::doubleClicked, this, &ContentManager::openBookWithIndex);
connect(&m_remoteLibraryManager, &OpdsRequestManager::languagesReceived, this, &ContentManager::updateLanguages);
connect(&m_remoteLibraryManager, &OpdsRequestManager::categoriesReceived, this, &ContentManager::updateCategories);
setCategories();
juuz0 marked this conversation as resolved.
Show resolved Hide resolved
setLanguages();
}

QList<QMap<QString, QVariant>> ContentManager::getBooksList()
Expand Down Expand Up @@ -143,6 +146,8 @@ void ContentManager::setLocal(bool local) {
}
m_local = local;
emit(filterParamsChanged());
setCategories();
setLanguages();
}

QStringList ContentManager::getTranslations(const QStringList &keys)
Expand All @@ -155,6 +160,40 @@ QStringList ContentManager::getTranslations(const QStringList &keys)
return translations;
}

void ContentManager::setCategories()
{
QStringList categories;
if (m_local) {
auto categoryData = mp_library->getKiwixLibrary().getBooksCategories();
categories.push_back("all");
for (auto category : categoryData) {
auto categoryName = QString::fromStdString(category);
categories.push_back(categoryName);
}
m_categories = categories;
emit(categoriesLoaded(m_categories));
return;
}
m_remoteLibraryManager.getCategoriesFromOpds();
}

void ContentManager::setLanguages()
{
LanguageList languages;
if (m_local) {
auto languageData = mp_library->getKiwixLibrary().getBooksLanguages();
for (auto language : languageData) {
auto langCode = QString::fromStdString(language);
auto selfName = QString::fromStdString(kiwix::getLanguageSelfName(language));
languages.push_back({langCode, selfName});
}
m_languages = languages;
emit(languagesLoaded(m_languages));
return;
}
m_remoteLibraryManager.getLanguagesFromOpds();
}

#define ADD_V(KEY, METH) {if(key==KEY) values.insert(key, QString::fromStdString((b->METH())));}
QMap<QString, QVariant> ContentManager::getBookInfos(QString id, const QStringList &keys)
{
Expand Down Expand Up @@ -544,13 +583,17 @@ void ContentManager::setCurrentLanguage(QString language)
kiwix::converta2toa3(language.toStdString()));
} catch (std::out_of_range&) {}
}
if (m_currentLanguage == language)
return;
m_currentLanguage = language;
emit(currentLangChanged());
emit(filterParamsChanged());
}

void ContentManager::setCurrentCategoryFilter(QString category)
{
if (m_categoryFilter == category)
return;
m_categoryFilter = category.toLower();
emit(filterParamsChanged());
}
Expand Down Expand Up @@ -585,6 +628,29 @@ void ContentManager::updateRemoteLibrary(const QString& content) {
});
}

void ContentManager::updateLanguages(const QString& content) {
auto languages = kiwix::readLanguagesFromFeed(content.toStdString());
LanguageList tempLanguages;
for (auto language : languages) {
auto code = QString::fromStdString(language.first);
auto title = QString::fromStdString(language.second);
tempLanguages.push_back({code, title});
}
m_languages = tempLanguages;
emit(languagesLoaded(m_languages));
}

void ContentManager::updateCategories(const QString& content) {;
auto categories = kiwix::readCategoriesFromFeed(content.toStdString());
QStringList tempCategories;
tempCategories.push_back("all");
for (auto catg : categories) {
tempCategories.push_back(QString::fromStdString(catg));
}
m_categories = tempCategories;
emit(categoriesLoaded(m_categories));
}

void ContentManager::setSearch(const QString &search)
{
m_searchQuery = search;
Expand All @@ -599,9 +665,9 @@ QStringList ContentManager::getBookIds()
acceptTags.push_back("_category:"+m_categoryFilter.toStdString());
}
if (m_categoryFilter == "other") {
for (auto& category: S_CATEGORIES) {
if (category.first != "other" && category.first != "all") {
rejectTags.push_back("_category:"+category.first.toStdString());
for (auto& category: m_categories) {
if (category != "other" && category != "all") {
rejectTags.push_back("_category:"+category.toStdString());
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/contentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ContentManager : public QObject
Q_PROPERTY(QString currentLanguage MEMBER m_currentLanguage WRITE setCurrentLanguage NOTIFY currentLangChanged)

public:
typedef QList<QPair<QString, QString>> LanguageList;
explicit ContentManager(Library* library, kiwix::Downloader *downloader, QObject *parent = nullptr);
virtual ~ContentManager() {}

Expand All @@ -28,6 +29,8 @@ class ContentManager : public QObject
void setCurrentCategoryFilter(QString category);
void setCurrentContentTypeFilter(QList<ContentTypeFilter*>& contentTypeFilter);
bool isLocal() const { return m_local; }
QStringList getCategories() const { return m_categories; }
LanguageList getLanguages() const { return m_languages; }

private:
Library* mp_library;
Expand All @@ -42,12 +45,16 @@ class ContentManager : public QObject
QList<ContentTypeFilter*> m_contentTypeFilters;
kiwix::supportedListSortBy m_sortBy = kiwix::UNSORTED;
bool m_sortOrderAsc = true;
LanguageList m_languages;
QStringList m_categories;

QStringList getBookIds();
void eraseBookFilesFromComputer(const QString dirPath, const QString filename);
QList<QMap<QString, QVariant>> getBooksList();
ContentManagerModel *managerModel;
QMutex remoteLibraryLocker;
void setCategories();
void setLanguages();

signals:
void filterParamsChanged();
Expand All @@ -57,6 +64,8 @@ class ContentManager : public QObject
void downloadsChanged();
void currentLangChanged();
void pendingRequest(const bool);
void categoriesLoaded(QStringList);
void languagesLoaded(LanguageList);

public slots:
QStringList getTranslations(const QStringList &keys);
Expand All @@ -70,6 +79,8 @@ public slots:
void setSortBy(const QString& sortBy, const bool sortOrderAsc);
void eraseBook(const QString& id);
void updateRemoteLibrary(const QString& content);
void updateLanguages(const QString& content);
void updateCategories(const QString& content);
void pauseBook(const QString& id);
void resumeBook(const QString& id);
void cancelBook(const QString& id);
Expand Down
94 changes: 55 additions & 39 deletions src/contentmanagerside.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "kiwixapp.h"

#include <QLocale>
#include <QDebug>

#include "klistwidgetitem.h"
#include "static_content.h"

ContentManagerSide::ContentManagerSide(QWidget *parent) :
QWidget(parent),
Expand Down Expand Up @@ -91,39 +91,10 @@ ContentManagerSide::ContentManagerSide(QWidget *parent) :
});
}

for(auto lang: S_LANGUAGES)
{
auto currentLang = QLocale().language();
auto locale = QLocale(lang);
if (locale.language() != lang) {
// Qt may not find the locale for the lang :/
// In this case, Qt return the current locale
// So we must be sure that the locale found correspond to the lang we want to add,
// else we may add several time the current language.
continue;
}
auto item = new KListWidgetItem(QLocale::languageToString(locale.language()));
item->setData(Qt::UserRole, lang);
mp_languageSelector->addItem(item);
if (lang == currentLang) {
item->setSelected(true);
}
}
mp_languageSelector->sortItems();
auto item = new KListWidgetItem("All");
item->setData(Qt::UserRole, QLocale::AnyLanguage);
mp_languageSelector->insertItem(0, item);

for (auto category: S_CATEGORIES)
{
auto item = new KListWidgetItem(category.second);
item->setData(Qt::UserRole, category.first);
mp_categorySelector->addItem(item);
if (category.first == "all")
{
item->setSelected(true);
}
}
setCategories(KiwixApp::instance()->getContentManager()->getCategories());
setLanguages(KiwixApp::instance()->getContentManager()->getLanguages());
connect(KiwixApp::instance()->getContentManager(), &ContentManager::categoriesLoaded, this, &ContentManagerSide::setCategories);
connect(KiwixApp::instance()->getContentManager(), &ContentManager::languagesLoaded, this, &ContentManagerSide::setLanguages);
}

ContentManagerSide::~ContentManagerSide()
Expand All @@ -138,14 +109,12 @@ void ContentManagerSide::setContentManager(ContentManager *contentManager)
this, [=]() {
auto item = mp_languageSelector->selectedItems().at(0);
if (!item) return;
auto langId = item->data(Qt::UserRole).toInt();
auto lang = QLocale::Language(langId);
if (lang == QLocale::AnyLanguage) {
auto lang = item->data(Qt::UserRole).toString();
if (lang == "all") {
mp_contentManager->setCurrentLanguage("*");
return;
}
auto locale = QLocale(lang);
mp_contentManager->setCurrentLanguage(locale.name().split("_").at(0));
mp_contentManager->setCurrentLanguage(lang);
});
connect(mp_categorySelector, &QListWidget::itemSelectionChanged,
this, [=]() {
Expand All @@ -155,3 +124,50 @@ void ContentManagerSide::setContentManager(ContentManager *contentManager)
mp_contentManager->setCurrentCategoryFilter(category);
});
}

QString beautify(QString word)
{
word = word.replace("_", " ");
word[0] = word[0].toUpper();
return word;
}

void ContentManagerSide::setCategories(QStringList categories)
{
mp_categorySelector->blockSignals(true);
mp_categorySelector->setHidden(true);
mp_categorySelector->clear();
mp_categorySelector->blockSignals(false);
for (auto category: categories)
{
auto item = new KListWidgetItem(beautify(category));
item->setData(Qt::UserRole, category);
mp_categorySelector->addItem(item);
if (category == "all")
{
item->setSelected(true);
}
}
}

void ContentManagerSide::setLanguages(ContentManager::LanguageList langList)
{
mp_languageSelector->blockSignals(true);
mp_languageSelector->setHidden(true);
mp_languageSelector->clear();
mp_languageSelector->blockSignals(false);
for(auto lang: langList)
{
auto currentLang = QLocale().language();
auto item = new KListWidgetItem(lang.second);
item->setData(Qt::UserRole, lang.first);
mp_languageSelector->addItem(item);
if (lang.second == QLocale::languageToString(currentLang)) {
item->setSelected(true);
}
}
mp_languageSelector->sortItems();
auto item = new KListWidgetItem("All");
item->setData(Qt::UserRole, "all");
mp_languageSelector->insertItem(0, item);
}
4 changes: 4 additions & 0 deletions src/contentmanagerside.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class ContentManagerSide : public QWidget
QListWidget* mp_categorySelector;
QCheckBox* mp_contentTypeButton;
QList<ContentTypeFilter*> m_contentTypeFilters;

public slots:
void setCategories(QStringList);
void setLanguages(ContentManager::LanguageList);
};

#endif // CONTENTMANAGERSIDE_H
3 changes: 0 additions & 3 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "kiwixapp.h"
#include "static_content.h"
#include "zim/error.h"
#include "zim/version.h"
#include "kiwix/tools.h"
Expand Down Expand Up @@ -66,8 +65,6 @@ void KiwixApp::init()
}
mp_manager = new ContentManager(&m_library, mp_downloader);

initStaticContent();

auto icon = QIcon();
icon.addFile(":/icons/kiwix-app-icons-square.svg");
setWindowIcon(icon);
Expand Down
Loading
Loading