akonadi
partfetcher.cpp
00001 /* 00002 Copyright (c) 2009 Stephen Kelly <steveire@gmail.com> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 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 // If m_persistentIndex comes from a selection proxy model, it could become 00075 // invalid if the user clicks around a lot. 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"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.