mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Make the contentManager the only interface from C++ to js/html.
Move all methods used by contentManager html into the contentManager class. We do not export anymore the kiwixapp, the library or the downloader. As the downloader was use only to export the methods to html, we can remove it (and use kiwix::Downloader).
This commit is contained in:
parent
235b82166e
commit
3410252837
@ -58,7 +58,6 @@ SOURCES += \
|
||||
src/tocsidebar.cpp \
|
||||
src/contentmanager.cpp \
|
||||
src/contentmanagerview.cpp \
|
||||
src/downloader.cpp
|
||||
|
||||
HEADERS += \
|
||||
src/mainwindow.h \
|
||||
@ -78,7 +77,6 @@ HEADERS += \
|
||||
src/tocsidebar.h \
|
||||
src/contentmanager.h \
|
||||
src/contentmanagerview.h \
|
||||
src/downloader.h
|
||||
|
||||
FORMS += \
|
||||
ui/mainwindow.ui \
|
||||
|
@ -30,16 +30,16 @@ function addBook(values) {
|
||||
}
|
||||
function onBooksChanged () {
|
||||
app.books = [];
|
||||
for(var i=contentManager.startBookIndex; i<contentManager.endBookIndex; i++) {
|
||||
var id = library.bookIds[i];
|
||||
library.getBookInfos(id, BOOK_KEYS, addBook);
|
||||
for(var i=0; i<contentManager.bookIds.length; i++) {
|
||||
var id = contentManager.bookIds[i];
|
||||
contentManager.getBookInfos(id, BOOK_KEYS, addBook);
|
||||
}
|
||||
}
|
||||
|
||||
downloadUpdaters = {}
|
||||
const DOWNLOAD_KEYS = ["id", "status", "followedBy", "path", "totalLength", "completedLength", "downloadSpeed", "verifiedLength"];
|
||||
function getDownloadInfo(id) {
|
||||
downloader.updateDownloadInfos(id, DOWNLOAD_KEYS, function(values) {
|
||||
contentManager.updateDownloadInfos(id, DOWNLOAD_KEYS, function(values) {
|
||||
if (values.length == 0) {
|
||||
clearInterval(downloadUpdaters[id]);
|
||||
return;
|
||||
@ -55,22 +55,16 @@ function getDownloadInfo(id) {
|
||||
function init() {
|
||||
new QWebChannel(qt.webChannelTransport, function(channel) {
|
||||
contentManager = channel.objects.contentManager;
|
||||
library = channel.objects.library;
|
||||
kiwix = channel.objects.kiwix;
|
||||
downloader = channel.objects.downloader;
|
||||
app = new Vue({
|
||||
el: "#app",
|
||||
data: {
|
||||
contentManager: contentManager,
|
||||
library: library,
|
||||
kiwix: kiwix,
|
||||
downloader: downloader,
|
||||
books: [],
|
||||
downloads: {}
|
||||
},
|
||||
methods: {
|
||||
openBook : function(book) {
|
||||
kiwix.openUrl("zim://"+book.id+".zim/", true, function() {});
|
||||
contentManager.openBook(book.id, function() {});
|
||||
},
|
||||
changePage : function(delta) {
|
||||
var newPage = contentManager.currentPage+delta;
|
||||
@ -79,7 +73,7 @@ function init() {
|
||||
contentManager.currentPage = newPage;
|
||||
},
|
||||
downloadBook : function(book) {
|
||||
downloader.downloadBook(book.id, function(did) {
|
||||
contentManager.downloadBook(book.id, function(did) {
|
||||
book.downloadId = did;
|
||||
downloadUpdaters[book.id] = setInterval(function() { getDownloadInfo(book.id); }, 1000);
|
||||
});
|
||||
@ -87,8 +81,7 @@ function init() {
|
||||
niceBytes : niceBytes
|
||||
}
|
||||
});
|
||||
library.booksChanged.connect(onBooksChanged);
|
||||
contentManager.pagingChanged.connect(onBooksChanged);
|
||||
contentManager.booksChanged.connect(onBooksChanged);
|
||||
onBooksChanged();
|
||||
});
|
||||
}
|
||||
|
@ -1,16 +1,156 @@
|
||||
#include "contentmanager.h"
|
||||
#include "contentmanager.h"
|
||||
|
||||
#include "kiwixapp.h"
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
ContentManager::ContentManager(Library* library, QObject *parent)
|
||||
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
|
||||
: QObject(parent),
|
||||
mp_library(library)
|
||||
mp_library(library),
|
||||
mp_downloader(downloader)
|
||||
{
|
||||
// mp_view will be passed to the tab who will take ownership,
|
||||
// so, we don't need to delete it.
|
||||
mp_view = new ContentManagerView();
|
||||
mp_view->registerObject("contentManager", this);
|
||||
mp_view->registerObject("library", mp_library);
|
||||
mp_view->setHtml();
|
||||
connect(mp_library, &Library::booksChanged, this, [=]() {emit(this->booksChanged());});
|
||||
}
|
||||
|
||||
|
||||
#define ADD_V(KEY, METH) {if(key==KEY) values.append(QString::fromStdString((b.METH())));}
|
||||
QStringList ContentManager::getBookInfos(QString id, const QStringList &keys)
|
||||
{
|
||||
QStringList values;
|
||||
if (id.endsWith(".zim")) {
|
||||
id.resize(id.size()-4);
|
||||
}
|
||||
auto& b = mp_library->getBookById(id);
|
||||
for(auto& key: keys){
|
||||
ADD_V("id", getId);
|
||||
ADD_V("path", getPath);
|
||||
ADD_V("indexPath", getIndexPath);
|
||||
ADD_V("title", getTitle);
|
||||
ADD_V("description", getDescription);
|
||||
ADD_V("language", getLanguage);
|
||||
ADD_V("creator", getCreator);
|
||||
ADD_V("publisher", getPublisher);
|
||||
ADD_V("date", getDate);
|
||||
ADD_V("url", getUrl);
|
||||
ADD_V("name", getName);
|
||||
ADD_V("tags", getTags);
|
||||
ADD_V("origId", getOrigId);
|
||||
ADD_V("faviconMimeType", getFaviconMimeType);
|
||||
ADD_V("downloadId", getDownloadId);
|
||||
if (key == "favicon") {
|
||||
auto s = b.getFavicon();
|
||||
values.append(QByteArray::fromStdString(s).toBase64());
|
||||
}
|
||||
if (key == "size") {
|
||||
values.append(QString::number(b.getSize()));
|
||||
}
|
||||
if (key == "articleCount") {
|
||||
values.append(QString::number(b.getArticleCount()));
|
||||
}
|
||||
if (key == "mediaCount") {
|
||||
values.append(QString::number(b.getMediaCount()));
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
#undef ADD_V
|
||||
|
||||
void ContentManager::openBook(const QString &id)
|
||||
{
|
||||
QUrl url("zim://"+id+".zim/");
|
||||
KiwixApp::instance()->openUrl(url, true);
|
||||
}
|
||||
|
||||
#define ADD_V(KEY, METH) {if(key==KEY) {values.append(QString::fromStdString((d->METH()))); continue;}}
|
||||
QStringList ContentManager::updateDownloadInfos(QString id, const QStringList &keys)
|
||||
{
|
||||
QStringList values;
|
||||
if (id.endsWith(".zim")) {
|
||||
id.resize(id.size()-4);
|
||||
}
|
||||
auto& b = mp_library->getBookById(id);
|
||||
kiwix::Download* d;
|
||||
try {
|
||||
d = mp_downloader->getDownload(b.getDownloadId());
|
||||
} catch(...) {
|
||||
b.setDownloadId("");
|
||||
mp_library->save();
|
||||
emit(mp_library->booksChanged());
|
||||
return values;
|
||||
}
|
||||
|
||||
d->updateStatus(true);
|
||||
if (d->getStatus() == kiwix::Download::COMPLETE) {
|
||||
b.setPath(d->getPath());
|
||||
b.setDownloadId("");
|
||||
mp_library->save();
|
||||
emit(mp_library->booksChanged());
|
||||
}
|
||||
for(auto& key: keys){
|
||||
ADD_V("id", getDid);
|
||||
if(key == "status") {
|
||||
switch(d->getStatus()){
|
||||
case kiwix::Download::ACTIVE:
|
||||
values.append("active");
|
||||
break;
|
||||
case kiwix::Download::WAITING:
|
||||
values.append("waiting");
|
||||
break;
|
||||
case kiwix::Download::PAUSED:
|
||||
values.append("paused");
|
||||
break;
|
||||
case kiwix::Download::ERROR:
|
||||
values.append("error");
|
||||
break;
|
||||
case kiwix::Download::COMPLETE:
|
||||
values.append("completed");
|
||||
break;
|
||||
case kiwix::Download::REMOVED:
|
||||
values.append("removed");
|
||||
break;
|
||||
default:
|
||||
values.append("unknown");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ADD_V("followedBy", getFollowedBy);
|
||||
ADD_V("path", getPath);
|
||||
if(key == "totalLength") {
|
||||
values.append(QString::number(d->getTotalLength()));
|
||||
}
|
||||
if(key == "completedLength") {
|
||||
values.append(QString::number(d->getCompletedLength()));
|
||||
}
|
||||
if(key == "downloadSpeed") {
|
||||
values.append(QString::number(d->getDownloadSpeed()));
|
||||
}
|
||||
if(key == "verifiedLength") {
|
||||
values.append(QString::number(d->getVerifiedLength()));
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
#undef ADD_V
|
||||
|
||||
QString ContentManager::downloadBook(const QString &id)
|
||||
{
|
||||
auto& book = mp_library->getBookById(id);
|
||||
auto download = mp_downloader->startDownload(book.getUrl());
|
||||
book.setDownloadId(download->getDid());
|
||||
return QString::fromStdString(download->getDid());
|
||||
emit(booksChanged());
|
||||
}
|
||||
|
||||
QStringList ContentManager::getDownloadIds()
|
||||
{
|
||||
QStringList list;
|
||||
for(auto& id: mp_downloader->getDownloadIds()) {
|
||||
list.append(QString::fromStdString(id));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
@ -5,35 +5,45 @@
|
||||
#include <math.h>
|
||||
#include "library.h"
|
||||
#include "contentmanagerview.h"
|
||||
#include <kiwix/downloader.h>
|
||||
|
||||
class ContentManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int booksPerPage MEMBER m_booksPerPage NOTIFY pagingChanged)
|
||||
Q_PROPERTY(int nbPages READ getNbPages NOTIFY pagingChanged)
|
||||
Q_PROPERTY(int currentPage MEMBER m_currentPage WRITE setCurrentPage NOTIFY pagingChanged)
|
||||
Q_PROPERTY(int startBookIndex READ getStartBookIndex NOTIFY pagingChanged)
|
||||
Q_PROPERTY(int endBookIndex READ getEndBookIndex NOTIFY pagingChanged)
|
||||
Q_PROPERTY(int booksPerPage MEMBER m_booksPerPage NOTIFY booksChanged)
|
||||
Q_PROPERTY(int nbPages READ getNbPages NOTIFY booksChanged)
|
||||
Q_PROPERTY(int currentPage MEMBER m_currentPage WRITE setCurrentPage NOTIFY booksChanged)
|
||||
Q_PROPERTY(int startBookIndex READ getStartBookIndex NOTIFY booksChanged)
|
||||
Q_PROPERTY(int endBookIndex READ getEndBookIndex NOTIFY booksChanged)
|
||||
Q_PROPERTY(QStringList bookIds READ getBookIds NOTIFY booksChanged)
|
||||
Q_PROPERTY(QStringList downloadIds READ getDownloadIds NOTIFY downloadsChanged)
|
||||
|
||||
public:
|
||||
|
||||
explicit ContentManager(Library* library, QObject *parent = nullptr);
|
||||
explicit ContentManager(Library* library, kiwix::Downloader *downloader, QObject *parent = nullptr);
|
||||
virtual ~ContentManager() {}
|
||||
|
||||
ContentManagerView* getView() { return mp_view; }
|
||||
QStringList getDownloadIds();
|
||||
private:
|
||||
Library* mp_library;
|
||||
kiwix::Downloader* mp_downloader;
|
||||
ContentManagerView* mp_view;
|
||||
int m_booksPerPage = 10;
|
||||
int m_currentPage = 0;
|
||||
void setCurrentPage(int currentPage) {
|
||||
m_currentPage = max(0, min(currentPage, getNbPages()));
|
||||
emit(pagingChanged());
|
||||
emit(booksChanged());
|
||||
}
|
||||
|
||||
QStringList getBookIds() {
|
||||
return mp_library->getBookIds().mid(getStartBookIndex(), m_booksPerPage);
|
||||
}
|
||||
|
||||
|
||||
signals:
|
||||
void pagingChanged();
|
||||
void booksChanged();
|
||||
void downloadsChanged();
|
||||
|
||||
public slots:
|
||||
int getNbPages() {
|
||||
@ -45,6 +55,10 @@ public slots:
|
||||
int getEndBookIndex() {
|
||||
return min((m_currentPage+1) * m_booksPerPage, mp_library->getBookIds().length());
|
||||
}
|
||||
QStringList getBookInfos(QString id, const QStringList &keys);
|
||||
void openBook(const QString& id);
|
||||
QStringList updateDownloadInfos(QString id, const QStringList& keys);
|
||||
QString downloadBook(const QString& id);
|
||||
};
|
||||
|
||||
#endif // CONTENTMANAGER_H
|
||||
|
@ -1,104 +0,0 @@
|
||||
#include "downloader.h"
|
||||
|
||||
Downloader::Downloader(Library* library, QObject *parent)
|
||||
: QObject(parent),
|
||||
mp_library(library)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Downloader::~Downloader()
|
||||
{
|
||||
m_downloader.close();
|
||||
}
|
||||
|
||||
QStringList Downloader::getDownloadIds()
|
||||
{
|
||||
QStringList list;
|
||||
for(auto& id: m_downloader.getDownloadIds()) {
|
||||
list.append(QString::fromStdString(id));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
QString Downloader::downloadBook(const QString& id) {
|
||||
auto& book = mp_library->getBookById(id);
|
||||
auto download = m_downloader.startDownload(book.getUrl());
|
||||
book.setDownloadId(download->getDid());
|
||||
return QString::fromStdString(download->getDid());
|
||||
}
|
||||
|
||||
#define ADD_V(KEY, METH) {if(key==KEY) {values.append(QString::fromStdString((d->METH()))); continue;}}
|
||||
QStringList Downloader::updateDownloadInfos(QString id, const QStringList &keys)
|
||||
{
|
||||
QStringList values;
|
||||
if (id.endsWith(".zim")) {
|
||||
id.resize(id.size()-4);
|
||||
}
|
||||
auto& b = mp_library->getBookById(id);
|
||||
kiwix::Download* d;
|
||||
try {
|
||||
d = m_downloader.getDownload(b.getDownloadId());
|
||||
} catch(...) {
|
||||
b.setDownloadId("");
|
||||
mp_library->save();
|
||||
emit(mp_library->booksChanged());
|
||||
return values;
|
||||
}
|
||||
|
||||
d->updateStatus(true);
|
||||
if (d->getStatus() == kiwix::Download::COMPLETE) {
|
||||
b.setPath(d->getPath());
|
||||
b.setDownloadId("");
|
||||
mp_library->save();
|
||||
emit(mp_library->booksChanged());
|
||||
}
|
||||
for(auto& key: keys){
|
||||
ADD_V("id", getDid);
|
||||
if(key == "status") {
|
||||
switch(d->getStatus()){
|
||||
case kiwix::Download::ACTIVE:
|
||||
values.append("active");
|
||||
break;
|
||||
case kiwix::Download::WAITING:
|
||||
values.append("waiting");
|
||||
break;
|
||||
case kiwix::Download::PAUSED:
|
||||
values.append("paused");
|
||||
break;
|
||||
case kiwix::Download::ERROR:
|
||||
values.append("error");
|
||||
break;
|
||||
case kiwix::Download::COMPLETE:
|
||||
values.append("completed");
|
||||
break;
|
||||
case kiwix::Download::REMOVED:
|
||||
values.append("removed");
|
||||
break;
|
||||
default:
|
||||
values.append("unknown");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ADD_V("followedBy", getFollowedBy);
|
||||
ADD_V("path", getPath);
|
||||
if(key == "totalLength") {
|
||||
values.append(QString::number(d->getTotalLength()));
|
||||
}
|
||||
if(key == "completedLength") {
|
||||
values.append(QString::number(d->getCompletedLength()));
|
||||
}
|
||||
if(key == "downloadSpeed") {
|
||||
values.append(QString::number(d->getDownloadSpeed()));
|
||||
}
|
||||
if(key == "verifiedLength") {
|
||||
values.append(QString::number(d->getVerifiedLength()));
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
#undef ADD_V
|
||||
|
||||
int Downloader::getNbDownload() {
|
||||
return m_downloader.getNbDownload();
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
#ifndef DOWNLOADER_H
|
||||
#define DOWNLOADER_H
|
||||
|
||||
#include <QObject>
|
||||
#include "library.h"
|
||||
#include <kiwix/downloader.h>
|
||||
#include <QDebug>
|
||||
|
||||
class Downloader : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(int nbDownload READ getNbDownload NOTIFY downloadsChanged)
|
||||
Q_PROPERTY(QStringList downloadIds READ getDownloadIds NOTIFY downloadsChanged)
|
||||
public:
|
||||
explicit Downloader(Library* library, QObject *parent = nullptr);
|
||||
virtual ~Downloader();
|
||||
QStringList getDownloadIds();
|
||||
|
||||
signals:
|
||||
void downloadsChanged();
|
||||
|
||||
public slots:
|
||||
QString downloadBook(const QString& id);
|
||||
QStringList updateDownloadInfos(QString id, const QStringList& keys);
|
||||
int getNbDownload();
|
||||
|
||||
private:
|
||||
Library* mp_library;
|
||||
kiwix::Downloader m_downloader;
|
||||
};
|
||||
|
||||
#endif // DOWNLOADER_H
|
@ -14,8 +14,8 @@
|
||||
KiwixApp::KiwixApp(int& argc, char *argv[])
|
||||
: QApplication(argc, argv),
|
||||
m_library(),
|
||||
m_manager(&m_library),
|
||||
m_downloader(&m_library)
|
||||
m_downloader(),
|
||||
m_manager(&m_library, &m_downloader)
|
||||
{
|
||||
m_qtTranslator.load(QLocale(), "qt", "_",
|
||||
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
||||
@ -73,8 +73,6 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
|
||||
mp_mainWindow = new MainWindow;
|
||||
mp_tabWidget = mp_mainWindow->getTabWidget();
|
||||
mp_tabWidget->setContentManagerView(m_manager.getView());
|
||||
m_manager.getView()->registerObject("kiwix", this);
|
||||
m_manager.getView()->registerObject("downloader", &m_downloader);
|
||||
postInit();
|
||||
|
||||
mp_errorDialog = new QErrorMessage(mp_mainWindow);
|
||||
@ -83,6 +81,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[])
|
||||
|
||||
KiwixApp::~KiwixApp()
|
||||
{
|
||||
m_downloader.close();
|
||||
delete mp_errorDialog;
|
||||
delete mp_mainWindow;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "library.h"
|
||||
#include "contentmanager.h"
|
||||
#include "mainwindow.h"
|
||||
#include "downloader.h"
|
||||
#include "kiwix/downloader.h"
|
||||
#include "tabwidget.h"
|
||||
#include "tocsidebar.h"
|
||||
#include "urlschemehandler.h"
|
||||
@ -62,6 +62,7 @@ public:
|
||||
RequestInterceptor* getRequestInterceptor() { return &m_requestInterceptor; }
|
||||
Library* getLibrary() { return &m_library; }
|
||||
MainWindow* getMainWindow() { return mp_mainWindow; }
|
||||
kiwix::Downloader* getDownloader() { return &m_downloader; }
|
||||
TabWidget* getTabWidget() { return mp_tabWidget; }
|
||||
QAction* getAction(Actions action);
|
||||
|
||||
@ -78,8 +79,8 @@ protected:
|
||||
private:
|
||||
QTranslator m_qtTranslator, m_appTranslator;
|
||||
Library m_library;
|
||||
kiwix::Downloader m_downloader;
|
||||
ContentManager m_manager;
|
||||
Downloader m_downloader;
|
||||
MainWindow* mp_mainWindow;
|
||||
TabWidget* mp_tabWidget;
|
||||
QErrorMessage* mp_errorDialog;
|
||||
|
@ -93,47 +93,6 @@ void Library::save()
|
||||
m_library.writeToFile(appendToDirectory(getDataDirectory(),"library.xml"));
|
||||
}
|
||||
|
||||
#define ADD_V(KEY, METH) {if(key==KEY) values.append(QString::fromStdString((b.METH())));}
|
||||
QStringList Library::getBookInfos(QString id, const QStringList &keys)
|
||||
{
|
||||
QStringList values;
|
||||
if (id.endsWith(".zim")) {
|
||||
id.resize(id.size()-4);
|
||||
}
|
||||
auto& b = m_library.getBookById(id.toStdString());
|
||||
for(auto& key: keys){
|
||||
ADD_V("id", getId);
|
||||
ADD_V("path", getPath);
|
||||
ADD_V("indexPath", getIndexPath);
|
||||
ADD_V("title", getTitle);
|
||||
ADD_V("description", getDescription);
|
||||
ADD_V("language", getLanguage);
|
||||
ADD_V("creator", getCreator);
|
||||
ADD_V("publisher", getPublisher);
|
||||
ADD_V("date", getDate);
|
||||
ADD_V("url", getUrl);
|
||||
ADD_V("name", getName);
|
||||
ADD_V("tags", getTags);
|
||||
ADD_V("origId", getOrigId);
|
||||
ADD_V("faviconMimeType", getFaviconMimeType);
|
||||
ADD_V("downloadId", getDownloadId);
|
||||
if (key == "favicon") {
|
||||
auto s = b.getFavicon();
|
||||
values.append(QByteArray::fromStdString(s).toBase64());
|
||||
}
|
||||
if (key == "size") {
|
||||
values.append(QString::number(b.getSize()));
|
||||
}
|
||||
if (key == "articleCount") {
|
||||
values.append(QString::number(b.getArticleCount()));
|
||||
}
|
||||
if (key == "mediaCount") {
|
||||
values.append(QString::number(b.getMediaCount()));
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
kiwix::Book &Library::getBookById(QString id)
|
||||
{
|
||||
if (id.endsWith(".zim")) {
|
||||
|
@ -31,7 +31,6 @@ public:
|
||||
QStringList getBookIds();
|
||||
void save();
|
||||
public slots:
|
||||
QStringList getBookInfos(QString id, const QStringList &keys);
|
||||
QString openBookById(const QString& _id);
|
||||
kiwix::Book& getBookById(QString id);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user