Commit 0164b8b8 authored by Carl Schwan's avatar Carl Schwan 🚴
Browse files

Remove webengine from C++



Signed-off-by: Carl Schwan's avatarCarl Schwan <carl@carlschwan.eu>
parent ae13b023
......@@ -53,7 +53,7 @@ ecm_setup_version(${PROJECT_VERSION}
)
################# Find dependencies #################
find_package(Qt${QT_MAJOR_VERSION} ${QT_MIN_VERSION} REQUIRED COMPONENTS Core Gui Qml QuickControls2 Svg DBus Test QuickTest WebEngineWidgets)
find_package(Qt${QT_MAJOR_VERSION} ${QT_MIN_VERSION} REQUIRED COMPONENTS Core Gui Qml QuickControls2 Svg DBus Test QuickTest)
find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS Kirigami2 DBusAddons I18n CalendarCore ConfigWidgets WindowSystem CoreAddons QQC2DesktopStyle Contacts ItemModels XmlGui IconThemes)
find_package(Gpgme REQUIRED)
set_package_properties(KF5QQC2DesktopStyle PROPERTIES
......
......@@ -62,7 +62,8 @@ ecm_qt_declare_logging_category(kalendar_contact_plugin
EXPORT KALENDAR
)
target_link_libraries(kalendar_mail_plugin PRIVATE kalendar_lib KF5::MailCommon KF5::AkonadiMime Qt5::WebEngineWidgets)
target_link_libraries(kalendar_mail_plugin PRIVATE kalendar_lib KF5::MailCommon KF5::AkonadiMime)
ecm_qt_install_logging_categories(
EXPORT KALENDAR
......
......@@ -14,15 +14,10 @@
#include <QTextCodec>
#include <QTextDocument>
#include <QUuid>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineSettings>
#include <functional>
#include <KCodecs/KCharsets>
#include <KMime/Types>
#include <qstringliteral.h>
#include "../mimetreeparser/objecttreeparser.h"
......@@ -247,7 +242,7 @@ QString plainToHtml(const QString &body)
}
// TODO implement this function using a DOM tree parser
QString makeValidHtml(const QString &body, const QString &headElement)
QString makeValidHtml(const QString &body)
{
QString newBody = body;
QRegExp regEx;
......@@ -259,10 +254,10 @@ QString makeValidHtml(const QString &body, const QString &headElement)
if (!newBody.contains(regEx)) {
newBody = QLatin1String("<body>") + body + QLatin1String("<br/></body>");
}
regEx.setPattern(QStringLiteral("<head.*>"));
if (!newBody.contains(regEx)) {
newBody = QLatin1String("<head>") + headElement + QLatin1String("</head>") + body;
}
// regEx.setPattern(QStringLiteral("<head.*>"));
// if (!newBody.contains(regEx)) {
// newBody = QLatin1String("<head>") + headElement + QLatin1String("</head>") + body;
// }
newBody = QLatin1String("<html>") + body + QLatin1String("</html>");
}
return newBody;
......@@ -334,74 +329,12 @@ static QString stripSignature(const QString &msg)
return res;
}
static void setupPage(QWebEnginePage *page)
{
page->profile()->setHttpCacheType(QWebEngineProfile::MemoryHttpCache);
page->profile()->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
page->settings()->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
page->settings()->setAttribute(QWebEngineSettings::JavascriptCanAccessClipboard, false);
page->settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::XSSAuditingEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, false);
page->settings()->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, false);
page->settings()->setAttribute(QWebEngineSettings::HyperlinkAuditingEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::ScreenCaptureEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::WebGLEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, false);
page->settings()->setAttribute(QWebEngineSettings::Accelerated2dCanvasEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false);
page->settings()->setAttribute(QWebEngineSettings::AllowRunningInsecureContent, false);
}
static void plainMessageText(const QString &plainTextContent, const QString &htmlContent, const std::function<void(const QString &)> &callback)
{
const auto result = plainTextContent.isEmpty() ? toPlainText(htmlContent) : plainTextContent;
callback(result);
}
static QString extractHeaderBodyScript()
{
return QStringLiteral(
"(function() {"
"var res = {"
" body: document.getElementsByTagName('body')[0].innerHTML,"
" header: document.getElementsByTagName('head')[0].innerHTML"
"};"
"return res;"
"})()");
}
void htmlMessageText(const QString &plainTextContent, const QString &htmlContent, const std::function<void(const QString &body, QString &head)> &callback)
{
QString htmlElement = htmlContent;
if (htmlElement.isEmpty()) { // plain mails only
QString htmlReplace = plainTextContent.toHtmlEscaped();
htmlReplace = htmlReplace.replace(QStringLiteral("\n"), QStringLiteral("<br />"));
htmlElement = QStringLiteral("<html><head></head><body>%1</body></html>\n").arg(htmlReplace);
}
auto page = new QWebEnginePage;
setupPage(page);
page->setHtml(htmlElement);
page->runJavaScript(extractHeaderBodyScript(), QWebEngineScript::ApplicationWorld, [=](const QVariant &result) {
page->deleteLater();
const QVariantMap map = result.toMap();
auto bodyElement = map.value(QStringLiteral("body")).toString();
auto headerElement = map.value(QStringLiteral("header")).toString();
if (!bodyElement.isEmpty()) {
return callback(bodyElement, headerElement);
}
return callback(htmlElement, headerElement);
});
}
QString formatQuotePrefix(const QString &wildString, const QString &fromDisplayString)
{
QString result;
......@@ -490,44 +423,6 @@ QString quotedHtmlText(const QString &selection)
return content;
}
void applyCharset(const KMime::Message::Ptr msg, const KMime::Message::Ptr &origMsg)
{
// first convert the body from its current encoding to unicode representation
QTextCodec *bodyCodec = KCharsets::charsets()->codecForName(QString::fromLatin1(msg->contentType()->charset()));
if (!bodyCodec) {
bodyCodec = KCharsets::charsets()->codecForName(QStringLiteral("UTF-8"));
}
const QString body = bodyCodec->toUnicode(msg->body());
// then apply the encoding of the original message
msg->contentType()->setCharset(origMsg->contentType()->charset());
QTextCodec *codec = KCharsets::charsets()->codecForName(QString::fromLatin1(msg->contentType()->charset()));
if (!codec) {
qCritical() << "Could not get text codec for charset" << msg->contentType()->charset();
} else if (!codec->canEncode(body)) { // charset can't encode body, fall back to preferred
const QStringList charsets /*= preferredCharsets() */;
QList<QByteArray> chars;
chars.reserve(charsets.count());
for (const QString &charset : charsets) {
chars << charset.toLatin1();
}
// FIXME
QByteArray fallbackCharset /* = selectCharset(chars, body)*/;
if (fallbackCharset.isEmpty()) { // UTF-8 as fall-through
fallbackCharset = "UTF-8";
}
codec = KCharsets::charsets()->codecForName(QString::fromLatin1(fallbackCharset));
msg->setBody(codec->fromUnicode(body));
} else {
msg->setBody(codec->fromUnicode(body));
}
}
enum ReplyStrategy { ReplyList, ReplySmart, ReplyAll, ReplyAuthor, ReplyNone };
static QByteArray as7BitString(const KMime::Headers::Base *h)
......@@ -672,9 +567,6 @@ void MailTemplates::reply(const KMime::Message::Ptr &origMsg,
const std::function<void(const KMime::Message::Ptr &result)> &callback,
const KMime::Types::AddrSpecList &me)
{
// FIXME
const bool alwaysPlain = true;
// Decrypt what we have to
MimeTreeParser::ObjectTreeParser otp;
otp.parseObjectTree(origMsg.data());
......@@ -757,32 +649,19 @@ void MailTemplates::reply(const KMime::Message::Ptr &origMsg,
}
// The plain body is complete
QString plainBodyResult = plainBody + result;
htmlMessageText(plainTextContent, htmlContent, [=](const QString &body, const QString &headElement) {
QString result = stripSignature(body);
QString htmlBodyResult;
// The html body is complete
if (!alwaysPlain) {
htmlBodyResult = htmlBody + quotedHtmlText(result);
htmlBodyResult = makeValidHtml(htmlBodyResult, headElement);
}
// Assemble the message
msg->contentType()->clear(); // to get rid of old boundary
// Assemble the message
msg->contentType()->clear(); // to get rid of old boundary
KMime::Content *const mainTextPart = htmlBodyResult.isEmpty() ? createPlainPartContent(plainBodyResult, msg.data())
: createMultipartAlternativeContent(plainBodyResult, htmlBodyResult, msg.data());
mainTextPart->assemble();
KMime::Content *const mainTextPart = createPlainPartContent(plainBodyResult, msg.data());
mainTextPart->assemble();
msg->setBody(mainTextPart->encodedBody());
msg->setHeader(mainTextPart->contentType());
msg->setHeader(mainTextPart->contentTransferEncoding());
// FIXME this does more harm than good right now.
// applyCharset(msg, origMsg);
msg->assemble();
msg->setBody(mainTextPart->encodedBody());
msg->setHeader(mainTextPart->contentType());
msg->setHeader(mainTextPart->contentTransferEncoding());
msg->assemble();
callback(msg);
});
callback(msg);
});
}
......
......@@ -8,7 +8,7 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/..
)
find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test WebEngine)
find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test)
include( ${CMAKE_SOURCE_DIR}/cmake/modules/add_gpg_crypto_test.cmake )
......@@ -20,10 +20,8 @@ add_gpg_crypto_test(mailtemplatetest mailtemplatetest)
target_link_libraries(mailtemplatetest
Qt5::Core
Qt5::Test
Qt5::WebEngine
kalendar_lib
KF5::MailCommon
KF5::AkonadiMime
Qt5::WebEngineWidgets
kalendar_mail_plugin
)
......@@ -6,9 +6,7 @@
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTest>
#include <QtWebEngine>
#include <functional>
#include <qchar.h>
#include "../mailcrypto.h"
#include "../mailtemplates.h"
......@@ -78,11 +76,6 @@ class MailTemplateTest : public QObject
private Q_SLOTS:
void initTestCase()
{
QtWebEngine::initialize();
}
// Ensures we don't crash on garbage
void testEmpty()
{
......
......@@ -25,7 +25,6 @@ function(add_mimetreeparser_class_unittest _name _additionalSource)
kalendar_lib
KF5::MailCommon
KF5::AkonadiMime
Qt5::WebEngineWidgets
kalendar_mail_plugin
)
add_test(NAME ${_name} COMMAND $<TARGET_FILE:${_name}>)
......@@ -42,7 +41,6 @@ function(add_mimetreeparser_crypto_unittest _name)
Gpgme::Gpgme
KF5::MailCommon
KF5::AkonadiMime
Qt5::WebEngineWidgets
kalendar_mail_plugin
)
endfunction()
......
......@@ -21,7 +21,6 @@ target_link_libraries(mimetreeparsertest
kalendar_lib
KF5::MailCommon
KF5::AkonadiMime
Qt5::WebEngineWidgets
kalendar_mail_plugin
)
......@@ -33,7 +32,6 @@ target_link_libraries(gpgerrortest
kalendar_lib
KF5::MailCommon
KF5::AkonadiMime
Qt5::WebEngineWidgets
kalendar_mail_plugin
)
add_test(NAME gpgerrortest COMMAND $<TARGET_FILE:gpgerrortest>)
......@@ -10,7 +10,7 @@ include_directories(
include_directories(${GPGME_INCLUDE_DIRS})
include(ECMAddTests)
find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test WebEngine)
find_package(Qt5 REQUIRED NO_MODULE COMPONENTS Core Test)
add_executable(cryptotest cryptotest.cpp)
add_gpg_crypto_test(cryptotest cryptotest)
......@@ -19,8 +19,6 @@ target_link_libraries(cryptotest
kalendar_mail_plugin
Qt5::Core
Qt5::Test
Qt5::WebEngine
Qt5::WebEngineWidgets
KF5::MailCommon
KF5::AkonadiMime
)
......@@ -5,17 +5,12 @@
#include <QDebug>
#include <QTest>
#include <QtWebEngine>
class CryptoTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
QtWebEngine::initialize();
}
private Q_SLOTS:
void testDecrypt()
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment