• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • akonadi
partfetcher.cpp
1 /*
2  Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "partfetcher.h"
21 
22 #include "entitytreemodel.h"
23 #include "session.h"
24 #include "itemfetchjob.h"
25 #include "itemfetchscope.h"
26 #include <KLocalizedString>
27 
28 #ifndef KDE_USE_FINAL
29 Q_DECLARE_METATYPE( QSet<QByteArray> )
30 #endif
31 
32 using namespace Akonadi;
33 
34 namespace Akonadi
35 {
36 
37 class PartFetcherPrivate
38 {
39  PartFetcherPrivate( PartFetcher *partFetcher, const QModelIndex &index, const QByteArray &partName )
40  : m_persistentIndex( index ), m_partName( partName ), q_ptr( partFetcher )
41  {
42  }
43 
44  void fetchJobDone( KJob* );
45 
46  void modelDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight );
47 
48  QPersistentModelIndex m_persistentIndex;
49  QByteArray m_partName;
50  Item m_item;
51 
52  Q_DECLARE_PUBLIC( PartFetcher )
53  PartFetcher *q_ptr;
54 };
55 
56 }
57 
58 void PartFetcherPrivate::fetchJobDone( KJob *job )
59 {
60  Q_Q( PartFetcher );
61  if ( job->error() ) {
62  q->setError( KJob::UserDefinedError );
63  q->setErrorText( i18n( "Unable to fetch item for index" ) );
64  q->emitResult();
65  return;
66  }
67 
68  ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob*>( job );
69 
70  const Item::List list = fetchJob->items();
71 
72  Q_ASSERT( list.size() == 1 );
73 
74  // If m_persistentIndex comes from a selection proxy model, it could become
75  // invalid if the user clicks around a lot.
76  if ( !m_persistentIndex.isValid() ) {
77  q->setError( KJob::UserDefinedError );
78  q->setErrorText( i18n( "Index is no longer available" ) );
79  q->emitResult();
80  return;
81  }
82 
83  const QSet<QByteArray> loadedParts = m_persistentIndex.data( EntityTreeModel::LoadedPartsRole ).value<QSet<QByteArray> >();
84 
85  Q_ASSERT( !loadedParts.contains( m_partName ) );
86 
87  Item item = m_persistentIndex.data( EntityTreeModel::ItemRole ).value<Item>();
88 
89  item.apply( list.at( 0 ) );
90 
91  QAbstractItemModel *model = const_cast<QAbstractItemModel *>( m_persistentIndex.model() );
92 
93  Q_ASSERT( model );
94 
95  QVariant itemVariant = QVariant::fromValue( item );
96  model->setData( m_persistentIndex, itemVariant, EntityTreeModel::ItemRole );
97 
98  m_item = item;
99 
100  emit q->emitResult();
101 }
102 
103 PartFetcher::PartFetcher( const QModelIndex &index, const QByteArray &partName, QObject *parent )
104  : KJob( parent ), d_ptr( new PartFetcherPrivate( this, index, partName ) )
105 {
106 }
107 
108 PartFetcher::~PartFetcher()
109 {
110  delete d_ptr;
111 }
112 
113 void PartFetcher::start()
114 {
115  Q_D( PartFetcher );
116 
117  const QModelIndex index = d->m_persistentIndex;
118 
119  const QSet<QByteArray> loadedParts = index.data( EntityTreeModel::LoadedPartsRole ).value<QSet<QByteArray> >();
120 
121  if ( loadedParts.contains( d->m_partName ) ) {
122  d->m_item = d->m_persistentIndex.data( EntityTreeModel::ItemRole ).value<Item>();
123  emitResult();
124  return;
125  }
126 
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 ) ) );
131  emitResult();
132  return;
133  }
134 
135  Akonadi::Session *session = qobject_cast<Akonadi::Session *>( qvariant_cast<QObject *>( index.data( EntityTreeModel::SessionRole ) ) );
136 
137  if ( !session ) {
138  setError( UserDefinedError );
139  setErrorText( i18n( "No session available for this index" ) );
140  emitResult();
141  return;
142  }
143 
144  const Akonadi::Item item = index.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
145 
146  if ( !item.isValid() ) {
147  setError( UserDefinedError );
148  setErrorText( i18n( "No item available for this index" ) );
149  emitResult();
150  return;
151  }
152 
153  ItemFetchScope scope;
154  scope.fetchPayloadPart( d->m_partName );
155  ItemFetchJob *itemFetchJob = new Akonadi::ItemFetchJob( item, session );
156  itemFetchJob->setFetchScope( scope );
157 
158  connect( itemFetchJob, SIGNAL(result(KJob*)),
159  this, SLOT(fetchJobDone(KJob*)) );
160 }
161 
162 QModelIndex PartFetcher::index() const
163 {
164  Q_D( const PartFetcher );
165 
166  return d->m_persistentIndex;
167 }
168 
169 QByteArray PartFetcher::partName() const
170 {
171  Q_D( const PartFetcher );
172 
173  return d->m_partName;
174 }
175 
176 Item PartFetcher::item() const
177 {
178  Q_D( const PartFetcher );
179 
180  return d->m_item;
181 }
182 
183 #include "moc_partfetcher.cpp"
Akonadi::EntityTreeModel::LoadedPartsRole
Parts available in the model for the item.
Definition: entitytreemodel.h:342
Akonadi::PartFetcher::start
virtual void start()
Starts the fetch operation.
Definition: partfetcher.cpp:113
Akonadi::ItemFetchJob::items
Item::List items() const
Returns the fetched items.
Definition: itemfetchjob.cpp:228
Akonadi::ItemFetchJob::setFetchScope
void setFetchScope(ItemFetchScope &fetchScope)
Sets the item fetch scope.
Definition: itemfetchjob.cpp:242
Akonadi::PartFetcher::partName
QByteArray partName() const
Returns the name of the part that has been fetched.
Definition: partfetcher.cpp:169
Akonadi::Session
A communication session with the Akonadi storage.
Definition: session.h:59
Akonadi::ItemFetchScope
Specifies which parts of an item should be fetched from the Akonadi storage.
Definition: itemfetchscope.h:68
Akonadi::PartFetcher::item
Item item() const
Returns the item that contains the fetched payload part.
Definition: partfetcher.cpp:176
Akonadi::EntityTreeModel::AvailablePartsRole
Parts available in the Akonadi server for the item.
Definition: entitytreemodel.h:343
Akonadi::EntityTreeModel::ItemRole
The Item.
Definition: entitytreemodel.h:331
Akonadi::PartFetcher
Convenience class for getting payload parts from an Akonadi Model.
Definition: partfetcher.h:73
Akonadi::ItemFetchJob
Job that fetches items from the Akonadi storage.
Definition: itemfetchjob.h:82
Akonadi::PartFetcher::index
QModelIndex index() const
Returns the index of the item the part was fetched from.
Definition: partfetcher.cpp:162
Akonadi::PartFetcher::PartFetcher
PartFetcher(const QModelIndex &index, const QByteArray &partName, QObject *parent=0)
Creates a new part fetcher.
Definition: partfetcher.cpp:103
Akonadi::EntityTreeModel::SessionRole
Definition: entitytreemodel.h:344
Akonadi::ItemFetchScope::fetchPayloadPart
void fetchPayloadPart(const QByteArray &part, bool fetch=true)
Sets which payload parts shall be fetched.
Definition: itemfetchscope.cpp:55
Akonadi::PartFetcher::~PartFetcher
virtual ~PartFetcher()
Destroys the part fetcher.
Definition: partfetcher.cpp:108
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal