File indexing completed on 2024-12-22 05:07:42
0001 /* 0002 SPDX-FileCopyrightText: 2010-2023 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 // This code was taken from kmail-account-wizard 0008 0009 #include "restoreldapsettingsjob.h" 0010 0011 #include <KConfig> 0012 #include <KLDAP/LdapClientSearchConfig> 0013 #include <KLDAP/LdapClientSearchConfigReadConfigJob> 0014 #include <KLDAP/LdapClientSearchConfigWriteConfigJob> 0015 0016 #include <QDebug> 0017 0018 RestoreLdapSettingsJob::RestoreLdapSettingsJob(QObject *parent) 0019 : QObject(parent) 0020 { 0021 } 0022 0023 RestoreLdapSettingsJob::~RestoreLdapSettingsJob() = default; 0024 0025 void RestoreLdapSettingsJob::start() 0026 { 0027 if (!canStart()) { 0028 deleteLater(); 0029 qWarning() << "Impossible to start RestoreLdapSettingsJob"; 0030 Q_EMIT restoreDone(); 0031 return; 0032 } 0033 restore(); 0034 } 0035 0036 void RestoreLdapSettingsJob::slotConfigSelectedHostLoaded(const KLDAP::LdapServer &server) 0037 { 0038 mSelHosts.append(server); 0039 mCurrentIndex++; 0040 loadNextSelectHostSettings(); 0041 } 0042 0043 void RestoreLdapSettingsJob::loadNextSelectHostSettings() 0044 { 0045 if (mCurrentIndex < mNumSelHosts) { 0046 if (mCurrentIndex != mEntry) { 0047 auto job = new KLDAP::LdapClientSearchConfigReadConfigJob(this); 0048 connect(job, &KLDAP::LdapClientSearchConfigReadConfigJob::configLoaded, this, &RestoreLdapSettingsJob::slotConfigSelectedHostLoaded); 0049 job->setActive(true); 0050 job->setConfig(mCurrentGroup); 0051 job->setServerIndex(mCurrentIndex); 0052 job->start(); 0053 } else { 0054 mCurrentIndex++; 0055 loadNextSelectHostSettings(); 0056 } 0057 } else { 0058 // Reset index; 0059 mCurrentIndex = 0; 0060 loadNextHostSettings(); 0061 } 0062 } 0063 0064 void RestoreLdapSettingsJob::slotConfigHostLoaded(const KLDAP::LdapServer &server) 0065 { 0066 mHosts.append(server); 0067 mCurrentIndex++; 0068 loadNextHostSettings(); 0069 } 0070 0071 void RestoreLdapSettingsJob::loadNextHostSettings() 0072 { 0073 if (mCurrentIndex < mNumHosts) { 0074 auto job = new KLDAP::LdapClientSearchConfigReadConfigJob(this); 0075 connect(job, &KLDAP::LdapClientSearchConfigReadConfigJob::configLoaded, this, &RestoreLdapSettingsJob::slotConfigHostLoaded); 0076 job->setActive(false); 0077 job->setConfig(mCurrentGroup); 0078 job->setServerIndex(mCurrentIndex); 0079 job->start(); 0080 } else { 0081 saveLdapSettings(); 0082 } 0083 } 0084 0085 void RestoreLdapSettingsJob::restore() 0086 { 0087 if (mEntry >= 0) { 0088 mCurrentGroup = mConfig->group(QStringLiteral("LDAP")); 0089 mNumSelHosts = mCurrentGroup.readEntry(QStringLiteral("NumSelectedHosts"), 0); 0090 mNumHosts = mCurrentGroup.readEntry(QStringLiteral("NumHosts"), 0); 0091 loadNextSelectHostSettings(); 0092 } else { 0093 restoreSettingsDone(); 0094 } 0095 } 0096 0097 void RestoreLdapSettingsJob::restoreSettingsDone() 0098 { 0099 Q_EMIT restoreDone(); 0100 deleteLater(); 0101 } 0102 0103 void RestoreLdapSettingsJob::saveNextSelectHostSettings() 0104 { 0105 if (mCurrentIndex < mNumSelHosts - 1) { 0106 auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob(this); 0107 connect(job, &KLDAP::LdapClientSearchConfigWriteConfigJob::configSaved, this, &RestoreLdapSettingsJob::saveNextSelectHostSettings); 0108 job->setActive(true); 0109 job->setConfig(mCurrentGroup); 0110 job->setServer(mSelHosts.at(mCurrentIndex)); 0111 job->setServerIndex(mCurrentIndex); 0112 job->start(); 0113 } else { 0114 mCurrentIndex = 0; 0115 saveNextHostSettings(); 0116 } 0117 } 0118 0119 void RestoreLdapSettingsJob::saveNextHostSettings() 0120 { 0121 if (mCurrentIndex < mNumHosts) { 0122 auto job = new KLDAP::LdapClientSearchConfigWriteConfigJob(this); 0123 connect(job, &KLDAP::LdapClientSearchConfigWriteConfigJob::configSaved, this, &RestoreLdapSettingsJob::saveNextHostSettings); 0124 job->setActive(false); 0125 job->setConfig(mCurrentGroup); 0126 job->setServer(mHosts.at(mCurrentIndex)); 0127 job->setServerIndex(mCurrentIndex); 0128 job->start(); 0129 } else { 0130 mCurrentGroup.writeEntry(QStringLiteral("NumSelectedHosts"), mNumSelHosts - 1); 0131 mCurrentGroup.writeEntry(QStringLiteral("NumHosts"), mNumHosts); 0132 mConfig->sync(); 0133 restoreSettingsDone(); 0134 } 0135 } 0136 0137 void RestoreLdapSettingsJob::saveLdapSettings() 0138 { 0139 mConfig->deleteGroup(QStringLiteral("LDAP")); 0140 mCurrentGroup = KConfigGroup(mConfig, QStringLiteral("LDAP")); 0141 0142 mCurrentIndex = 0; 0143 saveNextSelectHostSettings(); 0144 } 0145 0146 int RestoreLdapSettingsJob::entry() const 0147 { 0148 return mEntry; 0149 } 0150 0151 void RestoreLdapSettingsJob::setEntry(int entry) 0152 { 0153 mEntry = entry; 0154 } 0155 0156 KConfig *RestoreLdapSettingsJob::config() const 0157 { 0158 return mConfig; 0159 } 0160 0161 void RestoreLdapSettingsJob::setConfig(KConfig *config) 0162 { 0163 mConfig = config; 0164 } 0165 0166 bool RestoreLdapSettingsJob::canStart() const 0167 { 0168 return (mConfig != nullptr) && (mEntry != -1); 0169 }