summaryrefslogtreecommitdiff
path: root/src/libicalss/icalspanlist_cxx.cpp
blob: 4f72eb50fc26cfe00859cd95f4d442a98a72d3f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * @file     icalspanlist_cxx.cpp
 * @author   Critical Path
 * @brief    C++ class wrapping the icalspanlist data structure
 *

 (C) COPYRIGHT 2001, Critical Path

 This library is free software; you can redistribute it and/or modify
 it under the terms of either:

    The LGPL as published by the Free Software Foundation, version
    2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html

 Or:

    The Mozilla Public License Version 2.0. You may obtain a copy of
    the License at https://www.mozilla.org/MPL/
*/

#include "icalspanlist_cxx.h"
#include "vcomponent_cxx.h"

#include <cstdlib> // for free()

using namespace LibICal;

ICalSpanList::ICalSpanList() : data(0)
{
    throw icalerrno;
}

ICalSpanList::ICalSpanList(const ICalSpanList &v) : data(v.data)
{
    if (data == NULL) {
        throw icalerrno;
    }
}

/** Construct an ICalSpanList from an icalset
    @param set     The icalset containing the VEVENTS
    @param start   Designated start of the spanlist
    @param end     Designated end of the spanlist
*/
ICalSpanList::ICalSpanList(icalset *set, icaltimetype start, icaltimetype end)
    : data(icalspanlist_new(set, start, end))
{
    if (data == NULL) {
        throw icalerrno;
    }
}

/** @brief Constructor
    @param comp  A valid icalcomponent with a VFREEBUSY section
*/

ICalSpanList::ICalSpanList(icalcomponent *comp) : data(icalspanlist_from_vfreebusy(comp))
{
    if (data == NULL) {
        throw icalerrno;
    }
}

/** @brief Constructor
    @param comp  A valid VComponent with a VFREEBUSY section
*/
ICalSpanList::ICalSpanList(VComponent &comp)
    : data(icalspanlist_from_vfreebusy(static_cast<icalcomponent *>(comp)))
{
    if (data == NULL) {
        throw icalerrno;
    }
}

void ICalSpanList::dump()
{
    icalspanlist_dump(data);
}

/** Destructor */
ICalSpanList::~ICalSpanList()
{
    if (data == NULL) {
        icalspanlist_free(data);
    }
}

/**
 * @brief Returns a VFREEBUSY component for the object.
 *
 * @see icalspanlist_as_vfreebusy()
 */

VComponent *ICalSpanList::get_vfreebusy(const char *organizer, const char *attendee)
{
    icalcomponent *comp;
    VComponent    *vcomp;

    comp = icalspanlist_as_vfreebusy(data, organizer, attendee);
    if (comp == NULL) {
        throw icalerrno;
    }

    vcomp = new VComponent(comp);
    if (vcomp == NULL) {
        throw icalerrno;
    }

    return vcomp;
}

/**
 * @brief Returns a summary of events over delta_t
 *
 * @param delta_t    Number of seconds to divide the spanlist time period
 *                   into.
 *
 * This method calculates the total number of events in each time slot
 * of delta_t seconds.
 *
 * @see icalspanlist_as_freebusy_matrix()
 */

std::vector<int> ICalSpanList::as_vector(int delta_t)
{
    int *matrix;
    int i = 0;
    std::vector<int> event_vec;

    matrix = icalspanlist_as_freebusy_matrix(data, delta_t);

    if (matrix == NULL) {
        throw icalerrno;
    }

    while (matrix[i] != -1) {
        event_vec.push_back(matrix[i]); // Add item at end of vector
    }

    free(matrix);
    return event_vec;
}