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 PartFetcher::~PartFetcher()
00109 {
00110 delete d_ptr;
00111 }
00112
00113 void PartFetcher::start()
00114 {
00115 Q_D( PartFetcher );
00116
00117 const QModelIndex index = d->m_persistentIndex;
00118
00119 const QSet<QByteArray> loadedParts = index.data( EntityTreeModel::LoadedPartsRole ).value<QSet<QByteArray> >();
00120
00121 if ( loadedParts.contains( d->m_partName ) ) {
00122 d->m_item = d->m_persistentIndex.data( EntityTreeModel::ItemRole ).value<Item>();
00123 emitResult();
00124 return;
00125 }
00126
00127 const QSet<QByteArray> availableParts = index.data( EntityTreeModel::AvailablePartsRole ).value<QSet<QByteArray> >();
00128 if ( !availableParts.contains( d->m_partName ) ) {
00129 setError( UserDefinedError );
00130 setErrorText( i18n( "Payload part '%1' is not available for this index" , QString::fromLatin1( d->m_partName ) ) );
00131 emitResult();
00132 return;
00133 }
00134
00135 Akonadi::Session *session = qobject_cast<Akonadi::Session *>( qvariant_cast<QObject *>( index.data( EntityTreeModel::SessionRole ) ) );
00136
00137 if ( !session ) {
00138 setError( UserDefinedError );
00139 setErrorText( i18n( "No session available for this index" ) );
00140 emitResult();
00141 return;
00142 }
00143
00144 const Akonadi::Item item = index.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
00145
00146 if ( !item.isValid() ) {
00147 setError( UserDefinedError );
00148 setErrorText( i18n( "No item available for this index" ) );
00149 emitResult();
00150 return;
00151 }
00152
00153 ItemFetchScope scope;
00154 scope.fetchPayloadPart( d->m_partName );
00155 ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( item, session );
00156 itemFetchJob->setFetchScope( scope );
00157
00158 connect( itemFetchJob, SIGNAL( result( KJob* ) ),
00159 this, SLOT( fetchJobDone( KJob* ) ) );
00160 }
00161
00162 QModelIndex PartFetcher::index() const
00163 {
00164 Q_D( const PartFetcher );
00165
00166 return d->m_persistentIndex;
00167 }
00168
00169 QByteArray PartFetcher::partName() const
00170 {
00171 Q_D( const PartFetcher );
00172
00173 return d->m_partName;
00174 }
00175
00176 Item PartFetcher::item() const
00177 {
00178 Q_D( const PartFetcher );
00179
00180 return d->m_item;
00181 }
00182
00183 #include "partfetcher.moc"