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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*
This file is part of the kcalcore library.
SPDX-FileCopyrightText: 2001 Cornelius Schumacher <schumacher@kde.org>
SPDX-FileCopyrightText: 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
/**
@file
This file is part of the API for handling calendar data and
defines the FreeBusy class.
@brief
Provides information about the free/busy time of a calendar user.
@author Cornelius Schumacher \<schumacher@kde.org\>
@author Reinhold Kainhofer \<reinhold@kainhofer.com\>
*/
#include "freebusy.h"
#include "utils_p.h"
#include "visitor.h"
#include "icalformat.h"
#include "incidencebase_p.h"
#include "kcalendarcore_debug.h"
#include <QTime>
using namespace KCalendarCore;
//@cond PRIVATE
class KCalendarCore::FreeBusyPrivate : public IncidenceBasePrivate
{
public:
FreeBusyPrivate() = default;
FreeBusyPrivate(const FreeBusyPrivate &other) = default;
FreeBusyPrivate(const FreeBusyPeriod::List &busyPeriods)
: IncidenceBasePrivate()
, mBusyPeriods(busyPeriods)
{
}
void init(const FreeBusyPrivate &other);
void init(const Event::List &events, const QDateTime &start, const QDateTime &end);
QDateTime mDtEnd; // end datetime
FreeBusyPeriod::List mBusyPeriods; // list of periods
// This is used for creating a freebusy object for the current user
bool addLocalPeriod(const QDateTime &eventStart, const QDateTime &eventEnd);
void sortBusyPeriods()
{
std::sort(mBusyPeriods.begin(), mBusyPeriods.end());
}
};
void FreeBusyPrivate::init(const FreeBusyPrivate &other)
{
mDtEnd = other.mDtEnd;
mBusyPeriods = other.mBusyPeriods;
}
//@endcond
FreeBusy::FreeBusy()
: IncidenceBase(new FreeBusyPrivate())
{
}
FreeBusy::FreeBusy(const FreeBusy &other)
: IncidenceBase(other, new FreeBusyPrivate(*other.d_func()))
{
}
FreeBusy::FreeBusy(const QDateTime &start, const QDateTime &end)
: FreeBusy()
{
setDtStart(start); // NOLINT false clang-analyzer-optin.cplusplus.VirtualCall
setDtEnd(end); // NOLINT false clang-analyzer-optin.cplusplus.VirtualCall
}
FreeBusy::FreeBusy(const Event::List &events, const QDateTime &start, const QDateTime &end)
: FreeBusy()
{
setDtStart(start); // NOLINT false clang-analyzer-optin.cplusplus.VirtualCall
setDtEnd(end); // NOLINT false clang-analyzer-optin.cplusplus.VirtualCall
Q_D(FreeBusy);
d->init(events, start, end);
}
//@cond PRIVATE
void FreeBusyPrivate::init(const Event::List &eventList, const QDateTime &start, const QDateTime &end)
{
const qint64 duration = start.daysTo(end);
QDate day;
QDateTime tmpStart;
QDateTime tmpEnd;
// Loops through every event in the calendar
for (auto event : eventList) {
// If this event is transparent it shouldn't be in the freebusy list.
if (event->transparency() == Event::Transparent) {
continue;
}
// The code below can not handle all-day events. Fixing this resulted
// in a lot of duplicated code. Instead, make a copy of the event and
// set the period to the full day(s). This trick works for recurring,
// multiday, and single day all-day events.
Event::Ptr allDayEvent;
if (event->allDay()) {
// addDay event. Do the hack
qCDebug(KCALCORE_LOG) << "All-day event";
allDayEvent = Event::Ptr(new Event(*event));
// Set the start and end times to be on midnight
QDateTime st = allDayEvent->dtStart();
st.setTime(QTime(0, 0));
QDateTime nd = allDayEvent->dtEnd();
nd.setTime(QTime(23, 59, 59, 999));
allDayEvent->setAllDay(false);
allDayEvent->setDtStart(st);
allDayEvent->setDtEnd(nd);
qCDebug(KCALCORE_LOG) << "Use:" << st.toString() << "to" << nd.toString();
// Finally, use this event for the setting below
event = allDayEvent;
}
// This whole for loop is for recurring events, it loops through
// each of the days of the freebusy request
for (qint64 i = 0; i <= duration; ++i) {
day = start.addDays(i).date();
tmpStart.setDate(day);
tmpEnd.setDate(day);
if (event->recurs()) {
if (event->isMultiDay()) {
// FIXME: This doesn't work for sub-daily recurrences or recurrences with
// a different time than the original event.
const qint64 extraDays = event->dtStart().daysTo(event->dtEnd());
for (qint64 x = 0; x <= extraDays; ++x) {
if (event->recursOn(day.addDays(-x), start.timeZone())) {
tmpStart.setDate(day.addDays(-x));
tmpStart.setTime(event->dtStart().time());
tmpEnd = event->duration().end(tmpStart);
addLocalPeriod(tmpStart, tmpEnd);
break;
}
}
} else {
if (event->recursOn(day, start.timeZone())) {
tmpStart.setTime(event->dtStart().time());
tmpEnd.setTime(event->dtEnd().time());
addLocalPeriod(tmpStart, tmpEnd);
}
}
}
}
// Non-recurring events
addLocalPeriod(event->dtStart(), event->dtEnd());
}
sortBusyPeriods();
}
//@endcond
FreeBusy::FreeBusy(const Period::List &busyPeriods)
: IncidenceBase(new FreeBusyPrivate())
{
addPeriods(busyPeriods);
}
FreeBusy::FreeBusy(const FreeBusyPeriod::List &busyPeriods)
: IncidenceBase(new FreeBusyPrivate(busyPeriods))
{
}
FreeBusy::~FreeBusy() = default;
IncidenceBase::IncidenceType FreeBusy::type() const
{
return TypeFreeBusy;
}
QByteArray FreeBusy::typeStr() const
{
return QByteArrayLiteral("FreeBusy");
}
void FreeBusy::setDtStart(const QDateTime &start)
{
IncidenceBase::setDtStart(start.toUTC());
}
void FreeBusy::setDtEnd(const QDateTime &end)
{
Q_D(FreeBusy);
update();
d->mDtEnd = end;
setFieldDirty(FieldDtEnd);
updated();
}
QDateTime FreeBusy::dtEnd() const
{
Q_D(const FreeBusy);
return d->mDtEnd;
}
Period::List FreeBusy::busyPeriods() const
{
Period::List res;
Q_D(const FreeBusy);
res.reserve(d->mBusyPeriods.count());
for (const FreeBusyPeriod &p : std::as_const(d->mBusyPeriods)) {
res << p;
}
return res;
}
FreeBusyPeriod::List FreeBusy::fullBusyPeriods() const
{
Q_D(const FreeBusy);
return d->mBusyPeriods;
}
void FreeBusy::sortList()
{
Q_D(FreeBusy);
d->sortBusyPeriods();
}
void FreeBusy::addPeriods(const Period::List &list)
{
Q_D(FreeBusy);
d->mBusyPeriods.reserve(d->mBusyPeriods.count() + list.count());
for (const Period &p : std::as_const(list)) {
d->mBusyPeriods << FreeBusyPeriod(p);
}
sortList();
}
void FreeBusy::addPeriods(const FreeBusyPeriod::List &list)
{
Q_D(FreeBusy);
d->mBusyPeriods += list;
sortList();
}
void FreeBusy::addPeriod(const QDateTime &start, const QDateTime &end)
{
Q_D(FreeBusy);
d->mBusyPeriods.append(FreeBusyPeriod(start, end));
sortList();
}
void FreeBusy::addPeriod(const QDateTime &start, const Duration &duration)
{
Q_D(FreeBusy);
d->mBusyPeriods.append(FreeBusyPeriod(start, duration));
sortList();
}
void FreeBusy::merge(const FreeBusy::Ptr &freeBusy)
{
if (freeBusy->dtStart() < dtStart()) {
setDtStart(freeBusy->dtStart());
}
if (freeBusy->dtEnd() > dtEnd()) {
setDtEnd(freeBusy->dtEnd());
}
Q_D(FreeBusy);
const Period::List periods = freeBusy->busyPeriods();
d->mBusyPeriods.reserve(d->mBusyPeriods.count() + periods.count());
for (const auto &p : periods) {
d->mBusyPeriods.append(FreeBusyPeriod(p.start(), p.end()));
}
sortList();
}
void FreeBusy::shiftTimes(const QTimeZone &oldZone, const QTimeZone &newZone)
{
Q_D(FreeBusy);
if (oldZone.isValid() && newZone.isValid() && oldZone != newZone) {
IncidenceBase::shiftTimes(oldZone, newZone);
update();
d->mDtEnd = d->mDtEnd.toTimeZone(oldZone);
d->mDtEnd.setTimeZone(newZone);
for (FreeBusyPeriod p : std::as_const(d->mBusyPeriods)) {
p.shiftTimes(oldZone, newZone);
}
setFieldDirty(FieldDtEnd);
updated();
}
}
IncidenceBase &FreeBusy::assign(const IncidenceBase &other)
{
Q_D(FreeBusy);
if (&other != this) {
IncidenceBase::assign(other);
const FreeBusy *f = static_cast<const FreeBusy *>(&other);
d->init(*(f->d_func()));
}
return *this;
}
bool FreeBusy::equals(const IncidenceBase &freeBusy) const
{
if (!IncidenceBase::equals(freeBusy)) {
return false;
} else {
Q_D(const FreeBusy);
// If they weren't the same type IncidenceBase::equals would had returned false already
const FreeBusy *fb = static_cast<const FreeBusy *>(&freeBusy);
return identical(dtEnd(), fb->dtEnd()) && d->mBusyPeriods == fb->d_func()->mBusyPeriods;
}
}
bool FreeBusy::accept(Visitor &v, const IncidenceBase::Ptr &incidence)
{
return v.visit(incidence.staticCast<FreeBusy>());
}
QDateTime FreeBusy::dateTime(DateTimeRole role) const
{
Q_UNUSED(role);
// No roles affecting freeBusy yet
return QDateTime();
}
void FreeBusy::setDateTime(const QDateTime &dateTime, DateTimeRole role)
{
Q_UNUSED(dateTime);
Q_UNUSED(role);
}
void FreeBusy::virtual_hook(VirtualHook id, void *data)
{
Q_UNUSED(id);
Q_UNUSED(data);
Q_ASSERT(false);
}
//@cond PRIVATE
bool FreeBusyPrivate::addLocalPeriod(const QDateTime &eventStart, const QDateTime &eventEnd)
{
QDateTime tmpStart;
QDateTime tmpEnd;
// Check to see if the start *or* end of the event is
// between the start and end of the freebusy dates.
QDateTime start = mDtStart;
if (!(((start.secsTo(eventStart) >= 0) && (eventStart.secsTo(mDtEnd) >= 0)) || ((start.secsTo(eventEnd) >= 0) && (eventEnd.secsTo(mDtEnd) >= 0)))) {
return false;
}
if (eventStart.secsTo(start) >= 0) {
tmpStart = start;
} else {
tmpStart = eventStart;
}
if (eventEnd.secsTo(mDtEnd) <= 0) {
tmpEnd = mDtEnd;
} else {
tmpEnd = eventEnd;
}
FreeBusyPeriod p(tmpStart, tmpEnd);
mBusyPeriods.append(p);
return true;
}
//@endcond
QLatin1String FreeBusy::mimeType() const
{
return FreeBusy::freeBusyMimeType();
}
QLatin1String KCalendarCore::FreeBusy::freeBusyMimeType()
{
return QLatin1String("application/x-vnd.akonadi.calendar.freebusy");
}
QDataStream &KCalendarCore::operator<<(QDataStream &stream, const KCalendarCore::FreeBusy::Ptr &freebusy)
{
KCalendarCore::ICalFormat format;
QString data = format.createScheduleMessage(freebusy, iTIPPublish);
return stream << data;
}
QDataStream &KCalendarCore::operator>>(QDataStream &stream, KCalendarCore::FreeBusy::Ptr &freebusy)
{
QString freeBusyVCal;
stream >> freeBusyVCal;
KCalendarCore::ICalFormat format;
freebusy = format.parseFreeBusy(freeBusyVCal);
if (!freebusy) {
qCDebug(KCALCORE_LOG) << "Error parsing free/busy";
qCDebug(KCALCORE_LOG) << freeBusyVCal;
}
return stream;
}
|