Commit 6e7f225b authored by Daniel Vrátil's avatar Daniel Vrátil 🤖

Implement loading calendar color from KOrganizer

parent 291edc93
......@@ -22,6 +22,7 @@ target_link_libraries(pimevents
KF5::AkonadiCalendar
KF5::CalendarCore
KF5::CalendarEvents
KF5::EventViews # for reading KOrganizer calendar colors
)
install(TARGETS pimevents
......
......@@ -19,18 +19,26 @@
#include "akonadipimdatasource.h"
#include "settingschangenotifier.h"
#include "pimeventsplugin_debug.h"
#include <AkonadiCore/ChangeRecorder>
#include <AkonadiCore/ItemFetchScope>
#include <AkonadiCore/EntityDisplayAttribute>
#include <AkonadiCore/CollectionColorAttribute>
#include <AkonadiCore/AttributeFactory>
#include <Akonadi/Calendar/ETMCalendar>
#include <KSharedConfig>
#include <KConfigGroup>
#include <KCoreConfigSkeleton>
#include <EventViews/Prefs>
AkonadiPimDataSource::AkonadiPimDataSource(QObject *parent)
: QObject(parent)
{
Akonadi::AttributeFactory::registerAttribute<Akonadi::CollectionColorAttribute>();
connect(SettingsChangeNotifier::self(), &SettingsChangeNotifier::settingsChanged,
this, &AkonadiPimDataSource::onSettingsChanged);
......@@ -38,11 +46,18 @@ AkonadiPimDataSource::AkonadiPimDataSource(QObject *parent)
mMonitor->setChangeRecordingEnabled(false);
mMonitor->itemFetchScope().fetchFullPayload(true);
mMonitor->itemFetchScope().fetchAttribute<Akonadi::EntityDisplayAttribute>();
mMonitor->itemFetchScope().fetchAttribute<Akonadi::CollectionColorAttribute>();
onSettingsChanged();
mCalendar = new Akonadi::ETMCalendar(mMonitor, this);
// TOOD: Only retrieve PLD:HEAD once it's supported
mCalendar->setCollectionFilteringEnabled(false);
// Would be nice to have a proper API to read KOrganizer calendar colors...
KSharedConfig::Ptr config = KSharedConfig::openConfig(QStringLiteral("korganizerrc"));
KCoreConfigSkeleton *skel = new KCoreConfigSkeleton(config);
mEventViewsPrefs = EventViews::PrefsPtr(new EventViews::Prefs(skel));
mEventViewsPrefs->readConfig();
}
AkonadiPimDataSource::~AkonadiPimDataSource()
......@@ -59,6 +74,36 @@ KCalCore::Calendar *AkonadiPimDataSource::calendar() const
return mCalendar;
}
QString AkonadiPimDataSource::calendarColorForIncidence(const KCalCore::Incidence::Ptr &incidence) const
{
const auto &item = mCalendar->item(incidence);
if (!item.isValid()) {
return QString();
}
const auto &col = mCalendar->collection(item.parentCollection().id());
if (!col.isValid()) {
return QString();
}
auto it = mColorCache.find(col.id());
if (it == mColorCache.end()) {
if (col.hasAttribute<Akonadi::CollectionColorAttribute>()) {
const auto attr = col.attribute<Akonadi::CollectionColorAttribute>();
it = mColorCache.insert(col.id(), attr->color().name());
} else {
QColor color = mEventViewsPrefs->resourceColorKnown(QString::number(col.id()));
if (color.isValid()) {
it = mColorCache.insert(col.id(), color.name());
} else {
it = mColorCache.insert(col.id(), QString());
}
}
qCDebug(PIMEVENTSPLUGIN_LOG) << "GOT" << (*it) << "for" << col.displayName();
}
return (*it);
}
void AkonadiPimDataSource::onSettingsChanged()
{
QSet<Akonadi::Collection> currentCols;
......
......@@ -22,6 +22,7 @@
#include "pimdatasource.h"
#include <QObject>
#include <EventViews/Prefs>
namespace Akonadi {
class ChangeRecorder;
......@@ -39,6 +40,7 @@ public:
qint64 akonadiIdForIncidence(const KCalCore::Incidence::Ptr &incidence) const Q_DECL_OVERRIDE;
KCalCore::Calendar *calendar() const Q_DECL_OVERRIDE;
QString calendarColorForIncidence(const KCalCore::Incidence::Ptr &incidence) const Q_DECL_OVERRIDE;
private Q_SLOTS:
void onSettingsChanged();
......@@ -46,6 +48,8 @@ private Q_SLOTS:
private:
Akonadi::ChangeRecorder *mMonitor;
Akonadi::ETMCalendar *mCalendar;
EventViews::PrefsPtr mEventViewsPrefs;
mutable QHash<qint64, QString> mColorCache;
};
......
......@@ -10,9 +10,17 @@ macro(add_plasma_pimeventsplugin_test _source _additional)
add_test(${_name} ${_name})
ecm_mark_as_test(plasma-pimeventsplugin-${_name})
add_definitions(-DQT_TESTCASE_BUILDDIR=\"${CMAKE_CURRENT_SOURCE_DIR}\")
target_link_libraries(${_name} Qt5::Core Qt5::Test KF5::AkonadiCore KF5::AkonadiCalendar KF5::CalendarCore KF5::CalendarEvents)
target_link_libraries(${_name} Qt5::Core
Qt5::Test
KF5::AkonadiCore
KF5::AkonadiCalendar
KF5::CalendarCore
KF5::CalendarEvents
KF5::EventViews
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..
${CMAKE_CURRENT_BINARY_DIR}/..)
${CMAKE_CURRENT_BINARY_DIR}/..
)
endmacro()
add_plasma_pimeventsplugin_test(eventdatavisitortest.cpp ../eventdatavisitor.cpp)
......
......@@ -46,3 +46,8 @@ KCalCore::Calendar *FakePimDataSource::calendar() const
{
return mCalendar;
}
QString FakePimDataSource::calendarColorForIncidence(const KCalCore::Incidence::Ptr &) const
{
return QString();
}
......@@ -33,6 +33,8 @@ public:
KCalCore::Calendar *calendar() const Q_DECL_OVERRIDE;
QString calendarColorForIncidence(const KCalCore::Incidence::Ptr &incidence) const Q_DECL_OVERRIDE;
private:
QMap<KCalCore::Incidence::Ptr, qint64> mAkonadiIdMap;
KCalCore::Calendar *mCalendar;
......
......@@ -107,8 +107,6 @@ QVector<CalendarEvents::EventData> BaseEventDataVisitor::explodeIncidenceOccuren
CalendarEvents::EventData copy = ed;
const QDateTime dt = rec.dateTime();
copy.setStartDateTime(dt);
// TODO: Is there a better way to find when an instance ends without
// going through the expensive lookup in Incidence::instance(uid, recurrenceId)?
copy.setEndDateTime(dt.addSecs(duration));
copy.setUid(generateUid(incidence, rec));
results.push_back(copy);
......@@ -181,7 +179,7 @@ CalendarEvents::EventData EventDataVisitor::incidenceData(const KCalCore::Incide
data.setUid(generateUid(incidence));
data.setStartDateTime(incidence->dtStart().dateTime());
data.setEndDateTime(incidence->dateTime(KCalCore::Incidence::RoleEnd).dateTime());
// TODO: Set calendar color
data.setEventColor(mDataSource->calendarColorForIncidence(incidence));
return data;
}
......
......@@ -33,6 +33,7 @@ public:
virtual KCalCore::Calendar *calendar() const = 0;
virtual qint64 akonadiIdForIncidence(const KCalCore::Incidence::Ptr &incidence) const = 0;
virtual QString calendarColorForIncidence(const KCalCore::Incidence::Ptr &incidence) const = 0;
};
#endif
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