File indexing completed on 2024-12-22 05:05:24

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "importmailjobinterfacetestimpl.h"
0008 #include "archivestorage.h"
0009 #include "importmailfolderattributejobtestimpl.h"
0010 #include "mail/smtpmailtransport.h"
0011 #include "resourceconvertertest.h"
0012 #include "testimportfile.h"
0013 #include "utils.h"
0014 #include <KConfigGroup>
0015 #include <KIdentityManagementCore/Identity>
0016 #include <QTest>
0017 
0018 #define REMOVE_TEMPORARY_DIRECTORIES 1
0019 
0020 ImportMailJobInterfaceTestImpl::ImportMailJobInterfaceTestImpl(QObject *parent,
0021                                                                Utils::StoredTypes typeSelected,
0022                                                                ArchiveStorage *archiveStorage,
0023                                                                int numberOfStep)
0024     : ImportMailJobInterface(parent, typeSelected, archiveStorage, numberOfStep)
0025 {
0026 }
0027 
0028 ImportMailJobInterfaceTestImpl::~ImportMailJobInterfaceTestImpl()
0029 {
0030 #ifdef REMOVE_TEMPORARY_DIRECTORIES
0031     QVERIFY(QDir(extractPath()).removeRecursively());
0032     QVERIFY(QDir(QDir::tempPath() + QLatin1Char('/') + Utils::storeMails()).removeRecursively());
0033 #endif
0034 }
0035 
0036 void ImportMailJobInterfaceTestImpl::start()
0037 {
0038     QDir().mkpath(extractPath());
0039     ImportMailJobInterface::start();
0040 }
0041 
0042 void ImportMailJobInterfaceTestImpl::setExistingPathConfig(const QString &path)
0043 {
0044     mExistingPathConfig = path;
0045 }
0046 
0047 Akonadi::Collection::Id ImportMailJobInterfaceTestImpl::convertFolderPathToCollectionId(const QString &path)
0048 {
0049     ResourceConverterTest resourceConverterTest;
0050     resourceConverterTest.setTestPath(mPathConfig);
0051     return resourceConverterTest.convertFolderPathToCollectionId(path);
0052 }
0053 
0054 void ImportMailJobInterfaceTestImpl::synchronizeResource(const QStringList &lst)
0055 {
0056     mLogCreateResource->logSynchronizeResource(lst);
0057     slotNextStep();
0058 }
0059 
0060 QString
0061 ImportMailJobInterfaceTestImpl::createResource(const QString &resources, const QString &name, const QMap<QString, QVariant> &settings, bool synchronizeTree)
0062 {
0063     return mLogCreateResource->logCreateResource(resources, name, settings, synchronizeTree);
0064 }
0065 
0066 void ImportMailJobInterfaceTestImpl::registerSpecialCollection(Akonadi::SpecialMailCollections::Type type, qint64 colId)
0067 {
0068     mLogCreateResource->logRegisterSpecialCollection(type, colId);
0069 }
0070 
0071 void ImportMailJobInterfaceTestImpl::importFilters(const QString &filename)
0072 {
0073     // qDebug() << " void ImportMailJobInterfaceTestImpl::importFilters(const QString &filename)" << filename << " extractpath " << extractPath();
0074     QFile f(filename);
0075     QDir dir(extractPath());
0076     QVERIFY(dir.mkpath(QStringLiteral("config")));
0077     QVERIFY(f.copy(extractPath() + QStringLiteral("/config/filters")));
0078 }
0079 
0080 QString ImportMailJobInterfaceTestImpl::adaptResourcePath(const KSharedConfigPtr &resourceConfig, const QString &storedData)
0081 {
0082     ResourceConverterTest resourceConverterTest;
0083     resourceConverterTest.setTestPath(mPathConfig);
0084     return resourceConverterTest.adaptResourcePath(resourceConfig, storedData);
0085 }
0086 
0087 QString ImportMailJobInterfaceTestImpl::adaptNewResourceUrl(bool overwriteResources, const KSharedConfig::Ptr &resourceConfig, const QString &storePath)
0088 {
0089     ResourceConverterTest resourceConverterTest;
0090     resourceConverterTest.setTestPath(mPathConfig);
0091     return resourceConverterTest.adaptNewResourceUrl(overwriteResources, resourceConfig, storePath);
0092 }
0093 
0094 void ImportMailJobInterfaceTestImpl::addNewIdentity(const QString &name, KConfigGroup &group, int defaultIdentities, int oldUid)
0095 {
0096     // qDebug() << " void ImportMailJobInterfaceTestImpl::addNewIdentity(const QString &name, KConfigGroup &group, int defaultIdentities, int oldUid) not
0097     // implemented yet" << oldUid << " name " << name;
0098     const QString uniqueName = uniqueIdentityName(name);
0099     KIdentityManagementCore::Identity identity;
0100     uint value = mIdentityUoid;
0101     mIdentityUoid++;
0102     identity.setUoid(value);
0103     group.writeEntry(QStringLiteral("Name"), uniqueName);
0104     group.sync();
0105 
0106     KConfig config(extractPath() + QStringLiteral("/identities/emailidentities"));
0107 
0108     identity.readConfig(group);
0109 
0110     if (oldUid != -1) {
0111         mHashIdentity.insert(oldUid, identity.uoid());
0112         if (oldUid == defaultIdentities) {
0113             KConfigGroup grpGeneral = config.group(QStringLiteral("General"));
0114             grpGeneral.writeEntry("Default Identity", identity.uoid());
0115         }
0116     }
0117     KConfigGroup grp = config.group(QStringLiteral("Identity #%1").arg(value - 1));
0118     identity.writeConfig(grp);
0119 }
0120 
0121 QString ImportMailJobInterfaceTestImpl::uniqueIdentityName(const QString &name)
0122 {
0123     QString newName(name);
0124     int i = 0;
0125 
0126     const QStringList existingIdentityNames{QStringLiteral("identity1"), QStringLiteral("identity2")};
0127 
0128     while (existingIdentityNames.contains(newName)) {
0129         newName = QStringLiteral("%1_%2").arg(name).arg(i);
0130         ++i;
0131     }
0132     return newName;
0133 }
0134 
0135 void ImportMailJobInterfaceTestImpl::importCustomMailTransport(const QString &identifierValue, const KConfigGroup &group, int defaultTransport, int transportId)
0136 {
0137     Q_UNUSED(identifierValue);
0138     Q_UNUSED(group);
0139     Q_UNUSED(defaultTransport);
0140     Q_UNUSED(transportId);
0141     qDebug()
0142         << "importCustomMailTransport(const QString &identifierValue, const KConfigGroup &group, int defaultTransport, int transportId) not implemented yet";
0143 }
0144 
0145 void ImportMailJobInterfaceTestImpl::importSmtpMailTransport(const SmtpMailTransport &smtpMailTransport, int defaultTransport, int transportId)
0146 {
0147     // qDebug() << " defaultTransport " << defaultTransport << " transportId " << transportId;
0148     KConfig config(extractPath() + QStringLiteral("/config/mailtransports"));
0149     const int transportValue = mMailTransportId;
0150     mMailTransportId++;
0151     KConfigGroup grp = config.group(QStringLiteral("Transport %1").arg(transportValue));
0152 
0153     const auto name = smtpMailTransport.name();
0154     grp.writeEntry(QStringLiteral("name"), name);
0155     const auto host = smtpMailTransport.host();
0156     grp.writeEntry(QStringLiteral("host"), host);
0157     const auto port = smtpMailTransport.port();
0158     grp.writeEntry(QStringLiteral("port"), port);
0159     const auto userName = smtpMailTransport.userName();
0160     grp.writeEntry(QStringLiteral("userName"), userName);
0161     const auto precommand = smtpMailTransport.precommand();
0162     grp.writeEntry(QStringLiteral("precommand"), precommand);
0163     const auto requiresAuthentication = smtpMailTransport.requiresAuthentication();
0164     grp.writeEntry(QStringLiteral("requiresAuthentication"), requiresAuthentication);
0165     const auto specifyHostname = smtpMailTransport.specifyHostname();
0166     grp.writeEntry(QStringLiteral("specifyHostname"), specifyHostname);
0167     const auto localHostname = smtpMailTransport.localHostname();
0168     grp.writeEntry(QStringLiteral("localHostname"), localHostname);
0169     const auto specifySenderOverwriteAddress = smtpMailTransport.specifySenderOverwriteAddress();
0170     grp.writeEntry(QStringLiteral("specifySenderOverwriteAddress"), specifySenderOverwriteAddress);
0171     const auto storePassword = smtpMailTransport.storePassword();
0172     grp.writeEntry(QStringLiteral("storePassword"), storePassword);
0173     const auto senderOverwriteAddress = smtpMailTransport.senderOverwriteAddress();
0174     grp.writeEntry(QStringLiteral("senderOverwriteAddress"), senderOverwriteAddress);
0175     const auto encryption = smtpMailTransport.encryption();
0176     grp.writeEntry(QStringLiteral("encryption"), encryption);
0177     const auto authenticationType = smtpMailTransport.authenticationType();
0178     grp.writeEntry(QStringLiteral("authenticationType"), authenticationType);
0179 
0180     if (transportId == defaultTransport) {
0181         KConfigGroup generalGrp = config.group(QStringLiteral("General"));
0182         generalGrp.writeEntry(QStringLiteral("default-transport"), transportValue);
0183     }
0184     mHashTransport.insert(transportId, transportValue);
0185 }
0186 
0187 QString ImportMailJobInterfaceTestImpl::configLocation() const
0188 {
0189     qDebug() << " QString ImportMailJobInterfaceTestImpl::configLocation() const " << mExistingPathConfig;
0190     return mExistingPathConfig;
0191 }
0192 
0193 void ImportMailJobInterfaceTestImpl::importFolderAttributes()
0194 {
0195     auto job = new ImportMailFolderAttributeJobTestImpl(this);
0196     job->setArchiveDirectory(mArchiveDirectory);
0197     job->setExtractPath(extractPath());
0198     job->setExportInterface(this);
0199     connect(job, &ImportMailFolderAttributeJobTestImpl::successed, this, [this]() {
0200         emitInfo(QStringLiteral("Backing up Folder Attributes done."));
0201         Q_EMIT jobFinished();
0202     });
0203     connect(job, &ImportMailFolderAttributeJobTestImpl::failed, this, [this]() {
0204         Q_EMIT error(QStringLiteral("Folder Attributes cannot be exported."));
0205         Q_EMIT jobFinished();
0206     });
0207     job->start();
0208 }
0209 
0210 #include "moc_importmailjobinterfacetestimpl.cpp"