sieveeditorabstractwidget.cpp 3.55 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/* Copyright (C) 2013-2019 Laurent Montel <montel@kde.org>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "sieveeditorabstractwidget.h"
Laurent Montel's avatar
Laurent Montel committed
20
#include <PimCommon/PimUtil>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include <KLocalizedString>
#include <KMessageBox>
#include <QFileDialog>

#include <QTextStream>
#include <QPointer>

#include <errno.h>

using namespace KSieveUi;
SieveEditorAbstractWidget::SieveEditorAbstractWidget(QWidget *parent)
    : QWidget(parent)
{
}

SieveEditorAbstractWidget::~SieveEditorAbstractWidget()
{
}

void SieveEditorAbstractWidget::saveAs(const QString &defaultName)
{
    const QString filter = i18n("Sieve Files (*.siv);;All Files (*)");
    PimCommon::Util::saveTextAs(currentscript(), filter, this, QUrl::fromLocalFile(defaultName),
                                i18nc("@title:window", "Save Script"));
}

48
49
50
51
52
53
54
55
56
57
KSieveUi::SieveImapAccountSettings SieveEditorAbstractWidget::sieveImapAccountSettings() const
{
    return mSieveImapAccountSettings;
}

void SieveEditorAbstractWidget::setSieveImapAccountSettings(const SieveImapAccountSettings &sieveImapAccountSettings)
{
    mSieveImapAccountSettings = sieveImapAccountSettings;
}

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
QString SieveEditorAbstractWidget::currentscript()
{
    return QString();
}

void SieveEditorAbstractWidget::setImportScript(const QString &)
{
}

void SieveEditorAbstractWidget::slotImport()
{
    if (!currentscript().isEmpty()) {
        if (KMessageBox::warningYesNo(this, i18n("You will overwrite script. Do you want to continue?"), i18n("Import Script")) == KMessageBox::No) {
            return;
        }
    }
    const QString filter = i18n("*.siv;;sieve files (*.siv);;all files (*)");
75
    QPointer<QFileDialog> fdlg(new QFileDialog(this, i18nc("@title:window", "Import Script Sieve"), QString(), filter));
76
    fdlg->setFileMode(QFileDialog::ExistingFile);
Laurent Montel's avatar
Laurent Montel committed
77
    if (fdlg->exec() == QDialog::Accepted) {
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        const QStringList fileNames = fdlg->selectedFiles();
        if (!loadFromFile(fileNames.at(0))) {
            KMessageBox::error(this,
                               i18n("Could not load the file %1:\n"
                                    "\"%2\" is the detailed error description.",
                                    fileNames.at(0),
                                    QString::fromLocal8Bit(strerror(errno))),
                               i18n("Sieve Editor Error"));
        }
    }
    delete fdlg;
}

bool SieveEditorAbstractWidget::loadFromFile(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return false;
    }

    QTextStream in(&file);
    QString line = in.readLine();
    QString scriptText;
    while (!line.isNull()) {
        if (scriptText.isEmpty()) {
            scriptText = line;
        } else {
            scriptText += QLatin1Char('\n') + line;
        }
        line = in.readLine();
    }
    setImportScript(scriptText);
    return true;
}