Commit 367a6c60 authored by Shashwat Jolly's avatar Shashwat Jolly

Detect new accounts using the new list_multi API

We fetch collection list with limit 1 and check account permissions and detect new account from the result.
Account initialization has been moved to LoginJob instead of JournalsFetchJob.
parent 358db854
Pipeline #38298 canceled with stage
......@@ -141,7 +141,6 @@ void EntriesFetchJob::setupItem(const EtebaseItem *etesyncItem, const QString &t
etebase_item_get_content(etesyncItem, content.data(), len);
}
item.setPayloadFromData(content);
// qCDebug(ETESYNC_LOG) << item.payloadData()<<endl;
if (etebase_item_is_deleted(etesyncItem)) {
mRemovedItems.push_back(item);
......
......@@ -100,12 +100,12 @@ void EteSyncClientState::logout()
deleteWalletEntry();
}
bool EteSyncClientState::accountStatus()
EteSyncClientState::AccountStatus EteSyncClientState::accountStatus()
{
if (!mAccount) {
qCDebug(ETESYNC_LOG) << "Could not fetch collection list with limit 1";
qCDebug(ETESYNC_LOG) << "Etebase account is null";
return false;
return ERROR;
}
EtebaseCollectionManagerPtr collectionManager(etebase_account_get_collection_manager(mAccount.get()));
EtebaseFetchOptionsPtr fetchOptions(etebase_fetch_options_new());
......@@ -116,9 +116,14 @@ bool EteSyncClientState::accountStatus()
if (!collectionList) {
qCDebug(ETESYNC_LOG) << "Could not fetch collection list with limit 1";
qCDebug(ETESYNC_LOG) << "Etebase error" << etebase_error_get_message();
return false;
return ERROR;
}
return true;
uintptr_t dataLength = etebase_collection_list_response_get_data_length(collectionList.get());
if (dataLength == 0) {
return NEW_ACCOUNT;
}
return OK;
}
void EteSyncClientState::refreshToken()
......
......@@ -20,12 +20,18 @@ public:
typedef std::unique_ptr<EteSyncClientState> Ptr;
explicit EteSyncClientState(const QString &agentId, WId winId);
enum AccountStatus {
OK,
NEW_ACCOUNT,
ERROR
};
void init();
void saveSettings();
void invalidateToken();
bool login(const QString &serverUrl, const QString &username, const QString &password);
void logout();
bool accountStatus();
AccountStatus accountStatus();
bool openWalletFolder();
void deleteWalletEntry();
void saveAccount();
......
......@@ -17,9 +17,6 @@
#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;
......@@ -60,10 +57,6 @@ 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));
......@@ -119,13 +112,6 @@ 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(const EtebaseCollection *etesyncCollection)
......@@ -159,17 +145,14 @@ void JournalsFetchJob::setupCollection(const EtebaseCollection *etesyncCollectio
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;
......@@ -203,43 +186,3 @@ void JournalsFetchJob::setupCollection(const EtebaseCollection *etesyncCollectio
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();
EtebaseItemMetadataPtr collectionMetaData(etebase_item_metadata_new());
etebase_item_metadata_set_item_type(collectionMetaData.get(), collectionType);
etebase_item_metadata_set_name(collectionMetaData.get(), collectionName);
etebase_item_metadata_set_color(collectionMetaData.get(), ETESYNC_DEFAULT_COLLECTION_COLOR);
etebase_item_metadata_set_mtime(collectionMetaData.get(), &modificationTimeSinceEpoch);
qCDebug(ETESYNC_LOG) << "Created metadata";
// Create EteSync collection
EtebaseCollectionPtr etesyncCollection(etebase_collection_manager_create(collectionManager.get(), collectionType, 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());
}
......@@ -42,14 +42,12 @@ public:
protected:
void fetchJournals();
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
......
......@@ -10,6 +10,10 @@
#include "etesync_debug.h"
#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;
LoginJob::LoginJob(EteSyncClientState *clientState, const QString &serverUrl, const QString &username, const QString &password, QObject *parent)
......@@ -44,12 +48,58 @@ void LoginJob::login()
setErrorText(QString::fromUtf8(err));
return;
}
mAccountStatusResult = mClientState->accountStatus();
qCDebug(ETESYNC_LOG) << "Account status result" << mAccountStatusResult;
if (!mAccountStatusResult) {
EteSyncClientState::AccountStatus accountStatusResult = mClientState->accountStatus();
qCDebug(ETESYNC_LOG) << "Account status result" << accountStatusResult;
if (accountStatusResult == EteSyncClientState::ERROR) {
qCDebug(ETESYNC_LOG) << "Returning error from LoginJob";
setError(etebase_error_get_code());
const char *err = etebase_error_get_message();
setErrorText(QString::fromUtf8(err));
return;
}
// Create default collections if this is a new user and has no EteSync collections
if (accountStatusResult == EteSyncClientState::NEW_ACCOUNT) {
qCDebug(ETESYNC_LOG) << "New account: creating default collections";
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);
}
mAccountStatusResult = true;
}
void LoginJob::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();
EtebaseItemMetadataPtr collectionMetaData(etebase_item_metadata_new());
etebase_item_metadata_set_item_type(collectionMetaData.get(), collectionType);
etebase_item_metadata_set_name(collectionMetaData.get(), collectionName);
etebase_item_metadata_set_color(collectionMetaData.get(), ETESYNC_DEFAULT_COLLECTION_COLOR);
etebase_item_metadata_set_mtime(collectionMetaData.get(), &modificationTimeSinceEpoch);
qCDebug(ETESYNC_LOG) << "Created metadata";
// Create EteSync collection
EtebaseCollectionPtr etesyncCollection(etebase_collection_manager_create(collectionManager.get(), collectionType, 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";
}
}
......@@ -34,13 +34,14 @@ public:
private:
void login();
void createDefaultCollection(const QString &collectionType, const QString &collectionName);
EteSyncClientState *mClientState = nullptr;
QString mServerUrl;
QString mUsername;
QString mPassword;
bool mLoginResult;
bool mAccountStatusResult;
bool mAccountStatusResult = false;
bool mUserInfoResult;
};
} // 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