File indexing completed on 2025-01-05 04:49:48

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "akonadipimdatasource.h"
0009 #include "eventmodel.h"
0010 #include "pimeventsplugin_debug.h"
0011 #include "settingschangenotifier.h"
0012 
0013 #include <Akonadi/AttributeFactory>
0014 #include <Akonadi/Collection>
0015 #include <Akonadi/CollectionColorAttribute>
0016 
0017 #include <QSet>
0018 
0019 #include <KCoreConfigSkeleton>
0020 #include <KSharedConfig>
0021 
0022 using namespace std::placeholders;
0023 
0024 AkonadiPimDataSource::AkonadiPimDataSource(QObject *parent)
0025     : QObject(parent)
0026     , mCalendar(new EventModel(this))
0027 {
0028     Akonadi::AttributeFactory::registerAttribute<Akonadi::CollectionColorAttribute>();
0029 
0030     connect(SettingsChangeNotifier::self(), &SettingsChangeNotifier::settingsChanged, this, &AkonadiPimDataSource::onSettingsChanged);
0031 
0032     onSettingsChanged();
0033 
0034     // Would be nice to have a proper API to read KOrganizer calendar colors...
0035     const auto korganizerrc = KSharedConfig::openConfig(QStringLiteral("korganizerrc"));
0036     const auto skel = new KCoreConfigSkeleton(korganizerrc);
0037     mEventViewsPrefs = EventViews::PrefsPtr(new EventViews::Prefs(skel));
0038     mEventViewsPrefs->readConfig();
0039 }
0040 
0041 AkonadiPimDataSource::~AkonadiPimDataSource() = default;
0042 
0043 qint64 AkonadiPimDataSource::akonadiIdForIncidence(const KCalendarCore::Incidence::Ptr &incidence) const
0044 {
0045     return mCalendar->item(incidence).id();
0046 }
0047 
0048 KCalendarCore::Calendar *AkonadiPimDataSource::calendar() const
0049 {
0050     return mCalendar;
0051 }
0052 
0053 QString AkonadiPimDataSource::calendarColorForIncidence(const KCalendarCore::Incidence::Ptr &incidence) const
0054 {
0055     const auto &item = mCalendar->item(incidence);
0056     if (!item.isValid()) {
0057         return {};
0058     }
0059 
0060     const auto &col = mCalendar->collection(item.parentCollection().id());
0061     if (!col.isValid()) {
0062         return {};
0063     }
0064 
0065     auto it = mColorCache.find(col.id());
0066     if (it == mColorCache.end()) {
0067         if (col.hasAttribute<Akonadi::CollectionColorAttribute>()) {
0068             const auto attr = col.attribute<Akonadi::CollectionColorAttribute>();
0069             it = mColorCache.insert(col.id(), attr->color().name());
0070         } else {
0071             QColor color = mEventViewsPrefs->resourceColorKnown(QString::number(col.id()));
0072             if (color.isValid()) {
0073                 it = mColorCache.insert(col.id(), color.name());
0074             } else {
0075                 it = mColorCache.insert(col.id(), QString());
0076             }
0077         }
0078     }
0079     return *it;
0080 }
0081 
0082 void AkonadiPimDataSource::onSettingsChanged()
0083 {
0084     QSet<Akonadi::Collection> monitored;
0085     const auto collections = mCalendar->collections();
0086     monitored.reserve(collections.count());
0087     for (const Akonadi::Collection &col : collections) {
0088         monitored.insert(col);
0089     }
0090 
0091     const auto config = KSharedConfig::openConfig();
0092     const auto group = config->group(QStringLiteral("PIMEventsPlugin"));
0093     const QList<qint64> calendars = group.readEntry(QStringLiteral("calendars"), QList<qint64>());
0094     QSet<Akonadi::Collection> configured;
0095     for (const auto colId : calendars) {
0096         configured.insert(Akonadi::Collection(colId));
0097     }
0098 
0099     const auto toEnable = configured - monitored;
0100     std::for_each(toEnable.cbegin(), toEnable.cend(), std::bind(&EventModel::addCalendar, mCalendar, _1));
0101     const auto toDisable = monitored - configured;
0102     std::for_each(toDisable.cbegin(), toDisable.cend(), std::bind(&EventModel::removeCalendar, mCalendar, _1));
0103 }
0104 
0105 #include "moc_akonadipimdatasource.cpp"