diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index bf3f519..e1a95cd 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -36,7 +36,8 @@ SOURCES += \ kiwixapp.cpp \ blobbuffer.cpp \ kiwixrequestinterceptor.cpp \ - kiwixwebview.cpp + kiwixwebview.cpp \ + library.cpp HEADERS += \ mainwindow.h \ @@ -44,7 +45,8 @@ HEADERS += \ kiwixapp.h \ blobbuffer.h \ kiwixrequestinterceptor.h \ - kiwixwebview.h + kiwixwebview.h \ + library.h FORMS += \ mainwindow.ui diff --git a/kiwixapp.cpp b/kiwixapp.cpp index 1696f61..1613dc2 100644 --- a/kiwixapp.cpp +++ b/kiwixapp.cpp @@ -2,8 +2,7 @@ #include "zim/error.h" KiwixApp::KiwixApp(int& argc, char *argv[]) - : QApplication(argc, argv), - reader(nullptr) + : QApplication(argc, argv) { mainWindow = new MainWindow; setApplicationName("kiwix-desktop"); @@ -13,30 +12,17 @@ KiwixApp::KiwixApp(int& argc, char *argv[]) KiwixApp::~KiwixApp() { delete mainWindow; - if (reader) - delete reader; } void KiwixApp::openZimFile(const QString &zimfile) { - if (reader) - delete reader; const std::string zimfile_ = zimfile.toLocal8Bit().constData(); std::cout << "Opening " << zimfile_ << std::endl; try { - reader = new kiwix::Reader(zimfile_); - } catch (const zim::ZimFileFormatError& e) { - std::cout << "Cannot open " << zimfile_ << std::endl; - std::cout << e.what() << std::endl; - reader = nullptr; + auto zimId = library.openBook(zimfile); + mainWindow->displayReader(library.getReader(zimId)); } catch (const std::exception& e) { std::cout << "oup" << e.what() << std::endl; - reader = nullptr; } } - -kiwix::Reader* KiwixApp::getReader() -{ - return reader; -} diff --git a/kiwixapp.h b/kiwixapp.h index fb368f5..ca4fccc 100644 --- a/kiwixapp.h +++ b/kiwixapp.h @@ -5,7 +5,7 @@ #include "kiwixrequestinterceptor.h" #include -#include +#include "library.h" #include "mainwindow.h" @@ -16,12 +16,16 @@ public: virtual ~KiwixApp(); void openZimFile(const QString& zimfile); - kiwix::Reader* getReader(); - + KiwixSchemeHandler* getSchemeHandler() { return &schemeHandler; } + KiwixRequestInterceptor* getRequestInterceptor() { return &requestIntercetor; } + Library* getLibrary() { return &library; } private: - kiwix::Reader* reader; + Library library; MainWindow* mainWindow; + + KiwixSchemeHandler schemeHandler; + KiwixRequestInterceptor requestIntercetor; }; #endif // KIWIXAPP_H diff --git a/kiwixschemehandler.cpp b/kiwixschemehandler.cpp index 3b8a5a9..5820e8e 100644 --- a/kiwixschemehandler.cpp +++ b/kiwixschemehandler.cpp @@ -16,11 +16,14 @@ void KiwixSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request) { std::cout << "Handling request " << request->requestUrl().toString().toUtf8().constData() << std::endl; - std::string url = request->requestUrl().path().toUtf8().constData(); + auto qurl = request->requestUrl(); + std::string url = qurl.path().toUtf8().constData(); std::cout << "Url is " << url << std::endl; if (url[0] == '/') url = url.substr(1); - auto reader = static_cast(KiwixApp::instance())->getReader(); + auto library = static_cast(KiwixApp::instance())->getLibrary(); + auto zim_id = qurl.host(); + auto reader = library->getReader(zim_id); if ( reader == nullptr) { request->fail(QWebEngineUrlRequestJob::UrlNotFound); return; diff --git a/kiwixwebview.cpp b/kiwixwebview.cpp index 3b860b1..7aa230e 100644 --- a/kiwixwebview.cpp +++ b/kiwixwebview.cpp @@ -2,15 +2,25 @@ #include #include +#include "kiwixapp.h" KiwixWebView::KiwixWebView(QWidget *parent) : QWebEngineView(parent) { auto profile = page()->profile(); - profile->installUrlSchemeHandler("zim", &schemeHandler); - profile->setRequestInterceptor(&requestInterceptor); - + auto app = static_cast(KiwixApp::instance()); + profile->installUrlSchemeHandler("zim", app->getSchemeHandler()); + profile->setRequestInterceptor(app->getRequestInterceptor()); } KiwixWebView::~KiwixWebView() {} + + +void KiwixWebView::initFromReader(std::shared_ptr reader) +{ + std::string url("zim://"); + url += reader->getId(); + url += ".zim/"; + page()->setUrl(QUrl(QString::fromStdString(url))); +} diff --git a/kiwixwebview.h b/kiwixwebview.h index ed19a02..ae20166 100644 --- a/kiwixwebview.h +++ b/kiwixwebview.h @@ -2,9 +2,7 @@ #define KIWIXWEBVIEW_H #include -#include "kiwixschemehandler.h" -#include "kiwixrequestinterceptor.h" - +#include class KiwixWebView : public QWebEngineView { @@ -14,9 +12,7 @@ public: KiwixWebView(QWidget *parent = Q_NULLPTR); virtual ~KiwixWebView(); -private: - KiwixSchemeHandler schemeHandler; - KiwixRequestInterceptor requestInterceptor; + void initFromReader(std::shared_ptr reader); }; #endif // KIWIXWEBVIEW_H diff --git a/library.cpp b/library.cpp new file mode 100644 index 0000000..74dbfcc --- /dev/null +++ b/library.cpp @@ -0,0 +1,30 @@ +#include "library.h" + +Library::Library() +{ + +} + +QString Library::openBook(const QString &zimPath) +{ + for(auto it=readers_map.begin(); + it != readers_map.end(); + it++) + { + if(QString::fromStdString(it->second->getZimFilePath()) == zimPath) + return it->first; + } + const std::string zimPath_ = zimPath.toLocal8Bit().constData(); + auto reader = std::shared_ptr(new kiwix::Reader(zimPath_)); + auto id = QString::fromStdString(reader->getId() + ".zim"); + readers_map[id] = reader; + return id; +} + +std::shared_ptr Library::getReader(const QString &zimId) +{ + auto it = readers_map.find(zimId); + if (it != readers_map.end()) + return it->second; + return nullptr; +} diff --git a/library.h b/library.h new file mode 100644 index 0000000..89a7415 --- /dev/null +++ b/library.h @@ -0,0 +1,18 @@ +#ifndef LIBRARY_H +#define LIBRARY_H + +#include +#include +#include + +class Library : public kiwix::Manager +{ +public: + Library(); + QString openBook(const QString& zimPath); + std::shared_ptr getReader(const QString& zimId); +private: + std::map> readers_map; +}; + +#endif // LIBRARY_H diff --git a/mainwindow.cpp b/mainwindow.cpp index b14f8a6..8161717 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -9,10 +9,7 @@ MainWindow::MainWindow(QWidget *parent) : ui(new Ui::MainWindow) { ui->setupUi(this); - ui->webview->page()->setUrl(QUrl("http://foo.zim")); - //ui->webview->page()->setUrl(QUrl("http://localhost:8080")); - - QObject::connect(ui->webview, SIGNAL(urlChanged(const QUrl&)), this, SLOT(on_urlChanged_triggered(const QUrl&))); + ui->tabWidget->tabBar()->setExpanding(false); } MainWindow::~MainWindow() @@ -20,14 +17,16 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::on_pushButton_clicked() +void MainWindow::displayReader(std::shared_ptr reader) { - ui->webview->reload(); -} - - -void MainWindow::on_urlChanged_triggered(const QUrl& url) -{ - std::cout << "new url : " << url.toString().toUtf8().constData() << std::endl; - ui->addressBar->setText(url.toString()); + auto webview = new KiwixWebView(); + std::string favicon_content; + std::string favicon_mimetype; + reader->getFavicon(favicon_content, favicon_mimetype); + QPixmap pixmap; + pixmap.loadFromData((const uchar*)favicon_content.data(), favicon_content.size()); + auto icon = QIcon(pixmap); + // Ownership of webview is passed to the tabWidget + ui->tabWidget->addTab(webview, icon, QString::fromStdString(reader->getTitle())); + webview->initFromReader(reader); } diff --git a/mainwindow.h b/mainwindow.h index 6432c14..e3abd5b 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include "kiwixwebview.h" namespace Ui { class MainWindow; @@ -15,12 +16,11 @@ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); -private slots: - void on_pushButton_clicked(); - void on_urlChanged_triggered(const QUrl& url); + void displayReader(std::shared_ptr reader); private: Ui::MainWindow *ui; + std::map, KiwixWebView*> webviews_map; }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 8603c11..50141bb 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -16,28 +16,17 @@ - - - QLayout::SetMinimumSize + + + QTabWidget::Rounded - - - - false - - - - - - - Refresh - - - - - - - + + -1 + + + false + + @@ -68,14 +57,6 @@ - - - KiwixWebView - QWidget -
kiwixwebview.h
- 1 -
-