Commit d642383e authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Allow to reuse code

parent df3ba5ad
......@@ -59,6 +59,7 @@ void ExportNotesJobInterfaceTestImpl::exportResourceToArchive(const QString &arc
qDebug() << " void ExportNotesJobInterfaceTestImpl::exportResourceToArchive(const QString &archivePath, const QString &url, const QString &identifier)" << archivePath << " url " << url << " identifier " << identifier;
QVERIFY(identifier.startsWith(QLatin1String("akonadi_akonotes_resource_")));
QVERIFY(mArchiveStorage->archive()->addLocalFile(url + identifier + QLatin1String(".zip"), archivePath + Utils::resourceNoteArchiveName()));
//TODO export config file too.
slotNoteJobTerminated();
}
......
......@@ -116,7 +116,8 @@ void ExportMailJobInterfaceImpl::exportResourceToArchive(const QString &archiveP
QString ExportMailJobInterfaceImpl::storeResources(KZip *archive, const QString &identifier, const QString &path)
{
return Utils::storeResources(archive, identifier, path);
ResourceConverterImpl converter;
return converter.storeResources(archive, identifier, path);
}
QString ExportMailJobInterfaceImpl::resourcePath(const QString &identifier) const
......
......@@ -19,9 +19,14 @@
#include "resourceconverterbase.h"
#include "utils.h"
#include <PimCommon/PimUtil>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KZip>
#include <QDir>
#include <QFileInfo>
#include <QFileInfo>
#include <QTemporaryFile>
ResourceConverterBase::ResourceConverterBase()
{
......@@ -171,3 +176,47 @@ QString ResourceConverterBase::agentFileName(const QString &filename)
agentFileConfigName = Utils::resourcesPath() + Utils::prefixAkonadiConfigFile() + agentFileConfigName;
return agentFileConfigName;
}
QString ResourceConverterBase::storeResources(KZip *archive, const QString &identifier, const QString &path)
{
const QString agentFileName = identifier + QStringLiteral("rc");
const QString configFileName = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + agentFileName;
qCDebug(PIMDATAEXPORTERCORE_LOG) << "configFileName " << configFileName << "agentFileName " << configFileName;
KSharedConfigPtr resourceConfig = KSharedConfig::openConfig(configFileName);
QTemporaryFile tmp;
tmp.open();
KConfig *config = resourceConfig->copyTo(tmp.fileName());
if (identifier.contains(POP3_RESOURCE_IDENTIFIER)) {
const QString targetCollection = QStringLiteral("targetCollection");
KConfigGroup group = config->group("General");
if (group.hasKey(targetCollection)) {
group.writeEntry(targetCollection, convertToFullCollectionPath(group.readEntry(targetCollection).toLongLong()));
}
} else if (PimCommon::Util::isImapResource(identifier)) {
const QString trash = QStringLiteral("TrashCollection");
KConfigGroup group = config->group("cache");
if (group.hasKey(trash)) {
group.writeEntry(trash, convertToFullCollectionPath(group.readEntry(trash).toLongLong()));
}
}
//Customize resource if necessary here.
config->sync();
bool fileAdded = archive->addLocalFile(tmp.fileName(), path + agentFileName);
delete config;
if (!fileAdded) {
return i18n("Resource file \"%1\" cannot be added to backup file.", agentFileName);
}
const QString agentConfigFileName = Utils::prefixAkonadiConfigFile() + identifier;
const QString agentConfigFileNamePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1String("/akonadi/") + agentConfigFileName;
if (QFileInfo::exists(agentConfigFileNamePath)) {
fileAdded = archive->addLocalFile(agentConfigFileNamePath, path + agentConfigFileName);
if (!fileAdded) {
return i18n("Resource file \"%1\" cannot be added to backup file.", agentFileName);
}
}
return QString();
}
......@@ -24,6 +24,7 @@
#include <QString>
#include <KSharedConfig>
#include <AkonadiCore/Collection>
class KZip;
class PIMDATAEXPORTER_EXPORT ResourceConverterBase
{
public:
......@@ -40,6 +41,7 @@ public:
virtual Q_REQUIRED_RESULT QString convertToFullCollectionPath(const qlonglong collectionValue) = 0;
virtual Q_REQUIRED_RESULT Akonadi::Collection::Id convertFolderPathToCollectionId(const QString &path) = 0;
Q_REQUIRED_RESULT QString storeResources(KZip *archive, const QString &identifier, const QString &path);
};
#endif // RESOURCECONVERTER_H
......@@ -20,6 +20,7 @@
#include "storeresourcejob.h"
#include "utils.h"
#include "pimdataexportcore_debug.h"
#include "resourceconverterimpl.h"
#include <KLocalizedString>
#include <KZip>
#include <AkonadiCore/ServerManager>
......@@ -59,7 +60,9 @@ void StoreResourceJob::start()
deleteLater();
return;
}
const QString errorStr = Utils::storeResources(mZip, mIdentifier, mArchivePath);
ResourceConverterImpl converter;
const QString errorStr = converter.storeResources(mZip, mIdentifier, mArchivePath);
if (!errorStr.isEmpty()) {
Q_EMIT error(errorStr);
}
......
......@@ -110,50 +110,6 @@ QString Utils::infoPath()
return QStringLiteral("information/");
}
QString Utils::storeResources(KZip *archive, const QString &identifier, const QString &path)
{
const QString agentFileName = identifier + QStringLiteral("rc");
const QString configFileName = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + agentFileName;
qCDebug(PIMDATAEXPORTERCORE_LOG) << "configFileName " << configFileName << "agentFileName " << configFileName;
KSharedConfigPtr resourceConfig = KSharedConfig::openConfig(configFileName);
QTemporaryFile tmp;
tmp.open();
KConfig *config = resourceConfig->copyTo(tmp.fileName());
if (identifier.contains(POP3_RESOURCE_IDENTIFIER)) {
const QString targetCollection = QStringLiteral("targetCollection");
KConfigGroup group = config->group("General");
if (group.hasKey(targetCollection)) {
group.writeEntry(targetCollection, MailCommon::Util::fullCollectionPath(Akonadi::Collection(group.readEntry(targetCollection).toLongLong())));
}
} else if (PimCommon::Util::isImapResource(identifier)) {
const QString trash = QStringLiteral("TrashCollection");
KConfigGroup group = config->group("cache");
if (group.hasKey(trash)) {
group.writeEntry(trash, MailCommon::Util::fullCollectionPath(Akonadi::Collection(group.readEntry(trash).toLongLong())));
}
}
//Customize resource if necessary here.
config->sync();
bool fileAdded = archive->addLocalFile(tmp.fileName(), path + agentFileName);
delete config;
if (!fileAdded) {
return i18n("Resource file \"%1\" cannot be added to backup file.", agentFileName);
}
const QString agentConfigFileName = Utils::prefixAkonadiConfigFile() + identifier;
const QString agentConfigFileNamePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1String("/akonadi/") + agentConfigFileName;
if (QFileInfo::exists(agentConfigFileNamePath)) {
fileAdded = archive->addLocalFile(agentConfigFileNamePath, path + agentConfigFileName);
if (!fileAdded) {
return i18n("Resource file \"%1\" cannot be added to backup file.", agentFileName);
}
}
return QString();
}
QString Utils::akonadiAgentName(const QString &configPath)
{
QSettings settings(configPath, QSettings::IniFormat);
......
......@@ -103,7 +103,6 @@ PIMDATAEXPORTER_EXPORT Q_REQUIRED_RESULT QString exportDataTypeFileName();
Q_REQUIRED_RESULT QString akonadiAgentName(const QString &configPath);
PIMDATAEXPORTER_EXPORT Q_REQUIRED_RESULT QVector<Utils::AkonadiInstanceInfo> listOfResource();
Q_REQUIRED_RESULT QString storeResources(KZip *archive, const QString &identifier, const QString &path);
KZip *openZip(const QString &filename, QString &errorMsg);
PIMDATAEXPORTER_EXPORT void storeDataExportInfo(KZip *archive);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment