Mailcommon

filterconverttosieveresultdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "filterconverttosieveresultdialog.h"
8#include "filterconverttosievepurposemenuwidget.h"
9#include <PimCommon/PimUtil>
10#include <PimCommon/PurposeMenuMessageWidget>
11#include <TextCustomEditor/PlainTextEditor>
12#include <TextCustomEditor/PlainTextEditorWidget>
13
14#include <KLocalizedString>
15#include <KSyntaxHighlighting/Definition>
16#include <KSyntaxHighlighting/SyntaxHighlighter>
17#include <KSyntaxHighlighting/Theme>
18
19#include <KConfigGroup>
20#include <KSharedConfig>
21#include <KWindowConfig>
22#include <QDialogButtonBox>
23#include <QPushButton>
24#include <QVBoxLayout>
25#include <QWindow>
26
27using namespace MailCommon;
28namespace
29{
30static const char myFilterConvertToSieveResultDialogName[] = "FilterConvertToSieveResultDialog";
31}
32FilterConvertToSieveResultDialog::FilterConvertToSieveResultDialog(QWidget *parent)
33 : QDialog(parent)
34 , mEditor(new TextCustomEditor::PlainTextEditorWidget(this))
35 , mPurposeMenuMessageWidget(new PimCommon::PurposeMenuMessageWidget(this))
36{
37 setWindowTitle(i18nc("@title:window", "Convert to Sieve Script"));
38 auto topLayout = new QVBoxLayout(this);
39 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
40 buttonBox->setObjectName(QLatin1StringView("buttonbox"));
41 auto saveButton = new QPushButton(this);
42 buttonBox->addButton(saveButton, QDialogButtonBox::ActionRole);
43 connect(buttonBox, &QDialogButtonBox::rejected, this, &FilterConvertToSieveResultDialog::reject);
44 saveButton->setText(i18n("Save…"));
45 saveButton->setIcon(QIcon::fromTheme("document-save"));
46 saveButton->setObjectName(QLatin1StringView("savebutton"));
47 saveButton->setDefault(true);
48 setModal(true);
49 connect(saveButton, &QPushButton::clicked, this, &FilterConvertToSieveResultDialog::slotSave);
50
51 mEditor->editor()->setSpellCheckingSupport(false);
52 mEditor->setObjectName(QLatin1StringView("editor"));
53 auto syntaxHighlighter = new KSyntaxHighlighting::SyntaxHighlighter(mEditor->editor()->document());
54 syntaxHighlighter->setDefinition(mSyntaxRepo.definitionForName(QStringLiteral("Sieve")));
55 syntaxHighlighter->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
56 : mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
57 topLayout->addWidget(mPurposeMenuMessageWidget);
58 topLayout->addWidget(mEditor);
59 topLayout->addWidget(buttonBox);
60
61 auto purposeMenu = new FilterconverttosievePurposeMenuWidget(this, this);
62 connect(purposeMenu, &FilterconverttosievePurposeMenuWidget::shareError, mPurposeMenuMessageWidget, &PimCommon::PurposeMenuMessageWidget::slotShareError);
63 connect(purposeMenu,
64 &FilterconverttosievePurposeMenuWidget::shareSuccess,
65 mPurposeMenuMessageWidget,
66 &PimCommon::PurposeMenuMessageWidget::slotShareSuccess);
67 auto shareButton = new QPushButton(i18nc("@action:button", "Share…"), this);
68 shareButton->setMenu(purposeMenu->menu());
69 shareButton->setIcon(QIcon::fromTheme(QStringLiteral("document-share")));
70 purposeMenu->setEditorWidget(mEditor->editor());
71 buttonBox->addButton(shareButton, QDialogButtonBox::ActionRole);
72
73 readConfig();
74}
75
76FilterConvertToSieveResultDialog::~FilterConvertToSieveResultDialog()
77{
78 writeConfig();
79}
80
81void FilterConvertToSieveResultDialog::slotSave()
82{
83 const QString filter = i18n("Sieve Files (*.siv);;All Files (*)");
84 PimCommon::Util::saveTextAs(mEditor->editor()->toPlainText(), filter, this, QUrl(), i18nc("@title:window", "Convert to Script Sieve"));
85}
86
87void FilterConvertToSieveResultDialog::setCode(const QString &code)
88{
89 mEditor->editor()->setPlainText(code);
90}
91
92void FilterConvertToSieveResultDialog::readConfig()
93{
94 create(); // ensure a window is created
95 windowHandle()->resize(QSize(500, 300));
96 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterConvertToSieveResultDialogName));
98 resize(windowHandle()->size()); // workaround for QTBUG-40584
99}
100
101void FilterConvertToSieveResultDialog::writeConfig()
102{
103 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myFilterConvertToSieveResultDialogName));
105 group.sync();
106}
107
108#include "moc_filterconverttosieveresultdialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
The filter dialog.
void clicked(bool checked)
QIcon fromTheme(const QString &name)
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.