Mailcommon

filterconverttosieveresultdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "filterconverttosieveresultdialog.h"
8 #include "filterconverttosievepurposemenuwidget.h"
9 #include <KPIMTextEdit/PlainTextEditor>
10 #include <KPIMTextEdit/PlainTextEditorWidget>
11 #include <PimCommon/PimUtil>
12 
13 #include <KLocalizedString>
14 #include <KSyntaxHighlighting/Definition>
15 #include <KSyntaxHighlighting/SyntaxHighlighter>
16 #include <KSyntaxHighlighting/Theme>
17 
18 #include <KConfigGroup>
19 #include <KSharedConfig>
20 #include <QDialogButtonBox>
21 #include <QPushButton>
22 #include <QVBoxLayout>
23 
24 using namespace MailCommon;
25 
26 FilterConvertToSieveResultDialog::FilterConvertToSieveResultDialog(QWidget *parent)
27  : QDialog(parent)
28  , mEditor(new KPIMTextEdit::PlainTextEditorWidget(this))
29 {
30  setWindowTitle(i18nc("@title:window", "Convert to Sieve Script"));
31  auto topLayout = new QVBoxLayout(this);
32  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
33  buttonBox->setObjectName(QStringLiteral("buttonbox"));
34  auto saveButton = new QPushButton(this);
35  buttonBox->addButton(saveButton, QDialogButtonBox::ActionRole);
36  connect(buttonBox, &QDialogButtonBox::rejected, this, &FilterConvertToSieveResultDialog::reject);
37  saveButton->setText(i18n("Save..."));
38  saveButton->setObjectName(QStringLiteral("savebutton"));
39  saveButton->setDefault(true);
40  setModal(true);
41  connect(saveButton, &QPushButton::clicked, this, &FilterConvertToSieveResultDialog::slotSave);
42 
43  mEditor->editor()->setSpellCheckingSupport(false);
44  mEditor->setObjectName(QStringLiteral("editor"));
45  auto syntaxHighlighter = new KSyntaxHighlighting::SyntaxHighlighter(mEditor->editor()->document());
46  syntaxHighlighter->setDefinition(mSyntaxRepo.definitionForName(QStringLiteral("Sieve")));
47  syntaxHighlighter->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
48  : mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
49  topLayout->addWidget(mEditor);
50  topLayout->addWidget(buttonBox);
51 
52  auto purposeMenu = new FilterconverttosievePurposeMenuWidget(this, this);
53  auto shareButton = new QPushButton(i18n("Share..."), this);
54  shareButton->setMenu(purposeMenu->menu());
55  shareButton->setIcon(QIcon::fromTheme(QStringLiteral("document-share")));
56  purposeMenu->setEditorWidget(mEditor->editor());
57  buttonBox->addButton(shareButton, QDialogButtonBox::ActionRole);
58 
59  readConfig();
60 }
61 
62 FilterConvertToSieveResultDialog::~FilterConvertToSieveResultDialog()
63 {
64  writeConfig();
65 }
66 
67 void FilterConvertToSieveResultDialog::slotSave()
68 {
69  const QString filter = i18n("Sieve Files (*.siv);;All Files (*)");
70  PimCommon::Util::saveTextAs(mEditor->editor()->toPlainText(), filter, this, QUrl(), i18nc("@title:window", "Convert to Script Sieve"));
71 }
72 
73 void FilterConvertToSieveResultDialog::setCode(const QString &code)
74 {
75  mEditor->editor()->setPlainText(code);
76 }
77 
78 static const char myFilterConvertToSieveResultDialogName[] = "FilterConvertToSieveResultDialog";
79 
80 void FilterConvertToSieveResultDialog::readConfig()
81 {
82  KConfigGroup group(KSharedConfig::openStateConfig(), myFilterConvertToSieveResultDialogName);
83 
84  const QSize size = group.readEntry("Size", QSize(500, 300));
85  if (size.isValid()) {
86  resize(size);
87  }
88 }
89 
90 void FilterConvertToSieveResultDialog::writeConfig()
91 {
92  KConfigGroup group(KSharedConfig::openStateConfig(), myFilterConvertToSieveResultDialogName);
93  group.writeEntry("Size", size());
94  group.sync();
95 }
bool isValid() const const
void clicked(bool checked)
void readConfig()
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString i18n(const char *text, const TYPE &arg...)
void setWindowTitle(const QString &)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.