File indexing completed on 2024-12-15 04:55:35
0001 /* 0002 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "importimapsettingsakonadicheckjob.h" 0008 #include "importimapsettingsakonadipassword.h" 0009 #include "libsieveeditor_export.h" 0010 #include "sieveeditor_debug.h" 0011 #include <KConfigGroup> 0012 #include <KLocalizedString> 0013 #include <KSharedConfig> 0014 #include <QDir> 0015 #include <QFile> 0016 #include <QStandardPaths> 0017 0018 ImportImapSettingsAkonadiCheckJob::ImportImapSettingsAkonadiCheckJob(QObject *parent) 0019 : AbstractImapSettingsCheckJob(parent) 0020 { 0021 } 0022 0023 ImportImapSettingsAkonadiCheckJob::~ImportImapSettingsAkonadiCheckJob() = default; 0024 0025 bool ImportImapSettingsAkonadiCheckJob::resourceCanHaveSieveSupport(const QString &filename) const 0026 { 0027 return filename.startsWith(QLatin1StringView("akonadi_kolab_resource")) || filename.startsWith(QLatin1StringView("akonadi_imap_resource")); 0028 } 0029 0030 void ImportImapSettingsAkonadiCheckJob::start() 0031 { 0032 mSettingsWereImported = false; 0033 const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::ConfigLocation, QString(), QStandardPaths::LocateDirectory); 0034 for (const QString &dir : dirs) { 0035 const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*rc")); 0036 for (const QString &file : fileNames) { 0037 if (resourceCanHaveSieveSupport(file)) { 0038 mSieveServerLst.insert(dir, file); 0039 } 0040 } 0041 } 0042 loadSieveServerSettings(); 0043 } 0044 0045 void ImportImapSettingsAkonadiCheckJob::slotImportNextServerSieveDone(const SieveEditorUtil::SieveServerConfig &config, const QString &filename) 0046 { 0047 if (config.isValid()) { 0048 Q_EMIT importSetting(filename, config); 0049 mSettingsWereImported = true; 0050 } 0051 importNextServerSieve(); 0052 } 0053 0054 void ImportImapSettingsAkonadiCheckJob::loadSieveServerSettings() 0055 { 0056 mSieveServerMapIterator = mSieveServerLst.constBegin(); 0057 if (mSieveServerMapIterator != mSieveServerLst.constEnd()) { 0058 importSettings(mSieveServerMapIterator.key(), mSieveServerMapIterator.value()); 0059 } 0060 } 0061 0062 void ImportImapSettingsAkonadiCheckJob::importNextServerSieve() 0063 { 0064 ++mSieveServerMapIterator; 0065 if (mSieveServerMapIterator != mSieveServerLst.constEnd()) { 0066 importSettings(mSieveServerMapIterator.key(), mSieveServerMapIterator.value()); 0067 } else { 0068 checkNoSettingsImported(); 0069 } 0070 } 0071 0072 void ImportImapSettingsAkonadiCheckJob::importSettings(const QString &directory, const QString &filename) 0073 { 0074 const QString filePath = directory + QLatin1Char('/') + filename; 0075 // qCDebug(SIEVEEDITOR_LOG) << "importSettings filename:" << filePath; 0076 if (!QFileInfo::exists(filePath)) { 0077 qCWarning(SIEVEEDITOR_LOG) << "Unable to open file " << filePath; 0078 importNextServerSieve(); 0079 return; 0080 } 0081 SieveEditorUtil::SieveServerConfig config; 0082 const bool isKolabSettings = filePath.contains(QLatin1StringView("/akonadi_kolab_resource")); 0083 KSharedConfigPtr resourceConfig = KSharedConfig::openConfig(filePath); 0084 0085 KConfigGroup sieveGroup = resourceConfig->group(QStringLiteral("siever")); 0086 const bool hasSieveSupport = sieveGroup.readEntry(QStringLiteral("SieveSupport"), isKolabSettings ? true : false); 0087 if (hasSieveSupport) { 0088 bool reuseImapSettings = sieveGroup.readEntry(QStringLiteral("SieveReuseConfig"), true); 0089 KConfigGroup networkGroup = resourceConfig->group(QStringLiteral("network")); 0090 const QString userName = networkGroup.readEntry(QStringLiteral("UserName"), QString()); 0091 const QString imapServerName = networkGroup.readEntry(QStringLiteral("ImapServer"), QString()); 0092 const int imapPort = networkGroup.readEntry(QStringLiteral("ImapPort"), isKolabSettings ? 143 : 993); 0093 config.sieveImapAccountSettings.setUserName(userName); 0094 config.sieveImapAccountSettings.setServerName(imapServerName); 0095 config.sieveImapAccountSettings.setPort(imapPort); 0096 config.sieveImapAccountSettings.setAuthenticationType(static_cast<KSieveCore::SieveImapAccountSettings::AuthenticationMode>( 0097 networkGroup.readEntry(QStringLiteral("Authentication"), static_cast<int>(KSieveCore::SieveImapAccountSettings::Plain)))); 0098 const QString encryption = networkGroup.readEntry(QStringLiteral("Safety")); 0099 if (encryption == QLatin1StringView("SSL")) { 0100 config.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::SSLorTLS); 0101 } else if (encryption == QLatin1StringView("STARTTLS")) { 0102 config.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::STARTTLS); 0103 } else if (encryption == QLatin1StringView("None")) { 0104 config.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::Unencrypted); 0105 } else if (encryption.isEmpty()) { // Default value 0106 if (isKolabSettings) { 0107 config.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::STARTTLS); 0108 } else { 0109 config.sieveImapAccountSettings.setEncryptionMode(KSieveCore::SieveImapAccountSettings::EncryptionMode::Unencrypted); 0110 } 0111 } else { 0112 qCWarning(SIEVEEDITOR_LOG) << "Unknown encryption mode " << encryption; 0113 } 0114 const int sievePort = sieveGroup.readEntry(QStringLiteral("SievePort"), 4190); 0115 if (sievePort != -1) { 0116 config.sieveSettings.port = sievePort; 0117 } 0118 if (reuseImapSettings) { 0119 config.sieveSettings.serverName = imapServerName; 0120 config.sieveSettings.userName = userName; 0121 config.sieveSettings.authenticationType = static_cast<MailTransport::Transport::EnumAuthenticationType>( 0122 sieveGroup.readEntry(QStringLiteral("Authentication"), static_cast<int>(MailTransport::Transport::EnumAuthenticationType::PLAIN))); 0123 } else { 0124 const QString sieveCustomUserName = sieveGroup.readEntry(QStringLiteral("SieveCustomUsername")); 0125 config.sieveSettings.userName = sieveCustomUserName; 0126 config.sieveSettings.serverName = imapServerName; // FIXME 0127 // TODO 0128 } 0129 Q_ASSERT_X(mPasswordImporter, "Missing mPasswordImporter", "You must create a mPasswordImporter"); 0130 mPasswordImporter->importPasswords(config, filename, reuseImapSettings); 0131 } else { 0132 importNextServerSieve(); 0133 } 0134 } 0135 0136 bool ImportImapSettingsAkonadiCheckJob::settingsCanBeImported() const 0137 { 0138 const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::ConfigLocation, QString(), QStandardPaths::LocateDirectory); 0139 for (const QString &dir : dirs) { 0140 const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*rc")); 0141 for (const QString &file : fileNames) { 0142 if (resourceCanHaveSieveSupport(file)) { 0143 return true; 0144 } 0145 } 0146 } 0147 return false; 0148 } 0149 0150 QString ImportImapSettingsAkonadiCheckJob::name() const 0151 { 0152 return i18n("Akonadi IMAP Settings"); 0153 } 0154 0155 void ImportImapSettingsAkonadiCheckJob::setImapSettingsPassword(AbstractImapSettingsPassword *passwordImporter) 0156 { 0157 mPasswordImporter = passwordImporter; 0158 connect(mPasswordImporter, &AbstractImapSettingsPassword::importPasswordDone, this, &ImportImapSettingsAkonadiCheckJob::slotImportNextServerSieveDone); 0159 } 0160 0161 #include "moc_importimapsettingsakonadicheckjob.cpp"