eventdatavisitor.cpp 6.25 KB
Newer Older
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
/*
 * Copyright (C) 2016  Daniel Vrátil <dvratil@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include "eventdatavisitor.h"
#include "pimeventsplugin_debug.h"

#include <Akonadi/Calendar/ETMCalendar>

BaseEventDataVisitor::BaseEventDataVisitor(Akonadi::ETMCalendar *calendar,
                                           const QDate &start, const QDate &end)
    : mCalendar(calendar)
    , mStart(start)
    , mEnd(end)
{
}

BaseEventDataVisitor::~BaseEventDataVisitor()
{
}

bool BaseEventDataVisitor::act(const KCalCore::Incidence::Ptr &incidence)
{
    return incidence->accept(*this, incidence);
}

bool BaseEventDataVisitor::act(const KCalCore::Event::List &events)
{
    bool ok = false;
    Q_FOREACH (const KCalCore::Event::Ptr &event, events) {
        ok = event.staticCast<KCalCore::IncidenceBase>()->accept(*this, event) || ok;
    }
    return ok;
}
bool BaseEventDataVisitor::act(const KCalCore::Todo::List &todos)
{
    bool ok = false;
    Q_FOREACH (const KCalCore::Todo::Ptr &todo, todos) {
        ok = todo.staticCast<KCalCore::IncidenceBase>()->accept(*this, todo) || ok;
    }
    return ok;
}

bool BaseEventDataVisitor::isInRange(const QDate &start, const QDate &end) const
{
    if (!mStart.isValid() || !mEnd.isValid()) {
        return true;
    }

    if (!end.isValid() && start >= mStart && start <= mEnd) {
        return true;
    } else if (start < mStart) {
        return end >= mStart;
    } else if (end > mEnd) {
        return start <= mEnd;
    } else {
        return true;
    }
}

QString BaseEventDataVisitor::generateUid(const KCalCore::Incidence::Ptr &incidence,
                                          const KDateTime &recurrenceId) const
{
    // Get a corresponding Akonadi Item: Akonadi ID is the only reliably unique
    // and persistent identifier when dealing with incidences from multiple
    // calendars
    const Akonadi::Item item = mCalendar->item(incidence);
    if (!item.isValid()) {
        // Can this happen? What do we do now?!
        return QString();
    }

    if (recurrenceId.isValid()) {
        return QStringLiteral("Akonadi-%1-%2").arg(item.id())
                                              .arg(recurrenceId.toString(QStringLiteral("%Y%m%dT%H%M%S%Z")));
    } else {
        return QStringLiteral("Akonadi-%1").arg(item.id());
    }
}



EventDataVisitor::EventDataVisitor(Akonadi::ETMCalendar *calendar,
                                   const QDate &start, const QDate &end)
    : BaseEventDataVisitor(calendar, start , end)
{
}

EventDataVisitor::~EventDataVisitor()
{
}


QMultiHash<QDate, CalendarEvents::EventData> EventDataVisitor::results() const
{
    return mResults;
}


bool EventDataVisitor::visit(const KCalCore::Event::Ptr &event)
{
    CalendarEvents::EventData data = incidenceData(event);
    data.setEventType(CalendarEvents::EventData::Event);
    if (event->recurs()) {
        return explodeIncidenceOccurences(data, event);
    } else if (isInRange(event->dtStart().date(), event->dtEnd().date())) {
        data.setStartDateTime(event->dtStart().dateTime());
        data.setEndDateTime(event->dtEnd().dateTime());
        mResults.insert(data.startDateTime().date(), data);
        return true;
    }

    return false;
}

bool EventDataVisitor::visit(const KCalCore::Todo::Ptr &todo)
{
    CalendarEvents::EventData data = incidenceData(todo);
    data.setEventType(CalendarEvents::EventData::Todo);
    if (todo->recurs()) {
        return explodeIncidenceOccurences(data, todo);
    } else if (isInRange(todo->dtStart().date(), todo->dtDue().date())) {
        data.setStartDateTime(todo->dtStart().dateTime());
        data.setEndDateTime(todo->dtDue().dateTime());
        mResults.insert(data.startDateTime().date(), data);
        return true;
    }

    return false;
}

CalendarEvents::EventData EventDataVisitor::incidenceData(const KCalCore::Incidence::Ptr &incidence) const
{
    CalendarEvents::EventData data;
    data.setTitle(incidence->summary());
    data.setDescription(incidence->description());
    data.setIsAllDay(incidence->allDay());
    data.setIsMinor(false);
    data.setUid(generateUid(incidence));
    // TODO: Set calendar color
    return data;
}

bool EventDataVisitor::explodeIncidenceOccurences(const CalendarEvents::EventData &ed,
                                                  const KCalCore::Incidence::Ptr &incidence)
{
    Q_ASSERT(incidence->recurs());

    const qint64 duration = ed.startDateTime().secsTo(ed.endDateTime());

    KDateTime rec(mStart.addDays(-1), QTime(0, 0, 0));
    rec = incidence->recurrence()->getNextDateTime(rec);
    while (rec.isValid() && rec.date() <= mEnd) {
        CalendarEvents::EventData copy = ed;
        const QDateTime dt = rec.dateTime();
        copy.setStartDateTime(dt);
        // TODO: Is there a better way to find when an instance ends without
        // going through the expensive lookup in Incidence::instance(uid, recurrenceId)?
        copy.setEndDateTime(dt.addSecs(duration));
        copy.setUid(generateUid(incidence, rec));
        mResults.insert(dt.date(), copy);

        rec = incidence->recurrence()->getNextDateTime(rec);
    }

    return true;
}


EventDataIdVisitor::EventDataIdVisitor(Akonadi::ETMCalendar *calendar, const QDate &start, const QDate &end)
    : BaseEventDataVisitor(calendar, start, end)
{
}

QStringList EventDataIdVisitor::results() const
{
    return mResults;
}

bool EventDataIdVisitor::visit(const KCalCore::Event::Ptr &event)
{
    mResults.push_back(generateUid(event, event->recurrenceId()));
    return true;
}

bool EventDataIdVisitor::visit(const KCalCore::Todo::Ptr &todo)
{
    mResults.push_back(generateUid(todo, todo->recurrenceId()));
    return true;
}