incidencemodel.cpp 10.5 KB
Newer Older
1
/*
2
 * SPDX-FileCopyrightText: 2020 Dimitris Kardarakos <dimkard@posteo.net>
3
 *
4
 * SPDX-License-Identifier: GPL-3.0-or-later
5 6 7 8 9 10 11 12
 */

#include "incidencemodel.h"
#include <KLocalizedString>
using namespace KCalendarCore;

IncidenceModel::IncidenceModel(QObject* parent) :
    QAbstractListModel(parent),
13
    m_filter_mode(FilterModes::Invalid),
14 15 16 17 18
    m_filter_dt(QDate()),
    m_filter_hour(-1),
    m_calendar(nullptr),
    m_incidences(Incidence::List())
{
19
    connect(this, &IncidenceModel::filterModeChanged, this, &IncidenceModel::loadIncidences);
20 21 22 23 24 25 26
    connect(this, &IncidenceModel::filterDtChanged, this, &IncidenceModel::loadIncidences);
    connect(this, &IncidenceModel::filterHourChanged, this, &IncidenceModel::loadIncidences);
    connect(this, &IncidenceModel::calendarChanged, this, &IncidenceModel::loadIncidences);
}

IncidenceModel::~IncidenceModel() = default;

27 28 29 30 31 32 33 34 35 36 37 38 39
int IncidenceModel::filterMode() const
{
    return m_filter_mode;
}

void IncidenceModel::setFilterMode(const int mode)
{
    m_filter_mode = mode;

    Q_EMIT filterModeChanged();
}


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
QDate IncidenceModel::filterDt() const
{
    return m_filter_dt;
}

void IncidenceModel::setFilterDt(const QDate& filterDate)
{
    m_filter_dt = filterDate;

    Q_EMIT filterDtChanged();
}

int IncidenceModel::filterHour() const
{
    return m_filter_hour;
}

void IncidenceModel::setFilterHour(const int hour)
{
    m_filter_hour = hour;

    Q_EMIT filterHourChanged();
}

LocalCalendar *IncidenceModel::calendar() const
{
    return m_calendar;
}

void IncidenceModel::setCalendar(LocalCalendar *calendarPtr)
{
    m_calendar = calendarPtr;

    connect(m_calendar, &LocalCalendar::eventsChanged, this, &IncidenceModel::loadIncidences);
    connect(m_calendar, &LocalCalendar::todosChanged, this, &IncidenceModel::loadIncidences);

    Q_EMIT calendarChanged();
}

QHash<int, QByteArray> IncidenceModel::roleNames() const
{
    return {
        { Uid, "uid" },
        { DtStart, "dtstart" },
        { AllDay, "allday" },
        { Description, "description" },
        { Summary, "summary" },
        { LastModified, "lastmodified" },
        { Location, "location" },
        { Categories, "categories" },
        { Priority, "priority" },
        { Created, "created" },
        { Secrecy, "secrecy" },
        { EndDate, "dtend" },
        { RepeatPeriodType, "repeatType" },
        { RepeatEvery, "repeatEvery" },
        { RepeatStopAfter, "repeatStopAfter" },
        { IsRepeating, "isRepeating" },
        { DisplayDate, "displayDate" },
        { DisplayTime, "displayTime" },
        { Completed, "completed" },
101 102
        { IncidenceType, "type" },
        { DisplayStartEndTime, "startEndTime" }
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
    };
}

QVariant IncidenceModel::data(const QModelIndex& index, int role) const
{
    if(!index.isValid())
        return QVariant();

    int row = index.row();
    auto type = m_incidences.at(row)->type();

    switch(role)
    {
        case Uid:
            return m_incidences.at(row)->uid();
        case DtStart:
            return m_incidences.at(row)->dtStart();
        case AllDay:
            return m_incidences.at(row)->allDay();
        case Description:
            return m_incidences.at(row)->description();
        case Summary:
            return m_incidences.at(row)->summary();
        case LastModified:
            return m_incidences.at(row)->lastModified();
        case Location:
            return m_incidences.at(row)->location();
        case Categories:
            return m_incidences.at(row)->categories();
        case Priority:
            return m_incidences.at(row)->priority();
        case Created:
            return m_incidences.at(row)->created();
        case Secrecy:
            return m_incidences.at(row)->secrecy();
        case EndDate:
            return (type == IncidenceBase::TypeEvent) ? m_incidences.at(row).dynamicCast<Event>()->dtEnd() : QDateTime();
        case RepeatPeriodType:
            return repeatPeriodType(row);
        case RepeatEvery:
            return repeatEvery(row);
        case RepeatStopAfter:
            return repeatStopAfter(row);
        case IsRepeating:
            return m_incidences.at(row)->recurs();
        case DisplayDate:
            return m_incidences.at(row)->dtStart().date().toString(Qt::SystemLocaleLongDate);
        case DisplayTime:
            return m_incidences.at(row)->allDay() ? i18n("All day") : m_incidences.at(row)->dtStart().time().toString("hh:mm");
152 153
        case Completed:
            return (type == IncidenceBase::TypeTodo) ? m_incidences.at(row).dynamicCast<Todo>()->isCompleted() : false;
154 155
        case IncidenceType:
            return type;
156 157
        case DisplayStartEndTime:
            return displayStartEndTime(row);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        default:
            return QVariant();
    }
}

int IncidenceModel::rowCount(const QModelIndex& parent) const
{
    if(parent.isValid())
        return 0;

    return m_incidences.count();
}

void IncidenceModel::loadIncidences()
{
    beginResetModel();
    m_incidences.clear();

    Event::List events;
    Todo::List todos;

179 180 181 182 183
    auto hourModelReady = (m_calendar != nullptr && m_filter_dt.isValid() && m_filter_hour >= 0);
    auto dayModelReady = (m_calendar != nullptr && m_filter_dt.isValid());
    auto allModelReady = (m_calendar != nullptr);

    switch(m_filter_mode)
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
        case FilterModes::HourIncidences:
        {
            m_incidences = (hourModelReady) ? hourIncidences() : m_incidences;
            break;
        }
        case FilterModes::HourEvents:
        {
            m_incidences = (hourModelReady) ? hourEvents() : m_incidences;
            break;
        }
        case FilterModes::HourTodos:
        {
            m_incidences = (hourModelReady) ? hourTodos() : m_incidences;
            break;
        }
        case FilterModes::DayIncidences:
        {
            m_incidences = (dayModelReady) ? dayIncidences() : m_incidences;
            break;
        }
        case FilterModes::DayEvents:
        {
            m_incidences = (dayModelReady) ? dayEvents() : m_incidences;
            break;
        }
        case FilterModes::DayTodos:
        {
            m_incidences = (dayModelReady) ? dayTodos() : m_incidences;
            break;
        }
        case FilterModes::AllIncidences:
        {
            m_incidences = (allModelReady) ? allIncidences() : m_incidences;
            break;
        }
        case FilterModes::AllEvents:
        {
            m_incidences = (allModelReady) ? allEvents() : m_incidences;
            break;
        }
        case FilterModes::AllTodos:
        {
            m_incidences = (allModelReady) ? allTodos() : m_incidences;
            break;
        }
        case Invalid:
            break;
        default:
            break;
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
    }

    endResetModel();
}

int IncidenceModel::repeatEvery(const int idx) const
{
    if(!(m_incidences.at(idx)->recurs())) return 0;

    return m_incidences.at(idx)->recurrence()->frequency();
}

int IncidenceModel::repeatStopAfter(const int idx) const
{

    if(!(m_incidences.at(idx)->recurs())) return -1;

    return m_incidences.at(idx)->recurrence()->duration();
}

ushort IncidenceModel::repeatPeriodType(const int idx) const
{
    if(!(m_incidences.at(idx)->recurs())) return Recurrence::rNone;

    return m_incidences.at(idx)->recurrence()->recurrenceType();
}

261
Incidence::List IncidenceModel::hourIncidences() const
262
{
263 264 265 266 267 268 269 270 271
    auto incidences = hourEvents();
    incidences.append(hourTodos());

    return incidences;
}

Incidence::List IncidenceModel::hourEvents() const
{
    Incidence::List incidences;
272 273
    auto filterStartDtTime = QDateTime(m_filter_dt).addSecs(m_filter_hour * 3600);
    auto filterEndtDtTime = QDateTime(m_filter_dt).addSecs(m_filter_hour * 3600 + 3599);
274
    auto dayEventList = dayEvents();
275

276
    for(const auto & d : dayEventList)
277
    {
278
        auto e = d.dynamicCast<Event>();
279

280 281 282
        auto eventStartWithinFilter = e->dtStart() >= filterStartDtTime && e->dtStart() <= filterEndtDtTime;
        auto eventEndWithinFilter = e->dtEnd() > filterStartDtTime && e->dtEnd() <= filterEndtDtTime;
        auto filterWithinEvent =  e->dtStart() < filterStartDtTime && filterEndtDtTime < e->dtEnd();
283

284
        if((eventStartWithinFilter || eventEndWithinFilter || filterWithinEvent))
285
        {
286
            incidences.append(e);
287 288 289 290
        }

    }

291 292 293 294 295 296 297 298 299
    return incidences;
}

Incidence::List IncidenceModel::hourTodos() const
{
    Incidence::List incidences;
    auto dayTodoList = dayTodos();

    for(const auto & t : dayTodoList)
300 301 302 303
    {
        auto k = t->allDay() ? 0 : t->dtStart().time().hour();
        if(k == m_filter_hour || t->allDay())
        {
304
            incidences.append(t);
305 306 307
        }
    }

308
    return incidences;
309 310
}

311 312 313 314 315 316 317 318 319 320
Incidence::List IncidenceModel::dayIncidences() const
{
    auto incidences = dayEvents();
    incidences.append(dayTodos());

    return incidences;
}

Incidence::List IncidenceModel::dayEvents() const
{
321
    auto events = m_calendar->memorycalendar()->rawEventsForDate(m_filter_dt, {}, EventSortStartDate, SortDirectionAscending);
322 323 324 325 326 327

    return toIncidences(events);
}

Incidence::List IncidenceModel::dayTodos() const
{
328
    auto todos =  m_calendar->memorycalendar()->rawTodos(m_filter_dt, m_filter_dt);
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

    return toIncidences(todos);
}

Incidence::List IncidenceModel::allIncidences() const
{
    auto incidences = m_calendar->memorycalendar()->rawIncidences();

    return incidences;
}

Incidence::List IncidenceModel::allTodos() const
{
    auto todos =  m_calendar->memorycalendar()->rawTodos(TodoSortStartDate, SortDirectionDescending);

    return toIncidences(todos);
}

Incidence::List IncidenceModel::allEvents() const
{
    auto events = m_calendar->memorycalendar()->rawEvents(EventSortStartDate, SortDirectionDescending);

    return toIncidences(events);
}

Incidence::List IncidenceModel::toIncidences(const Event::List& eventList) const
{
    Incidence::List incidences;

    for(const auto & e : eventList)
    {
        incidences.append(e.dynamicCast<Incidence>());
    }
362

363 364 365 366 367 368
    return incidences;
}

Incidence::List IncidenceModel::toIncidences(const Todo::List& todoList) const
{
    Incidence::List incidences;
369

370 371 372 373 374 375 376
    for(const auto & e : todoList)
    {
        incidences.append(e.dynamicCast<Incidence>());
    }

    return incidences;
}
377

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
QString IncidenceModel::displayStartEndTime(const int idx) const
{
    auto incidence = m_incidences.at(idx);

    if(incidence->allDay())
    {
        return QString();
    }

    if(incidence->type() == IncidenceBase::TypeEvent)
    {
        return QString("%1 - %2").arg(incidence->dtStart().time().toString("hh:mm")).arg(incidence.dynamicCast<Event>()->dtEnd().time().toString("hh:mm"));
    }

    return incidence->dtStart().time().toString("hh:mm");
}