File indexing completed on 2024-11-24 04:44:10

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "event.h"
0008 #include "conversion/commonconversion.h"
0009 #include "conversion/kcalconversion.h"
0010 #include "kolabformat/kolabobject.h"
0011 
0012 #include <iostream>
0013 #include <kolabevent_p.h>
0014 #include <kolabformat.h>
0015 
0016 namespace Kolab
0017 {
0018 namespace Calendaring
0019 {
0020 Event::Event()
0021     : Kolab::Event()
0022 {
0023     setUid(Kolab::generateUID());
0024 }
0025 
0026 Event::Event(const Kolab::Event &e)
0027     : Kolab::Event(e)
0028 {
0029 }
0030 
0031 Event::~Event() = default;
0032 
0033 bool Event::read(const std::string &string)
0034 {
0035     const Kolab::Event &e = Kolab::readEvent(string, false);
0036     if (Kolab::error()) {
0037         return false;
0038     }
0039     Kolab::Event::operator=(e);
0040     return true;
0041 }
0042 
0043 std::string Event::write() const
0044 {
0045     return Kolab::writeEvent(*this);
0046 }
0047 
0048 bool Event::fromMime(const std::string &input)
0049 {
0050     KMime::Message::Ptr msg = KMime::Message::Ptr(new KMime::Message);
0051     msg->setContent(KMime::CRLFtoLF(Kolab::Conversion::fromStdString(input).toUtf8()));
0052     msg->parse();
0053     msg->content(KMime::ContentIndex());
0054     KolabObjectReader reader(msg);
0055     if (reader.getType() != EventObject) {
0056         std::cout << "not an event ";
0057         return false;
0058     }
0059     const Kolab::Event &e = Kolab::Conversion::fromKCalendarCore(*reader.getEvent());
0060     Kolab::Event::operator=(e);
0061     return true;
0062 }
0063 
0064 std::string Event::toMime() const
0065 {
0066     return std::string(KolabObjectWriter::writeEvent(Kolab::Conversion::toKCalendarCore(*this))->encodedContent().constData());
0067 }
0068 
0069 bool Event::fromICal(const std::string &input)
0070 {
0071     std::vector<Kolab::Event> list = fromICalEvents(input);
0072     if (list.size() != 1) {
0073         std::cout << "invalid number of events: " << list.size();
0074         return false;
0075     }
0076     Kolab::Event::operator=(list.at(0));
0077     return true;
0078 }
0079 
0080 std::string Event::toICal() const
0081 {
0082     std::vector<Kolab::Event> list;
0083     list.push_back(*this);
0084     return Kolab::toICal(list);
0085 }
0086 
0087 bool Event::fromIMip(const std::string &input)
0088 {
0089     const std::vector<Kolab::Event> list = mITipHandler.fromIMip(input);
0090     if (list.size() != 1) {
0091         std::cout << "invalid number of events: " << list.size();
0092         return false;
0093     }
0094     Kolab::Event::operator=(list.at(0));
0095     return true;
0096 }
0097 
0098 std::string Event::toIMip(ITipMethod method) const
0099 {
0100     std::vector<Kolab::Event> list;
0101     list.push_back(*this);
0102     return mITipHandler.toIMip(*this, static_cast<ITipHandler::ITipMethod>(method), organizer().email());
0103 }
0104 
0105 Calendaring::Event::ITipMethod Event::getSchedulingMethod() const
0106 {
0107     Q_ASSERT((int)iTIPPublish == (int)ITipHandler::iTIPPublish);
0108     Q_ASSERT((int)iTIPNoMethod == (int)ITipHandler::iTIPNoMethod);
0109     return static_cast<ITipMethod>(mITipHandler.method());
0110 }
0111 
0112 bool contains(const Kolab::ContactReference &delegatorRef, const std::vector<Kolab::ContactReference> &list)
0113 {
0114     for (const Kolab::ContactReference &ref : list) {
0115         if (delegatorRef.uid() == ref.uid() || delegatorRef.email() == ref.email() || delegatorRef.name() == ref.name()) {
0116             return true;
0117         }
0118     }
0119     return false;
0120 }
0121 
0122 void Event::delegate(const std::vector<Attendee> &delegators, const std::vector<Attendee> &delegatees)
0123 {
0124     // First build a list of attendee references, and insert any missing attendees
0125     std::vector<Kolab::Attendee *> delegateesRef;
0126     for (const Attendee &a : delegatees) {
0127         if (Attendee *attendee = getAttendee(a.contact())) {
0128             delegateesRef.push_back(attendee);
0129         } else {
0130             d->attendees.push_back(a);
0131             delegateesRef.push_back(&d->attendees.back());
0132         }
0133     }
0134 
0135     std::vector<Kolab::Attendee *> delegatorsRef;
0136     for (const Attendee &a : delegators) {
0137         if (Attendee *attendee = getAttendee(a.contact())) {
0138             delegatorsRef.push_back(attendee);
0139         } else {
0140             std::cout << "missing delegator";
0141         }
0142     }
0143 
0144     for (Attendee *delegatee : std::as_const(delegateesRef)) {
0145         std::vector<Kolab::ContactReference> delegatedFrom = delegatee->delegatedFrom();
0146         for (Attendee *delegator : std::as_const(delegatorsRef)) {
0147             // Set the delegator on each delegatee
0148             const ContactReference &delegatorRef = delegator->contact();
0149             if (!contains(delegatorRef, delegatedFrom)) {
0150                 delegatedFrom.emplace_back(Kolab::ContactReference::EmailReference, delegatorRef.email(), delegatorRef.name());
0151             }
0152 
0153             // Set the delegatee on each delegator
0154             std::vector<Kolab::ContactReference> delegatedTo = delegator->delegatedTo();
0155             const ContactReference &delegaeeRef = delegatee->contact();
0156             if (!contains(delegaeeRef, delegatedTo)) {
0157                 delegatedTo.emplace_back(Kolab::ContactReference::EmailReference, delegaeeRef.email(), delegaeeRef.name());
0158             }
0159             delegator->setDelegatedTo(delegatedTo);
0160         }
0161         delegatee->setDelegatedFrom(delegatedFrom);
0162     }
0163 }
0164 
0165 Attendee *Event::getAttendee(const ContactReference &ref)
0166 {
0167     for (auto it = d->attendees.begin(), end = d->attendees.end(); it != end; ++it) {
0168         if (it->contact().uid() == ref.uid() || it->contact().email() == ref.email() || it->contact().name() == ref.name()) {
0169             return &*it;
0170         }
0171     }
0172     return nullptr;
0173 }
0174 
0175 Attendee Event::getAttendee(const std::string &s)
0176 {
0177     const auto atts{attendees()};
0178     for (const Attendee &a : atts) {
0179         if (a.contact().uid() == s || a.contact().email() == s || a.contact().name() == s) {
0180             return a;
0181         }
0182     }
0183     return {};
0184 }
0185 
0186 cDateTime Calendaring::Event::getNextOccurence(const cDateTime &date)
0187 {
0188     KCalendarCore::Event::Ptr event = Kolab::Conversion::toKCalendarCore(*this);
0189     if (!event->recurs()) {
0190         return {};
0191     }
0192     const QDateTime nextDate = event->recurrence()->getNextDateTime(Kolab::Conversion::toDate(date));
0193     return Kolab::Conversion::fromDate(nextDate, date.isDateOnly());
0194 }
0195 
0196 cDateTime Calendaring::Event::getOccurenceEndDate(const cDateTime &startDate)
0197 {
0198     KCalendarCore::Event::Ptr event = Kolab::Conversion::toKCalendarCore(*this);
0199     const QDateTime start = Kolab::Conversion::toDate(startDate);
0200     return Kolab::Conversion::fromDate(event->endDateForStart(start), event->allDay());
0201 }
0202 
0203 cDateTime Calendaring::Event::getLastOccurrence() const
0204 {
0205     KCalendarCore::Event::Ptr event = Kolab::Conversion::toKCalendarCore(*this);
0206     if (!event->recurs()) {
0207         return {};
0208     }
0209     const QDateTime endDate = event->recurrence()->endDateTime();
0210     return Kolab::Conversion::fromDate(endDate, event->allDay());
0211 }
0212 }
0213 }