diff --git a/core/abstractimportexportjob.cpp b/core/abstractimportexportjob.cpp index dc4a2de..eb4c19f 100644 --- a/core/abstractimportexportjob.cpp +++ b/core/abstractimportexportjob.cpp @@ -1,621 +1,621 @@ /* Copyright (C) 2012-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "abstractimportexportjob.h" #include "archivestorage.h" #include "importexportprogressindicatorbase.h" #include "synchronizeresourcejob.h" #include "MailCommon/MailUtil" #include #include #include #include #include #include #include #include #include #include #include int AbstractImportExportJob::sArchiveVersion = -1; AbstractImportExportJob::AbstractImportExportJob(QObject *parent, ArchiveStorage *archiveStorage, Utils::StoredTypes typeSelected, int numberOfStep) : QObject(parent) , mTypeSelected(typeSelected) , mArchiveStorage(archiveStorage) , mIdentityManager(KIdentityManagement::IdentityManager::self()) , mNumberOfStep(numberOfStep) , mIndex(-1) , mImportExportProgressIndicator(new ImportExportProgressIndicatorBase(this)) { mImportExportProgressIndicator->setNumberOfStep(numberOfStep); connect(mImportExportProgressIndicator, &ImportExportProgressIndicatorBase::info, this, &AbstractImportExportJob::info); } AbstractImportExportJob::~AbstractImportExportJob() { delete mCreateResource; delete mTempDir; } void AbstractImportExportJob::createProgressDialog(const QString &title) { mImportExportProgressIndicator->createProgressDialog(title); connect(mImportExportProgressIndicator, &ImportExportProgressIndicatorBase::canceled, this, &AbstractImportExportJob::slotTaskCanceled); } void AbstractImportExportJob::slotTaskCanceled() { Q_EMIT error(i18n("Task Canceled")); Q_EMIT jobFinished(); } bool AbstractImportExportJob::wasCanceled() const { return mImportExportProgressIndicator->wasCanceled(); } void AbstractImportExportJob::increaseProgressDialog() { mImportExportProgressIndicator->increaseProgressDialog(); } void AbstractImportExportJob::setProgressDialogLabel(const QString &text) { mImportExportProgressIndicator->setProgressDialogLabel(text); } ImportExportProgressIndicatorBase *AbstractImportExportJob::importExportProgressIndicator() const { return mImportExportProgressIndicator; } void AbstractImportExportJob::setImportExportProgressIndicator(ImportExportProgressIndicatorBase *importExportProgressIndicator) { delete mImportExportProgressIndicator; mImportExportProgressIndicator = importExportProgressIndicator; mImportExportProgressIndicator->setNumberOfStep(mNumberOfStep); } KZip *AbstractImportExportJob::archive() const { return mArchiveStorage->archive(); } void AbstractImportExportJob::backupUiRcFile(const QString &configFileName, const QString &application) { const QString configrcStr(configFileName); const QString configrc = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kxmlgui5/") + application + QLatin1Char('/') + configrcStr; if (QFileInfo::exists(configrc)) { backupFile(configrc, Utils::configsPath(), configrcStr); } } void AbstractImportExportJob::backupConfigFile(const QString &configFileName) { const QString configrcStr(configFileName); const QString configrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + configrcStr; if (QFileInfo::exists(configrc)) { backupFile(configrc, Utils::configsPath(), configrcStr); } } void AbstractImportExportJob::backupFile(const QString &filename, const QString &path, const QString &storedName) { if (QFileInfo::exists(filename)) { const bool fileAdded = archive()->addLocalFile(filename, path + storedName); if (fileAdded) { Q_EMIT info(i18n("\"%1\" backup done.", path + storedName)); } else { Q_EMIT error(i18n("\"%1\" cannot be exported.", path + storedName)); } } else { Q_EMIT error(i18n("\"%1\" does not exist.", filename)); } } int AbstractImportExportJob::mergeConfigMessageBox(const QString &configName) const { return mImportExportProgressIndicator->mergeConfigMessageBox(configName); } bool AbstractImportExportJob::overwriteConfigMessageBox(const QString &configName) const { return mImportExportProgressIndicator->overwriteConfigMessageBox(configName); } void AbstractImportExportJob::overwriteDirectory(const QString &path, const KArchiveEntry *entry) { if (QDir(path).exists()) { if (overwriteDirectoryMessageBox(path)) { const KArchiveDirectory *dirEntry = static_cast(entry); if (!dirEntry->copyTo(path)) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "directory cannot overwrite to " << path; } } } else { const KArchiveDirectory *dirEntry = static_cast(entry); if (dirEntry->copyTo(path)) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "directory cannot overwrite to " << path; } } } void AbstractImportExportJob::searchAllFiles(const KArchiveDirectory *dir, const QString &prefix, const QString &searchEntryName) { const QStringList lst = dir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = dir->entry(entryName); if (entry && entry->isDirectory()) { const QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName; if (entryName == searchEntryName) { storeArchiveInfoResources(static_cast(entry), entryName); } else { searchAllFiles(static_cast(entry), newPrefix, searchEntryName); } } } } void AbstractImportExportJob::storeArchiveInfoResources(const KArchiveDirectory *dir, const QString &prefix) { const QStringList lst = dir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = dir->entry(entryName); if (entry && entry->isDirectory()) { const KArchiveDirectory *resourceDir = static_cast(entry); const QStringList lst = resourceDir->entries(); if (lst.count() >= 2) { const QString archPath(prefix + QLatin1Char('/') + entryName + QLatin1Char('/')); resourceFiles files; for (const QString &name : lst) { if (isAConfigFile(name)) { files.akonadiConfigFile = archPath + name; } else if (name.startsWith(Utils::prefixAkonadiConfigFile())) { files.akonadiAgentConfigFile = archPath + name; } else { files.akonadiResources = archPath + name; } } files.debug(); mListResourceFile.append(files); } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " Problem in archive. number of file " << lst.count(); } } } } bool AbstractImportExportJob::isAConfigFile(const QString &name) const { Q_UNUSED(name); //Redefine in subclass return true; } bool AbstractImportExportJob::overwriteDirectoryMessageBox(const QString &directory) const { return mImportExportProgressIndicator->overwriteDirectoryMessageBox(directory); } qint64 AbstractImportExportJob::convertRealPathToCollection(KConfigGroup &group, const QString ¤tKey, bool addCollectionPrefix) { qint64 colId = -1; if (group.hasKey(currentKey)) { const QString path = group.readEntry(currentKey); if (!path.isEmpty()) { const Akonadi::Collection::Id id = convertPathToId(path); if (id != -1) { if (addCollectionPrefix) { group.writeEntry(currentKey, QStringLiteral("c%1").arg(id)); } else { group.writeEntry(currentKey, id); } } else { group.deleteEntry(currentKey); } colId = id; } } return colId; } void AbstractImportExportJob::convertRealPathToCollectionList(KConfigGroup &group, const QString ¤tKey, bool addCollectionPrefix) { if (group.hasKey(currentKey)) { const QStringList listExpension = group.readEntry(currentKey, QStringList()); QStringList result; if (!listExpension.isEmpty()) { for (const QString &collection : listExpension) { const Akonadi::Collection::Id id = convertPathToId(collection); if (id != -1) { if (addCollectionPrefix) { result << QStringLiteral("c%1").arg(id); } else { result << QStringLiteral("%1").arg(id); } } } if (result.isEmpty()) { group.deleteEntry(currentKey); } else { group.writeEntry(currentKey, result); } } } } Akonadi::Collection::Id AbstractImportExportJob::convertPathToId(const QString &path) { Akonadi::Collection::Id val = mHashConvertPathCollectionId.value(path, -1); if (val != -1) { return val; } const Akonadi::Collection::Id id = MailCommon::Util::convertFolderPathToCollectionId(path); if (id != -1) { mHashConvertPathCollectionId.insert(path, id); } return id; } void AbstractImportExportJob::initializeImportJob() { if (mTempDir) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " initializeImportJob already called"; } else { mTempDir = new QTemporaryDir(); mTempDirName = mTempDir->path(); mCreateResource = new PimCommon::CreateResource(); connect(mCreateResource, &PimCommon::CreateResource::createResourceInfo, this, &AbstractImportExportJob::info); connect(mCreateResource, &PimCommon::CreateResource::createResourceError, this, &AbstractImportExportJob::error); } } void AbstractImportExportJob::copyToDirectory(const KArchiveEntry *entry, const QString &dest) { const KArchiveDirectory *subfolderDir = static_cast(entry); if (!subfolderDir->copyTo(dest)) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "directory cannot copy to " << dest; } Q_EMIT info(i18n("\"%1\" was copied.", dest)); } void AbstractImportExportJob::copyToFile(const KArchiveFile *archivefile, const QString &dest, const QString &filename, const QString &prefix) { QDir dir(mTempDirName); const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix); const bool created = dir.mkpath(copyToDirName); if (!created) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " directory :" << prefix << " not created"; } if (!archivefile->copyTo(copyToDirName)) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "file " << filename << " can not copy to " << dest; } QFile file; file.setFileName(copyToDirName + QLatin1Char('/') + filename); //QFile doesn't overwrite => remove old file before //qCDebug(PIMSETTINGEXPORTERCORE_LOG)<<" dest "<showErrorMessage(i18n("File \"%1\" cannot be copied to \"%2\".", filename, dest), i18n("Copy file")); } else { Q_EMIT info(i18n("\"%1\" was restored.", filename)); } } void AbstractImportExportJob::backupResourceFile(const Akonadi::AgentInstance &agent, const QString &defaultPath) { const QString identifier = agent.identifier(); const QString archivePath = defaultPath + identifier + QDir::separator(); QString url = Utils::resourcePath(agent); if (!url.isEmpty()) { QFileInfo fi(url); QString filename = fi.fileName(); const bool fileAdded = archive()->addLocalFile(url, archivePath + filename); if (fileAdded) { const QString errorStr = Utils::storeResources(archive(), identifier, archivePath); if (!errorStr.isEmpty()) { Q_EMIT error(errorStr); } Q_EMIT info(i18n("\"%1\" was backed up.", filename)); url = Akonadi::ServerManager::agentConfigFilePath(identifier); if (!url.isEmpty()) { fi = QFileInfo(url); filename = fi.fileName(); const bool fileAdded = archive()->addLocalFile(url, archivePath + filename); if (fileAdded) { Q_EMIT info(i18n("\"%1\" was backed up.", filename)); } else { Q_EMIT error(i18n("\"%1\" file cannot be added to backup file.", filename)); } } } else { Q_EMIT error(i18n("\"%1\" file cannot be added to backup file.", filename)); } } } QStringList AbstractImportExportJob::restoreResourceFile(const QString &resourceBaseName, const QString &defaultPath, const QString &storePath, bool overwriteResources) { QStringList resourceToSync; //TODO fix sync config after created a resource if (!mListResourceFile.isEmpty()) { QDir dir(mTempDirName); dir.mkdir(defaultPath); const QString copyToDirName(mTempDirName + QLatin1Char('/') + defaultPath); for (int i = 0; i < mListResourceFile.size(); ++i) { resourceFiles value = mListResourceFile.at(i); QMap settings; if (value.akonadiConfigFile.contains(resourceBaseName + QLatin1Char('_'))) { const KArchiveEntry *fileResouceEntry = mArchiveDirectory->entry(value.akonadiConfigFile); if (fileResouceEntry && fileResouceEntry->isFile()) { const KArchiveFile *file = static_cast(fileResouceEntry); file->copyTo(copyToDirName); QString resourceName(file->name()); QString filename(file->name()); //TODO adapt filename otherwise it will use all the time the same filename. qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " filename :" << filename; KSharedConfig::Ptr resourceConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + resourceName); QString newUrl; if (overwriteResources) { newUrl = Utils::resourcePath(resourceConfig); } else { newUrl = Utils::adaptResourcePath(resourceConfig, storePath); } const QString dataFile = value.akonadiResources; const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(dataFile); if (dataResouceEntry->isFile()) { const KArchiveFile *file = static_cast(dataResouceEntry); file->copyTo(newUrl); } settings.insert(QStringLiteral("Path"), newUrl); const QString agentConfigFile = value.akonadiAgentConfigFile; if (!agentConfigFile.isEmpty()) { const KArchiveEntry *akonadiAgentConfigEntry = mArchiveDirectory->entry(agentConfigFile); if (akonadiAgentConfigEntry->isFile()) { const KArchiveFile *file = static_cast(akonadiAgentConfigEntry); file->copyTo(copyToDirName); resourceName = file->name(); const QString configPath = copyToDirName + QLatin1Char('/') + resourceName; filename = Utils::akonadiAgentName(configPath); } } addSpecificResourceSettings(resourceConfig, resourceBaseName, settings); const QString newResource = mCreateResource->createResource(resourceBaseName, filename, settings); infoAboutNewResource(newResource); resourceToSync << newResource; qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " newResource" << newResource; } } } Q_EMIT info(i18n("Resources restored.")); } else { Q_EMIT error(i18n("No resources files found.")); } return resourceToSync; } -void AbstractImportExportJob::addSpecificResourceSettings(KSharedConfig::Ptr /*resourceConfig*/, const QString & /*resourceName*/, QMap & /*settings*/) +void AbstractImportExportJob::addSpecificResourceSettings(const KSharedConfig::Ptr /*resourceConfig*/&, const QString & /*resourceName*/, QMap & /*settings*/) { //Redefine it in subclass } void AbstractImportExportJob::extractZipFile(const KArchiveFile *file, const QString &source, const QString &destination, bool isStoredAsZippedArchive) { file->copyTo(source); QDir dest(destination); if (!dest.exists()) { dest.mkpath(destination); } if (isStoredAsZippedArchive) { QString errorMsg; KZip *zip = Utils::openZip(source + QLatin1Char('/') + file->name(), errorMsg); if (zip) { const KArchiveDirectory *zipDir = zip->directory(); const QStringList lst = zipDir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = zipDir->entry(entryName); if (entry) { if (entry->isDirectory()) { const KArchiveDirectory *dir = static_cast(entry); dir->copyTo(destination + QDir::separator() + dir->name(), true); } else if (entry->isFile()) { const KArchiveFile *dir = static_cast(entry); dir->copyTo(destination); } } qApp->processEvents(); } delete zip; } else { Q_EMIT error(errorMsg); } } else { QFile archiveFile(source + QLatin1Char('/') + file->name()); if (!archiveFile.copy(destination + QLatin1Char('/') + file->name())) { Q_EMIT error(i18n("Unable to copy file %1", file->name())); } } } void AbstractImportExportJob::restoreUiRcFile(const QString &configNameStr, const QString &applicationName) { const KArchiveEntry *configNameentry = mArchiveDirectory->entry(Utils::configsPath() + configNameStr); if (configNameentry && configNameentry->isFile()) { const KArchiveFile *configNameconfiguration = static_cast(configNameentry); const QString configNamerc = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kxmlgui5/") + applicationName + QLatin1Char('/') + configNameStr; if (QFileInfo::exists(configNamerc)) { if (overwriteConfigMessageBox(configNameStr)) { copyToFile(configNameconfiguration, configNamerc, configNameStr, Utils::configsPath()); } } else { copyToFile(configNameconfiguration, configNamerc, configNameStr, Utils::configsPath()); } } } void AbstractImportExportJob::restoreConfigFile(const QString &configNameStr) { const KArchiveEntry *configNameentry = mArchiveDirectory->entry(Utils::configsPath() + configNameStr); if (configNameentry && configNameentry->isFile()) { const KArchiveFile *configNameconfiguration = static_cast(configNameentry); const QString configNamerc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + configNameStr; if (QFileInfo::exists(configNamerc)) { //TODO 4.12 allow to merge config. if (overwriteConfigMessageBox(configNameStr)) { copyToFile(configNameconfiguration, configNamerc, configNameStr, Utils::configsPath()); } } else { copyToFile(configNameconfiguration, configNamerc, configNameStr, Utils::configsPath()); } } } void AbstractImportExportJob::infoAboutNewResource(const QString &resourceName) { Q_EMIT info(i18n("Resource \'%1\' created.", resourceName)); } int AbstractImportExportJob::archiveVersion() { return sArchiveVersion; } void AbstractImportExportJob::setArchiveVersion(int version) { sArchiveVersion = version; } void AbstractImportExportJob::slotSynchronizeInstanceFailed(const QString &instance) { Q_EMIT error(i18n("Failed to synchronize %1.", instance)); } void AbstractImportExportJob::slotSynchronizeInstanceDone(const QString &name, const QString &identifier) { Q_EMIT info(i18n("Resource %1 synchronized.", name)); Q_EMIT needSynchronizeResource(name, identifier); } void AbstractImportExportJob::slotAllResourceSynchronized() { Q_EMIT info(i18n("All resources synchronized.")); slotNextStep(); } void AbstractImportExportJob::slotNextStep() { //Implement in sub class. } void AbstractImportExportJob::startSynchronizeResources(const QStringList &listResourceToSync) { Q_EMIT info(i18n("Start synchronizing...")); SynchronizeResourceJob *job = new SynchronizeResourceJob(this); job->setListResources(listResourceToSync); connect(job, &SynchronizeResourceJob::synchronizationFinished, this, &AbstractImportExportJob::slotAllResourceSynchronized); connect(job, &SynchronizeResourceJob::synchronizationInstanceDone, this, &AbstractImportExportJob::slotSynchronizeInstanceDone); connect(job, &SynchronizeResourceJob::synchronizationInstanceFailed, this, &AbstractImportExportJob::slotSynchronizeInstanceFailed); job->start(); } void AbstractImportExportJob::initializeListStep() { if (mTypeSelected & Utils::MailTransport) { mListStep << Utils::MailTransport; } if (mTypeSelected & Utils::Mails) { mListStep << Utils::Mails; } if (mTypeSelected & Utils::Resources) { mListStep << Utils::Resources; } if (mTypeSelected & Utils::Identity) { mListStep << Utils::Identity; } if (mTypeSelected & Utils::Config) { mListStep << Utils::Config; } if (mTypeSelected & Utils::Data) { mListStep << Utils::Data; } } void AbstractImportExportJob::storeDirectory(const QString &subDirectory) { const QDir directoryToStore(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + subDirectory); if (directoryToStore.exists()) { const bool templateDirAdded = archive()->addLocalDirectory(directoryToStore.path(), Utils::dataPath() + subDirectory); if (templateDirAdded) { Q_EMIT info(i18n("Directory \"%1\" added to backup file.", directoryToStore.path())); } else { Q_EMIT error(i18n("Directory \"%1\" cannot be added to backup file.", directoryToStore.path())); } } } void AbstractImportExportJob::importDataSubdirectory(const QString &subdirectoryRelativePath) { const KArchiveEntry *themeEntry = mArchiveDirectory->entry(Utils::dataPath() + subdirectoryRelativePath); if (themeEntry && themeEntry->isDirectory()) { const KArchiveDirectory *themeDir = static_cast(themeEntry); const QStringList lst = themeDir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = themeDir->entry(entryName); if (entry && entry->isDirectory()) { QString subFolderName = entryName; const QString topLevelPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + subdirectoryRelativePath; QDir themeDirectory(topLevelPath + QStringLiteral("/%1").arg(entryName)); int i = 1; while (themeDirectory.exists()) { subFolderName = entryName + QStringLiteral("_%1").arg(i); themeDirectory = QDir(topLevelPath + QStringLiteral("/%1").arg(subFolderName)); ++i; } copyToDirectory(entry, topLevelPath + QStringLiteral("/%1").arg(subFolderName)); } } } } diff --git a/core/abstractimportexportjob.h b/core/abstractimportexportjob.h index e0c8944..88552db 100644 --- a/core/abstractimportexportjob.h +++ b/core/abstractimportexportjob.h @@ -1,138 +1,138 @@ /* Copyright (C) 2012-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef ABSTRACTIMPORTEXPORTJOB_H #define ABSTRACTIMPORTEXPORTJOB_H #include #include "utils.h" #include #include "pimsettingexporter_export.h" #include #include class ArchiveStorage; class KArchiveDirectory; class QTemporaryDir; class KZip; class KArchiveFile; class KArchiveEntry; namespace KIdentityManagement { class Identity; class IdentityManager; } namespace PimCommon { class CreateResource; } class ImportExportProgressIndicatorBase; class PIMSETTINGEXPORTER_EXPORT AbstractImportExportJob : public QObject { Q_OBJECT public: explicit AbstractImportExportJob(QObject *parent, ArchiveStorage *archiveStorage, Utils::StoredTypes typeSelected, int numberOfStep); ~AbstractImportExportJob(); virtual void start() = 0; bool wasCanceled() const; static int archiveVersion(); static void setArchiveVersion(int version); void setImportExportProgressIndicator(ImportExportProgressIndicatorBase *importExportProgressIndicator); ImportExportProgressIndicatorBase *importExportProgressIndicator() const; Q_SIGNALS: void info(const QString &); void error(const QString &); void title(const QString &); void endLine(); void needSynchronizeResource(const QString &name, const QString &identifier); void jobFinished(); protected: virtual void slotNextStep(); protected: void initializeListStep(); void startSynchronizeResources(const QStringList &listResourceToSync); void infoAboutNewResource(const QString &resourceName); void copyToDirectory(const KArchiveEntry *entry, const QString &dest); void extractZipFile(const KArchiveFile *file, const QString &source, const QString &destination, bool isStoredAsZippedArchive = true); qint64 convertRealPathToCollection(KConfigGroup &group, const QString ¤tKey, bool addCollectionPrefix = false); void convertRealPathToCollectionList(KConfigGroup &group, const QString ¤tKey, bool addCollectionPrefix = true); void copyToFile(const KArchiveFile *archivefile, const QString &dest, const QString &filename, const QString &prefix); void initializeImportJob(); void backupFile(const QString &filename, const QString &path, const QString &storedName); void backupConfigFile(const QString &configFileName); void backupUiRcFile(const QString &configFileName, const QString &applicationName); void restoreUiRcFile(const QString &configNameStr, const QString &applicationName); int mergeConfigMessageBox(const QString &configName) const; bool overwriteConfigMessageBox(const QString &configName) const; Akonadi::Collection::Id convertPathToId(const QString &path); void backupResourceFile(const Akonadi::AgentInstance &agent, const QString &defaultPath); QStringList restoreResourceFile(const QString &resourceName, const QString &defaultPath, const QString &storePath, bool overwriteResources = false); - virtual void addSpecificResourceSettings(KSharedConfig::Ptr resourceConfig, const QString &resourceName, QMap &settings); + virtual void addSpecificResourceSettings(const KSharedConfig::Ptr &resourceConfig, const QString &resourceName, QMap &settings); void restoreConfigFile(const QString &configNameStr); bool overwriteDirectoryMessageBox(const QString &directory) const; void overwriteDirectory(const QString &path, const KArchiveEntry *entry); virtual bool isAConfigFile(const QString &name) const; void searchAllFiles(const KArchiveDirectory *dir, const QString &prefix, const QString &searchEntryName); void storeArchiveInfoResources(const KArchiveDirectory *dir, const QString &prefix); KZip *archive() const; void increaseProgressDialog(); void createProgressDialog(const QString &title = QString()); void setProgressDialogLabel(const QString &text); void storeDirectory(const QString &subDirectory); void importDataSubdirectory(const QString &subdirectoryRelativePath); QHash mHashConvertPathCollectionId; QVector mListResourceFile; QString mTempDirName; Utils::StoredTypes mTypeSelected; ArchiveStorage *mArchiveStorage = nullptr; KIdentityManagement::IdentityManager *mIdentityManager = nullptr; QTemporaryDir *mTempDir = nullptr; const KArchiveDirectory *mArchiveDirectory = nullptr; int mNumberOfStep = -1; PimCommon::CreateResource *mCreateResource = nullptr; QStringList mAgentPaths; QList mListStep; int mIndex = -1; static int sArchiveVersion; private: void slotAllResourceSynchronized(); void slotSynchronizeInstanceDone(const QString &, const QString &identifier); void slotSynchronizeInstanceFailed(const QString &instance); void slotTaskCanceled(); ImportExportProgressIndicatorBase *mImportExportProgressIndicator = nullptr; }; #endif // ABSTRACTIMPORTEXPORTJOB_H diff --git a/core/addressbook/importaddressbookjob.cpp b/core/addressbook/importaddressbookjob.cpp index 71423c3..3485486 100644 --- a/core/addressbook/importaddressbookjob.cpp +++ b/core/addressbook/importaddressbookjob.cpp @@ -1,248 +1,248 @@ /* Copyright (C) 2013-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "importaddressbookjob.h" #include "archivestorage.h" #include #include #include #include #include #include #include #include #include namespace { inline const QString storeAddressbook() { return QStringLiteral("backupaddressbook/"); } } ImportAddressbookJob::ImportAddressbookJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep) : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep) { initializeImportJob(); } ImportAddressbookJob::~ImportAddressbookJob() { } void ImportAddressbookJob::start() { Q_EMIT title(i18n("Starting to import KAddressBook settings...")); mArchiveDirectory = archive()->directory(); createProgressDialog(i18n("Import KAddressBook settings")); searchAllFiles(mArchiveDirectory, QString(), QStringLiteral("addressbook")); initializeListStep(); QTimer::singleShot(0, this, &ImportAddressbookJob::slotNextStep); } void ImportAddressbookJob::slotNextStep() { ++mIndex; if (mIndex < mListStep.count()) { const Utils::StoredType type = mListStep.at(mIndex); if (type == Utils::Resources) { restoreResources(); } else if (type == Utils::Config) { restoreConfig(); } } else { Q_EMIT jobFinished(); } } void ImportAddressbookJob::restoreResources() { Q_EMIT info(i18n("Restore resources...")); setProgressDialogLabel(i18n("Restore resources...")); increaseProgressDialog(); QStringList listResource; listResource << restoreResourceFile(QStringLiteral("akonadi_vcard_resource"), Utils::addressbookPath(), QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String( "/share/kaddressbook/")); if (!mListResourceFile.isEmpty()) { QDir dir(mTempDirName); dir.mkdir(Utils::addressbookPath()); const QString copyToDirName(mTempDirName + QLatin1Char('/') + Utils::addressbookPath()); const int numberOfResourceFile = mListResourceFile.size(); for (int i = 0; i < numberOfResourceFile; ++i) { resourceFiles value = mListResourceFile.at(i); QMap settings; if (value.akonadiConfigFile.contains(QLatin1String("akonadi_vcarddir_resource_")) || value.akonadiConfigFile.contains(QLatin1String("akonadi_contacts_resource_"))) { const KArchiveEntry *fileResouceEntry = mArchiveDirectory->entry(value.akonadiConfigFile); if (fileResouceEntry && fileResouceEntry->isFile()) { const KArchiveFile *file = static_cast(fileResouceEntry); file->copyTo(copyToDirName); QString resourceName(file->name()); QString filename(file->name()); //TODO adapt filename otherwise it will use all the time the same filename. KSharedConfig::Ptr resourceConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + resourceName); //TODO fix default path const QString newUrl = Utils::adaptResourcePath(resourceConfig, QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/contacts")); QFileInfo newUrlInfo(newUrl); const QString dataFile = value.akonadiResources; const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(dataFile); if (dataResouceEntry->isFile()) { const KArchiveFile *file = static_cast(dataResouceEntry); //TODO adapt directory name too extractZipFile(file, copyToDirName, newUrlInfo.path(), value.akonadiConfigFile.contains(QLatin1String("akonadi_contacts_resource_"))); } settings.insert(QStringLiteral("Path"), newUrl); const QString agentConfigFile = value.akonadiAgentConfigFile; if (!agentConfigFile.isEmpty()) { const KArchiveEntry *akonadiAgentConfigEntry = mArchiveDirectory->entry(agentConfigFile); if (akonadiAgentConfigEntry->isFile()) { const KArchiveFile *file = static_cast(akonadiAgentConfigEntry); file->copyTo(copyToDirName); resourceName = file->name(); filename = Utils::akonadiAgentName(copyToDirName + QLatin1Char('/') + resourceName); } } QString instanceType; if (value.akonadiConfigFile.contains(QLatin1String("akonadi_vcarddir_resource_"))) { instanceType = QStringLiteral("akonadi_vcarddir_resource"); } else if (value.akonadiConfigFile.contains(QLatin1String("akonadi_contacts_resource_"))) { instanceType = QStringLiteral("akonadi_contacts_resource"); } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " not supported" << value.akonadiConfigFile; } const QString newResource = mCreateResource->createResource(instanceType, filename, settings, true); infoAboutNewResource(newResource); qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " newResource" << newResource; listResource << newResource; } } } } Q_EMIT info(i18n("Resources restored.")); //It's maildir support. Need to add support startSynchronizeResources(listResource); } -void ImportAddressbookJob::addSpecificResourceSettings(KSharedConfig::Ptr resourceConfig, const QString &resourceName, QMap &settings) +void ImportAddressbookJob::addSpecificResourceSettings(const KSharedConfig::Ptr &resourceConfig, const QString &resourceName, QMap &settings) { if (resourceName == QLatin1String("akonadi_vcard_resource")) { KConfigGroup general = resourceConfig->group(QStringLiteral("General")); if (general.hasKey(QStringLiteral("DisplayName"))) { settings.insert(QStringLiteral("DisplayName"), general.readEntry(QStringLiteral("DisplayName"))); } if (general.hasKey(QStringLiteral("ReadOnly"))) { settings.insert(QStringLiteral("ReadOnly"), general.readEntry(QStringLiteral("ReadOnly"), false)); } if (general.hasKey(QStringLiteral("MonitorFile"))) { settings.insert(QStringLiteral("MonitorFile"), general.readEntry(QStringLiteral("MonitorFile"), true)); } } } bool ImportAddressbookJob::isAConfigFile(const QString &name) const { return name.endsWith(QLatin1String("rc")) && (name.startsWith(QLatin1String("akonadi_vcarddir_resource_")) || name.startsWith(QLatin1String("akonadi_vcard_resource_")) || name.startsWith(QLatin1String("akonadi_contacts_resource_"))); } void ImportAddressbookJob::restoreConfig() { increaseProgressDialog(); setProgressDialogLabel(i18n("Restore configs...")); const QString kaddressbookStr(QStringLiteral("kaddressbookrc")); const KArchiveEntry *kaddressbookrcentry = mArchiveDirectory->entry(Utils::configsPath() + kaddressbookStr); if (kaddressbookrcentry && kaddressbookrcentry->isFile()) { const KArchiveFile *kaddressbookrcFile = static_cast(kaddressbookrcentry); const QString kaddressbookrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kaddressbookStr; if (QFileInfo::exists(kaddressbookrc)) { if (overwriteConfigMessageBox(kaddressbookStr)) { importkaddressBookConfig(kaddressbookrcFile, kaddressbookrc, kaddressbookStr, Utils::configsPath()); } } else { importkaddressBookConfig(kaddressbookrcFile, kaddressbookrc, kaddressbookStr, Utils::configsPath()); } } restoreUiRcFile(QStringLiteral("kaddressbookui.rc"), QStringLiteral("kaddressbook")); Q_EMIT info(i18n("Config restored.")); QTimer::singleShot(0, this, &ImportAddressbookJob::slotNextStep); } void ImportAddressbookJob::importkaddressBookConfig(const KArchiveFile *file, const QString &config, const QString &filename, const QString &prefix) { copyToFile(file, config, filename, prefix); KSharedConfig::Ptr kaddressBookConfig = KSharedConfig::openConfig(config); const QString collectionViewCheckStateStr(QStringLiteral("CollectionViewCheckState")); if (kaddressBookConfig->hasGroup(collectionViewCheckStateStr)) { KConfigGroup group = kaddressBookConfig->group(collectionViewCheckStateStr); const QString selectionKey(QStringLiteral("Selection")); convertRealPathToCollectionList(group, selectionKey, true); } const QString collectionViewStateStr(QStringLiteral("CollectionViewState")); if (kaddressBookConfig->hasGroup(collectionViewStateStr)) { KConfigGroup group = kaddressBookConfig->group(collectionViewStateStr); QString currentKey(QStringLiteral("Current")); convertRealPathToCollection(group, currentKey, true); currentKey = QStringLiteral("Expansion"); convertRealPathToCollection(group, currentKey, true); currentKey = QStringLiteral("Selection"); convertRealPathToCollection(group, currentKey, true); } const QString cvsTemplateDirName = QStringLiteral("/kaddressbook/csv-templates/"); const KArchiveEntry *csvtemplateEntry = mArchiveDirectory->entry(Utils::dataPath() + cvsTemplateDirName); if (csvtemplateEntry && csvtemplateEntry->isDirectory()) { const KArchiveDirectory *csvTemplateDir = static_cast(csvtemplateEntry); const QStringList lst = csvTemplateDir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = csvTemplateDir->entry(entryName); if (entry && entry->isFile()) { const KArchiveFile *csvTemplateFile = static_cast(entry); const QString name = csvTemplateFile->name(); QString autocorrectionPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + cvsTemplateDirName; if (QFileInfo::exists(autocorrectionPath)) { if (overwriteConfigMessageBox(name)) { copyToFile(csvTemplateFile, autocorrectionPath + QLatin1Char('/') + name, name, Utils::dataPath() + cvsTemplateDirName); } } else { autocorrectionPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + cvsTemplateDirName; copyToFile(csvTemplateFile, autocorrectionPath + QLatin1Char('/') + name, name, Utils::dataPath() + cvsTemplateDirName); } } } } importDataSubdirectory(QStringLiteral("/kaddressbook/viewertemplates/")); importDataSubdirectory(QStringLiteral("/kaddressbook/printing/")); kaddressBookConfig->sync(); } diff --git a/core/addressbook/importaddressbookjob.h b/core/addressbook/importaddressbookjob.h index 3fdde34..1c29fe6 100644 --- a/core/addressbook/importaddressbookjob.h +++ b/core/addressbook/importaddressbookjob.h @@ -1,48 +1,48 @@ /* Copyright (C) 2013-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef IMPORTADDRESSBOOKJOB_H #define IMPORTADDRESSBOOKJOB_H #include "abstractimportexportjob.h" #include "pimsettingexportercore_private_export.h" class ArchiveStorage; class KArchiveFile; class PIMSETTINGEXPORTER_TESTS_EXPORT ImportAddressbookJob : public AbstractImportExportJob { Q_OBJECT public: explicit ImportAddressbookJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep); ~ImportAddressbookJob() override; void start() override; protected: void slotNextStep() override; private: bool isAConfigFile(const QString &name) const override; void importkaddressBookConfig(const KArchiveFile *file, const QString &config, const QString &filename, const QString &prefix); void restoreResources(); void restoreConfig(); - void addSpecificResourceSettings(KSharedConfig::Ptr resourceConfig, const QString &resourceName, QMap &settings) override; + void addSpecificResourceSettings(const KSharedConfig::Ptr &resourceConfig, const QString &resourceName, QMap &settings) override; }; #endif // IMPORTADDRESSBOOKJOB_H diff --git a/core/calendar/importcalendarjob.cpp b/core/calendar/importcalendarjob.cpp index 1f6df83..2850ee0 100644 --- a/core/calendar/importcalendarjob.cpp +++ b/core/calendar/importcalendarjob.cpp @@ -1,290 +1,290 @@ /* Copyright (C) 2012-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "importcalendarjob.h" #include "archivestorage.h" #include #include #include #include #include #include #include #include "pimsettingexportcore_debug.h" #include #include #include #include namespace { inline const QString storeCalendar() { return QStringLiteral("backupcalendar/"); } } ImportCalendarJob::ImportCalendarJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep) : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep) { initializeImportJob(); } ImportCalendarJob::~ImportCalendarJob() { } void ImportCalendarJob::start() { Q_EMIT title(i18n("Starting to import KOrganizer settings...")); mArchiveDirectory = archive()->directory(); createProgressDialog(i18n("Import KOrganizer settings")); searchAllFiles(mArchiveDirectory, QString(), QStringLiteral("calendar")); initializeListStep(); QTimer::singleShot(0, this, &ImportCalendarJob::slotNextStep); } void ImportCalendarJob::slotNextStep() { ++mIndex; if (mIndex < mListStep.count()) { Utils::StoredType type = mListStep.at(mIndex); if (type == Utils::Resources) { restoreResources(); } else if (type == Utils::Config) { restoreConfig(); } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << Q_FUNC_INFO << " not supported type " << type; slotNextStep(); } } else { Q_EMIT jobFinished(); } } void ImportCalendarJob::restoreResources() { Q_EMIT info(i18n("Restore resources...")); setProgressDialogLabel(i18n("Restore resources...")); increaseProgressDialog(); QStringList listResource; listResource << restoreResourceFile(QStringLiteral("akonadi_ical_resource"), Utils::calendarPath(), storeCalendar()); if (!mListResourceFile.isEmpty()) { QDir dir(mTempDirName); dir.mkdir(Utils::addressbookPath()); const QString copyToDirName(mTempDirName + QLatin1Char('/') + Utils::calendarPath()); const int numberOfResourceFile = mListResourceFile.size(); for (int i = 0; i < numberOfResourceFile; ++i) { resourceFiles value = mListResourceFile.at(i); QMap settings; if (value.akonadiConfigFile.contains(QLatin1String("akonadi_icaldir_resource_")) || value.akonadiConfigFile.contains(QLatin1String("akonadi_ical_resource_"))) { const KArchiveEntry *fileResouceEntry = mArchiveDirectory->entry(value.akonadiConfigFile); if (fileResouceEntry && fileResouceEntry->isFile()) { const KArchiveFile *file = static_cast(fileResouceEntry); if (!file->copyTo(copyToDirName)) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "file can not copy to " << copyToDirName; } QString resourceName(file->name()); QString filename(file->name()); //TODO adapt filename otherwise it will use all the time the same filename. qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " filename :" << filename; KSharedConfig::Ptr resourceConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + resourceName); const QString newUrl = Utils::adaptResourcePath(resourceConfig, storeCalendar()); QFileInfo newUrlInfo(newUrl); const QString dataFile = value.akonadiResources; const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(dataFile); bool isDirResource = value.akonadiConfigFile.contains(QLatin1String("akonadi_icaldir_resource_")); if (dataResouceEntry->isFile()) { const KArchiveFile *file = static_cast(dataResouceEntry); //TODO adapt directory name too extractZipFile(file, copyToDirName, newUrlInfo.path(), value.akonadiConfigFile.contains(QLatin1String("akonadi_icaldir_resource_"))); } settings.insert(QStringLiteral("Path"), newUrl); const QString agentConfigFile = value.akonadiAgentConfigFile; if (!agentConfigFile.isEmpty()) { const KArchiveEntry *akonadiAgentConfigEntry = mArchiveDirectory->entry(agentConfigFile); if (akonadiAgentConfigEntry->isFile()) { const KArchiveFile *file = static_cast(akonadiAgentConfigEntry); file->copyTo(copyToDirName); resourceName = file->name(); filename = Utils::akonadiAgentName(copyToDirName + QLatin1Char('/') + resourceName); } } const QString resourceTypeName = isDirResource ? QStringLiteral("akonadi_icaldir_resource") : QStringLiteral("akonadi_ical_resource"); const QString newResource = mCreateResource->createResource(resourceTypeName, filename, settings, true); infoAboutNewResource(newResource); listResource << newResource; qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " newResource" << newResource; } } } } //It's maildir support. Need to add support startSynchronizeResources(listResource); } -void ImportCalendarJob::addSpecificResourceSettings(KSharedConfig::Ptr resourceConfig, const QString &resourceName, QMap &settings) +void ImportCalendarJob::addSpecificResourceSettings(const KSharedConfig::Ptr &resourceConfig, const QString &resourceName, QMap &settings) { if (resourceName == QLatin1String("akonadi_ical_resource")) { KConfigGroup general = resourceConfig->group(QStringLiteral("General")); if (general.hasKey(QStringLiteral("DisplayName"))) { settings.insert(QStringLiteral("DisplayName"), general.readEntry(QStringLiteral("DisplayName"))); } if (general.hasKey(QStringLiteral("ReadOnly"))) { settings.insert(QStringLiteral("ReadOnly"), general.readEntry(QStringLiteral("ReadOnly"), false)); } if (general.hasKey(QStringLiteral("MonitorFile"))) { settings.insert(QStringLiteral("MonitorFile"), general.readEntry(QStringLiteral("MonitorFile"), true)); } } } bool ImportCalendarJob::isAConfigFile(const QString &name) const { return name.endsWith(QLatin1String("rc")) && (name.contains(QLatin1String("akonadi_ical_resource_")) || name.contains(QLatin1String("akonadi_icaldir_resource_"))); } void ImportCalendarJob::restoreConfig() { increaseProgressDialog(); setProgressDialogLabel(i18n("Restore configs...")); const QString korganizerPrinterrcStr(QStringLiteral("calendar_printing.rc")); const QString oldKorganizerPrintrrcStr(QStringLiteral("korganizer_printing.rc")); const KArchiveEntry *oldKorganizerPrinterEntry = mArchiveDirectory->entry(Utils::configsPath() + oldKorganizerPrintrrcStr); if (oldKorganizerPrinterEntry && oldKorganizerPrinterEntry->isFile()) { const KArchiveFile *korganizerFile = static_cast(oldKorganizerPrinterEntry); const QString oldKorganizerPrintrrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + korganizerPrinterrcStr; if (QFileInfo::exists(oldKorganizerPrintrrc)) { if (overwriteConfigMessageBox(oldKorganizerPrintrrc)) { copyToFile(korganizerFile, oldKorganizerPrintrrc, oldKorganizerPrintrrcStr, Utils::configsPath()); } } else { copyToFile(korganizerFile, oldKorganizerPrintrrc, oldKorganizerPrintrrcStr, Utils::configsPath()); } } else { const KArchiveEntry *korganizerPrinterEntry = mArchiveDirectory->entry(Utils::configsPath() + korganizerPrinterrcStr); if (korganizerPrinterEntry && korganizerPrinterEntry->isFile()) { const KArchiveFile *korganizerFile = static_cast(korganizerPrinterEntry); const QString korganizerPrinterrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + korganizerPrinterrcStr; if (QFileInfo::exists(korganizerPrinterrc)) { if (overwriteConfigMessageBox(korganizerPrinterrcStr)) { copyToFile(korganizerFile, korganizerPrinterrc, korganizerPrinterrcStr, Utils::configsPath()); } } else { copyToFile(korganizerFile, korganizerPrinterrc, korganizerPrinterrcStr, Utils::configsPath()); } } } const QString korganizerStr(QStringLiteral("korganizerrc")); const KArchiveEntry *korganizerrcentry = mArchiveDirectory->entry(Utils::configsPath() + korganizerStr); if (korganizerrcentry && korganizerrcentry->isFile()) { const KArchiveFile *korganizerrcFile = static_cast(korganizerrcentry); const QString korganizerrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + korganizerStr; if (QFileInfo::exists(korganizerrc)) { if (overwriteConfigMessageBox(korganizerStr)) { importkorganizerConfig(korganizerrcFile, korganizerrc, korganizerStr, Utils::configsPath()); } } else { importkorganizerConfig(korganizerrcFile, korganizerrc, korganizerStr, Utils::configsPath()); } } const QString korgacStr(QStringLiteral("korgacrc")); const KArchiveEntry *korgacrcentry = mArchiveDirectory->entry(Utils::configsPath() + korgacStr); if (korgacrcentry && korgacrcentry->isFile()) { const KArchiveFile *korgacrcFile = static_cast(korgacrcentry); const QString korgacrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + korgacStr; if (QFileInfo::exists(korgacrc)) { if (overwriteConfigMessageBox(korgacStr)) { copyToFile(korgacrcFile, korgacrc, korgacStr, Utils::configsPath()); } } else { copyToFile(korgacrcFile, korgacrc, korgacStr, Utils::configsPath()); } } const QString freebusyStr(QStringLiteral("freebusyurls")); const KArchiveEntry *freebusyentry = mArchiveDirectory->entry(Utils::dataPath() + QLatin1String("korganizer/") + freebusyStr); if (freebusyentry && freebusyentry->isFile()) { const KArchiveFile *freebusyrcFile = static_cast(freebusyentry); const QString freebusypath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/korganizer/") + freebusyStr; if (QFileInfo::exists(freebusypath)) { //TODO 4.12 merge it. if (overwriteConfigMessageBox(freebusyStr)) { copyToFile(freebusyrcFile, freebusypath, freebusyStr, Utils::dataPath()); } } else { copyToFile(freebusyrcFile, freebusypath, freebusyStr, Utils::dataPath()); } } const KArchiveEntry *templateEntry = mArchiveDirectory->entry(Utils::dataPath() + QLatin1String("korganizer/templates/")); if (templateEntry && templateEntry->isDirectory()) { //TODO 4.12 verify if template already exists. const QString templatePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QLatin1String("korganizer/templates/"); const KArchiveDirectory *templateDir = static_cast(templateEntry); if (!templateDir->copyTo(templatePath)) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "template cannot copy to " << templatePath; } } const KArchiveEntry *designerEntry = mArchiveDirectory->entry(Utils::dataPath() + QLatin1String("korganizer/designer/")); if (designerEntry && designerEntry->isDirectory()) { const QString templatePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QLatin1String("korganizer/designer/"); const KArchiveDirectory *templateDir = static_cast(designerEntry); if (!templateDir->copyTo(templatePath)) { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << "template cannot copy to " << templatePath; } } restoreUiRcFile(QStringLiteral("korganizerui.rc"), QStringLiteral("korganizer")); restoreUiRcFile(QStringLiteral("korganizer_part.rc"), QStringLiteral("korganizer")); restoreConfigFile(QStringLiteral("eventviewsrc")); Q_EMIT info(i18n("Config restored.")); QTimer::singleShot(0, this, &ImportCalendarJob::slotNextStep); } void ImportCalendarJob::importkorganizerConfig(const KArchiveFile *file, const QString &config, const QString &filename, const QString &prefix) { copyToFile(file, config, filename, prefix); KSharedConfig::Ptr korganizerConfig = KSharedConfig::openConfig(config); const QString collectionsStr(QStringLiteral("GlobalCollectionSelection")); if (korganizerConfig->hasGroup(collectionsStr)) { KConfigGroup group = korganizerConfig->group(collectionsStr); const QString selectionKey(QStringLiteral("Selection")); convertRealPathToCollectionList(group, selectionKey, true); } korganizerConfig->sync(); } diff --git a/core/calendar/importcalendarjob.h b/core/calendar/importcalendarjob.h index 36a65ae..31da504 100644 --- a/core/calendar/importcalendarjob.h +++ b/core/calendar/importcalendarjob.h @@ -1,48 +1,48 @@ /* Copyright (C) 2012-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef IMPORTCALENDARJOB_H #define IMPORTCALENDARJOB_H #include "abstractimportexportjob.h" #include "pimsettingexportercore_private_export.h" class ArchiveStorage; class KArchive; class PIMSETTINGEXPORTER_TESTS_EXPORT ImportCalendarJob : public AbstractImportExportJob { Q_OBJECT public: explicit ImportCalendarJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep); ~ImportCalendarJob() override; void start() override; protected: void slotNextStep() override; private: bool isAConfigFile(const QString &name) const override; void importkorganizerConfig(const KArchiveFile *file, const QString &config, const QString &filename, const QString &prefix); void restoreResources(); void restoreConfig(); - void addSpecificResourceSettings(KSharedConfig::Ptr resourceConfig, const QString &resourceName, QMap &settings) override; + void addSpecificResourceSettings(const KSharedConfig::Ptr &resourceConfig, const QString &resourceName, QMap &settings) override; }; #endif // IMPORTCALENDARJOB_H diff --git a/core/mail/importmailjob.cpp b/core/mail/importmailjob.cpp index 93e58c3..6b60087 100644 --- a/core/mail/importmailjob.cpp +++ b/core/mail/importmailjob.cpp @@ -1,1345 +1,1345 @@ /* Copyright (C) 2012-2019 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "importmailjob.h" #include "archivestorage.h" #include "MailCommon/FilterManager" #include "MailCommon/FilterImporterExporter" #include "MailCommon/MailUtil" #include #include #include #include #include #include #include #include #include #include #include #include #include "pimsettingexportcore_debug.h" #include #include #include #include #include #include #include #include #include using namespace Akonadi; namespace { inline const QString storeMails() { return QStringLiteral("backupmail/"); } } ImportMailJob::ImportMailJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep) : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep) { initializeImportJob(); } ImportMailJob::~ImportMailJob() { } void ImportMailJob::start() { Q_EMIT title(i18n("Starting to import KMail settings...")); createProgressDialog(i18n("Import KMail settings")); mArchiveDirectory = archive()->directory(); searchAllMailsFiles(mArchiveDirectory, QString()); if (!mFileList.isEmpty() || !mListResourceFile.isEmpty()) { initializeListStep(); QTimer::singleShot(0, this, &ImportMailJob::slotNextStep); } else { Q_EMIT jobFinished(); } } void ImportMailJob::slotNextStep() { ++mIndex; if (mIndex < mListStep.count()) { const Utils::StoredType type = mListStep.at(mIndex); if (type == Utils::MailTransport) { restoreTransports(); } else if (type == Utils::Mails) { restoreMails(); } else if (type == Utils::Resources) { restoreResources(); } else if (type == Utils::Identity) { restoreIdentity(); } else if (type == Utils::Config) { restoreConfig(); } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << Q_FUNC_INFO << " not supported type " << type; slotNextStep(); } } else { Q_EMIT jobFinished(); } } void ImportMailJob::searchAllMailsFiles(const KArchiveDirectory *dir, const QString &prefix) { const QStringList lst = dir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = dir->entry(entryName); if (entry && entry->isDirectory()) { const QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName; if (entryName == QLatin1String("mails")) { storeMailArchiveResource(static_cast(entry), entryName); } else { searchAllMailsFiles(static_cast(entry), newPrefix); } } else if (entry) { const QString fileName = prefix.isEmpty() ? entry->name() : prefix + QLatin1Char('/') + entry->name(); mFileList << fileName; } } } void ImportMailJob::storeMailArchiveResource(const KArchiveDirectory *dir, const QString &prefix) { const QStringList lst = dir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = dir->entry(entryName); if (entry && entry->isDirectory()) { const KArchiveDirectory *resourceDir = static_cast(entry); const QStringList lst = resourceDir->entries(); if (lst.count() >= 2) { const QString archPath(prefix + QLatin1Char('/') + entryName + QLatin1Char('/')); resourceFiles files; for (const QString &name : lst) { if (name.endsWith(QLatin1String("rc")) && (name.contains(QLatin1String("akonadi_mbox_resource_")) || name.contains(QLatin1String("akonadi_mixedmaildir_resource_")) || name.contains(QLatin1String("akonadi_maildir_resource_")))) { files.akonadiConfigFile = archPath + name; } else if (name.startsWith(Utils::prefixAkonadiConfigFile())) { files.akonadiAgentConfigFile = archPath + name; } else { files.akonadiResources = archPath + name; } } //Show debug: files.debug(); mListResourceFile.append(files); } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " Problem in archive. number of file " << lst.count(); } } } } void ImportMailJob::importMailTransport(const QString &tempDirName) { KSharedConfig::Ptr transportConfig = KSharedConfig::openConfig(tempDirName + QLatin1Char('/') + QLatin1String("mailtransports")); int defaultTransport = -1; if (transportConfig->hasGroup(QStringLiteral("General"))) { KConfigGroup group = transportConfig->group(QStringLiteral("General")); defaultTransport = group.readEntry(QStringLiteral("default-transport"), -1); } const QStringList transportList = transportConfig->groupList().filter(QRegularExpression(QStringLiteral("Transport \\d+"))); for (const QString &transport : transportList) { KConfigGroup group = transportConfig->group(transport); const int transportId = group.readEntry(QStringLiteral("id"), -1); if (transportId == -1) { qCWarning(PIMSETTINGEXPORTERCORE_LOG) << "Mail Transport is incorrect. Missing Id"; continue; } const QString identifierStr(QStringLiteral("identifier")); if (group.hasKey(identifierStr)) { const QString identifierValue = group.readEntry(identifierStr); if (!identifierValue.isEmpty()) { if (identifierValue == QLatin1String("sendmail") || identifierValue == QLatin1String("akonadi_ewsmta_resource")) { MailTransport::Transport *mt = MailTransport::TransportManager::self()->createTransport(); mt->setName(group.readEntry(QStringLiteral("name"))); const QString hostStr(QStringLiteral("host")); if (group.hasKey(hostStr)) { mt->setHost(group.readEntry(hostStr)); } mt->setIdentifier(identifierValue); addMailTransport(mt, defaultTransport, transportId); } else { qCWarning(PIMSETTINGEXPORTERCORE_LOG) << "Unknown identifier type " << identifierValue; } } else { qCWarning(PIMSETTINGEXPORTERCORE_LOG) << "identifier value is empty"; } } else { MailTransport::Transport *mt = MailTransport::TransportManager::self()->createTransport(); mt->setName(group.readEntry(QStringLiteral("name"))); const QString hostStr(QStringLiteral("host")); if (group.hasKey(hostStr)) { mt->setHost(group.readEntry(hostStr)); } const QString portStr(QStringLiteral("port")); if (group.hasKey(portStr)) { mt->setPort(group.readEntry(portStr, -1)); } const QString userNameStr(QStringLiteral("user")); if (group.hasKey(userNameStr)) { mt->setUserName(group.readEntry(userNameStr)); } const QString precommandStr(QStringLiteral("precommand")); if (group.hasKey(precommandStr)) { mt->setPrecommand(group.readEntry(precommandStr)); } const QString requiresAuthenticationStr(QStringLiteral("auth")); if (group.hasKey(requiresAuthenticationStr)) { mt->setRequiresAuthentication(group.readEntry(requiresAuthenticationStr, false)); } const QString specifyHostnameStr(QStringLiteral("specifyHostname")); if (group.hasKey(specifyHostnameStr)) { mt->setSpecifyHostname(group.readEntry(specifyHostnameStr, false)); } const QString localHostnameStr(QStringLiteral("localHostname")); if (group.hasKey(localHostnameStr)) { mt->setLocalHostname(group.readEntry(localHostnameStr)); } const QString specifySenderOverwriteAddressStr(QStringLiteral("specifySenderOverwriteAddress")); if (group.hasKey(specifySenderOverwriteAddressStr)) { mt->setSpecifySenderOverwriteAddress(group.readEntry(specifySenderOverwriteAddressStr, false)); } const QString storePasswordStr(QStringLiteral("storepass")); if (group.hasKey(storePasswordStr)) { mt->setStorePassword(group.readEntry(storePasswordStr, false)); } const QString senderOverwriteAddressStr(QStringLiteral("senderOverwriteAddress")); if (group.hasKey(senderOverwriteAddressStr)) { mt->setSenderOverwriteAddress(group.readEntry(senderOverwriteAddressStr)); } const QString encryptionStr(QStringLiteral("encryption")); if (group.hasKey(encryptionStr)) { const QString encryptionType = group.readEntry(encryptionStr, QString()); if (!encryptionType.isEmpty()) { if (encryptionType == QLatin1String("TLS")) { mt->setEncryption(static_cast(MailTransport::TransportBase::EnumEncryption::TLS)); } else if (encryptionType == QLatin1String("SSL")) { mt->setEncryption(static_cast(MailTransport::TransportBase::EnumEncryption::SSL)); } else if (encryptionType == QLatin1String("None")) { mt->setEncryption(static_cast(MailTransport::TransportBase::EnumEncryption::None)); } else { qCWarning(PIMSETTINGEXPORTERCORE_LOG) << "Unknown encryption type " << encryptionType; } } else { qCWarning(PIMSETTINGEXPORTERCORE_LOG) << "Encryption type is empty. It's a bug"; } mt->setEncryption(group.readEntry(encryptionStr, 1)); //TODO verify } const QString authenticationTypeStr(QStringLiteral("authtype")); if (group.hasKey(authenticationTypeStr)) { mt->setAuthenticationType(group.readEntry(authenticationTypeStr, 1)); //TODO verify } addMailTransport(mt, defaultTransport, transportId); } } } void ImportMailJob::restoreTransports() { setProgressDialogLabel(i18n("Restore transports...")); increaseProgressDialog(); const QString path = Utils::transportsPath() + QLatin1String("mailtransports"); if (!mFileList.contains(path)) { Q_EMIT error(i18n("mailtransports file could not be found in the archive.")); } else { Q_EMIT info(i18n("Restore transports...")); const KArchiveEntry *transport = mArchiveDirectory->entry(path); if (transport && transport->isFile()) { const KArchiveFile *fileTransport = static_cast(transport); fileTransport->copyTo(mTempDirName); importMailTransport(mTempDirName); Q_EMIT info(i18n("Transports restored.")); } else { Q_EMIT error(i18n("Failed to restore transports file.")); } } QTimer::singleShot(0, this, &ImportMailJob::slotNextStep); } void ImportMailJob::addMailTransport(MailTransport::Transport *mt, int defaultTransport, int transportId) { mt->forceUniqueName(); mt->save(); MailTransport::TransportManager::self()->addTransport(mt); if (transportId == defaultTransport) { MailTransport::TransportManager::self()->setDefaultTransport(mt->id()); } mHashTransport.insert(transportId, mt->id()); } void ImportMailJob::restoreResources() { increaseProgressDialog(); setProgressDialogLabel(i18n("Restore resources...")); Q_EMIT info(i18n("Restore resources...")); QDir dir(mTempDirName); dir.mkdir(Utils::resourcesPath()); for (const QString &filename : qAsConst(mFileList)) { if (filename.startsWith(Utils::resourcesPath())) { const KArchiveEntry *fileEntry = mArchiveDirectory->entry(filename); if (fileEntry && fileEntry->isFile()) { const KArchiveFile *file = static_cast(fileEntry); const QString destDirectory = mTempDirName + QLatin1Char('/') + Utils::resourcesPath(); file->copyTo(destDirectory); const QString filename(file->name()); const QString resourceFileName = destDirectory + QLatin1Char('/') + filename; KSharedConfig::Ptr resourceConfig = KSharedConfig::openConfig(resourceFileName); QMap settings; if (filename.contains(QLatin1String("pop3"))) { KConfigGroup general = resourceConfig->group(QStringLiteral("General")); if (general.hasKey(QStringLiteral("login"))) { settings.insert(QStringLiteral("Login"), general.readEntry("login")); } if (general.hasKey(QStringLiteral("host"))) { settings.insert(QStringLiteral("Host"), general.readEntry("host")); } if (general.hasKey(QStringLiteral("port"))) { settings.insert(QStringLiteral("Port"), general.readEntry("port", 110)); } if (general.hasKey(QStringLiteral("authenticationMethod"))) { settings.insert(QStringLiteral("AuthenticationMethod"), general.readEntry("authenticationMethod", 7)); } if (general.hasKey(QStringLiteral("useSSL"))) { settings.insert(QStringLiteral("UseSSL"), general.readEntry("useSSL", false)); } if (general.hasKey(QStringLiteral("useTLS"))) { settings.insert(QStringLiteral("UseTLS"), general.readEntry("useTLS", false)); } if (general.hasKey(QStringLiteral("pipelining"))) { settings.insert(QStringLiteral("Pipelining"), general.readEntry("pipelining", false)); } if (general.hasKey(QStringLiteral("leaveOnServer"))) { settings.insert(QStringLiteral("LeaveOnServer"), general.readEntry("leaveOnServer", false)); } if (general.hasKey(QStringLiteral("leaveOnServerDays"))) { settings.insert(QStringLiteral("LeaveOnServerDays"), general.readEntry("leaveOnServerDays", -1)); } if (general.hasKey(QStringLiteral("leaveOnServerCount"))) { settings.insert(QStringLiteral("LeaveOnServerCount"), general.readEntry("leaveOnServerCount", -1)); } if (general.hasKey(QStringLiteral("leaveOnServerSize"))) { settings.insert(QStringLiteral("LeaveOnServerSize"), general.readEntry("leaveOnServerSize", -1)); } if (general.hasKey(QStringLiteral("filterOnServer"))) { settings.insert(QStringLiteral("FilterOnServer"), general.readEntry("filterOnServer", false)); } if (general.hasKey(QStringLiteral("filterCheckSize"))) { settings.insert(QStringLiteral("FilterCheckSize"), general.readEntry("filterCheckSize")); } if (general.hasKey(QStringLiteral("targetCollection"))) { const Akonadi::Collection::Id collection = convertPathToId(general.readEntry("targetCollection")); if (collection != -1) { settings.insert(QStringLiteral("TargetCollection"), collection); } } if (general.hasKey(QStringLiteral("precommand"))) { settings.insert(QStringLiteral("Precommand"), general.readEntry("precommand")); } if (general.hasKey(QStringLiteral("intervalCheckEnabled"))) { settings.insert(QStringLiteral("IntervalCheckEnabled"), general.readEntry("intervalCheckEnabled", false)); } if (general.hasKey(QStringLiteral("intervalCheckInterval"))) { settings.insert(QStringLiteral("IntervalCheckInterval"), general.readEntry("intervalCheckInterval", 5)); } KConfigGroup leaveOnserver = resourceConfig->group(QStringLiteral("LeaveOnServer")); if (leaveOnserver.hasKey(QStringLiteral("seenUidList"))) { settings.insert(QStringLiteral("SeenUidList"), leaveOnserver.readEntry("seenUidList", QStringList())); } #if 0 if (leaveOnserver.hasKey(QStringLiteral("seenUidTimeList"))) { //FIXME //settings.insert(QLatin1String("SeenUidTimeList"),QVariant::fromValue >(leaveOnserver.readEntry("seenUidTimeList",QList()))); } #endif if (leaveOnserver.hasKey(QStringLiteral("downloadLater"))) { settings.insert(QStringLiteral("DownloadLater"), leaveOnserver.readEntry("downloadLater", QStringList())); } const QString newResource = mCreateResource->createResource(QStringLiteral("akonadi_pop3_resource"), filename, settings); if (!newResource.isEmpty()) { mHashResources.insert(filename, newResource); infoAboutNewResource(newResource); } } else if (filename.contains(QLatin1String("imap")) || filename.contains(QLatin1String("kolab_")) || filename.contains(QLatin1String("gmail_"))) { KConfigGroup network = resourceConfig->group(QStringLiteral("network")); if (network.hasKey(QStringLiteral("Authentication"))) { settings.insert(QStringLiteral("Authentication"), network.readEntry("Authentication", 1)); } if (network.hasKey(QStringLiteral("ImapPort"))) { settings.insert(QStringLiteral("ImapPort"), network.readEntry("ImapPort", 993)); } if (network.hasKey(QStringLiteral("ImapServer"))) { settings.insert(QStringLiteral("ImapServer"), network.readEntry("ImapServer")); } if (network.hasKey(QStringLiteral("Safety"))) { settings.insert(QStringLiteral("Safety"), network.readEntry("Safety", "SSL")); } if (network.hasKey(QStringLiteral("SubscriptionEnabled"))) { settings.insert(QStringLiteral("SubscriptionEnabled"), network.readEntry("SubscriptionEnabled", false)); } if (network.hasKey(QStringLiteral("UserName"))) { settings.insert(QStringLiteral("UserName"), network.readEntry("UserName")); } if (network.hasKey(QStringLiteral("SessionTimeout"))) { settings.insert(QStringLiteral("SessionTimeout"), network.readEntry("SessionTimeout", 30)); } KConfigGroup cache = resourceConfig->group(QStringLiteral("cache")); if (cache.hasKey(QStringLiteral("AccountIdentity"))) { const int identity = cache.readEntry("AccountIdentity", -1); if (identity != -1) { if (mHashIdentity.contains(identity)) { settings.insert(QStringLiteral("AccountIdentity"), mHashIdentity.value(identity)); } else { settings.insert(QStringLiteral("AccountIdentity"), identity); } } } if (cache.hasKey(QStringLiteral("IntervalCheckEnabled"))) { settings.insert(QStringLiteral("IntervalCheckEnabled"), cache.readEntry("IntervalCheckEnabled", true)); } if (cache.hasKey(QStringLiteral("RetrieveMetadataOnFolderListing"))) { settings.insert(QStringLiteral("RetrieveMetadataOnFolderListing"), cache.readEntry("RetrieveMetadataOnFolderListing", true)); } if (cache.hasKey(QStringLiteral("AutomaticExpungeEnabled"))) { settings.insert(QStringLiteral("AutomaticExpungeEnabled"), cache.readEntry("AutomaticExpungeEnabled", true)); } if (cache.hasKey(QStringLiteral("DisconnectedModeEnabled"))) { settings.insert(QStringLiteral("DisconnectedModeEnabled"), cache.readEntry("DisconnectedModeEnabled", false)); } if (cache.hasKey(QStringLiteral("IntervalCheckTime"))) { settings.insert(QStringLiteral("IntervalCheckTime"), cache.readEntry("IntervalCheckTime", -1)); } if (cache.hasKey(QStringLiteral("UseDefaultIdentity"))) { settings.insert(QStringLiteral("UseDefaultIdentity"), cache.readEntry("UseDefaultIdentity", true)); } if (cache.hasKey(QStringLiteral("TrashCollection"))) { const Akonadi::Collection::Id collection = convertPathToId(cache.readEntry("TrashCollection")); if (collection != -1) { settings.insert(QStringLiteral("TrashCollection"), collection); } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " Use default trash folder"; } } KConfigGroup siever = resourceConfig->group(QStringLiteral("siever")); if (siever.hasKey(QStringLiteral("SieveSupport"))) { settings.insert(QStringLiteral("SieveSupport"), siever.readEntry("SieveSupport", false)); } if (siever.hasKey(QStringLiteral("SieveReuseConfig"))) { settings.insert(QStringLiteral("SieveReuseConfig"), siever.readEntry("SieveReuseConfig", true)); } if (siever.hasKey(QStringLiteral("SievePort"))) { settings.insert(QStringLiteral("SievePort"), siever.readEntry("SievePort", 4190)); } if (siever.hasKey(QStringLiteral("SieveAlternateUrl"))) { settings.insert(QStringLiteral("SieveAlternateUrl"), siever.readEntry("SieveAlternateUrl")); } if (siever.hasKey(QStringLiteral("AlternateAuthentication"))) { settings.insert(QStringLiteral("AlternateAuthentication"), siever.readEntry("AlternateAuthentication")); } if (siever.hasKey(QStringLiteral("SieveVacationFilename"))) { settings.insert(QStringLiteral("SieveVacationFilename"), siever.readEntry("SieveVacationFilename")); } if (siever.hasKey(QStringLiteral("SieveCustomUsername"))) { settings.insert(QStringLiteral("SieveCustomUsername"), siever.readEntry("SieveCustomUsername")); } if (siever.hasKey(QStringLiteral("SieveCustomAuthentification"))) { settings.insert(QStringLiteral("SieveCustomAuthentification"), siever.readEntry("SieveCustomAuthentification")); } QString newResource; if (filename.contains(QLatin1String("kolab_"))) { newResource = mCreateResource->createResource(QStringLiteral("akonadi_kolab_resource"), filename, settings); } else if (filename.contains(QLatin1String("gmail_"))) { newResource = mCreateResource->createResource(QStringLiteral("akonadi_gmail_resource"), filename, settings); } else { newResource = mCreateResource->createResource(QStringLiteral("akonadi_imap_resource"), filename, settings); } if (!newResource.isEmpty()) { mHashResources.insert(filename, newResource); infoAboutNewResource(newResource); } } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " problem with resource"; } } } } //TODO synctree ? Q_EMIT info(i18n("Resources restored.")); QTimer::singleShot(0, this, &ImportMailJob::slotNextStep); } void ImportMailJob::restoreMails() { increaseProgressDialog(); setProgressDialogLabel(i18n("Restore mails...")); QStringList listResourceToSync; Q_EMIT info(i18n("Restore mails...")); QDir dir(mTempDirName); dir.mkdir(Utils::mailsPath()); const QString copyToDirName(mTempDirName + QLatin1Char('/') + Utils::mailsPath()); const int numberOfResourceFile = mListResourceFile.size(); for (int i = 0; i < numberOfResourceFile; ++i) { resourceFiles value = mListResourceFile.at(i); value.debug(); const QString resourceFile = value.akonadiConfigFile; const KArchiveEntry *fileResouceEntry = mArchiveDirectory->entry(resourceFile); if (fileResouceEntry && fileResouceEntry->isFile()) { const KArchiveFile *file = static_cast(fileResouceEntry); file->copyTo(copyToDirName); QString resourceName(file->name()); QString filename(file->name()); //qCDebug(PIMSETTINGEXPORTERCORE_LOG)<<" filename "<entry(agentConfigFile); if (akonadiAgentConfigEntry->isFile()) { const KArchiveFile *file = static_cast(akonadiAgentConfigEntry); file->copyTo(copyToDirName); resourceName = file->name(); filename = Utils::akonadiAgentName(copyToDirName + QLatin1Char('/') + resourceName); } } QMap settings; if (resourceName.contains(QLatin1String("akonadi_mbox_resource_"))) { const QString dataFile = value.akonadiResources; const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(dataFile); if (dataResouceEntry->isFile()) { const KArchiveFile *file = static_cast(dataResouceEntry); file->copyTo(newUrl); } settings.insert(QStringLiteral("Path"), newUrl); KConfigGroup general = resourceConfig->group(QStringLiteral("General")); if (general.hasKey(QStringLiteral("DisplayName"))) { settings.insert(QStringLiteral("DisplayName"), general.readEntry(QStringLiteral("DisplayName"))); } if (general.hasKey(QStringLiteral("ReadOnly"))) { settings.insert(QStringLiteral("ReadOnly"), general.readEntry(QStringLiteral("ReadOnly"), false)); } if (general.hasKey(QStringLiteral("MonitorFile"))) { settings.insert(QStringLiteral("MonitorFile"), general.readEntry(QStringLiteral("MonitorFile"), false)); } if (resourceConfig->hasGroup(QStringLiteral("Locking"))) { KConfigGroup locking = resourceConfig->group(QStringLiteral("Locking")); if (locking.hasKey(QStringLiteral("Lockfile"))) { settings.insert(QStringLiteral("Lockfile"), locking.readEntry(QStringLiteral("Lockfile"))); } //TODO verify if (locking.hasKey(QStringLiteral("LockfileMethod"))) { settings.insert(QStringLiteral("LockfileMethod"), locking.readEntry(QStringLiteral("LockfileMethod"), 4)); } } if (resourceConfig->hasGroup(QStringLiteral("Compacting"))) { KConfigGroup compacting = resourceConfig->group(QStringLiteral("Compacting")); if (compacting.hasKey(QStringLiteral("CompactFrequency"))) { settings.insert(QStringLiteral("CompactFrequency"), compacting.readEntry(QStringLiteral("CompactFrequency"), 1)); } if (compacting.hasKey(QStringLiteral("MessageCount"))) { settings.insert(QStringLiteral("MessageCount"), compacting.readEntry(QStringLiteral("MessageCount"), 50)); } } const QString newResource = mCreateResource->createResource(QStringLiteral("akonadi_mbox_resource"), filename, settings); if (!newResource.isEmpty()) { mHashResources.insert(filename, newResource); infoAboutNewResource(newResource); } } else if (resourceName.contains(QLatin1String("akonadi_maildir_resource_")) || resourceName.contains(QLatin1String("akonadi_mixedmaildir_resource_"))) { settings.insert(QStringLiteral("Path"), newUrl); KConfigGroup general = resourceConfig->group(QStringLiteral("General")); if (general.hasKey(QStringLiteral("TopLevelIsContainer"))) { settings.insert(QStringLiteral("TopLevelIsContainer"), general.readEntry(QStringLiteral("TopLevelIsContainer"), false)); } if (general.hasKey(QStringLiteral("ReadOnly"))) { settings.insert(QStringLiteral("ReadOnly"), general.readEntry(QStringLiteral("ReadOnly"), false)); } if (general.hasKey(QStringLiteral("MonitorFilesystem"))) { settings.insert(QStringLiteral("MonitorFilesystem"), general.readEntry(QStringLiteral("MonitorFilesystem"), true)); } const QString newResource = mCreateResource->createResource(resourceName.contains(QLatin1String("akonadi_mixedmaildir_resource_")) ? QStringLiteral("akonadi_mixedmaildir_resource") : QStringLiteral("akonadi_maildir_resource"), filename, settings); if (!newResource.isEmpty()) { mHashResources.insert(filename, newResource); infoAboutNewResource(newResource); } const QString mailFile = value.akonadiResources; const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(mailFile); if (dataResouceEntry && dataResouceEntry->isFile()) { const KArchiveFile *file = static_cast(dataResouceEntry); //TODO Fix me not correct zip filename. extractZipFile(file, copyToDirName, newUrl); } listResourceToSync << newResource; } else { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " resource name not supported " << resourceName; } //qCDebug(PIMSETTINGEXPORTERCORE_LOG)<<"url "<entry(filtersPath); if (filter && filter->isFile()) { const KArchiveFile *fileFilter = static_cast(filter); fileFilter->copyTo(mTempDirName); const QString filterFileName(mTempDirName + QLatin1Char('/') + QLatin1String("filters")); KSharedConfig::Ptr filtersConfig = KSharedConfig::openConfig(filterFileName); const QStringList filterList = filtersConfig->groupList().filter(QRegularExpression(QStringLiteral("Filter #\\d+"))); for (const QString &filterStr : filterList) { KConfigGroup group = filtersConfig->group(filterStr); const QString accountStr(QStringLiteral("accounts-set")); if (group.hasKey(accountStr)) { const QString accounts = group.readEntry(accountStr); if (!accounts.isEmpty()) { const QStringList lstAccounts = accounts.split(QLatin1Char(',')); QStringList newLstAccounts; for (const QString &acc : lstAccounts) { if (mHashResources.contains(acc)) { newLstAccounts.append(mHashResources.value(acc)); } else { newLstAccounts.append(acc); } } group.writeEntry(accountStr, newLstAccounts); } } const int numActions = group.readEntry("actions", 0); QString actName; QString argsName; for (int i = 0; i < numActions; ++i) { actName.sprintf("action-name-%d", i); argsName.sprintf("action-args-%d", i); const QString actValue = group.readEntry(actName); if (actValue == QLatin1String("set identity")) { const int argsValue = group.readEntry(argsName, -1); if (argsValue != -1) { if (mHashIdentity.contains(argsValue)) { group.writeEntry(argsName, mHashIdentity.value(argsValue)); } } } else if (actValue == QLatin1String("set transport")) { const int argsValue = group.readEntry(argsName, -1); if (argsValue != -1) { if (mHashTransport.contains(argsValue)) { group.writeEntry(argsName, mHashTransport.value(argsValue)); } } } } } filtersConfig->sync(); bool canceled = false; MailCommon::FilterImporterExporter exportFilters; QList lstFilter = exportFilters.importFilters(canceled, MailCommon::FilterImporterExporter::KMailFilter, filterFileName); if (canceled) { MailCommon::FilterManager::instance()->appendFilters(lstFilter); } } } const QString kmailsnippetrcStr(QStringLiteral("kmailsnippetrc")); const KArchiveEntry *kmailsnippetentry = mArchiveDirectory->entry(Utils::configsPath() + kmailsnippetrcStr); if (kmailsnippetentry && kmailsnippetentry->isFile()) { const KArchiveFile *kmailsnippet = static_cast(kmailsnippetentry); const QString kmailsnippetrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kmailsnippetrcStr; if (QFileInfo::exists(kmailsnippetrc)) { //TODO 4.13 allow to merge config. if (overwriteConfigMessageBox(kmailsnippetrcStr)) { copyToFile(kmailsnippet, kmailsnippetrc, kmailsnippetrcStr, Utils::configsPath()); } } else { copyToFile(kmailsnippet, kmailsnippetrc, kmailsnippetrcStr, Utils::configsPath()); } } const QString labldaprcStr(QStringLiteral("kabldaprc")); const KArchiveEntry *kabldapentry = mArchiveDirectory->entry(Utils::configsPath() + labldaprcStr); if (kabldapentry && kabldapentry->isFile()) { const KArchiveFile *kabldap = static_cast(kabldapentry); const QString kabldaprc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + labldaprcStr; if (QFileInfo::exists(kabldaprc)) { const int result = mergeConfigMessageBox(labldaprcStr); if (result == KMessageBox::Yes) { copyToFile(kabldap, kabldaprc, labldaprcStr, Utils::configsPath()); } else if (result == KMessageBox::No) { mergeLdapConfig(kabldap, labldaprcStr, Utils::configsPath()); } } else { copyToFile(kabldap, kabldaprc, labldaprcStr, Utils::configsPath()); } } const QString archiveconfigurationrcStr(QStringLiteral("akonadi_archivemail_agentrc")); const KArchiveEntry *archiveconfigurationentry = mArchiveDirectory->entry(Utils::configsPath() + archiveconfigurationrcStr); if (archiveconfigurationentry && archiveconfigurationentry->isFile()) { const KArchiveFile *archiveconfiguration = static_cast(archiveconfigurationentry); const QString archiveconfigurationrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + archiveconfigurationrcStr; if (QFileInfo::exists(archiveconfigurationrc)) { const int result = mergeConfigMessageBox(archiveconfigurationrcStr); if (result == KMessageBox::Yes) { importArchiveConfig(archiveconfiguration, archiveconfigurationrc, archiveconfigurationrcStr, Utils::configsPath()); } else if (result == KMessageBox::No) { mergeArchiveMailAgentConfig(archiveconfiguration, archiveconfigurationrcStr, Utils::configsPath()); } } else { importArchiveConfig(archiveconfiguration, archiveconfigurationrc, archiveconfigurationrcStr, Utils::configsPath()); } } const QString templatesconfigurationrcStr(QStringLiteral("templatesconfigurationrc")); const KArchiveEntry *templatesconfigurationentry = mArchiveDirectory->entry(Utils::configsPath() + templatesconfigurationrcStr); if (templatesconfigurationentry && templatesconfigurationentry->isFile()) { const KArchiveFile *templatesconfiguration = static_cast(templatesconfigurationentry); const QString templatesconfigurationrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + templatesconfigurationrcStr; if (QFileInfo::exists(templatesconfigurationrc)) { //TODO 4.13 allow to merge config. if (overwriteConfigMessageBox(templatesconfigurationrcStr)) { importTemplatesConfig(templatesconfiguration, templatesconfigurationrc, templatesconfigurationrcStr, Utils::configsPath()); } } else { importTemplatesConfig(templatesconfiguration, templatesconfigurationrc, templatesconfigurationrcStr, Utils::configsPath()); } } const QString kmailStr(QStringLiteral("kmail2rc")); const KArchiveEntry *kmail2rcentry = mArchiveDirectory->entry(Utils::configsPath() + kmailStr); if (kmail2rcentry && kmail2rcentry->isFile()) { const KArchiveFile *kmailrc = static_cast(kmail2rcentry); const QString kmail2rc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kmailStr; if (QFileInfo::exists(kmail2rc)) { if (overwriteConfigMessageBox(kmailStr)) { importKmailConfig(kmailrc, kmail2rc, kmailStr, Utils::configsPath()); } } else { importKmailConfig(kmailrc, kmail2rc, kmailStr, Utils::configsPath()); } } const QString sievetemplatercStr(QStringLiteral("sievetemplaterc")); const KArchiveEntry *sievetemplatentry = mArchiveDirectory->entry(Utils::configsPath() + sievetemplatercStr); if (sievetemplatentry && sievetemplatentry->isFile()) { const KArchiveFile *sievetemplateconfiguration = static_cast(sievetemplatentry); const QString sievetemplaterc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + sievetemplatercStr; if (QFileInfo::exists(sievetemplaterc)) { const int result = mergeConfigMessageBox(sievetemplatercStr); if (result == KMessageBox::Yes) { copyToFile(sievetemplateconfiguration, sievetemplaterc, sievetemplatercStr, Utils::configsPath()); } else if (result == KMessageBox::No) { mergeSieveTemplate(sievetemplateconfiguration, sievetemplatercStr, Utils::configsPath()); } } else { copyToFile(sievetemplateconfiguration, sievetemplaterc, sievetemplatercStr, Utils::configsPath()); } } const QString customTemplateStr(QStringLiteral("customtemplatesrc")); const KArchiveEntry *customtemplatentry = mArchiveDirectory->entry(Utils::configsPath() + customTemplateStr); if (customtemplatentry && customtemplatentry->isFile()) { const KArchiveFile *customtemplateconfiguration = static_cast(customtemplatentry); const QString customtemplaterc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + customTemplateStr; if (QFileInfo::exists(customtemplaterc)) { //TODO 4.13 allow to merge config. if (overwriteConfigMessageBox(customTemplateStr)) { copyToFile(customtemplateconfiguration, customtemplaterc, customTemplateStr, Utils::configsPath()); } } else { copyToFile(customtemplateconfiguration, customtemplaterc, customTemplateStr, Utils::configsPath()); } } const QString adblockStr(QStringLiteral("messagevieweradblockrc")); const KArchiveEntry *adblockentry = mArchiveDirectory->entry(Utils::configsPath() + adblockStr); if (adblockentry && adblockentry->isFile()) { const KArchiveFile *adblockconfiguration = static_cast(adblockentry); const QString adblockrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + adblockStr; if (QFileInfo::exists(adblockrc)) { //TODO 4.13 allow to merge config. if (overwriteConfigMessageBox(adblockStr)) { copyToFile(adblockconfiguration, adblockrc, adblockStr, Utils::configsPath()); } } else { copyToFile(adblockconfiguration, adblockrc, adblockStr, Utils::configsPath()); } } restoreUiRcFile(QStringLiteral("sieveeditorui.rc"), QStringLiteral("sieveeditor")); restoreUiRcFile(QStringLiteral("storageservicemanagerui.rc"), QStringLiteral("storageservicemanager")); restoreUiRcFile(QStringLiteral("headerthemeeditorui.rc"), QStringLiteral("headerthemeeditor")); restoreUiRcFile(QStringLiteral("contactthemeeditorui.rc"), QStringLiteral("contactthemeeditor")); restoreUiRcFile(QStringLiteral("contactprintthemeeditorui.rc"), QStringLiteral("contactprintthemeeditor")); restoreUiRcFile(QStringLiteral("kmreadermainwin.rc"), QStringLiteral("kmail2")); restoreUiRcFile(QStringLiteral("kmcomposerui.rc"), QStringLiteral("kmail2")); restoreUiRcFile(QStringLiteral("kmmainwin.rc"), QStringLiteral("kmail2")); restoreUiRcFile(QStringLiteral("kmail_part.rc"), QStringLiteral("kmail2")); restoreUiRcFile(QStringLiteral("kontactui.rc"), QStringLiteral("kontact")); restoreUiRcFile(QStringLiteral("kleopatra.rc"), QStringLiteral("kleopatra")); restoreUiRcFile(QStringLiteral("kontactsummary_part.rc"), QStringLiteral("kontactsummary")); restoreUiRcFile(QStringLiteral("kwatchgnupgui.rc"), QStringLiteral("kwatchgnupg")); restoreUiRcFile(QStringLiteral("akonadiconsoleui.rc"), QStringLiteral("akonadiconsole")); restoreConfigFile(QStringLiteral("kontactrc")); restoreConfigFile(QStringLiteral("kontact_summaryrc")); restoreConfigFile(QStringLiteral("storageservicerc")); restoreConfigFile(QStringLiteral("kpimbalooblacklist")); restoreConfigFile(QStringLiteral("kleopatrarc")); restoreConfigFile(QStringLiteral("sieveeditorrc")); restoreConfigFile(QStringLiteral("kwatchgnupgrc")); restoreConfigFile(QStringLiteral("pimpluginsrc")); restoreConfigFile(QStringLiteral("texttospeechrc")); restoreConfigFile(QStringLiteral("kleopatracertificateselectiondialogrc")); //Restore notify file const QStringList lstNotify = { QStringLiteral("akonadi_mailfilter_agent.notifyrc"), QStringLiteral("akonadi_sendlater_agent.notifyrc"), QStringLiteral("akonadi_archivemail_agent.notifyrc"), QStringLiteral("kmail2.notifyrc"), QStringLiteral("akonadi_newmailnotifier_agent.notifyrc"), QStringLiteral("akonadi_maildispatcher_agent.notifyrc"), QStringLiteral("akonadi_followupreminder_agent.notifyrc"), QStringLiteral("messageviewer.notifyrc"), QStringLiteral("storageservicemanager.notifyrc") }; //We can't merge it. for (const QString &filename : lstNotify) { restoreConfigFile(filename); } importSimpleFilesInDirectory(QStringLiteral("/autocorrect/")); importSimpleFilesInDirectory(QStringLiteral("/gravatar/")); const KArchiveEntry *kmail2Entry = mArchiveDirectory->entry(Utils::dataPath() + QLatin1String("kmail2/adblockrules_local")); if (kmail2Entry && kmail2Entry->isFile()) { const KArchiveFile *entry = static_cast(kmail2Entry); const QString adblockPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QLatin1String("kmail2/adblockrules_local"); if (QFileInfo::exists(adblockPath)) { if (overwriteConfigMessageBox(QStringLiteral("adblockrules_local"))) { copyToFile(entry, QString(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/") + QLatin1String("kmail2/adblockrules_local")), QStringLiteral("adblockrules_local"), Utils::dataPath() + QLatin1String("kmail2/")); } } else { copyToFile(entry, QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QLatin1String("kmail2/adblockrules_local"), QStringLiteral( "adblockrules_local"), Utils::dataPath() + QLatin1String("kmail2/")); } } importDataSubdirectory(QStringLiteral("/messageviewer/themes/")); importDataSubdirectory(QStringLiteral("/messageviewerplugins/")); Q_EMIT info(i18n("Config restored.")); QTimer::singleShot(0, this, &ImportMailJob::slotNextStep); } void ImportMailJob::importSimpleFilesInDirectory(const QString &relativePath) { const KArchiveEntry *autocorrectionEntry = mArchiveDirectory->entry(Utils::dataPath() + relativePath); if (autocorrectionEntry && autocorrectionEntry->isDirectory()) { const KArchiveDirectory *autoCorrectionDir = static_cast(autocorrectionEntry); const QStringList lst = autoCorrectionDir->entries(); for (const QString &entryName : lst) { const KArchiveEntry *entry = autoCorrectionDir->entry(entryName); if (entry && entry->isFile()) { const KArchiveFile *autocorrectionFile = static_cast(entry); const QString name = autocorrectionFile->name(); QString autocorrectionPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + relativePath + QLatin1Char('/') + name; if (QFileInfo::exists(autocorrectionPath)) { if (overwriteConfigMessageBox(name)) { copyToFile(autocorrectionFile, autocorrectionPath, name, Utils::dataPath() + relativePath); } } else { autocorrectionPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + relativePath; copyToFile(autocorrectionFile, autocorrectionPath + QLatin1Char('/') + name, name, Utils::dataPath() + relativePath); } } } } } void ImportMailJob::registerSpecialCollection(Akonadi::SpecialMailCollections::Type type, qint64 colId) { auto fetch = new Akonadi::CollectionFetchJob(Akonadi::Collection(colId), Akonadi::CollectionFetchJob::Base, this); connect(fetch, &Akonadi::CollectionFetchJob::collectionsReceived, - this, [this, type](const Akonadi::Collection::List &cols) { + this, [ type](const Akonadi::Collection::List &cols) { if (cols.count() != 1) { return; } Akonadi::SpecialMailCollections::self()->registerCollection(type, cols.first()); }); } void ImportMailJob::restoreIdentity() { increaseProgressDialog(); setProgressDialogLabel(i18n("Restoring identities...")); const QString path(Utils::identitiesPath() + QLatin1String("emailidentities")); if (!mFileList.contains(path)) { Q_EMIT error(i18n("emailidentities file could not be found in the archive.")); } else { Q_EMIT info(i18n("Restoring identities...")); const KArchiveEntry *identity = mArchiveDirectory->entry(path); if (identity && identity->isFile()) { const KArchiveFile *fileIdentity = static_cast(identity); fileIdentity->copyTo(mTempDirName); KSharedConfig::Ptr identityConfig = KSharedConfig::openConfig(mTempDirName + QLatin1Char('/') + QLatin1String("emailidentities")); KConfigGroup general = identityConfig->group(QStringLiteral("General")); const int defaultIdentity = general.readEntry(QStringLiteral("Default Identity"), -1); const QStringList identityList = identityConfig->groupList().filter(QRegularExpression(QStringLiteral("Identity #\\d+"))); for (const QString &identityStr : identityList) { KConfigGroup group = identityConfig->group(identityStr); int oldUid = -1; const QString uidStr(QStringLiteral("uoid")); if (group.hasKey(uidStr)) { oldUid = group.readEntry(uidStr).toUInt(); group.deleteEntry(uidStr); } const QString fcc(QStringLiteral("Fcc")); qint64 fccId = convertRealPathToCollection(group, fcc); registerSpecialCollection(Akonadi::SpecialMailCollections::SentMail, fccId); const QString draft = QStringLiteral("Drafts"); qint64 draftId = convertRealPathToCollection(group, draft); registerSpecialCollection(Akonadi::SpecialMailCollections::Drafts, draftId); const QString templates = QStringLiteral("Templates"); qint64 templateId = convertRealPathToCollection(group, templates); registerSpecialCollection(Akonadi::SpecialMailCollections::Templates, templateId); if (oldUid != -1) { const QString vcard = QStringLiteral("VCardFile"); if (group.hasKey(vcard)) { const QString vcardFileName = group.readEntry(vcard); if (!vcardFileName.isEmpty()) { QFileInfo fileInfo(vcardFileName); QFile file(vcardFileName); const KArchiveEntry *vcardEntry = mArchiveDirectory->entry(Utils::identitiesPath() + QString::number(oldUid) + QDir::separator() + file.fileName()); if (vcardEntry && vcardEntry->isFile()) { const KArchiveFile *vcardFile = static_cast(vcardEntry); QString vcardFilePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kmail2/%1").arg(fileInfo.fileName()); int i = 1; while (QFileInfo::exists(vcardFileName)) { vcardFilePath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/kmail2/%1_%2").arg(i).arg( fileInfo.fileName()); ++i; } vcardFile->copyTo(QFileInfo(vcardFilePath).absolutePath()); group.writeEntry(vcard, vcardFilePath); } } } } QString name = group.readEntry(QStringLiteral("Name")); KIdentityManagement::Identity *identity = &mIdentityManager->newFromScratch(uniqueIdentityName(name)); group.writeEntry(QStringLiteral("Name"), name); group.sync(); identity->readConfig(group); if (oldUid != -1) { mHashIdentity.insert(oldUid, identity->uoid()); if (oldUid == defaultIdentity) { mIdentityManager->setAsDefault(identity->uoid()); } } mIdentityManager->commit(); } Q_EMIT info(i18n("Identities restored.")); } else { Q_EMIT error(i18n("Failed to restore identity file.")); } } QTimer::singleShot(0, this, &ImportMailJob::slotNextStep); } QString ImportMailJob::uniqueIdentityName(const QString &name) { QString newName(name); int i = 0; while (!mIdentityManager->isUnique(newName)) { newName = QStringLiteral("%1_%2").arg(name).arg(i); ++i; } return newName; } void ImportMailJob::importArchiveConfig(const KArchiveFile *archiveconfiguration, const QString &archiveconfigurationrc, const QString &filename, const QString &prefix) { copyToFile(archiveconfiguration, archiveconfigurationrc, filename, prefix); KSharedConfig::Ptr archiveConfig = KSharedConfig::openConfig(archiveconfigurationrc); copyArchiveMailAgentConfigGroup(archiveConfig, archiveConfig); archiveConfig->sync(); } void ImportMailJob::importFolderArchiveConfig(const KArchiveFile *archiveconfiguration, const QString &archiveconfigurationrc, const QString &filename, const QString &prefix) { copyToFile(archiveconfiguration, archiveconfigurationrc, filename, prefix); KSharedConfig::Ptr archiveConfig = KSharedConfig::openConfig(archiveconfigurationrc); const QStringList archiveList = archiveConfig->groupList().filter(QRegularExpression(QStringLiteral("FolderArchiveAccount "))); for (const QString &str : archiveList) { KConfigGroup oldGroup = archiveConfig->group(str); const Akonadi::Collection::Id id = convertPathToId(oldGroup.readEntry(QStringLiteral("topLevelCollectionId"))); if (id != -1) { oldGroup.writeEntry(QStringLiteral("topLevelCollectionId"), id); } } archiveConfig->sync(); } void ImportMailJob::copyArchiveMailAgentConfigGroup(const KSharedConfig::Ptr &archiveConfigOrigin, const KSharedConfig::Ptr &archiveConfigDestination) { //adapt id const QString archiveGroupPattern = QStringLiteral("ArchiveMailCollection "); const QStringList archiveList = archiveConfigOrigin->groupList().filter(archiveGroupPattern); for (const QString &str : archiveList) { const QString path = str.right(str.length() - archiveGroupPattern.length()); if (!path.isEmpty()) { KConfigGroup oldGroup = archiveConfigOrigin->group(str); const Akonadi::Collection::Id id = convertPathToId(path); if (id != -1) { KConfigGroup newGroup(archiveConfigDestination, archiveGroupPattern + QString::number(id)); oldGroup.copyTo(&newGroup); newGroup.writeEntry(QStringLiteral("saveCollectionId"), id); QUrl path = newGroup.readEntry("storePath", QUrl()); if (!QDir(path.path()).exists()) { newGroup.writeEntry(QStringLiteral("storePath"), QUrl::fromLocalFile(QDir::homePath())); } } oldGroup.deleteGroup(); } } } void ImportMailJob::importTemplatesConfig(const KArchiveFile *templatesconfiguration, const QString &templatesconfigurationrc, const QString &filename, const QString &prefix) { copyToFile(templatesconfiguration, templatesconfigurationrc, filename, prefix); KSharedConfig::Ptr templateConfig = KSharedConfig::openConfig(templatesconfigurationrc); //adapt id const QString templateGroupPattern = QStringLiteral("Templates #"); const QStringList templateList = templateConfig->groupList().filter(templateGroupPattern); for (const QString &str : templateList) { const QString path = str.right(str.length() - templateGroupPattern.length()); if (!path.isEmpty()) { KConfigGroup oldGroup = templateConfig->group(str); const Akonadi::Collection::Id id = convertPathToId(path); if (id != -1) { KConfigGroup newGroup(templateConfig, templateGroupPattern + QString::number(id)); oldGroup.copyTo(&newGroup); } oldGroup.deleteGroup(); } } //adapt identity const QString templateGroupIdentityPattern = QStringLiteral("Templates #IDENTITY_"); const QStringList templateListIdentity = templateConfig->groupList().filter(templateGroupIdentityPattern); for (const QString &str : templateListIdentity) { bool found = false; const int identity = str.rightRef(str.length() - templateGroupIdentityPattern.length()).toInt(&found); if (found) { KConfigGroup oldGroup = templateConfig->group(str); if (mHashIdentity.contains(identity)) { KConfigGroup newGroup(templateConfig, templateGroupPattern + QString::number(mHashIdentity.value(identity))); oldGroup.copyTo(&newGroup); } oldGroup.deleteGroup(); } } templateConfig->sync(); } void ImportMailJob::importKmailConfig(const KArchiveFile *kmailsnippet, const QString &kmail2rc, const QString &filename, const QString &prefix) { copyToFile(kmailsnippet, kmail2rc, filename, prefix); KSharedConfig::Ptr kmailConfig = KSharedConfig::openConfig(kmail2rc); //Be sure to delete Search group const QString search(QStringLiteral("Search")); if (kmailConfig->hasGroup(search)) { KConfigGroup searchGroup = kmailConfig->group(search); searchGroup.deleteGroup(); } //adapt folder id const QString folderGroupPattern = QStringLiteral("Folder-"); const QStringList folderList = kmailConfig->groupList().filter(folderGroupPattern); for (const QString &str : folderList) { const QString path = str.right(str.length() - folderGroupPattern.length()); if (!path.isEmpty()) { KConfigGroup oldGroup = kmailConfig->group(str); const Akonadi::Collection::Id id = convertPathToId(path); if (id != -1) { KConfigGroup newGroup(kmailConfig, folderGroupPattern + QString::number(id)); oldGroup.copyTo(&newGroup); } oldGroup.deleteGroup(); } } const QString accountOrder(QStringLiteral("AccountOrder")); if (kmailConfig->hasGroup(accountOrder)) { KConfigGroup group = kmailConfig->group(accountOrder); const QStringList orderList = group.readEntry(QStringLiteral("order"), QStringList()); QStringList newOrderList; if (!orderList.isEmpty()) { for (const QString &account : orderList) { if (mHashResources.contains(account)) { newOrderList.append(mHashResources.value(account)); } else { newOrderList.append(account); } } } } const QString composerStr(QStringLiteral("Composer")); if (kmailConfig->hasGroup(composerStr)) { KConfigGroup composerGroup = kmailConfig->group(composerStr); const QString previousStr(QStringLiteral("previous-fcc")); convertRealPathToCollection(composerGroup, previousStr); const QString previousIdentityStr(QStringLiteral("previous-identity")); if (composerGroup.hasKey(previousIdentityStr)) { const int identityValue = composerGroup.readEntry(previousIdentityStr, -1); if (identityValue != -1) { if (mHashIdentity.contains(identityValue)) { composerGroup.writeEntry(previousIdentityStr, mHashIdentity.value(identityValue)); } else { composerGroup.writeEntry(previousIdentityStr, identityValue); } } } } const QString collectionFolderViewStr(QStringLiteral("CollectionFolderView")); if (kmailConfig->hasGroup(collectionFolderViewStr)) { KConfigGroup favoriteGroup = kmailConfig->group(collectionFolderViewStr); const QString currentKey(QStringLiteral("Current")); convertRealPathToCollection(favoriteGroup, currentKey, true); const QString expensionKey(QStringLiteral("Expansion")); convertRealPathToCollectionList(favoriteGroup, expensionKey); } const QString generalStr(QStringLiteral("General")); if (kmailConfig->hasGroup(generalStr)) { KConfigGroup generalGroup = kmailConfig->group(generalStr); //Be sure to delete default domain const QString defaultDomainStr(QStringLiteral("Default domain")); if (generalGroup.hasKey(defaultDomainStr)) { generalGroup.deleteEntry(defaultDomainStr); } const QString startupFolderStr(QStringLiteral("startupFolder")); convertRealPathToCollection(generalGroup, startupFolderStr); } const QString resourceGroupPattern = QStringLiteral("Resource "); const QStringList resourceList = kmailConfig->groupList().filter(resourceGroupPattern); for (const QString &str : resourceList) { const QString res = str.right(str.length() - resourceGroupPattern.length()); if (!res.isEmpty()) { KConfigGroup oldGroup = kmailConfig->group(str); if (mHashResources.contains(res)) { KConfigGroup newGroup(kmailConfig, folderGroupPattern + mHashResources.value(res)); oldGroup.copyTo(&newGroup); } oldGroup.deleteGroup(); } } kmailConfig->sync(); } void ImportMailJob::mergeLdapConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix) { QDir dir(mTempDirName); dir.mkdir(prefix); const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix); archivefile->copyTo(copyToDirName); KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename); KConfigGroup grpExisting = existingConfig->group(QStringLiteral("LDAP")); int existingNumberHosts = grpExisting.readEntry(QStringLiteral("NumHosts"), 0); int existingNumberSelectedHosts = grpExisting.readEntry(QStringLiteral("NumSelectedHosts"), 0); KSharedConfig::Ptr importingLdapConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename); KConfigGroup grpImporting = importingLdapConfig->group(QStringLiteral("LDAP")); int importingNumberHosts = grpImporting.readEntry(QStringLiteral("NumHosts"), 0); int importingNumberSelectedHosts = grpImporting.readEntry(QStringLiteral("NumSelectedHosts"), 0); grpExisting.writeEntry(QStringLiteral("NumHosts"), (existingNumberHosts + importingNumberHosts)); grpExisting.writeEntry(QStringLiteral("NumSelectedHosts"), (existingNumberSelectedHosts + importingNumberSelectedHosts)); for (int i = 0; i < importingNumberSelectedHosts; ++i) { const QString auth = grpImporting.readEntry(QStringLiteral("SelectedAuth%1").arg(i), QString()); grpExisting.writeEntry(QStringLiteral("SelectedAuth%1").arg(existingNumberSelectedHosts + i + 1), auth); grpExisting.writeEntry(QStringLiteral("SelectedBase%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedBase%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SelectedBind%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedBind%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SelectedHost%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedHost%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SelectedMech%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedMech%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SelectedPageSize%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedPageSize%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("SelectedPort%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedPort%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("SelectedPwdBind%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedPwdBind%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SelectedSecurity%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedSecurity%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SelectedSizeLimit%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedSizeLimit%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("SelectedTimeLimit%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedTimeLimit%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("SelectedUser%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedUser%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SelectedVersion%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedVersion%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("SelectedUserFilter%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("SelectedUserFilter%1").arg(i), 0)); } for (int i = 0; i < importingNumberHosts; ++i) { grpExisting.writeEntry(QStringLiteral("Auth%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Auth%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("Base%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Base%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("Bind%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Bind%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("Host%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Host%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("Mech%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Mech%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("PageSize%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("PageSize%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("Port%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Port%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("PwdBind%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("PwdBind%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("Security%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Security%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("SizeLimit%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("SizeLimit%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("TimeLimit%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("TimeLimit%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("User%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("User%1").arg(i), QString())); grpExisting.writeEntry(QStringLiteral("Version%1").arg(existingNumberHosts + i + 1), grpImporting.readEntry(QStringLiteral("Version%1").arg(i), 0)); grpExisting.writeEntry(QStringLiteral("UserFilter%1").arg(existingNumberSelectedHosts + i + 1), grpImporting.readEntry(QStringLiteral("UserFilter%1").arg(i), 0)); } grpExisting.sync(); } void ImportMailJob::mergeKmailSnippetConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix) { //TODO QDir dir(mTempDirName); dir.mkdir(prefix); const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix); archivefile->copyTo(copyToDirName); KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename); KSharedConfig::Ptr importingKMailSnipperConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename); } void ImportMailJob::mergeArchiveMailAgentConfig(const KArchiveFile *archivefile, const QString &filename, const QString &prefix) { QDir dir(mTempDirName); dir.mkdir(prefix); const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix); archivefile->copyTo(copyToDirName); KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename); KSharedConfig::Ptr importingArchiveMailAgentConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename); copyArchiveMailAgentConfigGroup(importingArchiveMailAgentConfig, existingConfig); existingConfig->sync(); } void ImportMailJob::mergeSieveTemplate(const KArchiveFile *archivefile, const QString &filename, const QString &prefix) { QDir dir(mTempDirName); dir.mkdir(prefix); const QString copyToDirName(mTempDirName + QLatin1Char('/') + prefix); archivefile->copyTo(copyToDirName); KSharedConfig::Ptr existingConfig = KSharedConfig::openConfig(filename); KSharedConfig::Ptr importingSieveTemplateConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + filename); KConfigGroup grpExisting = existingConfig->group(QStringLiteral("template")); int numberOfExistingTemplate = grpExisting.readEntry(QStringLiteral("templateCount"), 0); KConfigGroup grpImportExisting = importingSieveTemplateConfig->group(QStringLiteral("template")); const int numberOfImportingTemplate = grpImportExisting.readEntry(QStringLiteral("templateCount"), 0); for (int i = 0; i < numberOfImportingTemplate; ++i) { KConfigGroup templateDefine = importingSieveTemplateConfig->group(QStringLiteral("templateDefine_%1").arg(i)); KConfigGroup newTemplateDefineGrp = existingConfig->group(QStringLiteral("templateDefine_%1").arg(numberOfExistingTemplate)); newTemplateDefineGrp.writeEntry(QStringLiteral("Name"), templateDefine.readEntry(QStringLiteral("Name"))); newTemplateDefineGrp.writeEntry(QStringLiteral("Text"), templateDefine.readEntry(QStringLiteral("Text"))); ++numberOfExistingTemplate; newTemplateDefineGrp.sync(); } grpExisting.writeEntry(QStringLiteral("templateCount"), numberOfExistingTemplate); grpExisting.sync(); }