8#include "freebusyitemmodel.h"
10#include <Akonadi/FreeBusyManager>
12#include <KLocalizedString>
17using namespace CalendarSupport;
22 ItemPrivateData(ItemPrivateData *parent)
29 qDeleteAll(childItems);
32 ItemPrivateData *child(
int row)
34 return childItems.
value(row);
37 void appendChild(ItemPrivateData *item)
42 ItemPrivateData *removeChild(
int row)
44 return childItems.
takeAt(row);
47 [[nodiscard]]
int childCount()
const
49 return childItems.
count();
52 [[nodiscard]]
int row()
const
55 return parentItem->childItems.
indexOf(
const_cast<ItemPrivateData *
>(
this));
60 ItemPrivateData *parent()
67 ItemPrivateData *parentItem =
nullptr;
70class CalendarSupport::FreeBusyItemModelPrivate
73 ~FreeBusyItemModelPrivate()
79 bool mForceDownload =
false;
81 ItemPrivateData *mRootData =
nullptr;
84FreeBusyItemModel::FreeBusyItemModel(
QObject *parent)
86 , d(new CalendarSupport::FreeBusyItemModelPrivate)
88 qRegisterMetaType<KCalendarCore::Attendee>();
89 qRegisterMetaType<KCalendarCore::FreeBusy::Ptr>(
"KCalendarCore::FreeBusy::Ptr");
90 qRegisterMetaType<KCalendarCore::Period>(
"KCalendarCore::Period");
92 Akonadi::FreeBusyManager *m = Akonadi::FreeBusyManager::self();
93 connect(m, &Akonadi::FreeBusyManager::freeBusyRetrieved,
this, &FreeBusyItemModel::slotInsertFreeBusy);
96 d->mReloadTimer.setSingleShot(
true);
98 d->mRootData =
new ItemPrivateData(
nullptr);
101FreeBusyItemModel::~FreeBusyItemModel() =
default;
111 if (data->parent() == d->mRootData) {
112 int row = index.
row();
113 if (row >= d->mFreeBusyItems.size()) {
119 return d->mFreeBusyItems.at(row)->attendee().fullName();
120 case FreeBusyItemModel::AttendeeRole:
122 case FreeBusyItemModel::FreeBusyRole:
123 if (d->mFreeBusyItems.at(row)->freeBusy()) {
134 if (!fbitem->freeBusy() || index.
row() >= fbitem->freeBusy()->busyPeriods().size()) {
143 case FreeBusyItemModel::FreeBusyPeriodRole:
150int FreeBusyItemModel::rowCount(
const QModelIndex &parent)
const
152 ItemPrivateData *parentData =
nullptr;
153 if (
parent.column() > 0) {
158 parentData = d->mRootData;
160 parentData =
static_cast<ItemPrivateData *
>(
parent.internalPointer());
163 return parentData->childCount();
166int FreeBusyItemModel::columnCount(
const QModelIndex &parent)
const
178 ItemPrivateData *parentData =
nullptr;
180 parentData = d->mRootData;
182 parentData =
static_cast<ItemPrivateData *
>(
parent.internalPointer());
185 ItemPrivateData *childData = parentData->child(row);
199 auto childData =
static_cast<ItemPrivateData *
>(child.
internalPointer());
200 ItemPrivateData *parentData = childData->parent();
201 if (parentData == d->mRootData) {
205 return createIndex(parentData->row(), 0, parentData);
211 return i18n(
"Attendee");
218 int row = d->mFreeBusyItems.size();
220 d->mFreeBusyItems.append(freebusy);
221 auto data =
new ItemPrivateData(d->mRootData);
222 d->mRootData->appendChild(data);
225 if (freebusy->freeBusy() && !freebusy->freeBusy()->fullBusyPeriods().isEmpty()) {
227 setFreeBusyPeriods(
parent, freebusy->freeBusy()->fullBusyPeriods());
229 updateFreeBusyData(freebusy);
238 auto parentData =
static_cast<ItemPrivateData *
>(
parent.internalPointer());
240 int childCount = parentData->childCount();
244 if (childCount > 0 && fb_count < childCount) {
246 for (
int i = childCount - 1; i > fb_count; --i) {
247 delete parentData->removeChild(i);
251 last = index(fb_count - 1, 0,
parent);
254 }
else if (fb_count > childCount) {
256 for (
int i = childCount; i < fb_count; ++i) {
257 auto childData =
new ItemPrivateData(parentData);
258 parentData->appendChild(childData);
261 if (childCount > 0) {
262 last = index(childCount - 1, 0,
parent);
265 }
else if (fb_count == childCount && fb_count > 0) {
270void FreeBusyItemModel::clear()
273 d->mFreeBusyItems.clear();
275 d->mRootData =
new ItemPrivateData(
nullptr);
279void FreeBusyItemModel::removeRow(
int row)
282 d->mFreeBusyItems.removeAt(row);
283 ItemPrivateData *data = d->mRootData->removeChild(row);
290 int row = d->mFreeBusyItems.indexOf(freebusy);
299 for (
int i = 0; i < d->mFreeBusyItems.count(); ++i) {
300 anItem = d->mFreeBusyItems[i];
301 if (anItem->attendee() == attendee) {
302 if (anItem->updateTimerID() != 0) {
314 for (
int i = 0; i < d->mFreeBusyItems.count(); ++i) {
315 anItem = d->mFreeBusyItems[i];
316 if (anItem->attendee() == attendee) {
325 if (item->isDownloading()) {
330 if (item->updateTimerID() != 0) {
340void FreeBusyItemModel::timerEvent(
QTimerEvent *event)
344 if (item->updateTimerID() ==
event->timerId()) {
345 item->setUpdateTimerID(0);
346 item->startDownload(d->mForceDownload);
358 if (fb->fullBusyPeriods().isEmpty()) {
365 if (item->email() == email) {
366 item->setFreeBusy(fb);
367 const int row = d->mFreeBusyItems.indexOf(item);
370 setFreeBusyPeriods(
parent, fb->fullBusyPeriods());
375void FreeBusyItemModel::autoReload()
377 d->mForceDownload =
false;
381void FreeBusyItemModel::reload()
384 if (d->mForceDownload) {
385 item->startDownload(d->mForceDownload);
387 updateFreeBusyData(item);
392void FreeBusyItemModel::triggerReload()
394 d->mReloadTimer.start(1000);
397void FreeBusyItemModel::cancelReload()
399 d->mReloadTimer.stop();
402#include "moc_freebusyitemmodel.cpp"
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
bool hasIndex(int row, int column, const QModelIndex &parent) const const
QDateTime toLocalTime() const const
void append(QList< T > &&value)
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
qsizetype size() const const
T value(qsizetype i) const const
void * internalPointer() const const
bool isValid() const const
virtual bool event(QEvent *e)
QObject * parent() const const
int startTimer(int interval, Qt::TimerType timerType)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)