Messagelib

cidreferencesurlinterceptor.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "cidreferencesurlinterceptor.h"
8 #include "htmlwriter/webengineembedpart.h"
9 
10 #include <QBuffer>
11 #include <QDebug>
12 #include <QImage>
13 #include <QUrl>
14 #include <QWebEngineUrlRequestInfo>
15 
16 using namespace MessageViewer;
17 // #define LOAD_FROM_FILE 1
18 CidReferencesUrlInterceptor::CidReferencesUrlInterceptor(QObject *parent)
19  : WebEngineViewer::NetworkPluginUrlInterceptorInterface(parent)
20 {
21 }
22 
23 CidReferencesUrlInterceptor::~CidReferencesUrlInterceptor() = default;
24 
25 bool CidReferencesUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
26 {
27  const QUrl urlRequestUrl(info.requestUrl());
28  if (urlRequestUrl.scheme() == QLatin1StringView("cid")) {
29  if (info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) {
30  const QUrl newUrl = QUrl(MessageViewer::WebEngineEmbedPart::self()->contentUrl(urlRequestUrl.path()));
31  if (!newUrl.isEmpty()) {
32 #ifdef LOAD_FROM_FILE
33  QImage image(newUrl.path());
34  QByteArray ba;
35  QBuffer buf(&ba);
36  // Reduce image.
37  image = image.scaled(800, 600);
38  image.save(&buf, "png");
39 
40  const QByteArray hexed = ba.toBase64();
41  buf.close();
42  const QUrl r = QUrl(QString::fromUtf8(QByteArray("data:image/png;base64,") + hexed));
43  info.redirect(r);
44 #else
45  info.redirect(newUrl);
46 #endif
47  }
48  }
49  }
50  return false;
51 }
52 
53 #include "moc_cidreferencesurlinterceptor.cpp"
QString fromUtf8(const char *str, int size)
QByteArray toBase64(QByteArray::Base64Options options) const const
bool isEmpty() const const
QString path(QUrl::ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:19 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.