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 #include "abstractsettings.h"
0007 #include "abstractdisplayinfo.h"
0008 
0009 #include <KIdentityManagementCore/Identity>
0010 #include <KIdentityManagementCore/IdentityManager>
0011 #include <MailTransport/TransportManager>
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <Akonadi/AgentInstanceCreateJob>
0016 #include <Akonadi/AgentManager>
0017 #include <Akonadi/AgentType>
0018 
0019 #include <QMetaMethod>
0020 
0021 using namespace Akonadi;
0022 
0023 AbstractSettings::AbstractSettings()
0024 {
0025     mManager = KIdentityManagementCore::IdentityManager::self();
0026     mKmailConfig = KSharedConfig::openConfig(QStringLiteral("kmail2rc"));
0027 }
0028 
0029 AbstractSettings::~AbstractSettings()
0030 {
0031     syncKmailConfig();
0032 }
0033 
0034 KIdentityManagementCore::Identity *AbstractSettings::createIdentity(QString &name)
0035 {
0036     name = uniqueIdentityName(name);
0037     KIdentityManagementCore::Identity *identity = &mManager->newFromScratch(name);
0038     addImportInfo(i18n("Setting up identity..."));
0039     return identity;
0040 }
0041 
0042 void AbstractSettings::storeIdentity(KIdentityManagementCore::Identity *identity)
0043 {
0044     mManager->setAsDefault(identity->uoid());
0045     mManager->commit();
0046     addImportInfo(i18n("Identity set up."));
0047 }
0048 
0049 QString AbstractSettings::uniqueIdentityName(const QString &name)
0050 {
0051     QString newName(name);
0052     int i = 0;
0053     while (!mManager->isUnique(newName)) {
0054         newName = QStringLiteral("%1_%2").arg(name).arg(i);
0055         ++i;
0056     }
0057     return newName;
0058 }
0059 
0060 MailTransport::Transport *AbstractSettings::createTransport()
0061 {
0062     MailTransport::Transport *mt = MailTransport::TransportManager::self()->createTransport();
0063     addImportInfo(i18n("Setting up transport..."));
0064     return mt;
0065 }
0066 
0067 void AbstractSettings::storeTransport(MailTransport::Transport *mt, bool isDefault)
0068 {
0069     mt->forceUniqueName();
0070     mt->save();
0071     MailTransport::TransportManager::self()->addTransport(mt);
0072     if (isDefault) {
0073         MailTransport::TransportManager::self()->setDefaultTransport(mt->id());
0074     }
0075     addImportInfo(i18n("Transport set up."));
0076 }
0077 
0078 void AbstractSettings::addImportInfo(const QString &log)
0079 {
0080     Q_ASSERT_X(mAbstractDisplayInfo, __FUNCTION__, "mAbstractDisplayInfo is not setting");
0081     if (mAbstractDisplayInfo) {
0082         mAbstractDisplayInfo->settingsImportInfo(log);
0083     }
0084 }
0085 
0086 void AbstractSettings::addImportError(const QString &log)
0087 {
0088     Q_ASSERT_X(mAbstractDisplayInfo, __FUNCTION__, "mAbstractDisplayInfo is not setting");
0089     if (mAbstractDisplayInfo) {
0090         mAbstractDisplayInfo->settingsImportError(log);
0091     }
0092 }
0093 
0094 void AbstractSettings::addCheckMailOnStartup(const QString &agentIdentifyName, bool loginAtStartup)
0095 {
0096     if (agentIdentifyName.isEmpty()) {
0097         return;
0098     }
0099     const QString groupName = QStringLiteral("Resource %1").arg(agentIdentifyName);
0100     addKmailConfig(groupName, QStringLiteral("CheckOnStartup"), loginAtStartup);
0101 }
0102 
0103 void AbstractSettings::addToManualCheck(const QString &agentIdentifyName, bool manualCheck)
0104 {
0105     if (agentIdentifyName.isEmpty()) {
0106         return;
0107     }
0108     const QString groupName = QStringLiteral("Resource %1").arg(agentIdentifyName);
0109     addKmailConfig(groupName, QStringLiteral("IncludeInManualChecks"), manualCheck);
0110 }
0111 
0112 void AbstractSettings::addComposerHeaderGroup(const QString &groupName, const QString &name, const QString &value)
0113 {
0114     KConfigGroup group = mKmailConfig->group(groupName);
0115     group.writeEntry(QStringLiteral("name"), name);
0116     group.writeEntry(QStringLiteral("value"), value);
0117 }
0118 
0119 void AbstractSettings::addKmailConfig(const QString &groupName, const QString &key, const QString &value)
0120 {
0121     KConfigGroup group = mKmailConfig->group(groupName);
0122     group.writeEntry(key, value);
0123 }
0124 
0125 void AbstractSettings::addKmailConfig(const QString &groupName, const QString &key, bool value)
0126 {
0127     KConfigGroup group = mKmailConfig->group(groupName);
0128     group.writeEntry(key, value);
0129 }
0130 
0131 void AbstractSettings::addKmailConfig(const QString &groupName, const QString &key, int value)
0132 {
0133     KConfigGroup group = mKmailConfig->group(groupName);
0134     group.writeEntry(key, value);
0135 }
0136 
0137 void AbstractSettings::syncKmailConfig()
0138 {
0139     mKmailConfig->sync();
0140 }
0141 
0142 int AbstractSettings::readKmailSettings(const QString &groupName, const QString &key)
0143 {
0144     KConfigGroup group = mKmailConfig->group(groupName);
0145     int value = group.readEntry(key, -1);
0146     return value;
0147 }
0148 
0149 #include "moc_abstractsettings.cpp"