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

akonadi

  • akonadi
item_p.h
1 /*
2  Copyright (c) 2008 Tobias Koenig <tokoe@kde.org>
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 #ifndef AKONADI_ITEM_P_H
21 #define AKONADI_ITEM_P_H
22 
23 #include <QtCore/QDateTime>
24 #include <QtCore/QMap>
25 #include <QtCore/QVarLengthArray>
26 
27 #include "entity_p.h"
28 #include "itempayloadinternals_p.h"
29 
30 #include <boost/bind.hpp>
31 
32 #include <memory>
33 #include <algorithm>
34 #include <cassert>
35 #include <vector>
36 
37 namespace Akonadi {
38 
39 namespace _detail {
40 
41  template <typename T>
42  class clone_ptr {
43  T * t;
44  public:
45  clone_ptr() : t( 0 ) {}
46  explicit clone_ptr( T * t ) : t( t ) {}
47  clone_ptr( const clone_ptr & other ) : t ( other.t ? other.t->clone() : 0 ) {}
48  ~clone_ptr() { delete t; }
49  clone_ptr & operator=( const clone_ptr & other ) {
50  if ( this != &other ) {
51  clone_ptr copy( other );
52  swap( copy );
53  }
54  return *this;
55  }
56  void swap( clone_ptr & other ) {
57  using std::swap;
58  swap( t, other.t );
59  }
60  T * operator->() const { return get(); }
61  T & operator*() const { assert( get() != 0 ); return *get(); }
62  T * get() const { return t; }
63  T * release() { T * const r = t; t = 0; return r; }
64  void reset( T * other=0 ) { delete t; t = other; }
65 
66  private:
67  struct _save_bool { void f() {}; };
68  typedef void (_save_bool::*save_bool)();
69  public:
70  operator save_bool () const { return get() ? &_save_bool::f : 0 ; }
71  };
72 
73  template <typename T>
74  inline void swap( clone_ptr<T> & lhs, clone_ptr<T> & rhs ) {
75  lhs.swap( rhs );
76  }
77 
78  template <typename T, size_t N>
79  class VarLengthArray {
80  QVarLengthArray<T,N> impl; // ###should be replaced by self-written container that doesn't waste so much space
81  public:
82  typedef T value_type;
83  typedef T* iterator;
84  typedef const T* const_iterator;
85  typedef T* pointer;
86  typedef const T* const_pointer;
87  typedef T & reference;
88  typedef const T & const_reference;
89 
90  explicit VarLengthArray( int size=0 ) : impl( size ) {}
91  // compiler-generated dtor, copy ctor, copy assignment are ok
92  // swap() makes little sense
93 
94  void push_back( const T & t ) { impl.append( t ); }
95  int capacity() const { return impl.capacity(); }
96  void clear() { impl.clear(); }
97  size_t size() const { return impl.count(); }
98  bool empty() const { return impl.isEmpty(); }
99  void pop_back() { return impl.removeLast(); }
100  void reserve( size_t n ) { impl.reserve( n ); }
101  void resize( size_t n ) { impl.resize( n ); }
102 
103  iterator begin() { return impl.data(); }
104  iterator end() { return impl.data() + impl.size(); }
105  const_iterator begin() const { return impl.data(); }
106  const_iterator end() const { return impl.data() + impl.size(); }
107  const_iterator cbegin() const { return begin(); }
108  const_iterator cend() const { return end(); }
109 
110  reference front() { return *impl.data(); }
111  reference back() { return *(impl.data()+impl.size()); }
112  const_reference front() const { return *impl.data(); }
113  const_reference back() const { return *(impl.data()+impl.size()); }
114 
115  reference operator[]( size_t n ) { return impl[n]; }
116  const_reference operator[]( size_t n ) const { return impl[n]; }
117  };
118 
119  struct TypedPayload {
120  clone_ptr<PayloadBase> payload;
121  int sharedPointerId;
122  int metaTypeId;
123  };
124 
125  struct BySharedPointerAndMetaTypeID : std::unary_function<TypedPayload,bool> {
126  const int spid;
127  const int mtid;
128  BySharedPointerAndMetaTypeID( int spid, int mtid ) : spid( spid ), mtid( mtid ) {}
129  bool operator()( const TypedPayload & tp ) const {
130  return ( mtid == -1 || mtid == tp.metaTypeId )
131  && ( spid == -1 || spid == tp.sharedPointerId ) ;
132  }
133  };
134 
135 }
136 
137 } // namespace Akonadi
138 
139 namespace std {
140  template <>
141  inline void swap<Akonadi::_detail::TypedPayload>( Akonadi::_detail::TypedPayload & lhs, Akonadi::_detail::TypedPayload & rhs ) {
142  lhs.payload.swap( rhs.payload );
143  swap( lhs.sharedPointerId, rhs.sharedPointerId );
144  swap( lhs.metaTypeId, rhs.metaTypeId );
145  }
146 }
147 
148 namespace Akonadi {
149 //typedef _detail::VarLengthArray<_detail::TypedPayload,2> PayloadContainer;
150 typedef std::vector<_detail::TypedPayload> PayloadContainer;
151 }
152 
153 // disable Q_FOREACH on PayloadContainer (b/c it likes to take copies and clone_ptr doesn't like that)
154 template <>
155 class QForeachContainer<Akonadi::PayloadContainer> {};
156 
157 namespace Akonadi {
158 
159 
163 class ItemPrivate : public EntityPrivate
164 {
165  public:
166  explicit ItemPrivate( Item::Id id = -1 )
167  : EntityPrivate( id ),
168  mLegacyPayload(),
169  mPayloads(),
170  mConversionInProgress( false ),
171  mRevision( -1 ),
172  mCollectionId( -1 ),
173  mSize( 0 ),
174  mModificationTime(),
175  mFlagsOverwritten( false ),
176  mSizeChanged( false ),
177  mClearPayload( false )
178  {
179  }
180 
181 #if 0
182  ItemPrivate( const ItemPrivate &other )
183  : EntityPrivate( other )
184  {
185  mFlags = other.mFlags;
186  mRevision = other.mRevision;
187  mSize = other.mSize;
188  mModificationTime = other.mModificationTime;
189  mMimeType = other.mMimeType;
190  mLegacyPayload = other.mLegacyPayload;
191  mPayloads = other.mPayloads;
192  mConversionInProgress = false;
193  mAddedFlags = other.mAddedFlags;
194  mDeletedFlags = other.mDeletedFlags;
195  mFlagsOverwritten = other.mFlagsOverwritten;
196  mSizeChanged = other.mSizeChanged;
197  mCollectionId = other.mCollectionId;
198  mClearPayload = other.mClearPayload;
199  }
200 #endif
201 
202  ~ItemPrivate()
203  {
204  }
205 
206  void resetChangeLog()
207  {
208  mFlagsOverwritten = false;
209  mAddedFlags.clear();
210  mDeletedFlags.clear();
211  mSizeChanged = false;
212  }
213 
214  EntityPrivate *clone() const
215  {
216  return new ItemPrivate( *this );
217  }
218 
219  bool hasMetaTypeId( int mtid ) const {
220  return std::find_if( mPayloads.begin(), mPayloads.end(),
221  _detail::BySharedPointerAndMetaTypeID( -1, mtid ) )
222  != mPayloads.end();
223  }
224 
225  PayloadBase * payloadBaseImpl( int spid, int mtid ) const {
226  const PayloadContainer::const_iterator it
227  = std::find_if( mPayloads.begin(), mPayloads.end(),
228  _detail::BySharedPointerAndMetaTypeID( spid, mtid ) );
229  return it == mPayloads.end() ? 0 : it->payload.get() ;
230  }
231 
232  bool movePayloadFrom( ItemPrivate * other, int mtid ) const /*sic!*/ {
233  assert( other );
234  const size_t oldSize = mPayloads.size();
235  PayloadContainer & oPayloads = other->mPayloads;
236  const _detail::BySharedPointerAndMetaTypeID matcher( -1, mtid );
237  const size_t numMatching
238  = std::count_if( oPayloads.begin(), oPayloads.end(), matcher );
239  mPayloads.resize( oldSize + numMatching );
240  using namespace std; // for swap()
241  for ( PayloadContainer::iterator
242  dst = mPayloads.begin() + oldSize,
243  src = oPayloads.begin(), end = oPayloads.end() ; src != end ; ++src )
244  if ( matcher( *src ) ) {
245  swap( *dst, *src );
246  ++dst;
247  }
248  return numMatching > 0 ;
249  }
250 
251 #if 0
252  std::auto_ptr<PayloadBase> takePayloadBaseImpl( int spid, int mtid ) {
253  PayloadContainer::iterator it
254  = std::find_if( mPayloads.begin(), mPayloads.end(),
255  _detail::BySharedPointerAndMetaTypeID( spid, mtid ) );
256  if ( it == mPayloads.end() )
257  return std::auto_ptr<PayloadBase>();
258  std::rotate( it, it + 1, mPayloads.end() );
259  std::auto_ptr<PayloadBase> result( it->payload.release() );
260  mPayloads.pop_back();
261  return result;
262  }
263 #endif
264 
265  void setPayloadBaseImpl( int spid, int mtid, std::auto_ptr<PayloadBase> p, bool add ) const /*sic!*/ {
266 
267  if ( !add )
268  mLegacyPayload.reset();
269 
270  if ( !p.get() ) {
271  if ( !add )
272  mPayloads.clear();
273  return;
274  }
275 
276  // if !add, delete all payload variants
277  // (they're conversions of each other)
278  mPayloads.resize( add ? mPayloads.size() + 1 : 1 );
279  _detail::TypedPayload & tp = mPayloads.back();
280  tp.payload.reset( p.release() );
281  tp.sharedPointerId = spid;
282  tp.metaTypeId = mtid;
283  }
284 
285  void setLegacyPayloadBaseImpl( std::auto_ptr<PayloadBase> p );
286  void tryEnsureLegacyPayload() const;
287 
288  mutable _detail::clone_ptr<PayloadBase> mLegacyPayload;
289  mutable PayloadContainer mPayloads;
290  mutable bool mConversionInProgress;
291  int mRevision;
292  Item::Flags mFlags;
293  Entity::Id mCollectionId;
294  qint64 mSize;
295  QDateTime mModificationTime;
296  QString mMimeType;
297  Item::Flags mAddedFlags;
298  Item::Flags mDeletedFlags;
299  QSet<QByteArray> mCachedPayloadParts;
300  bool mFlagsOverwritten : 1;
301  bool mSizeChanged : 1;
302  bool mClearPayload : 1;
303 };
304 
305 }
306 
307 #endif
308 
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:64
Akonadi::ItemPrivate
Definition: item_p.h:163
Akonadi::EntityPrivate
Definition: entity_p.h:39
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:17 by doxygen 1.8.5 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-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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