CalendarSupport

incidenceattachmentmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3 Author: Stephen Kelly <stephen@kdab.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "incidenceattachmentmodel.h"
9
10#include <Akonadi/EntityTreeModel>
11#include <Akonadi/ItemFetchJob>
12#include <Akonadi/ItemFetchScope>
13#include <Akonadi/Monitor>
14
15using namespace CalendarSupport;
16using namespace Akonadi;
17
18namespace CalendarSupport
19{
20class IncidenceAttachmentModelPrivate
21{
22 IncidenceAttachmentModelPrivate(IncidenceAttachmentModel *qq, const QPersistentModelIndex &modelIndex, const Akonadi::Item &item = Akonadi::Item())
23 : q_ptr(qq)
24 , m_modelIndex(modelIndex)
25 , m_item(item)
26 {
27 if (modelIndex.isValid()) {
28 QObject::connect(modelIndex.model(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), qq, SLOT(resetModel()));
29 } else if (item.isValid()) {
30 createMonitor();
31 resetInternalData();
32 }
33 }
34
35 void resetModel()
36 {
37 Q_Q(IncidenceAttachmentModel);
38 q->beginResetModel();
39 resetInternalData();
40 q->endResetModel();
41 Q_EMIT q->rowCountChanged();
42 }
43
44 void itemFetched(Akonadi::Item::List list)
45 {
46 Q_ASSERT(list.size() == 1);
47 setItem(list.first());
48 }
49
50 void setItem(const Akonadi::Item &item);
51
52 void createMonitor()
53 {
54 if (m_monitor) {
55 return;
56 }
57
58 m_monitor = new Akonadi::Monitor(q_ptr);
59 m_monitor->setObjectName(QLatin1StringView("IncidenceAttachmentModelMonitor"));
60 m_monitor->setItemMonitored(m_item);
61 m_monitor->itemFetchScope().fetchFullPayload(true);
62 QObject::connect(m_monitor, SIGNAL(itemChanged(Akonadi::Item, QSet<QByteArray>)), q_ptr, SLOT(resetModel()));
63 QObject::connect(m_monitor, SIGNAL(itemRemoved(Akonadi::Item)), q_ptr, SLOT(resetModel()));
64 }
65
66 void resetInternalData()
67 {
68 Item item = m_item;
69 if (m_modelIndex.isValid()) {
70 item = m_modelIndex.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
71 }
72
73 if (!item.isValid() || !item.hasPayload<KCalendarCore::Incidence::Ptr>()) {
74 m_incidence = KCalendarCore::Incidence::Ptr();
75 return;
76 }
77 m_incidence = item.payload<KCalendarCore::Incidence::Ptr>();
78 }
79
80 Q_DECLARE_PUBLIC(IncidenceAttachmentModel)
81 IncidenceAttachmentModel *const q_ptr;
82
83 QModelIndex m_modelIndex;
84 Akonadi::Item m_item;
86 Akonadi::Monitor *m_monitor = nullptr;
87};
88}
89
90IncidenceAttachmentModel::IncidenceAttachmentModel(const QPersistentModelIndex &modelIndex, QObject *parent)
91 : QAbstractListModel(parent)
92 , d_ptr(new IncidenceAttachmentModelPrivate(this, modelIndex))
93{
94}
95
96IncidenceAttachmentModel::IncidenceAttachmentModel(const Akonadi::Item &item, QObject *parent)
97 : QAbstractListModel(parent)
98 , d_ptr(new IncidenceAttachmentModelPrivate(this, QModelIndex(), item))
99{
100}
101
102IncidenceAttachmentModel::IncidenceAttachmentModel(QObject *parent)
103 : QAbstractListModel(parent)
104 , d_ptr(new IncidenceAttachmentModelPrivate(this, QModelIndex()))
105{
106}
107
108IncidenceAttachmentModel::~IncidenceAttachmentModel() = default;
109
110KCalendarCore::Incidence::Ptr IncidenceAttachmentModel::incidence() const
111{
112 Q_D(const IncidenceAttachmentModel);
113 return d->m_incidence;
114}
115
116void IncidenceAttachmentModel::setIndex(const QPersistentModelIndex &modelIndex)
117{
118 Q_D(IncidenceAttachmentModel);
120 d->m_modelIndex = modelIndex;
121 d->m_item = Akonadi::Item();
122 d->resetInternalData();
124 Q_EMIT rowCountChanged();
125}
126
127void IncidenceAttachmentModel::setItem(const Akonadi::Item &item)
128{
129 Q_D(IncidenceAttachmentModel);
131 auto job = new ItemFetchJob(item);
132 job->fetchScope().fetchFullPayload(true);
133 connect(job, SIGNAL(itemsReceived(Akonadi::Item::List)), SLOT(itemFetched(Akonadi::Item::List)));
134 return;
135 }
136 d->setItem(item);
137}
138
139void IncidenceAttachmentModelPrivate::setItem(const Akonadi::Item &item)
140{
141 Q_Q(IncidenceAttachmentModel);
142 q->beginResetModel();
143 m_modelIndex = QModelIndex();
144 m_item = item;
145 createMonitor();
146 resetInternalData();
147 q->endResetModel();
148 Q_EMIT q->rowCountChanged();
149}
150
151int IncidenceAttachmentModel::rowCount(const QModelIndex &) const
152{
153 Q_D(const IncidenceAttachmentModel);
154 if (!d->m_incidence) {
155 return 0;
156 } else {
157 return d->m_incidence->attachments().size();
158 }
159}
160
161QVariant IncidenceAttachmentModel::data(const QModelIndex &index, int role) const
162{
163 Q_D(const IncidenceAttachmentModel);
164 if (!d->m_incidence) {
165 return {};
166 }
167
168 const KCalendarCore::Attachment attachment = d->m_incidence->attachments().at(index.row());
169 switch (role) {
170 case Qt::DisplayRole:
171 return attachment.label();
172 case AttachmentDataRole:
173 return attachment.decodedData();
174 case MimeTypeRole:
175 return attachment.mimeType();
176 }
177 return {};
178}
179
180QVariant IncidenceAttachmentModel::headerData(int section, Qt::Orientation orientation, int role) const
181{
182 return QAbstractItemModel::headerData(section, orientation, role);
183}
184
185QHash<int, QByteArray> CalendarSupport::IncidenceAttachmentModel::roleNames() const
186{
188 roleNames.insert(IncidenceAttachmentModel::MimeTypeRole, "mimeType");
189 return roleNames;
190}
191
192#include "moc_incidenceattachmentmodel.cpp"
void fetchFullPayload(bool fetch=true)
bool hasPayload() const
ItemFetchScope & itemFetchScope()
void setItemMonitored(const Item &item, bool monitored=true)
QByteArray decodedData() const
QString mimeType() const
QSharedPointer< Incidence > Ptr
KIOCORE_EXPORT QStringList list(const QString &fileClass)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const const
virtual QHash< int, QByteArray > roleNames() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
iterator insert(const Key &key, const T &value)
T & first()
qsizetype size() const const
QVariant data(int role) const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
void setObjectName(QAnyStringView name)
bool isValid() const const
const QAbstractItemModel * model() const const
DisplayRole
Orientation
T value() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.