File indexing completed on 2024-05-12 05:10:40
0001 /* 0002 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org> 0003 SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #include "akonadicalendarplugin.h" 0007 #include "akonadicalendarplugin_debug.h" 0008 #include "singlecollectioncalendar.h" 0009 0010 #include <Akonadi/CollectionFetchJob> 0011 #include <Akonadi/CollectionFetchScope> 0012 #include <Akonadi/Monitor> 0013 #include <Akonadi/ServerManager> 0014 0015 static bool filterCollection(const Akonadi::Collection &col) 0016 { 0017 return !col.isVirtual(); 0018 } 0019 0020 AkonadiCalendarPlugin::AkonadiCalendarPlugin(QObject *parent, const QVariantList &args) 0021 : KCalendarCore::CalendarPlugin(parent, args) 0022 { 0023 // don't automatically start Akonadi if that's explicitly forbidden 0024 // (useful in e.g. the CI environment) 0025 if (qEnvironmentVariableIsSet("AKONADI_CALENDAR_KCALENDARCORE_PLUGIN_NO_AUTO_LAUNCH") && !Akonadi::ServerManager::isRunning()) { 0026 qCWarning(AKONADICALENDARPLUGIN_LOG) << "Akonadi is not running, but auto-launch is disabled!"; 0027 return; 0028 } 0029 0030 auto job = new Akonadi::CollectionFetchJob(Akonadi::Collection::root(), Akonadi::CollectionFetchJob::Recursive, this); 0031 job->fetchScope().setContentMimeTypes(KCalendarCore::Incidence::mimeTypes()); 0032 connect(job, &Akonadi::CollectionFetchJob::finished, this, [this, job]() { 0033 const auto results = job->collections(); 0034 for (const auto &col : results) { 0035 if (!filterCollection(col)) { 0036 continue; 0037 } 0038 KCalendarCore::Calendar::Ptr cal(new SingleCollectionCalendar(col)); 0039 m_calendars.push_back(cal); 0040 } 0041 }); 0042 0043 auto monitor = new Akonadi::Monitor(this); 0044 monitor->setCollectionFetchScope(job->fetchScope()); 0045 connect(monitor, &Akonadi::Monitor::collectionAdded, this, [this](const Akonadi::Collection &c) { 0046 if (!filterCollection(c)) { 0047 return; 0048 } 0049 m_calendars.push_back(KCalendarCore::Calendar::Ptr(new SingleCollectionCalendar(c))); 0050 Q_EMIT calendarsChanged(); 0051 }); 0052 connect(monitor, &Akonadi::Monitor::collectionRemoved, this, [this](const Akonadi::Collection &c) { 0053 m_calendars.erase(std::remove_if(m_calendars.begin(), 0054 m_calendars.end(), 0055 [c](const KCalendarCore::Calendar::Ptr &cal) { 0056 return cal.staticCast<SingleCollectionCalendar>()->collection().id() == c.id(); 0057 }), 0058 m_calendars.end()); 0059 Q_EMIT calendarsChanged(); 0060 }); 0061 connect(monitor, qOverload<const Akonadi::Collection &>(&Akonadi::Monitor::collectionChanged), this, [this](const Akonadi::Collection &col) { 0062 for (const auto &c : m_calendars) { 0063 const auto cal = c.staticCast<SingleCollectionCalendar>(); 0064 if (cal->collection().id() == col.id()) { 0065 cal->setCollection(col); 0066 Q_EMIT calendarsChanged(); 0067 return; 0068 } 0069 } 0070 }); 0071 } 0072 0073 AkonadiCalendarPlugin::~AkonadiCalendarPlugin() = default; 0074 0075 QList<KCalendarCore::Calendar::Ptr> AkonadiCalendarPlugin::calendars() const 0076 { 0077 return m_calendars; 0078 } 0079 0080 #include "moc_akonadicalendarplugin.cpp"