Fix wrong connect
[kdepim.git] / calendarsupport / incidenceattachmentmodel.cpp
blob055ac13eef0468264855bb2e742033039dec930a
1 /*
2 Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3 Author: Stephen Kelly <stephen@kdab.com>
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
21 #include "incidenceattachmentmodel.h"
23 #include <EntityTreeModel>
24 #include <ItemFetchJob>
25 #include <ItemFetchScope>
26 #include <Monitor>
28 using namespace CalendarSupport;
29 using namespace Akonadi;
31 namespace CalendarSupport
34 class IncidenceAttachmentModelPrivate
36 IncidenceAttachmentModelPrivate(IncidenceAttachmentModel *qq,
37 const QPersistentModelIndex &modelIndex,
38 const Akonadi::Item &item = Akonadi::Item())
39 : q_ptr(qq), m_modelIndex(modelIndex), m_item(item), m_monitor(0)
41 if (modelIndex.isValid()) {
42 QObject::connect(modelIndex.model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
43 qq, SLOT(resetModel()));
44 } else if (item.isValid()) {
45 createMonitor();
46 resetInternalData();
48 QHash<int, QByteArray> roleNames = qq->roleNames();
49 roleNames.insert(IncidenceAttachmentModel::MimeTypeRole, "mimeType");
50 roleNames.insert(IncidenceAttachmentModel::AttachmentUrl, "attachmentUrl");
51 qq->setRoleNames(roleNames);
54 void resetModel()
56 Q_Q(IncidenceAttachmentModel);
57 q->beginResetModel();
58 resetInternalData();
59 q->endResetModel();
60 Q_EMIT q->rowCountChanged();
63 void itemFetched(Akonadi::Item::List list)
65 Q_ASSERT(list.size() == 1);
66 setItem(list.first());
69 void setItem(const Akonadi::Item &item);
71 void createMonitor()
73 if (m_monitor) {
74 return;
77 m_monitor = new Akonadi::Monitor(q_ptr);
78 m_monitor->setItemMonitored(m_item);
79 m_monitor->itemFetchScope().fetchFullPayload(true);
80 QObject::connect(m_monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
81 q_ptr, SLOT(resetModel()));
82 QObject::connect(m_monitor, SIGNAL(itemRemoved(Akonadi::Item)),
83 q_ptr, SLOT(resetModel()));
86 void resetInternalData()
88 if (m_incidence) {
89 m_incidence->clearTempFiles();
91 Item item = m_item;
92 if (m_modelIndex.isValid()) {
93 item = m_modelIndex.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
96 if (!item.isValid() || !item.hasPayload<KCalCore::Incidence::Ptr>()) {
97 m_incidence = KCalCore::Incidence::Ptr();
98 return;
100 m_incidence = item.payload<KCalCore::Incidence::Ptr>();
103 Q_DECLARE_PUBLIC(IncidenceAttachmentModel)
104 IncidenceAttachmentModel *const q_ptr;
106 QModelIndex m_modelIndex;
107 Akonadi::Item m_item;
108 KCalCore::Incidence::Ptr m_incidence;
109 Akonadi::Monitor *m_monitor;
114 IncidenceAttachmentModel::IncidenceAttachmentModel(const QPersistentModelIndex &modelIndex,
115 QObject *parent)
116 : QAbstractListModel(parent),
117 d_ptr(new IncidenceAttachmentModelPrivate(this, modelIndex))
122 IncidenceAttachmentModel::IncidenceAttachmentModel(const Akonadi::Item &item, QObject *parent)
123 : QAbstractListModel(parent),
124 d_ptr(new IncidenceAttachmentModelPrivate(this, QModelIndex(), item))
129 IncidenceAttachmentModel::IncidenceAttachmentModel(QObject *parent)
130 : QAbstractListModel(parent),
131 d_ptr(new IncidenceAttachmentModelPrivate(this, QModelIndex()))
136 IncidenceAttachmentModel::~IncidenceAttachmentModel()
138 delete d_ptr;
141 KCalCore::Incidence::Ptr IncidenceAttachmentModel::incidence() const
143 Q_D(const IncidenceAttachmentModel);
144 return d->m_incidence;
147 void IncidenceAttachmentModel::setIndex(const QPersistentModelIndex &modelIndex)
149 Q_D(IncidenceAttachmentModel);
150 beginResetModel();
151 d->m_modelIndex = modelIndex;
152 d->m_item = Akonadi::Item();
153 d->resetInternalData();
154 endResetModel();
155 Q_EMIT rowCountChanged();
158 void IncidenceAttachmentModel::setItem(const Akonadi::Item &item)
160 Q_D(IncidenceAttachmentModel);
161 if (!item.hasPayload<KCalCore::Incidence::Ptr>()) {
162 ItemFetchJob *job = new ItemFetchJob(item);
163 job->fetchScope().fetchFullPayload(true);
164 connect(job, SIGNAL(itemsReceived(Akonadi::Item::List)),
165 SLOT(itemFetched(Akonadi::Item::List)));
166 return;
168 d->setItem(item);
171 void IncidenceAttachmentModelPrivate::setItem(const Akonadi::Item &item)
173 Q_Q(IncidenceAttachmentModel);
174 q->beginResetModel();
175 m_modelIndex = QModelIndex();
176 m_item = item;
177 createMonitor();
178 resetInternalData();
179 q->endResetModel();
180 Q_EMIT q->rowCountChanged();
183 int IncidenceAttachmentModel::rowCount(const QModelIndex &) const
185 Q_D(const IncidenceAttachmentModel);
186 if (!d->m_incidence) {
187 return 0;
188 } else {
189 return d->m_incidence->attachments().size();
193 QVariant IncidenceAttachmentModel::data(const QModelIndex &index, int role) const
195 Q_D(const IncidenceAttachmentModel);
196 if (!d->m_incidence) {
197 return QVariant();
200 KCalCore::Attachment::Ptr attachment = d->m_incidence->attachments().at(index.row());
201 switch (role) {
202 case Qt::DisplayRole:
203 return attachment->label();
204 case AttachmentDataRole:
205 return attachment->decodedData();
206 case MimeTypeRole:
207 return attachment->mimeType();
208 case AttachmentUrl:
209 return d->m_incidence->writeAttachmentToTempFile(attachment);
211 return QVariant();
214 QVariant IncidenceAttachmentModel::headerData(int section,
215 Qt::Orientation orientation,
216 int role) const
218 return QAbstractItemModel::headerData(section, orientation, role);
221 #include "moc_incidenceattachmentmodel.cpp"