File indexing completed on 2024-11-24 04:42:06

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "androidcalendarplugin.h"
0007 #include "androidcalendar.h"
0008 
0009 #include "android/calendardata.h"
0010 
0011 #include "kandroidextras/calendarcontract.h"
0012 #include <KAndroidExtras/JniObject>
0013 
0014 #include <QCoreApplication>
0015 #include <QDebug>
0016 
0017 using namespace KCalendarCore;
0018 using namespace KAndroidExtras;
0019 
0020 AndroidCalendarPlugin::AndroidCalendarPlugin(QObject *parent, const QVariantList &args)
0021     : CalendarPlugin(parent, args)
0022     , m_jni(Jni::fromHandle<android::content::Context>(QJniObject(QNativeInterface::QAndroidApplication::context())))
0023 {
0024 }
0025 
0026 AndroidCalendarPlugin::~AndroidCalendarPlugin() = default;
0027 
0028 QVector<KCalendarCore::Calendar::Ptr> AndroidCalendarPlugin::calendars() const
0029 {
0030     if (m_calendars.isEmpty()) {
0031         loadCalendars();
0032     }
0033 
0034     return m_calendars;
0035 }
0036 
0037 void AndroidCalendarPlugin::loadCalendars() const
0038 {
0039     const Jni::Array<JniCalendarData> cals = m_jni.getCalendars();
0040     for (const JniCalendarData &calData : cals) {
0041         auto *cal = new AndroidCalendar(QTimeZone(QString(calData.timezone).toUtf8()), calData.owner, calData.id);
0042         cal->setName(calData.displayName);
0043 
0044         const int accessLevel = calData.accessLevel;
0045         if (accessLevel == CalendarColumns::CAL_ACCESS_ROOT || accessLevel == CalendarColumns::CAL_ACCESS_OWNER
0046             || accessLevel == CalendarColumns::CAL_ACCESS_EDITOR || accessLevel == CalendarColumns::CAL_ACCESS_CONTRIBUTOR) {
0047             cal->setAccessMode(KCalendarCore::ReadWrite);
0048         } else {
0049             cal->setAccessMode(KCalendarCore::ReadOnly);
0050         }
0051 
0052         // TODO calendar color
0053 
0054         m_calendars.push_back(KCalendarCore::Calendar::Ptr(cal));
0055     }
0056 }
0057 
0058 #include "moc_androidcalendarplugin.cpp"