mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-23 20:20:58 -04:00
Real book data in library
Adds the book data from library
This commit is contained in:
parent
e5c8a31ec0
commit
f968a24adc
@ -12,6 +12,8 @@
|
|||||||
#include <QStorageInfo>
|
#include <QStorageInfo>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include "contentmanagermodel.h"
|
#include "contentmanagermodel.h"
|
||||||
|
#include <zim/error.h>
|
||||||
|
#include <zim/item.h>
|
||||||
|
|
||||||
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
|
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
|
||||||
: QObject(parent),
|
: QObject(parent),
|
||||||
@ -23,14 +25,46 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
|
|||||||
// so, we don't need to delete it.
|
// so, we don't need to delete it.
|
||||||
mp_view = new ContentManagerView();
|
mp_view = new ContentManagerView();
|
||||||
auto managerModel = new ContentManagerModel();
|
auto managerModel = new ContentManagerModel();
|
||||||
|
const auto booksList = getBooksList();
|
||||||
|
managerModel->setBooksData(booksList);
|
||||||
mp_view->setModel(managerModel);
|
mp_view->setModel(managerModel);
|
||||||
mp_view->show();
|
mp_view->show();
|
||||||
setCurrentLanguage(QLocale().name().split("_").at(0));
|
setCurrentLanguage(QLocale().name().split("_").at(0));
|
||||||
connect(mp_library, &Library::booksChanged, this, [=]() {emit(this->booksChanged());});
|
connect(mp_library, &Library::booksChanged, this, [=]() {emit(this->booksChanged());});
|
||||||
connect(this, &ContentManager::filterParamsChanged, this, &ContentManager::updateLibrary);
|
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);
|
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) {
|
void ContentManager::setLocal(bool local) {
|
||||||
if (local == m_local) {
|
if (local == m_local) {
|
||||||
return;
|
return;
|
||||||
|
@ -43,6 +43,7 @@ private:
|
|||||||
|
|
||||||
QStringList getBookIds();
|
QStringList getBookIds();
|
||||||
void eraseBookFilesFromComputer(const QString dirPath, const QString filename);
|
void eraseBookFilesFromComputer(const QString dirPath, const QString filename);
|
||||||
|
QList<QMap<QString, QVariant>> getBooksList();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void filterParamsChanged();
|
void filterParamsChanged();
|
||||||
|
@ -10,9 +10,6 @@
|
|||||||
ContentManagerModel::ContentManagerModel(QObject *parent)
|
ContentManagerModel::ContentManagerModel(QObject *parent)
|
||||||
: QAbstractItemModel(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()
|
ContentManagerModel::~ContentManagerModel()
|
||||||
@ -83,7 +80,8 @@ QModelIndex ContentManagerModel::parent(const QModelIndex &index) const
|
|||||||
|
|
||||||
int ContentManagerModel::rowCount(const QModelIndex &parent) 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
|
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();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -24,8 +24,11 @@ public:
|
|||||||
QModelIndex parent(const QModelIndex &index) const override;
|
QModelIndex parent(const QModelIndex &index) const override;
|
||||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
int columnCount(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:
|
private:
|
||||||
|
QList<QMap<QString, QVariant>> m_data;
|
||||||
Node *rootNode;
|
Node *rootNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user