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 
9 using 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 
34 using namespace MessageViewer;
35 namespace
36 {
37 static const char myMailSourceWebEngineViewerConfigGroupName[] = "MailSourceWebEngineViewer";
38 }
39 MailSourceWebEngineViewer::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 
82 MailSourceWebEngineViewer::~MailSourceWebEngineViewer()
83 {
84  writeConfig();
85 }
86 
87 void MailSourceWebEngineViewer::readConfig()
88 {
89  create(); // ensure a window is created
90  windowHandle()->resize(QSize(600, 400));
91  KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myMailSourceWebEngineViewerConfigGroupName));
92  KWindowConfig::restoreWindowSize(windowHandle(), group);
93  resize(windowHandle()->size()); // workaround for QTBUG-40584
94 }
95 
96 void MailSourceWebEngineViewer::writeConfig()
97 {
98  KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myMailSourceWebEngineViewerConfigGroupName));
99  KWindowConfig::saveWindowSize(windowHandle(), group);
100 }
101 
102 void MailSourceWebEngineViewer::setRawSource(const QString &source)
103 {
104  mRawBrowser->setText(source);
105 }
106 
107 void 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 
119 void MailSourceWebEngineViewer::setFixedFont()
120 {
121  mRawBrowser->setFixedFont();
122  if (mShowHtmlSource) {
123  mHtmlBrowser->setFixedFont();
124  }
125 }
126 
127 #include "moc_mailsourcewebengineviewer.cpp"
const QList< QKeySequence > & shortcut(StandardShortcut id)
void activated()
void clicked(bool checked)
QAction * create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent)
QString i18n(const char *text, const TYPE &arg...)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
Key_Escape
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void readConfig(const KConfigGroup &group)
Restores the configuration for the mailing list from the config group.
WA_DeleteOnClose
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.