Libksieve

sieveeditorabstractwidget.cpp
1/* SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
2 *
3 * SPDX-License-Identifier: LGPL-2.0-or-later
4 */
5
6#include "sieveeditorabstractwidget.h"
7#include <PimCommon/PimUtil>
8
9#include <KLocalizedString>
10#include <KMessageBox>
11#include <QFileDialog>
12
13#include <QPointer>
14#include <QTextStream>
15
16#include <cerrno>
17
18using namespace KSieveUi;
19SieveEditorAbstractWidget::SieveEditorAbstractWidget(QWidget *parent)
20 : QWidget(parent)
21{
22}
23
24SieveEditorAbstractWidget::~SieveEditorAbstractWidget() = default;
25
26void SieveEditorAbstractWidget::saveAs(const QString &defaultName)
27{
28 const QString filter = i18n("Sieve Files (*.siv);;All Files (*)");
29 PimCommon::Util::saveTextAs(currentscript(), filter, this, QUrl::fromLocalFile(defaultName), i18nc("@title:window", "Save Script"));
30}
31
32KSieveCore::SieveImapAccountSettings SieveEditorAbstractWidget::sieveImapAccountSettings() const
33{
34 return mSieveImapAccountSettings;
35}
36
37void SieveEditorAbstractWidget::setSieveImapAccountSettings(const KSieveCore::SieveImapAccountSettings &sieveImapAccountSettings)
38{
39 mSieveImapAccountSettings = sieveImapAccountSettings;
40}
41
42QString SieveEditorAbstractWidget::currentscript()
43{
44 return {};
45}
46
47void SieveEditorAbstractWidget::setImportScript(const QString &)
48{
49}
50
51void SieveEditorAbstractWidget::slotImport()
52{
53 if (!currentscript().isEmpty()) {
54 const int answer = KMessageBox::warningTwoActions(this,
55 i18n("You will overwrite script. Do you want to continue?"),
56 i18nc("@title:window", "Import Script"),
59 if (answer == KMessageBox::ButtonCode::SecondaryAction) {
60 return;
61 }
62 }
63 const QString filter = i18n("*.siv;;sieve files (*.siv);;all files (*)");
64 QPointer<QFileDialog> fdlg(new QFileDialog(this, i18nc("@title:window", "Import Script Sieve"), QString(), filter));
65 fdlg->setFileMode(QFileDialog::ExistingFile);
66 if (fdlg->exec() == QDialog::Accepted) {
67 const QStringList fileNames = fdlg->selectedFiles();
68 if (!loadFromFile(fileNames.at(0))) {
70 i18n("Could not load the file %1:\n"
71 "\"%2\" is the detailed error description.",
72 fileNames.at(0),
73 QString::fromLocal8Bit(strerror(errno))),
74 i18nc("@title:window", "Sieve Editor Error"));
75 }
76 }
77 delete fdlg;
78}
79
80bool SieveEditorAbstractWidget::loadFromFile(const QString &filename)
81{
82 QFile file(filename);
83 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
84 return false;
85 }
86
87 QTextStream in(&file);
88 QString line = in.readLine();
89 QString scriptText;
90 while (!line.isNull()) {
91 if (scriptText.isEmpty()) {
92 scriptText = line;
93 } else {
94 scriptText += QLatin1Char('\n') + line;
95 }
96 line = in.readLine();
97 }
98 setImportScript(scriptText);
99 return true;
100}
101
102#include "moc_sieveeditorabstractwidget.cpp"
The SieveImapAccountSettings class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
ButtonCode warningTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem cont()
KGuiItem cancel()
KEDUVOCDOCUMENT_EXPORT QStringList fileNames(const QString &language=QString())
const_reference at(qsizetype i) const const
QString fromLocal8Bit(QByteArrayView str)
bool isEmpty() const const
bool isNull() const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QUrl fromLocalFile(const QString &localFile)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:56:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.