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

0001 /* SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0002  *
0003  * SPDX-License-Identifier: LGPL-2.0-or-later
0004  */
0005 
0006 #include "sieveeditorabstractwidget.h"
0007 #include <PimCommon/PimUtil>
0008 
0009 #include <KLocalizedString>
0010 #include <KMessageBox>
0011 #include <QFileDialog>
0012 
0013 #include <QPointer>
0014 #include <QTextStream>
0015 
0016 #include <cerrno>
0017 
0018 using namespace KSieveUi;
0019 SieveEditorAbstractWidget::SieveEditorAbstractWidget(QWidget *parent)
0020     : QWidget(parent)
0021 {
0022 }
0023 
0024 SieveEditorAbstractWidget::~SieveEditorAbstractWidget() = default;
0025 
0026 void SieveEditorAbstractWidget::saveAs(const QString &defaultName)
0027 {
0028     const QString filter = i18n("Sieve Files (*.siv);;All Files (*)");
0029     PimCommon::Util::saveTextAs(currentscript(), filter, this, QUrl::fromLocalFile(defaultName), i18nc("@title:window", "Save Script"));
0030 }
0031 
0032 KSieveCore::SieveImapAccountSettings SieveEditorAbstractWidget::sieveImapAccountSettings() const
0033 {
0034     return mSieveImapAccountSettings;
0035 }
0036 
0037 void SieveEditorAbstractWidget::setSieveImapAccountSettings(const KSieveCore::SieveImapAccountSettings &sieveImapAccountSettings)
0038 {
0039     mSieveImapAccountSettings = sieveImapAccountSettings;
0040 }
0041 
0042 QString SieveEditorAbstractWidget::currentscript()
0043 {
0044     return {};
0045 }
0046 
0047 void SieveEditorAbstractWidget::setImportScript(const QString &)
0048 {
0049 }
0050 
0051 void SieveEditorAbstractWidget::slotImport()
0052 {
0053     if (!currentscript().isEmpty()) {
0054         const int answer = KMessageBox::warningTwoActions(this,
0055                                                           i18n("You will overwrite script. Do you want to continue?"),
0056                                                           i18nc("@title:window", "Import Script"),
0057                                                           KStandardGuiItem::cont(),
0058                                                           KStandardGuiItem::cancel());
0059         if (answer == KMessageBox::ButtonCode::SecondaryAction) {
0060             return;
0061         }
0062     }
0063     const QString filter = i18n("*.siv;;sieve files (*.siv);;all files (*)");
0064     QPointer<QFileDialog> fdlg(new QFileDialog(this, i18nc("@title:window", "Import Script Sieve"), QString(), filter));
0065     fdlg->setFileMode(QFileDialog::ExistingFile);
0066     if (fdlg->exec() == QDialog::Accepted) {
0067         const QStringList fileNames = fdlg->selectedFiles();
0068         if (!loadFromFile(fileNames.at(0))) {
0069             KMessageBox::error(this,
0070                                i18n("Could not load the file %1:\n"
0071                                     "\"%2\" is the detailed error description.",
0072                                     fileNames.at(0),
0073                                     QString::fromLocal8Bit(strerror(errno))),
0074                                i18n("Sieve Editor Error"));
0075         }
0076     }
0077     delete fdlg;
0078 }
0079 
0080 bool SieveEditorAbstractWidget::loadFromFile(const QString &filename)
0081 {
0082     QFile file(filename);
0083     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
0084         return false;
0085     }
0086 
0087     QTextStream in(&file);
0088     QString line = in.readLine();
0089     QString scriptText;
0090     while (!line.isNull()) {
0091         if (scriptText.isEmpty()) {
0092             scriptText = line;
0093         } else {
0094             scriptText += QLatin1Char('\n') + line;
0095         }
0096         line = in.readLine();
0097     }
0098     setImportScript(scriptText);
0099     return true;
0100 }
0101 
0102 #include "moc_sieveeditorabstractwidget.cpp"