Commit 957bb92a authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Implement etebase account caching

Resource cache dir: ~/.local/share/akonadi_etesync_resource/
Account cache dir: resourceCacheDir/username/
Contents:
username <- stores account
CollectionCache/
ItemCache/
parent 54ab67f1
......@@ -19,6 +19,25 @@ QString QStringFromCharPtr(const CharPtr &str)
return ret;
}
void saveEtebaseAccountCache(EtebaseAccount *account, const QString &username, const QString &cacheDir)
{
if (!account) {
qCDebug(ETESYNC_LOG) << "Unable to save etebase account cache - account is null";
return;
}
qCDebug(ETESYNC_LOG) << "Saving cache for account" << username;
CharPtr cacheData(etebase_account_save(account, nullptr, 0));
QString filePath = cacheDir + QStringLiteral("/") + username;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << filePath << file.errorString();
return;
}
file.write(QByteArray(cacheData.get()));
}
void saveEtebaseCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etesyncCollection, const QString &cacheDir)
{
if (!etesyncCollection) {
......@@ -66,7 +85,37 @@ void saveEtebaseItemCache(const EtebaseItemManager *itemManager, const EtebaseIt
file.write(cacheData);
}
EtebaseCollectionPtr getEtebaseCollectionFromCache(const EtebaseCollectionManager *collectionManager, const QString &collectionUid, const QString &cachePath)
EtebaseAccountPtr getEtebaseAccountFromCache(const EtebaseClient *client, const QString &username, const QString &cacheDir)
{
if (!client) {
qCDebug(ETESYNC_LOG) << "Unable to get account from cache - client is null";
return nullptr;
}
QString accountCachePath = cacheDir + QStringLiteral("/") + username;
qCDebug(ETESYNC_LOG) << "accountCachePath" << accountCachePath;
QFile accountCacheFile(accountCachePath);
if (!accountCacheFile.exists()) {
qCDebug(ETESYNC_LOG) << "No cache file for account" << username;
return nullptr;
}
if (!accountCacheFile.open(QIODevice::ReadOnly)) {
qCDebug(ETESYNC_LOG) << "Unable to open " << accountCachePath << accountCacheFile.errorString();
return nullptr;
}
QByteArray accountCache = accountCacheFile.readAll();
EtebaseAccountPtr etebaseAccount(etebase_account_restore(client, accountCache.constData(), nullptr, 0));
return etebaseAccount;
}
EtebaseCollectionPtr getEtebaseCollectionFromCache(const EtebaseCollectionManager *collectionManager, const QString &collectionUid, const QString &cacheDir)
{
if (collectionUid.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Unable to get collection cache - uid is empty";
......@@ -75,7 +124,7 @@ EtebaseCollectionPtr getEtebaseCollectionFromCache(const EtebaseCollectionManage
qCDebug(ETESYNC_LOG) << "Getting cache for collection" << collectionUid;
QString collectionCachePath = cachePath + QLatin1Char('/') + collectionUid;
QString collectionCachePath = cacheDir + QLatin1Char('/') + collectionUid;
QFile collectionCacheFile(collectionCachePath);
......
......@@ -98,8 +98,10 @@ using EtebaseCachePtr = std::unique_ptr<void, EtebaseDeleter>;
using CharPtr = std::unique_ptr<char, EtebaseDeleter>;
QString QStringFromCharPtr(const CharPtr &str);
void saveEtebaseAccountCache(EtebaseAccount *account, const QString &username, const QString &cacheDir);
void saveEtebaseCollectionCache(const EtebaseCollectionManager *collectionManager, const EtebaseCollection *etesyncCollection, const QString &cacheDir);
void saveEtebaseItemCache(const EtebaseItemManager *itemManager, const EtebaseItem *etesyncItem, const QString &cacheDir);
EtebaseAccountPtr getEtebaseAccountFromCache(const EtebaseClient *client, const QString &username, 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);
......
......@@ -16,38 +16,23 @@ void EteSyncClientState::init()
Settings::self()->load();
mServerUrl = Settings::self()->baseUrl();
mUsername = Settings::self()->username();
mPassword = Settings::self()->password();
mEncryptionPassword = Settings::self()->encryptionPassword();
if (mServerUrl.isEmpty() || mUsername.isEmpty() || mPassword.isEmpty() || mEncryptionPassword.isEmpty()) {
if (mServerUrl.isEmpty() || mUsername.isEmpty()) {
Q_EMIT clientInitialised(false);
return;
}
// Initialise EteSync client state
mClient = etesync_new(QStringLiteral("Akonadi EteSync Resource"), mServerUrl);
mToken = etesync_auth_get_token(mClient.get(), mUsername, mPassword);
if (mToken.isEmpty()) {
qCWarning(ETESYNC_LOG) << "Unable to obtain token from server" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
Q_EMIT clientInitialised(false);
return;
}
etesync_set_auth_token(mClient.get(), mToken);
mJournalManager = EteSyncJournalManagerPtr(etesync_journal_manager_new(mClient.get()));
mDerived = etesync_crypto_derive_key(mClient.get(), mUsername, mEncryptionPassword);
// Get user keypair
EteSyncUserInfoManagerPtr userInfoManager(etesync_user_info_manager_new(mClient.get()));
mUserInfo = etesync_user_info_manager_fetch(userInfoManager.get(), mUsername);
if (!mUserInfo) {
qCWarning(ETESYNC_LOG) << "init() - User info obtained from server is NULL";
invalidateToken();
// Initialize client object
mClientXXX = etebase_client_new(QStringLiteral("Akonadi EteSync Resource"), mServerUrl);
if (!mClientXXX) {
qCDebug(ETESYNC_LOG) << "Could not initialise Etebase client";
qCDebug(ETESYNC_LOG) << "Etebase error" << etebase_error_get_message();
Q_EMIT clientInitialised(false);
return;
}
EteSyncCryptoManagerPtr userInfoCryptoManager = etesync_user_info_get_crypto_manager(mUserInfo.get(), mDerived);
mKeypair = EteSyncAsymmetricKeyPairPtr(etesync_user_info_get_keypair(mUserInfo.get(), userInfoCryptoManager.get()));
// Load Etebase account from cache
mAccountXXX = getEtebaseAccountFromCache(mClientXXX.get(), mUsername, Settings::self()->cacheDir());
Q_EMIT clientInitialised(true);
}
......@@ -80,7 +65,6 @@ bool EteSyncClientState::login(const QString &serverUrl, const QString &username
{
mServerUrl = serverUrl;
mUsername = username;
mPassword = password;
mClientXXX = etebase_client_new(QStringLiteral("Akonadi EteSync Resource"), mServerUrl);
if (!mClientXXX) {
......@@ -88,7 +72,7 @@ bool EteSyncClientState::login(const QString &serverUrl, const QString &username
qCDebug(ETESYNC_LOG) << "Etebase error" << etebase_error_get_message();
return false;
}
mAccountXXX = etebase_account_login(mClientXXX.get(), mUsername, mPassword);
mAccountXXX = etebase_account_login(mClientXXX.get(), mUsername, password);
if (!mAccountXXX) {
qCDebug(ETESYNC_LOG) << "Could not fetch Etebase account";
qCDebug(ETESYNC_LOG) << "Etebase error" << etebase_error_get_message();
......@@ -239,7 +223,9 @@ void EteSyncClientState::saveSettings()
{
Settings::self()->setBaseUrl(mServerUrl);
Settings::self()->setUsername(mUsername);
Settings::self()->setPassword(mPassword);
Settings::self()->setEncryptionPassword(mEncryptionPassword);
const QString cacheDir = Settings::self()->basePath() + QStringLiteral("/") + mUsername;
Settings::self()->setCacheDir(cacheDir);
Settings::self()->save();
}
......@@ -55,10 +55,8 @@ EteSyncResource::EteSyncResource(const QString &id)
changeRecorder()->fetchCollection(true);
changeRecorder()->collectionFetchScope().setAncestorRetrieval(CollectionFetchScope::All);
// Make resource directories
// Make resource directory
initialiseDirectory(baseDirectoryPath());
initialiseDirectory(collectionsCacheDirectoryPath());
initialiseDirectory(itemsCacheDirectoryPath());
mClientState = EteSyncClientState::Ptr(new EteSyncClientState());
connect(mClientState.get(), &EteSyncClientState::clientInitialised, this, &EteSyncResource::initialiseDone);
......@@ -97,6 +95,15 @@ void EteSyncResource::configure(WId windowId)
const int result = wizard.exec();
if (result == QDialog::Accepted) {
mClientState->saveSettings();
// Init cache directories
initialiseDirectory(cacheDirectoryPath());
initialiseDirectory(collectionsCacheDirectoryPath());
initialiseDirectory(itemsCacheDirectoryPath());
// Save account cache
saveEtebaseAccountCache(mClientState->account(), mClientState->username(), cacheDirectoryPath());
mCredentialsRequired = false;
qCDebug(ETESYNC_LOG) << "Setting online";
setOnline(true);
......@@ -411,14 +418,19 @@ QString EteSyncResource::baseDirectoryPath() const
return Settings::self()->basePath();
}
QString EteSyncResource::cacheDirectoryPath() const
{
return Settings::self()->cacheDir();
}
QString EteSyncResource::collectionsCacheDirectoryPath() const
{
return baseDirectoryPath() + QStringLiteral("/CollectionCache");
return cacheDirectoryPath() + QStringLiteral("/CollectionCache");
}
QString EteSyncResource::itemsCacheDirectoryPath() const
{
return baseDirectoryPath() + QStringLiteral("/ItemCache");
return cacheDirectoryPath() + QStringLiteral("/ItemCache");
}
void EteSyncResource::initialiseDirectory(const QString &path) const
......
......@@ -54,6 +54,7 @@ protected:
void initialiseDirectory(const QString &path) const;
QString baseDirectoryPath() const;
QString cacheDirectoryPath() const;
QString collectionsCacheDirectoryPath() const;
QString itemsCacheDirectoryPath() const;
......
......@@ -30,5 +30,8 @@
<label>Path to base directory</label>
<default>$HOME/.local/share/akonadi_etesync_resource</default>
</entry>
<entry name="CacheDir" type="Path">
<label>Path to cache directory specific to account</label>
</entry>
</group>
</kcfg>
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