Commit 6b47ccde authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Move caching functions to etebaseadapter.cpp

Caching functions are required in the main resource file as well as fetch jobs
parent f8d6bb55
......@@ -95,7 +95,7 @@ void EntriesFetchJob::fetchEntries()
Item item;
for (int i = 0; i < dataLength; i++) {
saveItemCache(itemManager.get(), etesyncItems[i], item);
saveEtebaseItemCache(itemManager.get(), etesyncItems[i], mCacheDir);
setupItem(item, etesyncItems[i], type);
}
}
......@@ -144,26 +144,3 @@ void EntriesFetchJob::setupItem(Akonadi::Item &item, const EtebaseItem *etesyncI
mItems.push_back(item);
}
void EntriesFetchJob::saveItemCache(const EtebaseItemManager *itemManager, const EtebaseItem *etesyncItem, Item &item)
{
if (!etesyncItem) {
qCDebug(ETESYNC_LOG) << "Unable to save item cache - etesyncItem is null";
return;
}
QString etesyncItemUid = QString::fromUtf8(etebase_item_get_uid(etesyncItem));
qCDebug(ETESYNC_LOG) << "Saving cache for item" << etesyncItemUid;
uintptr_t ret_size;
EtebaseCachePtr cache(etebase_item_manager_cache_save(itemManager, etesyncItem, &ret_size));
QByteArray cacheData((char *)cache.get(), ret_size);
const QString path = mCacheDir + QLatin1Char('/') + etesyncItemUid;
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << path << file.errorString();
return;
}
file.write(cacheData);
}
......@@ -42,7 +42,6 @@ public:
protected:
void fetchEntries();
void setupItem(Akonadi::Item &item, const EtebaseItem *etesyncItem, const QString &type);
void saveItemCache(const EtebaseItemManager *itemManager, const EtebaseItem *etesyncItem, Akonadi::Item &item);
private:
const EtebaseAccount *mAccount = nullptr;
......
......@@ -4,6 +4,8 @@
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QFile>
#include "etebaseadapter.h"
#include "etesync_debug.h"
......@@ -17,6 +19,134 @@ QString QStringFromCharPtr(const CharPtr &str)
return ret;
}
void saveEtebaseCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etesyncCollection, const QString &cacheDir)
{
if (!etesyncCollection) {
qCDebug(ETESYNC_LOG) << "Unable to save collection cache - etesyncCollection is null";
return;
}
QString etesyncCollectionUid = QString::fromUtf8(etebase_collection_get_uid(etesyncCollection));
qCDebug(ETESYNC_LOG) << "Saving cache for collection" << etesyncCollectionUid;
uintptr_t ret_size;
EtebaseCachePtr cache(etebase_collection_manager_cache_save(collectionManager, etesyncCollection, &ret_size));
QByteArray cacheData((char *)cache.get(), ret_size);
QString filePath = cacheDir + QStringLiteral("/") + etesyncCollectionUid;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << filePath << file.errorString();
return;
}
file.write(cacheData);
}
void saveEtebaseItemCache(const EtebaseItemManager *itemManager, const EtebaseItem *etesyncItem, const QString &cacheDir)
{
if (!etesyncItem) {
qCDebug(ETESYNC_LOG) << "Unable to save item cache - etesyncItem is null";
return;
}
QString etesyncItemUid = QString::fromUtf8(etebase_item_get_uid(etesyncItem));
qCDebug(ETESYNC_LOG) << "Saving cache for item" << etesyncItemUid;
uintptr_t ret_size;
EtebaseCachePtr cache(etebase_item_manager_cache_save(itemManager, etesyncItem, &ret_size));
QByteArray cacheData((char *)cache.get(), ret_size);
const QString filePath = cacheDir + QStringLiteral("/") + etesyncItemUid;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << filePath << file.errorString();
return;
}
file.write(cacheData);
}
EtebaseCollectionPtr getEtebaseCollectionFromCache(const EtebaseCollectionManager *collectionManager, const QString &collectionUid, const QString &cachePath)
{
if (collectionUid.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Unable to get collection cache - uid is empty";
return nullptr;
}
qCDebug(ETESYNC_LOG) << "Getting cache for collection" << collectionUid;
QString collectionCachePath = cachePath + QLatin1Char('/') + collectionUid;
QFile collectionCacheFile(collectionCachePath);
if (!collectionCacheFile.exists()) {
qCDebug(ETESYNC_LOG) << "No cache file for collection" << collectionUid;
return nullptr;
}
if (!collectionCacheFile.open(QIODevice::ReadOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << collectionCachePath << collectionCacheFile.errorString();
return nullptr;
}
QByteArray collectionCache = collectionCacheFile.readAll();
if (collectionCache.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Empty cache file for collection" << collectionUid;
return nullptr;
}
EtebaseCollectionPtr etesyncCollection(etebase_collection_manager_cache_load(collectionManager, collectionCache.constData(), collectionCache.size()));
return etesyncCollection;
}
EtebaseItemPtr getEtebaseItemFromCache(const EtebaseItemManager *itemManager, const QString &itemUid, const QString &cacheDir)
{
if (itemUid.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Unable to get item cache - uid is empty";
return nullptr;
}
qCDebug(ETESYNC_LOG) << "Getting cache for item" << itemUid;
QString itemCachePath = cacheDir + QLatin1Char('/') + itemUid;
QFile itemCacheFile(itemCachePath);
if (!itemCacheFile.exists()) {
qCDebug(ETESYNC_LOG) << "No cache file for item" << itemUid;
return nullptr;
}
if (!itemCacheFile.open(QIODevice::ReadOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << itemCachePath << itemCacheFile.errorString();
return nullptr;
}
QByteArray itemCache = itemCacheFile.readAll();
if (itemCache.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Empty cache file for item" << itemUid;
return nullptr;
}
EtebaseItemPtr etesyncItem(etebase_item_manager_cache_load(itemManager, itemCache.constData(), itemCache.size()));
return etesyncItem;
}
void deleteCacheFile(const QString &etebaseUid, const QString &cacheDir)
{
qCDebug(ETESYNC_LOG) << "Deleting cache file" << etebaseUid;
const QString path = cacheDir + QLatin1Char('/') + etebaseUid;
QFile file(path);
if (!file.remove()) {
qCDebug(ETESYNC_LOG) << "Unable to remove " << path << file.errorString();
return;
}
}
EtebaseClientPtr etebase_client_new(const QString &client_name, const QString &server_url)
{
return EtebaseClientPtr(etebase_client_new(charArrFromQString(client_name), charArrFromQString(server_url)));
......
......@@ -8,7 +8,6 @@
#define ETEBASEADAPTER_H
#include <etebase.h>
#include <QString>
#include <memory>
#include <vector>
......@@ -99,6 +98,11 @@ using EtebaseCachePtr = std::unique_ptr<void, EtebaseDeleter>;
using CharPtr = std::unique_ptr<char, EtebaseDeleter>;
QString QStringFromCharPtr(const CharPtr &str);
void saveEtebaseCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etesyncCollection, const QString &cacheDir);
void saveEtebaseItemCache(const EtebaseItemManager *itemManager, const EtebaseItem *etesyncItem, const QString &cacheDir);
EtebaseCollectionPtr getEtebaseCollectionFromCache(const EtebaseCollectionManager *collectionManager, const QString &collectionUid, const QString &cacheDir);
EtebaseItemPtr getEtebaseItemFromCache(const EtebaseItemManager *itemManager, const QString &itemUid, const QString &cacheDir);
void deleteCacheFile(const QString &etebaseUid, const QString &cacheDir);
EtebaseClientPtr etebase_client_new(const QString &client_name, const QString &server_url);
......
......@@ -83,7 +83,7 @@ void JournalsFetchJob::fetchJournals()
Collection collection;
for (int i = 0; i < dataLength; i++) {
saveCollectionCache(collectionManager.get(), etesyncCollections[i], collection);
saveEtebaseCollectionCache(collectionManager.get(), etesyncCollections[i], mCacheDir);
setupCollection(collection, etesyncCollections[i]);
}
......@@ -169,26 +169,3 @@ void JournalsFetchJob::setupCollection(Akonadi::Collection &collection, const Et
mCollections.push_back(collection);
}
void JournalsFetchJob::saveCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etesyncCollection, Collection &collection)
{
if (!etesyncCollection) {
qCDebug(ETESYNC_LOG) << "Unable to save collection cache - etesyncCollection is null";
return;
}
QString etesyncCollectionUid = QString::fromUtf8(etebase_collection_get_uid(etesyncCollection));
qCDebug(ETESYNC_LOG) << "Saving cache for collection" << etesyncCollectionUid;
uintptr_t ret_size;
EtebaseCachePtr cache(etebase_collection_manager_cache_save(collectionManager, etesyncCollection, &ret_size));
QByteArray cacheData((char *)cache.get(), ret_size);
const QString path = mCacheDir + QLatin1Char('/') + etesyncCollectionUid;
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << path << file.errorString();
return;
}
file.write(cacheData);
}
......@@ -42,7 +42,7 @@ public:
protected:
void fetchJournals();
void setupCollection(Akonadi::Collection &collection, const EtebaseCollection *etesyncCollection);
void saveCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etesyncCollection, Akonadi::Collection &collection);
private:
const EtebaseAccount *mAccount = nullptr;
Akonadi::Collection::List mCollections;
......
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