• 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
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 
162 class ItemPrivate : public EntityPrivate
163 {
164  public:
165  explicit ItemPrivate( Item::Id id = -1 )
166  : EntityPrivate( id ),
167  mLegacyPayload(),
168  mPayloads(),
169  mConversionInProgress( false ),
170  mRevision( -1 ),
171  mCollectionId( -1 ),
172  mSize( 0 ),
173  mModificationTime(),
174  mFlagsOverwritten( false ),
175  mSizeChanged( false ),
176  mClearPayload( false )
177  {
178  }
179 
180 #if 0
181  ItemPrivate( const ItemPrivate &other )
182  : EntityPrivate( other )
183  {
184  mFlags = other.mFlags;
185  mRevision = other.mRevision;
186  mSize = other.mSize;
187  mModificationTime = other.mModificationTime;
188  mMimeType = other.mMimeType;
189  mLegacyPayload = other.mLegacyPayload;
190  mPayloads = other.mPayloads;
191  mConversionInProgress = false;
192  mAddedFlags = other.mAddedFlags;
193  mDeletedFlags = other.mDeletedFlags;
194  mFlagsOverwritten = other.mFlagsOverwritten;
195  mSizeChanged = other.mSizeChanged;
196  mCollectionId = other.mCollectionId;
197  mClearPayload = other.mClearPayload;
198  }
199 #endif
200 
201  ~ItemPrivate()
202  {
203  }
204 
205  void resetChangeLog()
206  {
207  mFlagsOverwritten = false;
208  mAddedFlags.clear();
209  mDeletedFlags.clear();
210  mSizeChanged = false;
211  }
212 
213  EntityPrivate *clone() const
214  {
215  return new ItemPrivate( *this );
216  }
217 
218  bool hasMetaTypeId( int mtid ) const {
219  return std::find_if( mPayloads.begin(), mPayloads.end(),
220  _detail::BySharedPointerAndMetaTypeID( -1, mtid ) )
221  != mPayloads.end();
222  }
223 
224  PayloadBase * payloadBaseImpl( int spid, int mtid ) const {
225  const PayloadContainer::const_iterator it
226  = std::find_if( mPayloads.begin(), mPayloads.end(),
227  _detail::BySharedPointerAndMetaTypeID( spid, mtid ) );
228  return it == mPayloads.end() ? 0 : it->payload.get() ;
229  }
230 
231  bool movePayloadFrom( ItemPrivate * other, int mtid ) const /*sic!*/ {
232  assert( other );
233  const size_t oldSize = mPayloads.size();
234  PayloadContainer & oPayloads = other->mPayloads;
235  const _detail::BySharedPointerAndMetaTypeID matcher( -1, mtid );
236  const size_t numMatching
237  = std::count_if( oPayloads.begin(), oPayloads.end(), matcher );
238  mPayloads.resize( oldSize + numMatching );
239  using namespace std; // for swap()
240  for ( PayloadContainer::iterator
241  dst = mPayloads.begin() + oldSize,
242  src = oPayloads.begin(), end = oPayloads.end() ; src != end ; ++src )
243  if ( matcher( *src ) ) {
244  swap( *dst, *src );
245  ++dst;
246  }
247  return numMatching > 0 ;
248  }
249 
250 #if 0
251  std::auto_ptr<PayloadBase> takePayloadBaseImpl( int spid, int mtid ) {
252  PayloadContainer::iterator it
253  = std::find_if( mPayloads.begin(), mPayloads.end(),
254  _detail::BySharedPointerAndMetaTypeID( spid, mtid ) );
255  if ( it == mPayloads.end() )
256  return std::auto_ptr<PayloadBase>();
257  std::rotate( it, it + 1, mPayloads.end() );
258  std::auto_ptr<PayloadBase> result( it->payload.release() );
259  mPayloads.pop_back();
260  return result;
261  }
262 #endif
263 
264  void setPayloadBaseImpl( int spid, int mtid, std::auto_ptr<PayloadBase> p, bool add ) const /*sic!*/ {
265 
266  if ( !add )
267  mLegacyPayload.reset();
268 
269  if ( !p.get() ) {
270  if ( !add )
271  mPayloads.clear();
272  return;
273  }
274 
275  // if !add, delete all payload variants
276  // (they're conversions of each other)
277  mPayloads.resize( add ? mPayloads.size() + 1 : 1 );
278  _detail::TypedPayload & tp = mPayloads.back();
279  tp.payload.reset( p.release() );
280  tp.sharedPointerId = spid;
281  tp.metaTypeId = mtid;
282  }
283 
284  void setLegacyPayloadBaseImpl( std::auto_ptr<PayloadBase> p );
285  void tryEnsureLegacyPayload() const;
286 
287  mutable _detail::clone_ptr<PayloadBase> mLegacyPayload;
288  mutable PayloadContainer mPayloads;
289  mutable bool mConversionInProgress;
290  int mRevision;
291  Item::Flags mFlags;
292  Entity::Id mCollectionId;
293  qint64 mSize;
294  QDateTime mModificationTime;
295  QString mMimeType;
296  QString mGid;
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:65
Akonadi::ItemPrivate
Definition: item_p.h:162
Akonadi::EntityPrivate
Definition: entity_p.h:39
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