calendaring/event.cpp calendaring/event.h tests/calendaringtest.cpp tests/calendaringtest.h

Christian Mollekopf mollekopf at kolabsys.com
Thu May 24 16:06:58 CEST 2012


 calendaring/event.cpp     |  100 ++++++++++++++++++++++++++++++++++++++++++++++
 calendaring/event.h       |   21 +++++++++
 tests/calendaringtest.cpp |   31 ++++++++++++++
 tests/calendaringtest.h   |    2 
 4 files changed, 154 insertions(+)

New commits:
commit c781551464ec2056201e93b47feacbd1ed6c7f5d
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Thu May 24 16:06:55 2012 +0200

    Delegation helper function.

diff --git a/calendaring/event.cpp b/calendaring/event.cpp
index 7a6cf24..4c18137 100644
--- a/calendaring/event.cpp
+++ b/calendaring/event.cpp
@@ -17,9 +17,11 @@
 
 
 #include "event.h"
+#include <icalendar/icalendar.h>
 
 #include <iostream>
 #include <kolabformat.h>
+#include <kolabevent_p.h>
 
 namespace Kolab {
     namespace Calendaring {
@@ -45,5 +47,103 @@ std::string Event::write() const
     return Kolab::writeEvent(*this);
 }
 
+bool Event::fromIcal(const std::string &input)
+{
+    std::vector<Kolab::Event> list = fromICalEvents(input);
+    if (list.size() != 1) {
+        std::cout << "invalid number of events: " << list.size();
+        return false;
+    }
+    Kolab::Event::operator=(list.at(0));
+    return true;
+}
+
+std::string Event::toIcal() const
+{
+    std::vector<Kolab::Event> list;
+    list.push_back(*this);
+    return Kolab::toICal(list);
+}
+
+bool contains(const Kolab::ContactReference &delegatorRef, const std::vector <Kolab::ContactReference > &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<Kolab::Attendee*> delegateesRef;
+    foreach(const Attendee &a, delegatees) {
+        if (Attendee *attendee = getAttendee(a.contact())) {
+            delegateesRef.push_back(attendee);
+        } else {
+            d->attendees.push_back(a);
+            delegateesRef.push_back(&d->attendees.back());
+        }
+    }
+
+    std::vector<Kolab::Attendee*> 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 <Kolab::ContactReference > 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 <Kolab::ContactReference > 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 <Kolab::Attendee >::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();
+}
+
+
+
     };
 };
diff --git a/calendaring/event.h b/calendaring/event.h
index 3c0e99d..35ac4f7 100644
--- a/calendaring/event.h
+++ b/calendaring/event.h
@@ -37,6 +37,27 @@ public:
 
     bool read(const std::string &);
     std::string write() const;
+
+    bool fromIcal(const std::string &);
+    std::string toIcal() const;
+
+
+    /**
+     * Updates the delegators and delegatees of the event.
+     *
+     * Creates a new attendee for each missing delegatee (delegators are expected to be existing), and then updates each delegatee with the delegator (delegatedFrom).
+     * Delegators delegatedTo is updated accordingly.
+     * Existing attendees are tried to be found by uid/email/name (in this order).
+     *
+     */
+    void delegate(const std::vector<Kolab::Attendee> &delegators, const std::vector<Kolab::Attendee> &delegatees);
+
+    /**
+     * Get attendee by uid/email/name (in this order)
+     */
+    Kolab::Attendee getAttendee(const std::string &);
+private:
+    Kolab::Attendee *getAttendee(const ContactReference &);
 };
 
     };
diff --git a/tests/calendaringtest.cpp b/tests/calendaringtest.cpp
index 1971587..fd88cd6 100644
--- a/tests/calendaringtest.cpp
+++ b/tests/calendaringtest.cpp
@@ -23,6 +23,7 @@
 #include <kolabevent.h>
 
 #include "calendaring/calendaring.h"
+#include <calendaring/event.h>
 
 #include "testhelpers.h"
 
@@ -305,6 +306,36 @@ void CalendaringTest::testCalendar()
     compareEvents(result, expectedResult);
 }
 
+void CalendaringTest::delegationTest()
+{
+    Kolab::Calendaring::Event event;
+    event.setStart(Kolab::cDateTime("Europe/Zurich",2012,5,5,3,4,4));
+
+    Kolab::Attendee att1(Kolab::ContactReference("email1", "name1", "uid1"));
+    Kolab::Attendee att2(Kolab::ContactReference("email2", "name2", "uid2"));
+    Kolab::Attendee att3(Kolab::ContactReference("email3", "name3", "uid3"));
+    Kolab::Attendee att4(Kolab::ContactReference("email4", "name4", "uid4"));
+    
+    std::vector <Kolab::Attendee > attendees;
+    attendees.push_back(att1);
+    attendees.push_back(att2);
+    attendees.push_back(att3);
+    event.setAttendees(attendees);
+
+    std::vector <Kolab::Attendee > delegators;
+    delegators.push_back(att1);
+    delegators.push_back(att2);
+
+    std::vector <Kolab::Attendee > delegatees;
+    delegatees.push_back(att3);
+    delegatees.push_back(att4);
+
+    event.delegate(delegators, delegatees);
+
+    std::cout << event.write();
+    //TODO write an actual test
+}
+
 
 QTEST_MAIN( CalendaringTest )
 
diff --git a/tests/calendaringtest.h b/tests/calendaringtest.h
index 27db5d5..02c39bc 100644
--- a/tests/calendaringtest.h
+++ b/tests/calendaringtest.h
@@ -37,6 +37,8 @@ private slots:
 
     void testCalendar_data();
     void testCalendar();
+
+    void delegationTest();
 };
 
 #endif // CALENDARINGTEST_H





More information about the commits mailing list