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

Skip menu "kdepimlibs-4.13.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