File indexing completed on 2024-06-16 04:50:14

0001 /*
0002     SPDX-FileCopyrightText: 2007 Till Adam <adam@kde.org>
0003     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <QByteArray>
0011 #include <QSet>
0012 
0013 #include "akonaditests_export.h"
0014 
0015 #include "itemserializerplugin.h"
0016 
0017 #include <memory>
0018 
0019 class QIODevice;
0020 
0021 namespace Akonadi
0022 {
0023 class Item;
0024 
0025 /**
0026   @internal
0027   Serialization/Deserialization of item parts, serializer plugin management.
0028 */
0029 class AKONADI_TESTS_EXPORT ItemSerializer
0030 {
0031 public:
0032     enum PayloadStorage {
0033         Internal,
0034         External,
0035         Foreign,
0036     };
0037 
0038     /** throws ItemSerializerException on failure */
0039     static void deserialize(Item &item, const QByteArray &label, const QByteArray &data, int version, PayloadStorage storage);
0040     /** throws ItemSerializerException on failure */
0041     static void deserialize(Item &item, const QByteArray &label, QIODevice &data, int version);
0042     /** throws ItemSerializerException on failure */
0043     static void serialize(const Item &item, const QByteArray &label, QByteArray &data, int &version);
0044     /** throws ItemSerializerException on failure */
0045     static void serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version);
0046 
0047     /**
0048      * Throws ItemSerializerException on failure.
0049      * @param item the item to apply to
0050      * @param other the item to get values from
0051      * @since 4.4
0052      */
0053     static void apply(Item &item, const Item &other);
0054 
0055     /**
0056      * Returns a list of parts available in the item payload.
0057      */
0058     static QSet<QByteArray> parts(const Item &item);
0059 
0060     /**
0061      * Returns a list of parts available remotely in the item payload.
0062      * @param item the item for which to list payload parts
0063      * @since 4.4
0064      */
0065     static QSet<QByteArray> availableParts(const Item &item);
0066 
0067     /**
0068      * Returns list of parts of the item payload that can be stored using
0069      * foreign payload.
0070      *
0071      * @since 5.7
0072      */
0073     static QSet<QByteArray> allowedForeignParts(const Item &item);
0074 
0075     /**
0076      * Tries to convert the payload in \a item into type with
0077      * metatype-id \a metaTypeId.
0078      * Throws ItemSerializerException or returns an Item w/o payload on failure.
0079      * @param item the item to convert
0080      * @param metaTypeId the meta type id used to convert items payload
0081      * @since 4.6
0082      */
0083     static Item convert(const Item &item, int metaTypeId);
0084 
0085     /**
0086      * Override the plugin-lookup with @p plugin.
0087      *
0088      * After calling this each lookup will always return @p plugin.
0089      * This is useful to inject a special plugin for testing purposes.
0090      * To reset the plugin, set to 0.
0091      *
0092      * @since 4.12
0093      */
0094     static void overridePluginLookup(QObject *plugin);
0095 };
0096 
0097 /**
0098   @internal
0099   Default implementation for serializer plugin.
0100 */
0101 class DefaultItemSerializerPlugin : public QObject, public ItemSerializerPlugin
0102 {
0103     Q_OBJECT
0104     Q_INTERFACES(Akonadi::ItemSerializerPlugin)
0105 public:
0106     DefaultItemSerializerPlugin();
0107 
0108     bool deserialize(Item &item, const QByteArray &label, QIODevice &data, int version) override;
0109     void serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version) override;
0110 };
0111 
0112 /**
0113  @internal
0114  Serializer plugin implementation for std::string
0115 */
0116 class StdStringItemSerializerPlugin : public QObject, public ItemSerializerPlugin
0117 {
0118     Q_OBJECT
0119     Q_INTERFACES(Akonadi::ItemSerializerPlugin)
0120 public:
0121     bool deserialize(Item &item, const QByteArray &label, QIODevice &data, int version) override;
0122     void serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version) override;
0123 };
0124 
0125 }