Commit 1e5cbee8 authored by Shashwat Jolly's avatar Shashwat Jolly

Create default journals on initialising account

parent a70638a5
......@@ -65,7 +65,7 @@ bool BaseHandler::createEteSyncEntry(const EteSyncSyncEntry *syncEntry, const Et
EteSyncEntryPtr entry = etesync_entry_from_sync_entry(cryptoManager, syncEntry, collection.remoteRevision());
if (!entry) {
qCDebug(ETESYNC_LOG) << "Could not create entry from sync entry";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message();
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
return false;
}
EteSyncEntryManagerPtr entryManager = etesync_entry_manager_new(mClientState->client(), collection.remoteId());
......
......@@ -60,7 +60,7 @@ void CalendarTaskBaseHandler::getItemListFromEntries(std::vector<EteSyncEntryPtr
if (!syncEntry) {
qCDebug(ETESYNC_LOG) << "SetupItems: syncEntry is null for entry" << etesync_entry_get_uid(entry.get());
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message();
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
prevUid = QStringFromCharPtr(CharPtr(etesync_entry_get_uid(entry.get())));
continue;
}
......@@ -91,7 +91,7 @@ void CalendarTaskBaseHandler::getItemListFromEntries(std::vector<EteSyncEntryPtr
item.setRemoteId(incidence->uid());
removedItems.push_back(item);
deleteLocalCalendar(incidence);
deleteLocalCalendar(incidence->uid());
}
}
......@@ -144,9 +144,9 @@ bool CalendarTaskBaseHandler::updateLocalCalendar(const KCalendarCore::Incidence
return true;
}
void CalendarTaskBaseHandler::deleteLocalCalendar(const KCalendarCore::Incidence::Ptr &incidence)
void CalendarTaskBaseHandler::deleteLocalCalendar(const QString &incidenceUid)
{
const QString path = baseDirectoryPath() + QLatin1Char('/') + incidence->uid() + QLatin1String(".ical");
const QString path = baseDirectoryPath() + QLatin1Char('/') + incidenceUid + QLatin1String(".ical");
QFile file(path);
if (!file.remove()) {
qCDebug(ETESYNC_LOG) << "Unable to remove " << path << file.errorString();
......@@ -158,7 +158,7 @@ void CalendarTaskBaseHandler::itemAdded(const Akonadi::Item &item,
const Akonadi::Collection &collection)
{
if (!item.hasPayload<Incidence::Ptr>()) {
qCDebug(ETESYNC_LOG) << "Received item with unknown payload";
qCDebug(ETESYNC_LOG) << "Received item with unknown payload - Remote ID: " << item.remoteId();
mResource->cancelTask(i18n("Received item with unknown payload %1", item.mimeType()));
return;
}
......@@ -248,6 +248,16 @@ void CalendarTaskBaseHandler::itemRemoved(const Akonadi::Item &item)
{
Collection collection = item.parentCollection();
const QString calendar = getLocalCalendar(item.remoteId());
if (calendar.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Could not get local calendar";
mResource->cancelTask(i18n("Could not get local calendar"));
return;
}
// Delete now, because itemRemoved() may be called when collection is removed
deleteLocalCalendar(item.remoteId());
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
......@@ -258,14 +268,6 @@ void CalendarTaskBaseHandler::itemRemoved(const Akonadi::Item &item)
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
const QString calendar = getLocalCalendar(item.remoteId());
if (calendar.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Could not get local calendar";
mResource->cancelTask(i18n("Could not get local calendar"));
return;
}
EteSyncSyncEntryPtr syncEntry = etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_DELETE), calendar);
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
......@@ -282,21 +284,20 @@ void CalendarTaskBaseHandler::collectionAdded(const Akonadi::Collection &collect
const QString journalUid = QStringFromCharPtr(CharPtr(etesync_gen_uid()));
EteSyncJournalPtr journal = etesync_journal_new(journalUid, ETESYNC_CURRENT_VERSION);
/// TODO: Description?
EteSyncCollectionInfoPtr info = etesync_collection_info_new(etesyncCollectionType(), collection.displayName(), QString(), ETESYNC_COLLECTION_DEFAULT_COLOR);
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
if (etesync_journal_set_info(journal.get(), cryptoManager.get(), info.get())) {
qCDebug(ETESYNC_LOG) << "Could not set journal info";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->cancelTask(i18n("Could not set journal info"));
return;
};
if (etesync_journal_manager_create(mClientState->journalManager(), journal.get())) {
qCDebug(ETESYNC_LOG) << "Could not create journal";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->handleTokenError();
return;
}
......@@ -332,14 +333,14 @@ void CalendarTaskBaseHandler::collectionChanged(const Akonadi::Collection &colle
if (etesync_journal_set_info(journal.get(), cryptoManager.get(), info.get())) {
qCDebug(ETESYNC_LOG) << "Could not set journal info";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->cancelTask(i18n("Could not set journal info"));
return;
};
if (etesync_journal_manager_update(mClientState->journalManager(), journal.get())) {
qCDebug(ETESYNC_LOG) << "Could not update journal";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->handleTokenError();
return;
}
......@@ -362,7 +363,7 @@ void CalendarTaskBaseHandler::collectionRemoved(const Akonadi::Collection &colle
if (etesync_journal_manager_delete(mClientState->journalManager(), journal.get())) {
qCDebug(ETESYNC_LOG) << "Could not delete journal";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->handleTokenError();
return;
}
......
......@@ -47,7 +47,7 @@ public:
protected:
QString getLocalCalendar(const QString &incidenceUid) const;
bool updateLocalCalendar(const KCalendarCore::Incidence::Ptr &incidence);
void deleteLocalCalendar(const KCalendarCore::Incidence::Ptr &incidence);
void deleteLocalCalendar(const QString &incidenceUid);
QString baseDirectoryPath() const override;
};
......
......@@ -66,7 +66,7 @@ void ContactHandler::getItemListFromEntries(std::vector<EteSyncEntryPtr> &entrie
if (!syncEntry) {
qCDebug(ETESYNC_LOG) << "SetupItems: syncEntry is null for entry" << etesync_entry_get_uid(entry.get());
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message();
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
prevUid = QStringFromCharPtr(CharPtr(etesync_entry_get_uid(entry.get())));
continue;
}
......@@ -97,7 +97,7 @@ void ContactHandler::getItemListFromEntries(std::vector<EteSyncEntryPtr> &entrie
item.setRemoteId(contact.uid());
removedItems.push_back(item);
deleteLocalContact(contact);
deleteLocalContact(contact.uid());
}
}
......@@ -147,9 +147,9 @@ void ContactHandler::updateLocalContact(const KContacts::Addressee &contact)
file.write(converter.createVCard(contact));
}
void ContactHandler::deleteLocalContact(const KContacts::Addressee &contact)
void ContactHandler::deleteLocalContact(const QString &contactUid)
{
const QString path = baseDirectoryPath() + QLatin1Char('/') + contact.uid() + QLatin1String(".vcf");
const QString path = baseDirectoryPath() + QLatin1Char('/') + contactUid + QLatin1String(".vcf");
QFile file(path);
if (!file.remove()) {
qCDebug(ETESYNC_LOG) << "Unable to remove " << path << file.errorString();
......@@ -252,6 +252,17 @@ void ContactHandler::itemRemoved(const Akonadi::Item &item)
{
Collection collection = item.parentCollection();
const QString contact = getLocalContact(item.remoteId());
if (contact.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Could not get local contact";
mResource->cancelTask(i18n("Could not get local contact"));
return;
}
// Delete now, because itemRemoved() may be called when collection is removed
deleteLocalContact(item.remoteId());
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
......@@ -262,14 +273,6 @@ void ContactHandler::itemRemoved(const Akonadi::Item &item)
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
const QString contact = getLocalContact(item.remoteId());
if (contact.isEmpty()) {
qCDebug(ETESYNC_LOG) << "Could not get local contact";
mResource->cancelTask(i18n("Could not get local contact"));
return;
}
EteSyncSyncEntryPtr syncEntry = etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_DELETE), contact);
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
......@@ -292,14 +295,14 @@ void ContactHandler::collectionAdded(const Akonadi::Collection &collection, cons
if (etesync_journal_set_info(journal.get(), cryptoManager.get(), info.get())) {
qCDebug(ETESYNC_LOG) << "Could not set journal info";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->cancelTask(i18n("Could not set journal info"));
return;
};
if (etesync_journal_manager_create(mClientState->journalManager(), journal.get())) {
qCDebug(ETESYNC_LOG) << "Could not create journal";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->handleTokenError();
return;
}
......@@ -327,14 +330,14 @@ void ContactHandler::collectionChanged(const Akonadi::Collection &collection)
if (etesync_journal_set_info(journal.get(), cryptoManager.get(), info.get())) {
qCDebug(ETESYNC_LOG) << "Could not set journal info";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->cancelTask(i18n("Could not set journal info"));
return;
};
if (etesync_journal_manager_update(mClientState->journalManager(), journal.get())) {
qCDebug(ETESYNC_LOG) << "Could not update journal";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->handleTokenError();
return;
}
......@@ -357,7 +360,7 @@ void ContactHandler::collectionRemoved(const Akonadi::Collection &collection)
if (etesync_journal_manager_delete(mClientState->journalManager(), journal.get())) {
qCDebug(ETESYNC_LOG) << "Could not delete journal";
qCDebug(ETESYNC_LOG) << "EteSync error" << etesync_get_error_message;
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
mResource->handleTokenError();
return;
}
......
......@@ -50,7 +50,7 @@ public:
protected:
QString getLocalContact(const QString &contactUid) const;
void updateLocalContact(const KContacts::Addressee &contact);
void deleteLocalContact(const KContacts::Addressee &contact);
void deleteLocalContact(const QString &incidenceUid);
QString baseDirectoryPath() const override;
......
......@@ -17,6 +17,8 @@
#include "etesyncclientstate.h"
#include <KLocalizedString>
#include "etesync_debug.h"
void EteSyncClientState::init()
......@@ -103,6 +105,7 @@ bool EteSyncClientState::initUserInfo()
mUserInfo = etesync_user_info_manager_fetch(userInfoManager.get(), mUsername);
if (!mUserInfo) {
qCWarning(ETESYNC_LOG) << "User info obtained from server is NULL";
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
invalidateToken();
return false;
}
......@@ -123,13 +126,13 @@ bool EteSyncClientState::initKeypair(const QString &encryptionPassword)
mKeypair = EteSyncAsymmetricKeyPairPtr(etesync_user_info_get_keypair(mUserInfo.get(), userInfoCryptoManager.get()));
if (!mKeypair) {
qCDebug(ETESYNC_LOG) << "Empty keypair";
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
return false;
}
qCDebug(ETESYNC_LOG) << "Received keypair";
return true;
}
/// TODO: Create default journals
void EteSyncClientState::initAccount(const QString &encryptionPassword)
{
mEncryptionPassword = encryptionPassword;
......@@ -139,7 +142,37 @@ void EteSyncClientState::initAccount(const QString &encryptionPassword)
EteSyncCryptoManagerPtr userInfoCryptoManager = etesync_user_info_get_crypto_manager(mUserInfo.get(), mDerived);
etesync_user_info_set_keypair(mUserInfo.get(), userInfoCryptoManager.get(), mKeypair.get());
EteSyncUserInfoManagerPtr userInfoManager(etesync_user_info_manager_new(mClient.get()));
etesync_user_info_manager_create(userInfoManager.get(), mUserInfo.get());
if (!etesync_user_info_manager_create(userInfoManager.get(), mUserInfo.get())) {
qCDebug(ETESYNC_LOG) << "Could not create user info";
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
return;
};
createDefaultJournal(QStringLiteral(ETESYNC_COLLECTION_TYPE_CALENDAR), i18n("My Calendar"));
createDefaultJournal(QStringLiteral(ETESYNC_COLLECTION_TYPE_ADDRESS_BOOK), i18n("My Contacts"));
createDefaultJournal(QStringLiteral(ETESYNC_COLLECTION_TYPE_TASKS), i18n("My Tasks"));
}
void EteSyncClientState::createDefaultJournal(const QString &journalType, const QString &journalName)
{
const QString journalUid = QStringFromCharPtr(CharPtr(etesync_gen_uid()));
EteSyncJournalPtr journal = etesync_journal_new(journalUid, ETESYNC_CURRENT_VERSION);
EteSyncCollectionInfoPtr info = etesync_collection_info_new(journalType, journalName, QString(), ETESYNC_COLLECTION_DEFAULT_COLOR);
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mDerived, mKeypair.get());
if (etesync_journal_set_info(journal.get(), cryptoManager.get(), info.get())) {
qCDebug(ETESYNC_LOG) << "Could not set journal info";
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
return;
};
if (etesync_journal_manager_create(mJournalManager.get(), journal.get())) {
qCDebug(ETESYNC_LOG) << "Could not create journal";
qCDebug(ETESYNC_LOG) << "EteSync error" << QStringFromCharPtr(CharPtr(etesync_get_error_message()));
return;
}
}
void EteSyncClientState::saveSettings()
......
......@@ -69,6 +69,7 @@ Q_SIGNALS:
void tokenRefreshed();
private:
void createDefaultJournal(const QString &journalType, const QString &journalName);
EteSyncPtr mClient;
QString mDerived;
QString mToken;
......
......@@ -151,7 +151,6 @@ void EteSyncResource::slotCollectionsRetrieved(KJob *job)
{
if (job->error()) {
qCWarning(ETESYNC_LOG) << "Error in fetching journals";
qCDebug(ETESYNC_LOG) << "Incorrect token in retrieveCollections()";
handleTokenError();
return;
}
......@@ -287,7 +286,6 @@ void EteSyncResource::slotItemsRetrieved(KJob *job)
{
if (job->error()) {
qCWarning(ETESYNC_LOG) << job->errorText();
qCDebug(ETESYNC_LOG) << "Incorrect token in retrieveItems()";
handleTokenError();
return;
}
......@@ -355,6 +353,7 @@ void EteSyncResource::itemAdded(const Akonadi::Item &item,
const Akonadi::Collection &collection)
{
qCDebug(ETESYNC_LOG) << "Item added" << item.mimeType();
qCDebug(ETESYNC_LOG) << "Journal UID" << collection.remoteId();
auto handler = fetchHandlerForMimeType(item.mimeType());
if (handler) {
......@@ -369,6 +368,7 @@ void EteSyncResource::itemChanged(const Akonadi::Item &item,
const QSet<QByteArray> &parts)
{
qCDebug(ETESYNC_LOG) << "Item changed" << item.mimeType();
qCDebug(ETESYNC_LOG) << "Journal UID" << item.parentCollection().remoteId();
auto handler = fetchHandlerForMimeType(item.mimeType());
if (handler) {
......@@ -420,7 +420,6 @@ void EteSyncResource::collectionChanged(const Akonadi::Collection &collection)
void EteSyncResource::collectionRemoved(const Akonadi::Collection &collection)
{
/// TODO: Remove local contacts for this collection
qCDebug(ETESYNC_LOG) << "Collection removed" << collection.mimeType();
auto handler = fetchHandlerForCollection(collection);
......
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