diff --git a/src/datatypes/action.cpp b/src/datatypes/action.cpp index 700c6e1..4f6381a 100644 --- a/src/datatypes/action.cpp +++ b/src/datatypes/action.cpp @@ -1,81 +1,88 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "action.h" #include "datatypes_p.h" #include using namespace KItinerary; namespace KItinerary { class ActionPrivate : public QSharedData { KITINERARY_PRIVATE_BASE_GADGET(Action) public: QUrl target; }; KITINERARY_MAKE_BASE_CLASS(Action) KITINERARY_MAKE_PROPERTY(Action, QUrl, target, setTarget) KITINERARY_MAKE_OPERATOR(Action) class CancelActionPrivate : public ActionPrivate { KITINERARY_PRIVATE_GADGET(CancelAction) }; KITINERARY_MAKE_SUB_CLASS(CancelAction, Action) KITINERARY_MAKE_OPERATOR(CancelAction) class CheckInActionPrivate : public ActionPrivate { KITINERARY_PRIVATE_GADGET(CheckInAction) }; KITINERARY_MAKE_SUB_CLASS(CheckInAction, Action) KITINERARY_MAKE_OPERATOR(CheckInAction) class DownloadActionPrivate : public ActionPrivate { KITINERARY_PRIVATE_GADGET(DownloadAction) }; KITINERARY_MAKE_SUB_CLASS(DownloadAction, Action) KITINERARY_MAKE_OPERATOR(DownloadAction) +class ReserveActionPrivate : public ActionPrivate +{ + KITINERARY_PRIVATE_GADGET(ReserveAction) +}; +KITINERARY_MAKE_SUB_CLASS(ReserveAction, Action) +KITINERARY_MAKE_OPERATOR(ReserveAction) + class UpdateActionPrivate : public ActionPrivate { KITINERARY_PRIVATE_GADGET(UpdateAction) }; KITINERARY_MAKE_SUB_CLASS(UpdateAction, Action) KITINERARY_MAKE_OPERATOR(UpdateAction) class ViewActionPrivate : public ActionPrivate { KITINERARY_PRIVATE_GADGET(ViewAction) }; KITINERARY_MAKE_SUB_CLASS(ViewAction, Action) KITINERARY_MAKE_OPERATOR(ViewAction) } template <> KItinerary::ActionPrivate *QExplicitlySharedDataPointer::clone() { return d->clone(); } #include "moc_action.cpp" diff --git a/src/datatypes/action.h b/src/datatypes/action.h index 7895336..6c920da 100644 --- a/src/datatypes/action.h +++ b/src/datatypes/action.h @@ -1,85 +1,93 @@ /* Copyright (C) 2018 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef KITINERARY_ACTION_H #define KITINERARY_ACTION_H #include "kitinerary_export.h" #include "datatypes.h" class QUrl; namespace KItinerary { class ActionPrivate; /** Base class for actions. * @see https://schema.org/Action */ class KITINERARY_EXPORT Action { KITINERARY_BASE_GADGET(Action) KITINERARY_PROPERTY(QUrl, target, setTarget) protected: ///@cond internal QExplicitlySharedDataPointer d; ///@endcond }; /** Cancel action. * @see https://schema.org/CancelAction */ class KITINERARY_EXPORT CancelAction : public Action { KITINERARY_GADGET(CancelAction) }; /** Check-in action. * @see https://schema.org/CheckInAction */ class KITINERARY_EXPORT CheckInAction : public Action { KITINERARY_GADGET(CheckInAction) }; /** Download action. * @see https://schema.org/DownloadAction */ class KITINERARY_EXPORT DownloadAction : public Action { KITINERARY_GADGET(DownloadAction) }; +/** Reserve action. + * @see https://schema.org/ReserveAction + */ +class KITINERARY_EXPORT ReserveAction : public Action +{ + KITINERARY_GADGET(ReserveAction) +}; + /** Edit/update action. * @see https://schema.org/UpdateAction */ class KITINERARY_EXPORT UpdateAction : public Action { KITINERARY_GADGET(UpdateAction) }; /** View action. * @see https://schema.org/ViewAction */ class KITINERARY_EXPORT ViewAction : public Action { KITINERARY_GADGET(ViewAction) }; } #endif // KITINERARY_ACTION_H diff --git a/src/jsonlddocument.cpp b/src/jsonlddocument.cpp index 5305eba..884d58a 100644 --- a/src/jsonlddocument.cpp +++ b/src/jsonlddocument.cpp @@ -1,429 +1,430 @@ /* Copyright (c) 2017 Volker Krause This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "jsonlddocument.h" #include "jsonldimportfilter.h" #include "logging.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace KItinerary; static QVariant createInstance(const QJsonObject &obj); // Eurowings workarounds... static const char* const fallbackDateTimePattern[] = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "MM-dd-yyyy HH:mm" // yes, seriously ;( }; static const auto fallbackDateTimePatternCount = sizeof(fallbackDateTimePattern) / sizeof(const char *); static double doubleValue(const QJsonValue &v) { if (v.isDouble()) { return v.toDouble(); } return v.toString().toDouble(); } static QVariant propertyValue(const QMetaProperty &prop, const QJsonValue &v) { switch (prop.type()) { case QVariant::String: return v.toString(); case QVariant::Date: return QDate::fromString(v.toString(), Qt::ISODate); case QVariant::DateTime: { QDateTime dt; if (v.isObject()) { const auto dtObj = v.toObject(); if (dtObj.value(QLatin1String("@type")).toString() == QLatin1String("QDateTime")) { dt = QDateTime::fromString(dtObj.value(QLatin1String("@value")).toString(), Qt::ISODate); dt.setTimeZone(QTimeZone(dtObj.value(QLatin1String("timezone")).toString().toUtf8())); } } else { auto str = v.toString(); dt = QDateTime::fromString(str, Qt::ISODate); for (unsigned int i = 0; i < fallbackDateTimePatternCount && dt.isNull(); ++i) { dt = QDateTime::fromString(str, QString::fromLatin1(fallbackDateTimePattern[i])); } if (dt.isNull()) { qCDebug(Log) << "Datetime parsing failed for" << str; } } return dt; } case QVariant::Double: return doubleValue(v); case QVariant::Int: if (v.isDouble()) { return v.toDouble(); } return v.toString().toInt(); case QVariant::Url: return QUrl(v.toString()); default: break; } if (prop.type() == qMetaTypeId()) { return doubleValue(v); } if (prop.userType() == qMetaTypeId()) { QVariantList l; if (v.isArray()) { const auto array = v.toArray(); l.reserve(array.size()); for (const auto &elem : array) { if (elem.isObject()) { const auto var = createInstance(elem.toObject()); if (!var.isNull()) { l.push_back(var); } } else if (elem.isString()) { l.push_back(elem.toString()); } } } return QVariant::fromValue(l); } return createInstance(v.toObject()); } static void createInstance(const QMetaObject *mo, void *v, const QJsonObject &obj) { for (auto it = obj.begin(); it != obj.end(); ++it) { if (it.key().startsWith(QLatin1Char('@'))) { continue; } const auto idx = mo->indexOfProperty(it.key().toLatin1().constData()); if (idx < 0) { qCDebug(Log) << "property" << it.key() << "could not be set on object of type" << mo->className(); continue; } const auto prop = mo->property(idx); const auto value = propertyValue(prop, it.value()); prop.writeOnGadget(v, value); } } template static QVariant createInstance(const QJsonObject &obj) { T t; createInstance(&T::staticMetaObject, &t, obj); return QVariant::fromValue(t); } #define MAKE_FACTORY(Class) \ if (type == QLatin1String(#Class)) \ return createInstance(obj) static QVariant createInstance(const QJsonObject &obj) { const auto type = obj.value(QLatin1String("@type")).toString(); MAKE_FACTORY(Action); MAKE_FACTORY(Airline); MAKE_FACTORY(Airport); MAKE_FACTORY(Brand); MAKE_FACTORY(BusReservation); MAKE_FACTORY(BusStation); MAKE_FACTORY(BusTrip); MAKE_FACTORY(CancelAction); MAKE_FACTORY(CheckInAction); MAKE_FACTORY(CreativeWork); MAKE_FACTORY(DigitalDocument); MAKE_FACTORY(DownloadAction); MAKE_FACTORY(EmailMessage); MAKE_FACTORY(Event); MAKE_FACTORY(EventReservation); MAKE_FACTORY(Flight); MAKE_FACTORY(FlightReservation); MAKE_FACTORY(FoodEstablishment); MAKE_FACTORY(FoodEstablishmentReservation); MAKE_FACTORY(RentalCarReservation); MAKE_FACTORY(RentalCar); + MAKE_FACTORY(ReserveAction); MAKE_FACTORY(GeoCoordinates); MAKE_FACTORY(LodgingBusiness); MAKE_FACTORY(LodgingReservation); MAKE_FACTORY(Organization); MAKE_FACTORY(Person); MAKE_FACTORY(Place); MAKE_FACTORY(PostalAddress); MAKE_FACTORY(Seat); MAKE_FACTORY(TaxiReservation); MAKE_FACTORY(Taxi); MAKE_FACTORY(Ticket); MAKE_FACTORY(TouristAttraction); MAKE_FACTORY(TouristAttractionVisit); MAKE_FACTORY(TrainReservation); MAKE_FACTORY(TrainStation); MAKE_FACTORY(TrainTrip); MAKE_FACTORY(UpdateAction); MAKE_FACTORY(ViewAction); return {}; } #undef MAKE_FACTORY QVector JsonLdDocument::fromJson(const QJsonArray &array) { QVector l; l.reserve(array.size()); for (const auto &obj : array) { const auto v = fromJson(obj.toObject()); if (!v.isNull()) { l.push_back(v); } } return l; } QVariant JsonLdDocument::fromJson(const QJsonObject& obj) { return createInstance(JsonLdImportFilter::filterObject(obj)); } static bool valueIsNull(const QVariant &v) { if (v.type() == QVariant::Url) { return !v.toUrl().isValid(); } if (v.type() == qMetaTypeId()) { return std::isnan(v.toFloat()); } return v.isNull(); } static QString typeName(const QMetaObject *mo, const QVariant &v) { const auto n = JsonLdDocument::readProperty(v, "className").toString(); if (!n.isEmpty()) { return n; } if (auto c = strstr(mo->className(), "::")) { return QString::fromUtf8(c + 2); } return QString::fromUtf8(mo->className()); } static QJsonValue toJsonValue(const QVariant &v) { const auto mo = QMetaType(v.userType()).metaObject(); if (!mo) { // basic types switch (v.type()) { case QVariant::String: return v.toString(); case QVariant::Double: return v.toDouble(); case QVariant::Int: return v.toInt(); case QVariant::Date: return v.toDate().toString(Qt::ISODate); case QVariant::DateTime: { const auto dt = v.toDateTime(); if (dt.timeSpec() == Qt::TimeZone) { QJsonObject dtObj; dtObj.insert(QStringLiteral("@type"), QStringLiteral("QDateTime")); dtObj.insert(QStringLiteral("@value"), dt.toString(Qt::ISODate)); dtObj.insert(QStringLiteral("timezone"), QString::fromUtf8(dt.timeZone().id())); return dtObj; } return v.toDateTime().toString(Qt::ISODate); } case QVariant::Url: return v.toUrl().toString(); case QVariant::Bool: return v.toBool(); default: break; } if (v.userType() == qMetaTypeId()) { return v.toFloat(); } if (v.canConvert()) { QSequentialIterable iterable = v.value(); if (iterable.size() == 0) { return {}; } QJsonArray array; for (const auto &var : iterable) { array.push_back(toJsonValue(var)); } return array; } qCDebug(Log) << "unhandled value:" << v; return {}; } // composite types QJsonObject obj; obj.insert(QStringLiteral("@type"), typeName(mo, v)); for (int i = 0; i < mo->propertyCount(); ++i) { const auto prop = mo->property(i); if (!prop.isStored()) { continue; } const auto value = prop.readOnGadget(v.constData()); if (!valueIsNull(value)) { const auto jsVal = toJsonValue(value); if (jsVal.type() != QJsonValue::Null) { obj.insert(QString::fromUtf8(prop.name()), jsVal); } } } if (obj.size() > 1) { return obj; } return {}; } QJsonArray JsonLdDocument::toJson(const QVector &data) { QJsonArray a; for (const auto &d : data) { const auto value = ::toJsonValue(d); if (!value.isObject()) { continue; } auto obj = value.toObject(); obj.insert(QStringLiteral("@context"), QStringLiteral("http://schema.org")); a.push_back(obj); } return a; } QJsonObject JsonLdDocument::toJson(const QVariant& data) { const auto value = ::toJsonValue(data); if (!value.isObject()) { return {}; } auto obj = value.toObject(); obj.insert(QStringLiteral("@context"), QStringLiteral("http://schema.org")); return obj; } QVariant JsonLdDocument::readProperty(const QVariant &obj, const char *name) { const auto mo = QMetaType(obj.userType()).metaObject(); if (!mo) { return {}; } const auto idx = mo->indexOfProperty(name); if (idx < 0) { return {}; } const auto prop = mo->property(idx); return prop.readOnGadget(obj.constData()); } void JsonLdDocument::writeProperty(QVariant &obj, const char *name, const QVariant &value) { const auto mo = QMetaType(obj.userType()).metaObject(); if (!mo) { return; } writePropertyImpl(mo, obj.data(), name, value); } void JsonLdDocument::writePropertyImpl(const QMetaObject* mo, void* obj, const char* name, const QVariant& value) { const auto idx = mo->indexOfProperty(name); if (idx < 0) { return; } const auto prop = mo->property(idx); prop.writeOnGadget(obj, value); } void JsonLdDocument::removeProperty(QVariant &obj, const char *name) { writeProperty(obj, name, QVariant()); } QVariant JsonLdDocument::apply(const QVariant& lhs, const QVariant& rhs) { if (rhs.isNull()) { return lhs; } if (lhs.isNull()) { return rhs; } if (lhs.userType() != rhs.userType()) { qCWarning(Log) << "type mismatch during merging:" << lhs << rhs; return {}; } auto res = lhs; const auto mo = QMetaType(res.userType()).metaObject(); for (int i = 0; i < mo->propertyCount(); ++i) { const auto prop = mo->property(i); if (!prop.isStored()) { continue; } auto pv = prop.readOnGadget(rhs.constData()); if (QMetaType(pv.userType()).metaObject()) { pv = apply(prop.readOnGadget(lhs.constData()), pv); } if (!pv.isNull()) { prop.writeOnGadget(res.data(), pv); } } return res; }