Commit 70f726f0 authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Add conflict error handling and token refresh

parent f8de2ffb
......@@ -160,12 +160,18 @@ void CalendarTaskBaseHandler::deleteLocalCalendar(const KCalendarCore::Incidence
void CalendarTaskBaseHandler::itemAdded(const Akonadi::Item &item,
const Akonadi::Collection &collection)
{
qCDebug(ETESYNC_LOG) << "Mime: " << collection.contentMimeTypes();
KCalendarCore::Calendar::Ptr calendar(new MemoryCalendar(QTimeZone::utc()));
calendar->addIncidence(item.payload<Incidence::Ptr>());
KCalendarCore::ICalFormat format;
// qCDebug(ETESYNC_LOG) << "Calendar item added " << format.toString(calendar);
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString lastJournalUid = QStringFromCharPtr(CharPtr(etesync_journal_get_last_uid(journal.get())));
if (lastJournalUid != collection.remoteRevision()) {
mResource->deferTask();
mResource->retrieveItems(collection);
}
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
EteSyncSyncEntryPtr syncEntry(etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_ADD), format.toString(calendar)));
......@@ -199,6 +205,11 @@ void CalendarTaskBaseHandler::itemChanged(const Akonadi::Item &item,
// qCDebug(ETESYNC_LOG) << "Calendar item changed " << format.toString(calendar);
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString lastJournalUid = QStringFromCharPtr(CharPtr(etesync_journal_get_last_uid(journal.get())));
if (lastJournalUid != collection.remoteRevision()) {
mResource->deferTask();
mResource->retrieveItems(collection);
}
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
EteSyncSyncEntryPtr syncEntry(etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_CHANGE), format.toString(calendar)));
......@@ -226,6 +237,11 @@ void CalendarTaskBaseHandler::itemRemoved(const Akonadi::Item &item)
Collection collection = item.parentCollection();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString lastJournalUid = QStringFromCharPtr(CharPtr(etesync_journal_get_last_uid(journal.get())));
if (lastJournalUid != collection.remoteRevision()) {
mResource->deferTask();
mResource->retrieveItems(collection);
}
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
QString calendar = getLocalCalendar(item.remoteId());
......
......@@ -162,6 +162,11 @@ void ContactHandler::itemAdded(const Akonadi::Item &item,
const Akonadi::Collection &collection)
{
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString lastJournalUid = QStringFromCharPtr(CharPtr(etesync_journal_get_last_uid(journal.get())));
if (lastJournalUid != collection.remoteRevision()) {
mResource->deferTask();
mResource->retrieveItems(collection);
}
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
KContacts::VCardConverter converter;
......@@ -193,6 +198,11 @@ void ContactHandler::itemChanged(const Akonadi::Item &item,
Collection collection = item.parentCollection();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString lastJournalUid = QStringFromCharPtr(CharPtr(etesync_journal_get_last_uid(journal.get())));
if (lastJournalUid != collection.remoteRevision()) {
mResource->deferTask();
mResource->retrieveItems(collection);
}
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
KContacts::VCardConverter converter;
......@@ -222,6 +232,11 @@ void ContactHandler::itemRemoved(const Akonadi::Item &item)
Collection collection = item.parentCollection();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString lastJournalUid = QStringFromCharPtr(CharPtr(etesync_journal_get_last_uid(journal.get())));
if (lastJournalUid != collection.remoteRevision()) {
mResource->deferTask();
mResource->retrieveItems(collection);
}
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
QString contact = getLocalContact(item.remoteId());
......
......@@ -25,8 +25,8 @@
using namespace EteSyncAPI;
EntriesFetchJob::EntriesFetchJob(const EteSync *client, const QString &journalUid, const QString &prevUid, QObject *parent)
: KJob(parent), mClient(client), mJournalUid(journalUid), mPrevUid(prevUid)
EntriesFetchJob::EntriesFetchJob(const EteSync *client, const Akonadi::Collection &collection, QObject *parent)
: KJob(parent), mClient(client), mCollection(collection)
{
}
......@@ -37,8 +37,10 @@ void EntriesFetchJob::start()
void EntriesFetchJob::fetchEntries()
{
EteSyncEntryManagerPtr entryManager(etesync_entry_manager_new(mClient, mJournalUid));
mEntries = etesync_entry_manager_list(entryManager.get(), mPrevUid, 0);
QString journalUid = mCollection.remoteId();
QString prevUid = mCollection.remoteRevision();
EteSyncEntryManagerPtr entryManager(etesync_entry_manager_new(mClient, journalUid));
mEntries = etesync_entry_manager_list(entryManager.get(), prevUid, 0);
if (!mEntries) {
setError(UserDefinedError);
......
......@@ -18,6 +18,7 @@
#ifndef ETESYNCENTRIESFETCHJOB_H
#define ETESYNCENTRIESFETCHJOB_H
#include <AkonadiCore/Collection>
#include <KJob>
#include "etesyncadapter.h"
......@@ -28,7 +29,7 @@ namespace EteSyncAPI {
Q_OBJECT
public:
EntriesFetchJob(const EteSync *client, const QString &journalUid, const QString &prevUid, QObject *parent = nullptr);
EntriesFetchJob(const EteSync *client, const Akonadi::Collection &collection, QObject *parent = nullptr);
void start() override;
......@@ -37,14 +38,18 @@ namespace EteSyncAPI {
return mEntries;
}
Akonadi::Collection collection()
{
return mCollection;
}
protected:
void fetchEntries();
private:
const EteSync *mClient = nullptr;
const QString mJournalUid;
const QString mPrevUid;
EteSyncEntry **mEntries = nullptr;
Akonadi::Collection mCollection;
};
} // namespace EteSyncAPI
......
......@@ -92,6 +92,17 @@ bool EteSyncClientState::initToken(QString &serverUrl, QString &username, QStrin
return true;
}
void EteSyncClientState::refreshToken()
{
mToken = etesync_auth_get_token(mClient.get(), mUsername, mPassword);
if (mToken.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Empty token";
return;
}
qCDebug(ETESYNC_LOG) << "Received token" << mToken;
etesync_set_auth_token(mClient.get(), mToken);
}
bool EteSyncClientState::initUserInfo()
{
mJournalManager = EteSyncJournalManagerPtr(etesync_journal_manager_new(mClient.get()));
......
......@@ -34,6 +34,7 @@ public:
bool initKeypair(const QString &encryptionPassword);
void initAccount(const QString &encryptionPassword);
void saveSettings();
void refreshToken();
EteSync *client()
{
......
......@@ -146,6 +146,7 @@ void EteSyncResource::retrieveCollections()
collectionsRetrieved(list);
});
job->start();
checkTokenRefresh();
}
void EteSyncResource::setupCollection(Collection &collection, EteSyncJournal *journal)
......@@ -186,14 +187,12 @@ void EteSyncResource::setupCollection(Collection &collection, EteSyncJournal *jo
void EteSyncResource::retrieveItems(const Akonadi::Collection &collection)
{
QString journalUid = collection.remoteId();
QString prevUid = collection.remoteRevision();
auto job = new EntriesFetchJob(mClientState->client(), journalUid, prevUid, this);
auto job = new EntriesFetchJob(mClientState->client(), collection, this);
connect(job, &EntriesFetchJob::finished, this, &EteSyncResource::slotItemsRetrieved);
job->start();
checkTokenRefresh();
}
void EteSyncResource::slotItemsRetrieved(KJob *job)
......@@ -206,13 +205,13 @@ void EteSyncResource::slotItemsRetrieved(KJob *job)
qCDebug(ETESYNC_LOG) << "Retrieving entries";
EteSyncEntry **entries = qobject_cast<EntriesFetchJob *>(job)->entries();
Collection collection = currentCollection();
Collection collection = qobject_cast<EntriesFetchJob *>(job)->collection();
if (collection.contentMimeTypes().contains(KContacts::Addressee::mimeType())) {
if (collection.contentMimeTypes().contains(KContacts::Addressee::mimeType()) || collection.contentMimeTypes().contains(QStringLiteral("text/directory"))) {
mContactHandler->setupItems(entries, collection);
} else if (collection.contentMimeTypes().contains(KCalendarCore::Event::eventMimeType())) {
} else if (collection.contentMimeTypes().contains(KCalendarCore::Event::eventMimeType()) || collection.contentMimeTypes().contains(QStringLiteral("application/x-vnd.akonadi.calendar.event"))) {
mCalendarHandler->setupItems(entries, collection);
} else if (collection.contentMimeTypes().contains(KCalendarCore::Todo::todoMimeType())) {
} else if (collection.contentMimeTypes().contains(KCalendarCore::Todo::todoMimeType()) || collection.contentMimeTypes().contains(QStringLiteral("application/x-vnd.akonadi.calendar.todo"))) {
mTaskHandler->setupItems(entries, collection);
} else {
qCDebug(ETESYNC_LOG) << "Unknown MIME type";
......@@ -239,6 +238,14 @@ void EteSyncResource::initialiseDone(bool successful)
}
}
void EteSyncResource::checkTokenRefresh()
{
if (etesync_get_error_code() == EteSyncErrorCode::ETESYNC_ERROR_CODE_UNAUTHORIZED) {
deferTask();
mClientState->refreshToken();
}
}
QString EteSyncResource::baseDirectoryPath() const
{
return Settings::self()->basePath();
......@@ -277,6 +284,7 @@ void EteSyncResource::itemAdded(const Akonadi::Item &item,
qCDebug(ETESYNC_LOG) << "Unknown MIME type";
changeCommitted(item);
}
checkTokenRefresh();
}
void EteSyncResource::itemChanged(const Akonadi::Item &item,
......@@ -293,6 +301,7 @@ void EteSyncResource::itemChanged(const Akonadi::Item &item,
qCDebug(ETESYNC_LOG) << "Unknown MIME type";
changeCommitted(item);
}
checkTokenRefresh();
}
void EteSyncResource::itemRemoved(const Akonadi::Item &item)
......@@ -308,6 +317,7 @@ void EteSyncResource::itemRemoved(const Akonadi::Item &item)
qCDebug(ETESYNC_LOG) << "Unknown MIME type";
changeCommitted(item);
}
checkTokenRefresh();
}
void EteSyncResource::collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent)
......@@ -323,6 +333,7 @@ void EteSyncResource::collectionAdded(const Akonadi::Collection &collection, con
qCDebug(ETESYNC_LOG) << "Unknown MIME type";
changeCommitted(collection);
}
checkTokenRefresh();
}
void EteSyncResource::collectionChanged(const Akonadi::Collection &collection)
......@@ -338,6 +349,7 @@ void EteSyncResource::collectionChanged(const Akonadi::Collection &collection)
qCDebug(ETESYNC_LOG) << "Unknown MIME type";
changeCommitted(collection);
}
checkTokenRefresh();
}
void EteSyncResource::collectionRemoved(const Akonadi::Collection &collection)
......@@ -353,6 +365,7 @@ void EteSyncResource::collectionRemoved(const Akonadi::Collection &collection)
} else {
qCDebug(ETESYNC_LOG) << "Unknown MIME type";
}
checkTokenRefresh();
}
AKONADI_RESOURCE_MAIN(EteSyncResource)
......@@ -73,6 +73,8 @@ protected:
QString baseDirectoryPath() const;
void checkTokenRefresh();
private Q_SLOTS:
void onReloadConfiguration();
void initialiseDone(bool successful);
......
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