1/*
2 Copyright (c) 2007 Till Adam <adam@kde.org>
3 Copyright (c) 2007 Volker Krause <vkrause@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_SERIALIZER_P_H
22#define AKONADI_ITEM_SERIALIZER_P_H
23
24#include <QtCore/QByteArray>
25#include <QtCore/QSet>
26
27#include "akonadiprivate_export.h"
28
29#include "itemserializerplugin.h"
30
31#include <memory>
32
33class QIODevice;
34
35namespace Akonadi {
36
37class Item;
38
39/**
40 @internal
41 Serialization/Deserialization of item parts, serializer plugin management.
42*/
43class AKONADI_TESTS_EXPORT ItemSerializer
44{
45public:
46 /** throws ItemSerializerException on failure */
47 static void deserialize(Item &item, const QByteArray &label, const QByteArray &data, int version, bool external);
48 /** throws ItemSerializerException on failure */
49 static void deserialize(Item &item, const QByteArray &label, QIODevice &data, int version);
50 /** throws ItemSerializerException on failure */
51 static void serialize(const Item &item, const QByteArray &label, QByteArray &data, int &version);
52 /** throws ItemSerializerException on failure */
53 static void serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version);
54
55 /**
56 * Throws ItemSerializerException on failure.
57 * @param item the item to apply to
58 * @param other the item to get values from
59 * @since 4.4
60 */
61 static void apply(Item &item, const Item &other);
62
63 /**
64 * Returns a list of parts available in the item payload.
65 */
66 static QSet<QByteArray> parts(const Item &item);
67
68 /**
69 * Returns a list of parts available remotely in the item payload.
70 * @param item the item for which to list payload parts
71 * @since 4.4
72 */
73 static QSet<QByteArray> availableParts(const Item &item);
74
75 /**
76 * Tries to convert the payload in \a item into type with
77 * metatype-id \a metaTypeId.
78 * Throws ItemSerializerException or returns an Item w/o payload on failure.
79 * @param item the item to convert
80 * @param metaTypeID the meta type id used to convert items payload
81 * @since 4.6
82 */
83 static Item convert(const Item &item, int metaTypeId);
84
85 /**
86 * Override the plugin-lookup with @p plugin.
87 *
88 * After calling this each lookup will always return @p plugin.
89 * This is useful to inject a special plugin for testing purposes.
90 * To reset the plugin, set to 0.
91 *
92 * @since 4.12
93 */
94 static void overridePluginLookup(QObject *plugin);
95};
96
97/**
98 @internal
99 Default implementation for serializer plugin.
100*/
101class DefaultItemSerializerPlugin : public QObject, public ItemSerializerPlugin
102{
103 Q_OBJECT
104 Q_INTERFACES(Akonadi::ItemSerializerPlugin)
105public:
106 DefaultItemSerializerPlugin();
107
108 bool deserialize(Item &, const QByteArray &, QIODevice &, int);
109 void serialize(const Item &, const QByteArray &, QIODevice &, int &);
110};
111
112/**
113 @internal
114 Serializer plugin implementation for std::string
115*/
116class StdStringItemSerializerPlugin : public QObject, public ItemSerializerPlugin
117{
118 Q_OBJECT
119 Q_INTERFACES(Akonadi::ItemSerializerPlugin)
120public:
121 StdStringItemSerializerPlugin();
122
123 bool deserialize(Item &, const QByteArray &, QIODevice &, int);
124 void serialize(const Item &, const QByteArray &, QIODevice &, int &);
125};
126
127}
128
129#endif
130