File indexing completed on 2024-12-22 04:52:48

0001 /*
0002    SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "importwizardutil.h"
0008 #include "importwizardsavepasswordjob.h"
0009 #include "libimportwizard_debug.h"
0010 #include <Akonadi/Tag>
0011 #include <Akonadi/TagAttribute>
0012 #include <Akonadi/TagCreateJob>
0013 #include <KConfigGroup>
0014 #include <KSharedConfig>
0015 
0016 void ImportWizardUtil::mergeLdap(const ldapStruct &ldap)
0017 {
0018     KSharedConfigPtr ldapConfig = KSharedConfig::openConfig(QStringLiteral("kabldaprc"));
0019     int numberOfLdapSelected = 0;
0020     KConfigGroup grp;
0021     if (ldapConfig->hasGroup(QStringLiteral("LDAP"))) {
0022         grp = ldapConfig->group(QStringLiteral("LDAP"));
0023         numberOfLdapSelected = grp.readEntry(QStringLiteral("NumSelectedHosts"), 0);
0024         grp.writeEntry(QStringLiteral("NumSelectedHosts"), (numberOfLdapSelected + 1));
0025     } else {
0026         grp = ldapConfig->group(QStringLiteral("LDAP"));
0027         grp.writeEntry(QStringLiteral("NumSelectedHosts"), 1);
0028 
0029         KConfigGroup ldapSeach = ldapConfig->group(QStringLiteral("LDAPSearch"));
0030         ldapSeach.writeEntry(QStringLiteral("SearchType"), 0);
0031     }
0032     const int port = ldap.port;
0033     if (port != -1) {
0034         grp.writeEntry(QStringLiteral("SelectedPort%1").arg(numberOfLdapSelected), port);
0035     }
0036     grp.writeEntry(QStringLiteral("SelectedHost%1").arg(numberOfLdapSelected), ldap.ldapUrl.host());
0037     if (ldap.useSSL) {
0038         grp.writeEntry(QStringLiteral("SelectedSecurity%1").arg(numberOfLdapSelected), QStringLiteral("SSL"));
0039     } else if (ldap.useTLS) {
0040         grp.writeEntry(QStringLiteral("SelectedSecurity%1").arg(numberOfLdapSelected), QStringLiteral("TLS"));
0041     } else {
0042         grp.writeEntry(QStringLiteral("SelectedSecurity%1").arg(numberOfLdapSelected), QStringLiteral("None"));
0043     }
0044 
0045     if (ldap.saslMech == QLatin1StringView("GSSAPI")) {
0046         grp.writeEntry(QStringLiteral("SelectedMech%1").arg(numberOfLdapSelected), QStringLiteral("GSSAPI"));
0047         grp.writeEntry(QStringLiteral("SelectedAuth%1").arg(numberOfLdapSelected), QStringLiteral("SASL"));
0048     } else if (ldap.saslMech.isEmpty()) {
0049         grp.writeEntry(QStringLiteral("SelectedMech%1").arg(numberOfLdapSelected), QStringLiteral("PLAIN"));
0050         grp.writeEntry(QStringLiteral("SelectedAuth%1").arg(numberOfLdapSelected), QStringLiteral("Simple"));
0051     } else {
0052         qCDebug(LIBIMPORTWIZARD_LOG) << " Mech SASL undefined" << ldap.saslMech;
0053     }
0054     grp.writeEntry(QStringLiteral("SelectedVersion%1").arg(numberOfLdapSelected), QString::number(3));
0055     grp.writeEntry(QStringLiteral("SelectedBind%1").arg(numberOfLdapSelected), ldap.dn);
0056     // TODO: Verify selectedbase
0057     grp.writeEntry(QStringLiteral("SelectedBase%1").arg(numberOfLdapSelected), ldap.ldapUrl.path());
0058     if (ldap.timeout != -1) {
0059         grp.writeEntry(QStringLiteral("SelectedTimeLimit%1").arg(numberOfLdapSelected), ldap.timeout);
0060     }
0061     if (ldap.limit != -1) {
0062         grp.writeEntry(QStringLiteral("SelectedSizeLimit%1").arg(numberOfLdapSelected), ldap.limit);
0063     }
0064     if (!ldap.password.isEmpty()) {
0065         storePassword(QStringLiteral("SelectedPwdBind%1").arg(numberOfLdapSelected), ImportWizardUtil::Ldap, ldap.password);
0066     }
0067     grp.sync();
0068 }
0069 
0070 void ImportWizardUtil::addAkonadiTag(const QList<tagStruct> &tagList)
0071 {
0072     for (int i = 0; i < tagList.size(); ++i) {
0073         Akonadi::Tag tag(tagList.at(i).name);
0074         if (tagList.at(i).color.isValid()) {
0075             tag.attribute<Akonadi::TagAttribute>(Akonadi::Tag::AddIfMissing)->setTextColor(tagList.at(i).color);
0076         }
0077         new Akonadi::TagCreateJob(tag);
0078     }
0079 }
0080 
0081 void ImportWizardUtil::storePassword(const QString &name, ImportWizardUtil::ResourceType type, const QString &password)
0082 {
0083     auto job = new ImportWizardSavePasswordJob;
0084     switch (type) {
0085     case Imap:
0086         job->setName(QStringLiteral("imap"));
0087         job->setPassword(password);
0088         job->setKey(name + QLatin1StringView("rc"));
0089         break;
0090     case Pop3:
0091         job->setName(QStringLiteral("pop3"));
0092         job->setPassword(password);
0093         job->setKey(name);
0094         break;
0095     case Ldap:
0096         job->setName(QStringLiteral("ldapclient"));
0097         job->setPassword(password);
0098         job->setKey(name);
0099     }
0100     job->start();
0101 }