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 "balsasettings.h"
0008 #include "balsaplugin_debug.h"
0009 #include <MailCommon/MailUtil>
0010 #include <MailTransport/TransportManager>
0011 #include <MessageViewer/KXFace>
0012 
0013 #include <KIdentityManagementCore/Identity>
0014 #include <KIdentityManagementCore/Signature>
0015 
0016 #include <KConfig>
0017 #include <KConfigGroup>
0018 #include <QImage>
0019 #include <QRegularExpression>
0020 
0021 BalsaSettings::BalsaSettings(const QString &filename)
0022     : mFileName(filename)
0023 {
0024 }
0025 
0026 BalsaSettings::~BalsaSettings() = default;
0027 
0028 void BalsaSettings::importSettings()
0029 {
0030     KConfig config(mFileName);
0031 
0032     bool autoCheck = false;
0033     int autoCheckDelay = -1;
0034     if (config.hasGroup(QStringLiteral("MailboxChecking"))) {
0035         KConfigGroup grp = config.group(QStringLiteral("MailboxChecking"));
0036         autoCheck = grp.readEntry(QStringLiteral("Auto"), false);
0037         autoCheckDelay = grp.readEntry(QStringLiteral("AutoDelay"), -1);
0038     }
0039 
0040     const QStringList mailBoxList = config.groupList().filter(QRegularExpression(QStringLiteral("mailbox-")));
0041     for (const QString &mailBox : mailBoxList) {
0042         KConfigGroup grp = config.group(mailBox);
0043         readAccount(grp, autoCheck, autoCheckDelay);
0044     }
0045 
0046     const QStringList smtpList = config.groupList().filter(QRegularExpression(QStringLiteral("smtp-server-")));
0047     for (const QString &smtp : smtpList) {
0048         KConfigGroup grp = config.group(smtp);
0049         readTransport(grp);
0050     }
0051     readGlobalSettings(config);
0052 }
0053 
0054 void BalsaSettings::readAccount(const KConfigGroup &grp, bool autoCheck, int autoDelay)
0055 {
0056     Q_UNUSED(autoDelay)
0057     const QString type = grp.readEntry(QStringLiteral("Type"));
0058     bool check = grp.readEntry(QStringLiteral("Check"), false);
0059     if (type == QLatin1StringView("LibBalsaMailboxPOP3")) {
0060         QMap<QString, QVariant> settings;
0061         const QString server = grp.readEntry(QStringLiteral("Server"));
0062         settings.insert(QStringLiteral("Host"), server);
0063         const QString name = grp.readEntry(QStringLiteral("Name"));
0064 
0065         // const bool apop = grp.readEntry(QStringLiteral("DisableApop"), false);
0066         // Q_UNUSED(apop)
0067         const QString agentIdentifyName = LibImportWizard::AbstractBase::createResource(QStringLiteral("akonadi_pop3_resource"), name, settings);
0068 
0069         addCheckMailOnStartup(agentIdentifyName, autoCheck);
0070         addToManualCheck(agentIdentifyName, check);
0071     } else if (type == QLatin1StringView("LibBalsaMailboxImap")) {
0072         QMap<QString, QVariant> settings;
0073         const QString server = grp.readEntry(QStringLiteral("Server"));
0074         settings.insert(QStringLiteral("ImapServer"), server);
0075         const QString name = grp.readEntry(QStringLiteral("Name"));
0076         const QString agentIdentifyName = LibImportWizard::AbstractBase::createResource(QStringLiteral("akonadi_imap_resource"), name, settings);
0077         addCheckMailOnStartup(agentIdentifyName, autoCheck);
0078         addToManualCheck(agentIdentifyName, check);
0079     } else {
0080         qCDebug(BALSAPLUGIN_LOG) << " unknown account type :" << type;
0081     }
0082 }
0083 
0084 void BalsaSettings::readIdentity(const KConfigGroup &grp)
0085 {
0086     QString name = grp.readEntry(QStringLiteral("FullName"));
0087     KIdentityManagementCore::Identity *newIdentity = createIdentity(name);
0088     newIdentity->setFullName(name);
0089     newIdentity->setPrimaryEmailAddress(grp.readEntry(QStringLiteral("Address")));
0090     newIdentity->setReplyToAddr(grp.readEntry(QStringLiteral("ReplyTo")));
0091     newIdentity->setBcc(grp.readEntry(QStringLiteral("Bcc")));
0092     const QString smtp = grp.readEntry(QStringLiteral("SmtpServer"));
0093     if (!smtp.isEmpty()) {
0094         const QString val = mHashSmtp.value(smtp);
0095         if (val.isEmpty()) {
0096             newIdentity->setTransport(val);
0097         }
0098     }
0099 
0100     const QString signaturePath = grp.readEntry(QStringLiteral("SignaturePath"));
0101     if (!signaturePath.isEmpty()) {
0102         KIdentityManagementCore::Signature signature;
0103         if (grp.readEntry(QStringLiteral("SigExecutable"), false)) {
0104             signature.setPath(signaturePath, true);
0105             signature.setType(KIdentityManagementCore::Signature::FromCommand);
0106         } else {
0107             signature.setType(KIdentityManagementCore::Signature::FromFile);
0108         }
0109         newIdentity->setSignature(signature);
0110     }
0111 
0112     const QString xfacePathStr = grp.readEntry(QStringLiteral("XFacePath"));
0113     if (!xfacePathStr.isEmpty()) {
0114         newIdentity->setXFaceEnabled(true);
0115         MessageViewer::KXFace xf;
0116         newIdentity->setXFace(xf.fromImage(QImage(xfacePathStr)));
0117     }
0118     storeIdentity(newIdentity);
0119 }
0120 
0121 void BalsaSettings::readTransport(const KConfigGroup &grp)
0122 {
0123     MailTransport::Transport *mt = createTransport();
0124     const QString smtp = grp.name().remove(QStringLiteral("smtp-server-"));
0125     const QString server = grp.readEntry(QStringLiteral("Server"));
0126     mt->setHost(server);
0127 
0128     const int tlsMode = grp.readEntry(QStringLiteral("TLSMode"), -1);
0129     // TODO
0130     switch (tlsMode) {
0131     case 0:
0132         break;
0133     case 1:
0134         mt->setEncryption(MailTransport::Transport::EnumEncryption::TLS);
0135         break;
0136     case 2:
0137         break;
0138     default:
0139         break;
0140     }
0141 
0142     const QString ssl = grp.readEntry(QStringLiteral("SSL"));
0143     if (ssl == QLatin1StringView("true")) {
0144         mt->setEncryption(MailTransport::Transport::EnumEncryption::SSL);
0145     } else if (ssl == QLatin1StringView("false")) {
0146         mt->setEncryption(MailTransport::Transport::EnumEncryption::None);
0147     } else {
0148         qCDebug(BALSAPLUGIN_LOG) << " unknown ssl value :" << ssl;
0149     }
0150 
0151     // TODO const QString anonymous = grp.readEntry(QStringLiteral("Anonymous"));
0152 
0153     // TODO
0154     storeTransport(mt, /*( smtp == defaultSmtp )*/ true); // FIXME
0155     mHashSmtp.insert(smtp, QString::number(mt->id()));
0156 
0157     // TODO
0158     /*
0159     Server=localhost:25
0160     Anonymous=false
0161     RememberPasswd=false
0162     SSL=false
0163     TLSMode=1
0164     BigMessage=0
0165     */
0166 }
0167 
0168 void BalsaSettings::readGlobalSettings(const KConfig &config)
0169 {
0170     if (config.hasGroup(QStringLiteral("Compose"))) {
0171         KConfigGroup compose = config.group(QStringLiteral("Compose"));
0172         if (compose.hasKey(QStringLiteral("QuoteString"))) {
0173             const QString quote = compose.readEntry(QStringLiteral("QuoteString"));
0174             if (!quote.isEmpty()) {
0175                 addKmailConfig(QStringLiteral("TemplateParser"), QStringLiteral("QuoteString"), quote);
0176             }
0177         }
0178     }
0179     if (config.hasGroup(QStringLiteral("MessageDisplay"))) {
0180         KConfigGroup messageDisplay = config.group(QStringLiteral("MessageDisplay"));
0181 #if 0
0182         if (messageDisplay.hasKey(QStringLiteral("WordWrap"))) {
0183             //bool wordWrap = messageDisplay.readEntry(QStringLiteral("WordWrap"), false);
0184             //Q_UNUSED(wordWrap)
0185             //TODO not implemented in kmail.
0186         }
0187         if (messageDisplay.hasKey(QStringLiteral("WordWrapLength"))) {
0188             const int wordWrapLength = messageDisplay.readEntry(QStringLiteral("WordWrapLength"), -1);
0189             Q_UNUSED(wordWrapLength)
0190             //TODO not implemented in kmail
0191         }
0192 #endif
0193         if (messageDisplay.hasKey(QStringLiteral("DateFormat"))) {
0194             const QString dateFormat = messageDisplay.readEntry(QStringLiteral("DateFormat"));
0195             if (!dateFormat.isEmpty()) {
0196                 addKmailConfig(QStringLiteral("General"), QStringLiteral("customDateFormat"), dateFormat);
0197             }
0198         }
0199     }
0200 
0201     if (config.hasGroup(QStringLiteral("Sending"))) {
0202         KConfigGroup sending = config.group(QStringLiteral("Sending"));
0203         if (sending.hasKey(QStringLiteral("WordWrap"))) {
0204             const bool wordWrap = sending.readEntry(QStringLiteral("WordWrap"), false);
0205             addKmailConfig(QStringLiteral("Composer"), QStringLiteral("word-wrap"), wordWrap);
0206         }
0207         if (sending.hasKey(QStringLiteral("break-at"))) {
0208             const int wordWrapLength = sending.readEntry(QStringLiteral("break-at"), -1);
0209             if (wordWrapLength != -1) {
0210                 addKmailConfig(QStringLiteral("Composer"), QStringLiteral("break-at"), wordWrapLength);
0211             }
0212         }
0213     }
0214     if (config.hasGroup(QStringLiteral("Global"))) {
0215         KConfigGroup global = config.group(QStringLiteral("Global"));
0216         if (global.hasKey(QStringLiteral("EmptyTrash"))) {
0217             const bool emptyTrash = global.readEntry(QStringLiteral("EmptyTrash"), false);
0218             addKmailConfig(QStringLiteral("General"), QStringLiteral("empty-trash-on-exit"), emptyTrash);
0219         }
0220     }
0221     if (config.hasGroup(QStringLiteral("Spelling"))) {
0222         KConfigGroup spellChecking = config.group(QStringLiteral("Spelling"));
0223         if (spellChecking.hasKey(QStringLiteral("SpellCheckActive"))) {
0224             const bool active = spellChecking.readEntry(QStringLiteral("SpellCheckActive"), false);
0225             addKmailConfig(QStringLiteral("Spelling"), QStringLiteral("backgroundCheckerEnabled"), active);
0226             addKmailConfig(QStringLiteral("Spelling"), QStringLiteral("checkerEnabledByDefault"), active);
0227         }
0228         if (spellChecking.hasKey(QStringLiteral("SpellCheckLanguage"))) {
0229             const QString spellCheck = spellChecking.readEntry(QStringLiteral("defaultLanguage"));
0230             addKmailConfig(QStringLiteral("Spelling"), QStringLiteral("defaultLanguage"), spellCheck);
0231         }
0232     }
0233 }