diff --git a/calendaring/event.cpp b/calendaring/event.cpp index bcdf833..ea9ead1 100644 --- a/calendaring/event.cpp +++ b/calendaring/event.cpp @@ -1,176 +1,181 @@ /* * Copyright (C) 2012 Christian Mollekopf * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "event.h" #include #include #include #include #include #include namespace Kolab { namespace Calendaring { Event::Event() : Kolab::Event() { setUid(Kolab::generateUID()); } Event::Event(const Kolab::Event &e) : Kolab::Event(e) { } Event::~Event() { } bool Event::read(const std::string &string) { const Kolab::Event &e = Kolab::readEvent(string, false); if (Kolab::error()) { return false; } Kolab::Event::operator=(e); return true; } std::string Event::write() const { return Kolab::writeEvent(*this); } bool contains(const Kolab::ContactReference &delegatorRef, const std::vector &list) { foreach (const Kolab::ContactReference &ref, list) { if (delegatorRef.uid() == ref.uid() || delegatorRef.email() == ref.email() || delegatorRef.name() == ref.name()) { return true; } } return false; } void Event::delegate(const std::vector< Attendee >& delegators, const std::vector< Attendee >& delegatees) { - //First build a list of attendee references, and insert any missing attendees - std::vector delegateesRef; + //First insert any missing attendees foreach(const Attendee &a, delegatees) { - if (Attendee *attendee = getAttendee(a.contact())) { - delegateesRef.push_back(attendee); - } else { + if (!getAttendee(a.contact())) { d->attendees.push_back(a); - delegateesRef.push_back(&d->attendees.back()); } } + //Build a list of attendee references + //These are pointers into d->attendees, so we MUST NOT modify that vector after this point! + std::vector delegateesRef; + foreach(const Attendee &a, delegatees) { + Attendee *attendee = getAttendee(a.contact()); + Q_ASSERT(attendee); + delegateesRef.push_back(attendee); + } + std::vector delegatorsRef; foreach(const Attendee& a, delegators) { if (Attendee *attendee = getAttendee(a.contact())) { delegatorsRef.push_back(attendee); } else { std::cout << "missing delegator"; } } foreach (Attendee *delegatee, delegateesRef) { std::vector delegatedFrom = delegatee->delegatedFrom(); foreach (Attendee *delegator, delegatorsRef) { //Set the delegator on each delegatee const ContactReference &delegatorRef = delegator->contact(); if (!contains(delegatorRef, delegatedFrom)) { delegatedFrom.push_back(Kolab::ContactReference(Kolab::ContactReference::EmailReference, delegatorRef.email(), delegatorRef.name())); } //Set the delegatee on each delegator std::vector delegatedTo = delegator->delegatedTo(); const ContactReference &delegaeeRef = delegatee->contact(); if (!contains(delegaeeRef, delegatedTo)) { delegatedTo.push_back(Kolab::ContactReference(Kolab::ContactReference::EmailReference, delegaeeRef.email(), delegaeeRef.name())); } delegator->setDelegatedTo(delegatedTo); } delegatee->setDelegatedFrom(delegatedFrom); } } Attendee *Event::getAttendee(const ContactReference &ref) { for(std::vector ::iterator it = d->attendees.begin(); it != d->attendees.end(); it++) { if (it->contact().uid() == ref.uid() || it->contact().email() == ref.email() || it->contact().name() == ref.name()) { return &*it; } } return 0; } Attendee Event::getAttendee(const std::string &s) { foreach(const Attendee &a, attendees()) { if (a.contact().uid() == s || a.contact().email() == s || a.contact().name() == s) { return a; } } return Attendee(); } cDateTime Calendaring::Event::getNextOccurence(const cDateTime &date) { KCalCore::Event::Ptr event = Kolab::Conversion::toKCalCore(*this); qWarning() << "Converted start " << event->dtStart() << Kolab::Conversion::toDate(date); if (!event->recurs()) { return cDateTime(); } const QDateTime nextDate = event->recurrence()->getNextDateTime(Kolab::Conversion::toDate(date)); qWarning() << "Next date " << nextDate; return Kolab::Conversion::fromDate(nextDate); } cDateTime Calendaring::Event::getOccurenceEndDate(const cDateTime &startDate) { KCalCore::Event::Ptr event = Kolab::Conversion::toKCalCore(*this); const QDateTime start = Kolab::Conversion::toDate(startDate); return Kolab::Conversion::fromDate(event->endDateForStart(start)); } cDateTime Calendaring::Event::getLastOccurrence() const { KCalCore::Event::Ptr event = Kolab::Conversion::toKCalCore(*this); if (!event->recurs()) { return cDateTime(); } const QDateTime endDate = event->recurrence()->endDateTime(); return Kolab::Conversion::fromDate(endDate); } }; };