File indexing completed on 2024-11-10 04:50:05

0001 /*
0002   SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "filterconverttosieveresultdialog.h"
0008 #include "filterconverttosievepurposemenuwidget.h"
0009 #include <PimCommon/PimUtil>
0010 #include <PimCommon/PurposeMenuMessageWidget>
0011 #include <TextCustomEditor/PlainTextEditor>
0012 #include <TextCustomEditor/PlainTextEditorWidget>
0013 
0014 #include <KLocalizedString>
0015 #include <KSyntaxHighlighting/Definition>
0016 #include <KSyntaxHighlighting/SyntaxHighlighter>
0017 #include <KSyntaxHighlighting/Theme>
0018 
0019 #include <KConfigGroup>
0020 #include <KSharedConfig>
0021 #include <KWindowConfig>
0022 #include <QDialogButtonBox>
0023 #include <QPushButton>
0024 #include <QVBoxLayout>
0025 #include <QWindow>
0026 
0027 using namespace MailCommon;
0028 namespace
0029 {
0030 static const char myFilterConvertToSieveResultDialogName[] = "FilterConvertToSieveResultDialog";
0031 }
0032 FilterConvertToSieveResultDialog::FilterConvertToSieveResultDialog(QWidget *parent)
0033     : QDialog(parent)
0034     , mEditor(new TextCustomEditor::PlainTextEditorWidget(this))
0035     , mPurposeMenuMessageWidget(new PimCommon::PurposeMenuMessageWidget(this))
0036 {
0037     setWindowTitle(i18nc("@title:window", "Convert to Sieve Script"));
0038     auto topLayout = new QVBoxLayout(this);
0039     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
0040     buttonBox->setObjectName(QLatin1StringView("buttonbox"));
0041     auto saveButton = new QPushButton(this);
0042     buttonBox->addButton(saveButton, QDialogButtonBox::ActionRole);
0043     connect(buttonBox, &QDialogButtonBox::rejected, this, &FilterConvertToSieveResultDialog::reject);
0044     saveButton->setText(i18n("Save..."));
0045     saveButton->setIcon(QIcon::fromTheme("document-save"));
0046     saveButton->setObjectName(QLatin1StringView("savebutton"));
0047     saveButton->setDefault(true);
0048     setModal(true);
0049     connect(saveButton, &QPushButton::clicked, this, &FilterConvertToSieveResultDialog::slotSave);
0050 
0051     mEditor->editor()->setSpellCheckingSupport(false);
0052     mEditor->setObjectName(QLatin1StringView("editor"));
0053     auto syntaxHighlighter = new KSyntaxHighlighting::SyntaxHighlighter(mEditor->editor()->document());
0054     syntaxHighlighter->setDefinition(mSyntaxRepo.definitionForName(QStringLiteral("Sieve")));
0055     syntaxHighlighter->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
0056                                                                                     : mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
0057     topLayout->addWidget(mPurposeMenuMessageWidget);
0058     topLayout->addWidget(mEditor);
0059     topLayout->addWidget(buttonBox);
0060 
0061     auto purposeMenu = new FilterconverttosievePurposeMenuWidget(this, this);
0062     connect(purposeMenu, &FilterconverttosievePurposeMenuWidget::shareError, mPurposeMenuMessageWidget, &PimCommon::PurposeMenuMessageWidget::slotShareError);
0063     connect(purposeMenu,
0064             &FilterconverttosievePurposeMenuWidget::shareSuccess,
0065             mPurposeMenuMessageWidget,
0066             &PimCommon::PurposeMenuMessageWidget::slotShareSuccess);
0067     auto shareButton = new QPushButton(i18n("Share..."), this);
0068     shareButton->setMenu(purposeMenu->menu());
0069     shareButton->setIcon(QIcon::fromTheme(QStringLiteral("document-share")));
0070     purposeMenu->setEditorWidget(mEditor->editor());
0071     buttonBox->addButton(shareButton, QDialogButtonBox::ActionRole);
0072 
0073     readConfig();
0074 }
0075 
0076 FilterConvertToSieveResultDialog::~FilterConvertToSieveResultDialog()
0077 {
0078     writeConfig();
0079 }
0080 
0081 void FilterConvertToSieveResultDialog::slotSave()
0082 {
0083     const QString filter = i18n("Sieve Files (*.siv);;All Files (*)");
0084     PimCommon::Util::saveTextAs(mEditor->editor()->toPlainText(), filter, this, QUrl(), i18nc("@title:window", "Convert to Script Sieve"));
0085 }
0086 
0087 void FilterConvertToSieveResultDialog::setCode(const QString &code)
0088 {
0089     mEditor->editor()->setPlainText(code);
0090 }
0091 
0092 void FilterConvertToSieveResultDialog::readConfig()
0093 {
0094     create(); // ensure a window is created
0095     windowHandle()->resize(QSize(500, 300));
0096     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterConvertToSieveResultDialogName));
0097     KWindowConfig::restoreWindowSize(windowHandle(), group);
0098     resize(windowHandle()->size()); // workaround for QTBUG-40584
0099 }
0100 
0101 void FilterConvertToSieveResultDialog::writeConfig()
0102 {
0103     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterConvertToSieveResultDialogName));
0104     KWindowConfig::saveWindowSize(windowHandle(), group);
0105     group.sync();
0106 }
0107 
0108 #include "moc_filterconverttosieveresultdialog.cpp"