summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVolker Krause <[email protected]>2017-11-11 19:00:57 +0100
committerVolker Krause <[email protected]>2017-11-11 19:00:57 +0100
commit2d06a92e2dbbd56c9cfa2de20fac8a955bedbbee (patch)
tree6c4af3434b90a59b5bfd85ebacd2c09839316f27
parentc7b61377d6fea79316d359a3c9c9c3d1278bba44 (diff)
Allow to add reservations to the calendarv17.11.80
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/CMakeLists.txt1
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.cpp80
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.h6
3 files changed, 84 insertions, 3 deletions
diff --git a/plugins/messageviewer/bodypartformatter/semantic/CMakeLists.txt b/plugins/messageviewer/bodypartformatter/semantic/CMakeLists.txt
index adb3dfa..f83e9d9 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/CMakeLists.txt
+++ b/plugins/messageviewer/bodypartformatter/semantic/CMakeLists.txt
@@ -15,6 +15,7 @@ add_library(messageviewer_bodypartformatter_semantic MODULE ${semantic_srcs})
target_compile_definitions(messageviewer_bodypartformatter_semantic PRIVATE -DTRANSLATION_DOMAIN=\"messageviewer_semantic_plugin\")
target_link_libraries(messageviewer_bodypartformatter_semantic
KF5::MessageViewer
+ KF5::CalendarSupport
KF5::I18n
Grantlee5::Templates
Qt5::DBus
diff --git a/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.cpp b/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.cpp
index 8e451d6..f25fd16 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.cpp
+++ b/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.cpp
@@ -25,6 +25,8 @@
#include <MimeTreeParser/BodyPart>
#include <MimeTreeParser/NodeHelper>
+#include <CalendarSupport/CalendarSingleton>
+
#include <KMime/Content>
#include <KDBusServiceStarter>
@@ -65,8 +67,8 @@ bool SemanticUrlHandler::handleContextMenuRequest(MimeTreeParser::Interface::Bod
action = menu.addAction(QIcon::fromTheme(QStringLiteral("view-calendar")), i18n("Show Calendar"));
QObject::connect(action, &QAction::triggered, this, [this, date](){ showCalendar(date); });
}
-// action = menu.addAction(QIcon::fromTheme(QStringLiteral("appointment-new")), i18n("Add To Calendar"));
-// QObject::connect(action, &QAction::triggered, this, [this, m](){ addToCalendar(m); });
+ action = menu.addAction(QIcon::fromTheme(QStringLiteral("appointment-new")), i18n("Add To Calendar"));
+ QObject::connect(action, &QAction::triggered, this, [this, m](){ addToCalendar(m); });
if (menu.isEmpty())
return true;
@@ -148,7 +150,79 @@ void SemanticUrlHandler::showCalendar(const QDate &date) const
korgIface->call(QStringLiteral("showDate"), date);
}
+KCalCore::Event::Ptr SemanticUrlHandler::eventForReservation(const QVariant& reservation) const
+{
+ if (reservation.userType() == qMetaTypeId<FlightReservation>())
+ return eventForFlightReservation(reservation);
+ else if (reservation.userType() == qMetaTypeId<LodgingReservation>())
+ return eventForLodgingReservation(reservation);
+ return {};
+}
+
+KCalCore::Event::Ptr SemanticUrlHandler::eventForFlightReservation(const QVariant& reservation) const
+{
+ using namespace KCalCore;
+
+ const auto flight = readOnGadget<FlightReservation>(reservation, "reservationFor");
+ const auto airline = readOnGadget<Flight>(flight, "airline");
+ const auto depPort = readOnGadget<Flight>(flight, "departureAirport");
+ const auto arrPort = readOnGadget<Flight>(flight, "arrivalAirport");
+ if (flight.isNull() || airline.isNull() || depPort.isNull() || arrPort.isNull())
+ return {};
+
+ Event::Ptr event(new Event);
+ event->setSummary(i18n("Flight %1 %2 from %3 to %4",
+ readOnGadget<Airline>(airline, "iataCode").toString(),
+ readOnGadget<Flight>(flight, "flightNumber").toString(),
+ readOnGadget<Airport>(depPort, "iataCode").toString(),
+ readOnGadget<Airport>(arrPort, "iataCode").toString()
+ ));
+ event->setLocation(readOnGadget<Airport>(depPort, "name").toString());
+ event->setDtStart(readOnGadget<Flight>(flight, "departureTime").toDateTime());
+ event->setDtEnd(readOnGadget<Flight>(flight, "arrivalTime").toDateTime());
+ event->setAllDay(false);
+ event->setDescription(i18n("Booking reference: %1",
+ readOnGadget<FlightReservation>(reservation, "reservationNumber").toString()
+ ));
+ return event;
+}
+
+KCalCore::Event::Ptr SemanticUrlHandler::eventForLodgingReservation(const QVariant& reservation) const
+{
+ using namespace KCalCore;
+
+ const auto lodgingBusiness = readOnGadget<LodgingReservation>(reservation, "reservationFor");
+ const auto address = readOnGadget<LodgingBusiness>(lodgingBusiness, "address");
+ if (lodgingBusiness.isNull() || address.isNull())
+ return {};
+
+ Event::Ptr event(new Event);
+ event->setSummary(i18n("Hotel reservation: %1",
+ readOnGadget<LodgingBusiness>(lodgingBusiness, "name").toString()
+ ));
+ event->setLocation(i18n("%1, %2 %3, %4",
+ readOnGadget<PostalAddress>(address, "streetAddress").toString(),
+ readOnGadget<PostalAddress>(address, "postalCode").toString(),
+ readOnGadget<PostalAddress>(address, "addressLocality").toString(),
+ readOnGadget<PostalAddress>(address, "addressCountry").toString()
+ ));
+ event->setDtStart(QDateTime(readOnGadget<LodgingReservation>(reservation, "checkinDate").toDate(), QTime()));
+ event->setDtEnd(QDateTime(readOnGadget<LodgingReservation>(reservation, "checkoutDate").toDate(), QTime(23, 59, 59)));
+ event->setAllDay(true);
+ event->setDescription(i18n("Booking reference: %1",
+ readOnGadget<FlightReservation>(reservation, "reservationNumber").toString()
+ ));
+ event->setTransparency(Event::Transparent);
+ return event;
+}
+
void SemanticUrlHandler::addToCalendar(SemanticMemento *memento) const
{
- qCDebug(SEMANTIC_LOG) << "ADD TO CALENDAR" << memento;
+ auto calendar = CalendarSupport::calendarSingleton(true);
+ for (const auto &r : memento->data()) {
+ const auto event = eventForReservation(r);
+ if (!event)
+ continue;
+ calendar->addEvent(event);
+ }
}
diff --git a/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.h b/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.h
index f035151..3c0c08f 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.h
+++ b/plugins/messageviewer/bodypartformatter/semantic/semanticurlhandler.h
@@ -22,6 +22,8 @@
#include <MessageViewer/BodyPartURLHandler>
+#include <KCalCore/Event>
+
#include <QObject>
class SemanticMemento;
@@ -42,6 +44,10 @@ private:
SemanticMemento* memento(MimeTreeParser::Interface::BodyPart *part) const;
QDate dateForReservation(SemanticMemento *memento) const;
void showCalendar(const QDate &date) const;
+
+ KCalCore::Event::Ptr eventForReservation(const QVariant &reservation) const;
+ KCalCore::Event::Ptr eventForFlightReservation(const QVariant &reservation) const;
+ KCalCore::Event::Ptr eventForLodgingReservation(const QVariant &reservation) const;
void addToCalendar(SemanticMemento *memento) const;
};