Libksieve

sieveeditorabstractwidget.cpp
1 /* SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 
18 using namespace KSieveUi;
19 SieveEditorAbstractWidget::SieveEditorAbstractWidget(QWidget *parent)
20  : QWidget(parent)
21 {
22 }
23 
24 SieveEditorAbstractWidget::~SieveEditorAbstractWidget()
25 {
26 }
27 
28 void SieveEditorAbstractWidget::saveAs(const QString &defaultName)
29 {
30  const QString filter = i18n("Sieve Files (*.siv);;All Files (*)");
31  PimCommon::Util::saveTextAs(currentscript(), filter, this, QUrl::fromLocalFile(defaultName), i18nc("@title:window", "Save Script"));
32 }
33 
34 KSieveUi::SieveImapAccountSettings SieveEditorAbstractWidget::sieveImapAccountSettings() const
35 {
36  return mSieveImapAccountSettings;
37 }
38 
39 void SieveEditorAbstractWidget::setSieveImapAccountSettings(const SieveImapAccountSettings &sieveImapAccountSettings)
40 {
41  mSieveImapAccountSettings = sieveImapAccountSettings;
42 }
43 
44 QString SieveEditorAbstractWidget::currentscript()
45 {
46  return QString();
47 }
48 
49 void SieveEditorAbstractWidget::setImportScript(const QString &)
50 {
51 }
52 
53 void SieveEditorAbstractWidget::slotImport()
54 {
55  if (!currentscript().isEmpty()) {
56  if (KMessageBox::warningYesNo(this, i18n("You will overwrite script. Do you want to continue?"), i18n("Import Script")) == KMessageBox::No) {
57  return;
58  }
59  }
60  const QString filter = i18n("*.siv;;sieve files (*.siv);;all files (*)");
61  QPointer<QFileDialog> fdlg(new QFileDialog(this, i18nc("@title:window", "Import Script Sieve"), QString(), filter));
62  fdlg->setFileMode(QFileDialog::ExistingFile);
63  if (fdlg->exec() == QDialog::Accepted) {
64  const QStringList fileNames = fdlg->selectedFiles();
65  if (!loadFromFile(fileNames.at(0))) {
66  KMessageBox::error(this,
67  i18n("Could not load the file %1:\n"
68  "\"%2\" is the detailed error description.",
69  fileNames.at(0),
70  QString::fromLocal8Bit(strerror(errno))),
71  i18n("Sieve Editor Error"));
72  }
73  }
74  delete fdlg;
75 }
76 
77 bool SieveEditorAbstractWidget::loadFromFile(const QString &filename)
78 {
79  QFile file(filename);
80  if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
81  return false;
82  }
83 
84  QTextStream in(&file);
85  QString line = in.readLine();
86  QString scriptText;
87  while (!line.isNull()) {
88  if (scriptText.isEmpty()) {
89  scriptText = line;
90  } else {
91  scriptText += QLatin1Char('\n') + line;
92  }
93  line = in.readLine();
94  }
95  setImportScript(scriptText);
96  return true;
97 }
const T & at(int i) const const
bool isNull() const const
QString fromLocal8Bit(const char *str, int size)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QString i18n(const char *text, const TYPE &arg...)
The SieveImapAccountSettings class.
QUrl fromLocalFile(const QString &localFile)
ButtonCode warningYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Options(Notify|Dangerous))
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.