• 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.h
1 /*
2  Copyright (c) 2006 Volker Krause <vkrause@kde.org>
3  2007 Till Adam <adam@kde.org>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #ifndef AKONADI_ITEM_H
22 #define AKONADI_ITEM_H
23 
24 #include "akonadi_export.h"
25 
26 #include <akonadi/entity.h>
27 #include <akonadi/exception.h>
28 #include "itempayloadinternals_p.h"
29 
30 #include <QtCore/QByteArray>
31 #include <QtCore/QMetaType>
32 #include <QtCore/QSet>
33 
34 #include <boost/static_assert.hpp>
35 #include <boost/type_traits/is_pointer.hpp>
36 #include <boost/utility/enable_if.hpp>
37 
38 #include <typeinfo>
39 #include <memory>
40 
41 class KUrl;
42 
43 template <typename T>
44 class QVector;
45 
46 namespace Akonadi {
47 
48 class ItemPrivate;
49 
115 class AKONADI_EXPORT Item : public Entity
116 {
117  public:
121  typedef QList<Item> List;
122 
126  typedef QByteArray Flag;
127 
131  typedef QSet<QByteArray> Flags;
132 
137  static const char* FullPayload;
138 
142  Item();
143 
147  explicit Item( Id id );
148 
154  explicit Item( const QString &mimeType );
155 
159  Item( const Item &other );
160 
164  ~Item();
165 
169  static Item fromUrl( const KUrl &url );
170 
174  Flags flags() const;
175 
180  QDateTime modificationTime() const;
181 
189  void setModificationTime( const QDateTime &datetime );
190 
195  bool hasFlag( const QByteArray &name ) const;
196 
200  void setFlag( const QByteArray &name );
201 
205  void clearFlag( const QByteArray &name );
206 
210  void setFlags( const Flags &flags );
211 
215  void clearFlags();
216 
224  void setPayloadFromData( const QByteArray &data );
225 
232  QByteArray payloadData() const;
233 
238  QSet<QByteArray> loadedPayloadParts() const;
239 
249  void clearPayload();
250 
257  void setRevision( int revision );
258 
262  int revision() const;
263 
272  Entity::Id storageCollectionId() const;
273 
279  void setSize( qint64 size );
280 
286  qint64 size() const;
287 
291  void setMimeType( const QString &mimeType );
292 
296  QString mimeType() const;
297 
303  void setGid( const QString &gid );
304 
310  QString gid() const;
311 
318  QVector<int> availablePayloadMetaTypeIds() const;
319 
331  template <typename T> void setPayload( const T &p );
332  //@cond PRIVATE
333  template <typename T> void setPayload( T* p );
334  template <typename T> void setPayload( std::auto_ptr<T> p );
335  //@endcond
336 
350  template <typename T> T payload() const;
351 
355  bool hasPayload() const;
356 
366  template <typename T> bool hasPayload() const;
367 
371  enum UrlType
372  {
373  UrlShort = 0,
374  UrlWithMimeType = 1
375  };
376 
380  KUrl url( UrlType type = UrlShort ) const;
381 
390  QSet<QByteArray> availablePayloadParts() const;
391 
401  QSet<QByteArray> cachedPayloadParts() const;
402 
416  void apply( const Item &other );
417 
427  template <typename T> static void addToLegacyMapping( const QString & mimeType );
428  void setCachedPayloadParts(const QSet<QByteArray> &cachedParts);
429 
430  private:
431  //@cond PRIVATE
432  friend class ItemCreateJob;
433  friend class ItemModifyJob;
434  friend class ItemModifyJobPrivate;
435  friend class ItemSync;
436  friend class ProtocolHelper;
437  PayloadBase* payloadBase() const;
438  void setPayloadBase( PayloadBase* );
439  PayloadBase* payloadBaseV2( int sharedPointerId, int metaTypeId ) const;
440  //std::auto_ptr<PayloadBase> takePayloadBase( int sharedPointerId, int metaTypeId );
441  void setPayloadBaseV2( int sharedPointerId, int metaTypeId, std::auto_ptr<PayloadBase> p );
442  void addPayloadBaseVariant( int sharedPointerId, int metaTypeId, std::auto_ptr<PayloadBase> p ) const;
443  static void addToLegacyMappingImpl( const QString & mimeType, int sharedPointerId, int metaTypeId, std::auto_ptr<PayloadBase> p );
444 
449  bool ensureMetaTypeId( int mtid ) const;
450 
451  template <typename T>
452  typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,void>::type
453  setPayloadImpl( const T &, const int * /*disambiguate*/ = 0 );
454  template <typename T>
455  typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,void>::type
456  setPayloadImpl( const T & );
457 
458  template <typename T>
459  typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
460  payloadImpl( const int * /*disambiguate*/ = 0 ) const;
461  template <typename T>
462  typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
463  payloadImpl() const;
464 
465  template <typename T>
466  typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,bool>::type
467  hasPayloadImpl( const int * /*disambiguate*/ = 0 ) const;
468  template <typename T>
469  typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,bool>::type
470  hasPayloadImpl() const;
471 
472  template <typename T>
473  typename boost::enable_if<Internal::is_shared_pointer<T>,bool>::type
474  tryToClone( T *, const int * /*disambiguate*/ = 0 ) const;
475  template <typename T>
476  typename boost::disable_if<Internal::is_shared_pointer<T>,bool>::type
477  tryToClone( T * ) const;
478 
484  void setStorageCollectionId( Entity::Id collectionId);
485 
486 #if 0
487 
490  QString payloadExceptionText( int spid, int mtid ) const;
491 
497  inline void throwPayloadException( int spid, int mtid ) const {
498  throw PayloadException( payloadExceptionText( spid, mtid ) );
499  }
500 #else
501  void throwPayloadException( int spid, int mtid ) const;
502 #endif
503  //@endcond
504 
505  AKONADI_DECLARE_PRIVATE( Item )
506 };
507 
508 template <typename T>
509 T Item::payload() const
510 {
511  BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
512 
513  if ( !hasPayload() )
514  throwPayloadException( -1, -1 );
515 
516  return payloadImpl<T>();
517 }
518 
519 template <typename T>
520 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
521 Item::payloadImpl( const int * ) const
522 {
523  typedef Internal::PayloadTrait<T> PayloadType;
524  BOOST_STATIC_ASSERT(( PayloadType::isPolymorphic ));
525 
526  typedef typename Internal::get_hierarchy_root<T>::type Root_T;
527  typedef Internal::PayloadTrait<Root_T> RootType;
528  BOOST_STATIC_ASSERT(( !RootType::isPolymorphic )); // prevent endless recursion
529 
530  return PayloadType::castFrom( payloadImpl<Root_T>() );
531 }
532 
533 template <typename T>
534 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,T>::type
535 Item::payloadImpl() const
536 {
537  typedef Internal::PayloadTrait<T> PayloadType;
538  BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
539 
540  const int metaTypeId = PayloadType::elementMetaTypeId();
541 
542  // make sure that we have a payload format represented by 'metaTypeId':
543  if ( !ensureMetaTypeId( metaTypeId ) )
544  throwPayloadException( PayloadType::sharedPointerId, metaTypeId );
545 
546  // Check whether we have the exact payload
547  // (metatype id and shared pointer type match)
548  if ( const Payload<T> * const p = Internal::payload_cast<T>( payloadBaseV2( PayloadType::sharedPointerId, metaTypeId ) ) )
549  return p->payload;
550 
551  T ret;
552  if ( !tryToClone<T>( &ret ) )
553  throwPayloadException( PayloadType::sharedPointerId, metaTypeId );
554  return ret;
555 }
556 
557 template <typename T>
558 typename boost::enable_if<Internal::is_shared_pointer<T>,bool>::type
559 Item::tryToClone( T * ret, const int * ) const
560 {
561  typedef Internal::PayloadTrait<T> PayloadType;
562  BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
563 
564  const int metaTypeId = PayloadType::elementMetaTypeId();
565 
566  // Check whether we have the same payload in 'the other
567  // shared pointer' (### make it recurse, trying to find one, but
568  // don't introduce infinite recursion):
569  typedef typename Internal::shared_pointer_traits<T>::next_shared_ptr NewT;
570  typedef Internal::PayloadTrait<NewT> NewPayloadType;
571 
572  if ( const Payload<NewT> * const p = Internal::payload_cast<NewT>( payloadBaseV2( NewPayloadType::sharedPointerId, metaTypeId ) ) ) {
573  // If found, attempt to make a clone (required the payload to provide virtual T * T::clone() const)
574  const T nt = PayloadType::clone( p->payload );
575  if ( !PayloadType::isNull( nt ) ) {
576  // if clone succeeded, add the clone to the Item:
577  std::auto_ptr<PayloadBase> npb( new Payload<T>( nt ) );
578  addPayloadBaseVariant( PayloadType::sharedPointerId, metaTypeId, npb );
579  // and return it
580  if ( ret ) {
581  *ret = nt;
582  }
583  return true;
584  }
585  }
586 
587  return false;
588 }
589 
590 template <typename T>
591 typename boost::disable_if<Internal::is_shared_pointer<T>, bool>::type
592 Item::tryToClone( T * ) const
593 {
594  typedef Internal::PayloadTrait<T> PayloadType;
595  BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
596 
597  return false;
598 }
599 
600 template <typename T>
601 bool Item::hasPayload() const
602 {
603  BOOST_STATIC_ASSERT( !boost::is_pointer<T>::value );
604  return hasPayload() && hasPayloadImpl<T>();
605 }
606 
607 template <typename T>
608 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic,bool>::type
609 Item::hasPayloadImpl( const int * ) const
610 {
611  typedef Internal::PayloadTrait<T> PayloadType;
612  BOOST_STATIC_ASSERT(( PayloadType::isPolymorphic ));
613 
614  typedef typename Internal::get_hierarchy_root<T>::type Root_T;
615  typedef Internal::PayloadTrait<Root_T> RootType;
616  BOOST_STATIC_ASSERT(( !RootType::isPolymorphic )); // prevent endless recursion
617 
618  try {
619  return hasPayloadImpl<Root_T>()
620  && PayloadType::canCastFrom( payload<Root_T>() );
621  } catch ( const Akonadi::PayloadException & ) {
622  return false;
623  }
624 }
625 
626 template <typename T>
627 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic,bool>::type
628 Item::hasPayloadImpl() const
629 {
630  typedef Internal::PayloadTrait<T> PayloadType;
631  BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
632 
633  const int metaTypeId = PayloadType::elementMetaTypeId();
634 
635  // make sure that we have a payload format represented by 'metaTypeId':
636  if ( !ensureMetaTypeId( metaTypeId ) )
637  return false;
638 
639  // Check whether we have the exact payload
640  // (metatype id and shared pointer type match)
641  if ( const Payload<T> * const p = Internal::payload_cast<T>( payloadBaseV2( PayloadType::sharedPointerId, metaTypeId ) ) )
642  return true;
643 
644  return tryToClone<T>( 0 );
645 }
646 
647 template <typename T>
648 void Item::setPayload( const T &p )
649 {
650  BOOST_STATIC_ASSERT(( !boost::is_pointer<T>::value ));
651  setPayloadImpl( p );
652 }
653 
654 template <typename T>
655 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic>::type
656 Item::setPayloadImpl( const T & p, const int * )
657 {
658  typedef Internal::PayloadTrait<T> PayloadType;
659  BOOST_STATIC_ASSERT(( PayloadType::isPolymorphic ));
660 
661  typedef typename Internal::get_hierarchy_root<T>::type Root_T;
662  typedef Internal::PayloadTrait<Root_T> RootType;
663  BOOST_STATIC_ASSERT(( !RootType::isPolymorphic )); // prevent endless recursion
664 
665  setPayloadImpl<Root_T>( p );
666 }
667 
668 template <typename T>
669 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic>::type
670 Item::setPayloadImpl( const T & p )
671 {
672  typedef Internal::PayloadTrait<T> PayloadType;
673  std::auto_ptr<PayloadBase> pb( new Payload<T>( p ) );
674  setPayloadBaseV2( PayloadType::sharedPointerId,
675  PayloadType::elementMetaTypeId(),
676  pb );
677 }
678 
679 template <typename T>
680 void Item::setPayload( T* p )
681 {
682  p->You_MUST_NOT_use_a_pointer_as_payload;
683 }
684 
685 template <typename T>
686 void Item::setPayload( std::auto_ptr<T> p )
687 {
688  p.Nice_try_but_a_std_auto_ptr_is_not_allowed_as_payload_either;
689 }
690 
691 template <typename T>
692 void Item::addToLegacyMapping( const QString & mimeType ) {
693  typedef Internal::PayloadTrait<T> PayloadType;
694  BOOST_STATIC_ASSERT(( !PayloadType::isPolymorphic ));
695  std::auto_ptr<PayloadBase> p( new Payload<T> );
696  addToLegacyMappingImpl( mimeType, PayloadType::sharedPointerId, PayloadType::elementMetaTypeId(), p );
697 }
698 
699 }
700 
701 Q_DECLARE_METATYPE(Akonadi::Item)
702 Q_DECLARE_METATYPE(Akonadi::Item::List)
703 
704 #endif
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:65
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