From 4af442583111f32e5c0a1e021276955a21df760d Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Wed, 11 Mar 2020 13:28:40 +1000 Subject: [PATCH] Refactor out a function for improved maintainability Setting notebook properties based on calendar info. --- .../googlecalendarsyncadaptor.cpp | 58 ++++++++----------- .../googlecalendarsyncadaptor.h | 23 +++++--- 2 files changed, 39 insertions(+), 42 deletions(-) diff --git a/src/google/google-calendars/googlecalendarsyncadaptor.cpp b/src/google/google-calendars/googlecalendarsyncadaptor.cpp index 0c88f6c..93f049e 100644 --- a/src/google/google-calendars/googlecalendarsyncadaptor.cpp +++ b/src/google/google-calendars/googlecalendarsyncadaptor.cpp @@ -2269,6 +2269,27 @@ void GoogleCalendarSyncAdaptor::upsyncFinishedHandler() decrementSemaphore(accountId); } +void GoogleCalendarSyncAdaptor::setCalendarProperties( + mKCal::Notebook::Ptr notebook, + const CalendarInfo &calendarInfo, + const QString &serverCalendarId, + int accountId, + Accounts::Manager *accountManager) +{ + notebook->setIsReadOnly(false); + notebook->setName(calendarInfo.summary); + notebook->setColor(calendarInfo.color); + notebook->setDescription(calendarInfo.description); + notebook->setPluginName(QStringLiteral("google")); + notebook->setCustomProperty(NOTEBOOK_SERVER_ID_PROPERTY, serverCalendarId); + if (calendarInfo.access == GoogleCalendarSyncAdaptor::Owner) { + notebook->setCustomProperty(NOTEBOOK_EMAIL_PROPERTY, ownerEmailAddress(accountManager, accountId)); + } + // extra calendars have their own email addresses. using this property to pass it forward. + notebook->setSharedWith(QStringList() << serverCalendarId); + notebook->setAccount(QString::number(accountId)); +} + void GoogleCalendarSyncAdaptor::applyRemoteChangesLocally(int accountId) { SOCIALD_LOG_DEBUG("applying all remote changes to local database"); @@ -2284,18 +2305,7 @@ void GoogleCalendarSyncAdaptor::applyRemoteChangesLocally(int accountId) case GoogleCalendarSyncAdaptor::Insert: { SOCIALD_LOG_DEBUG("Adding local notebook for new server calendar:" << serverCalendarId); mKCal::Notebook::Ptr notebook = mKCal::Notebook::Ptr(new mKCal::Notebook); - notebook->setIsReadOnly(false); - notebook->setName(calendarInfo.summary); - notebook->setColor(calendarInfo.color); - notebook->setDescription(calendarInfo.description); - notebook->setPluginName(QStringLiteral("google")); - notebook->setCustomProperty(NOTEBOOK_SERVER_ID_PROPERTY, serverCalendarId); - if (calendarInfo.access == GoogleCalendarSyncAdaptor::Owner) { - notebook->setCustomProperty(NOTEBOOK_EMAIL_PROPERTY, ownerEmailAddress(m_accountManager, accountId)); - } - // extra calendars have their own email addresses. using this property to pass it forward. - notebook->setSharedWith(QStringList() << serverCalendarId); - notebook->setAccount(QString::number(accountId)); + setCalendarProperties(notebook, calendarInfo, serverCalendarId, accountId, m_accountManager); m_storage->addNotebook(notebook); m_storageNeedsSave = true; } break; @@ -2308,17 +2318,7 @@ void GoogleCalendarSyncAdaptor::applyRemoteChangesLocally(int accountId) // apply other database modifications if possible, in order to leave // the local database in a usable state even after failed sync. } else { - notebook->setIsReadOnly(false); - notebook->setName(calendarInfo.summary); - notebook->setColor(calendarInfo.color); - notebook->setDescription(calendarInfo.description); - notebook->setPluginName(QStringLiteral("google")); - notebook->setCustomProperty(NOTEBOOK_SERVER_ID_PROPERTY, serverCalendarId); - if (calendarInfo.access == GoogleCalendarSyncAdaptor::Owner) { - notebook->setCustomProperty(NOTEBOOK_EMAIL_PROPERTY, ownerEmailAddress(m_accountManager, accountId)); - } - // TODO: might be able to remove this some day when all calendars are migrated to have (now 2019/06) - notebook->setSharedWith(QStringList() << serverCalendarId); + setCalendarProperties(notebook, calendarInfo, serverCalendarId, accountId, m_accountManager); m_storage->updateNotebook(notebook); m_storageNeedsSave = true; } @@ -2355,20 +2355,10 @@ void GoogleCalendarSyncAdaptor::applyRemoteChangesLocally(int accountId) // and then recreate. SOCIALD_LOG_DEBUG("recreating notebook:" << notebook->uid() << "due to clean sync"); notebook = mKCal::Notebook::Ptr(new mKCal::Notebook); - notebook->setIsReadOnly(false); if (!notebookUid.isEmpty()) { notebook->setUid(notebookUid); } - notebook->setName(calendarInfo.summary); - notebook->setColor(calendarInfo.color); - notebook->setDescription(calendarInfo.description); - notebook->setPluginName(QStringLiteral("google")); - notebook->setCustomProperty(NOTEBOOK_SERVER_ID_PROPERTY, serverCalendarId); - if (calendarInfo.access == GoogleCalendarSyncAdaptor::Owner) { - notebook->setCustomProperty(NOTEBOOK_EMAIL_PROPERTY, ownerEmailAddress(m_accountManager, accountId)); - } - notebook->setSharedWith(QStringList() << serverCalendarId); - notebook->setAccount(QString::number(accountId)); + setCalendarProperties(notebook, calendarInfo, serverCalendarId, accountId, m_accountManager); m_storage->addNotebook(notebook); m_storageNeedsSave = true; } break; diff --git a/src/google/google-calendars/googlecalendarsyncadaptor.h b/src/google/google-calendars/googlecalendarsyncadaptor.h index 25b329f..65bea18 100644 --- a/src/google/google-calendars/googlecalendarsyncadaptor.h +++ b/src/google/google-calendars/googlecalendarsyncadaptor.h @@ -80,6 +80,15 @@ class GoogleCalendarSyncAdaptor : public GoogleDataTypeSyncAdaptor QByteArray eventData; }; + struct CalendarInfo { + CalendarInfo() : change(NoChange), access(NoAccess) {} + QString summary; + QString description; + QString color; + ChangeType change; + AccessRole access; + }; + void requestCalendars(int accountId, const QString &accessToken, bool needCleanSync, const QString &pageToken = QString()); void requestEvents(int accountId, const QString &accessToken, @@ -102,20 +111,18 @@ class GoogleCalendarSyncAdaptor : public GoogleDataTypeSyncAdaptor const QString &nextSyncToken, const QDateTime &since, const QString &updateTimestampStr); + static void setCalendarProperties(mKCal::Notebook::Ptr notebook, + const CalendarInfo &calendarInfo, + const QString &serverCalendarId, + int accountId, + Accounts::Manager *accountManager); + private Q_SLOTS: void calendarsFinishedHandler(); void eventsFinishedHandler(); void upsyncFinishedHandler(); private: - struct CalendarInfo { - CalendarInfo() : change(NoChange), access(NoAccess) {} - QString summary; - QString description; - QString color; - ChangeType change; - AccessRole access; - }; QMap > m_serverCalendarIdToCalendarInfo; QMap > m_serverCalendarIdToDefaultReminderTimes; QMap > m_calendarIdToEventObjects;