1 /**
2  * @file     icalspanlist_cxx.cpp
3  * @author   Critical Path
4  * @brief    C++ class wrapping the icalspanlist data structure
5  *
6 
7  (C) COPYRIGHT 2001, Critical Path
8 
9  This library is free software; you can redistribute it and/or modify
10  it under the terms of either:
11 
12     The LGPL as published by the Free Software Foundation, version
13     2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html
14 
15  Or:
16 
17     The Mozilla Public License Version 2.0. You may obtain a copy of
18     the License at https://www.mozilla.org/MPL/
19 */
20 
21 #include "icalspanlist_cxx.h"
22 #include "vcomponent_cxx.h"
23 
24 #include <cstdlib> // for free()
25 
26 using namespace LibICal;
27 
ICalSpanList()28 ICalSpanList::ICalSpanList() : data(0)
29 {
30     throw icalerrno;
31 }
32 
ICalSpanList(const ICalSpanList & v)33 ICalSpanList::ICalSpanList(const ICalSpanList &v) : data(v.data)
34 {
35     if (data == NULL) {
36         throw icalerrno;
37     }
38 }
39 
40 /** Construct an ICalSpanList from an icalset
41     @param set     The icalset containing the VEVENTS
42     @param start   Designated start of the spanlist
43     @param end     Designated end of the spanlist
44 */
ICalSpanList(icalset * set,icaltimetype start,icaltimetype end)45 ICalSpanList::ICalSpanList(icalset *set, icaltimetype start, icaltimetype end)
46     : data(icalspanlist_new(set, start, end))
47 {
48     if (data == NULL) {
49         throw icalerrno;
50     }
51 }
52 
53 /** @brief Constructor
54     @param comp  A valid icalcomponent with a VFREEBUSY section
55 */
56 
ICalSpanList(icalcomponent * comp)57 ICalSpanList::ICalSpanList(icalcomponent *comp) : data(icalspanlist_from_vfreebusy(comp))
58 {
59     if (data == NULL) {
60         throw icalerrno;
61     }
62 }
63 
64 /** @brief Constructor
65     @param comp  A valid VComponent with a VFREEBUSY section
66 */
ICalSpanList(VComponent & comp)67 ICalSpanList::ICalSpanList(VComponent &comp)
68     : data(icalspanlist_from_vfreebusy(static_cast<icalcomponent *>(comp)))
69 {
70     if (data == NULL) {
71         throw icalerrno;
72     }
73 }
74 
dump()75 void ICalSpanList::dump()
76 {
77     icalspanlist_dump(data);
78 }
79 
80 /** Destructor */
~ICalSpanList()81 ICalSpanList::~ICalSpanList()
82 {
83     if (data == NULL) {
84         icalspanlist_free(data);
85     }
86 }
87 
88 /**
89  * @brief Returns a VFREEBUSY component for the object.
90  *
91  * @see icalspanlist_as_vfreebusy()
92  */
93 
get_vfreebusy(const char * organizer,const char * attendee)94 VComponent *ICalSpanList::get_vfreebusy(const char *organizer, const char *attendee)
95 {
96     icalcomponent *comp;
97     VComponent    *vcomp;
98 
99     comp = icalspanlist_as_vfreebusy(data, organizer, attendee);
100     if (comp == NULL) {
101         throw icalerrno;
102     }
103 
104     vcomp = new VComponent(comp);
105     if (vcomp == NULL) {
106         throw icalerrno;
107     }
108 
109     return vcomp;
110 }
111 
112 /**
113  * @brief Returns a summary of events over delta_t
114  *
115  * @param delta_t    Number of seconds to divide the spanlist time period
116  *                   into.
117  *
118  * This method calculates the total number of events in each time slot
119  * of delta_t seconds.
120  *
121  * @see icalspanlist_as_freebusy_matrix()
122  */
123 
as_vector(int delta_t)124 std::vector<int> ICalSpanList::as_vector(int delta_t)
125 {
126     int *matrix;
127     int i = 0;
128     std::vector<int> event_vec;
129 
130     matrix = icalspanlist_as_freebusy_matrix(data, delta_t);
131 
132     if (matrix == NULL) {
133         throw icalerrno;
134     }
135 
136     while (matrix[i] != -1) {
137         event_vec.push_back(matrix[i]); // Add item at end of vector
138     }
139 
140     free(matrix);
141     return event_vec;
142 }
143