Messagelib

mailsourcewebengineviewer.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "mailsourcewebengineviewer.h"
8 
9 using namespace MessageViewer;
10 
11 #include "findbar/findbarsourceview.h"
12 #include "mailsourceviewtextbrowserwidget.h"
13 #include "messageviewer/messageviewerutil.h"
14 #include <KPIMTextEdit/SlideContainer>
15 
16 #include <KSyntaxHighlighting/Definition>
17 #include <KSyntaxHighlighting/SyntaxHighlighter>
18 #include <KSyntaxHighlighting/Theme>
19 
20 #include <KLocalizedString>
21 #include <PimCommon/PimUtil>
22 #include <QTabWidget>
23 
24 #include <KConfigGroup>
25 #include <QShortcut>
26 #include <QVBoxLayout>
27 
28 #include <KSharedConfig>
29 #include <QDialogButtonBox>
30 #include <QPushButton>
31 #include <QWebEnginePage>
32 
33 using namespace MessageViewer;
34 namespace
35 {
36 static const char myMailSourceWebEngineViewerConfigGroupName[] = "MailSourceWebEngineViewer";
37 }
38 MailSourceWebEngineViewer::MailSourceWebEngineViewer(QWidget *parent)
39  : QDialog(parent)
40 {
41  setAttribute(Qt::WA_DeleteOnClose);
42  auto mainLayout = new QVBoxLayout(this);
43  mRawBrowser = new MailSourceViewTextBrowserWidget(QStringLiteral("Email"), this);
44 #ifndef NDEBUG
45  mShowHtmlSource = true;
46 #endif
47  mShowHtmlSource = mShowHtmlSource || !qEnvironmentVariableIsEmpty("KDEPIM_DEBUGGING");
48  if (mShowHtmlSource) {
49  mTabWidget = new QTabWidget(this);
50  mainLayout->addWidget(mTabWidget);
51 
52  mTabWidget->addTab(mRawBrowser, i18nc("Unchanged mail message", "Raw Source"));
53  mTabWidget->setTabToolTip(0, i18n("Raw, unmodified mail as it is stored on the filesystem or on the server"));
54 
55  mHtmlBrowser = new MailSourceViewTextBrowserWidget(QStringLiteral("HTML"), this);
56  mTabWidget->addTab(mHtmlBrowser, i18nc("Mail message as shown, in HTML format", "HTML Source"));
57  mTabWidget->setTabToolTip(1, i18n("HTML code for displaying the message to the user"));
58 
59  mTabWidget->setCurrentIndex(0);
60  } else {
61  mainLayout->addWidget(mRawBrowser);
62  }
63 
64  // combining the shortcuts in one qkeysequence() did not work...
65  auto shortcut = new QShortcut(this);
66  shortcut->setKey(Qt::Key_Escape);
67  connect(shortcut, &QShortcut::activated, this, &MailSourceWebEngineViewer::close);
68  shortcut = new QShortcut(this);
69  shortcut->setKey(Qt::Key_W | Qt::CTRL);
70  connect(shortcut, &QShortcut::activated, this, &MailSourceWebEngineViewer::close);
71 
72  mRawBrowser->textBrowser()->setFocus();
73  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
74  connect(buttonBox, &QDialogButtonBox::rejected, this, &MailSourceWebEngineViewer::reject);
75  connect(buttonBox->button(QDialogButtonBox::Close), &QPushButton::clicked, this, &MailSourceWebEngineViewer::close);
76 
77  mainLayout->addWidget(buttonBox);
78  readConfig();
79 }
80 
81 MailSourceWebEngineViewer::~MailSourceWebEngineViewer()
82 {
83  writeConfig();
84 }
85 
86 void MailSourceWebEngineViewer::readConfig()
87 {
88  KConfigGroup group(KSharedConfig::openStateConfig(), myMailSourceWebEngineViewerConfigGroupName);
89  const QSize size = group.readEntry("Size", QSize(600, 400));
90  if (size.isValid()) {
91  resize(size);
92  }
93 }
94 
95 void MailSourceWebEngineViewer::writeConfig()
96 {
97  KConfigGroup group(KSharedConfig::openStateConfig(), myMailSourceWebEngineViewerConfigGroupName);
98  group.writeEntry("Size", size());
99  group.sync();
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 }
bool isValid() const const
void writeConfig(KConfigGroup &group) const
Saves the configuration for the mailing list to the config group.
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
void readConfig(const KConfigGroup &group)
Restores the configuration for the mailing list from the config group.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(bool checked)
void activated()
WA_DeleteOnClose
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & shortcut(StandardShortcut id)
Key_Escape
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.