Skip to content

Commit

Permalink
Revamp categories handling.
Browse files Browse the repository at this point in the history
Move categories list in `static_content.cpp`
We use two "string" to handle each category. The first one is the
tag to use to do the opds request. The second one is the one displayed to
the user. The later is translatable.
The list is taken from issue kiwix/kiwix-tools#317

Fix #193
  • Loading branch information
mgautierfr committed Apr 8, 2020
1 parent c804cc5 commit d775b2e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 40 deletions.
12 changes: 6 additions & 6 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "contentmanager.h"

#include "kiwixapp.h"
#include "static_content.h"
#include <kiwix/tools/networkTools.h>
#include <kiwix/tools/otherTools.h>
#include <kiwix/manager.h>
Expand Down Expand Up @@ -365,14 +366,13 @@ QStringList ContentManager::getBookIds()
kiwix::Filter filter;
std::vector<std::string> tags;
if (m_categoryFilter != "all" && m_categoryFilter != "other") {
tags.push_back(m_categoryFilter.toStdString());
tags.push_back("_category:"+m_categoryFilter.toStdString());
filter.acceptTags(tags);
}
if (m_categoryFilter == "other") {
auto categoryList = KiwixApp::instance()->getMainWindow()->getSideContentManager()->getCategoryList();
for (auto& category: categoryList) {
if (category != "Other") {
tags.push_back(category.toLower().toStdString());
for (auto& category: S_CATEGORIES) {
if (category.first != "other" && category.first != "all") {
tags.push_back("_category:"+category.first.toStdString());
}
}
filter.rejectTags(tags);
Expand Down Expand Up @@ -408,4 +408,4 @@ void ContentManager::setSortBy(const QString& sortBy, const bool sortOrderAsc)
}
m_sortOrderAsc = sortOrderAsc;
emit(booksChanged());
}
}
32 changes: 5 additions & 27 deletions src/contentmanagerside.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,12 @@ ContentManagerSide::ContentManagerSide(QWidget *parent) :
}
}

for (auto category: {
"All",
"Gutenberg", // Gutenberg book has wrong tag
"Other",
"Phet", // Phet books have no tags
"Psiram",
"Stack Exchange",
"Ted", // Ted books have wrong tags
"Vikidia",
"Wikibooks", // wikibooks have no tags
"Wikinews",
"Wikipedia",
"Wikiquote",
"Wikisource",
// "Wikispecies", // Wikispecies books have wrong tags
"Wikiversity",
"Wikivoyage",
"Wiktionary"
})
for (auto category: S_CATEGORIES)
{
auto c = QString(category);
m_categoryList.append(c);
auto item = new KListWidgetItem(c);
auto item = new KListWidgetItem(category.second);
item->setData(Qt::UserRole, category.first);
mp_categorySelector->addItem(item);
if (c == "All")
if (category.first == "all")
{
item->setSelected(true);
}
Expand Down Expand Up @@ -121,10 +102,7 @@ void ContentManagerSide::setContentManager(ContentManager *contentManager)
this, [=]() {
auto item = mp_categorySelector->selectedItems().at(0);
if (!item) return;
auto category = item->text();
if (category == "Stack Exchange") {
category = "Stackexchange";
}
auto category = item->data(Qt::UserRole).toString();
mp_contentManager->setCurrentCategoryFilter(category);
});
}
2 changes: 0 additions & 2 deletions src/contentmanagerside.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ContentManagerSide : public QWidget
~ContentManagerSide();

void setContentManager(ContentManager* contentManager);
QStringList getCategoryList() { return m_categoryList;};

private:
Ui::contentmanagerside *mp_ui;
Expand All @@ -28,7 +27,6 @@ class ContentManagerSide : public QWidget
QListWidget* mp_languageSelector;
QCheckBox* mp_categoryButton;
QListWidget* mp_categorySelector;
QStringList m_categoryList;
};

#endif // CONTENTMANAGERSIDE_H
10 changes: 5 additions & 5 deletions src/opdsrequestmanager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "opdsrequestmanager.h"
#include "static_content.h"
#include "kiwixapp.h"

OpdsRequestManager::OpdsRequestManager()
Expand All @@ -16,15 +17,14 @@ void OpdsRequestManager::doUpdate(const QString& currentLanguage, const QString&
}
query.addQueryItem("count", QString::number(0));
if (categoryFilter != "all" && categoryFilter != "other") {
query.addQueryItem("tag", categoryFilter);
query.addQueryItem("tag", "_category:"+categoryFilter);
}

if (categoryFilter == "other") {
auto allCategories = KiwixApp::instance()->getMainWindow()->getSideContentManager()->getCategoryList();
QStringList excludeTags;
for (auto& category:allCategories) {
if (category != "Other") {
excludeTags += category.toLower();
for (auto& category: S_CATEGORIES) {
if (category.first != "other" && category.first != "all") {
excludeTags += "_category:"+category.first;
}
}
query.addQueryItem("notag", excludeTags.join(";"));
Expand Down
21 changes: 21 additions & 0 deletions src/static_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,29 @@


std::vector<QLocale::Language> S_LANGUAGES;
std::vector<std::pair<QString, QString>> S_CATEGORIES;

void initStaticContent() {
#define PUSH(key, value) S_CATEGORIES.push_back(std::make_pair(QString::fromStdString(key), QObject::tr(value)))
PUSH("all", "All");
PUSH("other", "Other");
// PUSH("gutenberg", "Gutenberg"); // Gutenberg book has wrong tag
// PUSH("phet", "Phet"); // Phet books have no tags
PUSH("psiram", "Psiram");
PUSH("stack_exchange", "Stack Exchange");
// PUSH("ted", "Ted"); // Ted books have wrong tags
PUSH("vikidia", "Vikidia");
// PUSH("wikibooks", "Wikibooks"); // wikibooks have no tags
PUSH("wikinews", "Wikinews");
PUSH("wikipedia", "Wikipedia");
PUSH("wikiquote", "Wikiquote");
PUSH("wikisource", "Wikisource");
// PUSH("wikispecies", "Wikispecies"); // Wikispecies books have wrong tags
PUSH("wikiversity", "Wikiversity");
PUSH("wikivoyage", "Wikivoyage");
PUSH("wiktionary", "Wiktionary");
#undef PUSH

#define PUSH(value) S_LANGUAGES.push_back(value)
PUSH(QLocale::AnyLanguage);
PUSH(QLocale::Afar);
Expand Down
3 changes: 3 additions & 0 deletions src/static_content.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
#define STATICCONTENT_H

#include <QLocale>
#include <QString>
#include <utility>
#include <vector>

extern std::vector<QLocale::Language> S_LANGUAGES;
extern std::vector<std::pair<QString, QString>> S_CATEGORIES;

void initStaticContent();

Expand Down

0 comments on commit d775b2e

Please sign in to comment.