Commit f1e19681 authored by Volker Krause's avatar Volker Krause
Browse files

Add additional event properties if we have a preceding transfer

Transfers are something the KItinerary code doesn't know about. This adds
a reminder at the time of departure, and the X-APPLE-TRAVEL-DURATION
property which calendar apps can use for marking travel time.
parent 060d78c4
......@@ -34,6 +34,7 @@ ecm_add_test(livedatamanagertest.cpp LINK_LIBRARIES Qt::Test itinerary)
ecm_add_test(healthcertificatemanagertest.cpp LINK_LIBRARIES Qt::Test itinerary)
ecm_add_test(passmanagertest.cpp LINK_LIBRARIES Qt::Test itinerary)
ecm_add_test(calendarimportmodeltest.cpp LINK_LIBRARIES Qt::Test itinerary)
ecm_add_test(calendarhelpertest.cpp LINK_LIBRARIES Qt::Test itinerary)
ecm_add_test(weathertest.cpp LINK_LIBRARIES Qt::Test itinerary-weather)
target_include_directories(weathertest PRIVATE ${CMAKE_BINARY_DIR})
/*
SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <calendarhelper.h>
#include <transfer.h>
#include <KPublicTransport/Journey>
#include <KCalendarCore/ICalFormat>
#include <QtTest/qtest.h>
#include <QStandardPaths>
class CalendarHelperTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
qputenv("TZ", "UTC");
QStandardPaths::setTestModeEnabled(true);
}
void testFillPreTransfer()
{
KCalendarCore::Event::Ptr event(new KCalendarCore::Event);
event->setDtStart(QDateTime({2017, 9, 10}, {6, 30}, QTimeZone("Europe/Berlin")));
Transfer transfer;
transfer.setAlignment(Transfer::Before);
transfer.setState(Transfer::Selected);
transfer.setAnchorTimeDelta(60 * 60);
transfer.setAnchorTime(QDateTime({2017, 9, 10}, {6, 30}, QTimeZone("Europe/Berlin")));
transfer.setToName(QStringLiteral("Berlin Airport"));
KPublicTransport::Journey jny;
KPublicTransport::JourneySection section;
section.setScheduledDepartureTime(QDateTime({2017, 9, 10}, {5, 0}, QTimeZone("Europe/Berlin")));
section.setScheduledArrivalTime(QDateTime({2017, 9, 10}, {5, 30}, QTimeZone("Europe/Berlin")));
jny.setSections({section});
transfer.setJourney(jny);
CalendarHelper::fillPreTransfer(event, transfer);
KCalendarCore::ICalFormat format;
qDebug().noquote() << format.toString(event);
QCOMPARE(event->nonKDECustomProperty("X-APPLE-TRAVEL-DURATION"), QLatin1String("PT1H30M"));
QCOMPARE(event->alarms().size(), 1);
const auto alarm = event->alarms().at(0);
QCOMPARE(alarm->startOffset().asSeconds(), -5700);
}
};
QTEST_GUILESS_MAIN(CalendarHelperTest)
#include "calendarhelpertest.moc"
......@@ -7,6 +7,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/itinerary_version_detailed.h.cmake ${
add_library(itinerary STATIC)
target_sources(itinerary PRIVATE
applicationcontroller.cpp
calendarhelper.cpp
calendarimportmodel.cpp
documentmanager.cpp
favoritelocationmodel.cpp
......
/*
SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "calendarhelper.h"
#include "transfer.h"
#include <KCalendarCore/Alarm>
#include <KCalendarCore/ICalFormat>
#include <KLocalizedString>
#include <QDebug>
void CalendarHelper::fillPreTransfer(const KCalendarCore::Event::Ptr &event, const Transfer &transfer)
{
if (transfer.state() != Transfer::Selected) {
return;
}
// add a reminder 5min before the transfer departure
const auto depDt = transfer.journey().hasExpectedDepartureTime() ? transfer.journey().expectedDepartureTime() : transfer.journey().scheduledDepartureTime();
if (event->alarms().empty()) {
KCalendarCore::Alarm::Ptr alarm(new KCalendarCore::Alarm(event.data()));
KCalendarCore::Duration alarmDur(-depDt.secsTo(event->dtStart()) - 5 * 60);
alarm->setStartOffset(alarmDur);
alarm->setDisplayAlarm(i18n("Transfer to %1", transfer.toName()));
alarm->setEnabled(true);
event->addAlarm(alarm);
}
// add Apple travel duration property
KCalendarCore::Duration travelDur(depDt.secsTo(event->dtStart()));
KCalendarCore::ICalFormat format;
event->setNonKDECustomProperty("X-APPLE-TRAVEL-DURATION", format.toString(travelDur), QStringLiteral("VALUE=DURATION"));
// TODO add X-APPLE-TRAVEL-START property
}
/*
SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef CALENDARHELPER_H
#define CALENDARHELPER_H
#include <KCalendarCore/Event>
class Transfer;
/** Helper functions for dealing with calendar data. */
namespace CalendarHelper
{
/** Extension of KItinerary::CalendarHandler::fillEvent for pre-Transfers. */
void fillPreTransfer(const KCalendarCore::Event::Ptr &event, const Transfer &transfer);
}
#endif // CALENDARHELPER_H
......@@ -6,6 +6,7 @@
#include "timelinedelegatecontroller.h"
#include "calendarhelper.h"
#include "constants.h"
#include "livedatamanager.h"
#include "locationhelper.h"
......@@ -761,14 +762,20 @@ void TimelineDelegateController::addToCalendar(KCalendarCore::Calendar *cal)
}
const auto existingEvents = CalendarHandler::findEvents(cal, reservations.at(0));
KCalendarCore::Event::Ptr event;
if (existingEvents.size() == 1) {
auto event = existingEvents.at(0);
event = existingEvents.at(0);
event->startUpdates();
CalendarHandler::fillEvent(reservations, event);
} else {
event = KCalendarCore::Event::Ptr(new KCalendarCore::Event);
}
CalendarHandler::fillEvent(reservations, event);
CalendarHelper::fillPreTransfer(event, m_transferMgr->transfer(m_batchId, Transfer::Before));
if (existingEvents.size() == 1) {
event->endUpdates();
} else {
KCalendarCore::Event::Ptr event(new KCalendarCore::Event);
CalendarHandler::fillEvent(reservations, event);
cal->addEvent(event);
}
}
......
Supports Markdown
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