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

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "restoreldapsettingsjob.h"
#include <KLDAP/LdapClientSearchConfigWriteConfigJob>
Laurent Montel's avatar
Laurent Montel committed
9
#include <KLDAP/LdapClientSearchConfigReadConfigJob>
Laurent Montel's avatar
Laurent Montel committed
10
#include <KLDAP/LdapClientSearchConfig>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <KConfigGroup>
#include <KConfig>
#include <QDebug>

RestoreLdapSettingsJob::RestoreLdapSettingsJob(QObject *parent)
    : QObject(parent)
{

}

RestoreLdapSettingsJob::~RestoreLdapSettingsJob()
{

}

void RestoreLdapSettingsJob::start()
{
    if (!canStart()) {
        deleteLater();
        qWarning() << "Impossible to start RestoreLdapSettingsJob";
        Q_EMIT restoreDone();
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
34
35
36
    restore();
}

Laurent Montel's avatar
Laurent Montel committed
37
38
39
40
41
42
43
void RestoreLdapSettingsJob::slotConfigSelectedHostLoaded(const KLDAP::LdapServer &server)
{
    mSelHosts.append(server);
    mCurrentIndex++;
    loadNextSelectHostSettings();
}

44
void RestoreLdapSettingsJob::loadNextSelectHostSettings()
Laurent Montel's avatar
Laurent Montel committed
45
{
46
47
    if (mCurrentIndex < mNumSelHosts) {
        if (mCurrentIndex != mEntry) {
Laurent Montel's avatar
Laurent Montel committed
48
49
50
51
52
53
            auto job = new KLDAP::LdapClientSearchConfigReadConfigJob(this);
            connect(job, &KLDAP::LdapClientSearchConfigReadConfigJob::configLoaded, this, &RestoreLdapSettingsJob::slotConfigSelectedHostLoaded);
            job->setActive(true);
            job->setConfig(mCurrentGroup);
            job->setServerIndex(mCurrentIndex);
            job->start();
54
55
56
        } else {
            mCurrentIndex++;
            loadNextSelectHostSettings();
Laurent Montel's avatar
Laurent Montel committed
57
        }
58
59
60
61
62
63
64
    } else {
        //Reset index;
        mCurrentIndex = 0;
        loadNextHostSettings();
    }
}

Laurent Montel's avatar
Laurent Montel committed
65
66
67
68
69
70
71
void RestoreLdapSettingsJob::slotConfigHostLoaded(const KLDAP::LdapServer &server)
{
    mHosts.append(server);
    mCurrentIndex++;
    loadNextHostSettings();
}

72
73
74
void RestoreLdapSettingsJob::loadNextHostSettings()
{
    if (mCurrentIndex < mNumHosts) {
Laurent Montel's avatar
Laurent Montel committed
75
76
77
78
79
80
        auto job = new KLDAP::LdapClientSearchConfigReadConfigJob(this);
        connect(job, &KLDAP::LdapClientSearchConfigReadConfigJob::configLoaded, this, &RestoreLdapSettingsJob::slotConfigHostLoaded);
        job->setActive(false);
        job->setConfig(mCurrentGroup);
        job->setServerIndex(mCurrentIndex);
        job->start();
81
82
83
84
    } else {
        saveLdapSettings();
    }
}
Laurent Montel's avatar
Laurent Montel committed
85

86
87
88
89
90
91
92
void RestoreLdapSettingsJob::restore()
{
    if (mEntry >= 0) {
        mCurrentGroup = mConfig->group(QStringLiteral("LDAP"));
        mNumSelHosts = mCurrentGroup.readEntry(QStringLiteral("NumSelectedHosts"), 0);
        mNumHosts = mCurrentGroup.readEntry(QStringLiteral("NumHosts"), 0);
        loadNextSelectHostSettings();
93
    } else {
Laurent Montel's avatar
Laurent Montel committed
94
        restoreSettingsDone();
95
96
    }
}
Laurent Montel's avatar
Laurent Montel committed
97

Laurent Montel's avatar
Laurent Montel committed
98
99
100
101
102
103
void RestoreLdapSettingsJob::restoreSettingsDone()
{
    Q_EMIT restoreDone();
    deleteLater();
}

104
void RestoreLdapSettingsJob::saveLdapSettings()
105
106
{
    mConfig->deleteGroup(QStringLiteral("LDAP"));
107
    mCurrentGroup = KConfigGroup(mConfig, QStringLiteral("LDAP"));
108

109
110
    //Move async
    for (int i = 0; i < mNumSelHosts - 1; ++i) {
111
112
        auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob;
        job->setActive(true);
113
        job->setConfig(mCurrentGroup);
114
115
116
117
        job->setServer(mSelHosts.at(i));
        job->setServerIndex(i);
        job->start();
    }
Laurent Montel's avatar
Laurent Montel committed
118

119
    for (int i = 0; i < mNumHosts; ++i) {
120
121
        auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob;
        job->setActive(false);
122
        job->setConfig(mCurrentGroup);
123
124
125
        job->setServer(mHosts.at(i));
        job->setServerIndex(i);
        job->start();
Laurent Montel's avatar
Laurent Montel committed
126
    }
127

128
129
    mCurrentGroup.writeEntry(QStringLiteral("NumSelectedHosts"), mNumSelHosts - 1);
    mCurrentGroup.writeEntry(QStringLiteral("NumHosts"), mNumHosts);
130
    mConfig->sync();
Laurent Montel's avatar
Laurent Montel committed
131
    restoreSettingsDone();
132
133
}

Laurent Montel's avatar
Laurent Montel committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
KLDAP::LdapClientSearchConfig *RestoreLdapSettingsJob::clientSearchConfig() const
{
    return mClientSearchConfig;
}

void RestoreLdapSettingsJob::setClientSearchConfig(KLDAP::LdapClientSearchConfig *clientSearchConfig)
{
    mClientSearchConfig = clientSearchConfig;
}

int RestoreLdapSettingsJob::entry() const
{
    return mEntry;
}

void RestoreLdapSettingsJob::setEntry(int entry)
{
    mEntry = entry;
}


155
156
157
158
159
160
161
162
163
164
165
166
KConfig *RestoreLdapSettingsJob::config() const
{
    return mConfig;
}

void RestoreLdapSettingsJob::setConfig(KConfig *config)
{
    mConfig = config;
}

bool RestoreLdapSettingsJob::canStart() const
{
Laurent Montel's avatar
Laurent Montel committed
167
    return (mConfig != nullptr) && (mEntry != -1) && mClientSearchConfig;
168
}