CalendarSupport

freebusycalendar.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 *
6 */
7
8#include "freebusycalendar.h"
9#include "calendarsupport_debug.h"
10
11#include <KCalendarCore/FreeBusyPeriod>
12#include <KCalendarCore/MemoryCalendar>
13
14#include <KLocalizedString>
15
16#include <QTimeZone>
17
18using namespace CalendarSupport;
19
20class CalendarSupport::FreeBusyCalendarPrivate
21{
22public:
23 FreeBusyCalendarPrivate() = default;
24
25 FreeBusyItemModel *mModel = nullptr;
28};
29
31 : QObject(parent)
32 , d(new CalendarSupport::FreeBusyCalendarPrivate)
33{
35 qCDebug(CALENDARSUPPORT_LOG) << "creating" << this;
36}
37
38FreeBusyCalendar::~FreeBusyCalendar()
39{
40 qCDebug(CALENDARSUPPORT_LOG) << "deleting" << this;
41}
42
44{
45 return d->mCalendar;
46}
47
48FreeBusyItemModel *FreeBusyCalendar::model() const
49{
50 return d->mModel;
51}
52
53void FreeBusyCalendar::setModel(FreeBusyItemModel *model)
54{
55 if (model != d->mModel) {
56 if (d->mModel) {
57 disconnect(d->mModel, nullptr, nullptr, nullptr);
58 }
59 d->mModel = model;
60 connect(d->mModel, &QAbstractItemModel::layoutChanged, this, &FreeBusyCalendar::onLayoutChanged);
61 connect(d->mModel, &QAbstractItemModel::modelReset, this, &FreeBusyCalendar::onLayoutChanged);
62 connect(d->mModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &FreeBusyCalendar::onRowsRemoved);
63 connect(d->mModel, &QAbstractItemModel::rowsInserted, this, &FreeBusyCalendar::onRowsInserted);
64 connect(d->mModel, &QAbstractItemModel::dataChanged, this, &FreeBusyCalendar::onRowsChanged);
65 }
66}
67
68void FreeBusyCalendar::deleteAllEvents()
69{
70 const KCalendarCore::Event::List lstEvents = d->mCalendar->events();
72 d->mCalendar->deleteEvent(event);
73 }
74}
75
76void FreeBusyCalendar::onLayoutChanged()
77{
78 if (!d->mFbEvent.isEmpty()) {
79 deleteAllEvents();
80 d->mFbEvent.clear();
81 for (int i = d->mModel->rowCount() - 1; i >= 0; --i) {
82 QModelIndex parent = d->mModel->index(i, 0);
83 onRowsInserted(parent, 0, d->mModel->rowCount(parent) - 1);
84 }
85 }
86}
87
88void FreeBusyCalendar::onRowsInserted(const QModelIndex &parent, int first, int last)
89{
90 if (!parent.isValid()) {
91 return;
92 }
93 for (int i = first; i <= last; ++i) {
94 QModelIndex index = d->mModel->index(i, 0, parent);
95
96 const KCalendarCore::FreeBusyPeriod &period = d->mModel->data(index, FreeBusyItemModel::FreeBusyPeriodRole).value<KCalendarCore::FreeBusyPeriod>();
97 const KCalendarCore::FreeBusy::Ptr &fb = d->mModel->data(parent, FreeBusyItemModel::FreeBusyRole).value<KCalendarCore::FreeBusy::Ptr>();
98
100 inc->setDtStart(period.start());
101 inc->setDtEnd(period.end());
102 inc->setUid(QLatin1StringView("fb-") + fb->uid() + QLatin1StringView("-") + QString::number(i));
103
104 inc->setCustomProperty("FREEBUSY", "STATUS", QString::number(period.type()));
105 QString summary = period.summary();
106 if (summary.isEmpty()) {
107 switch (period.type()) {
108 case KCalendarCore::FreeBusyPeriod::Free:
109 summary = i18n("Free");
110 break;
111 case KCalendarCore::FreeBusyPeriod::Busy:
112 summary = i18n("Busy");
113 break;
114 case KCalendarCore::FreeBusyPeriod::BusyUnavailable:
115 summary = i18n("Unavailable");
116 break;
117 case KCalendarCore::FreeBusyPeriod::BusyTentative:
118 summary = i18n("Tentative");
119 break;
120 default:
121 summary = i18n("Unknown");
122 }
123 }
124 inc->setSummary(summary);
125
126 d->mFbEvent.insert(index, inc);
127 d->mCalendar->addEvent(inc);
128 }
129}
130
131void FreeBusyCalendar::onRowsRemoved(const QModelIndex &parent, int first, int last)
132{
133 if (!parent.isValid()) {
134 for (int i = first; i <= last; ++i) {
135 QModelIndex index = d->mModel->index(i, 0);
136 onRowsRemoved(index, 0, d->mModel->rowCount(index) - 1);
137 }
138 } else {
139 for (int i = first; i <= last; ++i) {
140 QModelIndex index = d->mModel->index(i, 0, parent);
141 KCalendarCore::Event::Ptr inc = d->mFbEvent.take(index);
142 d->mCalendar->deleteEvent(inc);
143 }
144 }
145}
146
147void FreeBusyCalendar::onRowsChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
148{
149 if (!topLeft.parent().isValid()) {
150 return;
151 }
152 for (int i = topLeft.row(); i <= bottomRight.row(); ++i) {
153 QModelIndex index = d->mModel->index(i, 0, topLeft.parent());
154 KCalendarCore::Event::Ptr inc = d->mFbEvent.value(index);
155 d->mCalendar->beginChange(inc);
156 d->mCalendar->endChange(inc);
157 }
158}
159
160#include "moc_freebusycalendar.cpp"
FreeBusyCalendar(QObject *parent=nullptr)
Constructor.
void setModel(FreeBusyItemModel *model)
Set the FreeBusyItemModel used by the FreeBusyCalendar.
KCalendarCore::Calendar::Ptr calendar() const
Get the calendar created from the FreeBusyItemModel.
FreeBusyItemModel * model() const
Get the FreeBusyItemModel used by the FreeBusyCalendar.
QSharedPointer< Calendar > Ptr
QSharedPointer< Event > Ptr
FreeBusyType type() const
QDateTime end() const
QDateTime start() const
QString i18n(const char *text, const TYPE &arg...)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
void rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
void rowsInserted(const QModelIndex &parent, int first, int last)
bool isValid() const const
QModelIndex parent() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
virtual bool event(QEvent *e)
QObject * parent() const const
T qobject_cast(QObject *object)
bool isEmpty() const const
QString number(double n, char format, int precision)
QTimeZone systemTimeZone()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.