diff --git a/framework/src/domain/invitationcontroller.cpp b/framework/src/domain/invitationcontroller.cpp index 074772ba..bb39690c 100644 --- a/framework/src/domain/invitationcontroller.cpp +++ b/framework/src/domain/invitationcontroller.cpp @@ -1,131 +1,132 @@ /* * Copyright (C) 2017 Michael Bohlender, * Copyright (C) 2018 Christian Mollekopf, * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "invitationcontroller.h" #include #include #include #include #include #include #include using namespace Sink::ApplicationDomain; InvitationController::InvitationController() : EventController(), action_accept{new Kube::ControllerAction{this, &InvitationController::accept}}, action_decline{new Kube::ControllerAction{this, &InvitationController::decline}} { } void InvitationController::loadICal(const QString &ical) { using namespace Sink; using namespace Sink::ApplicationDomain; KCalCore::Calendar::Ptr calendar(new KCalCore::MemoryCalendar{QTimeZone::systemTimeZone()}); auto msg = KCalCore::ICalFormat{}.parseScheduleMessage(calendar, ical.toUtf8()); if (!msg) { SinkWarning() << "Invalid scheudle message to process, ignoring..."; return; } auto icalEvent = msg->event().dynamicCast(); if (msg->method() != KCalCore::iTIPRequest) { SinkWarning() << "Invalid method " << msg->method(); return; } if(!icalEvent) { SinkWarning() << "Invalid ICal to process, ignoring..."; return; } Query query; query.request(); query.request(); query.filter(icalEvent->uid().toUtf8()); Store::fetchAll(query).then([this, icalEvent](const QList &events) { if (!events.isEmpty()) { setState(InvitationState::Accepted); auto icalEvent = KCalCore::ICalFormat().readIncidence(events.first()->getIcal()).dynamicCast(); if(!icalEvent) { SinkWarning() << "Invalid ICal to process, ignoring..."; return; } populateFromEvent(*icalEvent); setStart(icalEvent->dtStart()); setEnd(icalEvent->dtEnd()); setUid(icalEvent->uid().toUtf8()); } else { setState(InvitationState::Unknown); populateFromEvent(*icalEvent); setStart(icalEvent->dtStart()); setEnd(icalEvent->dtEnd()); setUid(icalEvent->uid().toUtf8()); } }).exec(); } void InvitationController::accept() { using namespace Sink; using namespace Sink::ApplicationDomain; const auto calendar = getCalendar(); if (!calendar) { qWarning() << "No calendar selected"; return; } auto calcoreEvent = QSharedPointer::create(); calcoreEvent->setUid(getUid()); calcoreEvent->setSummary(getSummary()); calcoreEvent->setDescription(getDescription()); calcoreEvent->setLocation(getLocation()); calcoreEvent->setDtStart(getStart()); calcoreEvent->setDtEnd(getEnd()); calcoreEvent->setAllDay(getAllDay()); Event event(calendar->resourceInstanceIdentifier()); event.setIcal(KCalCore::ICalFormat().toICalString(calcoreEvent).toUtf8()); event.setCalendar(*calendar); auto job = Store::create(event) .then([&] (const KAsync::Error &error) { if (error) { SinkWarning() << "Failed to save the event: " << error; } + setState(InvitationState::Accepted); emit done(); }); run(job); //TODO sendIMipMessage(calcoreEvent); } void InvitationController::decline() { } diff --git a/framework/src/tests/invitationcontrollertest.cpp b/framework/src/tests/invitationcontrollertest.cpp index 4dac9f86..b7a0cbaf 100644 --- a/framework/src/tests/invitationcontrollertest.cpp +++ b/framework/src/tests/invitationcontrollertest.cpp @@ -1,84 +1,95 @@ #include #include #include #include #include #include #include #include #include +#include #include "invitationcontroller.h" using namespace Sink::ApplicationDomain; class InvitationControllerTest : public QObject { Q_OBJECT QByteArray resourceId; QString createInvitation(const QByteArray &uid) { auto calcoreEvent = QSharedPointer::create(); calcoreEvent->setUid(uid); calcoreEvent->setSummary("summary"); calcoreEvent->setDescription("description"); calcoreEvent->setLocation("location"); calcoreEvent->setDtStart(QDateTime::currentDateTime()); + calcoreEvent->setOrganizer("organizer@test.com"); + calcoreEvent->addAttendee(KCalCore::Attendee::Ptr::create("John Doe", "attendee1@test.com", true, KCalCore::Attendee::NeedsAction)); return KCalCore::ICalFormat{}.createScheduleMessage(calcoreEvent, KCalCore::iTIPRequest); } private slots: void initTestCase() { Sink::Test::initTest(); auto account = ApplicationDomainType::createEntity(); Sink::Store::create(account).exec().waitForFinished(); auto resource = DummyResource::create(account.identifier()); Sink::Store::create(resource).exec().waitForFinished(); resourceId = resource.identifier(); } void testAccept() { auto calendar = ApplicationDomainType::createEntity(resourceId); Sink::Store::create(calendar).exec().waitForFinished(); - QByteArray uid{"uid1"}; - const QString ical = createInvitation(uid); + const QByteArray uid{"uid1"}; + const auto ical = createInvitation(uid); { InvitationController controller; controller.loadICal(ical); controller.setCalendar(ApplicationDomainType::Ptr::create(calendar)); QTRY_COMPARE(controller.getState(), InvitationController::Unknown); controller.acceptAction()->execute(); + QTRY_COMPARE(controller.getState(), InvitationController::Accepted); QTRY_COMPARE(Sink::Store::read(Sink::Query{}.filter(calendar)).size(), 1); auto list = Sink::Store::read(Sink::Query{}.filter(calendar)); QCOMPARE(list.size(), 1); + auto event = KCalCore::ICalFormat().readIncidence(list.first().getIcal()).dynamicCast(); + QVERIFY(event); + QCOMPARE(event->uid(), uid); + + const auto attendee = event->attendeeByMail("attendee1@test.com"); + QVERIFY(attendee); + QCOMPARE(attendee->status(), KCalCore::Attendee::Accepted); } - // { - // InvitationController controller; - // controller.loadICal(ical); - // QTRY_COMPARE(controller.state(), InvitationController::Accepted); - // QTRY_COMPARE(controller.uid(), eventUid); - // } + { + InvitationController controller; + controller.loadICal(ical); + QTRY_COMPARE(controller.getState(), InvitationController::Accepted); + QTRY_COMPARE(controller.getUid(), uid); + } } }; QTEST_MAIN(InvitationControllerTest) #include "invitationcontrollertest.moc"