Commit bfd5394b authored by chriadam's avatar chriadam

[buteo-sync-plugins-social] Store the owner email address as a custom property...

[buteo-sync-plugins-social] Store the owner email address as a custom property of the notebook. Contributes to JB#46575

For consistency with other plugins, store the owner email address
as a custom property of the notebook, for Google Calendars.

Also, make use of the custom property facility to store the
server-side calendar id, rather than abusing the plugin() field.
parent a90e5c55
......@@ -59,6 +59,8 @@
namespace {
static int GOOGLE_CAL_SYNC_PLUGIN_VERSION = 2;
static const QByteArray NOTEBOOK_EMAIL_PROPERTY = QByteArrayLiteral("userPrincipalEmail");
static const QByteArray NOTEBOOK_SERVER_ID_PROPERTY = QByteArrayLiteral("calendarServerId");
void errorDumpStr(const QString &str)
{
......@@ -990,6 +992,21 @@ QString syncTokenForCalendar(Accounts::Manager *manager, int accountId, const QS
return syncToken;
}
QString ownerEmailAddress(Accounts::Manager *manager, int accountId)
{
QString emailAddress;
Accounts::Account *account = Accounts::Account::fromId(manager, accountId, Q_NULLPTR);
if (!account) {
SOCIALD_LOG_ERROR("unable to load Google account" << accountId << "to retrieve calendar sync tokens");
} else {
Accounts::Service srv(manager->service(QStringLiteral("google-gmail")));
account->selectService(srv);
emailAddress = account->valueAsString(QStringLiteral("emailaddress"));
account->deleteLater();
}
return emailAddress;
}
}
GoogleCalendarSyncAdaptor::GoogleCalendarSyncAdaptor(QObject *parent)
......@@ -1151,7 +1168,7 @@ void GoogleCalendarSyncAdaptor::purgeDataForOldAccount(int oldId, SocialNetworkS
// We clean all the entries in the calendar
// Delete the notebooks from the storage
foreach (mKCal::Notebook::Ptr notebook, m_storage->notebooks()) {
if (notebook->pluginName().startsWith(QString(QLatin1String("google-")))
if (notebook->pluginName().startsWith(QString(QLatin1String("google")))
&& notebook->account() == QString::number(oldId)) {
// remove the incidences and delete the notebook
notebook->setIsReadOnly(false);
......@@ -1305,12 +1322,12 @@ void GoogleCalendarSyncAdaptor::updateLocalCalendarNotebooks(int accountId, cons
QStringList calendarsToDelete;
QStringList deviceCalendarIds;
foreach (mKCal::Notebook::Ptr notebook, m_storage->notebooks()) {
// notebook pluginName is of form: google-calendarId
// where the calendarId comes from the server.
if (notebook->pluginName().startsWith(QStringLiteral("google-"))
if (notebook->pluginName().startsWith(QStringLiteral("google"))
&& notebook->account() == QString::number(accountId)) {
QString currDeviceCalendarId = notebook->pluginName().mid(7);
// back compat: notebook pluginName was of form: google-calendarId
const QString currDeviceCalendarId = notebook->pluginName().startsWith(QStringLiteral("google-"))
? notebook->pluginName().mid(7)
: notebook->customProperty(NOTEBOOK_SERVER_ID_PROPERTY);
if (calendars.contains(currDeviceCalendarId)) {
// the server-side calendar exists on the device.
if (needCleanSync) {
......@@ -1548,8 +1565,10 @@ void GoogleCalendarSyncAdaptor::eventsFinishedHandler()
mKCal::Notebook::Ptr GoogleCalendarSyncAdaptor::notebookForCalendarId(int accountId, const QString &calendarId) const
{
foreach (mKCal::Notebook::Ptr notebook, m_storage->notebooks()) {
if (notebook->pluginName() == QString::fromLatin1("google-%1").arg(calendarId)
&& notebook->account() == QString::number(accountId)) {
if (notebook->account() == QString::number(accountId)
&& (notebook->customProperty(NOTEBOOK_SERVER_ID_PROPERTY) == calendarId
// for backward compatibility with old accounts / notebooks:
|| notebook->pluginName() == QString::fromLatin1("google-%1").arg(calendarId))) {
return notebook;
}
}
......@@ -2184,7 +2203,11 @@ void GoogleCalendarSyncAdaptor::applyRemoteChangesLocally(int accountId)
notebook->setName(calendarInfo.summary);
notebook->setColor(calendarInfo.color);
notebook->setDescription(calendarInfo.description);
notebook->setPluginName(QStringLiteral("google-") + serverCalendarId);
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));
......@@ -2204,6 +2227,11 @@ void GoogleCalendarSyncAdaptor::applyRemoteChangesLocally(int accountId)
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);
m_storage->updateNotebook(notebook);
......@@ -2249,7 +2277,11 @@ void GoogleCalendarSyncAdaptor::applyRemoteChangesLocally(int accountId)
notebook->setName(calendarInfo.summary);
notebook->setColor(calendarInfo.color);
notebook->setDescription(calendarInfo.description);
notebook->setPluginName(QStringLiteral("google-") + serverCalendarId);
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));
m_storage->addNotebook(notebook);
......
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