Use a global profile to handle to configure scheme handler and download.

We don't need to configure the (default) profile each time we create a
new web page.

Let's create a specific profile for the application and use it everywhere.

This is the profile who is responsible to :

- configure itself with the schemeHandler for "zim"
- handle the download (this probably fix a bug if a user close the page
  while a download associated to the page is running)
This commit is contained in:
Matthieu Gautier 2020-02-07 15:51:45 +01:00 committed by Kelson
parent f1fb16466a
commit 62a2b57795
11 changed files with 73 additions and 49 deletions

View File

@ -48,6 +48,7 @@ SOURCES += \
src/main.cpp \
src/mainwindow.cpp \
src/kiwixapp.cpp \
src/kprofile.cpp \
src/blobbuffer.cpp \
src/library.cpp \
src/settingsmanager.cpp \
@ -74,6 +75,7 @@ SOURCES += \
HEADERS += \
src/mainwindow.h \
src/kiwixapp.h \
src/kprofile.h \
src/blobbuffer.h \
src/library.h \
src/settingsmanager.h \
@ -169,4 +171,4 @@ unix {
system(lrelease $$_PRO_FILE_)
}
RC_ICONS = resources/icons/kiwix/app_icon.ico
RC_ICONS = resources/icons/kiwix/app_icon.ico

View File

@ -6,10 +6,9 @@
ContentManagerView::ContentManagerView(QWidget *parent)
: QWebEngineView(parent)
{
page()->setWebChannel(&m_webChannel);
auto profile = page()->profile();
auto app = KiwixApp::instance();
profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
QWebEnginePage* page = new QWebEnginePage(KiwixApp::instance()->getProfile(), this);
setPage(page);
page->setWebChannel(&m_webChannel);
setContextMenuPolicy( Qt::NoContextMenu );
}

View File

@ -16,6 +16,7 @@
KiwixApp::KiwixApp(int& argc, char *argv[])
: QApplication(argc, argv),
m_settingsManager(),
m_profile(),
m_libraryDirectory(findLibraryDirectory()),
m_library(),
mp_downloader(nullptr),
@ -396,4 +397,4 @@ void KiwixApp::disableItemsOnLibraryPage(bool libraryDisplayed)
KiwixApp::instance()->getAction(KiwixApp::ZoomInAction)->setDisabled(libraryDisplayed);
KiwixApp::instance()->getAction(KiwixApp::ZoomOutAction)->setDisabled(libraryDisplayed);
KiwixApp::instance()->getAction(KiwixApp::ZoomResetAction)->setDisabled(libraryDisplayed);
}
}

View File

@ -8,7 +8,7 @@
#include <kiwix/kiwixserve.h>
#include "tabbar.h"
#include "tocsidebar.h"
#include "urlschemehandler.h"
#include "kprofile.h"
#include "settingsmanager.h"
#include <QApplication>
@ -68,7 +68,7 @@ public:
void showMessage(const QString& message);
UrlSchemeHandler* getSchemeHandler() { return &m_schemeHandler; }
KProfile* getProfile() { return &m_profile; }
Library* getLibrary() { return &m_library; }
MainWindow* getMainWindow() { return mp_mainWindow; }
kiwix::Downloader* getDownloader() { return mp_downloader; }
@ -101,7 +101,7 @@ protected:
private:
QTranslator m_qtTranslator, m_appTranslator;
SettingsManager m_settingsManager;
UrlSchemeHandler m_schemeHandler;
KProfile m_profile;
QString m_libraryDirectory;
Library m_library;
kiwix::Downloader* mp_downloader;

38
src/kprofile.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "kprofile.h"
#include "kiwixapp.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QWebEngineSettings>
KProfile::KProfile(QObject *parent) :
QWebEngineProfile(parent)
{
connect(this, &QWebEngineProfile::downloadRequested, this, &KProfile::startDownload);
installUrlSchemeHandler("zim", &m_schemeHandler);
settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
}
void KProfile::startDownload(QWebEngineDownloadItem* download)
{
QString defaultFileName = QString::fromStdString(getLastPathElement(download->url().toString().toStdString()));
QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(),
tr("Save File as"), defaultFileName);
if (fileName.isEmpty()) {
return;
}
QString extension = "." + download->url().url().section('.', -1);
if (!fileName.endsWith(extension)) {
fileName.append(extension);
}
download->setPath(fileName);
connect(download, &QWebEngineDownloadItem::finished, this, &KProfile::downloadFinished);
download->accept();
}
void KProfile::downloadFinished()
{
QMessageBox msgBox;
msgBox.setText(tr("The document has been downloaded."));
msgBox.exec();
}

23
src/kprofile.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef KPROFILE_H
#define KPROFILE_H
#include <QWebEngineProfile>
#include "urlschemehandler.h"
class KProfile : public QWebEngineProfile
{
Q_OBJECT
public:
KProfile(QObject *parent = nullptr);
private:
UrlSchemeHandler m_schemeHandler;
signals:
public slots:
void startDownload(QWebEngineDownloadItem*);
void downloadFinished();
};
#endif // KPROFILE_H

View File

@ -6,7 +6,6 @@
#include "kiwixapp.h"
#include "kconstants.h"
#include <QWebEngineProfile>
#include <QDesktopServices>
MainWindow::MainWindow(QWidget *parent) :

View File

@ -1,7 +1,6 @@
#include "settingsmanagerview.h"
#include "kiwixapp.h"
#include <QFile>
#include <QWebEngineProfile>
SettingsManagerView::SettingsManagerView(QWidget *parent) : QWebEngineView(parent)
{

View File

@ -7,11 +7,10 @@
#include <QWebEngineProfile>
WebPage::WebPage(QObject *parent) :
QWebEnginePage(parent)
QWebEnginePage(KiwixApp::instance()->getProfile(), parent)
{
action(QWebEnginePage::SavePage)->setVisible(false);
action(QWebEnginePage::ViewSource)->setVisible(false);
connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, this, &WebPage::startDownload);
}
bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
@ -22,27 +21,3 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio
}
return true;
}
void WebPage::startDownload(QWebEngineDownloadItem* download)
{
QString defaultFileName = QString::fromStdString(getLastPathElement(download->url().toString().toStdString()));
QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(),
tr("Save File as"), defaultFileName);
if (fileName.isEmpty()) {
return;
}
QString extension = "." + download->url().url().section('.', -1);
if (!fileName.endsWith(extension)) {
fileName.append(extension);
}
download->setPath(fileName);
connect(download, &QWebEngineDownloadItem::finished, this, &WebPage::downloadFinished);
download->accept();
}
void WebPage::downloadFinished()
{
QMessageBox msgBox;
msgBox.setText(tr("The document has been downloaded."));
msgBox.exec();
}

View File

@ -5,18 +5,11 @@
class WebPage : public QWebEnginePage
{
Q_OBJECT
public:
explicit WebPage(QObject *parent = nullptr);
protected:
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame);
signals:
public slots:
void startDownload(QWebEngineDownloadItem*);
void downloadFinished();
};
#endif // WEBPAGE_H

View File

@ -1,7 +1,6 @@
#include "webview.h"
#include <QAction>
#include <QWebEngineProfile>
#include <iostream>
#include "kiwixapp.h"
#include "webpage.h"
@ -12,10 +11,6 @@ WebView::WebView(QWidget *parent)
: QWebEngineView(parent)
{
setPage(new WebPage(this));
auto profile = page()->profile();
auto app = KiwixApp::instance();
profile->installUrlSchemeHandler("zim", app->getSchemeHandler());
this->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
QObject::connect(this, &QWebEngineView::urlChanged, this, &WebView::onUrlChanged);
}