Commit dc3dec0c authored by Shashwat Jolly's avatar Shashwat Jolly
Browse files

Create new collections for a new EteSync user

JournalsFetchJob checks if the user has any previous EteSync collections, and creates the default collections if the user is new.
parent 4a648396
......@@ -17,6 +17,9 @@
#include "etesync_debug.h"
#define COLLECTIONS_FETCH_BATCH_SIZE 50
#define ETESYNC_DEFAULT_CALENDAR_NAME QStringLiteral("My Calendar")
#define ETESYNC_DEFAULT_ADDRESS_BOOK_NAME QStringLiteral("My Contacts")
#define ETESYNC_DEFAULT_TASKS_NAME QStringLiteral("My Tasks")
using namespace EteSyncAPI;
using namespace Akonadi;
......@@ -57,6 +60,10 @@ void JournalsFetchJob::fetchJournals()
}
mSyncToken = mResourceCollection.remoteRevision();
bool firstSync = false;
if (mSyncToken.isEmpty()) {
firstSync = true;
}
bool done = 0;
EtebaseCollectionManagerPtr collectionManager(etebase_account_get_collection_manager(account));
......@@ -88,11 +95,9 @@ void JournalsFetchJob::fetchJournals()
setErrorText(QString::fromUtf8(err));
}
Collection collection;
for (uintptr_t i = 0; i < dataLength; i++) {
mClientState->saveEtebaseCollectionCache(etesyncCollections[i]);
setupCollection(collection, etesyncCollections[i]);
setupCollection(etesyncCollections[i]);
}
uintptr_t removedCollectionsLength = etebase_collection_list_response_get_removed_memberships_length(collectionList.get());
......@@ -114,9 +119,16 @@ void JournalsFetchJob::fetchJournals()
}
}
}
// Create default collections if this is a new user and has no EteSync collections
if (firstSync && !mHasPimCollections) {
createDefaultCollection(ETEBASE_COLLECTION_TYPE_CALENDAR, ETESYNC_DEFAULT_CALENDAR_NAME);
createDefaultCollection(ETEBASE_COLLECTION_TYPE_ADDRESS_BOOK, ETESYNC_DEFAULT_ADDRESS_BOOK_NAME);
createDefaultCollection(ETEBASE_COLLECTION_TYPE_TASKS, ETESYNC_DEFAULT_TASKS_NAME);
}
}
void JournalsFetchJob::setupCollection(Akonadi::Collection &collection, const EtebaseCollection *etesyncCollection)
void JournalsFetchJob::setupCollection(const EtebaseCollection *etesyncCollection)
{
if (!etesyncCollection) {
qCDebug(ETESYNC_LOG) << "Unable to setup collection - etesyncCollection is null";
......@@ -133,6 +145,8 @@ void JournalsFetchJob::setupCollection(Akonadi::Collection &collection, const Et
QStringList mimeTypes;
Collection collection;
auto attr = collection.attribute<EntityDisplayAttribute>(Collection::AddIfMissing);
const QString displayName = QString::fromUtf8(etebase_collection_metadata_get_name(metaData.get()));
......@@ -144,14 +158,17 @@ void JournalsFetchJob::setupCollection(Akonadi::Collection &collection, const Et
mimeTypes.push_back(KContacts::ContactGroup::mimeType());
attr->setDisplayName(displayName);
attr->setIconName(QStringLiteral("view-pim-contacts"));
mHasPimCollections = true;
} else if (type == ETEBASE_COLLECTION_TYPE_CALENDAR) {
mimeTypes.push_back(KCalendarCore::Event::eventMimeType());
attr->setDisplayName(displayName);
attr->setIconName(QStringLiteral("view-calendar"));
mHasPimCollections = true;
} else if (type == ETEBASE_COLLECTION_TYPE_TASKS) {
mimeTypes.push_back(KCalendarCore::Todo::todoMimeType());
attr->setDisplayName(displayName);
attr->setIconName(QStringLiteral("view-pim-tasks"));
mHasPimCollections = true;
} else {
qCInfo(ETESYNC_LOG) << "Unknown collection type. Cannot set collection mime type.";
return;
......@@ -185,3 +202,41 @@ void JournalsFetchJob::setupCollection(Akonadi::Collection &collection, const Et
mCollections.push_back(collection);
}
void JournalsFetchJob::createDefaultCollection(const QString &collectionType, const QString &collectionName)
{
qCDebug(ETESYNC_LOG) << "Creating default collection" << collectionName;
EtebaseCollectionManagerPtr collectionManager(etebase_account_get_collection_manager(mClientState->account()));
// Create metadata
int64_t modificationTimeSinceEpoch = QDateTime::currentMSecsSinceEpoch();
EtebaseCollectionMetadataPtr collectionMetaData(etebase_collection_metadata_new(collectionType, collectionName));
etebase_collection_metadata_set_color(collectionMetaData.get(), ETESYNC_DEFAULT_COLLECTION_COLOR);
etebase_collection_metadata_set_mtime(collectionMetaData.get(), &modificationTimeSinceEpoch);
qCDebug(ETESYNC_LOG) << "Created metadata";
// Create EteSync collection
EtebaseCollectionPtr etesyncCollection(etebase_collection_manager_create(collectionManager.get(), collectionMetaData.get(), nullptr, 0));
if (!etesyncCollection) {
qCDebug(ETESYNC_LOG) << "Could not create new etesyncCollection";
qCDebug(ETESYNC_LOG) << "Etebase error;" << etebase_error_get_message();
return;
}
qCDebug(ETESYNC_LOG) << "Created EteSync collection";
// Upload to server
if (etebase_collection_manager_upload(collectionManager.get(), etesyncCollection.get(), NULL)) {
qCDebug(ETESYNC_LOG) << "Error uploading collection addition";
qCDebug(ETESYNC_LOG) << "Etebase error:" << etebase_error_get_message();
return;
} else {
qCDebug(ETESYNC_LOG) << "Uploaded collection addition to server";
}
// Save to cache
mClientState->saveEtebaseCollectionCache(etesyncCollection.get());
setupCollection(etesyncCollection.get());
}
......@@ -41,14 +41,15 @@ public:
protected:
void fetchJournals();
void setupCollection(Akonadi::Collection &collection, const EtebaseCollection *etesyncCollection);
void setupCollection(const EtebaseCollection *etesyncCollection);
void createDefaultCollection(const QString &collectionType, const QString &collectionName);
private:
const EteSyncClientState *mClientState = nullptr;
Akonadi::Collection::List mCollections;
Akonadi::Collection::List mRemovedCollections;
const Akonadi::Collection mResourceCollection;
QString mSyncToken;
bool mHasPimCollections = false;
};
} // namespace EteSyncAPI
......
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