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

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 "trojitasettings.h"
0008 #include <ImportWizard/ImportWizardUtil>
0009 
0010 #include <MailTransport/TransportManager>
0011 
0012 #include <KIdentityManagementCore/Identity>
0013 #include <KIdentityManagementCore/Signature>
0014 
0015 #include "trojitaplugin_debug.h"
0016 
0017 #include <QSettings>
0018 
0019 TrojitaSettings::TrojitaSettings(const QString &filename)
0020 {
0021     settings = new QSettings(filename, QSettings::IniFormat, this);
0022 }
0023 
0024 TrojitaSettings::~TrojitaSettings()
0025 {
0026     delete settings;
0027 }
0028 
0029 void TrojitaSettings::importSettings()
0030 {
0031     readImapAccount();
0032     readTransport();
0033     readIdentity();
0034     readGlobalSettings();
0035 }
0036 
0037 void TrojitaSettings::readImapAccount()
0038 {
0039     QMap<QString, QVariant> newSettings;
0040     QString name;
0041 
0042     name = settings->value(QStringLiteral("imap.host")).toString();
0043     if (!name.isEmpty()) {
0044         newSettings.insert(QStringLiteral("ImapServer"), name);
0045     }
0046 
0047     const int port = settings->value(QStringLiteral("imap.port"), -1).toInt();
0048     if (port > -1) {
0049         newSettings.insert(QStringLiteral("ImapPort"), port);
0050     }
0051 
0052     if (settings->contains(QStringLiteral("imap.starttls"))) {
0053         const bool useTLS = settings->value(QStringLiteral("imap.starttls")).toBool();
0054         if (useTLS) {
0055             newSettings.insert(QStringLiteral("Safety"), QStringLiteral("STARTTLS"));
0056         }
0057     }
0058 
0059     const QString userName = settings->value(QStringLiteral("imap.auth.user")).toString();
0060     if (!userName.isEmpty()) {
0061         newSettings.insert(QStringLiteral("Username"), userName);
0062     }
0063     const QString password = settings->value(QStringLiteral("imap.auth.pass")).toString();
0064     if (!password.isEmpty()) {
0065         newSettings.insert(QStringLiteral("Password"), password);
0066     }
0067 
0068 #if 0
0069     if (settings->contains(QStringLiteral("imap.process"))) {
0070         //What's this ?
0071     }
0072 
0073     if (settings->contains(QStringLiteral("imap.offline"))) {
0074         //const bool offlineStatus = settings->value(QStringLiteral("imap.offline")).toBool();
0075         //It's not a deconnected mode as imap disconnected #317023
0076         //Will implement soon.
0077         //TODO use akonadi cache.
0078     }
0079     if (settings->contains(QStringLiteral("imap.enableId"))) {
0080         //Not supported by Akonadi.
0081     }
0082 
0083     if (settings->contains(QStringLiteral("imap.ssl.pemCertificate"))) {
0084         //Not supported by akonadi.
0085     }
0086 
0087     if (settings->contains(QStringLiteral("imap.capabilities.blacklist"))) {
0088         //Not supported by akonadi-imap-resource.
0089     }
0090 #endif
0091     if (!name.isEmpty()) {
0092         const QString agentIdentifyName = LibImportWizard::AbstractBase::createResource(QStringLiteral("akonadi_imap_resource"), name, newSettings);
0093         // Check by default
0094         addCheckMailOnStartup(agentIdentifyName, true);
0095     }
0096 }
0097 
0098 void TrojitaSettings::readTransport()
0099 {
0100     settings->beginGroup(QStringLiteral("General"));
0101     const QString smtpMethod = settings->value(QStringLiteral("msa.method")).toString();
0102     if (!smtpMethod.isEmpty()) {
0103         MailTransport::Transport *mt = createTransport();
0104         if (smtpMethod == QLatin1StringView("IMAP-SENDMAIL")) {
0105             // see http://tools.ietf.org/html/draft-kundrat-imap-submit-01
0106         } else if (smtpMethod == QLatin1StringView("SMTP") || smtpMethod == QLatin1StringView("SSMTP")) {
0107             if (settings->contains(QStringLiteral("msa.smtp.host"))) {
0108                 mt->setHost(settings->value(QStringLiteral("msa.smtp.host")).toString());
0109             }
0110             if (settings->contains(QStringLiteral("msa.smtp.port"))) {
0111                 mt->setPort(settings->value(QStringLiteral("msa.smtp.port")).toInt());
0112             }
0113             if (settings->contains(QStringLiteral("msa.smtp.auth"))) {
0114                 if (settings->value(QStringLiteral("msa.smtp.auth")).toBool()) {
0115                     if (settings->contains(QStringLiteral("msa.smtp.auth.user"))) {
0116                         mt->setUserName(settings->value(QStringLiteral("msa.smtp.auth.user")).toString());
0117                     }
0118                     if (settings->contains(QStringLiteral("msa.smtp.auth.pass"))) {
0119                         mt->setPassword(settings->value(QStringLiteral("msa.smtp.auth.pass")).toString());
0120                     }
0121                 }
0122             }
0123 
0124             if (settings->contains(QStringLiteral("msa.smtp.starttls"))) {
0125                 if (settings->value(QStringLiteral("msa.smtp.starttls")).toBool()) {
0126                     mt->setEncryption(MailTransport::Transport::EnumEncryption::TLS);
0127                 }
0128             }
0129             mt->setIdentifier(QStringLiteral("SMTP"));
0130         } else {
0131             qCWarning(TROJITAPLUGIN_LOG) << " smtpMethod unknown " << smtpMethod;
0132         }
0133         storeTransport(mt, true); // only one smtp for the moment
0134     }
0135     settings->endGroup();
0136 }
0137 
0138 void TrojitaSettings::readIdentity()
0139 {
0140     const int size = settings->beginReadArray(QStringLiteral("identities"));
0141     for (int i = 0; i < size; ++i) {
0142         settings->setArrayIndex(i);
0143         QString realName = settings->value(QStringLiteral("realName")).toString();
0144         KIdentityManagementCore::Identity *identity = createIdentity(realName);
0145         identity->setFullName(realName);
0146         identity->setIdentityName(realName);
0147         const QString address = settings->value(QStringLiteral("address")).toString();
0148         identity->setPrimaryEmailAddress(address);
0149         const QString organisation = settings->value(QStringLiteral("organisation")).toString();
0150         identity->setOrganization(organisation);
0151         QString signatureStr = settings->value(QStringLiteral("signature")).toString();
0152         if (!signatureStr.isEmpty()) {
0153             KIdentityManagementCore::Signature signature;
0154             signature.setType(KIdentityManagementCore::Signature::Inlined);
0155             signature.setText(signatureStr);
0156             identity->setSignature(signature);
0157         }
0158         qCDebug(TROJITAPLUGIN_LOG) << " realName :" << realName << " address : " << address << " organisation : " << organisation
0159                                    << " signature: " << signatureStr;
0160         storeIdentity(identity);
0161     }
0162     settings->endArray();
0163 }
0164 
0165 void TrojitaSettings::readGlobalSettings()
0166 {
0167     // TODO
0168 }