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

Rename LodingReservation::check[in|out]Date

schema.org calls this check[in|out]Time, the old form is Google's still
widely used variant.
parent ce9a8675
......@@ -93,6 +93,27 @@ private Q_SLOTS:
p2.setName(QLatin1String("Volker Krause"));
QVERIFY(MergeUtil::isSamePerson(p1, p2));
}
void testIsSameLodingReservation()
{
LodgingReservation res1;
LodgingBusiness hotel1;
hotel1.setName(QLatin1String("Haus Randa"));
res1.setReservationFor(QVariant::fromValue(hotel1));
res1.setCheckinTime(QDateTime(QDate(2018, 4, 9), QTime(10, 0)));
res1.setReservationNumber(QLatin1String("1234"));
LodgingReservation res2;
QVERIFY(!MergeUtil::isSameReservation(QVariant::fromValue(res1), QVariant::fromValue(res2)));
res2.setReservationNumber(QLatin1String("1234"));
QVERIFY(!MergeUtil::isSameReservation(QVariant::fromValue(res1), QVariant::fromValue(res2)));
res2.setCheckinTime(QDateTime(QDate(2018, 4, 9), QTime(15, 0)));
QVERIFY(!MergeUtil::isSameReservation(QVariant::fromValue(res1), QVariant::fromValue(res2)));
LodgingBusiness hotel2;
hotel2.setName(QLatin1String("Haus Randa"));
res2.setReservationFor(QVariant::fromValue(hotel2));
QVERIFY(MergeUtil::isSameReservation(QVariant::fromValue(res1), QVariant::fromValue(res2)));
}
};
QTEST_APPLESS_MAIN(MergeUtilTest)
......
......@@ -39,7 +39,7 @@ QDateTime CalendarHandler::startDateTime(const QVariant &reservation)
const auto trip = JsonLdDocument::readProperty(reservation, "reservationFor");
return JsonLdDocument::readProperty(trip, "departureTime").toDateTime();
} else if (reservation.userType() == qMetaTypeId<LodgingReservation>()) {
return JsonLdDocument::readProperty(reservation, "checkinDate").toDateTime();
return reservation.value<LodgingReservation>().checkinTime();
}
return {};
}
......@@ -71,7 +71,7 @@ void CalendarHandler::fillEvent(const QVariant &reservation, const KCalCore::Eve
if (typeId == qMetaTypeId<FlightReservation>()) {
fillFlightReservation(reservation, event);
} else if (typeId == qMetaTypeId<LodgingReservation>()) {
fillLodgingReservation(reservation, event);
fillLodgingReservation(reservation.value<LodgingReservation>(), event);
} else if (typeId == qMetaTypeId<TrainReservation>()) {
fillTrainReservation(reservation, event);
} else if (typeId == qMetaTypeId<BusReservation>()) {
......@@ -217,35 +217,27 @@ void CalendarHandler::fillBusReservation(const QVariant &reservation, const KCal
fillTripReservation(reservation, event);
}
void CalendarHandler::fillLodgingReservation(const QVariant &reservation, const KCalCore::Event::Ptr &event)
void CalendarHandler::fillLodgingReservation(const LodgingReservation &reservation, const KCalCore::Event::Ptr &event)
{
const auto lodgingBusiness = JsonLdDocument::readProperty(reservation, "reservationFor");
const auto address = JsonLdDocument::readProperty(lodgingBusiness, "address");
if (lodgingBusiness.isNull() || address.isNull()) {
if (reservation.reservationFor().isNull()) {
return;
}
const auto lodgingBusiness = reservation.reservationFor().value<LodgingBusiness>();
const auto address = lodgingBusiness.address();
event->setSummary(i18n("Hotel reservation: %1",
JsonLdDocument::readProperty(lodgingBusiness, "name").toString()
));
event->setSummary(i18n("Hotel reservation: %1", lodgingBusiness.name()));
event->setLocation(i18nc("<street>, <postal code> <city>, <country>", "%1, %2 %3, %4",
JsonLdDocument::readProperty(address, "streetAddress").toString(),
JsonLdDocument::readProperty(address, "postalCode").toString(),
JsonLdDocument::readProperty(address, "addressLocality").toString(),
JsonLdDocument::readProperty(address, "addressCountry").toString()
));
fillGeoPosition(lodgingBusiness, event);
address.streetAddress(), address.postalCode(),
address.addressLocality(), address.addressCountry()));
fillGeoPosition(QVariant::fromValue(lodgingBusiness), event);
const auto checkinDt = JsonLdDocument::readProperty(reservation, "checkinDate").toDateTime();
const auto checkoutDt = JsonLdDocument::readProperty(reservation, "checkoutDate").toDateTime();
event->setDtStart(QDateTime(checkinDt.date(), QTime()));
event->setDtEnd(QDateTime(checkoutDt.date(), QTime()));
event->setDtStart(QDateTime(reservation.checkinTime().date(), QTime()));
event->setDtEnd(QDateTime(reservation.checkoutTime().date(), QTime()));
event->setAllDay(true);
event->setDescription(i18n("Check-in: %1\nCheck-out: %2\nBooking reference: %3",
QLocale().toString(checkinDt.time(), QLocale::ShortFormat),
QLocale().toString(checkoutDt.time(), QLocale::ShortFormat),
JsonLdDocument::readProperty(reservation, "reservationNumber").toString()
));
QLocale().toString(reservation.checkinTime().time(), QLocale::ShortFormat),
QLocale().toString(reservation.checkoutTime().time(), QLocale::ShortFormat),
reservation.reservationNumber()));
event->setTransparency(Event::Transparent);
}
......
......@@ -29,6 +29,8 @@ class QVariant;
namespace KItinerary {
class LodgingReservation;
/** Methods for converting between ical events and JSON-LD booking data. */
class KITINERARY_EXPORT CalendarHandler
{
......@@ -49,7 +51,7 @@ private:
static void fillTripReservation(const QVariant &reservation, const KCalCore::Event::Ptr &event);
static void fillTrainReservation(const QVariant &reservation, const KCalCore::Event::Ptr &event);
static void fillBusReservation(const QVariant &reservation, const KCalCore::Event::Ptr &event);
static void fillLodgingReservation(const QVariant &reservation, const KCalCore::Event::Ptr &event);
static void fillLodgingReservation(const LodgingReservation &reservation, const KCalCore::Event::Ptr &event);
static void fillGeoPosition(const QVariant &place, const KCalCore::Event::Ptr &event);
};
......
......@@ -58,23 +58,23 @@ class LodgingReservationPrivate : public ReservationPrivate
{
KITINERARY_PRIVATE_GADGET(LodgingReservation)
public:
QDateTime checkinDate;
QDateTime checkoutDate;
QDateTime checkinTime;
QDateTime checkoutTime;
};
KITINERARY_MAKE_SUB_CLASS(LodgingReservation, Reservation)
KITINERARY_MAKE_PROPERTY(LodgingReservation, QDateTime, checkinDate, setCheckinDate)
KITINERARY_MAKE_PROPERTY(LodgingReservation, QDateTime, checkoutDate, setCheckoutDate)
KITINERARY_MAKE_PROPERTY(LodgingReservation, QDateTime, checkinTime, setCheckinTime)
KITINERARY_MAKE_PROPERTY(LodgingReservation, QDateTime, checkoutTime, setCheckoutTime)
QString LodgingReservation::checkinDateLocalized() const
{
K_D(const LodgingReservation);
return QLocale().toString(d->checkinDate.date(), QLocale::ShortFormat);
return QLocale().toString(d->checkinTime.date(), QLocale::ShortFormat);
}
QString LodgingReservation::checkoutDateLocalized() const
{
K_D(const LodgingReservation);
return QLocale().toString(d->checkoutDate.date(), QLocale::ShortFormat);
return QLocale().toString(d->checkoutTime.date(), QLocale::ShortFormat);
}
......
......@@ -63,12 +63,13 @@ protected:
/** A hotel reservation.
* @see https://schema.org/LodgingReservation
* @see https://developers.google.com/gmail/markup/reference/hotel-reservation
*/
class KITINERARY_EXPORT LodgingReservation : public Reservation
{
KITINERARY_GADGET(LodgingReservation)
KITINERARY_PROPERTY(QDateTime, checkinDate, setCheckinDate)
KITINERARY_PROPERTY(QDateTime, checkoutDate, setCheckoutDate)
KITINERARY_PROPERTY(QDateTime, checkinTime, setCheckinTime)
KITINERARY_PROPERTY(QDateTime, checkoutTime, setCheckoutTime)
Q_PROPERTY(QString checkinDateLocalized READ checkinDateLocalized STORED false CONSTANT)
Q_PROPERTY(QString checkoutDateLocalized READ checkoutDateLocalized STORED false CONSTANT)
......
......@@ -50,7 +50,7 @@ public:
QVariant processReservation(QVariant res) const;
bool filterReservation(const QVariant &res) const;
bool filterLodgingReservation(const QVariant &res) const;
bool filterLodgingReservation(const LodgingReservation &res) const;
bool filterFlight(const Flight &flight) const;
bool filterAirport(const Airport &airport) const;
bool filterTrainOrBusTrip(const QVariant &trip) const;
......@@ -246,16 +246,14 @@ bool ExtractorPostprocessorPrivate::filterReservation(const QVariant &res) const
}
if (res.userType() == qMetaTypeId<LodgingReservation>()) {
return filterLodgingReservation(res);
return filterLodgingReservation(res.value<LodgingReservation>());
}
return true;
}
bool ExtractorPostprocessorPrivate::filterLodgingReservation(const QVariant &res) const
bool ExtractorPostprocessorPrivate::filterLodgingReservation(const LodgingReservation &res) const
{
const auto checkinDate = JsonLdDocument::readProperty(res, "checkinDate").toDateTime();
const auto checkoutDate = JsonLdDocument::readProperty(res, "checkoutDate").toDateTime();
return checkinDate.isValid() && checkoutDate.isValid();
return res.checkinTime().isValid() && res.checkoutTime().isValid();
}
bool ExtractorPostprocessorPrivate::filterFlight(const Flight &flight) const
......
......@@ -17,10 +17,20 @@
#include "jsonldimportfilter.h"
#include <QDebug>
#include <QJsonObject>
using namespace KItinerary;
static void renameProperty(QJsonObject &obj, const char *oldName, const char *newName)
{
const auto value = obj.value(QLatin1String(oldName));
if (!value.isNull() && !obj.contains(QLatin1String(newName))) {
obj.insert(QLatin1String(newName), value);
obj.remove(QLatin1String(oldName));
}
}
static void filterTrainTrip(QJsonObject &trip)
{
if (trip.value(QLatin1String("@type")).toString() != QLatin1String("TrainTrip")) {
......@@ -28,10 +38,14 @@ static void filterTrainTrip(QJsonObject &trip)
}
// move TrainTrip::trainCompany to TrainTrip::provider (as defined by schema.org)
const auto company = trip.value(QLatin1String("trainCompany")).toObject();
if (!company.isEmpty() && !trip.contains(QLatin1String("provider"))) {
trip.insert(QLatin1String("provider"), company);
}
renameProperty(trip, "trainCompany", "provider");
}
static void filterLodgingReservation(QJsonObject &res)
{
// check[in|out]Date -> check[in|out]Time (legacy Google format)
renameProperty(res, "checkinDate", "checkinTime");
renameProperty(res, "checkoutDate", "checkoutTime");
}
static void filterReservation(QJsonObject &res)
......@@ -64,6 +78,8 @@ QJsonObject JsonLdImportFilter::filterObject(const QJsonObject& obj)
if (!train.isEmpty()) {
res.insert(QLatin1String("reservationFor"), train);
}
} else if (type == QLatin1String("LodgingReservation")) {
filterLodgingReservation(res);
}
return res;
......
......@@ -27,6 +27,7 @@ namespace KItinerary {
*/
namespace JsonLdImportFilter
{
/** Filter the top-level object @p obj for loading with JsonLdDocument. */
QJsonObject filterObject(const QJsonObject &obj);
}
......
......@@ -95,7 +95,7 @@ bool MergeUtil::isSameReservation(const QVariant& lhs, const QVariant& rhs)
}
const auto lhsHotel = lhsRes.reservationFor().value<LodgingBusiness>();
const auto rhsHotel = rhsRes.reservationFor().value<LodgingBusiness>();
if (!isSameLodingBusiness(lhsHotel, rhsHotel) || lhsRes.checkinDate() != rhsRes.checkinDate()) {
if (!isSameLodingBusiness(lhsHotel, rhsHotel) || lhsRes.checkinTime().date() != rhsRes.checkinTime().date()) {
return false;
}
}
......
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