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"
9using namespace Qt::Literals::StringLiterals;
10
11#include <Akonadi/EntityTreeModel>
12#include <Akonadi/ItemFetchJob>
13#include <Akonadi/ItemFetchScope>
14#include <Akonadi/Monitor>
15
16using namespace CalendarSupport;
17using namespace Akonadi;
18
19namespace CalendarSupport
20{
21class IncidenceAttachmentModelPrivate
22{
23 IncidenceAttachmentModelPrivate(IncidenceAttachmentModel *qq, const QPersistentModelIndex &modelIndex, const Akonadi::Item &item = Akonadi::Item())
24 : q_ptr(qq)
25 , m_modelIndex(modelIndex)
26 , m_item(item)
27 {
28 if (modelIndex.isValid()) {
29 QObject::connect(modelIndex.model(), SIGNAL(dataChanged(QModelIndex, QModelIndex)), qq, SLOT(resetModel()));
30 } else if (item.isValid()) {
31 createMonitor();
32 resetInternalData();
33 }
34 }
35
36 void resetModel()
37 {
38 Q_Q(IncidenceAttachmentModel);
39 q->beginResetModel();
40 resetInternalData();
41 q->endResetModel();
42 Q_EMIT q->rowCountChanged();
43 }
44
45 void itemFetched(Akonadi::Item::List list)
46 {
47 Q_ASSERT(list.size() == 1);
48 setItem(list.first());
49 }
50
51 void setItem(const Akonadi::Item &item);
52
53 void createMonitor()
54 {
55 if (m_monitor) {
56 return;
57 }
58
59 m_monitor = new Akonadi::Monitor(q_ptr);
60 m_monitor->setObjectName("IncidenceAttachmentModelMonitor"_L1);
61 m_monitor->setItemMonitored(m_item);
62 m_monitor->itemFetchScope().fetchFullPayload(true);
63 QObject::connect(m_monitor, SIGNAL(itemChanged(Akonadi::Item, QSet<QByteArray>)), q_ptr, SLOT(resetModel()));
64 QObject::connect(m_monitor, SIGNAL(itemRemoved(Akonadi::Item)), q_ptr, SLOT(resetModel()));
65 }
66
67 void resetInternalData()
68 {
69 Item item = m_item;
70 if (m_modelIndex.isValid()) {
71 item = m_modelIndex.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
72 }
73
74 if (!item.isValid() || !item.hasPayload<KCalendarCore::Incidence::Ptr>()) {
75 m_incidence = KCalendarCore::Incidence::Ptr();
76 return;
77 }
78 m_incidence = item.payload<KCalendarCore::Incidence::Ptr>();
79 }
80
81 Q_DECLARE_PUBLIC(IncidenceAttachmentModel)
82 IncidenceAttachmentModel *const q_ptr;
83
84 QModelIndex m_modelIndex;
85 Akonadi::Item m_item;
87 Akonadi::Monitor *m_monitor = nullptr;
88};
89}
90
91IncidenceAttachmentModel::IncidenceAttachmentModel(const QPersistentModelIndex &modelIndex, QObject *parent)
92 : QAbstractListModel(parent)
93 , d_ptr(new IncidenceAttachmentModelPrivate(this, modelIndex))
94{
95}
96
97IncidenceAttachmentModel::IncidenceAttachmentModel(const Akonadi::Item &item, QObject *parent)
98 : QAbstractListModel(parent)
99 , d_ptr(new IncidenceAttachmentModelPrivate(this, QModelIndex(), item))
100{
101}
102
103IncidenceAttachmentModel::IncidenceAttachmentModel(QObject *parent)
104 : QAbstractListModel(parent)
105 , d_ptr(new IncidenceAttachmentModelPrivate(this, QModelIndex()))
106{
107}
108
109IncidenceAttachmentModel::~IncidenceAttachmentModel() = default;
110
111KCalendarCore::Incidence::Ptr IncidenceAttachmentModel::incidence() const
112{
113 Q_D(const IncidenceAttachmentModel);
114 return d->m_incidence;
115}
116
117void IncidenceAttachmentModel::setIndex(const QPersistentModelIndex &modelIndex)
118{
119 Q_D(IncidenceAttachmentModel);
121 d->m_modelIndex = modelIndex;
122 d->m_item = Akonadi::Item();
123 d->resetInternalData();
125 Q_EMIT rowCountChanged();
126}
127
128void IncidenceAttachmentModel::setItem(const Akonadi::Item &item)
129{
130 Q_D(IncidenceAttachmentModel);
132 auto job = new ItemFetchJob(item);
133 job->fetchScope().fetchFullPayload(true);
134 connect(job, SIGNAL(itemsReceived(Akonadi::Item::List)), SLOT(itemFetched(Akonadi::Item::List)));
135 return;
136 }
137 d->setItem(item);
138}
139
140void IncidenceAttachmentModelPrivate::setItem(const Akonadi::Item &item)
141{
142 Q_Q(IncidenceAttachmentModel);
143 q->beginResetModel();
144 m_modelIndex = QModelIndex();
145 m_item = item;
146 createMonitor();
147 resetInternalData();
148 q->endResetModel();
149 Q_EMIT q->rowCountChanged();
150}
151
152int IncidenceAttachmentModel::rowCount(const QModelIndex &) const
153{
154 Q_D(const IncidenceAttachmentModel);
155 if (!d->m_incidence) {
156 return 0;
157 } else {
158 return d->m_incidence->attachments().size();
159 }
160}
161
162QVariant IncidenceAttachmentModel::data(const QModelIndex &index, int role) const
163{
164 Q_D(const IncidenceAttachmentModel);
165 if (!d->m_incidence) {
166 return {};
167 }
168
169 const KCalendarCore::Attachment attachment = d->m_incidence->attachments().at(index.row());
170 switch (role) {
171 case Qt::DisplayRole:
172 return attachment.label();
173 case AttachmentDataRole:
174 return attachment.decodedData();
175 case MimeTypeRole:
176 return attachment.mimeType();
177 }
178 return {};
179}
180
181QVariant IncidenceAttachmentModel::headerData(int section, Qt::Orientation orientation, int role) const
182{
183 return QAbstractItemModel::headerData(section, orientation, role);
184}
185
186QHash<int, QByteArray> CalendarSupport::IncidenceAttachmentModel::roleNames() const
187{
189 roleNames.insert(IncidenceAttachmentModel::MimeTypeRole, "mimeType");
190 return roleNames;
191}
192
193#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)
void setObjectName(QAnyStringView name)
bool isValid() 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 Fri Oct 11 2024 12:19:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.