Skip to content

Commit

Permalink
Merge pull request #11 from Karry/read-only-google-calendars
Browse files Browse the repository at this point in the history
fetch read-only google calendars
  • Loading branch information
pvuorela committed Mar 20, 2024
2 parents 385017d + 13a6b1b commit 17b85e9
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/google/google-calendars/googlecalendarsyncadaptor.cpp
Expand Up @@ -879,6 +879,10 @@ QString toBase32hex(QByteArray bytes)
return result;
}

QString percentEnc(const QString &str) {
return QString::fromUtf8(QUrl::toPercentEncoding(str));
}

QString generate_uuid()
{
// UUID documentation here:
Expand Down Expand Up @@ -1229,19 +1233,25 @@ void GoogleCalendarSyncAdaptor::calendarsFinishedHandler()
for (int i = 0; i < items.count(); ++i) {
QJsonObject currCalendar = items.at(i).toObject();
if (!currCalendar.isEmpty() && currCalendar.find(QStringLiteral("id")) != currCalendar.end()) {
// we only sync calendars which the user owns (ie, not autogenerated calendars)
QString accessRole = currCalendar.value(QStringLiteral("accessRole")).toString();
if (accessRole == QStringLiteral("owner") || accessRole == QStringLiteral("writer")) {
AccessRole access = NoAccess;
if (accessRole == QStringLiteral("owner")) {
access = Owner;
} else if (accessRole == QStringLiteral("writer")) {
access = Writer;
} else if (accessRole == QStringLiteral("reader")) {
access = Reader;
} else if (accessRole == QStringLiteral("freeBusyReader")) {
access = FreeBusyReader;
}

if (access != NoAccess) {
GoogleCalendarSyncAdaptor::CalendarInfo currCalendarInfo;
currCalendarInfo.color = currCalendar.value(QStringLiteral("backgroundColor")).toString();
currCalendarInfo.summary = currCalendar.value(QStringLiteral("summary")).toString();
currCalendarInfo.description = currCalendar.value(QStringLiteral("description")).toString();
currCalendarInfo.change = NoChange; // we detect the appropriate change type (if required) later.
if (accessRole == QStringLiteral("owner")) {
currCalendarInfo.access = Owner;
} else {
currCalendarInfo.access = Writer;
}
currCalendarInfo.access = access;
QString currCalendarId = currCalendar.value(QStringLiteral("id")).toString();
m_serverCalendarIdToCalendarInfo.insert(currCalendarId, currCalendarInfo);
}
Expand Down Expand Up @@ -1410,7 +1420,7 @@ void GoogleCalendarSyncAdaptor::requestEvents(const QString &accessToken, const
queryItems.append(QPair<QString, QString>(QString::fromLatin1("pageToken"), pageToken));
}

QUrl url(QString::fromLatin1("https://www.googleapis.com/calendar/v3/calendars/%1/events").arg(calendarId));
QUrl url(QString::fromLatin1("https://www.googleapis.com/calendar/v3/calendars/%1/events").arg(percentEnc(calendarId)));
QUrlQuery query(url);
query.setQueryItems(queryItems);
url.setQuery(query);
Expand Down Expand Up @@ -2143,8 +2153,8 @@ void GoogleCalendarSyncAdaptor::upsyncChanges(const UpsyncChange &changeToUpsync
const QByteArray &eventData = changeToUpsync.eventData;

QUrl requestUrl = upsyncType == GoogleCalendarSyncAdaptor::Insert
? QUrl(QString::fromLatin1("https://www.googleapis.com/calendar/v3/calendars/%1/events").arg(calendarId))
: QUrl(QString::fromLatin1("https://www.googleapis.com/calendar/v3/calendars/%1/events/%2").arg(calendarId).arg(eventId));
? QUrl(QString::fromLatin1("https://www.googleapis.com/calendar/v3/calendars/%1/events").arg(percentEnc(calendarId)))
: QUrl(QString::fromLatin1("https://www.googleapis.com/calendar/v3/calendars/%1/events/%2").arg(percentEnc(calendarId)).arg(eventId));

QNetworkRequest request(requestUrl);
request.setRawHeader("GData-Version", "3.0");
Expand Down Expand Up @@ -2442,7 +2452,7 @@ void GoogleCalendarSyncAdaptor::setCalendarProperties(
const QString &syncProfile,
const QString &ownerEmail)
{
notebook->setIsReadOnly(false);
notebook->setIsReadOnly(calendarInfo.access == GoogleCalendarSyncAdaptor::Reader || calendarInfo.access == GoogleCalendarSyncAdaptor::FreeBusyReader);
notebook->setName(calendarInfo.summary);
notebook->setDescription(calendarInfo.description);
notebook->setPluginName(QStringLiteral("google"));
Expand Down

0 comments on commit 17b85e9

Please sign in to comment.