freebusy.cpp 12 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * Copyright (C) 2012  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * 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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Laurent Montel's avatar
Laurent Montel committed
16
17
18
 */

#include "freebusy.h"
19
#include "pimkolab_debug.h"
Laurent Montel's avatar
Laurent Montel committed
20
21
22
#include "conversion/kcalconversion.h"
#include "conversion/commonconversion.h"
#include "libkolab-version.h"
23
24
#include <kcalendarcore/freebusy.h>
#include <kcalendarcore/icalformat.h>
Laurent Montel's avatar
Laurent Montel committed
25
#include <QUuid>
Laurent Montel's avatar
Laurent Montel committed
26
27
#include <QTime>

28
// namespace KCalendarCore {
Laurent Montel's avatar
Laurent Montel committed
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
//     struct KCalFreebusy
// {
//
// void init( const Event::List &eventList, const KDateTime &start, const KDateTime &end )
// {
//     mDtStart = start.toUtc();
//     mDtEnd = end.toUtc();
//
//   // Loops through every event in the calendar
//   Event::List::ConstIterator it;
//   for ( it = eventList.constBegin(); it != eventList.constEnd(); ++it ) {
//     Event::Ptr event = *it;
//
//     // If this event is transparent it shouldn't be in the freebusy list.
//     if ( event->transparency() == Event::Transparent ) {
//       continue;
//     }
//
//     if ( event->hasRecurrenceId() ) {
//       continue; //TODO apply special period exception (duration could be different)
//     }
//
//     const KDateTime eventStart = event->dtStart().toUtc();
//     const KDateTime eventEnd = event->dtEnd().toUtc();
//
//     if ( event->recurs() ) {
55
56
//         const KCalendarCore::Duration duration( eventStart, eventEnd );
//         const KCalendarCore::DateTimeList list = event->recurrence()->timesInInterval(start, end);
Laurent Montel's avatar
Laurent Montel committed
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
//         foreach (const KDateTime &dt, list) {
//             const KDateTime utc = dt.toUtc();
//             addLocalPeriod(utc, duration.end(utc) );
//         }
//     } else {
//         addLocalPeriod( eventStart, eventEnd );
//     }
//   }
//
// //   q->sortList();
// }
//
// bool addLocalPeriod(
//                                         const KDateTime &eventStart,
//                                         const KDateTime &eventEnd )
// {
//   KDateTime tmpStart;
//   KDateTime tmpEnd;
//
//   //Check to see if the start *or* end of the event is
//   //between the start and end of the freebusy dates.
//   if ( !( ( ( mDtStart.secsTo( eventStart ) >= 0 ) &&
//             ( eventStart.secsTo( mDtEnd ) >= 0 ) ) ||
//           ( ( mDtStart.secsTo( eventEnd ) >= 0 ) &&
//             ( eventEnd.secsTo( mDtEnd ) >= 0 ) ) ) ) {
82
//       qCDebug(PIMKOLAB_LOG) << "out of scope";
Laurent Montel's avatar
Laurent Montel committed
83
84
85
//     return false;
//   }
//
86
// //   qCDebug(PIMKOLAB_LOG) << eventStart.date().toString() << eventStart.time().toString() << mDtStart.toString();
Laurent Montel's avatar
Laurent Montel committed
87
//   if ( eventStart < mDtStart ) { //eventStart is before start
88
// //       qCDebug(PIMKOLAB_LOG) << "use start";
Laurent Montel's avatar
Laurent Montel committed
89
90
91
92
93
//     tmpStart = mDtStart;
//   } else {
//     tmpStart = eventStart;
//   }
//
94
//   qCDebug(PIMKOLAB_LOG) << eventEnd.date().toString() << eventEnd.time().toString() << mDtEnd.toString();
Laurent Montel's avatar
Laurent Montel committed
95
//   if ( eventEnd > mDtEnd ) { //event end is after dtEnd
96
// //     qCDebug(PIMKOLAB_LOG) << "use end";
Laurent Montel's avatar
Laurent Montel committed
97
98
99
100
101
//     tmpEnd = mDtEnd;
//   } else {
//     tmpEnd = eventEnd;
//   }
//
102
// //   qCDebug(PIMKOLAB_LOG) << "########## " << tmpStart.isValid();
Laurent Montel's avatar
Laurent Montel committed
103
104
//   Q_ASSERT(tmpStart.isValid());
//   Q_ASSERT(tmpEnd.isValid());
105
// //   qCDebug(PIMKOLAB_LOG) << tmpStart.date().toString() << tmpStart.time().toString() << tmpStart.toString();
Laurent Montel's avatar
Laurent Montel committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
//   FreeBusyPeriod p( tmpStart, tmpEnd );
//   mBusyPeriods.append( p );
//
//   return true;
// }
//
//     KDateTime mDtStart;
//     KDateTime mDtEnd;                  // end datetime
//     FreeBusyPeriod::List mBusyPeriods; // list of periods
//
// };
//
// } // Namespace

namespace Kolab {
Laurent Montel's avatar
Laurent Montel committed
122
namespace FreebusyUtils {
Laurent Montel's avatar
Laurent Montel committed
123
124
125
126
127
128
static QString createUuid()
{
    const QString uuid = QUuid::createUuid().toString();
    return uuid.mid(1, uuid.size()-2);
}

Laurent Montel's avatar
Laurent Montel committed
129
Kolab::Period addLocalPeriod(const QDateTime &eventStart, const QDateTime &eventEnd, const QDateTime &mDtStart, const QDateTime &mDtEnd, bool allDay)
Laurent Montel's avatar
Laurent Montel committed
130
{
Laurent Montel's avatar
Laurent Montel committed
131
132
    QDateTime tmpStart;
    QDateTime tmpEnd;
Laurent Montel's avatar
Laurent Montel committed
133

Laurent Montel's avatar
Laurent Montel committed
134
135
136
137
138
139
    //Check to see if the start *or* end of the event is
    //between the start and end of the freebusy dates.
    if (!(((mDtStart <= eventStart)
           && (eventStart <= mDtEnd))
          || ((mDtStart <= eventEnd)
              && (eventEnd <= mDtEnd)))) {
140
        qCDebug(PIMKOLAB_LOG) << "event is not within the fb range, skipping";
Laurent Montel's avatar
Laurent Montel committed
141
142
        return Kolab::Period();
    }
Laurent Montel's avatar
Laurent Montel committed
143

Laurent Montel's avatar
Laurent Montel committed
144
145
146
147
148
    if (eventStart < mDtStart) { //eventStart is before start
        tmpStart = mDtStart;
    } else {
        tmpStart = eventStart;
    }
Laurent Montel's avatar
Laurent Montel committed
149

150
//   qCDebug(PIMKOLAB_LOG) << eventEnd.date().toString() << eventEnd.time().toString() << mDtEnd.toString();
Laurent Montel's avatar
Laurent Montel committed
151
152
153
154
155
156
157
158
159
160
161
162
    if (eventEnd > mDtEnd) { //event end is after dtEnd
        tmpEnd = mDtEnd;
    } else {
        tmpEnd = eventEnd;
    }
    Q_ASSERT(tmpStart.isValid());
    Q_ASSERT(tmpEnd.isValid());
    if (allDay) {
        tmpStart.setTime(QTime(0, 0, 0, 0));
        tmpEnd.setTime(QTime(23, 59, 59, 999)); //The window is inclusive
    }
    return Kolab::Period(Kolab::Conversion::fromDate(tmpStart, allDay), Kolab::Conversion::fromDate(tmpEnd, allDay));
Laurent Montel's avatar
Laurent Montel committed
163
164
}

Laurent Montel's avatar
Laurent Montel committed
165
Freebusy generateFreeBusy(const std::vector< Event > &events, const cDateTime &startDate, const cDateTime &endDate)
Laurent Montel's avatar
Laurent Montel committed
166
{
167
    QList<KCalendarCore::Event::Ptr> list;
168
    list.reserve(events.size());
Laurent Montel's avatar
Laurent Montel committed
169
    for (const Kolab::Event &e : events) {
170
        list.append(Kolab::Conversion::toKCalendarCore(e));
Laurent Montel's avatar
Laurent Montel committed
171
    }
172
    KCalendarCore::Person person(QStringLiteral("dummyname"), QStringLiteral("dummyemail"));
Laurent Montel's avatar
Laurent Montel committed
173
174
175
    return generateFreeBusy(list, Kolab::Conversion::toDate(startDate), Kolab::Conversion::toDate(endDate), person, startDate.isDateOnly());
}

176
Freebusy generateFreeBusy(const QList<KCalendarCore::Event::Ptr> &events, const QDateTime &startDate, const QDateTime &endDate, const KCalendarCore::Person &organizer, bool allDay)
Laurent Montel's avatar
Laurent Montel committed
177
178
179
180
181
182
{
    /*
     * TODO the conversion of date-only values to date-time is only necessary because xCal doesn't allow date only. iCalendar doesn't seem to make this restriction so it looks like a bug.
     */
    QDateTime start = startDate.toUTC();
    if (allDay) {
Laurent Montel's avatar
Laurent Montel committed
183
        start.setTime(QTime(0, 0, 0, 0));
Laurent Montel's avatar
Laurent Montel committed
184
185
186
187
    }
    QDateTime end = endDate.toUTC();
    if (allDay) {
        end = end.addDays(1);
Laurent Montel's avatar
Laurent Montel committed
188
        end.setTime(QTime(0, 0, 0, 0)); //The window is inclusive
Laurent Montel's avatar
Laurent Montel committed
189
190
    }

191
    //TODO try to merge that with KCalendarCore::Freebusy
Laurent Montel's avatar
Laurent Montel committed
192
    std::vector<Kolab::FreebusyPeriod> freebusyPeriods;
193
    Q_FOREACH (const KCalendarCore::Event::Ptr &event, events) {
Laurent Montel's avatar
Laurent Montel committed
194
        // If this event is transparent it shouldn't be in the freebusy list.
195
        if (event->transparency() == KCalendarCore::Event::Transparent) {
Laurent Montel's avatar
Laurent Montel committed
196
197
198
            continue;
        }

Laurent Montel's avatar
Laurent Montel committed
199
        if (event->hasRecurrenceId()) {
Laurent Montel's avatar
Laurent Montel committed
200
201
202
203
204
205
206
            continue; //TODO apply special period exception (duration could be different)
        }

        const QDateTime eventStart = event->dtStart().toUTC();
        const QDateTime eventEnd = event->dtEnd().toUTC();

        std::vector <Kolab::Period> periods;
Laurent Montel's avatar
Laurent Montel committed
207
        if (event->recurs()) {
208
            const KCalendarCore::Duration duration(eventStart, eventEnd);
Laurent Montel's avatar
Laurent Montel committed
209
            const auto list = event->recurrence()->timesInInterval(start, end);
Laurent Montel's avatar
Laurent Montel committed
210
            for (const auto &dt : list) {
Laurent Montel's avatar
Laurent Montel committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
                const auto utc = dt.toUTC();
                const Kolab::Period &period = addLocalPeriod(utc, duration.end(utc), start, end, allDay);
                if (period.isValid()) {
                    periods.push_back(period);
                }
            }
        } else {
            const Kolab::Period &period = addLocalPeriod(eventStart, eventEnd, start, end, allDay);
            if (period.isValid()) {
                periods.push_back(period);
            }
        }
        if (!periods.empty()) {
            Kolab::FreebusyPeriod period;
            period.setPeriods(periods);
            //TODO get busy type from event (out-of-office, tentative)
            period.setType(Kolab::FreebusyPeriod::Busy);
            period.setEvent(Kolab::Conversion::toStdString(event->uid()), Kolab::Conversion::toStdString(event->summary()), Kolab::Conversion::toStdString(event->location()));
            freebusyPeriods.push_back(period);
        }
    }

    Kolab::Freebusy freebusy;
Laurent Montel's avatar
Laurent Montel committed
234

Laurent Montel's avatar
Laurent Montel committed
235
236
237
238
239
    freebusy.setStart(Kolab::Conversion::fromDate(start, allDay));
    freebusy.setEnd(Kolab::Conversion::fromDate(end, allDay));
    freebusy.setPeriods(freebusyPeriods);
    freebusy.setUid(createUuid().toStdString());
    freebusy.setTimestamp(Kolab::Conversion::fromDate(QDateTime::currentDateTimeUtc(), false));
240
241
    if (!organizer.isEmpty()) {
        freebusy.setOrganizer(ContactReference(Kolab::ContactReference::EmailReference, Kolab::Conversion::toStdString(organizer.email()), Kolab::Conversion::toStdString(organizer.name())));
Laurent Montel's avatar
Laurent Montel committed
242
    }
Laurent Montel's avatar
Laurent Montel committed
243

Laurent Montel's avatar
Laurent Montel committed
244
245
246
    return freebusy;
}

Laurent Montel's avatar
Laurent Montel committed
247
Freebusy aggregateFreeBusy(const std::vector< Freebusy > &fbList, const std::string &organizerEmail, const std::string &organizerName, bool simple)
Laurent Montel's avatar
Laurent Montel committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
{
    std::vector <Kolab::FreebusyPeriod > periods;

    QDateTime start;
    QDateTime end;
    bool allDay = false;
    Q_FOREACH (const Freebusy &fb, fbList) {
        const QDateTime &tmpStart = Kolab::Conversion::toDate(fb.start());
        if (!start.isValid() || tmpStart < start) {
            start = tmpStart;
            allDay |= fb.start().isDateOnly();
        }
        const QDateTime &tmpEnd = Kolab::Conversion::toDate(fb.end());
        if (!end.isValid() || tmpEnd > end) {
            end = tmpEnd;
            allDay |= fb.start().isDateOnly();
        }

        Q_FOREACH (const Kolab::FreebusyPeriod &period, fb.periods()) {
            Kolab::FreebusyPeriod simplifiedPeriod;
            simplifiedPeriod.setPeriods(period.periods());
            simplifiedPeriod.setType(period.type());
            if (!simple) { //Don't copy and reset to avoid unintentional information leaking into simple lists
                simplifiedPeriod.setEvent(period.eventSummary(), period.eventUid(), period.eventLocation());
            }
            periods.push_back(simplifiedPeriod);
        }
    }
Laurent Montel's avatar
Laurent Montel committed
276

Laurent Montel's avatar
Laurent Montel committed
277
278
279
280
281
282
283
284
285
286
287
288
289
    Freebusy aggregateFB;

    aggregateFB.setStart(Kolab::Conversion::fromDate(start, allDay));
    aggregateFB.setEnd(Kolab::Conversion::fromDate(end, allDay));
    aggregateFB.setPeriods(periods);
    aggregateFB.setUid(createUuid().toStdString());
    aggregateFB.setTimestamp(Kolab::Conversion::fromDate(QDateTime::currentDateTimeUtc(), false));
    aggregateFB.setOrganizer(ContactReference(Kolab::ContactReference::EmailReference, organizerEmail, organizerName));
    return aggregateFB;
}

std::string toIFB(const Kolab::Freebusy &freebusy)
{
290
291
    KCalendarCore::FreeBusy::Ptr fb(new KCalendarCore::FreeBusy(Kolab::Conversion::toDate(freebusy.start()), Kolab::Conversion::toDate(freebusy.end())));
    KCalendarCore::FreeBusyPeriod::List list;
Laurent Montel's avatar
Laurent Montel committed
292
293
    Q_FOREACH (const Kolab::FreebusyPeriod &fbPeriod, freebusy.periods()) {
        Q_FOREACH (const Kolab::Period &p, fbPeriod.periods()) {
294
            KCalendarCore::FreeBusyPeriod period(Kolab::Conversion::toDate(p.start), Kolab::Conversion::toDate(p.end));
Laurent Montel's avatar
Laurent Montel committed
295
296
297
298
299
300
301
302
//             period.setSummary("summary"); Doesn't even work. X-SUMMARY is read though (just not written out)s
            //TODO
            list.append(period);
        }
    }
    fb->addPeriods(list);

    fb->setUid(QString::fromStdString(freebusy.uid()));
303
    fb->setOrganizer(KCalendarCore::Person(Conversion::fromStdString(freebusy.organizer().name()), Conversion::fromStdString(freebusy.organizer().email())));
Laurent Montel's avatar
Laurent Montel committed
304
305
    fb->setLastModified(Kolab::Conversion::toDate(freebusy.timestamp()));

306
    KCalendarCore::ICalFormat format;
Laurent Montel's avatar
Laurent Montel committed
307
    format.setApplication(QStringLiteral("libkolab"), QStringLiteral(LIBKOLAB_LIB_VERSION_STRING));
308
    QString data = format.createScheduleMessage(fb, KCalendarCore::iTIPPublish);
Laurent Montel's avatar
Laurent Montel committed
309
310
    return Conversion::toStdString(data);
}
Laurent Montel's avatar
Laurent Montel committed
311
}
Laurent Montel's avatar
Laurent Montel committed
312
}