searchserverwithvacationsupportjob.cpp 3.04 KB
Newer Older
1
2
3
4
5
6
7
/*
  SPDX-FileCopyrightText: 2020 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: GPL-2.0-only
*/

#include "searchserverwithvacationsupportjob.h"
8
9
#include "util/findaccountinfojob.h"

10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace KSieveUi;

SearchServerWithVacationSupportJob::SearchServerWithVacationSupportJob(QObject *parent)
    : QObject(parent)
{

}

SearchServerWithVacationSupportJob::~SearchServerWithVacationSupportJob()
{

}

23
24
25
26
27
28
void SearchServerWithVacationSupportJob::sendAccountList()
{
    Q_EMIT searchServerWithVacationSupportFinished(mAccountList);
    deleteLater();
}

29
30
void SearchServerWithVacationSupportJob::start()
{
31
32
33
34
35
36
37
38
39
    if (!canStart()) {
        sendAccountList();
        return;
    }
    const QVector<KSieveUi::SieveImapInstance> instances = KSieveUi::Util::sieveImapInstances();
    for (const KSieveUi::SieveImapInstance &instance : instances) {
        if (instance.status() == KSieveUi::SieveImapInstance::Broken) {
            continue;
        }
Laurent Montel's avatar
Laurent Montel committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        mServerSieveInfos.insert(instance.name(), instance.identifier());
    }
    searchNextInfo();
}

void SearchServerWithVacationSupportJob::searchNextInfo()
{
    mSieveServerMapIterator = mServerSieveInfos.constBegin();
    if (mSieveServerMapIterator != mServerSieveInfos.constEnd()) {
        slotSearchSieveScript(mSieveServerMapIterator.key(), mSieveServerMapIterator.value());
    } else {
        sendAccountList();
    }
}

void SearchServerWithVacationSupportJob::slotSearchSieveScript(const QString &name, const QString &identifier)
{
       FindAccountInfoJob *job = new FindAccountInfoJob(this);
       connect(job, &FindAccountInfoJob::findAccountInfoFinished, this, &SearchServerWithVacationSupportJob::slotFindAccountInfoFinished);
       job->setIdentifier(identifier);
       job->setProperty("serverName", name);
       job->setProvider(mPasswordProvider);
       job->start();
}

void SearchServerWithVacationSupportJob::slotFindAccountInfoFinished(const KSieveUi::Util::AccountInfo &info)
{
    const QUrl url = info.sieveUrl;
    if (!url.isEmpty()) {
        const QString serverName = sender()->property("serverName").toString();
        mAccountList.insert(serverName, info);
    }
    searchNextServerSieve();
}

void SearchServerWithVacationSupportJob::searchNextServerSieve()
{
    ++mSieveServerMapIterator;
    if(mSieveServerMapIterator != mServerSieveInfos.constEnd()) {
        slotSearchSieveScript(mSieveServerMapIterator.key(), mSieveServerMapIterator.value());
    } else {
        sendAccountList();
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
    }
}

bool SearchServerWithVacationSupportJob::canStart() const
{
    return mPasswordProvider && !mIdentifier.isEmpty();
}

QString SearchServerWithVacationSupportJob::identifier() const
{
    return mIdentifier;
}

void SearchServerWithVacationSupportJob::setIdentifier(const QString &newIdentifier)
{
    mIdentifier = newIdentifier;
}

SieveImapPasswordProvider *SearchServerWithVacationSupportJob::passwordProvider() const
{
    return mPasswordProvider;
}

void SearchServerWithVacationSupportJob::setPasswordProvider(SieveImapPasswordProvider *newProvider)
{
    mPasswordProvider = newProvider;
108
}