00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "partfetcher.h"
00021
00022 #include "entitytreemodel.h"
00023 #include "session.h"
00024 #include "itemfetchjob.h"
00025 #include "itemfetchscope.h"
00026 #include <KLocale>
00027
00028 #ifndef KDE_USE_FINAL
00029 Q_DECLARE_METATYPE( QSet<QByteArray> )
00030 #endif
00031
00032 using namespace Akonadi;
00033
00034 namespace Akonadi
00035 {
00036
00037 class PartFetcherPrivate
00038 {
00039 PartFetcherPrivate( PartFetcher *partFetcher, const QModelIndex &index, const QByteArray &partName )
00040 : m_persistentIndex( index ), m_partName( partName ), q_ptr( partFetcher )
00041 {
00042 }
00043
00044 void fetchJobDone( KJob* );
00045
00046 void modelDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight );
00047
00048 QPersistentModelIndex m_persistentIndex;
00049 QByteArray m_partName;
00050 Item m_item;
00051
00052 Q_DECLARE_PUBLIC( PartFetcher )
00053 PartFetcher *q_ptr;
00054 };
00055
00056 }
00057
00058 void PartFetcherPrivate::fetchJobDone( KJob *job )
00059 {
00060 Q_Q( PartFetcher );
00061 if ( job->error() ) {
00062 q->setError( KJob::UserDefinedError );
00063 q->setErrorText( i18n( "Unable to fetch item for index" ) );
00064 q->emitResult();
00065 return;
00066 }
00067
00068 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
00069
00070 const Item::List list = fetchJob->items();
00071
00072 Q_ASSERT( list.size() == 1 );
00073
00074
00075
00076 if ( !m_persistentIndex.isValid() ) {
00077 q->setError( KJob::UserDefinedError );
00078 q->setErrorText( i18n( "Index is no longer available" ) );
00079 q->emitResult();
00080 return;
00081 }
00082
00083 const QSet<QByteArray> loadedParts = m_persistentIndex.data( EntityTreeModel::LoadedPartsRole ).value<QSet<QByteArray> >();
00084
00085 Q_ASSERT( !loadedParts.contains( m_partName ) );
00086
00087 Item item = m_persistentIndex.data( EntityTreeModel::ItemRole ).value<Item>();
00088
00089 item.apply( list.at( 0 ) );
00090
00091 QAbstractItemModel *model = const_cast<QAbstractItemModel *>( m_persistentIndex.model() );
00092
00093 Q_ASSERT( model );
00094
00095 QVariant itemVariant = QVariant::fromValue( item );
00096 model->setData( m_persistentIndex, itemVariant, EntityTreeModel::ItemRole );
00097
00098 m_item = item;
00099
00100 emit q->emitResult();
00101 }
00102
00103 PartFetcher::PartFetcher( const QModelIndex &index, const QByteArray &partName, QObject *parent )
00104 : KJob( parent ), d_ptr( new PartFetcherPrivate( this, index, partName ) )
00105 {
00106 }
00107
00108 void PartFetcher::start()
00109 {
00110 Q_D( PartFetcher );
00111
00112 const QModelIndex index = d->m_persistentIndex;
00113
00114 const QSet<QByteArray> loadedParts = index.data( EntityTreeModel::LoadedPartsRole ).value<QSet<QByteArray> >();
00115
00116 if ( loadedParts.contains( d->m_partName ) ) {
00117 d->m_item = d->m_persistentIndex.data( EntityTreeModel::ItemRole ).value<Item>();
00118 emitResult();
00119 return;
00120 }
00121
00122 const QSet<QByteArray> availableParts = index.data( EntityTreeModel::AvailablePartsRole ).value<QSet<QByteArray> >();
00123 if ( !availableParts.contains( d->m_partName ) ) {
00124 setError( UserDefinedError );
00125 setErrorText( i18n( "Payload part '%1' is not available for this index" , QString::fromLatin1( d->m_partName ) ) );
00126 emitResult();
00127 return;
00128 }
00129
00130 Akonadi::Session *session = qobject_cast<Akonadi::Session *>( qvariant_cast<QObject *>( index.data( EntityTreeModel::SessionRole ) ) );
00131
00132 if ( !session ) {
00133 setError( UserDefinedError );
00134 setErrorText( i18n( "No session available for this index" ) );
00135 emitResult();
00136 return;
00137 }
00138
00139 const Akonadi::Item item = index.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
00140
00141 if ( !item.isValid() ) {
00142 setError( UserDefinedError );
00143 setErrorText( i18n( "No item available for this index" ) );
00144 emitResult();
00145 return;
00146 }
00147
00148 ItemFetchScope scope;
00149 scope.fetchPayloadPart( d->m_partName );
00150 ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( item, session );
00151 itemFetchJob->setFetchScope( scope );
00152
00153 connect( itemFetchJob, SIGNAL( result( KJob* ) ),
00154 this, SLOT( fetchJobDone( KJob* ) ) );
00155 }
00156
00157 QModelIndex PartFetcher::index() const
00158 {
00159 Q_D( const PartFetcher );
00160
00161 return d->m_persistentIndex;
00162 }
00163
00164 QByteArray PartFetcher::partName() const
00165 {
00166 Q_D( const PartFetcher );
00167
00168 return d->m_partName;
00169 }
00170
00171 Item PartFetcher::item() const
00172 {
00173 Q_D( const PartFetcher );
00174
00175 return d->m_item;
00176 }
00177
00178 #include "partfetcher.moc"