File indexing completed on 2024-10-27 04:58:01

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 "identity.h"
0010 #include "transport.h"
0011 
0012 #include <KIdentityManagement/IdentityManager>
0013 #include <kidentitymanagement/identity.h>
0014 
0015 #include <KLocalizedString>
0016 
0017 Identity::Identity(QObject *parent)
0018     : SetupObject(parent)
0019 {
0020     m_identity = &KIdentityManagement::IdentityManager::self()->newFromScratch(QString());
0021     Q_ASSERT(m_identity != nullptr);
0022 }
0023 
0024 Identity::~Identity() = default;
0025 
0026 void Identity::create()
0027 {
0028     Q_EMIT info(i18n("Setting up identity..."));
0029 
0030     // store identity information
0031     m_identityName = identityName();
0032     m_identity->setIdentityName(m_identityName);
0033     auto manager = KIdentityManagement::IdentityManager::self();
0034     manager->commit();
0035     if (!manager->setAsDefault(m_identity->uoid())) {
0036         qWarning() << "Impossible to find identity";
0037     }
0038 
0039     Q_EMIT finished(i18n("Identity set up."));
0040 }
0041 
0042 QString Identity::identityName() const
0043 {
0044     // create identity name
0045     QString name(m_identityName);
0046     if (name.isEmpty()) {
0047         name = i18nc("Default name for new email accounts/identities.", "Unnamed");
0048 
0049         const QString idName = m_identity->primaryEmailAddress();
0050         int pos = idName.indexOf(QLatin1Char('@'));
0051         if (pos != -1) {
0052             name = idName.mid(0, pos);
0053         }
0054 
0055         // Make the name a bit more human friendly
0056         name.replace(QLatin1Char('.'), QLatin1Char(' '));
0057         pos = name.indexOf(QLatin1Char(' '));
0058         if (pos != 0) {
0059             name[pos + 1] = name[pos + 1].toUpper();
0060         }
0061         name[0] = name[0].toUpper();
0062     }
0063 
0064     auto manager = KIdentityManagement::IdentityManager::self();
0065     if (!manager->isUnique(name)) {
0066         name = manager->makeUnique(name);
0067     }
0068     return name;
0069 }
0070 
0071 void Identity::destroy()
0072 {
0073     auto manager = KIdentityManagement::IdentityManager::self();
0074     if (!manager->removeIdentityForced(m_identityName)) {
0075         qWarning() << " impossible to remove identity " << m_identityName;
0076     }
0077     manager->commit();
0078     m_identity = nullptr;
0079     Q_EMIT info(i18n("Identity removed."));
0080 }
0081 
0082 void Identity::setIdentityName(const QString &name)
0083 {
0084     m_identityName = name;
0085 }
0086 
0087 void Identity::setRealName(const QString &name)
0088 {
0089     m_identity->setFullName(name);
0090 }
0091 
0092 void Identity::setOrganization(const QString &org)
0093 {
0094     m_identity->setOrganization(org);
0095 }
0096 
0097 void Identity::setEmail(const QString &email)
0098 {
0099     m_identity->setPrimaryEmailAddress(email);
0100 }
0101 
0102 uint Identity::uoid() const
0103 {
0104     return m_identity->uoid();
0105 }
0106 
0107 void Identity::setTransport(QObject *transport)
0108 {
0109     if (transport) {
0110         m_identity->setTransport(QString::number(qobject_cast<Transport *>(transport)->transportId()));
0111     } else {
0112         m_identity->setTransport(QString());
0113     }
0114     setDependsOn(qobject_cast<SetupObject *>(transport));
0115 }
0116 
0117 void Identity::setSignature(const QString &sig)
0118 {
0119     if (!sig.isEmpty()) {
0120         const KIdentityManagement::Signature signature(sig);
0121         m_identity->setSignature(signature);
0122     } else {
0123         m_identity->setSignature(KIdentityManagement::Signature());
0124     }
0125 }
0126 
0127 void Identity::setPreferredCryptoMessageFormat(const QString &format)
0128 {
0129     m_identity->setPreferredCryptoMessageFormat(format);
0130 }
0131 
0132 void Identity::setXFace(const QString &xface)
0133 {
0134     m_identity->setXFaceEnabled(!xface.isEmpty());
0135     m_identity->setXFace(xface);
0136 }
0137 
0138 void Identity::setPgpAutoEncrypt(bool autoencrypt)
0139 {
0140     m_identity->setPgpAutoEncrypt(autoencrypt);
0141 }
0142 
0143 void Identity::setPgpAutoSign(bool autosign)
0144 {
0145     m_identity->setPgpAutoSign(autosign);
0146 }
0147 
0148 void Identity::setKey(GpgME::Protocol protocol, const QByteArray &fingerprint)
0149 {
0150     if (fingerprint.isEmpty()) {
0151         m_identity->setPGPEncryptionKey(QByteArray());
0152         m_identity->setPGPSigningKey(QByteArray());
0153         m_identity->setSMIMEEncryptionKey(QByteArray());
0154         m_identity->setSMIMESigningKey(QByteArray());
0155     } else if (protocol == GpgME::OpenPGP) {
0156         m_identity->setPGPSigningKey(fingerprint);
0157         m_identity->setPGPEncryptionKey(fingerprint);
0158     } else if (protocol == GpgME::CMS) {
0159         m_identity->setSMIMESigningKey(fingerprint);
0160         m_identity->setSMIMEEncryptionKey(fingerprint);
0161     }
0162 }