diff --git a/core/autotests/mail/importmailjobinterfacetest.cpp b/core/autotests/mail/importmailjobinterfacetest.cpp index 53e38e2..c536eb0 100644 --- a/core/autotests/mail/importmailjobinterfacetest.cpp +++ b/core/autotests/mail/importmailjobinterfacetest.cpp @@ -1,156 +1,158 @@ /* Copyright (C) 2020 Laurent Montel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "importmailjobinterfacetest.h" #include "archivestorage.h" #include "resourceconvertertest.h" #include "testimportfile.h" #include "utils.h" +#include "mail/smtpmailtransport.h" #include #include #include #include QTEST_MAIN(ImportMailJobInterfaceTest) ImportMailJobInterfaceTestImpl::ImportMailJobInterfaceTestImpl(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep) : ImportMailJobInterface(parent, typeSelected, archiveStorage, numberOfStep) { } ImportMailJobInterfaceTestImpl::~ImportMailJobInterfaceTestImpl() { //Clean up temp repo QVERIFY(QDir(QDir::tempPath() + QLatin1Char('/') + Utils::storeMails()).removeRecursively()); } Akonadi::Collection::Id ImportMailJobInterfaceTestImpl::convertFolderPathToCollectionId(const QString &path) { ResourceConverterTest resourceConverterTest; resourceConverterTest.setTestPath(mPathConfig); return resourceConverterTest.convertFolderPathToCollectionId(path); } void ImportMailJobInterfaceTestImpl::synchronizeResource(const QStringList &lst) { mLogCreateResource->logSynchronizeResource(lst); slotNextStep(); } QString ImportMailJobInterfaceTestImpl::createResource(const QString &resources, const QString &name, const QMap &settings, bool synchronizeTree) { return mLogCreateResource->logCreateResource(resources, name, settings, synchronizeTree); } void ImportMailJobInterfaceTestImpl::registerSpecialCollection(Akonadi::SpecialMailCollections::Type type, qint64 colId) { mLogCreateResource->logRegisterSpecialCollection(type, colId); } void ImportMailJobInterfaceTestImpl::importFilters(const QVector &filters) { qDebug() << " void ImportMailJobInterfaceTestImpl::importFilters(const QVector &filters)"; } QString ImportMailJobInterfaceTestImpl::adaptResourcePath(const KSharedConfigPtr &resourceConfig, const QString &storedData) { ResourceConverterTest resourceConverterTest; resourceConverterTest.setTestPath(mPathConfig); return resourceConverterTest.adaptResourcePath(resourceConfig, storedData); } QString ImportMailJobInterfaceTestImpl::adaptNewResourceUrl(bool overwriteResources, const KSharedConfig::Ptr &resourceConfig, const QString &storePath) { ResourceConverterTest resourceConverterTest; resourceConverterTest.setTestPath(mPathConfig); return resourceConverterTest.adaptNewResourceUrl(overwriteResources, resourceConfig, storePath); } void ImportMailJobInterfaceTestImpl::addNewIdentity(const QString &name, KConfigGroup &group, int defaultIdentities, int oldUid) { qDebug() << " void ImportMailJobInterfaceTestImpl::addNewIdentity(const QString &name, KConfigGroup &group, int defaultIdentities, int oldUid) not implemented yet" << oldUid << " name " << name; KIdentityManagement::Identity identity; identity.setUoid(mIdentityUoid++); group.writeEntry(QStringLiteral("Name"), name); group.sync(); identity.readConfig(group); if (oldUid != -1) { mHashIdentity.insert(oldUid, identity.uoid()); if (oldUid == defaultIdentities) { //TODO //mIdentityManager->setAsDefault(identity->uoid()); } } // identity.writeConfig(); //TODO write identity ? //TODO log it. //mIdentityManager->commit(); } void ImportMailJobInterfaceTestImpl::importCustomMailTransport(const QString &identifierValue, const KConfigGroup &group, int defaultTransport, int transportId) { qDebug() << "importCustomMailTransport(const QString &identifierValue, const KConfigGroup &group, int defaultTransport, int transportId) not implemented yet"; } void ImportMailJobInterfaceTestImpl::importSmtpMailTransport(const KConfigGroup &group, int defaultTransport, int transportId) { + SmtpMailTransport smtpMailTransport(group); qDebug() << "ImportMailJobInterfaceTestImpl::importSmtpMailTransport(const KConfigGroup &group, int defaultTransport, int transportId) not implement yet"; qDebug() << " defaultTransport " << defaultTransport << " transportId " << transportId; } ImportMailJobInterfaceTest::ImportMailJobInterfaceTest(QObject *parent) : QObject(parent) { } void ImportMailJobInterfaceTest::importMail_data() { QTest::addColumn("zipFilePath"); QTest::addColumn("testPath"); QTest::addColumn("options"); Utils::StoredTypes options = {Utils::StoredType::Config}; const QByteArray pathConfig(QByteArray(PIMDATAEXPORTER_DIR) + "/import/"); QTest::newRow("mailonlyconfig") << QString::fromLatin1(pathConfig) << QStringLiteral("/mailonlyconfig/") << options; options = {Utils::StoredType::Config|Utils::StoredType::Resources}; QTest::newRow("mailconfigandresource") << QString::fromLatin1(pathConfig) << QStringLiteral("mailconfigandresource/") << options; options = {Utils::StoredType::MailTransport}; QTest::newRow("mailtransport") << QString::fromLatin1(pathConfig) << QStringLiteral("mailtransport/") << options; options = {Utils::StoredType::Identity}; QTest::newRow("identities") << QString::fromLatin1(pathConfig) << QStringLiteral("identities/") << options; } void ImportMailJobInterfaceTest::importMail() { QFETCH(QString, zipFilePath); QFETCH(QString, testPath); QFETCH(Utils::StoredTypes, options); TestImportFile *file = new TestImportFile(zipFilePath + testPath, this); file->setPathConfig(zipFilePath + testPath); file->setExtractPath(QDir::tempPath() + QLatin1Char('/') + testPath); file->setExcludePath(Utils::mailsPath()); // ??? ImportMailJobInterfaceTestImpl *impl = new ImportMailJobInterfaceTestImpl(this, options, file->archiveStorage(), 1); impl->setPathConfig(file->pathConfig()); file->setAbstractImportExportJob(impl); file->setLoggingFilePath(impl->loggingFilePath()); file->start(); delete impl; delete file; } diff --git a/core/mail/importmailjobinterfaceimpl.cpp b/core/mail/importmailjobinterfaceimpl.cpp index 27343d8..3f53eb3 100644 --- a/core/mail/importmailjobinterfaceimpl.cpp +++ b/core/mail/importmailjobinterfaceimpl.cpp @@ -1,214 +1,166 @@ /* Copyright (C) 2020 Laurent Montel This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "importmailjobinterfaceimpl.h" #include "resourceconverterimpl.h" #include "archivestorage.h" #include #include #include #include #include #include +#include "smtpmailtransport.h" using namespace Akonadi; ImportMailJobInterfaceImpl::ImportMailJobInterfaceImpl(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep) : ImportMailJobInterface(parent, typeSelected, archiveStorage, numberOfStep) , mIdentityManager(KIdentityManagement::IdentityManager::self()) { } ImportMailJobInterfaceImpl::~ImportMailJobInterfaceImpl() { } QString ImportMailJobInterfaceImpl::adaptResourcePath(const KSharedConfigPtr &resourceConfig, const QString &storedData) { ResourceConverterImpl converter; return converter.adaptResourcePath(resourceConfig, storedData); } void ImportMailJobInterfaceImpl::registerSpecialCollection(Akonadi::SpecialMailCollections::Type type, qint64 colId) { auto fetch = new Akonadi::CollectionFetchJob(Akonadi::Collection(colId), Akonadi::CollectionFetchJob::Base, this); connect(fetch, &Akonadi::CollectionFetchJob::collectionsReceived, this, [ type](const Akonadi::Collection::List &cols) { if (cols.count() != 1) { return; } Akonadi::SpecialMailCollections::self()->registerCollection(type, cols.first()); }); } QString ImportMailJobInterfaceImpl::createResource(const QString &resources, const QString &name, const QMap &settings, bool synchronizeTree) { return mCreateResource->createResource(resources, name, settings, synchronizeTree); } void ImportMailJobInterfaceImpl::synchronizeResource(const QStringList &lst) { //It's maildir support. Need to add support startSynchronizeResources(lst); } void ImportMailJobInterfaceImpl::importFilters(const QVector &filters) { MailCommon::FilterManager::instance()->appendFilters(filters); } Collection::Id ImportMailJobInterfaceImpl::convertFolderPathToCollectionId(const QString &path) { ResourceConverterImpl converter; return converter.convertFolderPathToCollectionId(path); } QString ImportMailJobInterfaceImpl::adaptNewResourceUrl(bool overwriteResources, const KSharedConfig::Ptr &resourceConfig, const QString &storePath) { ResourceConverterImpl converter; return converter.adaptNewResourceUrl(overwriteResources, resourceConfig, storePath); } void ImportMailJobInterfaceImpl::addNewIdentity(const QString &name, KConfigGroup &group, int defaultIdentity, int oldUid) { KIdentityManagement::Identity *identity = &mIdentityManager->newFromScratch(uniqueIdentityName(name)); group.writeEntry(QStringLiteral("Name"), name); group.sync(); identity->readConfig(group); if (oldUid != -1) { mHashIdentity.insert(oldUid, identity->uoid()); if (oldUid == defaultIdentity) { mIdentityManager->setAsDefault(identity->uoid()); } } mIdentityManager->commit(); } QString ImportMailJobInterfaceImpl::uniqueIdentityName(const QString &name) { QString newName(name); int i = 0; while (!mIdentityManager->isUnique(newName)) { newName = QStringLiteral("%1_%2").arg(name).arg(i); ++i; } return newName; } void ImportMailJobInterfaceImpl::importCustomMailTransport(const QString &identifierValue, const KConfigGroup &group, int defaultTransport, int transportId) { if (!identifierValue.isEmpty()) { if (identifierValue == QLatin1String("sendmail") || identifierValue == QLatin1String("akonadi_ewsmta_resource")) { MailTransport::Transport *mt = MailTransport::TransportManager::self()->createTransport(); mt->setName(group.readEntry(QStringLiteral("name"))); const QString hostStr(QStringLiteral("host")); if (group.hasKey(hostStr)) { mt->setHost(group.readEntry(hostStr)); } mt->setIdentifier(identifierValue); addMailTransport(mt, defaultTransport, transportId); } else { qCWarning(PIMDATAEXPORTERCORE_LOG) << "Unknown identifier type " << identifierValue; } } else { qCWarning(PIMDATAEXPORTERCORE_LOG) << "identifier value is empty"; } } void ImportMailJobInterfaceImpl::importSmtpMailTransport(const KConfigGroup &group, int defaultTransport, int transportId) { + SmtpMailTransport smtpMailTransport(group); MailTransport::Transport *mt = MailTransport::TransportManager::self()->createTransport(); - mt->setName(group.readEntry(QStringLiteral("name"))); - const QString hostStr(QStringLiteral("host")); - if (group.hasKey(hostStr)) { - mt->setHost(group.readEntry(hostStr)); - } - const QString portStr(QStringLiteral("port")); - if (group.hasKey(portStr)) { - mt->setPort(group.readEntry(portStr, -1)); - } - const QString userNameStr(QStringLiteral("user")); - if (group.hasKey(userNameStr)) { - mt->setUserName(group.readEntry(userNameStr)); - } - const QString precommandStr(QStringLiteral("precommand")); - if (group.hasKey(precommandStr)) { - mt->setPrecommand(group.readEntry(precommandStr)); - } - const QString requiresAuthenticationStr(QStringLiteral("auth")); - if (group.hasKey(requiresAuthenticationStr)) { - mt->setRequiresAuthentication(group.readEntry(requiresAuthenticationStr, false)); - } - const QString specifyHostnameStr(QStringLiteral("specifyHostname")); - if (group.hasKey(specifyHostnameStr)) { - mt->setSpecifyHostname(group.readEntry(specifyHostnameStr, false)); - } - const QString localHostnameStr(QStringLiteral("localHostname")); - if (group.hasKey(localHostnameStr)) { - mt->setLocalHostname(group.readEntry(localHostnameStr)); - } - const QString specifySenderOverwriteAddressStr(QStringLiteral("specifySenderOverwriteAddress")); - if (group.hasKey(specifySenderOverwriteAddressStr)) { - mt->setSpecifySenderOverwriteAddress(group.readEntry(specifySenderOverwriteAddressStr, false)); - } - const QString storePasswordStr(QStringLiteral("storepass")); - if (group.hasKey(storePasswordStr)) { - mt->setStorePassword(group.readEntry(storePasswordStr, false)); - } - const QString senderOverwriteAddressStr(QStringLiteral("senderOverwriteAddress")); - if (group.hasKey(senderOverwriteAddressStr)) { - mt->setSenderOverwriteAddress(group.readEntry(senderOverwriteAddressStr)); - } - const QString encryptionStr(QStringLiteral("encryption")); - if (group.hasKey(encryptionStr)) { - const QString encryptionType = group.readEntry(encryptionStr, QString()); - if (!encryptionType.isEmpty()) { - if (encryptionType == QLatin1String("TLS")) { - mt->setEncryption(static_cast(MailTransport::TransportBase::EnumEncryption::TLS)); - } else if (encryptionType == QLatin1String("SSL")) { - mt->setEncryption(static_cast(MailTransport::TransportBase::EnumEncryption::SSL)); - } else if (encryptionType == QLatin1String("None")) { - mt->setEncryption(static_cast(MailTransport::TransportBase::EnumEncryption::None)); - } else { - qCWarning(PIMDATAEXPORTERCORE_LOG) << "Unknown encryption type " << encryptionType; - } - } else { - qCWarning(PIMDATAEXPORTERCORE_LOG) << "Encryption type is empty. It's a bug"; - } - mt->setEncryption(group.readEntry(encryptionStr, 1)); //TODO verify - } - const QString authenticationTypeStr(QStringLiteral("authtype")); - if (group.hasKey(authenticationTypeStr)) { - mt->setAuthenticationType(group.readEntry(authenticationTypeStr, 1)); //TODO verify - } + mt->setName(smtpMailTransport.name()); + mt->setHost(smtpMailTransport.host()); + mt->setPort(smtpMailTransport.port()); + mt->setUserName(smtpMailTransport.userName()); + mt->setPrecommand(smtpMailTransport.precommand()); + mt->setRequiresAuthentication(smtpMailTransport.requiresAuthentication()); + mt->setSpecifyHostname(smtpMailTransport.specifyHostname()); + mt->setLocalHostname(smtpMailTransport.localHostname()); + mt->setSpecifySenderOverwriteAddress(smtpMailTransport.specifySenderOverwriteAddress()); + mt->setStorePassword(smtpMailTransport.storePassword()); + mt->setSenderOverwriteAddress(smtpMailTransport.senderOverwriteAddress()); + mt->setEncryption(smtpMailTransport.encryption()); + mt->setAuthenticationType(smtpMailTransport.authenticationType()); addMailTransport(mt, defaultTransport, transportId); } void ImportMailJobInterfaceImpl::addMailTransport(MailTransport::Transport *mt, int defaultTransport, int transportId) { mt->forceUniqueName(); mt->save(); MailTransport::TransportManager::self()->addTransport(mt); if (transportId == defaultTransport) { MailTransport::TransportManager::self()->setDefaultTransport(mt->id()); } mHashTransport.insert(transportId, mt->id()); qDebug() << " void ImportMailJobInterface::addMailTransport(MailTransport::Transport *mt, int defaultTransport, int transportId)" << transportId; }