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

Implement it

parent a356477c
......@@ -18,8 +18,147 @@
*/
#include "resourceconverter.h"
#include "utils.h"
#include <KConfigGroup>
#include <QDir>
#include <QFileInfo>
#include <AkonadiCore/AgentInstance>
#include <MailCommon/MailUtil>
ResourceConverter::ResourceConverter()
{
}
QString ResourceConverter::adaptResourcePath(const KSharedConfigPtr &resourceConfig, const QString &storedData)
{
QString newUrl = Utils::resourcePath(resourceConfig);
if (!newUrl.contains(QDir::homePath())) {
QFileInfo fileInfo(newUrl);
fileInfo.fileName();
//qCDebug(PIMDATAEXPORTERCORE_LOG)<<" url "<<url.path();
QString currentPath = QDir::homePath() + QLatin1Char('/') + storedData;
newUrl = (currentPath + QLatin1Char('/') + fileInfo.fileName());
if (!QDir(currentPath).exists()) {
QDir().mkdir(currentPath);
}
}
if (QFileInfo::exists(newUrl)) {
QString newFileName = newUrl;
QFileInfo fileInfo(newFileName);
for (int i = 0;; ++i) {
const QString currentPath = fileInfo.path() + QLatin1Char('/') + QString::number(i) + QLatin1Char('/');
newFileName = currentPath + fileInfo.fileName();
if (!QFileInfo::exists(newFileName)) {
QDir().mkdir(currentPath);
break;
}
}
newUrl = newFileName;
}
return newUrl;
}
QString ResourceConverter::resourcePath(const KSharedConfigPtr &resourceConfig, const QString &defaultPath)
{
KConfigGroup group = resourceConfig->group(QStringLiteral("General"));
QString url = group.readEntry(QStringLiteral("Path"), defaultPath);
if (!url.isEmpty()) {
url.replace(QLatin1String("$HOME"), QDir::homePath());
}
return url;
}
//Merge two methods I think
void ResourceConverter::convertCollectionIdsToRealPath(KConfigGroup &group, const QString &currentKey, const QString &prefixCollection)
{
if (group.hasKey(currentKey)) {
const QStringList value = group.readEntry(currentKey, QStringList());
QStringList newValue;
for (QString str : value) {
bool found = false;
if (!prefixCollection.isEmpty() && str.startsWith(prefixCollection)) {
str.remove(0, prefixCollection.length());
}
const qlonglong collectionId = str.toLongLong(&found);
if (found) {
const QString realPath = MailCommon::Util::fullCollectionPath(Akonadi::Collection(collectionId));
if (!realPath.isEmpty()) {
newValue << realPath;
}
}
}
if (newValue.isEmpty()) {
group.deleteEntry(currentKey);
} else {
group.writeEntry(currentKey, newValue);
}
}
}
void ResourceConverter::convertCollectionListToRealPath(KConfigGroup &group, const QString &currentKey)
{
if (group.hasKey(currentKey)) {
const QStringList listExpension = group.readEntry(currentKey, QStringList());
if (listExpension.isEmpty()) {
group.deleteEntry(currentKey);
} else {
QStringList result;
for (QString collection : listExpension) {
collection.remove(QLatin1Char('c'));
bool found = false;
const qlonglong collectionValue = collection.toLongLong(&found);
if (found && collectionValue != -1) {
const QString realPath = MailCommon::Util::fullCollectionPath(Akonadi::Collection(collectionValue));
if (!realPath.isEmpty()) {
result << realPath;
}
}
}
if (result.isEmpty()) {
group.deleteEntry(currentKey);
} else {
group.writeEntry(currentKey, result);
}
}
}
}
void ResourceConverter::convertCollectionToRealPath(KConfigGroup &group, const QString &currentKey)
{
if (group.hasKey(currentKey)) {
QString collectionId = group.readEntry(currentKey);
if (collectionId.isEmpty()) {
group.deleteEntry(currentKey);
} else {
collectionId.remove(QLatin1Char('c'));
bool found = false;
const qlonglong collectionValue = collectionId.toLongLong(&found);
if (found && collectionValue != -1) {
const QString realPath = MailCommon::Util::fullCollectionPath(Akonadi::Collection(collectionValue));
group.writeEntry(currentKey, realPath);
} else {
group.deleteEntry(currentKey);
}
}
}
}
QString ResourceConverter::resourcePath(const Akonadi::AgentInstance &agent, const QString &defaultPath)
{
const QString agentFileName = agent.identifier() + QStringLiteral("rc");
const QString configFileName = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QLatin1Char('/') + agentFileName;
KSharedConfigPtr resourceConfig = KSharedConfig::openConfig(configFileName);
const QString url = ResourceConverter::resourcePath(resourceConfig, defaultPath);
return url;
}
QString ResourceConverter::agentFileName(const QString &filename)
{
QString agentFileConfigName = filename;
agentFileConfigName.remove(Utils::resourcesPath());
agentFileConfigName.remove(agentFileConfigName.length() - 2, 2); //Remove "rc"
agentFileConfigName = Utils::resourcesPath() + Utils::prefixAkonadiConfigFile() + agentFileConfigName;
return agentFileConfigName;
}
......@@ -21,10 +21,22 @@
#define RESOURCECONVERTER_H
#include "pimdataexporter_export.h"
#include <QString>
#include <KSharedConfig>
namespace Akonadi {
class AgentInstance;
}
class PIMDATAEXPORTER_EXPORT ResourceConverter
{
public:
ResourceConverter();
QString agentFileName(const QString &filename);
QString resourcePath(const Akonadi::AgentInstance &agent, const QString &defaultPath = QString());
void convertCollectionToRealPath(KConfigGroup &group, const QString &currentKey);
void convertCollectionListToRealPath(KConfigGroup &group, const QString &currentKey);
void convertCollectionIdsToRealPath(KConfigGroup &group, const QString &currentKey, const QString &prefixCollection = QString());
QString resourcePath(const KSharedConfigPtr &resourceConfig, const QString &defaultPath);
QString adaptResourcePath(const KSharedConfigPtr &resourceConfig, const QString &storedData);
};
#endif // RESOURCECONVERTER_H
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