diff --git a/pimsettingexporter/core/abstractimportexportjob.cpp b/pimsettingexporter/core/abstractimportexportjob.cpp index 1ebaa08914..3b16ae4185 100644 --- a/pimsettingexporter/core/abstractimportexportjob.cpp +++ b/pimsettingexporter/core/abstractimportexportjob.cpp @@ -1,576 +1,620 @@ /* Copyright (c) 2012-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; 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 "PimCommon/CreateResource" #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(new KIdentityManagement::IdentityManager(false, this, "mIdentityManager")), mTempDir(Q_NULLPTR), mArchiveDirectory(Q_NULLPTR), mNumberOfStep(numberOfStep), mCreateResource(Q_NULLPTR), mIndex(-1), mImportExportProgressIndicator(new ImportExportProgressIndicatorBase(this)) { mImportExportProgressIndicator->setNumberOfStep(numberOfStep); connect(mImportExportProgressIndicator, &ImportExportProgressIndicatorBase::info, this, &AbstractImportExportJob::info); } AbstractImportExportJob::~AbstractImportExportJob() { delete mCreateResource; delete mIdentityManager; 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; qDebug() << " configrc" << configrc << " configFileName " << configFileName; if (QFile(configrc).exists()) { 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 (QFile(configrc).exists()) { backupFile(configrc, Utils::configsPath(), configrcStr); } } void AbstractImportExportJob::backupFile(const QString &filename, const QString &path, const QString &storedName) { if (QFile(filename).exists()) { const bool fileAdded = archive()->addLocalFile(filename, path + storedName); if (fileAdded) { Q_EMIT info(i18n("\"%1\" backup done.", storedName)); } else { Q_EMIT error(i18n("\"%1\" cannot be exported.", 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) +{ + Q_FOREACH (const QString &entryName, dir->entries()) { + 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) +{ + Q_FOREACH (const QString &entryName, dir->entries()) { + 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; + Q_FOREACH (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 { //Redefine in subclass return true; } bool AbstractImportExportJob::overwriteDirectoryMessageBox(const QString &directory) const { return mImportExportProgressIndicator->overwriteDirectoryMessageBox(directory); } void AbstractImportExportJob::convertRealPathToCollection(KConfigGroup &group, const QString ¤tKey, bool addCollectionPrefix) { 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); } } } } 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()) { Q_FOREACH (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) { if (mHashConvertPathCollectionId.contains(path)) { return mHashConvertPathCollectionId.value(path); } 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 = Utils::akonadiAgentConfigPath(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*/) { //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(); Q_FOREACH (const QString &entryName, zipDir->entries()) { 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 achiveFile(source + QLatin1Char('/') + file->name()); if (!achiveFile.copy(destination + QLatin1Char('/') + file->name())) { Q_EMIT error(i18n("Unable to copy file", 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 (QFile(configNamerc).exists()) { 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 (QFile(configNamerc).exists()) { //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::AkonadiDb) { mListStep << Utils::AkonadiDb; } 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); Q_FOREACH (const QString &entryName, themeDir->entries()) { const KArchiveEntry *entry = themeDir->entry(entryName); if (entry && entry->isDirectory()) { QString subFolderName = entryName; QDir themeDirectory(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + subdirectoryRelativePath + QStringLiteral("/%1").arg(entryName)); int i = 1; while (themeDirectory.exists()) { subFolderName = entryName + QStringLiteral("_%1").arg(i); themeDirectory = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + subdirectoryRelativePath + QStringLiteral("/%1").arg(subFolderName)); ++i; } copyToDirectory(entry, QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + subdirectoryRelativePath + QStringLiteral("/%1").arg(subFolderName)); } } } } diff --git a/pimsettingexporter/core/abstractimportexportjob.h b/pimsettingexporter/core/abstractimportexportjob.h index c79fafa24f..6ed5cd7e28 100644 --- a/pimsettingexporter/core/abstractimportexportjob.h +++ b/pimsettingexporter/core/abstractimportexportjob.h @@ -1,138 +1,140 @@ /* Copyright (c) 2012-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; 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(); private Q_SLOTS: void slotAllResourceSynchronized(); void slotSynchronizeInstanceDone(const QString &, const QString &identifier); void slotSynchronizeInstanceFailed(const QString &instance); void slotTaskCanceled(); 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); void 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); 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; KIdentityManagement::IdentityManager *mIdentityManager; QTemporaryDir *mTempDir; const KArchiveDirectory *mArchiveDirectory; int mNumberOfStep; PimCommon::CreateResource *mCreateResource; QStringList mAgentPaths; QList mListStep; int mIndex; static int sArchiveVersion; private: ImportExportProgressIndicatorBase *mImportExportProgressIndicator; }; #endif // ABSTRACTIMPORTEXPORTJOB_H diff --git a/pimsettingexporter/core/addressbook/importaddressbookjob.cpp b/pimsettingexporter/core/addressbook/importaddressbookjob.cpp index 7220c9ec01..fe929b750f 100644 --- a/pimsettingexporter/core/addressbook/importaddressbookjob.cpp +++ b/pimsettingexporter/core/addressbook/importaddressbookjob.cpp @@ -1,289 +1,245 @@ /* Copyright (c) 2013-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; 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 "PimCommon/CreateResource" #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("Start import KAddressBook settings...")); mArchiveDirectory = archive()->directory(); createProgressDialog(i18n("Import KAddressBook settings")); - searchAllFiles(mArchiveDirectory, QString()); + searchAllFiles(mArchiveDirectory, QString(), QStringLiteral("addressbook")); initializeListStep(); QTimer::singleShot(0, this, SLOT(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(QStringLiteral("akonadi_vcarddir_resource_")) || value.akonadiConfigFile.contains(QStringLiteral("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("/share/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(QStringLiteral("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(QStringLiteral("akonadi_vcarddir_resource_"))) { instanceType = QStringLiteral("akonadi_vcarddir_resource"); } else if (value.akonadiConfigFile.contains(QStringLiteral("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) { 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)); } } } -void ImportAddressbookJob::searchAllFiles(const KArchiveDirectory *dir, const QString &prefix) -{ - Q_FOREACH (const QString &entryName, dir->entries()) { - const KArchiveEntry *entry = dir->entry(entryName); - if (entry && entry->isDirectory()) { - const QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName; - if (entryName == QLatin1String("addressbook")) { - storeAddressBookArchiveResource(static_cast(entry), entryName); - } else { - searchAllFiles(static_cast(entry), newPrefix); - } - } - } -} - bool ImportAddressbookJob::isAConfigFile(const QString &name) const { return name.endsWith(QLatin1String("rc")) && (name.contains(QStringLiteral("akonadi_vcarddir_resource_")) || name.contains(QStringLiteral("akonadi_vcard_resource_")) || name.contains(QStringLiteral("akonadi_contacts_resource_"))); } -void ImportAddressbookJob::storeAddressBookArchiveResource(const KArchiveDirectory *dir, const QString &prefix) -{ - Q_FOREACH (const QString &entryName, dir->entries()) { - 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; - Q_FOREACH (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(); - } - } - } -} - 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 (QFile(kaddressbookrc).exists()) { 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); Q_FOREACH (const QString &entryName, csvTemplateDir->entries()) { 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 (QFile(autocorrectionPath).exists()) { 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/pimsettingexporter/core/addressbook/importaddressbookjob.h b/pimsettingexporter/core/addressbook/importaddressbookjob.h index b2f15f360d..7e408d3de8 100644 --- a/pimsettingexporter/core/addressbook/importaddressbookjob.h +++ b/pimsettingexporter/core/addressbook/importaddressbookjob.h @@ -1,48 +1,46 @@ /* Copyright (c) 2013-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; 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" class ArchiveStorage; class KArchiveFile; class ImportAddressbookJob : public AbstractImportExportJob { Q_OBJECT public: explicit ImportAddressbookJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep); ~ImportAddressbookJob(); void start() Q_DECL_OVERRIDE; protected Q_SLOTS: void slotNextStep() Q_DECL_OVERRIDE; private: bool isAConfigFile(const QString &name) const Q_DECL_OVERRIDE; - void searchAllFiles(const KArchiveDirectory *dir, const QString &prefix); - void storeAddressBookArchiveResource(const KArchiveDirectory *dir, const QString &prefix); 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) Q_DECL_OVERRIDE; }; #endif // IMPORTADDRESSBOOKJOB_H diff --git a/pimsettingexporter/core/alarm/importalarmjob.cpp b/pimsettingexporter/core/alarm/importalarmjob.cpp index dbe151062a..f9e43a190f 100644 --- a/pimsettingexporter/core/alarm/importalarmjob.cpp +++ b/pimsettingexporter/core/alarm/importalarmjob.cpp @@ -1,231 +1,188 @@ /* Copyright (c) 2012-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "importalarmjob.h" #include "archivestorage.h" #include "PimCommon/CreateResource" #include #include #include #include #include #include #include #include #include namespace { inline const QString storeAlarm() { return QStringLiteral("backupalarm/"); } } ImportAlarmJob::ImportAlarmJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep) : AbstractImportExportJob(parent, archiveStorage, typeSelected, numberOfStep) { initializeImportJob(); } ImportAlarmJob::~ImportAlarmJob() { } void ImportAlarmJob::start() { Q_EMIT title(i18n("Start import KAlarm settings...")); createProgressDialog(i18n("Import KAlarm settings")); mArchiveDirectory = archive()->directory(); - searchAllFiles(mArchiveDirectory, QString()); + searchAllFiles(mArchiveDirectory, QString(), QStringLiteral("alarm")); initializeListStep(); QTimer::singleShot(0, this, &ImportAlarmJob::slotNextStep); } void ImportAlarmJob::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 { qCDebug(PIMSETTINGEXPORTERCORE_LOG) << Q_FUNC_INFO << " not supported type " << type; slotNextStep(); } } else { Q_EMIT jobFinished(); } } void ImportAlarmJob::restoreResources() { Q_EMIT info(i18n("Restore resources...")); setProgressDialogLabel(i18n("Restore resources...")); increaseProgressDialog(); QStringList listResource; listResource << restoreResourceFile(QStringLiteral("akonadi_kalarm_resource"), Utils::alarmPath(), storeAlarm(), false); if (!mListResourceFile.isEmpty()) { QDir dir(mTempDirName); dir.mkdir(Utils::alarmPath()); const QString copyToDirName(mTempDirName + QLatin1Char('/') + Utils::alarmPath()); const int numberOfResourceFile = mListResourceFile.size(); for (int i = 0; i < numberOfResourceFile; ++i) { resourceFiles value = mListResourceFile.at(i); QMap settings; if (value.akonadiConfigFile.contains(QStringLiteral("akonadi_kalarm_dir_resource_")) || value.akonadiConfigFile.contains(QStringLiteral("akonadi_kalarm_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. qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " filename :" << filename; KSharedConfig::Ptr resourceConfig = KSharedConfig::openConfig(copyToDirName + QLatin1Char('/') + resourceName); const QString newUrl = Utils::adaptResourcePath(resourceConfig, storeAlarm()); QFileInfo newUrlInfo(newUrl); const QString dataFile = value.akonadiResources; const KArchiveEntry *dataResouceEntry = mArchiveDirectory->entry(dataFile); bool isDirResource = value.akonadiConfigFile.contains(QStringLiteral("akonadi_kalarm_dir_resource_")); if (dataResouceEntry->isFile()) { const KArchiveFile *file = static_cast(dataResouceEntry); //TODO adapt directory name too extractZipFile(file, copyToDirName, newUrlInfo.path(), isDirResource); } 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 archiveNameType = isDirResource ? QStringLiteral("akonadi_kalarm_dir_resource") : QStringLiteral("akonadi_kalarm_resource"); const QString newResource = mCreateResource->createResource(QStringLiteral("akonadi_kalarm_dir_resource"), filename, settings, true); infoAboutNewResource(newResource); listResource << newResource; qCDebug(PIMSETTINGEXPORTERCORE_LOG) << " newResource" << newResource; } } } } //It's maildir support. Need to add support startSynchronizeResources(listResource); } -void ImportAlarmJob::searchAllFiles(const KArchiveDirectory *dir, const QString &prefix) -{ - Q_FOREACH (const QString &entryName, dir->entries()) { - const KArchiveEntry *entry = dir->entry(entryName); - if (entry && entry->isDirectory()) { - const QString newPrefix = (prefix.isEmpty() ? prefix : prefix + QLatin1Char('/')) + entryName; - if (entryName == QLatin1String("alarm")) { - storeAlarmArchiveResource(static_cast(entry), entryName); - } else { - searchAllFiles(static_cast(entry), newPrefix); - } - } - } -} bool ImportAlarmJob::isAConfigFile(const QString &name) const { return name.endsWith(QLatin1String("rc")) && (name.contains(QStringLiteral("akonadi_kalarm_resource_")) || name.contains(QStringLiteral("akonadi_kalarm_dir_resource_"))); } -void ImportAlarmJob::storeAlarmArchiveResource(const KArchiveDirectory *dir, const QString &prefix) -{ - Q_FOREACH (const QString &entryName, dir->entries()) { - 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; - Q_FOREACH (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(); - } - } - } -} - void ImportAlarmJob::restoreConfig() { increaseProgressDialog(); setProgressDialogLabel(i18n("Restore configs...")); const QString kalarmStr(QStringLiteral("kalarmrc")); const KArchiveEntry *kalarmrcentry = mArchiveDirectory->entry(Utils::configsPath() + kalarmStr); if (kalarmrcentry && kalarmrcentry->isFile()) { const KArchiveFile *kalarmrcFile = static_cast(kalarmrcentry); const QString kalarmrc = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + kalarmStr; if (QFile(kalarmrc).exists()) { if (overwriteConfigMessageBox(kalarmStr)) { importkalarmConfig(kalarmrcFile, kalarmrc, kalarmStr, Utils::configsPath()); } } else { importkalarmConfig(kalarmrcFile, kalarmrc, kalarmStr, Utils::configsPath()); } } restoreUiRcFile(QStringLiteral("kalarmui.rc"), QStringLiteral("kalarm")); Q_EMIT info(i18n("Config restored.")); QTimer::singleShot(0, this, &ImportAlarmJob::slotNextStep); } void ImportAlarmJob::importkalarmConfig(const KArchiveFile *kalarmFile, const QString &kalarmrc, const QString &filename, const QString &prefix) { copyToFile(kalarmFile, kalarmrc, filename, prefix); KSharedConfig::Ptr kalarmConfig = KSharedConfig::openConfig(kalarmrc); const QString collectionsStr(QStringLiteral("Collections")); if (kalarmConfig->hasGroup(collectionsStr)) { KConfigGroup group = kalarmConfig->group(collectionsStr); const QString selectionKey(QStringLiteral("FavoriteCollectionIds")); convertRealPathToCollectionList(group, selectionKey, false); } kalarmConfig->sync(); } diff --git a/pimsettingexporter/core/alarm/importalarmjob.h b/pimsettingexporter/core/alarm/importalarmjob.h index 60d68dfca7..f868e06104 100644 --- a/pimsettingexporter/core/alarm/importalarmjob.h +++ b/pimsettingexporter/core/alarm/importalarmjob.h @@ -1,46 +1,44 @@ /* Copyright (c) 2012-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef IMPORTALARMJOB_H #define IMPORTALARMJOB_H #include "abstractimportexportjob.h" class ArchiveStorage; class ImportAlarmJob : public AbstractImportExportJob { Q_OBJECT public: explicit ImportAlarmJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep); ~ImportAlarmJob(); void start() Q_DECL_OVERRIDE; protected Q_SLOTS: void slotNextStep() Q_DECL_OVERRIDE; private: bool isAConfigFile(const QString &name) const Q_DECL_OVERRIDE; - void storeAlarmArchiveResource(const KArchiveDirectory *dir, const QString &prefix); - void searchAllFiles(const KArchiveDirectory *dir, const QString &prefix); void importkalarmConfig(const KArchiveFile *kalarmFile, const QString &kalarmrc, const QString &filename, const QString &prefix); void restoreResources(); void restoreConfig(); }; #endif // IMPORTALARMJOB_H diff --git a/pimsettingexporter/core/calendar/importcalendarjob.cpp b/pimsettingexporter/core/calendar/importcalendarjob.cpp index 4c1ec1dd49..e2545f2e6b 100644 --- a/pimsettingexporter/core/calendar/importcalendarjob.cpp +++ b/pimsettingexporter/core/calendar/importcalendarjob.cpp @@ -1,335 +1,291 @@ /* Copyright (c) 2012-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; 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 "PimCommon/CreateResource" #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("Start 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(QStringLiteral("akonadi_icaldir_resource_")) || value.akonadiConfigFile.contains(QStringLiteral("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(QStringLiteral("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(QStringLiteral("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) { 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)); } } } -void ImportCalendarJob::searchAllFiles(const KArchiveDirectory *dir, const QString &prefix, const QString &searchEntryName) -{ - Q_FOREACH (const QString &entryName, dir->entries()) { - 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); - } - } - } -} - bool ImportCalendarJob::isAConfigFile(const QString &name) const { return name.endsWith(QLatin1String("rc")) && (name.contains(QStringLiteral("akonadi_ical_resource_")) || name.contains(QStringLiteral("akonadi_icaldir_resource_"))); } -void ImportCalendarJob::storeArchiveInfoResources(const KArchiveDirectory *dir, const QString &prefix) -{ - Q_FOREACH (const QString &entryName, dir->entries()) { - 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; - Q_FOREACH (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(); - } - } - } -} - 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 (QFile(oldKorganizerPrintrrc).exists()) { 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 (QFile(korganizerPrinterrc).exists()) { 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 (QFile(korganizerrc).exists()) { 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 (QFile(korgacrc).exists()) { 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 (QFile(freebusypath).exists()) { //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")); 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/pimsettingexporter/core/calendar/importcalendarjob.h b/pimsettingexporter/core/calendar/importcalendarjob.h index c8e30b391e..61f1e44bd6 100644 --- a/pimsettingexporter/core/calendar/importcalendarjob.h +++ b/pimsettingexporter/core/calendar/importcalendarjob.h @@ -1,48 +1,46 @@ /* Copyright (c) 2012-2015 Montel Laurent This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. 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; 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" class ArchiveStorage; class KArchive; class ImportCalendarJob : public AbstractImportExportJob { Q_OBJECT public: explicit ImportCalendarJob(QObject *parent, Utils::StoredTypes typeSelected, ArchiveStorage *archiveStorage, int numberOfStep); ~ImportCalendarJob(); void start() Q_DECL_OVERRIDE; protected Q_SLOTS: void slotNextStep() Q_DECL_OVERRIDE; private: bool isAConfigFile(const QString &name) const Q_DECL_OVERRIDE; - void searchAllFiles(const KArchiveDirectory *dir, const QString &prefix, const QString &searchEntryName); - void storeArchiveInfoResources(const KArchiveDirectory *dir, const QString &prefix); 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) Q_DECL_OVERRIDE; }; #endif // IMPORTCALENDARJOB_H