File indexing completed on 2025-02-02 05:08:35
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 "identitybase.h" 0008 #include "accountwizard_debug.h" 0009 0010 #include <KIdentityManagementCore/Identity> 0011 #include <KIdentityManagementCore/IdentityManager> 0012 #include <MailTransport/Transport> 0013 0014 #include <KLocalizedString> 0015 0016 IdentityBase::IdentityBase(QObject *parent) 0017 : QObject(parent) 0018 { 0019 mIdentity = &KIdentityManagementCore::IdentityManager::self()->newFromScratch(QString()); 0020 Q_ASSERT(mIdentity != nullptr); 0021 } 0022 0023 void IdentityBase::create() 0024 { 0025 Q_EMIT info(i18n("Setting up identity...")); 0026 0027 // store identity information 0028 mIdentityName = identityName(); 0029 mIdentity->setIdentityName(mIdentityName); 0030 createNewIdentity(); 0031 Q_EMIT finished(i18n("Identity set up.")); 0032 } 0033 0034 void IdentityBase::createNewIdentity() 0035 { 0036 // TODO remove it for base. Add debug output only 0037 // Reimplement it. 0038 } 0039 0040 QString IdentityBase::identityName() const 0041 { 0042 // create identity name 0043 QString name(mIdentityName); 0044 if (name.isEmpty()) { 0045 name = i18nc("Default name for new email accounts/identities.", "Unnamed"); 0046 0047 const QString idName = mIdentity->primaryEmailAddress(); 0048 int pos = idName.indexOf(QLatin1Char('@')); 0049 if (pos != -1) { 0050 name = idName.mid(0, pos); 0051 } 0052 0053 // Make the name a bit more human friendly 0054 name.replace(QLatin1Char('.'), QLatin1Char(' ')); 0055 pos = name.indexOf(QLatin1Char(' ')); 0056 if (pos != 0) { 0057 name[pos + 1] = name[pos + 1].toUpper(); 0058 } 0059 name[0] = name[0].toUpper(); 0060 } 0061 0062 auto manager = KIdentityManagementCore::IdentityManager::self(); 0063 if (!manager->isUnique(name)) { 0064 name = manager->makeUnique(name); 0065 } 0066 return name; 0067 } 0068 0069 void IdentityBase::destroy() 0070 { 0071 auto manager = KIdentityManagementCore::IdentityManager::self(); 0072 if (!manager->removeIdentityForced(mIdentityName)) { 0073 qCWarning(ACCOUNTWIZARD_LOG) << " impossible to remove identity " << mIdentityName; 0074 } 0075 manager->commit(); 0076 mIdentity = nullptr; 0077 Q_EMIT info(i18n("Identity removed.")); 0078 } 0079 0080 void IdentityBase::setIdentityName(const QString &name) 0081 { 0082 mIdentityName = name; 0083 } 0084 0085 QString IdentityBase::fullName() const 0086 { 0087 return mIdentity->fullName(); 0088 } 0089 0090 void IdentityBase::setFullName(const QString &name) 0091 { 0092 if (name == fullName()) { 0093 return; 0094 } 0095 mIdentity->setFullName(name); 0096 Q_EMIT fullNameChanged(); 0097 } 0098 0099 QString IdentityBase::organization() const 0100 { 0101 return mIdentity->organization(); 0102 } 0103 0104 void IdentityBase::setOrganization(const QString &org) 0105 { 0106 if (org == organization()) { 0107 return; 0108 } 0109 mIdentity->setOrganization(org); 0110 Q_EMIT organizationChanged(); 0111 } 0112 0113 QString IdentityBase::email() const 0114 { 0115 return mIdentity->primaryEmailAddress(); 0116 } 0117 0118 void IdentityBase::setEmail(const QString &emailStr) 0119 { 0120 if (emailStr == email()) { 0121 return; 0122 } 0123 mIdentity->setPrimaryEmailAddress(emailStr); 0124 Q_EMIT emailChanged(); 0125 } 0126 0127 uint IdentityBase::uoid() const 0128 { 0129 return mIdentity->uoid(); 0130 } 0131 0132 void IdentityBase::setTransport(MailTransport::Transport *transport) 0133 { 0134 if (transport) { 0135 mIdentity->setTransport(QString::number(transport->id())); 0136 } else { 0137 mIdentity->setTransport({}); 0138 } 0139 } 0140 0141 QString IdentityBase::signature() const 0142 { 0143 return mIdentity->signature().text(); 0144 } 0145 0146 void IdentityBase::setSignature(const QString &sig) 0147 { 0148 if (!sig.isEmpty()) { 0149 const KIdentityManagementCore::Signature signature(sig); 0150 mIdentity->setSignature(signature); 0151 } else { 0152 mIdentity->setSignature(KIdentityManagementCore::Signature()); 0153 } 0154 } 0155 0156 void IdentityBase::setPreferredCryptoMessageFormat(const QString &format) 0157 { 0158 mIdentity->setPreferredCryptoMessageFormat(format); 0159 } 0160 0161 void IdentityBase::setXFace(const QString &xface) 0162 { 0163 mIdentity->setXFaceEnabled(!xface.isEmpty()); 0164 mIdentity->setXFace(xface); 0165 } 0166 0167 void IdentityBase::setPgpAutoEncrypt(bool autoencrypt) 0168 { 0169 mIdentity->setPgpAutoEncrypt(autoencrypt); 0170 } 0171 0172 void IdentityBase::setPgpAutoSign(bool autosign) 0173 { 0174 mIdentity->setPgpAutoSign(autosign); 0175 } 0176 0177 void IdentityBase::setKey(GpgME::Protocol protocol, const QByteArray &fingerprint) 0178 { 0179 if (fingerprint.isEmpty()) { 0180 mIdentity->setPGPEncryptionKey(QByteArray()); 0181 mIdentity->setPGPSigningKey(QByteArray()); 0182 mIdentity->setSMIMEEncryptionKey(QByteArray()); 0183 mIdentity->setSMIMESigningKey(QByteArray()); 0184 } else if (protocol == GpgME::OpenPGP) { 0185 mIdentity->setPGPSigningKey(fingerprint); 0186 mIdentity->setPGPEncryptionKey(fingerprint); 0187 } else if (protocol == GpgME::CMS) { 0188 mIdentity->setSMIMESigningKey(fingerprint); 0189 mIdentity->setSMIMEEncryptionKey(fingerprint); 0190 } 0191 } 0192 0193 #include "moc_identitybase.cpp"