23 #include <Akonadi/EntityTreeModel>
24 #include <Akonadi/ItemFetchJob>
25 #include <Akonadi/ItemFetchScope>
26 #include <Akonadi/Monitor>
28 using namespace CalendarSupport;
29 using namespace Akonadi;
31 namespace CalendarSupport {
33 class IncidenceAttachmentModelPrivate
36 const QPersistentModelIndex &modelIndex,
37 Akonadi::Item item = Akonadi::Item() )
38 : q_ptr( qq ), m_modelIndex( modelIndex ), m_item( item ), m_monitor( 0 )
40 if ( modelIndex.isValid() ) {
41 QObject::connect( modelIndex.model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
42 qq, SLOT(resetModel()) );
43 }
else if ( item.isValid() ) {
47 QHash<int, QByteArray> roleNames = qq->roleNames();
50 qq->setRoleNames( roleNames );
59 emit q->rowCountChanged();
62 void itemFetched( Akonadi::Item::List list )
64 Q_ASSERT( list.size() == 1 );
65 setItem( list.first() );
68 void setItem(
const Akonadi::Item &item );
76 m_monitor =
new Akonadi::Monitor( q_ptr );
77 m_monitor->setItemMonitored( m_item );
78 m_monitor->itemFetchScope().fetchFullPayload(
true );
79 QObject::connect( m_monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
80 q_ptr, SLOT(resetModel()) );
81 QObject::connect( m_monitor, SIGNAL(itemRemoved(Akonadi::Item)),
82 q_ptr, SLOT(resetModel()) );
85 void resetInternalData()
88 m_incidence->clearTempFiles();
91 if ( m_modelIndex.isValid() ) {
92 item = m_modelIndex.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
95 if ( !item.isValid() || !item.hasPayload<KCalCore::Incidence::Ptr>() ) {
96 m_incidence = KCalCore::Incidence::Ptr();
99 m_incidence = item.payload<KCalCore::Incidence::Ptr>();
105 QModelIndex m_modelIndex;
106 Akonadi::Item m_item;
107 KCalCore::Incidence::Ptr m_incidence;
108 Akonadi::Monitor *m_monitor;
116 d_ptr( new IncidenceAttachmentModelPrivate( this, modelIndex ) )
123 d_ptr( new IncidenceAttachmentModelPrivate( this, QModelIndex(), item ) )
130 d_ptr( new IncidenceAttachmentModelPrivate( this, QModelIndex() ) )
143 return d->m_incidence;
150 d->m_modelIndex = modelIndex;
151 d->m_item = Akonadi::Item();
152 d->resetInternalData();
160 if ( !item.hasPayload<KCalCore::Incidence::Ptr>() ) {
161 ItemFetchJob *job =
new ItemFetchJob( item );
162 job->fetchScope().fetchFullPayload(
true );
163 connect( job, SIGNAL(itemsReceived(Akonadi::Item::List)),
164 SLOT(itemFetched(Akonadi::Item::List)) );
170 void IncidenceAttachmentModelPrivate::setItem(
const Akonadi::Item &item )
173 q->beginResetModel();
174 m_modelIndex = QModelIndex();
179 emit q->rowCountChanged();
185 if ( !d->m_incidence ) {
188 return d->m_incidence->attachments().size();
195 if ( !d->m_incidence ) {
199 KCalCore::Attachment::Ptr attachment = d->m_incidence->attachments().at( index.row() );
201 case Qt::DisplayRole:
202 return attachment->label();
204 return attachment->decodedData();
206 return attachment->mimeType();
208 return d->m_incidence->writeAttachmentToTempFile( attachment );
214 Qt::Orientation orientation,
217 return QAbstractItemModel::headerData( section, orientation, role );
220 #include "incidenceattachmentmodel.moc"
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
KCalCore::Incidence::Ptr incidence() const
IncidenceAttachmentModel(const QPersistentModelIndex &modelIndex, QObject *parent=0)
void setIndex(const QPersistentModelIndex &modelIndex)
~IncidenceAttachmentModel()
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
void setItem(const Akonadi::Item &item)
int rowCount(const QModelIndex &parent=QModelIndex()) const