Commit 9223b950 authored by Shashwat Jolly's avatar Shashwat Jolly

Add null checks for robustness

parent b36568ad
......@@ -22,6 +22,7 @@
#include <AkonadiCore/CollectionModifyJob>
#include <KCalendarCore/ICalFormat>
#include <KCalendarCore/MemoryCalendar>
#include <KLocalizedString>
#include <QFile>
#include "entriesfetchjob.h"
......@@ -52,6 +53,10 @@ void CalendarTaskBaseHandler::getItemListFromEntries(std::vector<EteSyncEntryPtr
KCalendarCore::ICalFormat format;
const KCalendarCore::Incidence::Ptr incidence = format.fromString(QStringFromCharPtr(contentStr));
if (!incidence || (incidence->uid()).isEmpty()) {
continue;
}
qCDebug(ETESYNC_LOG) << "Entry parsed into incidence - UID" << incidence->uid();
const QString action = QStringFromCharPtr(CharPtr(etesync_sync_entry_get_action(syncEntry.get())));
......@@ -133,19 +138,41 @@ void CalendarTaskBaseHandler::deleteLocalCalendar(const KCalendarCore::Incidence
void CalendarTaskBaseHandler::itemAdded(const Akonadi::Item &item,
const Akonadi::Collection &collection)
{
if (!item.hasPayload<Incidence::Ptr>()) {
qCDebug(ETESYNC_LOG) << "Received item with unknown payload";
mResource->cancelTask(i18n("Received item with unknown payload %1", item.mimeType()));
return;
}
KCalendarCore::Calendar::Ptr calendar(new MemoryCalendar(QTimeZone::utc()));
const auto incidence = item.payload<Incidence::Ptr>();
qCDebug(ETESYNC_LOG) << "Incidence mime type" << incidence->mimeType();
if (!collection.contentMimeTypes().contains(incidence->mimeType())) {
qCDebug(ETESYNC_LOG) << "Received item of different type";
mResource->cancelTask(i18n("Received item of different type"));
return;
}
calendar->addIncidence(incidence);
KCalendarCore::ICalFormat format;
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
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));
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
qCDebug(ETESYNC_LOG) << "Could not create EteSync entry";
mResource->cancelTask(i18n("Could not create EteSync entry"));
return;
}
......@@ -160,6 +187,11 @@ void CalendarTaskBaseHandler::itemAdded(const Akonadi::Item &item,
void CalendarTaskBaseHandler::itemChanged(const Akonadi::Item &item,
const QSet<QByteArray> &parts)
{
if (!item.hasPayload<Incidence::Ptr>()) {
qCDebug(ETESYNC_LOG) << "Received item with unknown payload";
mResource->cancelTask(i18n("Received item with unknown payload %1", item.mimeType()));
return;
}
Collection collection = item.parentCollection();
KCalendarCore::Calendar::Ptr calendar(new MemoryCalendar(QTimeZone::utc()));
......@@ -170,11 +202,19 @@ void CalendarTaskBaseHandler::itemChanged(const Akonadi::Item &item,
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
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));
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
qCDebug(ETESYNC_LOG) << "Could not create EteSync entry";
mResource->cancelTask(i18n("Could not create EteSync entry"));
return;
}
......@@ -192,6 +232,11 @@ void CalendarTaskBaseHandler::itemRemoved(const Akonadi::Item &item)
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
mResource->cancelTask();
return;
}
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
const QString calendar = getLocalCalendar(item.remoteId());
......@@ -199,10 +244,12 @@ void CalendarTaskBaseHandler::itemRemoved(const Akonadi::Item &item)
EteSyncSyncEntryPtr syncEntry = etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_DELETE), calendar);
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
qCDebug(ETESYNC_LOG) << "Could not create EteSync entry";
mResource->cancelTask(i18n("Could not create EteSync entry"));
return;
}
mResource->changeCommitted(item);
mResource->changeProcessed();
}
void CalendarTaskBaseHandler::collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent)
......@@ -234,6 +281,12 @@ void CalendarTaskBaseHandler::collectionChanged(const Akonadi::Collection &colle
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
auto journalColor = ETESYNC_COLLECTION_DEFAULT_COLOR;
if (collection.hasAttribute<CollectionColorAttribute>()) {
const CollectionColorAttribute *colorAttr = collection.attribute<CollectionColorAttribute>();
......@@ -264,10 +317,16 @@ void CalendarTaskBaseHandler::collectionRemoved(const Akonadi::Collection &colle
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
const auto result = etesync_journal_manager_delete(mClientState->journalManager(), journal.get());
if (result) {
mResource->handleTokenError();
return;
}
mResource->changeCommitted(collection);
mResource->changeProcessed();
}
......@@ -20,6 +20,7 @@
#include <kcontacts/vcardconverter.h>
#include <AkonadiCore/CollectionModifyJob>
#include <KLocalizedString>
#include <QFile>
#include "entriesfetchjob.h"
......@@ -59,6 +60,10 @@ void ContactHandler::getItemListFromEntries(std::vector<EteSyncEntryPtr> &entrie
QByteArray content(contentStr.get());
const KContacts::Addressee contact = converter.parseVCard(content);
if ((contact.uid()).isEmpty()) {
continue;
}
const QString action = QStringFromCharPtr(CharPtr(etesync_sync_entry_get_action(syncEntry.get())));
if (action == QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_ADD) || action == QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_CHANGE)) {
contacts[contact.uid()] = contact;
......@@ -135,9 +140,20 @@ void ContactHandler::deleteLocalContact(const KContacts::Addressee &contact)
void ContactHandler::itemAdded(const Akonadi::Item &item,
const Akonadi::Collection &collection)
{
if (!item.hasPayload<KContacts::Addressee>()) {
qCDebug(ETESYNC_LOG) << "Received item with unknown payload";
mResource->cancelTask(i18n("Received item with unknown payload %1", item.mimeType()));
return;
}
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
KContacts::VCardConverter converter;
......@@ -147,6 +163,8 @@ void ContactHandler::itemAdded(const Akonadi::Item &item,
EteSyncSyncEntryPtr syncEntry = etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_ADD), QString::fromUtf8(content));
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
qCDebug(ETESYNC_LOG) << "Could not create EteSync entry";
mResource->cancelTask(i18n("Could not create EteSync entry"));
return;
}
......@@ -161,11 +179,22 @@ void ContactHandler::itemAdded(const Akonadi::Item &item,
void ContactHandler::itemChanged(const Akonadi::Item &item,
const QSet<QByteArray> &parts)
{
if (!item.hasPayload<KContacts::Addressee>()) {
qCDebug(ETESYNC_LOG) << "Received item with unknown payload";
mResource->cancelTask(i18n("Received item with unknown payload %1", item.mimeType()));
return;
}
Collection collection = item.parentCollection();
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
KContacts::VCardConverter converter;
......@@ -175,6 +204,8 @@ void ContactHandler::itemChanged(const Akonadi::Item &item,
EteSyncSyncEntryPtr syncEntry = etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_CHANGE), QString::fromUtf8(content));
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
qCDebug(ETESYNC_LOG) << "Could not create EteSync entry";
mResource->cancelTask(i18n("Could not create EteSync entry"));
return;
}
......@@ -192,6 +223,11 @@ void ContactHandler::itemRemoved(const Akonadi::Item &item)
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
mResource->cancelTask();
return;
}
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
const QString contact = getLocalContact(item.remoteId());
......@@ -199,10 +235,12 @@ void ContactHandler::itemRemoved(const Akonadi::Item &item)
EteSyncSyncEntryPtr syncEntry = etesync_sync_entry_new(QStringLiteral(ETESYNC_SYNC_ENTRY_ACTION_DELETE), contact);
if (!createEteSyncEntry(syncEntry.get(), cryptoManager.get(), collection)) {
qCDebug(ETESYNC_LOG) << "Could not create EteSync entry";
mResource->cancelTask(i18n("Could not create EteSync entry"));
return;
}
mResource->changeCommitted(item);
mResource->changeProcessed();
}
void ContactHandler::collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent)
......@@ -233,6 +271,12 @@ void ContactHandler::collectionChanged(const Akonadi::Collection &collection)
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
EteSyncCollectionInfoPtr info = etesync_collection_info_new(etesyncCollectionType(), collection.displayName(), QString(), EteSyncDEFAULT_COLOR);
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal.get(), mClientState->derived(), mClientState->keypair());
......@@ -255,10 +299,16 @@ void ContactHandler::collectionRemoved(const Akonadi::Collection &collection)
const QString journalUid = collection.remoteId();
const EteSyncJournalPtr &journal = mResource->getJournal(journalUid);
if (!journal) {
qCDebug(ETESYNC_LOG) << "Could not get journal";
mResource->cancelTask(i18n("Could not get journal"));
return;
}
const auto result = etesync_journal_manager_delete(mClientState->journalManager(), journal.get());
if (result) {
mResource->handleTokenError();
return;
}
mResource->changeCommitted(collection);
mResource->changeProcessed();
}
......@@ -17,6 +17,8 @@
#include "etesyncresource.h"
#include <kcontacts/addressee.h>
#include <kcontacts/contactgroup.h>
#include <kwindowsystem.h>
#include <AkonadiCore/CachePolicy>
......@@ -115,6 +117,8 @@ void EteSyncResource::retrieveCollections()
{
setCollectionStreamingEnabled(true);
mJournalsCache.clear();
auto job = new JournalsFetchJob(mClientState->client(), this);
connect(job, &JournalsFetchJob::finished, this, &EteSyncResource::slotCollectionsRetrieved);
job->start();
......@@ -183,6 +187,10 @@ bool EteSyncResource::handleTokenError()
void EteSyncResource::setupCollection(Collection &collection, EteSyncJournal *journal)
{
if (!journal) {
qCDebug(ETESYNC_LOG) << "Unable to setup collection - journal is null";
return;
}
EteSyncCryptoManagerPtr cryptoManager = etesync_journal_get_crypto_manager(journal, mClientState->derived(), mClientState->keypair());
EteSyncCollectionInfoPtr info(etesync_journal_get_info(journal, cryptoManager.get()));
......@@ -197,6 +205,7 @@ void EteSyncResource::setupCollection(Collection &collection, EteSyncJournal *jo
if (type == QStringLiteral(ETESYNC_COLLECTION_TYPE_ADDRESS_BOOK)) {
mimeTypes.push_back(KContacts::Addressee::mimeType());
mimeTypes.push_back(KContacts::ContactGroup::mimeType());
attr->setDisplayName(displayName);
attr->setIconName(QStringLiteral("view-pim-contacts"));
} else if (type == QStringLiteral(ETESYNC_COLLECTION_TYPE_CALENDAR)) {
......
......@@ -18,9 +18,6 @@
#ifndef ETESYNCRESOURCE_H
#define ETESYNCRESOURCE_H
#include <kcontacts/addressee.h>
#include <kcontacts/vcardconverter.h>
#include <AkonadiAgentBase/ResourceBase>
#include "calendarhandler.h"
......
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