File indexing completed on 2024-12-29 04:54:46

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "sievescriptparsingerrordialog.h"
0008 #include <PimCommon/PimUtil>
0009 #include <TextCustomEditor/RichTextEditorWidget>
0010 
0011 #include <KLocalizedString>
0012 #include <KSharedConfig>
0013 
0014 #include <KConfigGroup>
0015 #include <KWindowConfig>
0016 #include <QDialogButtonBox>
0017 #include <QPushButton>
0018 #include <QVBoxLayout>
0019 #include <QWindow>
0020 #include <cerrno>
0021 using namespace KSieveUi;
0022 namespace
0023 {
0024 static const char mySieveScriptParsingErrorDialogGroupName[] = "SieveScriptParsingErrorDialog";
0025 }
0026 SieveScriptParsingErrorDialog::SieveScriptParsingErrorDialog(QWidget *parent)
0027     : QDialog(parent)
0028     , mTextEdit(new TextCustomEditor::RichTextEditorWidget(this))
0029 {
0030     setWindowTitle(i18nc("@title:window", "Sieve Parsing Error"));
0031     auto mainLayout = new QVBoxLayout(this);
0032 
0033     mTextEdit->setReadOnly(true);
0034 
0035     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
0036     auto user1Button = new QPushButton(this);
0037     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0038     connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveScriptParsingErrorDialog::reject);
0039     user1Button->setText(i18n("Save As..."));
0040     readConfig();
0041     connect(user1Button, &QPushButton::clicked, this, &SieveScriptParsingErrorDialog::slotSaveAs);
0042     mainLayout->addWidget(mTextEdit);
0043     mainLayout->addWidget(buttonBox);
0044 }
0045 
0046 SieveScriptParsingErrorDialog::~SieveScriptParsingErrorDialog()
0047 {
0048     writeConfig();
0049 }
0050 
0051 void SieveScriptParsingErrorDialog::setError(QString script, QString error)
0052 {
0053     QString str;
0054     str = QLatin1StringView("<b>") + i18n("Sieve script:") + QLatin1StringView("</b><br>");
0055     str += script.replace(QLatin1Char('\n'), QStringLiteral("<br>")) + QLatin1StringView("<br><br>");
0056     str += QLatin1StringView("<b>") + i18n("Errors reported:") + QLatin1StringView("</b><br>");
0057     str += error.replace(QLatin1Char('\n'), QStringLiteral("<br>")) + QLatin1StringView("<br>");
0058     mTextEdit->setHtml(str);
0059 }
0060 
0061 void SieveScriptParsingErrorDialog::readConfig()
0062 {
0063     create(); // ensure a window is created
0064     windowHandle()->resize(QSize(800, 600));
0065     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveScriptParsingErrorDialogGroupName));
0066     KWindowConfig::restoreWindowSize(windowHandle(), group);
0067     resize(windowHandle()->size()); // workaround for QTBUG-40584
0068 }
0069 
0070 void SieveScriptParsingErrorDialog::writeConfig()
0071 {
0072     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(mySieveScriptParsingErrorDialogGroupName));
0073     KWindowConfig::saveWindowSize(windowHandle(), group);
0074     group.sync();
0075 }
0076 
0077 void SieveScriptParsingErrorDialog::slotSaveAs()
0078 {
0079     const QString filter = i18n("All Files (*)");
0080     PimCommon::Util::saveTextAs(mTextEdit->toPlainText(), filter, this, QUrl(), i18nc("@title:window", "Save Log To File"));
0081 }
0082 
0083 #include "moc_sievescriptparsingerrordialog.cpp"