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

akonadi

  • sources
  • kde-4.14
  • 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 <akonadi/tag.h>
29 #include <akonadi/collection.h>
30 #include "itempayloadinternals_p.h"
31 
32 #include <QtCore/QByteArray>
33 #include <QtCore/QMetaType>
34 #include <QtCore/QSet>
35 
36 #include <boost/static_assert.hpp>
37 #include <boost/type_traits/is_pointer.hpp>
38 #include <boost/utility/enable_if.hpp>
39 
40 #include <typeinfo>
41 #include <memory>
42 
43 class KUrl;
44 
45 template <typename T>
46 class QVector;
47 
48 namespace Akonadi {
49 
50 class ItemPrivate;
51 
117 class AKONADI_EXPORT Item : public Entity
118 {
119 public:
123  typedef QList<Item> List;
124 
128  typedef QByteArray Flag;
129 
133  typedef QSet<QByteArray> Flags;
134 
139  static const char *FullPayload;
140 
144  Item();
145 
149  explicit Item(Id id);
150 
156  explicit Item(const QString &mimeType);
157 
161  Item(const Item &other);
162 
166  ~Item();
167 
171  static Item fromUrl(const KUrl &url);
172 
176  Flags flags() const;
177 
182  QDateTime modificationTime() const;
183 
191  void setModificationTime(const QDateTime &datetime);
192 
197  bool hasFlag(const QByteArray &name) const;
198 
202  void setFlag(const QByteArray &name);
203 
207  void clearFlag(const QByteArray &name);
208 
212  void setFlags(const Flags &flags);
213 
217  void clearFlags();
218 
219  void setTags(const Tag::List &list);
220 
221  void setTag(const Tag &tag);
222 
223  Tag::List tags() const;
224 
225  bool hasTag(const Tag &tag) const;
226 
227  void clearTag(const Tag &tag);
228 
229  void clearTags();
230 
238  void setPayloadFromData(const QByteArray &data);
239 
246  QByteArray payloadData() const;
247 
252  QSet<QByteArray> loadedPayloadParts() const;
253 
263  void clearPayload();
264 
271  void setRevision(int revision);
272 
276  int revision() const;
277 
286  Entity::Id storageCollectionId() const;
287 
293  void setSize(qint64 size);
294 
300  qint64 size() const;
301 
305  void setMimeType(const QString &mimeType);
306 
310  QString mimeType() const;
311 
317  void setGid(const QString &gid);
318 
324  QString gid() const;
325 
335  void setVirtualReferences(const Collection::List &collections);
336 
346  Collection::List virtualReferences() const;
347 
354  QVector<int> availablePayloadMetaTypeIds() const;
355 
367  template <typename T> void setPayload(const T &p);
368  //@cond PRIVATE
369  template <typename T> void setPayload(T *p);
370  template <typename T> void setPayload(std::auto_ptr<T> p);
371  //@endcond
372 
386  template <typename T> T payload() const;
387 
391  bool hasPayload() const;
392 
402  template <typename T> bool hasPayload() const;
403 
407  enum UrlType {
408  UrlShort = 0,
409  UrlWithMimeType = 1
410  };
411 
415  KUrl url(UrlType type = UrlShort) const;
416 
425  QSet<QByteArray> availablePayloadParts() const;
426 
436  QSet<QByteArray> cachedPayloadParts() const;
437 
451  void apply(const Item &other);
452 
462  template <typename T> static void addToLegacyMapping(const QString &mimeType);
463  void setCachedPayloadParts(const QSet<QByteArray> &cachedParts);
464 
465 private:
466  //@cond PRIVATE
467  friend class ItemCreateJob;
468  friend class ItemModifyJob;
469  friend class ItemModifyJobPrivate;
470  friend class ItemSync;
471  friend class ProtocolHelper;
472  PayloadBase *payloadBase() const;
473  void setPayloadBase(PayloadBase *p);
474  PayloadBase *payloadBaseV2(int sharedPointerId, int metaTypeId) const;
475  //std::auto_ptr<PayloadBase> takePayloadBase( int sharedPointerId, int metaTypeId );
476  void setPayloadBaseV2(int sharedPointerId, int metaTypeId, std::auto_ptr<PayloadBase> p);
477  void addPayloadBaseVariant(int sharedPointerId, int metaTypeId, std::auto_ptr<PayloadBase> p) const;
478  static void addToLegacyMappingImpl(const QString &mimeType, int sharedPointerId, int metaTypeId, std::auto_ptr<PayloadBase> p);
479 
484  bool ensureMetaTypeId(int mtid) const;
485 
486  template <typename T>
487  typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic, void>::type
488  setPayloadImpl(const T &p, const int * /*disambiguate*/ = 0);
489  template <typename T>
490  typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic, void>::type
491  setPayloadImpl(const T &p);
492 
493  template <typename T>
494  typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic, T>::type
495  payloadImpl(const int * /*disambiguate*/ = 0) const;
496  template <typename T>
497  typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic, T>::type
498  payloadImpl() const;
499 
500  template <typename T>
501  typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic, bool>::type
502  hasPayloadImpl(const int * /*disambiguate*/ = 0) const;
503  template <typename T>
504  typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic, bool>::type
505  hasPayloadImpl() const;
506 
507  template <typename T>
508  typename boost::enable_if<Internal::is_shared_pointer<T>, bool>::type
509  tryToClone(T *ret, const int * /*disambiguate*/ = 0) const;
510  template <typename T>
511  typename boost::disable_if<Internal::is_shared_pointer<T>, bool>::type
512  tryToClone(T *) const;
513 
519  void setStorageCollectionId(Entity::Id collectionId);
520 
521 #if 0
522 
525  QString payloadExceptionText(int spid, int mtid) const;
526 
532  inline void throwPayloadException(int spid, int mtid) const {
533  throw PayloadException(payloadExceptionText(spid, mtid));
534  }
535 #else
536  void throwPayloadException(int spid, int mtid) const;
537 #endif
538  //@endcond
539 
540  AKONADI_DECLARE_PRIVATE(Item)
541 };
542 
543 template <typename T>
544 T Item::payload() const
545 {
546  BOOST_STATIC_ASSERT(!boost::is_pointer<T>::value);
547 
548  if (!hasPayload()) {
549  throwPayloadException(-1, -1);
550  }
551 
552  return payloadImpl<T>();
553 }
554 
555 template <typename T>
556 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic, T>::type
557 Item::payloadImpl(const int *) const
558 {
559  typedef Internal::PayloadTrait<T> PayloadType;
560  BOOST_STATIC_ASSERT((PayloadType::isPolymorphic));
561 
562  typedef typename Internal::get_hierarchy_root<T>::type Root_T;
563  typedef Internal::PayloadTrait<Root_T> RootType;
564  BOOST_STATIC_ASSERT((!RootType::isPolymorphic)); // prevent endless recursion
565 
566  return PayloadType::castFrom(payloadImpl<Root_T>());
567 }
568 
569 template <typename T>
570 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic, T>::type
571 Item::payloadImpl() const
572 {
573  typedef Internal::PayloadTrait<T> PayloadType;
574  BOOST_STATIC_ASSERT((!PayloadType::isPolymorphic));
575 
576  const int metaTypeId = PayloadType::elementMetaTypeId();
577 
578  // make sure that we have a payload format represented by 'metaTypeId':
579  if (!ensureMetaTypeId(metaTypeId)) {
580  throwPayloadException(PayloadType::sharedPointerId, metaTypeId);
581  }
582 
583  // Check whether we have the exact payload
584  // (metatype id and shared pointer type match)
585  if (const Payload<T> *const p = Internal::payload_cast<T>(payloadBaseV2(PayloadType::sharedPointerId, metaTypeId))) {
586  return p->payload;
587  }
588 
589  T ret;
590  if (!tryToClone<T>(&ret)) {
591  throwPayloadException(PayloadType::sharedPointerId, metaTypeId);
592  }
593  return ret;
594 }
595 
596 template <typename T>
597 typename boost::enable_if<Internal::is_shared_pointer<T>, bool>::type
598 Item::tryToClone(T *ret, const int *) const
599 {
600  typedef Internal::PayloadTrait<T> PayloadType;
601  BOOST_STATIC_ASSERT((!PayloadType::isPolymorphic));
602 
603  const int metaTypeId = PayloadType::elementMetaTypeId();
604 
605  // Check whether we have the same payload in 'the other
606  // shared pointer' (### make it recurse, trying to find one, but
607  // don't introduce infinite recursion):
608  typedef typename Internal::shared_pointer_traits<T>::next_shared_ptr NewT;
609  typedef Internal::PayloadTrait<NewT> NewPayloadType;
610 
611  if (const Payload<NewT> *const p = Internal::payload_cast<NewT>(payloadBaseV2(NewPayloadType::sharedPointerId, metaTypeId))) {
612  // If found, attempt to make a clone (required the payload to provide virtual T * T::clone() const)
613  const T nt = PayloadType::clone(p->payload);
614  if (!PayloadType::isNull(nt)) {
615  // if clone succeeded, add the clone to the Item:
616  std::auto_ptr<PayloadBase> npb(new Payload<T>(nt));
617  addPayloadBaseVariant(PayloadType::sharedPointerId, metaTypeId, npb);
618  // and return it
619  if (ret) {
620  *ret = nt;
621  }
622  return true;
623  }
624  }
625 
626  return false;
627 }
628 
629 template <typename T>
630 typename boost::disable_if<Internal::is_shared_pointer<T>, bool>::type
631 Item::tryToClone(T *) const
632 {
633  typedef Internal::PayloadTrait<T> PayloadType;
634  BOOST_STATIC_ASSERT((!PayloadType::isPolymorphic));
635 
636  return false;
637 }
638 
639 template <typename T>
640 bool Item::hasPayload() const
641 {
642  BOOST_STATIC_ASSERT(!boost::is_pointer<T>::value);
643  return hasPayload() && hasPayloadImpl<T>();
644 }
645 
646 template <typename T>
647 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic, bool>::type
648 Item::hasPayloadImpl(const int *) const
649 {
650  typedef Internal::PayloadTrait<T> PayloadType;
651  BOOST_STATIC_ASSERT((PayloadType::isPolymorphic));
652 
653  typedef typename Internal::get_hierarchy_root<T>::type Root_T;
654  typedef Internal::PayloadTrait<Root_T> RootType;
655  BOOST_STATIC_ASSERT((!RootType::isPolymorphic)); // prevent endless recursion
656 
657  try {
658  return hasPayloadImpl<Root_T>()
659  && PayloadType::canCastFrom(payload<Root_T>());
660  } catch (const Akonadi::PayloadException &) {
661  return false;
662  }
663 }
664 
665 template <typename T>
666 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic, bool>::type
667 Item::hasPayloadImpl() const
668 {
669  typedef Internal::PayloadTrait<T> PayloadType;
670  BOOST_STATIC_ASSERT((!PayloadType::isPolymorphic));
671 
672  const int metaTypeId = PayloadType::elementMetaTypeId();
673 
674  // make sure that we have a payload format represented by 'metaTypeId':
675  if (!ensureMetaTypeId(metaTypeId)) {
676  return false;
677  }
678 
679  // Check whether we have the exact payload
680  // (metatype id and shared pointer type match)
681  if (const Payload<T> *const p = Internal::payload_cast<T>(payloadBaseV2(PayloadType::sharedPointerId, metaTypeId))) {
682  return true;
683  }
684 
685  return tryToClone<T>(0);
686 }
687 
688 template <typename T>
689 void Item::setPayload(const T &p)
690 {
691  BOOST_STATIC_ASSERT((!boost::is_pointer<T>::value));
692  setPayloadImpl(p);
693 }
694 
695 template <typename T>
696 typename boost::enable_if_c<Internal::PayloadTrait<T>::isPolymorphic>::type
697 Item::setPayloadImpl(const T &p, const int *)
698 {
699  typedef Internal::PayloadTrait<T> PayloadType;
700  BOOST_STATIC_ASSERT((PayloadType::isPolymorphic));
701 
702  typedef typename Internal::get_hierarchy_root<T>::type Root_T;
703  typedef Internal::PayloadTrait<Root_T> RootType;
704  BOOST_STATIC_ASSERT((!RootType::isPolymorphic)); // prevent endless recursion
705 
706  setPayloadImpl<Root_T>(p);
707 }
708 
709 template <typename T>
710 typename boost::disable_if_c<Internal::PayloadTrait<T>::isPolymorphic>::type
711 Item::setPayloadImpl(const T &p)
712 {
713  typedef Internal::PayloadTrait<T> PayloadType;
714  std::auto_ptr<PayloadBase> pb(new Payload<T>(p));
715  setPayloadBaseV2(PayloadType::sharedPointerId,
716  PayloadType::elementMetaTypeId(),
717  pb);
718 }
719 
720 template <typename T>
721 void Item::setPayload(T *p)
722 {
723  p->You_MUST_NOT_use_a_pointer_as_payload;
724 }
725 
726 template <typename T>
727 void Item::setPayload(std::auto_ptr<T> p)
728 {
729  p.Nice_try_but_a_std_auto_ptr_is_not_allowed_as_payload_either;
730 }
731 
732 template <typename T>
733 void Item::addToLegacyMapping(const QString &mimeType) {
734  typedef Internal::PayloadTrait<T> PayloadType;
735  BOOST_STATIC_ASSERT((!PayloadType::isPolymorphic));
736  std::auto_ptr<PayloadBase> p(new Payload<T>);
737  addToLegacyMappingImpl(mimeType, PayloadType::sharedPointerId, PayloadType::elementMetaTypeId(), p);
738 }
739 
740 }
741 
742 Q_DECLARE_METATYPE(Akonadi::Item)
743 Q_DECLARE_METATYPE(Akonadi::Item::List)
744 
745 #endif
QByteArray
Akonadi::Entity::Id
qint64 Id
Describes the unique id type.
Definition: entity.h:65
QSet< QByteArray >
QString
QList
QVector
Definition: kcolumnfilterproxymodel_p.h:27
Akonadi::Collection::List
QList< Collection > List
Describes a list of collections.
Definition: collection.h:81
QDateTime
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:38:03 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
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2

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