serversievelistwidget.cpp 4.15 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2013-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
Laurent Montel's avatar
Laurent Montel committed
18 19 20
*/

#include "serversievelistwidget.h"
21
#include "serversievesettingsdialog.h"
Laurent Montel's avatar
Laurent Montel committed
22

Laurent Montel's avatar
Laurent Montel committed
23 24
#include <KLocalizedString>

Laurent Montel's avatar
Laurent Montel committed
25
#include <QListWidgetItem>
26
#include <QPointer>
Laurent Montel's avatar
Laurent Montel committed
27 28

ServerSieveListWidget::ServerSieveListWidget(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
29
    : QListWidget(parent)
Laurent Montel's avatar
Laurent Montel committed
30
{
31
    connect(this, &ServerSieveListWidget::itemDoubleClicked, this, &ServerSieveListWidget::modifyServerConfig);
Laurent Montel's avatar
Laurent Montel committed
32 33 34 35 36 37 38 39
}

ServerSieveListWidget::~ServerSieveListWidget()
{
}

void ServerSieveListWidget::readConfig()
{
Laurent Montel's avatar
Laurent Montel committed
40
    const QVector<SieveEditorUtil::SieveServerConfig> lstServer = SieveEditorUtil::readServerSieveConfig();
Laurent Montel's avatar
Laurent Montel committed
41
    for (const SieveEditorUtil::SieveServerConfig &conf : lstServer) {
Laurent Montel's avatar
Laurent Montel committed
42
        auto *item = new ServerSieveListWidgetItem(this);
43
        item->setServerConfig(conf);
Laurent Montel's avatar
Laurent Montel committed
44 45 46 47 48
    }
}

void ServerSieveListWidget::writeConfig()
{
Laurent Montel's avatar
Laurent Montel committed
49
    QVector<SieveEditorUtil::SieveServerConfig> lstServerConfig;
Laurent Montel's avatar
Laurent Montel committed
50
    for (int i = 0; i < count(); ++i) {
Laurent Montel's avatar
Laurent Montel committed
51
        auto *serverSieveItem = static_cast<ServerSieveListWidgetItem *>(item(i));
52
        if (serverSieveItem) {
Laurent Montel's avatar
Laurent Montel committed
53 54 55
            SieveEditorUtil::SieveServerConfig config = serverSieveItem->serverConfig();
            config.enabled = (serverSieveItem->checkState() == Qt::Checked);
            lstServerConfig.append(config);
56
        }
Laurent Montel's avatar
Laurent Montel committed
57
    }
58
    SieveEditorUtil::writeServerSieveConfig(lstServerConfig);
59
    SieveEditorUtil::deletePasswords(mNeedToRemovePasswordInWallet);
Laurent Montel's avatar
Laurent Montel committed
60
}
Laurent Montel's avatar
Laurent Montel committed
61

62 63
void ServerSieveListWidget::modifyServerConfig()
{
Laurent Montel's avatar
Laurent Montel committed
64
    QListWidgetItem *item = currentItem();
Laurent Montel's avatar
Laurent Montel committed
65
    if (!item) {
66
        return;
Laurent Montel's avatar
Laurent Montel committed
67
    }
68

Laurent Montel's avatar
Laurent Montel committed
69
    auto *serverSieveListItem = static_cast<ServerSieveListWidgetItem *>(item);
Laurent Montel's avatar
Laurent Montel committed
70

71
    QPointer<ServerSieveSettingsDialog> dlg = new ServerSieveSettingsDialog(this);
72
    dlg->setWindowTitle(i18nc("@title:window", "Modify Settings"));
Laurent Montel's avatar
Laurent Montel committed
73
    dlg->setServerSieveConfig(serverSieveListItem->serverConfig());
74 75 76 77 78 79
    if (dlg->exec()) {
        serverSieveListItem->setServerConfig(dlg->serverSieveConfig());
    }
    delete dlg;
}

80 81
void ServerSieveListWidget::deleteServerConfig(QListWidgetItem *item)
{
Laurent Montel's avatar
Laurent Montel committed
82
    auto *serverSieveListItem = static_cast<ServerSieveListWidgetItem *>(item);
83 84 85 86 87 88 89 90 91
    SieveEditorUtil::SieveServerConfig conf = serverSieveListItem->serverConfig();

    mNeedToRemovePasswordInWallet.append(SieveEditorUtil::sievePasswordIdentifier(conf.sieveSettings.userName, conf.sieveSettings.serverName));
    const QString imapIdentifier = SieveEditorUtil::imapPasswordIdentifier(conf.sieveImapAccountSettings.userName(), conf.sieveImapAccountSettings.serverName());
    if (!imapIdentifier.isEmpty()) {
        mNeedToRemovePasswordInWallet.append(imapIdentifier);
    }
}

92 93 94 95
void ServerSieveListWidget::addServerConfig()
{
    QPointer<ServerSieveSettingsDialog> dlg = new ServerSieveSettingsDialog(this);
    if (dlg->exec()) {
Laurent Montel's avatar
Laurent Montel committed
96
        auto *item = new ServerSieveListWidgetItem(this);
97 98 99 100 101
        item->setServerConfig(dlg->serverSieveConfig());
    }
    delete dlg;
}

Laurent Montel's avatar
Laurent Montel committed
102 103
ServerSieveListWidgetItem::ServerSieveListWidgetItem(QListWidget *parent)
    : QListWidgetItem(parent)
Laurent Montel's avatar
Laurent Montel committed
104 105 106 107 108 109
{
}

ServerSieveListWidgetItem::~ServerSieveListWidgetItem()
{
}
110 111 112 113 114 115 116 117

SieveEditorUtil::SieveServerConfig ServerSieveListWidgetItem::serverConfig() const
{
    return mServerConfig;
}

void ServerSieveListWidgetItem::setServerConfig(const SieveEditorUtil::SieveServerConfig &conf)
{
Laurent Montel's avatar
Laurent Montel committed
118
    setCheckState(conf.enabled ? Qt::Checked : Qt::Unchecked);
Laurent Montel's avatar
Laurent Montel committed
119
    setText(i18nc("server name (username)", "%1 (%2)", conf.sieveSettings.serverName, conf.sieveSettings.userName));
120 121
    mServerConfig = conf;
}