From 574d1e2a296d7b853d00d7a45599b472d7b54ee3 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Tue, 11 Jul 2023 14:46:35 +0530 Subject: [PATCH] Dynamic category values The category values are not static after this change. If local library is displayed, we get the categories from local library If remote library is displayed, we send a request to /catalog/v2/categories --- src/contentmanager.cpp | 13 +++++++++---- src/contentmanager.h | 1 + src/contentmanagerside.cpp | 40 +++++++++++++++++++++++++++----------- src/contentmanagerside.h | 3 +++ src/opdsrequestmanager.cpp | 6 +++--- 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index bf4045b..c800217 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -70,7 +70,7 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, connect(treeView, &QTreeView::doubleClicked, this, &ContentManager::openBookWithIndex); connect(&m_remoteLibraryManager, &OpdsRequestManager::languagesReceived, this, &ContentManager::updateLanguages); connect(&m_remoteLibraryManager, &OpdsRequestManager::categoriesReceived, this, &ContentManager::updateCategories); - setLanguages(); + setCategories(); } QList> ContentManager::getBooksList() @@ -146,6 +146,7 @@ void ContentManager::setLocal(bool local) { } m_local = local; emit(filterParamsChanged()); + setCategories(); } QStringList ContentManager::getTranslations(const QStringList &keys) @@ -163,11 +164,13 @@ 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(); @@ -633,10 +636,12 @@ void ContentManager::updateLanguages(const QString& content) { 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) @@ -653,9 +658,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()); } } } diff --git a/src/contentmanager.h b/src/contentmanager.h index 45fb25c..3229e1f 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -64,6 +64,7 @@ signals: void downloadsChanged(); void currentLangChanged(); void pendingRequest(const bool); + void categoriesLoaded(QStringList); public slots: QStringList getTranslations(const QStringList &keys); diff --git a/src/contentmanagerside.cpp b/src/contentmanagerside.cpp index f361833..5fd81db 100644 --- a/src/contentmanagerside.cpp +++ b/src/contentmanagerside.cpp @@ -3,6 +3,7 @@ #include "kiwixapp.h" #include +#include #include "klistwidgetitem.h" #include "static_content.h" @@ -91,6 +92,9 @@ ContentManagerSide::ContentManagerSide(QWidget *parent) : }); } + setCategories(KiwixApp::instance()->getContentManager()->getCategories()); + connect(KiwixApp::instance()->getContentManager(), &ContentManager::categoriesLoaded, this, &ContentManagerSide::setCategories); + for(auto lang: S_LANGUAGES) { auto currentLang = QLocale().language(); @@ -113,17 +117,6 @@ ContentManagerSide::ContentManagerSide(QWidget *parent) : 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); - } - } } ContentManagerSide::~ContentManagerSide() @@ -155,3 +148,28 @@ 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); + } + } +} diff --git a/src/contentmanagerside.h b/src/contentmanagerside.h index da8dcff..1aab3e4 100644 --- a/src/contentmanagerside.h +++ b/src/contentmanagerside.h @@ -30,6 +30,9 @@ private: QListWidget* mp_categorySelector; QCheckBox* mp_contentTypeButton; QList m_contentTypeFilters; + +public slots: + void setCategories(QStringList); }; #endif // CONTENTMANAGERSIDE_H diff --git a/src/opdsrequestmanager.cpp b/src/opdsrequestmanager.cpp index a5e0ab1..a0ac41c 100644 --- a/src/opdsrequestmanager.cpp +++ b/src/opdsrequestmanager.cpp @@ -30,9 +30,9 @@ void OpdsRequestManager::doUpdate(const QString& currentLanguage, const QString& // Add "special negative" filter for "other" category (if necessary) if (categoryFilter == "other") { - for (auto& category: S_CATEGORIES) { - if (category.first != "other" && category.first != "all") { - excludeTags += "_category:"+category.first; + for (auto& category: KiwixApp::instance()->getContentManager()->getCategories()) { + if (category != "other" && category != "all") { + excludeTags += "_category:"+category; } } }