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