• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 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"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal