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

Add CalendarHandler::findEvents, and deprecate its singular predecessor

When searching for events for a minimal cancellation element we have to
expect more than one result, e.g. when canceling a multi-leg trip. In that
case we need a way to return all of those.
parent 5179091d
......@@ -33,4 +33,36 @@ DTSTART;TZID=America/Los_Angeles:20270304T201500
DTEND;TZID=America/New_York:20270305T063000
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20200328T095155Z
X-KDE-KITINERARY-RESERVATION:[{"@context":"http://schema.org"\,"@type":
"FlightReservation"\,"modifiedTime":"2027-01-01T08:30:00-08:
00"\,"reservationFor":{"@type":"Flight"\,"airline":{"@type":
"Airline"\,"iataCode":"UA"\,"name":"United"}\,"departureAirport":{"@type":
"Airport"\,"address":{"@type":"PostalAddress"\,"addressCountry":
"US"}\,"geo":{"@type":"GeoCoordinates"\,"latitude":
40.63970184326172\,"longitude":-73.77890014648438}\,"iataCode":
"JFK"\,"name":"John F. Kennedy International Airport"}\,"departureTime":
{"@type":"QDateTime"\,"@value":"2027-03-05T06:30:00-05:00"\,"timezone":
"America/New_York"}\,"arrivalAirport":{"@type":"Airport"\,"address":
{"@type":"PostalAddress"\,"addressCountry":"US"}\,"geo":{"@type":
"GeoCoordinates"\,"latitude":37.618900299072266\,"longitude":
-122.375}\,"iataCode":"SFO"\,"name":"San Francisco
Airport"}\,"departureDay":"2027-03-04"\,"arrivalTime":{"@type":
"QDateTime"\,"@value":"2027-03-04T20:15:00-08:00"\,"timezone":
"America/Los_Angeles"}\,"flightNumber":"110"}\,"reservationNumber":
"RXJ34P"\,"reservationStatus":"http:
//schema.org/ReservationConfirmed"\,"underName":{"@type":"Person"\,"name":
"Eva Green"}}]
CREATED:20200328T095155Z
UID:KIT-74e55159-9032-4e97-a947-59a92a5af9cd
LAST-MODIFIED:20200328T095155Z
DESCRIPTION:Eva Green\nBooking reference: RXJ34P
SUMMARY:Flight UA 110 from JFK to SFO
LOCATION:JFK
DTSTART;TZID=America/Los_Angeles:20270305T201500
DTEND;TZID=America/New_York:20270306T063000
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
......@@ -143,8 +143,9 @@ private Q_SLOTS:
ICalFormat format;
format.load(refCal, icalFile);
const auto event = CalendarHandler::findEvent(refCal, postproc.result().at(0));
QVERIFY(event);
const auto events = CalendarHandler::findEvents(refCal, postproc.result().at(0));
QCOMPARE(events.size(), 1);
QVERIFY(events[0]);
}
void testFindEventForCancellation()
......@@ -159,8 +160,10 @@ private Q_SLOTS:
ICalFormat format;
format.load(refCal, QStringLiteral(SOURCE_DIR "/calendarhandlerdata/to-be-cancelled.ics"));
const auto event = CalendarHandler::findEvent(refCal, cancel);
QVERIFY(event);
const auto events = CalendarHandler::findEvents(refCal, cancel);
QCOMPARE(events.size(), 2);
QVERIFY(events[0]);
QVERIFY(events[1]);
}
};
......
......@@ -81,14 +81,14 @@ static void fillRentalCarReservation(const RentalCarReservation &reservation, co
static void fillTaxiReservation(const TaxiReservation &reservation, const KCalendarCore::Event::Ptr &event);
#endif
QSharedPointer<KCalendarCore::Event> CalendarHandler::findEvent(const QSharedPointer<KCalendarCore::Calendar> &calendar, const QVariant &reservation)
QVector<QSharedPointer<KCalendarCore::Event> > CalendarHandler::findEvents(const QSharedPointer<KCalendarCore::Calendar> &calendar, const QVariant &reservation)
{
#ifdef HAVE_KCAL
if (!(JsonLd::canConvert<Reservation>(reservation) || JsonLd::canConvert<KItinerary::Event>(reservation)) || !calendar) {
return {};
}
QVector<KCalendarCore::Event::Ptr> events;
QVector<KCalendarCore::Event::Ptr> events, results;
const auto dt = SortUtil::startDateTime(reservation).toTimeZone(calendar->timeZone()).date();
if (dt.isValid()) {
// we know the exact day to search at
......@@ -110,16 +110,29 @@ QSharedPointer<KCalendarCore::Event> CalendarHandler::findEvent(const QSharedPoi
const auto otherRes = CalendarHandler::reservationsForEvent(event);
for (const auto &other : otherRes) {
if (MergeUtil::isSame(other, reservation)) {
return event;
results.push_back(event);
}
}
}
return results;
#else
Q_UNUSED(calendar);
Q_UNUSED(reservation);
return {};
#endif
}
QSharedPointer<KCalendarCore::Event> CalendarHandler::findEvent(const QSharedPointer<KCalendarCore::Calendar> &calendar, const QVariant &reservation)
{
#ifdef HAVE_KCAL
const auto evs = findEvents(calendar, reservation);
return evs.empty() ? KCalendarCore::Event::Ptr() : evs.at(0);
#else
Q_UNUSED(calendar);
Q_UNUSED(reservation);
return {};
#endif
}
QVector<QVariant> CalendarHandler::reservationsForEvent(const QSharedPointer<KCalendarCore::Event> &event)
......
......@@ -38,8 +38,18 @@ namespace KItinerary {
/** Methods for converting between ical events and JSON-LD booking data. */
namespace CalendarHandler
{
/** Attempts to find an event in @p calendar for @p reservation. */
KITINERARY_EXPORT QSharedPointer<KCalendarCore::Event> findEvent(const QSharedPointer<KCalendarCore::Calendar> &calendar, const QVariant &reservation);
/** Attempts to find calendar events in @p calendar for @p reservation.
* For a complete reservation this should not return more than one element,
* for a minimal cancellation element however this can return multiple events
* (e.g. all trip segments covered by the same reservation number).
* @since 20.08
*/
KITINERARY_EXPORT QVector<QSharedPointer<KCalendarCore::Event>> findEvents(const QSharedPointer<KCalendarCore::Calendar> &calendar, const QVariant &reservation);
/** Attempts to find an event in @p calendar for @p reservation.
* @deprecated since 20.08 use findEvents instead.
*/
KITINERARY_DEPRECATED_EXPORT QSharedPointer<KCalendarCore::Event> findEvent(const QSharedPointer<KCalendarCore::Calendar> &calendar, const QVariant &reservation);
/** Returns the reservations for this event.
* In case of a mult-travler trip, the result contains more than one reservation.
......
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