Libkleo

auditlogviewer.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2021 Laurent Montel <montel@kde.org>
3 SPDX-FileCopyrightText: 2021 g10 Code GmbH
4 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include <config-libkleo.h>
10
11#include "auditlogviewer.h"
12
13#include <libkleo/auditlogentry.h>
14#include <libkleo/formatting.h>
15
16#include <KConfigGroup>
17#include <KGuiItem>
18#include <KLocalizedString>
19#include <KMessageBox>
20#include <KSharedConfig>
21#include <KStandardGuiItem>
22
23#include <QTextEdit>
24
25#include <QDebug>
26#include <QDialogButtonBox>
27#include <QFileDialog>
28#include <QPushButton>
29#include <QSaveFile>
30#include <QStyle>
31#include <QTextStream>
32#include <QVBoxLayout>
33
34#include <gpgme++/error.h>
35
36using namespace Kleo;
37
38AuditLogViewer::AuditLogViewer(const QString &log, QWidget *parent)
39 : QDialog(parent)
40 , m_log(/* sic */)
41 , m_textEdit(new QTextEdit(this))
42{
43 setWindowTitle(i18nc("@title:window", "View GnuPG Audit Log"));
44 QDialogButtonBox *buttonBox = new QDialogButtonBox{};
45
46 auto copyClipBtn = buttonBox->addButton(i18nc("@action:button", "&Copy to Clipboard"), QDialogButtonBox::ActionRole);
47 copyClipBtn->setObjectName(QLatin1StringView("copyClipBtn"));
48 copyClipBtn->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
49 connect(copyClipBtn, &QPushButton::clicked, this, &AuditLogViewer::slotCopyClip);
50
51 auto saveAsBtn = buttonBox->addButton(i18nc("@action:button", "&Save to Disk..."), QDialogButtonBox::ActionRole);
52 saveAsBtn->setObjectName(QLatin1StringView("saveAsBtn"));
53 saveAsBtn->setIcon(QIcon::fromTheme(QStringLiteral("document-save-as")));
54 connect(saveAsBtn, &QPushButton::clicked, this, &AuditLogViewer::slotSaveAs);
55
56 auto closeBtn = buttonBox->addButton(QString{}, QDialogButtonBox::AcceptRole);
57 closeBtn->setObjectName(QLatin1StringView("Close"));
59
60 m_textEdit->setObjectName(QLatin1StringView("m_textEdit"));
61 m_textEdit->setReadOnly(true);
62
63 auto mainLayout = new QVBoxLayout(this);
64 mainLayout->addWidget(m_textEdit);
65 mainLayout->addWidget(buttonBox);
66
67#if 0
68 qDebug() << "buttonBox->style()->styleHint(QStyle::SH_DialogButtonLayout, ...):" << buttonBox->style()->styleHint(QStyle::SH_DialogButtonLayout, nullptr, buttonBox);
69 qDebug() << __func__ << "buttonBox->focusProxy():" << buttonBox->focusProxy();
70 qDebug() << __func__ << "copyClipBtn->nextInFocusChain():" << copyClipBtn->nextInFocusChain();
71 qDebug() << __func__ << "saveAsBtn->nextInFocusChain():" << saveAsBtn->nextInFocusChain();
72 qDebug() << __func__ << "closeBtn->nextInFocusChain():" << closeBtn->nextInFocusChain();
73#endif
74
77
78 setAuditLog(log);
79
80 readConfig();
81}
82
83AuditLogViewer::~AuditLogViewer()
84{
85 writeConfig();
86}
87
88// static
89void AuditLogViewer::showAuditLog(QWidget *parent, const AuditLogEntry &auditLog, const QString &title)
90{
91 const GpgME::Error err = auditLog.error();
92 if (err.code() == GPG_ERR_NOT_IMPLEMENTED) {
93 KMessageBox::information(parent, i18n("Your system does not have support for GnuPG Audit Logs"), i18nc("@title:window", "System Error"));
94 return;
95 }
96 if (err && err.code() != GPG_ERR_NO_DATA) {
98 i18n("An error occurred while trying to retrieve the GnuPG Audit Log:\n%1", Formatting::errorAsString(err)),
99 i18n("GnuPG Audit Log Error"));
100 return;
101 }
102 if (auditLog.text().isEmpty()) {
103 KMessageBox::information(parent, i18n("No GnuPG Audit Log available for this operation."), i18nc("@title:window", "No GnuPG Audit Log"));
104 return;
105 }
106
107 const auto alv = new AuditLogViewer{auditLog.text(), parent};
108 alv->setAttribute(Qt::WA_DeleteOnClose);
109 alv->setWindowTitle(title.isEmpty() ? i18nc("@title:window", "GnuPG Audit Log Viewer") : title);
110 alv->show();
111}
112
113void AuditLogViewer::setAuditLog(const QString &log)
114{
115 if (log == m_log) {
116 return;
117 }
118 m_log = log;
119 m_textEdit->setHtml(QLatin1StringView("<qt>") + log + QLatin1StringView("</qt>"));
120}
121
122void AuditLogViewer::slotSaveAs()
123{
124 const QString fileName = QFileDialog::getSaveFileName(this, i18n("Choose File to Save GnuPG Audit Log to"));
125 if (fileName.isEmpty()) {
126 return;
127 }
128
129 QSaveFile file(fileName);
130
131 if (file.open(QIODevice::WriteOnly)) {
132 QTextStream s(&file);
133 s << "<html><head>";
134 if (!windowTitle().isEmpty()) {
135 s << "\n<title>" << windowTitle().toHtmlEscaped() << "</title>\n";
136 }
137 s << "</head><body>\n" << m_log << "\n</body></html>\n";
138 s.flush();
139 file.commit();
140 }
141
142 if (const int err = file.error()) {
144 i18n("Could not save to file \"%1\": %2", file.fileName(), QString::fromLocal8Bit(strerror(err))),
145 i18nc("@title:window", "File Save Error"));
146 }
147}
148
149void AuditLogViewer::slotCopyClip()
150{
151#ifdef HAVE_PIMTEXTEDIT
152 m_textEdit->editor()->selectAll();
153 m_textEdit->editor()->copy();
154 m_textEdit->editor()->textCursor().clearSelection();
155#else
156 m_textEdit->selectAll();
157 m_textEdit->copy();
158 m_textEdit->textCursor().clearSelection();
159#endif
160}
161
162void AuditLogViewer::readConfig()
163{
164 KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("AuditLogViewer"));
165 const QSize size = group.readEntry("Size", QSize());
166 if (size.isValid()) {
167 resize(size);
168 } else {
169 resize(600, 400);
170 }
171}
172
173void AuditLogViewer::writeConfig()
174{
175 KConfigGroup group(KSharedConfig::openConfig(), QStringLiteral("AuditLogViewer"));
176 group.writeEntry("Size", size());
177 group.sync();
178}
179
180#include "moc_auditlogviewer.cpp"
static void assign(QPushButton *button, const KGuiItem &item)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem close()
void clicked(bool checked)
virtual void accept()
virtual void reject()
QPushButton * addButton(StandardButton button)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QIcon fromTheme(const QString &name)
QObject * parent() const const
void setObjectName(QAnyStringView name)
QString fromLocal8Bit(QByteArrayView str)
bool isEmpty() const const
SH_DialogButtonLayout
virtual int styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const const=0
WA_DeleteOnClose
void clearSelection()
void copy()
void setHtml(const QString &text)
void selectAll()
QTextCursor textCursor() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QWidget * focusProxy() const const
QWidget * nextInFocusChain() const const
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.