Changeset View
Changeset View
Standalone View
Standalone View
src/freebusymodel/freebusycalendar.cpp
Show All 16 Lines | |||||
17 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
19 | * | 19 | * | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "freebusycalendar.h" | 22 | #include "freebusycalendar.h" | ||
23 | #include "calendarsupport_debug.h" | 23 | #include "calendarsupport_debug.h" | ||
24 | 24 | | |||
25 | #include <KCalCore/Event> | 25 | #include <KCalendarCore/Event> | ||
26 | #include <KCalCore/FreeBusyPeriod> | 26 | #include <KCalendarCore/FreeBusyPeriod> | ||
27 | #include <KCalCore/MemoryCalendar> | 27 | #include <KCalendarCore/MemoryCalendar> | ||
28 | #include <KCalCore/CalFormat> | 28 | #include <KCalendarCore/CalFormat> | ||
29 | 29 | | |||
30 | #include <KLocalizedString> | 30 | #include <KLocalizedString> | ||
31 | 31 | | |||
32 | #include <QTimeZone> | 32 | #include <QTimeZone> | ||
33 | 33 | | |||
34 | using namespace CalendarSupport; | 34 | using namespace CalendarSupport; | ||
35 | 35 | | |||
36 | class CalendarSupport::FreeBusyCalendarPrivate | 36 | class CalendarSupport::FreeBusyCalendarPrivate | ||
37 | { | 37 | { | ||
38 | public: | 38 | public: | ||
39 | FreeBusyCalendarPrivate() | 39 | FreeBusyCalendarPrivate() | ||
40 | { | 40 | { | ||
41 | } | 41 | } | ||
42 | 42 | | |||
43 | FreeBusyItemModel *mModel = nullptr; | 43 | FreeBusyItemModel *mModel = nullptr; | ||
44 | KCalCore::Calendar::Ptr mCalendar; | 44 | KCalendarCore::Calendar::Ptr mCalendar; | ||
45 | QMap<QModelIndex, KCalCore::Event::Ptr> mFbEvent; | 45 | QMap<QModelIndex, KCalendarCore::Event::Ptr> mFbEvent; | ||
46 | }; | 46 | }; | ||
47 | 47 | | |||
48 | FreeBusyCalendar::FreeBusyCalendar(QObject *parent) | 48 | FreeBusyCalendar::FreeBusyCalendar(QObject *parent) | ||
49 | : QObject(parent) | 49 | : QObject(parent) | ||
50 | , d(new CalendarSupport::FreeBusyCalendarPrivate) | 50 | , d(new CalendarSupport::FreeBusyCalendarPrivate) | ||
51 | { | 51 | { | ||
52 | d->mCalendar | 52 | d->mCalendar | ||
53 | = KCalCore::Calendar::Ptr(new KCalCore::MemoryCalendar(QTimeZone::systemTimeZone())); | 53 | = KCalendarCore::Calendar::Ptr(new KCalendarCore::MemoryCalendar(QTimeZone::systemTimeZone())); | ||
54 | qCDebug(CALENDARSUPPORT_LOG) << "creating" << this; | 54 | qCDebug(CALENDARSUPPORT_LOG) << "creating" << this; | ||
55 | } | 55 | } | ||
56 | 56 | | |||
57 | FreeBusyCalendar::~FreeBusyCalendar() | 57 | FreeBusyCalendar::~FreeBusyCalendar() | ||
58 | { | 58 | { | ||
59 | qCDebug(CALENDARSUPPORT_LOG) << "deleting" << this; | 59 | qCDebug(CALENDARSUPPORT_LOG) << "deleting" << this; | ||
60 | delete d; | 60 | delete d; | ||
61 | } | 61 | } | ||
62 | 62 | | |||
63 | KCalCore::Calendar::Ptr FreeBusyCalendar::calendar() const | 63 | KCalendarCore::Calendar::Ptr FreeBusyCalendar::calendar() const | ||
64 | { | 64 | { | ||
65 | return d->mCalendar; | 65 | return d->mCalendar; | ||
66 | } | 66 | } | ||
67 | 67 | | |||
68 | FreeBusyItemModel *FreeBusyCalendar::model() const | 68 | FreeBusyItemModel *FreeBusyCalendar::model() const | ||
69 | { | 69 | { | ||
70 | return d->mModel; | 70 | return d->mModel; | ||
71 | } | 71 | } | ||
Show All 15 Lines | 86 | connect(d->mModel, &QAbstractItemModel::rowsInserted, | |||
87 | this, &FreeBusyCalendar::onRowsInserted); | 87 | this, &FreeBusyCalendar::onRowsInserted); | ||
88 | connect(d->mModel, &QAbstractItemModel::dataChanged, | 88 | connect(d->mModel, &QAbstractItemModel::dataChanged, | ||
89 | this, &FreeBusyCalendar::onRowsChanged); | 89 | this, &FreeBusyCalendar::onRowsChanged); | ||
90 | } | 90 | } | ||
91 | } | 91 | } | ||
92 | 92 | | |||
93 | void FreeBusyCalendar::deleteAllEvents() | 93 | void FreeBusyCalendar::deleteAllEvents() | ||
94 | { | 94 | { | ||
95 | const KCalCore::Event::List lstEvents = d->mCalendar->events(); | 95 | const KCalendarCore::Event::List lstEvents = d->mCalendar->events(); | ||
96 | for (const KCalCore::Event::Ptr &event : lstEvents) { | 96 | for (const KCalendarCore::Event::Ptr &event : lstEvents) { | ||
97 | d->mCalendar->deleteEvent(event); | 97 | d->mCalendar->deleteEvent(event); | ||
98 | } | 98 | } | ||
99 | } | 99 | } | ||
100 | 100 | | |||
101 | void FreeBusyCalendar::onLayoutChanged() | 101 | void FreeBusyCalendar::onLayoutChanged() | ||
102 | { | 102 | { | ||
103 | if (!d->mFbEvent.isEmpty()) { | 103 | if (!d->mFbEvent.isEmpty()) { | ||
104 | deleteAllEvents(); | 104 | deleteAllEvents(); | ||
105 | d->mFbEvent.clear(); | 105 | d->mFbEvent.clear(); | ||
106 | for (int i = d->mModel->rowCount() - 1; i >= 0; --i) { | 106 | for (int i = d->mModel->rowCount() - 1; i >= 0; --i) { | ||
107 | QModelIndex parent = d->mModel->index(i, 0); | 107 | QModelIndex parent = d->mModel->index(i, 0); | ||
108 | onRowsInserted(parent, 0, d->mModel->rowCount(parent) - 1); | 108 | onRowsInserted(parent, 0, d->mModel->rowCount(parent) - 1); | ||
109 | } | 109 | } | ||
110 | } | 110 | } | ||
111 | } | 111 | } | ||
112 | 112 | | |||
113 | void FreeBusyCalendar::onRowsInserted(const QModelIndex &parent, int first, int last) | 113 | void FreeBusyCalendar::onRowsInserted(const QModelIndex &parent, int first, int last) | ||
114 | { | 114 | { | ||
115 | if (!parent.isValid()) { | 115 | if (!parent.isValid()) { | ||
116 | return; | 116 | return; | ||
117 | } | 117 | } | ||
118 | for (int i = first; i <= last; ++i) { | 118 | for (int i = first; i <= last; ++i) { | ||
119 | QModelIndex index = d->mModel->index(i, 0, parent); | 119 | QModelIndex index = d->mModel->index(i, 0, parent); | ||
120 | 120 | | |||
121 | const KCalCore::FreeBusyPeriod &period = d->mModel->data(index, | 121 | const KCalendarCore::FreeBusyPeriod &period = d->mModel->data(index, | ||
122 | FreeBusyItemModel::FreeBusyPeriodRole) | 122 | FreeBusyItemModel::FreeBusyPeriodRole) | ||
123 | .value<KCalCore::FreeBusyPeriod>(); | 123 | .value<KCalendarCore::FreeBusyPeriod>(); | ||
124 | const KCalCore::FreeBusy::Ptr &fb | 124 | const KCalendarCore::FreeBusy::Ptr &fb | ||
125 | = d->mModel->data(parent, | 125 | = d->mModel->data(parent, | ||
126 | FreeBusyItemModel::FreeBusyRole).value<KCalCore::FreeBusy::Ptr>(); | 126 | FreeBusyItemModel::FreeBusyRole).value<KCalendarCore::FreeBusy::Ptr>(); | ||
127 | 127 | | |||
128 | KCalCore::Event::Ptr inc = KCalCore::Event::Ptr(new KCalCore::Event()); | 128 | KCalendarCore::Event::Ptr inc = KCalendarCore::Event::Ptr(new KCalendarCore::Event()); | ||
129 | inc->setDtStart(period.start()); | 129 | inc->setDtStart(period.start()); | ||
130 | inc->setDtEnd(period.end()); | 130 | inc->setDtEnd(period.end()); | ||
131 | inc->setUid(QLatin1String("fb-") + fb->uid() + QLatin1String("-") + QString::number(i)); | 131 | inc->setUid(QLatin1String("fb-") + fb->uid() + QLatin1String("-") + QString::number(i)); | ||
132 | 132 | | |||
133 | inc->setCustomProperty("FREEBUSY", "STATUS", QString::number(period.type())); | 133 | inc->setCustomProperty("FREEBUSY", "STATUS", QString::number(period.type())); | ||
134 | QString summary = period.summary(); | 134 | QString summary = period.summary(); | ||
135 | if (summary.isEmpty()) { | 135 | if (summary.isEmpty()) { | ||
136 | switch (period.type()) { | 136 | switch (period.type()) { | ||
137 | case KCalCore::FreeBusyPeriod::Free: | 137 | case KCalendarCore::FreeBusyPeriod::Free: | ||
138 | summary = i18n("Free"); | 138 | summary = i18n("Free"); | ||
139 | break; | 139 | break; | ||
140 | case KCalCore::FreeBusyPeriod::Busy: | 140 | case KCalendarCore::FreeBusyPeriod::Busy: | ||
141 | summary = i18n("Busy"); | 141 | summary = i18n("Busy"); | ||
142 | break; | 142 | break; | ||
143 | case KCalCore::FreeBusyPeriod::BusyUnavailable: | 143 | case KCalendarCore::FreeBusyPeriod::BusyUnavailable: | ||
144 | summary = i18n("Unavailable"); | 144 | summary = i18n("Unavailable"); | ||
145 | break; | 145 | break; | ||
146 | case KCalCore::FreeBusyPeriod::BusyTentative: | 146 | case KCalendarCore::FreeBusyPeriod::BusyTentative: | ||
147 | summary = i18n("Tentative"); | 147 | summary = i18n("Tentative"); | ||
148 | break; | 148 | break; | ||
149 | default: | 149 | default: | ||
150 | summary = i18n("Unknown"); | 150 | summary = i18n("Unknown"); | ||
151 | } | 151 | } | ||
152 | } | 152 | } | ||
153 | inc->setSummary(summary); | 153 | inc->setSummary(summary); | ||
154 | 154 | | |||
155 | d->mFbEvent.insert(index, inc); | 155 | d->mFbEvent.insert(index, inc); | ||
156 | d->mCalendar->addEvent(inc); | 156 | d->mCalendar->addEvent(inc); | ||
157 | } | 157 | } | ||
158 | } | 158 | } | ||
159 | 159 | | |||
160 | void FreeBusyCalendar::onRowsRemoved(const QModelIndex &parent, int first, int last) | 160 | void FreeBusyCalendar::onRowsRemoved(const QModelIndex &parent, int first, int last) | ||
161 | { | 161 | { | ||
162 | if (!parent.isValid()) { | 162 | if (!parent.isValid()) { | ||
163 | for (int i = first; i <= last; ++i) { | 163 | for (int i = first; i <= last; ++i) { | ||
164 | QModelIndex index = d->mModel->index(i, 0); | 164 | QModelIndex index = d->mModel->index(i, 0); | ||
165 | onRowsRemoved(index, 0, d->mModel->rowCount(index) - 1); | 165 | onRowsRemoved(index, 0, d->mModel->rowCount(index) - 1); | ||
166 | } | 166 | } | ||
167 | } else { | 167 | } else { | ||
168 | for (int i = first; i <= last; ++i) { | 168 | for (int i = first; i <= last; ++i) { | ||
169 | QModelIndex index = d->mModel->index(i, 0, parent); | 169 | QModelIndex index = d->mModel->index(i, 0, parent); | ||
170 | KCalCore::Event::Ptr inc = d->mFbEvent.take(index); | 170 | KCalendarCore::Event::Ptr inc = d->mFbEvent.take(index); | ||
171 | d->mCalendar->deleteEvent(inc); | 171 | d->mCalendar->deleteEvent(inc); | ||
172 | } | 172 | } | ||
173 | } | 173 | } | ||
174 | } | 174 | } | ||
175 | 175 | | |||
176 | void FreeBusyCalendar::onRowsChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) | 176 | void FreeBusyCalendar::onRowsChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight) | ||
177 | { | 177 | { | ||
178 | if (!topLeft.parent().isValid()) { | 178 | if (!topLeft.parent().isValid()) { | ||
179 | return; | 179 | return; | ||
180 | } | 180 | } | ||
181 | for (int i = topLeft.row(); i <= bottomRight.row(); ++i) { | 181 | for (int i = topLeft.row(); i <= bottomRight.row(); ++i) { | ||
182 | QModelIndex index = d->mModel->index(i, 0, topLeft.parent()); | 182 | QModelIndex index = d->mModel->index(i, 0, topLeft.parent()); | ||
183 | KCalCore::Event::Ptr inc = d->mFbEvent.value(index); | 183 | KCalendarCore::Event::Ptr inc = d->mFbEvent.value(index); | ||
184 | d->mCalendar->beginChange(inc); | 184 | d->mCalendar->beginChange(inc); | ||
185 | d->mCalendar->endChange(inc); | 185 | d->mCalendar->endChange(inc); | ||
186 | } | 186 | } | ||
187 | } | 187 | } |