Messagelib

mailsourcewebengineviewer.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 "mailsourcewebengineviewer.h"
8
9using namespace MessageViewer;
10
11#include "mailsourceviewtextbrowserwidget.h"
12#include "messageviewer/messageviewerutil.h"
13// #include <TextAddonsWidgets/SlideContainer>
14
15#include <KSyntaxHighlighting/Definition>
16#include <KSyntaxHighlighting/SyntaxHighlighter>
17#include <KSyntaxHighlighting/Theme>
18
19#include <KLocalizedString>
20#include <PimCommon/PimUtil>
21#include <QTabWidget>
22
23#include <KConfigGroup>
24#include <KWindowConfig>
25#include <QShortcut>
26#include <QVBoxLayout>
27#include <QWindow>
28
29#include <KSharedConfig>
30#include <QDialogButtonBox>
31#include <QPushButton>
32#include <QWebEnginePage>
33
34using namespace MessageViewer;
35namespace
36{
37static const char myMailSourceWebEngineViewerConfigGroupName[] = "MailSourceWebEngineViewer";
38}
39MailSourceWebEngineViewer::MailSourceWebEngineViewer(QWidget *parent)
40 : QDialog(parent)
41 , mRawBrowser(new MailSourceViewTextBrowserWidget(QStringLiteral("Email"), this))
42{
43 setAttribute(Qt::WA_DeleteOnClose);
44 auto mainLayout = new QVBoxLayout(this);
45#ifndef NDEBUG
46 mShowHtmlSource = true;
47#endif
48 mShowHtmlSource = mShowHtmlSource || !qEnvironmentVariableIsEmpty("KDEPIM_DEBUGGING");
49 if (mShowHtmlSource) {
50 mTabWidget = new QTabWidget(this);
51 mainLayout->addWidget(mTabWidget);
52
53 mTabWidget->addTab(mRawBrowser, i18nc("Unchanged mail message", "Raw Source"));
54 mTabWidget->setTabToolTip(0, i18n("Raw, unmodified mail as it is stored on the filesystem or on the server"));
55
56 mHtmlBrowser = new MailSourceViewTextBrowserWidget(QStringLiteral("HTML"), this);
57 mTabWidget->addTab(mHtmlBrowser, i18nc("Mail message as shown, in HTML format", "HTML Source"));
58 mTabWidget->setTabToolTip(1, i18n("HTML code for displaying the message to the user"));
59
60 mTabWidget->setCurrentIndex(0);
61 } else {
62 mainLayout->addWidget(mRawBrowser);
63 }
64
65 // combining the shortcuts in one qkeysequence() did not work...
66 auto shortcut = new QShortcut(this);
67 shortcut->setKey(Qt::Key_Escape);
68 connect(shortcut, &QShortcut::activated, this, &MailSourceWebEngineViewer::close);
69 shortcut = new QShortcut(this);
70 shortcut->setKey(Qt::Key_W | Qt::CTRL);
71 connect(shortcut, &QShortcut::activated, this, &MailSourceWebEngineViewer::close);
72
73 mRawBrowser->textBrowser()->setFocus();
74 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
75 connect(buttonBox, &QDialogButtonBox::rejected, this, &MailSourceWebEngineViewer::reject);
76 connect(buttonBox->button(QDialogButtonBox::Close), &QPushButton::clicked, this, &MailSourceWebEngineViewer::close);
77
78 mainLayout->addWidget(buttonBox);
79 readConfig();
80}
81
82MailSourceWebEngineViewer::~MailSourceWebEngineViewer()
83{
84 writeConfig();
85}
86
87void MailSourceWebEngineViewer::readConfig()
88{
89 create(); // ensure a window is created
90 windowHandle()->resize(QSize(600, 400));
91 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myMailSourceWebEngineViewerConfigGroupName));
93 resize(windowHandle()->size()); // workaround for QTBUG-40584
94}
95
96void MailSourceWebEngineViewer::writeConfig()
97{
98 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myMailSourceWebEngineViewerConfigGroupName));
100}
101
102void MailSourceWebEngineViewer::setRawSource(const QString &source)
103{
104 mRawBrowser->setText(source);
105}
106
107void MailSourceWebEngineViewer::setDisplayedSource(QWebEnginePage *page)
108{
109 if (mShowHtmlSource) {
110 if (page) {
111 MailSourceViewTextBrowserWidget *browser = mHtmlBrowser;
112 page->toHtml([browser](const QString &result) {
113 browser->setPlainText(result);
114 });
115 }
116 }
117}
118
119void MailSourceWebEngineViewer::setFixedFont()
120{
121 mRawBrowser->setFixedFont();
122 if (mShowHtmlSource) {
123 mHtmlBrowser->setFixedFont();
124 }
125}
126
127#include "moc_mailsourcewebengineviewer.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & shortcut(StandardShortcut id)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void clicked(bool checked)
int result() const const
void activated()
Key_Escape
WA_DeleteOnClose
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.