Commit d1a6a649 authored by Shashwat Jolly's avatar Shashwat Jolly

Fetch journal list once and cache them

Added a bool to determine if a normal sync is happening or if the user has force refreshed a collection
parent 796b2b95
......@@ -38,7 +38,7 @@ CalendarTaskBaseHandler::CalendarTaskBaseHandler(EteSyncResource *resource) : Ba
void CalendarTaskBaseHandler::getItemListFromEntries(EteSyncEntry **entries, Item::List &changedItems, Item::List &removedItems, Collection &collection, const QString &journalUid, QString &prevUid)
{
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), journalUid));
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
QMap<QString, KCalendarCore::Incidence::Ptr> incidences;
......@@ -137,7 +137,8 @@ void CalendarTaskBaseHandler::itemAdded(const Akonadi::Item &item,
calendar->addIncidence(item.payload<Incidence::Ptr>());
KCalendarCore::ICalFormat format;
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
......@@ -161,7 +162,8 @@ void CalendarTaskBaseHandler::itemChanged(const Akonadi::Item &item,
calendar->addIncidence(item.payload<Incidence::Ptr>());
KCalendarCore::ICalFormat format;
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
......@@ -180,7 +182,8 @@ void CalendarTaskBaseHandler::itemRemoved(const Akonadi::Item &item)
{
Collection collection = item.parentCollection();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
......@@ -221,7 +224,7 @@ void CalendarTaskBaseHandler::collectionAdded(const Akonadi::Collection &collect
void CalendarTaskBaseHandler::collectionChanged(const Akonadi::Collection &collection)
{
QString journalUid = collection.remoteId();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), journalUid));
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
auto journalColor = EteSyncDEFAULT_COLOR;
if (collection.hasAttribute<CollectionColorAttribute>()) {
......@@ -249,7 +252,7 @@ void CalendarTaskBaseHandler::collectionChanged(const Akonadi::Collection &colle
void CalendarTaskBaseHandler::collectionRemoved(const Akonadi::Collection &collection)
{
QString journalUid = collection.remoteId();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), journalUid));
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
const auto result = etesync_journal_manager_delete(mClientState->journalManager(), journal.get());
if (result) {
......
......@@ -45,7 +45,8 @@ const QString ContactHandler::etesyncCollectionType()
void ContactHandler::getItemListFromEntries(EteSyncEntry **entries, Item::List &changedItems, Item::List &removedItems, Collection &collection, const QString &journalUid, QString &prevUid)
{
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), journalUid));
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
QMap<QString, KContacts::Addressee> contacts;
......@@ -137,7 +138,8 @@ void ContactHandler::deleteLocalContact(const KContacts::Addressee &contact)
void ContactHandler::itemAdded(const Akonadi::Item &item,
const Akonadi::Collection &collection)
{
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
......@@ -160,7 +162,8 @@ void ContactHandler::itemChanged(const Akonadi::Item &item,
{
Collection collection = item.parentCollection();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
......@@ -182,7 +185,8 @@ void ContactHandler::itemRemoved(const Akonadi::Item &item)
{
Collection collection = item.parentCollection();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), collection.remoteId()));
QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCryptoManagerPtr cryptoManager(etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair()));
......@@ -218,7 +222,7 @@ void ContactHandler::collectionAdded(const Akonadi::Collection &collection, cons
void ContactHandler::collectionChanged(const Akonadi::Collection &collection)
{
QString journalUid = collection.remoteId();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), journalUid));
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
EteSyncCollectionInfoPtr info(etesync_collection_info_new(etesyncCollectionType(), collection.displayName(), QString(), EteSyncDEFAULT_COLOR));
......@@ -232,7 +236,7 @@ void ContactHandler::collectionChanged(const Akonadi::Collection &collection)
void ContactHandler::collectionRemoved(const Akonadi::Collection &collection)
{
QString journalUid = collection.remoteId();
EteSyncJournalPtr journal(etesync_journal_manager_fetch(mClientState->journalManager(), journalUid));
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
etesync_journal_manager_delete(mClientState->journalManager(), journal.get());
}
......@@ -75,7 +75,10 @@ private:
EteSyncJournalManagerPtr mJournalManager = nullptr;
EteSyncUserInfoPtr mUserInfo = nullptr;
EteSyncAsymmetricKeyPairPtr mKeypair = nullptr;
QString mUsername, mPassword, mServerUrl, mEncryptionPassword;
QString mUsername;
QString mPassword;
QString mServerUrl;
QString mEncryptionPassword;
};
#endif // ETESYNCSETTINGS_H
......@@ -113,6 +113,10 @@ void EteSyncResource::configure(WId windowId)
void EteSyncResource::retrieveCollections()
{
if (!mSyncing) {
mCollectionsToSync = 0;
}
mSyncing = true;
setCollectionStreamingEnabled(true);
// Set up root collection for resource
mResourceCollection = Collection();
......@@ -145,19 +149,17 @@ void EteSyncResource::slotCollectionsRetrieved(KJob *job)
if (job->error()) {
qCWarning(ETESYNC_LOG) << "Error in fetching journals";
qCDebug(ETESYNC_LOG) << "Incorrect token in retrieveCollections()";
handleTokenError(true);
handleTokenError();
return;
}
qCDebug(ETESYNC_LOG) << "Retrieving collections";
EteSyncJournal **journals = qobject_cast<JournalsFetchJob *>(job)->journals();
Collection::List list;
for (EteSyncJournal **iter = journals; *iter; iter++) {
EteSyncJournalPtr journal(*iter);
Collection collection;
collection.setParentCollection(mResourceCollection);
setupCollection(collection, journal.get());
setupCollection(collection, *iter);
mCollectionsToSync++;
list << collection;
}
free(journals);
......@@ -165,20 +167,13 @@ void EteSyncResource::slotCollectionsRetrieved(KJob *job)
collectionsRetrievalDone();
}
bool EteSyncResource::handleTokenError(bool forRetrieveCollections)
bool EteSyncResource::handleTokenError()
{
if (etesync_get_error_code() == EteSyncErrorCode::ETESYNC_ERROR_CODE_UNAUTHORIZED) {
qCDebug(ETESYNC_LOG) << "Invalid token";
if (forRetrieveCollections) {
mClientState->refreshToken();
auto job = new JournalsFetchJob(mClientState->client(), this);
connect(job, &JournalsFetchJob::finished, this, &EteSyncResource::slotCollectionsRetrieved);
job->start();
} else {
deferTask();
connect(mClientState, &EteSyncClientState::tokenRefreshed, this, &EteSyncResource::taskDone);
scheduleCustomTask(mClientState, "refreshToken", QVariant(), ResourceBase::Prepend);
}
deferTask();
connect(mClientState, &EteSyncClientState::tokenRefreshed, this, &EteSyncResource::taskDone);
scheduleCustomTask(mClientState, "refreshToken", QVariant(), ResourceBase::Prepend);
return false;
}
return true;
......@@ -221,6 +216,8 @@ void EteSyncResource::setupCollection(Collection &collection, EteSyncJournal *jo
collection.setRemoteId(journalUid);
collection.setName(displayName);
collection.setContentMimeTypes(mimeTypes);
mJournals[journalUid] = EteSyncJournalPtr(journal);
}
BaseHandler *EteSyncResource::fetchHandlerForMimeType(const QString &mimeType)
......@@ -253,6 +250,20 @@ BaseHandler *EteSyncResource::fetchHandlerForCollection(const Akonadi::Collectio
void EteSyncResource::retrieveItems(const Akonadi::Collection &collection)
{
if (mSyncing) {
mCollectionsToSync--;
if (mCollectionsToSync == 0) {
mSyncing = false;
}
QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = getJournal(journalUid);
QString lastEntryUid = QStringFromCharPtr(CharPtr(etesync_journal_get_last_uid(journal.get())));
if (lastEntryUid == collection.remoteRevision()) {
itemsRetrievalDone();
return;
}
}
auto job = new EntriesFetchJob(mClientState->client(), collection, this);
connect(job, &EntriesFetchJob::finished, this, &EteSyncResource::slotItemsRetrieved);
......
......@@ -74,7 +74,12 @@ protected:
QString baseDirectoryPath() const;
bool handleTokenError(bool forRetrieveCollections = false);
bool handleTokenError();
const EteSyncJournalPtr &getJournal(QString journalUid)
{
return mJournals[journalUid];
}
private Q_SLOTS:
void onReloadConfiguration();
......@@ -86,6 +91,9 @@ private:
Akonadi::Collection mResourceCollection;
EteSyncClientState *mClientState = nullptr;
std::vector<BaseHandler::Ptr> mHandlers;
std::map<QString, EteSyncJournalPtr> mJournals;
int mCollectionsToSync = 0;
bool mSyncing;
ContactHandler::Ptr mContactHandler = nullptr;
CalendarHandler::Ptr mCalendarHandler = nullptr;
......
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