summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Vrátil <[email protected]>2016-05-29 21:20:03 +0200
committerDaniel Vrátil <[email protected]>2016-05-29 21:20:03 +0200
commit6e7f225bb14032c37bb8badd07ac0ddeb8eedd4c (patch)
tree14d0830f3abac9658a5fcc09ac45258d0b1de03d
parent291edc939afb96b3e8a4f2ebdaa0d4b815d8f7aa (diff)
Implement loading calendar color from KOrganizer
-rw-r--r--plugins/plasma/pimeventsplugin/CMakeLists.txt1
-rw-r--r--plugins/plasma/pimeventsplugin/akonadipimdatasource.cpp45
-rw-r--r--plugins/plasma/pimeventsplugin/akonadipimdatasource.h4
-rw-r--r--plugins/plasma/pimeventsplugin/autotests/CMakeLists.txt12
-rw-r--r--plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.cpp5
-rw-r--r--plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.h2
-rw-r--r--plugins/plasma/pimeventsplugin/eventdatavisitor.cpp4
-rw-r--r--plugins/plasma/pimeventsplugin/pimdatasource.h1
8 files changed, 69 insertions, 5 deletions
diff --git a/plugins/plasma/pimeventsplugin/CMakeLists.txt b/plugins/plasma/pimeventsplugin/CMakeLists.txt
index 0f42337..79efc17 100644
--- a/plugins/plasma/pimeventsplugin/CMakeLists.txt
+++ b/plugins/plasma/pimeventsplugin/CMakeLists.txt
@@ -22,6 +22,7 @@ target_link_libraries(pimevents
KF5::AkonadiCalendar
KF5::CalendarCore
KF5::CalendarEvents
+ KF5::EventViews # for reading KOrganizer calendar colors
)
install(TARGETS pimevents
diff --git a/plugins/plasma/pimeventsplugin/akonadipimdatasource.cpp b/plugins/plasma/pimeventsplugin/akonadipimdatasource.cpp
index 7117d8b..936ce2d 100644
--- a/plugins/plasma/pimeventsplugin/akonadipimdatasource.cpp
+++ b/plugins/plasma/pimeventsplugin/akonadipimdatasource.cpp
@@ -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;
diff --git a/plugins/plasma/pimeventsplugin/akonadipimdatasource.h b/plugins/plasma/pimeventsplugin/akonadipimdatasource.h
index 9724396..e1da64e 100644
--- a/plugins/plasma/pimeventsplugin/akonadipimdatasource.h
+++ b/plugins/plasma/pimeventsplugin/akonadipimdatasource.h
@@ -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;
};
diff --git a/plugins/plasma/pimeventsplugin/autotests/CMakeLists.txt b/plugins/plasma/pimeventsplugin/autotests/CMakeLists.txt
index ee0d0b5..44a6d77 100644
--- a/plugins/plasma/pimeventsplugin/autotests/CMakeLists.txt
+++ b/plugins/plasma/pimeventsplugin/autotests/CMakeLists.txt
@@ -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)
diff --git a/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.cpp b/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.cpp
index 1f51ed4..a64076d 100644
--- a/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.cpp
+++ b/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.cpp
@@ -46,3 +46,8 @@ KCalCore::Calendar *FakePimDataSource::calendar() const
{
return mCalendar;
}
+
+QString FakePimDataSource::calendarColorForIncidence(const KCalCore::Incidence::Ptr &) const
+{
+ return QString();
+}
diff --git a/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.h b/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.h
index 91564d2..5c1fbda 100644
--- a/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.h
+++ b/plugins/plasma/pimeventsplugin/autotests/fakepimdatasource.h
@@ -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;
diff --git a/plugins/plasma/pimeventsplugin/eventdatavisitor.cpp b/plugins/plasma/pimeventsplugin/eventdatavisitor.cpp
index f83577f..af45f97 100644
--- a/plugins/plasma/pimeventsplugin/eventdatavisitor.cpp
+++ b/plugins/plasma/pimeventsplugin/eventdatavisitor.cpp
@@ -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;
}
diff --git a/plugins/plasma/pimeventsplugin/pimdatasource.h b/plugins/plasma/pimeventsplugin/pimdatasource.h
index 21216e9..b60ddd4 100644
--- a/plugins/plasma/pimeventsplugin/pimdatasource.h
+++ b/plugins/plasma/pimeventsplugin/pimdatasource.h
@@ -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