20 #include "partfetcher.h"
22 #include "entitytreemodel.h"
24 #include "itemfetchjob.h"
25 #include "itemfetchscope.h"
29 Q_DECLARE_METATYPE( QSet<QByteArray> )
32 using namespace Akonadi;
37 class PartFetcherPrivate
39 PartFetcherPrivate( PartFetcher *partFetcher,
const QModelIndex &index,
const QByteArray &partName )
40 : m_persistentIndex( index ), m_partName( partName ), q_ptr( partFetcher )
44 void fetchJobDone( KJob* );
46 void modelDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight );
48 QPersistentModelIndex m_persistentIndex;
49 QByteArray m_partName;
52 Q_DECLARE_PUBLIC( PartFetcher )
58 void PartFetcherPrivate::fetchJobDone( KJob *job )
62 q->setError( KJob::UserDefinedError );
63 q->setErrorText( i18n(
"Unable to fetch item for index" ) );
68 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
70 const Item::List list = fetchJob->items();
72 Q_ASSERT( list.size() == 1 );
76 if ( !m_persistentIndex.isValid() ) {
77 q->setError( KJob::UserDefinedError );
78 q->setErrorText( i18n(
"Index is no longer available" ) );
83 const QSet<QByteArray> loadedParts = m_persistentIndex.data( EntityTreeModel::LoadedPartsRole ).value<QSet<QByteArray> >();
85 Q_ASSERT( !loadedParts.contains( m_partName ) );
87 Item item = m_persistentIndex.data( EntityTreeModel::ItemRole ).value<Item>();
89 item.apply( list.at( 0 ) );
91 QAbstractItemModel *model =
const_cast<QAbstractItemModel *
>( m_persistentIndex.model() );
95 QVariant itemVariant = QVariant::fromValue( item );
96 model->setData( m_persistentIndex, itemVariant, EntityTreeModel::ItemRole );
100 emit q->emitResult();
103 PartFetcher::PartFetcher(
const QModelIndex &index,
const QByteArray &partName, QObject *parent )
104 : KJob( parent ), d_ptr( new PartFetcherPrivate( this, index, partName ) )
108 PartFetcher::~PartFetcher()
113 void PartFetcher::start()
117 const QModelIndex index = d->m_persistentIndex;
119 const QSet<QByteArray> loadedParts = index.data( EntityTreeModel::LoadedPartsRole ).value<QSet<QByteArray> >();
121 if ( loadedParts.contains( d->m_partName ) ) {
122 d->m_item = d->m_persistentIndex.data( EntityTreeModel::ItemRole ).value<
Item>();
127 const QSet<QByteArray> availableParts = index.data( EntityTreeModel::AvailablePartsRole ).value<QSet<QByteArray> >();
128 if ( !availableParts.contains( d->m_partName ) ) {
129 setError( UserDefinedError );
130 setErrorText( i18n(
"Payload part '%1' is not available for this index" , QString::fromLatin1( d->m_partName ) ) );
135 Akonadi::Session *session = qobject_cast<Akonadi::Session *>( qvariant_cast<QObject *>( index.data( EntityTreeModel::SessionRole ) ) );
138 setError( UserDefinedError );
139 setErrorText( i18n(
"No session available for this index" ) );
144 const Akonadi::Item item = index.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
146 if ( !item.isValid() ) {
147 setError( UserDefinedError );
148 setErrorText( i18n(
"No item available for this index" ) );
155 ItemFetchJob *itemFetchJob =
new Akonadi::ItemFetchJob( item, session );
158 connect( itemFetchJob, SIGNAL(result(KJob*)),
159 this, SLOT(fetchJobDone(KJob*)) );
162 QModelIndex PartFetcher::index()
const
166 return d->m_persistentIndex;
169 QByteArray PartFetcher::partName()
const
173 return d->m_partName;
183 #include "partfetcher.moc"