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

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 "importmailjobinterface.h"
0008 #include "archivestorage.h"
0009 #include "importexportmailutil.h"
0010 #include "pimdataexportcore_debug.h"
0011 #include "resourceconverterimpl.h"
0012 #include "smtpmailtransport.h"
0013 
0014 #include <MailCommon/MailUtil>
0015 
0016 #include <MailTransport/TransportManager>
0017 
0018 #include <KArchiveFile>
0019 #include <KConfigGroup>
0020 #include <KLocalizedString>
0021 #include <KMessageBox>
0022 #include <KSharedConfig>
0023 #include <KZip>
0024 
0025 #include <QDir>
0026 #include <QMetaMethod>
0027 #include <QRegularExpression>
0028 #include <QStandardPaths>
0029 #include <QTimer>
0030 
0031 using namespace Akonadi;
0032 
0033 ImportMailJobInterface::ImportMailJobInterface(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep)
0034     : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep)
0035 {
0036     initializeImportJob();
0037 }
0038 
0039 ImportMailJobInterface::~ImportMailJobInterface() = default;
0040 
0041 void ImportMailJobInterface::start()
0042 {
0043     Q_EMIT title(i18n("Starting to import KMail settings..."));
0044     createProgressDialog(i18n("Import KMail settings"));
0045     mArchiveDirectory = archive()->directory();
0046     searchAllMailsFiles(mArchiveDirectory, QString());
0047     if (!mFileList.isEmpty() || !mListResourceFile.isEmpty()) {
0048         initializeListStep();
0049         QTimer::singleShot(0, this, &ImportMailJobInterface::slotNextStep);
0050     } else {
0051         Q_EMIT jobFinished();
0052     }
0053 }
0054 
0055 void ImportMailJobInterface::slotNextStep()
0056 {
0057     ++mIndex;
0058     if (mIndex < mListStep.count()) {
0059         const Utils::StoredType type = mListStep.at(mIndex);
0060         if (type == Utils::MailTransport) {
0061             restoreTransports();
0062         } else if (type == Utils::Mails) {
0063             restoreMails();
0064         } else if (type == Utils::Resources) {
0065             restoreResources();
0066         } else if (type == Utils::Identity) {
0067             restoreIdentity();
0068         } else if (type == Utils::Config) {
0069             restoreConfig();
0070         } else {
0071             qCDebug(PIMDATAEXPORTERCORE_LOG) << Q_FUNC_INFO << " not supported type " << type;
0072             slotNextStep();
0073         }
0074     } else {
0075         // Reapply attribute at the end (after resource syncing)
0076         if (mListStep.contains(Utils::Mails)) {
0077             importFolderAttributes();
0078         } else {
0079             Q_EMIT jobFinished();
0080         }
0081     }
0082 }
0083 
0084 void ImportMailJobInterface::searchAllMailsFiles(const KArchiveDirectory *dir, const QString &prefix)
0085 {
0086     const QStringList lst = dir->entries();
0087     for (const QString &entryName : lst) {
0088         const KArchiveEntry *entry = dir->entry(entryName);
0089         if (entry && entry->isDirectory()) {
0090             const QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName;
0091             if (entryName == QLatin1StringView("mails")) {
0092                 storeMailArchiveResource(static_cast<const KArchiveDirectory *>(entry), entryName);
0093             } else {
0094                 searchAllMailsFiles(static_cast<const KArchiveDirectory *>(entry), newPrefix);
0095             }
0096         } else if (entry) {
0097             const QString fileName = prefix.isEmpty() ? entry->name() : prefix + QLatin1Char('/') + entry->name();
0098             mFileList << fileName;
0099         }
0100     }
0101     std::sort(mFileList.begin(), mFileList.end());
0102 }
0103 
0104 void ImportMailJobInterface::storeMailArchiveResource(const KArchiveDirectory *dir, const QString &prefix)
0105 {
0106     const QStringList lst = dir->entries();
0107     for (const QString &entryName : lst) {
0108         const KArchiveEntry *entry = dir->entry(entryName);
0109         if (entry && entry->isDirectory()) {
0110             const auto resourceDir = static_cast<const KArchiveDirectory *>(entry);
0111             const QStringList lstResourceDirEntries = resourceDir->entries();
0112             if (lstResourceDirEntries.count() >= 2) {
0113                 const QString archPath(prefix + QLatin1Char('/') + entryName + QLatin1Char('/'));
0114                 ResourceFiles files;
0115                 for (const QString &name : lstResourceDirEntries) {
0116                     if (name.endsWith(QLatin1StringView("rc"))
0117                         && (name.contains(QLatin1StringView("akonadi_mbox_resource_")) || name.contains(QLatin1StringView("akonadi_mixedmaildir_resource_"))
0118                             || name.contains(QLatin1StringView("akonadi_maildir_resource_")))) {
0119                         files.akonadiConfigFile = archPath + name;
0120                     } else if (name.startsWith(Utils::prefixAkonadiConfigFile())) {
0121                         files.akonadiAgentConfigFile = archPath + name;
0122                     } else {
0123                         files.akonadiResources = archPath + name;
0124                     }
0125                 }
0126                 // Show debug:
0127                 files.debug();
0128                 mListResourceFile.append(files);
0129             } else {
0130                 qCDebug(PIMDATAEXPORTERCORE_LOG) << " Problem in archive. number of file " << lstResourceDirEntries.count();
0131             }
0132         }
0133     }
0134     std::sort(mListResourceFile.begin(), mListResourceFile.end());
0135 }
0136 
0137 void ImportMailJobInterface::importMailTransport(const QString &tempDirName)
0138 {
0139     KSharedConfig::Ptr transportConfig = KSharedConfig::openConfig(tempDirName + QLatin1Char('/') + QStringLiteral("mailtransports"));
0140 
0141     int defaultTransport = -1;
0142     if (transportConfig->hasGroup(QStringLiteral("General"))) {
0143         KConfigGroup group = transportConfig->group(QStringLiteral("General"));
0144         defaultTransport = group.readEntry(QStringLiteral("default-transport"), -1);
0145     }
0146 
0147     QStringList transportList = transportConfig->groupList().filter(QRegularExpression(QStringLiteral("Transport \\d+")));
0148     std::sort(transportList.begin(), transportList.end());
0149     for (const QString &transport : std::as_const(transportList)) {
0150         KConfigGroup group = transportConfig->group(transport);
0151         const int transportId = group.readEntry(QStringLiteral("id"), -1);
0152         if (transportId == -1) {
0153             qCWarning(PIMDATAEXPORTERCORE_LOG) << "Mail Transport is incorrect. Missing Id";
0154             continue;
0155         }
0156         const QString identifierStr(QStringLiteral("identifier"));
0157         if (group.hasKey(identifierStr)) {
0158             const QString identifierValue = group.readEntry(identifierStr);
0159             importCustomMailTransport(identifierValue, group, defaultTransport, transportId);
0160         } else {
0161             SmtpMailTransport smtpMailTransport(group);
0162             importSmtpMailTransport(smtpMailTransport, defaultTransport, transportId);
0163         }
0164     }
0165 }
0166 
0167 void ImportMailJobInterface::restoreTransports()
0168 {
0169     setProgressDialogLabel(i18n("Restore transports..."));
0170     increaseProgressDialog();
0171     const QString path = Utils::transportsPath() + QStringLiteral("mailtransports");
0172     if (!mFileList.contains(path)) {
0173         emitError(i18n("mailtransports file could not be found in the archive."));
0174     } else {
0175         emitInfo(i18n("Restore transports..."));
0176 
0177         const KArchiveEntry *transport = mArchiveDirectory->entry(path);
0178         bool importDone = false;
0179         if (transport && transport->isFile()) {
0180             const auto fileTransport = static_cast<const KArchiveFile *>(transport);
0181 
0182             importDone = copyArchiveFileTo(fileTransport, mTempDirName);
0183             if (importDone) {
0184                 importMailTransport(mTempDirName);
0185             }
0186         }
0187         if (importDone) {
0188             emitInfo(i18n("Transports restored."));
0189         } else {
0190             emitError(i18n("Failed to restore transports file."));
0191         }
0192     }
0193     QTimer::singleShot(0, this, &ImportMailJobInterface::slotNextStep);
0194 }
0195 
0196 void ImportMailJobInterface::restoreResources()
0197 {
0198     increaseProgressDialog();
0199     setProgressDialogLabel(i18n("Restore resources..."));
0200     emitInfo(i18n("Restore resources..."));
0201 
0202     QDir dir(mTempDirName);
0203     dir.mkdir(Utils::resourcesPath());
0204     ResourceConverterImpl converter;
0205     QStringList listResourceToSync;
0206     for (const QString &filename : std::as_const(mFileList)) {
0207         // We need to find akonadi_* and agent_config_akonadi_*
0208         if (filename.startsWith(Utils::resourcesPath() + QStringLiteral("akonadi_"))) {
0209             const QString agentFileConfigName = converter.agentFileName(filename);
0210             QString resourceName;
0211             if (mFileList.contains(agentFileConfigName)) {
0212                 // Parse config file => get name
0213                 const KArchiveEntry *agentFileConfigEntry = mArchiveDirectory->entry(agentFileConfigName);
0214                 if (agentFileConfigEntry && agentFileConfigEntry->isFile()) {
0215                     const auto file = static_cast<const KArchiveFile *>(agentFileConfigEntry);
0216                     const QString destDirectory = mTempDirName + QLatin1Char('/') + Utils::resourcesPath();
0217                     // qDebug() << " destDirectory " << destDirectory;
0218                     copyArchiveFileTo(file, destDirectory);
0219                     const QString filename(file->name());
0220                     const QString agentResourceFileName = destDirectory + QLatin1Char('/') + filename;
0221                     resourceName = Utils::akonadiAgentName(agentResourceFileName);
0222                 }
0223             }
0224             const KArchiveEntry *fileEntry = mArchiveDirectory->entry(filename);
0225             if (fileEntry && fileEntry->isFile()) {
0226                 const auto file = static_cast<const KArchiveFile *>(fileEntry);
0227                 const QString destDirectory = mTempDirName + QLatin1Char('/') + Utils::resourcesPath();
0228 
0229                 copyArchiveFileTo(file, destDirectory);
0230 
0231                 const QString filename(file->name());
0232                 const QString resourceFileName = destDirectory + QLatin1Char('/') + filename;
0233                 KSharedConfig::Ptr resourceConfig = KSharedConfig::openConfig(resourceFileName);
0234                 QMap<QString, QVariant> settings;
0235                 if (filename.contains(QLatin1StringView("pop3"))) {
0236                     KConfigGroup general = resourceConfig->group(QStringLiteral("General"));
0237                     if (general.hasKey(QStringLiteral("login"))) {
0238                         settings.insert(QStringLiteral("Login"), general.readEntry("login"));
0239                     }
0240                     if (general.hasKey(QStringLiteral("host"))) {
0241                         settings.insert(QStringLiteral("Host"), general.readEntry("host"));
0242                     }
0243                     if (general.hasKey(QStringLiteral("port"))) {
0244                         settings.insert(QStringLiteral("Port"), general.readEntry("port", 110));
0245                     }
0246                     if (general.hasKey(QStringLiteral("authenticationMethod"))) {
0247                         settings.insert(QStringLiteral("AuthenticationMethod"), general.readEntry("authenticationMethod", 7));
0248                     }
0249                     if (general.hasKey(QStringLiteral("useSSL"))) {
0250                         settings.insert(QStringLiteral("UseSSL"), general.readEntry("useSSL", false));
0251                     }
0252                     if (general.hasKey(QStringLiteral("useTLS"))) {
0253                         settings.insert(QStringLiteral("UseTLS"), general.readEntry("useTLS", false));
0254                     }
0255                     if (general.hasKey(QStringLiteral("pipelining"))) {
0256                         settings.insert(QStringLiteral("Pipelining"), general.readEntry("pipelining", false));
0257                     }
0258                     if (general.hasKey(QStringLiteral("leaveOnServer"))) {
0259                         settings.insert(QStringLiteral("LeaveOnServer"), general.readEntry("leaveOnServer", false));
0260                     }
0261                     if (general.hasKey(QStringLiteral("leaveOnServerDays"))) {
0262                         settings.insert(QStringLiteral("LeaveOnServerDays"), general.readEntry("leaveOnServerDays", -1));
0263                     }
0264                     if (general.hasKey(QStringLiteral("leaveOnServerCount"))) {
0265                         settings.insert(QStringLiteral("LeaveOnServerCount"), general.readEntry("leaveOnServerCount", -1));
0266                     }
0267                     if (general.hasKey(QStringLiteral("leaveOnServerSize"))) {
0268                         settings.insert(QStringLiteral("LeaveOnServerSize"), general.readEntry("leaveOnServerSize", -1));
0269                     }
0270                     if (general.hasKey(QStringLiteral("filterOnServer"))) {
0271                         settings.insert(QStringLiteral("FilterOnServer"), general.readEntry("filterOnServer", false));
0272                     }
0273                     if (general.hasKey(QStringLiteral("filterCheckSize"))) {
0274                         settings.insert(QStringLiteral("FilterCheckSize"), general.readEntry("filterCheckSize"));
0275                     }
0276                     if (general.hasKey(QStringLiteral("targetCollection"))) {
0277                         const Akonadi::Collection::Id collection = convertPathToId(general.readEntry("targetCollection"));
0278                         if (collection != -1) {
0279                             settings.insert(QStringLiteral("TargetCollection"), collection);
0280                         }
0281                     }
0282                     if (general.hasKey(QStringLiteral("precommand"))) {
0283                         settings.insert(QStringLiteral("Precommand"), general.readEntry("precommand"));
0284                     }
0285                     if (general.hasKey(QStringLiteral("intervalCheckEnabled"))) {
0286                         settings.insert(QStringLiteral("IntervalCheckEnabled"), general.readEntry("intervalCheckEnabled", false));
0287                     }
0288                     if (general.hasKey(QStringLiteral("intervalCheckInterval"))) {
0289                         settings.insert(QStringLiteral("IntervalCheckInterval"), general.readEntry("intervalCheckInterval", 5));
0290                     }
0291 
0292                     KConfigGroup leaveOnserver = resourceConfig->group(QStringLiteral("LeaveOnServer"));
0293 
0294                     if (leaveOnserver.hasKey(QStringLiteral("seenUidList"))) {
0295                         settings.insert(QStringLiteral("SeenUidList"), leaveOnserver.readEntry("seenUidList", QStringList()));
0296                     }
0297 #if 0
0298                     if (leaveOnserver.hasKey(QStringLiteral("seenUidTimeList"))) {
0299                         //FIXME
0300                         //settings.insert(QLatin1StringView("SeenUidTimeList"),QVariant::fromValue<QList<int> >(leaveOnserver.readEntry("seenUidTimeList",QList<int>())));
0301                     }
0302 #endif
0303                     if (leaveOnserver.hasKey(QStringLiteral("downloadLater"))) {
0304                         settings.insert(QStringLiteral("DownloadLater"), leaveOnserver.readEntry("downloadLater", QStringList()));
0305                     }
0306                     const QString newResourceName = resourceName.isEmpty() ? filename : resourceName;
0307                     const QString newResource = createResource(QStringLiteral("akonadi_pop3_resource"), newResourceName, settings, false);
0308                     if (!newResource.isEmpty()) {
0309                         mHashResources.insert(filename, newResource);
0310                         infoAboutNewResource(newResource);
0311                         listResourceToSync << newResource;
0312                     }
0313                 } else if (filename.contains(QLatin1StringView("imap")) || filename.contains(QLatin1StringView("kolab_"))
0314                            || filename.contains(QLatin1StringView("gmail_"))) {
0315                     KConfigGroup network = resourceConfig->group(QStringLiteral("network"));
0316                     if (network.hasKey(QStringLiteral("Authentication"))) {
0317                         settings.insert(QStringLiteral("Authentication"), network.readEntry("Authentication", 1));
0318                     }
0319                     if (network.hasKey(QStringLiteral("ImapPort"))) {
0320                         settings.insert(QStringLiteral("ImapPort"), network.readEntry("ImapPort", 993));
0321                     }
0322                     if (network.hasKey(QStringLiteral("ImapServer"))) {
0323                         settings.insert(QStringLiteral("ImapServer"), network.readEntry("ImapServer"));
0324                     }
0325                     if (network.hasKey(QStringLiteral("Safety"))) {
0326                         settings.insert(QStringLiteral("Safety"), network.readEntry("Safety", "SSL"));
0327                     }
0328                     if (network.hasKey(QStringLiteral("SubscriptionEnabled"))) {
0329                         settings.insert(QStringLiteral("SubscriptionEnabled"), network.readEntry("SubscriptionEnabled", false));
0330                     }
0331                     if (network.hasKey(QStringLiteral("UserName"))) {
0332                         settings.insert(QStringLiteral("UserName"), network.readEntry("UserName"));
0333                     }
0334 
0335                     if (network.hasKey(QStringLiteral("SessionTimeout"))) {
0336                         settings.insert(QStringLiteral("SessionTimeout"), network.readEntry("SessionTimeout", 30));
0337                     }
0338 
0339                     KConfigGroup cache = resourceConfig->group(QStringLiteral("cache"));
0340 
0341                     if (cache.hasKey(QStringLiteral("AccountIdentity"))) {
0342                         const int identity = cache.readEntry("AccountIdentity", -1);
0343                         if (identity != -1) {
0344                             if (mHashIdentity.contains(identity)) {
0345                                 settings.insert(QStringLiteral("AccountIdentity"), mHashIdentity.value(identity));
0346                             } else {
0347                                 settings.insert(QStringLiteral("AccountIdentity"), identity);
0348                             }
0349                         }
0350                     }
0351                     if (cache.hasKey(QStringLiteral("IntervalCheckEnabled"))) {
0352                         settings.insert(QStringLiteral("IntervalCheckEnabled"), cache.readEntry("IntervalCheckEnabled", true));
0353                     }
0354                     if (cache.hasKey(QStringLiteral("RetrieveMetadataOnFolderListing"))) {
0355                         settings.insert(QStringLiteral("RetrieveMetadataOnFolderListing"), cache.readEntry("RetrieveMetadataOnFolderListing", true));
0356                     }
0357                     if (cache.hasKey(QStringLiteral("AutomaticExpungeEnabled"))) {
0358                         settings.insert(QStringLiteral("AutomaticExpungeEnabled"), cache.readEntry("AutomaticExpungeEnabled", true));
0359                     }
0360                     if (cache.hasKey(QStringLiteral("DisconnectedModeEnabled"))) {
0361                         settings.insert(QStringLiteral("DisconnectedModeEnabled"), cache.readEntry("DisconnectedModeEnabled", false));
0362                     }
0363                     if (cache.hasKey(QStringLiteral("IntervalCheckTime"))) {
0364                         settings.insert(QStringLiteral("IntervalCheckTime"), cache.readEntry("IntervalCheckTime", -1));
0365                     }
0366                     if (cache.hasKey(QStringLiteral("UseDefaultIdentity"))) {
0367                         settings.insert(QStringLiteral("UseDefaultIdentity"), cache.readEntry("UseDefaultIdentity", true));
0368                     }
0369                     if (cache.hasKey(QStringLiteral("TrashCollection"))) {
0370                         const Akonadi::Collection::Id collection = convertPathToId(cache.readEntry("TrashCollection"));
0371                         if (collection != -1) {
0372                             settings.insert(QStringLiteral("TrashCollection"), collection);
0373                         } else {
0374                             qCDebug(PIMDATAEXPORTERCORE_LOG) << " Use default trash folder";
0375                         }
0376                     }
0377 
0378                     KConfigGroup siever = resourceConfig->group(QStringLiteral("siever"));
0379                     if (siever.hasKey(QStringLiteral("SieveSupport"))) {
0380                         settings.insert(QStringLiteral("SieveSupport"), siever.readEntry("SieveSupport", false));
0381                     }
0382                     if (siever.hasKey(QStringLiteral("SieveReuseConfig"))) {
0383                         settings.insert(QStringLiteral("SieveReuseConfig"), siever.readEntry("SieveReuseConfig", true));
0384                     }
0385                     if (siever.hasKey(QStringLiteral("SievePort"))) {
0386                         settings.insert(QStringLiteral("SievePort"), siever.readEntry("SievePort", 4190));
0387                     }
0388                     if (siever.hasKey(QStringLiteral("SieveAlternateUrl"))) {
0389                         settings.insert(QStringLiteral("SieveAlternateUrl"), siever.readEntry("SieveAlternateUrl"));
0390                     }
0391                     if (siever.hasKey(QStringLiteral("AlternateAuthentication"))) {
0392                         settings.insert(QStringLiteral("AlternateAuthentication"), siever.readEntry("AlternateAuthentication"));
0393                     }
0394                     if (siever.hasKey(QStringLiteral("SieveVacationFilename"))) {
0395                         settings.insert(QStringLiteral("SieveVacationFilename"), siever.readEntry("SieveVacationFilename"));
0396                     }
0397                     if (siever.hasKey(QStringLiteral("SieveCustomUsername"))) {
0398                         settings.insert(QStringLiteral("SieveCustomUsername"), siever.readEntry("SieveCustomUsername"));
0399                     }
0400                     if (siever.hasKey(QStringLiteral("SieveCustomAuthentification"))) {
0401                         settings.insert(QStringLiteral("SieveCustomAuthentification"), siever.readEntry("SieveCustomAuthentification"));
0402                     }
0403 
0404                     QString newResource;
0405                     const QString newResourceName = resourceName.isEmpty() ? filename : resourceName;
0406                     if (filename.contains(QLatin1StringView("kolab_"))) {
0407                         newResource = createResource(QStringLiteral("akonadi_kolab_resource"), newResourceName, settings, true);
0408                     } else if (filename.contains(QLatin1StringView("gmail_"))) {
0409                         newResource = createResource(QStringLiteral("akonadi_gmail_resource"), newResourceName, settings, true);
0410                     } else {
0411                         newResource = createResource(QStringLiteral("akonadi_imap_resource"), newResourceName, settings, true);
0412                     }
0413                     if (!newResource.isEmpty()) {
0414                         mHashResources.insert(filename, newResource);
0415                         infoAboutNewResource(newResource);
0416                         listResourceToSync << newResource;
0417                     }
0418                 } else {
0419                     qCWarning(PIMDATAEXPORTERCORE_LOG) << " problem with resource: " << filename;
0420                 }
0421             }
0422         }
0423     }
0424     emitInfo(i18n("Resources restored."));
0425     synchronizeResource(listResourceToSync);
0426 }
0427 
0428 QString ImportMailJobInterface::applicationName() const
0429 {
0430     return QStringLiteral("[KMail]");
0431 }
0432 
0433 void ImportMailJobInterface::restoreMails()
0434 {
0435     increaseProgressDialog();
0436     setProgressDialogLabel(i18n("Restore mails..."));
0437     QStringList listResourceToSync;
0438     emitInfo(i18n("Restore mails..."));
0439 
0440     QDir dir(mTempDirName);
0441     dir.mkdir(Utils::mailsPath());
0442     const QString copyToDirName(mTempDirName + QLatin1Char('/') + Utils::mailsPath());
0443     QDir().mkpath(copyToDirName);
0444     const int numberOfResourceFile = mListResourceFile.size();
0445     for (int i = 0; i < numberOfResourceFile; ++i) {
0446         ResourceFiles value = mListResourceFile.at(i);
0447         value.debug();
0448         const QString resourceFile = value.akonadiConfigFile;
0449         const KArchiveEntry *fileResouceEntry = mArchiveDirectory->entry(resourceFile);
0450         if (fileResouceEntry && fileResouceEntry->isFile()) {
0451             const auto file = static_cast<const KArchiveFile *>(fileResouceEntry);
0452             copyArchiveFileTo(file, copyToDirName);
0453             QString resourceName(file->name());
0454             QString filename(file->name());
0455             // qCDebug(PIMDATAEXPORTERCORE_LOG)<<" filename "<<filename<<"
0456             // resourceName"<<resourceName;
0457             KSharedConfig::Ptr resourceConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + resourceName);
0458 
0459             const QString newUrl = adaptResourcePath(resourceConfig, Utils::storeMails());
0460 
0461             const QString agentConfigFile = value.akonadiAgentConfigFile;
0462             if (!agentConfigFile.isEmpty()) {
0463                 const KArchiveEntry *akonadiAgentConfigEntry = mArchiveDirectory->entry(agentConfigFile);
0464                 if (akonadiAgentConfigEntry->isFile()) {
0465                     const auto fileEntry = static_cast<const KArchiveFile *>(akonadiAgentConfigEntry);
0466                     copyArchiveFileTo(fileEntry, copyToDirName);
0467                     resourceName = fileEntry->name();
0468                     filename = Utils::akonadiAgentName(copyToDirName + QLatin1Char('/') + resourceName);
0469                 }
0470             }
0471 
0472             QMap<QString, QVariant> settings;
0473             if (resourceName.contains(QLatin1StringView("akonadi_mbox_resource_"))) {
0474                 const QString dataFile = value.akonadiResources;
0475                 const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(dataFile);
0476                 if (dataResouceEntry->isFile()) {
0477                     const auto file = static_cast<const KArchiveFile *>(dataResouceEntry);
0478                     copyArchiveFileTo(file, newUrl);
0479                 }
0480                 settings.insert(QStringLiteral("Path"), newUrl);
0481 
0482                 KConfigGroup general = resourceConfig->group(QStringLiteral("General"));
0483                 if (general.hasKey(QStringLiteral("DisplayName"))) {
0484                     settings.insert(QStringLiteral("DisplayName"), general.readEntry(QStringLiteral("DisplayName")));
0485                 }
0486                 if (general.hasKey(QStringLiteral("ReadOnly"))) {
0487                     settings.insert(QStringLiteral("ReadOnly"), general.readEntry(QStringLiteral("ReadOnly"), false));
0488                 }
0489                 if (general.hasKey(QStringLiteral("MonitorFile"))) {
0490                     settings.insert(QStringLiteral("MonitorFile"), general.readEntry(QStringLiteral("MonitorFile"), false));
0491                 }
0492                 if (resourceConfig->hasGroup(QStringLiteral("Locking"))) {
0493                     KConfigGroup locking = resourceConfig->group(QStringLiteral("Locking"));
0494                     if (locking.hasKey(QStringLiteral("Lockfile"))) {
0495                         settings.insert(QStringLiteral("Lockfile"), locking.readEntry(QStringLiteral("Lockfile")));
0496                     }
0497                     // TODO verify
0498                     if (locking.hasKey(QStringLiteral("LockfileMethod"))) {
0499                         settings.insert(QStringLiteral("LockfileMethod"), locking.readEntry(QStringLiteral("LockfileMethod"), 4));
0500                     }
0501                 }
0502                 if (resourceConfig->hasGroup(QStringLiteral("Compacting"))) {
0503                     KConfigGroup compacting = resourceConfig->group(QStringLiteral("Compacting"));
0504                     if (compacting.hasKey(QStringLiteral("CompactFrequency"))) {
0505                         settings.insert(QStringLiteral("CompactFrequency"), compacting.readEntry(QStringLiteral("CompactFrequency"), 1));
0506                     }
0507                     if (compacting.hasKey(QStringLiteral("MessageCount"))) {
0508                         settings.insert(QStringLiteral("MessageCount"), compacting.readEntry(QStringLiteral("MessageCount"), 50));
0509                     }
0510                 }
0511                 const QString newResource = createResource(QStringLiteral("akonadi_mbox_resource"), filename, settings, true);
0512                 if (!newResource.isEmpty()) {
0513                     mHashResources.insert(filename, newResource);
0514                     infoAboutNewResource(newResource);
0515                 }
0516             } else if (resourceName.contains(QLatin1StringView("akonadi_maildir_resource_"))
0517                        || resourceName.contains(QLatin1StringView("akonadi_mixedmaildir_resource_"))) {
0518                 settings.insert(QStringLiteral("Path"), newUrl);
0519                 KConfigGroup general = resourceConfig->group(QStringLiteral("General"));
0520                 if (general.hasKey(QStringLiteral("TopLevelIsContainer"))) {
0521                     settings.insert(QStringLiteral("TopLevelIsContainer"), general.readEntry(QStringLiteral("TopLevelIsContainer"), false));
0522                 }
0523                 if (general.hasKey(QStringLiteral("ReadOnly"))) {
0524                     settings.insert(QStringLiteral("ReadOnly"), general.readEntry(QStringLiteral("ReadOnly"), false));
0525                 }
0526                 if (general.hasKey(QStringLiteral("MonitorFilesystem"))) {
0527                     settings.insert(QStringLiteral("MonitorFilesystem"), general.readEntry(QStringLiteral("MonitorFilesystem"), true));
0528                 }
0529 
0530                 const QString newResource =
0531                     createResource(resourceName.contains(QLatin1StringView("akonadi_mixedmaildir_resource_")) ? QStringLiteral("akonadi_mixedmaildir_resource")
0532                                                                                                               : QStringLiteral("akonadi_maildir_resource"),
0533                                    filename,
0534                                    settings,
0535                                    true);
0536                 if (!newResource.isEmpty()) {
0537                     mHashResources.insert(filename, newResource);
0538                     infoAboutNewResource(newResource);
0539                 }
0540 
0541                 const QString mailFile = value.akonadiResources;
0542                 const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(mailFile);
0543                 if (dataResouceEntry && dataResouceEntry->isFile()) {
0544                     const auto fileEntry = static_cast<const KArchiveFile *>(dataResouceEntry);
0545                     // TODO Fix me not correct zip filename.
0546                     extractZipFile(fileEntry, copyToDirName, newUrl);
0547                 }
0548                 listResourceToSync << newResource;
0549             } else {
0550                 qCDebug(PIMDATAEXPORTERCORE_LOG) << " resource name not supported " << resourceName;
0551             }
0552             // qCDebug(PIMDATAEXPORTERCORE_LOG)<<"url "<<url;
0553         }
0554     }
0555     emitInfo(i18n("Mails restored."));
0556     synchronizeResource(listResourceToSync);
0557 }
0558 
0559 void ImportMailJobInterface::restoreConfig()
0560 {
0561     increaseProgressDialog();
0562     setProgressDialogLabel(i18n("Restore config..."));
0563     const QString filtersPath(Utils::configsPath() + QStringLiteral("filters"));
0564     if (!mFileList.contains(filtersPath)) {
0565         emitError(i18n("filters file could not be found in the archive."));
0566     } else {
0567         const KArchiveEntry *filter = mArchiveDirectory->entry(filtersPath);
0568         if (filter && filter->isFile()) {
0569             emitInfo(i18n("Restore filters..."));
0570             const auto fileFilter = static_cast<const KArchiveFile *>(filter);
0571 
0572             copyArchiveFileTo(fileFilter, mTempDirName);
0573             const QString filterFileName(mTempDirName + QLatin1Char('/') + QStringLiteral("filters"));
0574             KSharedConfig::Ptr filtersConfig = KSharedConfig::openConfig(filterFileName);
0575             const QStringList filterList = filtersConfig->groupList().filter(QRegularExpression(QStringLiteral("Filter #\\d+")));
0576             for (const QString &filterStr : filterList) {
0577                 KConfigGroup group = filtersConfig->group(filterStr);
0578                 const QString accountStr(QStringLiteral("accounts-set"));
0579                 if (group.hasKey(accountStr)) {
0580                     const QString accounts = group.readEntry(accountStr);
0581                     if (!accounts.isEmpty()) {
0582                         const QStringList lstAccounts = accounts.split(QLatin1Char(','));
0583                         QStringList newLstAccounts;
0584                         for (const QString &acc : lstAccounts) {
0585                             if (mHashResources.contains(acc)) {
0586                                 newLstAccounts.append(mHashResources.value(acc));
0587                             } else {
0588                                 newLstAccounts.append(acc);
0589                             }
0590                         }
0591                         group.writeEntry(accountStr, newLstAccounts);
0592                     }
0593                 }
0594                 const int numActions = group.readEntry("actions", 0);
0595                 for (int i = 0; i < numActions; ++i) {
0596                     const QString actName = QStringLiteral("action-name-%1").arg(i);
0597                     const QString argsName = QStringLiteral("action-args-%1").arg(i);
0598                     const QString actValue = group.readEntry(actName);
0599                     if (actValue == QLatin1StringView("set identity")) {
0600                         const int argsValue = group.readEntry(argsName, -1);
0601                         if (argsValue != -1) {
0602                             if (mHashIdentity.contains(argsValue)) {
0603                                 group.writeEntry(argsName, mHashIdentity.value(argsValue));
0604                             }
0605                         }
0606                     } else if (actValue == QLatin1StringView("set transport")) {
0607                         const int argsValue = group.readEntry(argsName, -1);
0608                         if (argsValue != -1) {
0609                             if (mHashTransport.contains(argsValue)) {
0610                                 group.writeEntry(argsName, mHashTransport.value(argsValue));
0611                             }
0612                         }
0613                     }
0614                 }
0615             }
0616             filtersConfig->sync();
0617             importFilters(filterFileName);
0618             emitInfo(i18n("Filters restored."));
0619         }
0620     }
0621     const QString kmailsnippetrcStr(QStringLiteral("kmailsnippetrc"));
0622     const KArchiveEntry *kmailsnippetentry = mArchiveDirectory->entry(Utils::configsPath() + kmailsnippetrcStr);
0623     if (kmailsnippetentry && kmailsnippetentry->isFile()) {
0624         const auto kmailsnippet = static_cast<const KArchiveFile *>(kmailsnippetentry);
0625         const QString kmailsnippetrc = configLocation() + kmailsnippetrcStr;
0626         if (QFileInfo::exists(kmailsnippetrc)) {
0627             // TODO 4.13 allow to merge config.
0628             if (overwriteConfigMessageBox(kmailsnippetrcStr)) {
0629                 copyToFile(kmailsnippet, kmailsnippetrc, kmailsnippetrcStr, Utils::configsPath());
0630             }
0631         } else {
0632             copyToFile(kmailsnippet, kmailsnippetrc, kmailsnippetrcStr, Utils::configsPath());
0633         }
0634     }
0635 
0636     {
0637         const QString labldaprcStr(QStringLiteral("kabldaprc"));
0638         const KArchiveEntry *kabldapentry = mArchiveDirectory->entry(Utils::configsPath() + labldaprcStr);
0639         if (kabldapentry && kabldapentry->isFile()) {
0640             const auto kabldap = static_cast<const KArchiveFile *>(kabldapentry);
0641             const QString kabldaprc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + labldaprcStr;
0642             if (QFileInfo::exists(kabldaprc)) {
0643                 const int result = mergeConfigMessageBox(labldaprcStr);
0644                 if (result == KMessageBox::ButtonCode::PrimaryAction) {
0645                     copyToFile(kabldap, kabldaprc, labldaprcStr, Utils::configsPath());
0646                 } else if (result == KMessageBox::ButtonCode::SecondaryAction) {
0647                     mergeLdapConfig(kabldap, labldaprcStr, Utils::configsPath());
0648                 }
0649             } else {
0650                 copyToFile(kabldap, kabldaprc, labldaprcStr, Utils::configsPath());
0651             }
0652         }
0653     }
0654     {
0655         const QString archiveconfigurationrcStr(QStringLiteral("akonadi_archivemail_agentrc"));
0656         const KArchiveEntry *archiveconfigurationentry = mArchiveDirectory->entry(Utils::configsPath() + archiveconfigurationrcStr);
0657         if (archiveconfigurationentry && archiveconfigurationentry->isFile()) {
0658             const auto archiveconfiguration = static_cast<const KArchiveFile *>(archiveconfigurationentry);
0659             const QString archiveconfigurationrc =
0660                 QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + archiveconfigurationrcStr;
0661             if (QFileInfo::exists(archiveconfigurationrc)) {
0662                 const int result = mergeConfigMessageBox(archiveconfigurationrcStr);
0663                 if (result == KMessageBox::ButtonCode::PrimaryAction) {
0664                     importArchiveConfig(archiveconfiguration, archiveconfigurationrc, archiveconfigurationrcStr, Utils::configsPath());
0665                 } else if (result == KMessageBox::ButtonCode::SecondaryAction) {
0666                     mergeArchiveMailAgentConfig(archiveconfiguration, archiveconfigurationrcStr, Utils::configsPath());
0667                 }
0668             } else {
0669                 importArchiveConfig(archiveconfiguration, archiveconfigurationrc, archiveconfigurationrcStr, Utils::configsPath());
0670             }
0671         }
0672     }
0673 
0674     {
0675         const QString folderMailArchiveStr(QStringLiteral("foldermailarchiverc"));
0676         const KArchiveEntry *archivemailentry = mArchiveDirectory->entry(Utils::configsPath() + folderMailArchiveStr);
0677         if (archivemailentry && archivemailentry->isFile()) {
0678             const auto archiveconfiguration = static_cast<const KArchiveFile *>(archivemailentry);
0679             const QString archiveconfigurationrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + folderMailArchiveStr;
0680             if (QFileInfo::exists(archiveconfigurationrc)) {
0681                 const int result = mergeConfigMessageBox(folderMailArchiveStr);
0682                 if (result == KMessageBox::ButtonCode::PrimaryAction) {
0683                     importMailArchiveConfig(archiveconfiguration, archiveconfigurationrc, folderMailArchiveStr, Utils::configsPath());
0684                 } else if (result == KMessageBox::ButtonCode::SecondaryAction) {
0685                     mergeMailArchiveConfig(archiveconfiguration, folderMailArchiveStr, Utils::configsPath());
0686                 }
0687             } else {
0688                 importMailArchiveConfig(archiveconfiguration, archiveconfigurationrc, folderMailArchiveStr, Utils::configsPath());
0689             }
0690         }
0691     }
0692 
0693     {
0694         const QString unifiedMailBoxStr(QStringLiteral("akonadi_unifiedmailbox_agentrc"));
0695         const KArchiveEntry *unifiedMailBoxEntry = mArchiveDirectory->entry(Utils::configsPath() + unifiedMailBoxStr);
0696         if (unifiedMailBoxEntry && unifiedMailBoxEntry->isFile()) {
0697             const auto archiveconfiguration = static_cast<const KArchiveFile *>(unifiedMailBoxEntry);
0698             const QString unifiedMailBoxrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + unifiedMailBoxStr;
0699             if (QFileInfo::exists(unifiedMailBoxrc)) {
0700                 const int result = mergeConfigMessageBox(unifiedMailBoxStr);
0701                 if (result == KMessageBox::ButtonCode::PrimaryAction) {
0702                     importUnifiedMailBoxConfig(archiveconfiguration, unifiedMailBoxrc, unifiedMailBoxStr, Utils::configsPath());
0703                 } else if (result == KMessageBox::ButtonCode::SecondaryAction) {
0704                     mergeUnifiedMailBoxConfig(archiveconfiguration, unifiedMailBoxStr, Utils::configsPath());
0705                 }
0706             } else {
0707                 importUnifiedMailBoxConfig(archiveconfiguration, unifiedMailBoxrc, unifiedMailBoxStr, Utils::configsPath());
0708             }
0709         }
0710     }
0711 
0712     {
0713         const QString templatesconfigurationrcStr(QStringLiteral("templatesconfigurationrc"));
0714         const KArchiveEntry *templatesconfigurationentry = mArchiveDirectory->entry(Utils::configsPath() + templatesconfigurationrcStr);
0715         if (templatesconfigurationentry && templatesconfigurationentry->isFile()) {
0716             const auto templatesconfiguration = static_cast<const KArchiveFile *>(templatesconfigurationentry);
0717             const QString templatesconfigurationrc =
0718                 QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + templatesconfigurationrcStr;
0719 
0720             if (QFileInfo::exists(templatesconfigurationrc)) {
0721                 // TODO 4.13 allow to merge config.
0722                 if (overwriteConfigMessageBox(templatesconfigurationrcStr)) {
0723                     importTemplatesConfig(templatesconfiguration, templatesconfigurationrc, templatesconfigurationrcStr, Utils::configsPath());
0724                 }
0725             } else {
0726                 importTemplatesConfig(templatesconfiguration, templatesconfigurationrc, templatesconfigurationrcStr, Utils::configsPath());
0727             }
0728         }
0729     }
0730     const QString kmailStr(QStringLiteral("kmail2rc"));
0731     const KArchiveEntry *kmail2rcentry = mArchiveDirectory->entry(Utils::configsPath() + kmailStr);
0732     if (kmail2rcentry && kmail2rcentry->isFile()) {
0733         const auto kmailrc = static_cast<const KArchiveFile *>(kmail2rcentry);
0734         const QString kmail2rc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kmailStr;
0735         if (QFileInfo::exists(kmail2rc)) {
0736             if (overwriteConfigMessageBox(kmailStr)) {
0737                 importKmailConfig(kmailrc, kmail2rc, kmailStr, Utils::configsPath());
0738             }
0739         } else {
0740             importKmailConfig(kmailrc, kmail2rc, kmailStr, Utils::configsPath());
0741         }
0742     }
0743 
0744     const QString sievetemplatercStr(QStringLiteral("sievetemplaterc"));
0745     const KArchiveEntry *sievetemplatentry = mArchiveDirectory->entry(Utils::configsPath() + sievetemplatercStr);
0746     if (sievetemplatentry && sievetemplatentry->isFile()) {
0747         const auto sievetemplateconfiguration = static_cast<const KArchiveFile *>(sievetemplatentry);
0748         const QString sievetemplaterc = configLocation() + sievetemplatercStr;
0749         if (QFileInfo::exists(sievetemplaterc)) {
0750             const int result = mergeConfigMessageBox(sievetemplatercStr);
0751             if (result == KMessageBox::ButtonCode::PrimaryAction) {
0752                 copyToFile(sievetemplateconfiguration, sievetemplaterc, sievetemplatercStr, Utils::configsPath());
0753             } else if (result == KMessageBox::ButtonCode::SecondaryAction) {
0754                 mergeSieveTemplate(sievetemplateconfiguration, sievetemplatercStr, Utils::configsPath());
0755             }
0756         } else {
0757             copyToFile(sievetemplateconfiguration, sievetemplaterc, sievetemplatercStr, Utils::configsPath());
0758         }
0759     }
0760     {
0761         const QString customTemplateStr(QStringLiteral("customtemplatesrc"));
0762         const KArchiveEntry *customtemplatentry = mArchiveDirectory->entry(Utils::configsPath() + customTemplateStr);
0763         if (customtemplatentry && customtemplatentry->isFile()) {
0764             const auto customtemplateconfiguration = static_cast<const KArchiveFile *>(customtemplatentry);
0765             const QString customtemplaterc = configLocation() + customTemplateStr;
0766             if (QFileInfo::exists(customtemplaterc)) {
0767                 // TODO 4.13 allow to merge config.
0768                 if (overwriteConfigMessageBox(customTemplateStr)) {
0769                     copyToFile(customtemplateconfiguration, customtemplaterc, customTemplateStr, Utils::configsPath());
0770                 }
0771             } else {
0772                 copyToFile(customtemplateconfiguration, customtemplaterc, customTemplateStr, Utils::configsPath());
0773             }
0774         }
0775     }
0776 
0777     {
0778         const QString adblockStr(QStringLiteral("messagevieweradblockrc"));
0779         const KArchiveEntry *adblockentry = mArchiveDirectory->entry(Utils::configsPath() + adblockStr);
0780         if (adblockentry && adblockentry->isFile()) {
0781             const auto adblockconfiguration = static_cast<const KArchiveFile *>(adblockentry);
0782             const QString adblockrc = configLocation() + adblockStr;
0783             if (QFileInfo::exists(adblockrc)) {
0784                 // TODO 4.13 allow to merge config.
0785                 if (overwriteConfigMessageBox(adblockStr)) {
0786                     copyToFile(adblockconfiguration, adblockrc, adblockStr, Utils::configsPath());
0787                 }
0788             } else {
0789                 copyToFile(adblockconfiguration, adblockrc, adblockStr, Utils::configsPath());
0790             }
0791         }
0792     }
0793 
0794     restoreUiRcFile(QStringLiteral("sieveeditorui.rc"), QStringLiteral("sieveeditor"));
0795     restoreUiRcFile(QStringLiteral("headerthemeeditorui.rc"), QStringLiteral("headerthemeeditor"));
0796     restoreUiRcFile(QStringLiteral("contactthemeeditorui.rc"), QStringLiteral("contactthemeeditor"));
0797     restoreUiRcFile(QStringLiteral("contactprintthemeeditorui.rc"), QStringLiteral("contactprintthemeeditor"));
0798 
0799     restoreUiRcFile(QStringLiteral("kmreadermainwin.rc"), QStringLiteral("kmail2"));
0800     restoreUiRcFile(QStringLiteral("kmcomposerui.rc"), QStringLiteral("kmail2"));
0801     restoreUiRcFile(QStringLiteral("kmmainwin.rc"), QStringLiteral("kmail2"));
0802     restoreUiRcFile(QStringLiteral("kmail_part.rc"), QStringLiteral("kmail2"));
0803     restoreUiRcFile(QStringLiteral("kontactui.rc"), QStringLiteral("kontact"));
0804     restoreUiRcFile(QStringLiteral("kleopatra.rc"), QStringLiteral("kleopatra"));
0805     restoreUiRcFile(QStringLiteral("kontactsummary_part.rc"), QStringLiteral("kontactsummary"));
0806     restoreUiRcFile(QStringLiteral("kwatchgnupgui.rc"), QStringLiteral("kwatchgnupg"));
0807     restoreUiRcFile(QStringLiteral("akonadiconsoleui.rc"), QStringLiteral("akonadiconsole"));
0808 
0809     restoreConfigFile(QStringLiteral("kontactrc"));
0810 
0811     restoreConfigFile(QStringLiteral("kontact_summaryrc"));
0812     restoreConfigFile(QStringLiteral("storageservicerc"));
0813     restoreConfigFile(QStringLiteral("kpimbalooblacklist"));
0814     restoreConfigFile(QStringLiteral("kleopatrarc"));
0815     restoreConfigFile(QStringLiteral("sieveeditorrc"));
0816     restoreConfigFile(QStringLiteral("kwatchgnupgrc"));
0817     restoreConfigFile(QStringLiteral("pimpluginsrc"));
0818     restoreConfigFile(QStringLiteral("texttospeechrc"));
0819     restoreConfigFile(QStringLiteral("kleopatracertificateselectiondialogrc"));
0820     restoreConfigFile(QStringLiteral("dkimsettingsrc"));
0821     restoreConfigFile(QStringLiteral("confirmbeforedeletingrc"));
0822 
0823     // Restore notify file
0824     const QStringList lstNotify = {
0825         QStringLiteral("akonadi_mailfilter_agent.notifyrc"),
0826         QStringLiteral("akonadi_sendlater_agent.notifyrc"),
0827         QStringLiteral("akonadi_archivemail_agent.notifyrc"),
0828         QStringLiteral("kmail2.notifyrc"),
0829         QStringLiteral("akonadi_newmailnotifier_agent.notifyrc"),
0830         QStringLiteral("akonadi_maildispatcher_agent.notifyrc"),
0831         QStringLiteral("akonadi_followupreminder_agent.notifyrc"),
0832         QStringLiteral("messageviewer.notifyrc"),
0833     };
0834 
0835     // We can't merge it.
0836     for (const QString &filename : lstNotify) {
0837         restoreConfigFile(filename);
0838     }
0839 
0840     importSimpleFilesInDirectory(QStringLiteral("/autocorrect/"));
0841     importSimpleFilesInDirectory(QStringLiteral("/gravatar/"));
0842     const KArchiveEntry *kmail2Entry = mArchiveDirectory->entry(Utils::dataPath() + QStringLiteral("kmail2/adblockrules_local"));
0843     if (kmail2Entry && kmail2Entry->isFile()) {
0844         const auto entry = static_cast<const KArchiveFile *>(kmail2Entry);
0845         const QString adblockPath =
0846             QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QStringLiteral("kmail2/adblockrules_local");
0847         if (QFileInfo::exists(adblockPath)) {
0848             if (overwriteConfigMessageBox(QStringLiteral("adblockrules_local"))) {
0849                 copyToFile(entry,
0850                            QString(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/")
0851                                    + QStringLiteral("kmail2/adblockrules_local")),
0852                            QStringLiteral("adblockrules_local"),
0853                            Utils::dataPath() + QStringLiteral("kmail2/"));
0854             }
0855         } else {
0856             copyToFile(entry,
0857                        QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QStringLiteral("kmail2/adblockrules_local"),
0858                        QStringLiteral("adblockrules_local"),
0859                        Utils::dataPath() + QStringLiteral("kmail2/"));
0860         }
0861     }
0862 
0863     importDataSubdirectory(QStringLiteral("/messageviewer/themes/"));
0864     importDataSubdirectory(QStringLiteral("/messageviewerplugins/"));
0865 
0866     emitInfo(i18n("Config restored."));
0867     QTimer::singleShot(0, this, &ImportMailJobInterface::slotNextStep);
0868 }
0869 
0870 void ImportMailJobInterface::importSimpleFilesInDirectory(const QString &relativePath)
0871 {
0872     const KArchiveEntry *autocorrectionEntry = mArchiveDirectory->entry(Utils::dataPath() + relativePath);
0873     if (autocorrectionEntry && autocorrectionEntry->isDirectory()) {
0874         const auto autoCorrectionDir = static_cast<const KArchiveDirectory *>(autocorrectionEntry);
0875         const QStringList lst = autoCorrectionDir->entries();
0876         for (const QString &entryName : lst) {
0877             const KArchiveEntry *entry = autoCorrectionDir->entry(entryName);
0878             if (entry && entry->isFile()) {
0879                 const auto autocorrectionFile = static_cast<const KArchiveFile *>(entry);
0880                 const QString name = autocorrectionFile->name();
0881                 QString autocorrectionPath =
0882                     QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + relativePath + QLatin1Char('/') + name;
0883                 if (QFileInfo::exists(autocorrectionPath)) {
0884                     if (overwriteConfigMessageBox(name)) {
0885                         copyToFile(autocorrectionFile, autocorrectionPath, name, Utils::dataPath() + relativePath);
0886                     }
0887                 } else {
0888                     autocorrectionPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + relativePath;
0889                     copyToFile(autocorrectionFile, autocorrectionPath + QLatin1Char('/') + name, name, Utils::dataPath() + relativePath);
0890                 }
0891             }
0892         }
0893     }
0894 }
0895 
0896 void ImportMailJobInterface::restoreIdentity()
0897 {
0898     increaseProgressDialog();
0899     setProgressDialogLabel(i18n("Restoring identities..."));
0900     const QString path(Utils::identitiesPath() + QStringLiteral("emailidentities"));
0901     if (!mFileList.contains(path)) {
0902         emitError(i18n("emailidentities file could not be found in the archive."));
0903     } else {
0904         emitInfo(i18n("Restoring identities..."));
0905         const KArchiveEntry *identity = mArchiveDirectory->entry(path);
0906         if (identity && identity->isFile()) {
0907             const auto fileIdentity = static_cast<const KArchiveFile *>(identity);
0908             copyArchiveFileTo(fileIdentity, mTempDirName);
0909             KSharedConfig::Ptr identityConfig = KSharedConfig::openConfig(mTempDirName + QLatin1Char('/') + QStringLiteral("emailidentities"));
0910             KConfigGroup general = identityConfig->group(QStringLiteral("General"));
0911             const int defaultIdentity = general.readEntry(QStringLiteral("Default Identity"), -1);
0912 
0913             QStringList identityList = identityConfig->groupList().filter(QRegularExpression(QStringLiteral("Identity #\\d+")));
0914             std::sort(identityList.begin(), identityList.end());
0915             for (const QString &identityStr : std::as_const(identityList)) {
0916                 KConfigGroup group = identityConfig->group(identityStr);
0917                 int oldUid = -1;
0918                 const QString uidStr(QStringLiteral("uoid"));
0919                 if (group.hasKey(uidStr)) {
0920                     oldUid = group.readEntry(uidStr).toUInt();
0921                     group.deleteEntry(uidStr);
0922                 }
0923                 const QString fcc(QStringLiteral("Fcc"));
0924                 qint64 fccId = convertRealPathToCollection(group, fcc);
0925                 registerSpecialCollection(Akonadi::SpecialMailCollections::SentMail, fccId);
0926 
0927                 const QString draft = QStringLiteral("Drafts");
0928                 qint64 draftId = convertRealPathToCollection(group, draft);
0929                 registerSpecialCollection(Akonadi::SpecialMailCollections::Drafts, draftId);
0930 
0931                 const QString templates = QStringLiteral("Templates");
0932                 qint64 templateId = convertRealPathToCollection(group, templates);
0933                 registerSpecialCollection(Akonadi::SpecialMailCollections::Templates, templateId);
0934 
0935                 if (oldUid != -1) {
0936                     const QString vcard = QStringLiteral("VCardFile");
0937                     if (group.hasKey(vcard)) {
0938                         const QString vcardFileName = group.readEntry(vcard);
0939                         if (!vcardFileName.isEmpty()) {
0940                             const QFileInfo fileInfo(vcardFileName);
0941                             QFile file(vcardFileName);
0942                             const KArchiveEntry *vcardEntry =
0943                                 mArchiveDirectory->entry(Utils::identitiesPath() + QString::number(oldUid) + QLatin1Char('/') + file.fileName());
0944                             if (vcardEntry && vcardEntry->isFile()) {
0945                                 const auto vcardFile = static_cast<const KArchiveFile *>(vcardEntry);
0946                                 QString vcardFilePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
0947                                     + QStringLiteral("/kmail2/%1").arg(fileInfo.fileName());
0948                                 int i = 1;
0949                                 while (QFileInfo::exists(vcardFileName)) {
0950                                     vcardFilePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
0951                                         + QStringLiteral("/kmail2/%1_%2").arg(i).arg(fileInfo.fileName());
0952                                     ++i;
0953                                 }
0954                                 copyArchiveFileTo(vcardFile, QFileInfo(vcardFilePath).absolutePath());
0955                                 group.writeEntry(vcard, vcardFilePath);
0956                             }
0957                         }
0958                     }
0959                 }
0960                 const QString name = group.readEntry(QStringLiteral("Identity"));
0961                 addNewIdentity(name, group, defaultIdentity, oldUid);
0962             }
0963             emitInfo(i18n("Identities restored."));
0964         } else {
0965             emitError(i18n("Failed to restore identity file."));
0966         }
0967     }
0968     QTimer::singleShot(0, this, &ImportMailJobInterface::slotNextStep);
0969 }
0970 
0971 void ImportMailJobInterface::importUnifiedMailBoxConfig(const KArchiveFile *archiveconfiguration,
0972                                                         const QString &archiveconfigurationrc,
0973                                                         const QString &filename,
0974                                                         const QString &prefix)
0975 {
0976     copyToFile(archiveconfiguration, archiveconfigurationrc, filename, prefix);
0977     KSharedConfig::Ptr archiveConfig = KSharedConfig::openConfig(archiveconfigurationrc);
0978 
0979     copyUnifiedMailBoxConfig(archiveConfig, archiveConfig);
0980     archiveConfig->sync();
0981 }
0982 
0983 void ImportMailJobInterface::importMailArchiveConfig(const KArchiveFile *archiveconfiguration,
0984                                                      const QString &archiveconfigurationrc,
0985                                                      const QString &filename,
0986                                                      const QString &prefix)
0987 {
0988     copyToFile(archiveconfiguration, archiveconfigurationrc, filename, prefix);
0989     KSharedConfig::Ptr archiveConfig = KSharedConfig::openConfig(archiveconfigurationrc);
0990 
0991     copyArchiveMailAgentConfigGroup(archiveConfig, archiveConfig);
0992     archiveConfig->sync();
0993 }
0994 
0995 void ImportMailJobInterface::importArchiveConfig(const KArchiveFile *archiveconfiguration,
0996                                                  const QString &archiveconfigurationrc,
0997                                                  const QString &filename,
0998                                                  const QString &prefix)
0999 {
1000     copyToFile(archiveconfiguration, archiveconfigurationrc, filename, prefix);
1001     KSharedConfig::Ptr archiveConfig = KSharedConfig::openConfig(archiveconfigurationrc);
1002 
1003     // TODO fix FolderArchiveAccount
1004     copyArchiveMailAgentConfigGroup(archiveConfig, archiveConfig);
1005     archiveConfig->sync();
1006 }
1007 
1008 void ImportMailJobInterface::importFolderArchiveConfig(const KArchiveFile *archiveconfiguration,
1009                                                        const QString &archiveconfigurationrc,
1010                                                        const QString &filename,
1011                                                        const QString &prefix)
1012 {
1013     copyToFile(archiveconfiguration, archiveconfigurationrc, filename, prefix);
1014     KSharedConfig::Ptr archiveConfig = KSharedConfig::openConfig(archiveconfigurationrc);
1015 
1016     const QStringList archiveList = archiveConfig->groupList().filter(QRegularExpression(QStringLiteral("FolderArchiveAccount ")));
1017 
1018     for (const QString &str : archiveList) {
1019         KConfigGroup oldGroup = archiveConfig->group(str);
1020         const Akonadi::Collection::Id id = convertPathToId(oldGroup.readEntry(QStringLiteral("topLevelCollectionId")));
1021         if (id != -1) {
1022             oldGroup.writeEntry(QStringLiteral("topLevelCollectionId"), id);
1023         }
1024     }
1025 
1026     archiveConfig->sync();
1027 }
1028 
1029 void ImportMailJobInterface::copyMailArchiveConfig(const KSharedConfig::Ptr &archiveConfigOrigin, const KSharedConfig::Ptr &archiveConfigDestination)
1030 {
1031     const QString archiveGroupPattern = QStringLiteral("FolderArchiveAccount ");
1032     const QStringList archiveList = archiveConfigOrigin->groupList().filter(archiveGroupPattern);
1033     for (const QString &str : archiveList) {
1034         const QString resourcename = str.right(str.length() - archiveGroupPattern.length());
1035         if (!resourcename.isEmpty()) {
1036             KConfigGroup oldGroup = archiveConfigOrigin->group(str);
1037             QString newResourceName;
1038             if (mHashResources.contains(resourcename)) {
1039                 newResourceName = mHashResources.value(resourcename);
1040 
1041                 const Akonadi::Collection::Id id = convertPathToId(oldGroup.readEntry(QStringLiteral("topLevelCollectionId")));
1042                 if (id != -1) {
1043                     KConfigGroup newGroup(archiveConfigDestination, archiveGroupPattern + newResourceName);
1044                     oldGroup.copyTo(&newGroup);
1045                     newGroup.writeEntry(QStringLiteral("topLevelCollectionId"), id);
1046                 }
1047             }
1048             oldGroup.deleteGroup();
1049         }
1050     }
1051 }
1052 
1053 void ImportMailJobInterface::copyUnifiedMailBoxConfig(const KSharedConfig::Ptr &archiveConfigOrigin, const KSharedConfig::Ptr &archiveConfigDestination)
1054 {
1055     auto group = archiveConfigOrigin->group(QStringLiteral("UnifiedMailboxes"));
1056     auto groupCopy = archiveConfigDestination->group(QStringLiteral("UnifiedMailboxes"));
1057     const auto boxGroups = group.groupList();
1058     for (const auto &str : boxGroups) {
1059         KConfigGroup oldGroup = group.group(str);
1060         const Akonadi::Collection::Id id = convertPathToId(oldGroup.readEntry(QStringLiteral("collectionId")));
1061         if (id != -1) {
1062             KConfigGroup newGroup = groupCopy.group(str);
1063             oldGroup.copyTo(&newGroup);
1064             newGroup.writeEntry(QStringLiteral("collectionId"), id);
1065         }
1066         const QString sourceKey(QStringLiteral("sources"));
1067         convertRealPathToCollectionList(oldGroup, sourceKey, false);
1068         // oldGroup.deleteGroup();
1069     }
1070 }
1071 
1072 void ImportMailJobInterface::copyArchiveMailAgentConfigGroup(const KSharedConfig::Ptr &archiveConfigOrigin, const KSharedConfig::Ptr &archiveConfigDestination)
1073 {
1074     // adapt id
1075     const QString archiveGroupPattern = QStringLiteral("ArchiveMailCollection ");
1076     const QStringList archiveList = archiveConfigOrigin->groupList().filter(archiveGroupPattern);
1077     for (const QString &str : archiveList) {
1078         const QString path = str.right(str.length() - archiveGroupPattern.length());
1079         if (!path.isEmpty()) {
1080             KConfigGroup oldGroup = archiveConfigOrigin->group(str);
1081             const Akonadi::Collection::Id id = convertPathToId(path);
1082             if (id != -1) {
1083                 KConfigGroup newGroup(archiveConfigDestination, archiveGroupPattern + QString::number(id));
1084                 oldGroup.copyTo(&newGroup);
1085                 newGroup.writeEntry(QStringLiteral("saveCollectionId"), id);
1086                 QString storePath = newGroup.readEntry("storePath", QString());
1087                 if (!QDir(storePath).exists()) {
1088                     newGroup.writeEntry(QStringLiteral("storePath"), QDir::homePath());
1089                 }
1090             }
1091             oldGroup.deleteGroup();
1092         }
1093     }
1094 }
1095 
1096 void ImportMailJobInterface::importTemplatesConfig(const KArchiveFile *templatesconfiguration,
1097                                                    const QString &templatesconfigurationrc,
1098                                                    const QString &filename,
1099                                                    const QString &prefix)
1100 {
1101     copyToFile(templatesconfiguration, templatesconfigurationrc, filename, prefix);
1102     KSharedConfig::Ptr templateConfig = KSharedConfig::openConfig(templatesconfigurationrc);
1103 
1104     // adapt id
1105     const QString templateGroupPattern = QStringLiteral("Templates #");
1106     const QString templateGroupIdentityPattern = QStringLiteral("Templates #IDENTITY_");
1107     const QStringList templateList = templateConfig->groupList().filter(templateGroupPattern);
1108     for (const QString &str : templateList) {
1109         // Identity
1110         if (str.startsWith(templateGroupIdentityPattern)) {
1111             bool found = false;
1112             const int identity = QStringView(str).right(str.length() - templateGroupIdentityPattern.length()).toInt(&found);
1113             if (found) {
1114                 KConfigGroup oldGroup = templateConfig->group(str);
1115                 if (mHashIdentity.contains(identity)) {
1116                     KConfigGroup newGroup(templateConfig, templateGroupPattern + QString::number(mHashIdentity.value(identity)));
1117                     oldGroup.copyTo(&newGroup);
1118                 }
1119                 oldGroup.deleteGroup();
1120             }
1121         } else { // Folder
1122             const QString path = str.right(str.length() - templateGroupPattern.length());
1123             if (!path.isEmpty()) {
1124                 KConfigGroup oldGroup = templateConfig->group(str);
1125                 const Akonadi::Collection::Id id = convertPathToId(path);
1126                 if (id != -1) {
1127                     KConfigGroup newGroup(templateConfig, templateGroupPattern + QString::number(id));
1128                     oldGroup.copyTo(&newGroup);
1129                 }
1130                 oldGroup.deleteGroup();
1131             }
1132         }
1133     }
1134     templateConfig->sync();
1135 }
1136 
1137 void ImportMailJobInterface::importKmailConfig(const KArchiveFile *kmailsnippet, const QString &kmail2rc, const QString &filename, const QString &prefix)
1138 {
1139     copyToFile(kmailsnippet, kmail2rc, filename, prefix);
1140     KSharedConfig::Ptr kmailConfig = KSharedConfig::openConfig(kmail2rc);
1141 
1142     // adapt folder id
1143     const QString folderGroupPattern = QStringLiteral("Folder-");
1144     const QStringList folderList = kmailConfig->groupList().filter(folderGroupPattern);
1145     for (const QString &str : folderList) {
1146         const QString path = str.right(str.length() - folderGroupPattern.length());
1147         if (!path.isEmpty()) {
1148             KConfigGroup oldGroup = kmailConfig->group(str);
1149             ImportExportMailUtil::cleanupFolderSettings(oldGroup);
1150             if (!oldGroup.keyList().isEmpty()) {
1151                 if (oldGroup.hasKey(QStringLiteral("Identity"))) {
1152                     const int identity = oldGroup.readEntry(QStringLiteral("Identity"), -1);
1153                     if (identity != -1) {
1154                         oldGroup.writeEntry(QStringLiteral("Identity"), mHashIdentity.value(identity));
1155                     }
1156                 }
1157                 const Akonadi::Collection::Id id = convertPathToId(path);
1158                 if (id != -1) {
1159                     KConfigGroup newGroup(kmailConfig, folderGroupPattern + QString::number(id));
1160                     oldGroup.copyTo(&newGroup);
1161                 }
1162             }
1163             oldGroup.deleteGroup();
1164         }
1165     }
1166     const QString accountOrder(QStringLiteral("AccountOrder"));
1167     if (kmailConfig->hasGroup(accountOrder)) {
1168         KConfigGroup group = kmailConfig->group(accountOrder);
1169         const QStringList orderList = group.readEntry(QStringLiteral("order"), QStringList());
1170         QStringList newOrderList;
1171         if (!orderList.isEmpty()) {
1172             for (const QString &account : orderList) {
1173                 if (mHashResources.contains(account)) {
1174                     newOrderList.append(mHashResources.value(account));
1175                 } else {
1176                     newOrderList.append(account);
1177                 }
1178             }
1179         }
1180     }
1181 
1182     const QString composerStr(QStringLiteral("Composer"));
1183     if (kmailConfig->hasGroup(composerStr)) {
1184         KConfigGroup composerGroup = kmailConfig->group(composerStr);
1185         const QString previousStr(QStringLiteral("previous-fcc"));
1186         (void)convertRealPathToCollection(composerGroup, previousStr);
1187 
1188         const QString previousIdentityStr(QStringLiteral("previous-identity"));
1189         if (composerGroup.hasKey(previousIdentityStr)) {
1190             const int identityValue = composerGroup.readEntry(previousIdentityStr, -1);
1191             if (identityValue != -1) {
1192                 if (mHashIdentity.contains(identityValue)) {
1193                     composerGroup.writeEntry(previousIdentityStr, mHashIdentity.value(identityValue));
1194                 } else {
1195                     composerGroup.writeEntry(previousIdentityStr, identityValue);
1196                 }
1197             }
1198         }
1199     }
1200 
1201     const QString collectionFolderViewStr(QStringLiteral("CollectionFolderView"));
1202     if (kmailConfig->hasGroup(collectionFolderViewStr)) {
1203         KConfigGroup favoriteGroup = kmailConfig->group(collectionFolderViewStr);
1204         const QString currentKey(QStringLiteral("Current"));
1205         (void)convertRealPathToCollection(favoriteGroup, currentKey, true);
1206 
1207         const QString expensionKey(QStringLiteral("Expansion"));
1208         convertRealPathToCollectionList(favoriteGroup, expensionKey);
1209     }
1210 
1211     convertCollectionListStrToAkonadiId(kmailConfig, QStringLiteral("FavoriteCollections"), QStringLiteral("FavoriteCollectionIds"), false);
1212 
1213     // For favorite id for root collection == 0 and we store only folder => c
1214     convertCollectionListStrToAkonadiId(kmailConfig, QStringLiteral("FavoriteCollectionsOrder"), QStringLiteral("0"), true);
1215 
1216     // Event collection
1217     convertCollectionStrToAkonadiId(kmailConfig, QStringLiteral("Event"), QStringLiteral("LastEventSelectedFolder"));
1218     // Todo collection
1219     convertCollectionStrToAkonadiId(kmailConfig, QStringLiteral("Todo"), QStringLiteral("LastSelectedFolder"));
1220     // Note collection
1221     convertCollectionStrToAkonadiId(kmailConfig, QStringLiteral("Note"), QStringLiteral("LastNoteSelectedFolder"));
1222 
1223     // FolderSelectionDialog collection
1224     convertCollectionListStrToAkonadiId(kmailConfig, QStringLiteral("FolderSelectionDialog"), QStringLiteral("LastSelectedFolder"), false);
1225 
1226     // Convert MessageListTab collection id
1227     const QString messageListPaneStr(QStringLiteral("MessageListPane"));
1228     if (kmailConfig->hasGroup(messageListPaneStr)) {
1229         KConfigGroup messageListPaneGroup = kmailConfig->group(messageListPaneStr);
1230         const int numberOfTab = messageListPaneGroup.readEntry(QStringLiteral("tabNumber"), 0);
1231         for (int i = 0; i < numberOfTab; ++i) {
1232             KConfigGroup messageListPaneTabGroup = kmailConfig->group(QStringLiteral("MessageListTab%1").arg(i));
1233             const QString messageListPaneTabFolderStr(QStringLiteral("collectionId"));
1234             convertRealPathToCollectionList(messageListPaneTabGroup, messageListPaneTabFolderStr, false);
1235         }
1236     }
1237 
1238     // Automatic Add Contacts
1239     QHash<int, uint>::const_iterator i = mHashIdentity.constBegin();
1240     while (i != mHashIdentity.constEnd()) {
1241         // Use old key
1242         const QString groupId = QStringLiteral("Automatic Add Contacts %1").arg(i.key());
1243         if (kmailConfig->hasGroup(groupId)) {
1244             KConfigGroup identityGroup = kmailConfig->group(groupId);
1245 
1246             KConfigGroup newGroup(kmailConfig, QStringLiteral("Automatic Add Contacts %1").arg(i.value()));
1247             identityGroup.copyTo(&newGroup);
1248             const QString automaticAddContactStr(QStringLiteral("Collection"));
1249             convertRealPathToCollectionList(newGroup, automaticAddContactStr, false);
1250             identityGroup.deleteGroup();
1251         }
1252         ++i;
1253     }
1254 
1255     const QString generalStr(QStringLiteral("General"));
1256     if (kmailConfig->hasGroup(generalStr)) {
1257         KConfigGroup generalGroup = kmailConfig->group(generalStr);
1258         // Be sure to delete default domain
1259         const QString defaultDomainStr(QStringLiteral("Default domain"));
1260         if (generalGroup.hasKey(defaultDomainStr)) {
1261             generalGroup.deleteEntry(defaultDomainStr);
1262         }
1263 
1264         const QString startupFolderStr(QStringLiteral("startupFolder"));
1265         convertRealPathToCollection(generalGroup, startupFolderStr);
1266     }
1267 
1268     const QString resourceGroupPattern = QStringLiteral("Resource ");
1269     const QStringList resourceList = kmailConfig->groupList().filter(resourceGroupPattern);
1270     for (const QString &str : resourceList) {
1271         const QString res = str.right(str.length() - resourceGroupPattern.length());
1272         if (!res.isEmpty()) {
1273             KConfigGroup oldGroup = kmailConfig->group(str);
1274             if (mHashResources.contains(res)) {
1275                 KConfigGroup newGroup(kmailConfig, folderGroupPattern + mHashResources.value(res));
1276                 oldGroup.copyTo(&newGroup);
1277             }
1278             oldGroup.deleteGroup();
1279         }
1280     }
1281 
1282     kmailConfig->sync();
1283     emitInfo(i18n("\"%1\" migration done.", filename));
1284 }
1285 
1286 void ImportMailJobInterface::mergeLdapConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix)
1287 {
1288     QDir dir(mTempDirName);
1289     dir.mkdir(prefix);
1290 
1291     const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix);
1292     copyArchiveFileTo(archivefile, copyToDirName);
1293 
1294     KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename);
1295     KConfigGroup grpExisting = existingConfig->group(QStringLiteral("LDAP"));
1296     int existingNumberHosts = grpExisting.readEntry(QStringLiteral("NumHosts"), 0);
1297     int existingNumberSelectedHosts = grpExisting.readEntry(QStringLiteral("NumSelectedHosts"), 0);
1298 
1299     KSharedConfig::Ptr importingLdapConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename);
1300     KConfigGroup grpImporting = importingLdapConfig->group(QStringLiteral("LDAP"));
1301     int importingNumberHosts = grpImporting.readEntry(QStringLiteral("NumHosts"), 0);
1302     int importingNumberSelectedHosts = grpImporting.readEntry(QStringLiteral("NumSelectedHosts"), 0);
1303 
1304     grpExisting.writeEntry(QStringLiteral("NumHosts"), (existingNumberHosts + importingNumberHosts));
1305     grpExisting.writeEntry(QStringLiteral("NumSelectedHosts"), (existingNumberSelectedHosts + importingNumberSelectedHosts));
1306 
1307     for (int i = 0; i < importingNumberSelectedHosts; ++i) {
1308         const QString auth = grpImporting.readEntry(QStringLiteral("SelectedAuth%1").arg(i), QString());
1309         const int numberHosts = existingNumberSelectedHosts + i + 1;
1310         grpExisting.writeEntry(QStringLiteral("SelectedAuth%1").arg(numberHosts), auth);
1311         grpExisting.writeEntry(QStringLiteral("SelectedBase%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedBase%1").arg(i), QString()));
1312         grpExisting.writeEntry(QStringLiteral("SelectedBind%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedBind%1").arg(i), QString()));
1313         grpExisting.writeEntry(QStringLiteral("SelectedHost%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedHost%1").arg(i), QString()));
1314         grpExisting.writeEntry(QStringLiteral("SelectedMech%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedMech%1").arg(i), QString()));
1315         grpExisting.writeEntry(QStringLiteral("SelectedPageSize%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedPageSize%1").arg(i), 0));
1316         grpExisting.writeEntry(QStringLiteral("SelectedPort%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedPort%1").arg(i), 0));
1317         grpExisting.writeEntry(QStringLiteral("SelectedPwdBind%1").arg(numberHosts),
1318                                grpImporting.readEntry(QStringLiteral("SelectedPwdBind%1").arg(i), QString()));
1319         grpExisting.writeEntry(QStringLiteral("SelectedSecurity%1").arg(numberHosts),
1320                                grpImporting.readEntry(QStringLiteral("SelectedSecurity%1").arg(i), QString()));
1321         grpExisting.writeEntry(QStringLiteral("SelectedSizeLimit%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedSizeLimit%1").arg(i), 0));
1322         grpExisting.writeEntry(QStringLiteral("SelectedTimeLimit%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedTimeLimit%1").arg(i), 0));
1323         grpExisting.writeEntry(QStringLiteral("SelectedUser%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedUser%1").arg(i), QString()));
1324         grpExisting.writeEntry(QStringLiteral("SelectedVersion%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SelectedVersion%1").arg(i), 0));
1325         grpExisting.writeEntry(QStringLiteral("SelectedUserFilter%1").arg(numberHosts),
1326                                grpImporting.readEntry(QStringLiteral("SelectedUserFilter%1").arg(i), 0));
1327     }
1328 
1329     for (int i = 0; i < importingNumberHosts; ++i) {
1330         const int numberHosts = existingNumberHosts + i + 1;
1331         grpExisting.writeEntry(QStringLiteral("Auth%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Auth%1").arg(i), QString()));
1332         grpExisting.writeEntry(QStringLiteral("Base%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Base%1").arg(i), QString()));
1333         grpExisting.writeEntry(QStringLiteral("Bind%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Bind%1").arg(i), QString()));
1334         grpExisting.writeEntry(QStringLiteral("Host%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Host%1").arg(i), QString()));
1335         grpExisting.writeEntry(QStringLiteral("Mech%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Mech%1").arg(i), QString()));
1336         grpExisting.writeEntry(QStringLiteral("PageSize%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("PageSize%1").arg(i), 0));
1337         grpExisting.writeEntry(QStringLiteral("Port%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Port%1").arg(i), 0));
1338         grpExisting.writeEntry(QStringLiteral("PwdBind%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("PwdBind%1").arg(i), QString()));
1339         grpExisting.writeEntry(QStringLiteral("Security%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Security%1").arg(i), QString()));
1340         grpExisting.writeEntry(QStringLiteral("SizeLimit%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("SizeLimit%1").arg(i), 0));
1341         grpExisting.writeEntry(QStringLiteral("TimeLimit%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("TimeLimit%1").arg(i), 0));
1342         grpExisting.writeEntry(QStringLiteral("User%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("User%1").arg(i), QString()));
1343         grpExisting.writeEntry(QStringLiteral("Version%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("Version%1").arg(i), 0));
1344         grpExisting.writeEntry(QStringLiteral("UserFilter%1").arg(numberHosts), grpImporting.readEntry(QStringLiteral("UserFilter%1").arg(i), 0));
1345     }
1346 
1347     grpExisting.sync();
1348 }
1349 
1350 void ImportMailJobInterface::mergeUnifiedMailBoxConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix)
1351 {
1352     QDir dir(mTempDirName);
1353     dir.mkdir(prefix);
1354 
1355     const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix);
1356     archivefile->copyTo(copyToDirName);
1357 
1358     KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename);
1359 
1360     KSharedConfig::Ptr importingMailArchiveConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename);
1361 
1362     copyUnifiedMailBoxConfig(importingMailArchiveConfig, existingConfig);
1363     existingConfig->sync();
1364 }
1365 
1366 void ImportMailJobInterface::mergeKmailSnippetConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix)
1367 {
1368     // TODO
1369     QDir dir(mTempDirName);
1370     dir.mkdir(prefix);
1371 
1372     const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix);
1373     copyArchiveFileTo(archivefile, copyToDirName);
1374 
1375     KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename);
1376 
1377     KSharedConfig::Ptr importingKMailSnipperConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename);
1378 }
1379 
1380 void ImportMailJobInterface::mergeMailArchiveConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix)
1381 {
1382     QDir dir(mTempDirName);
1383     dir.mkdir(prefix);
1384 
1385     const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix);
1386     archivefile->copyTo(copyToDirName);
1387 
1388     KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename);
1389 
1390     KSharedConfig::Ptr importingMailArchiveConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename);
1391 
1392     copyMailArchiveConfig(importingMailArchiveConfig, existingConfig);
1393     existingConfig->sync();
1394 }
1395 
1396 void ImportMailJobInterface::mergeArchiveMailAgentConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix)
1397 {
1398     QDir dir(mTempDirName);
1399     dir.mkdir(prefix);
1400 
1401     const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix);
1402     archivefile->copyTo(copyToDirName);
1403 
1404     KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename);
1405 
1406     KSharedConfig::Ptr importingArchiveMailAgentConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename);
1407 
1408     copyArchiveMailAgentConfigGroup(importingArchiveMailAgentConfig, existingConfig);
1409     existingConfig->sync();
1410 }
1411 
1412 void ImportMailJobInterface::mergeSieveTemplate(const KArchiveFile *archivefile, const QString &filename, const QString &prefix)
1413 {
1414     QDir dir(mTempDirName);
1415     dir.mkdir(prefix);
1416 
1417     const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix);
1418     copyArchiveFileTo(archivefile, copyToDirName);
1419 
1420     KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename);
1421 
1422     KSharedConfig::Ptr importingSieveTemplateConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename);
1423 
1424     KConfigGroup grpExisting = existingConfig->group(QStringLiteral("template"));
1425     int numberOfExistingTemplate = grpExisting.readEntry(QStringLiteral("templateCount"), 0);
1426 
1427     KConfigGroup grpImportExisting = importingSieveTemplateConfig->group(QStringLiteral("template"));
1428     const int numberOfImportingTemplate = grpImportExisting.readEntry(QStringLiteral("templateCount"), 0);
1429 
1430     for (int i = 0; i < numberOfImportingTemplate; ++i) {
1431         KConfigGroup templateDefine = importingSieveTemplateConfig->group(QStringLiteral("templateDefine_%1").arg(i));
1432 
1433         KConfigGroup newTemplateDefineGrp = existingConfig->group(QStringLiteral("templateDefine_%1").arg(numberOfExistingTemplate));
1434         newTemplateDefineGrp.writeEntry(QStringLiteral("Name"), templateDefine.readEntry(QStringLiteral("Name")));
1435         newTemplateDefineGrp.writeEntry(QStringLiteral("Text"), templateDefine.readEntry(QStringLiteral("Text")));
1436         ++numberOfExistingTemplate;
1437         newTemplateDefineGrp.sync();
1438     }
1439     grpExisting.writeEntry(QStringLiteral("templateCount"), numberOfExistingTemplate);
1440     grpExisting.sync();
1441 }
1442 
1443 void ImportMailJobInterface::convertCollectionStrToAkonadiId(const KSharedConfig::Ptr &config, const QString &groupName, const QString &key)
1444 {
1445     if (config->hasGroup(groupName)) {
1446         KConfigGroup eventGroup = config->group(groupName);
1447         (void)convertRealPathToCollection(eventGroup, key, false);
1448     }
1449 }
1450 
1451 #include "moc_importmailjobinterface.cpp"