Real book data in library

Adds the book data from library
This commit is contained in:
Nikhil Tanwar 2023-06-22 12:02:54 +05:30
parent e5c8a31ec0
commit f968a24adc
4 changed files with 80 additions and 4 deletions

View File

@ -12,6 +12,8 @@
#include <QStorageInfo>
#include <QMessageBox>
#include "contentmanagermodel.h"
#include <zim/error.h>
#include <zim/item.h>
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent),
@ -23,14 +25,46 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
// so, we don't need to delete it.
mp_view = new ContentManagerView();
auto managerModel = new ContentManagerModel();
const auto booksList = getBooksList();
managerModel->setBooksData(booksList);
mp_view->setModel(managerModel);
mp_view->show();
setCurrentLanguage(QLocale().name().split("_").at(0));
connect(mp_library, &Library::booksChanged, this, [=]() {emit(this->booksChanged());});
connect(this, &ContentManager::filterParamsChanged, this, &ContentManager::updateLibrary);
connect(this, &ContentManager::booksChanged, this, [=]() {
const auto nBookList = getBooksList();
managerModel->setBooksData(nBookList);
});
connect(&m_remoteLibraryManager, &OpdsRequestManager::requestReceived, this, &ContentManager::updateRemoteLibrary);
}
QList<QMap<QString, QVariant>> ContentManager::getBooksList()
{
const auto bookIds = getBookIds();
QList<QMap<QString, QVariant>> bookList;
QStringList keys = {"title", "tags", "date", "id", "size"};
auto app = KiwixApp::instance();
std::shared_ptr<zim::Archive> archive;
QIcon bookIcon;
for (auto bookId : bookIds) {
try {
archive = app->getLibrary()->getArchive(bookId);
std::string favicon, _mimetype;
auto item = archive->getIllustrationItem(48);
favicon = item.getData();
_mimetype = item.getMimetype();
QPixmap pixmap;
pixmap.loadFromData((const uchar*)favicon.data(), favicon.size());
bookIcon = QIcon(pixmap);
} catch (std::out_of_range& e) {}
auto mp = getBookInfos(bookId, keys);
mp["icon"] = bookIcon;
bookList.append(mp);
}
return bookList;
}
void ContentManager::setLocal(bool local) {
if (local == m_local) {
return;

View File

@ -43,6 +43,7 @@ private:
QStringList getBookIds();
void eraseBookFilesFromComputer(const QString dirPath, const QString filename);
QList<QMap<QString, QVariant>> getBooksList();
signals:
void filterParamsChanged();

View File

@ -10,9 +10,6 @@
ContentManagerModel::ContentManagerModel(QObject *parent)
: QAbstractItemModel(parent)
{
rootNode = new Node({tr("Icon"), tr("Name"), tr("Date"), tr("Size"), tr("Content Type"), tr("Download")});
auto childNode = new Node({"someIcon", "test name", "test date", "test size", "test content", "download link"}, rootNode);
rootNode->appendChild(childNode);
}
ContentManagerModel::~ContentManagerModel()
@ -83,7 +80,8 @@ QModelIndex ContentManagerModel::parent(const QModelIndex &index) const
int ContentManagerModel::rowCount(const QModelIndex &parent) const
{
return 5;
Q_UNUSED(parent);
return m_data.size();
}
QVariant ContentManagerModel::headerData(int section, Qt::Orientation orientation, int role) const
@ -101,3 +99,43 @@ QVariant ContentManagerModel::headerData(int section, Qt::Orientation orientatio
default: return QVariant();
}
}
void ContentManagerModel::setBooksData(const QList<QMap<QString, QVariant>>& data)
{
m_data = data;
rootNode = new Node({tr("Icon"), tr("Name"), tr("Date"), tr("Size"), tr("Content Type"), tr("Download")});
setupNodes();
emit dataChanged(QModelIndex(), QModelIndex());
}
QString convertToUnits(QString size)
{
QStringList units = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"};
int unitIndex = 0;
auto bytes = size.toDouble();
while (bytes >= 1024 && unitIndex < units.size()) {
bytes /= 1024;
unitIndex++;
}
const auto preciseBytes = QString::number(bytes, 'g', 3);
return preciseBytes + units[unitIndex];
}
void ContentManagerModel::setupNodes()
{
beginResetModel();
for (auto bookItem : m_data) {
auto name = bookItem["title"].toString();
auto date = bookItem["date"].toString();
auto size = convertToUnits(bookItem["size"].toString());
auto content = bookItem["tags"].toString();
auto id = bookItem["id"].toString();
auto description = bookItem["description"].toString();
auto icon = bookItem["icon"];
const auto temp = new Node({icon, name, date, size, content, id}, rootNode);
rootNode->appendChild(temp);
}
endResetModel();
}

View File

@ -24,8 +24,11 @@ public:
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
void setBooksData(const QList<QMap<QString, QVariant>>& data);
void setupNodes();
private:
QList<QMap<QString, QVariant>> m_data;
Node *rootNode;
};