Akonadi

itemserializer.cpp
1 /*
2  Copyright (c) 2007 Till Adam <[email protected]>
3  Copyright (c) 2007 Volker Krause <[email protected]>
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 #include "itemserializer_p.h"
22 #include "item.h"
23 #include "itemserializerplugin.h"
24 #include "typepluginloader_p.h"
25 #include "protocolhelper_p.h"
26 
27 #include "private/externalpartstorage_p.h"
28 
29 #include "akonadicore_debug.h"
30 
31 // Qt
32 #include <QBuffer>
33 #include <QFile>
34 #include <QIODevice>
35 #include <QString>
36 
37 #include <string>
38 
39 Q_DECLARE_METATYPE(std::string)
40 
41 namespace Akonadi
42 {
43 
44 DefaultItemSerializerPlugin::DefaultItemSerializerPlugin() = default;
45 
46 bool DefaultItemSerializerPlugin::deserialize(Item &item, const QByteArray &label, QIODevice &data, int)
47 {
48  if (label != Item::FullPayload) {
49  return false;
50  }
51 
52  item.setPayload(data.readAll());
53  return true;
54 }
55 
56 void DefaultItemSerializerPlugin::serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version)
57 {
58  Q_UNUSED(version)
59  Q_ASSERT(label == Item::FullPayload);
60  Q_UNUSED(label);
61  data.write(item.payload<QByteArray>());
62 }
63 
64 bool StdStringItemSerializerPlugin::deserialize(Item &item, const QByteArray &label, QIODevice &data, int)
65 {
66  if (label != Item::FullPayload) {
67  return false;
68  }
69  std::string str;
70  {
71  const QByteArray ba = data.readAll();
72  str.assign(ba.data(), ba.size());
73  }
74  item.setPayload(str);
75  return true;
76 }
77 
78 void StdStringItemSerializerPlugin::serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version)
79 {
80  Q_UNUSED(version)
81  Q_ASSERT(label == Item::FullPayload);
82  Q_UNUSED(label);
83  const std::string str = item.payload<std::string>();
84  data.write(QByteArray::fromRawData(str.data(), str.size()));
85 }
86 
87 /*static*/
88 void ItemSerializer::deserialize(Item &item, const QByteArray &label, const QByteArray &data,
89  int version, PayloadStorage storage)
90 {
91  if (storage == Internal) {
92  QBuffer buffer;
93  buffer.setData(data);
94  buffer.open(QIODevice::ReadOnly);
95  buffer.seek(0);
96  deserialize(item, label, buffer, version);
97  buffer.close();
98  } else {
99  QFile file;
100  if (storage == External) {
101  file.setFileName(ExternalPartStorage::resolveAbsolutePath(data));
102  } else {
103  file.setFileName(QString::fromUtf8(data));
104  }
105 
106  if (file.open(QIODevice::ReadOnly)) {
107  deserialize(item, label, file, version);
108  file.close();
109  } else {
110  qCWarning(AKONADICORE_LOG) << "Failed to open"
111  << ((storage == External) ? "external" : "foreign") << "payload:"
112  << file.fileName() << file.errorString();
113  }
114  }
115 }
116 
117 /*static*/
118 void ItemSerializer::deserialize(Item &item, const QByteArray &label, QIODevice &data, int version)
119 {
120  if (!TypePluginLoader::defaultPluginForMimeType(item.mimeType())->deserialize(item, label, data, version)) {
121  qCWarning(AKONADICORE_LOG) << "Unable to deserialize payload part:" << label << "in item" << item.id() << "collection" << item.parentCollection().id();
122  data.seek(0);
123  qCWarning(AKONADICORE_LOG) << "Payload data was: " << data.readAll();
124  }
125 }
126 
127 /*static*/
128 void ItemSerializer::serialize(const Item &item, const QByteArray &label, QByteArray &data, int &version)
129 {
130  QBuffer buffer;
131  buffer.setBuffer(&data);
132  buffer.open(QIODevice::WriteOnly);
133  buffer.seek(0);
134  serialize(item, label, buffer, version);
135  buffer.close();
136 }
137 
138 /*static*/
139 void ItemSerializer::serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version)
140 {
141  if (!item.hasPayload()) {
142  return;
143  }
144  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass(item.mimeType(), item.availablePayloadMetaTypeIds());
145  plugin->serialize(item, label, data, version);
146 }
147 
148 void ItemSerializer::apply(Item &item, const Item &other)
149 {
150  if (!other.hasPayload()) {
151  return;
152  }
153 
154  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass(item.mimeType(), item.availablePayloadMetaTypeIds());
155  plugin->apply(item, other);
156 }
157 
158 QSet<QByteArray> ItemSerializer::parts(const Item &item)
159 {
160  if (!item.hasPayload()) {
161  return QSet<QByteArray>();
162  }
163  return TypePluginLoader::pluginForMimeTypeAndClass(item.mimeType(), item.availablePayloadMetaTypeIds())->parts(item);
164 }
165 
166 QSet<QByteArray> ItemSerializer::availableParts(const Item &item)
167 {
168  if (!item.hasPayload()) {
169  return QSet<QByteArray>();
170  }
171  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass(item.mimeType(), item.availablePayloadMetaTypeIds());
172  return plugin->availableParts(item);
173 }
174 
175 QSet<QByteArray> ItemSerializer::allowedForeignParts(const Item &item)
176 {
177  if (!item.hasPayload()) {
178  return QSet<QByteArray>();
179  }
180 
181  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass(item.mimeType(), item.availablePayloadMetaTypeIds());
182  return plugin->allowedForeignParts(item);
183 }
184 
185 Item ItemSerializer::convert(const Item &item, int mtid)
186 {
187  qCDebug(AKONADICORE_LOG) << "asked to convert a" << item.mimeType() << "item to format" << ( mtid ? QMetaType::typeName( mtid ) : "<legacy>" );
188  if (!item.hasPayload()) {
189  qCDebug(AKONADICORE_LOG) << " -> but item has no payload!";
190  return Item();
191  }
192 
193  if (ItemSerializerPlugin *const plugin = TypePluginLoader::pluginForMimeTypeAndClass(item.mimeType(), QVector<int>(1, mtid), TypePluginLoader::NoDefault)) {
194  qCDebug(AKONADICORE_LOG) << " -> found a plugin that feels responsible, trying serialising the payload";
195  QBuffer buffer;
196  buffer.open(QIODevice::ReadWrite);
197  int version = 0;
198  serialize(item, Item::FullPayload, buffer, version);
199  buffer.seek(0);
200  qCDebug(AKONADICORE_LOG) << " -> serialized payload into" << buffer.size() << "bytes\n"
201  << " -> going to deserialize";
202  Item newItem;
203  if (plugin->deserialize(newItem, Item::FullPayload, buffer, version)) {
204  qCDebug(AKONADICORE_LOG) << " -> conversion successful";
205  return newItem;
206  } else {
207  qCDebug(AKONADICORE_LOG) << " -> conversion FAILED";
208  }
209  } else {
210 // qCDebug(AKONADICORE_LOG) << " -> found NO plugin that feels responsible";
211  }
212  return Item();
213 }
214 
215 void ItemSerializer::overridePluginLookup(QObject *p)
216 {
217  TypePluginLoader::overridePluginLookup(p);
218 }
219 
220 }
virtual qint64 size() const const override
virtual bool seek(qint64 pos)
QString errorString() const const
virtual QString fileName() const const override
void setData(const QByteArray &data)
void setFileName(const QString &name)
A glue between Qt and the standard library.
QString fromUtf8(const char *str, int size)
QByteArray readAll()
virtual bool open(QIODevice::OpenMode flags) override
virtual bool open(QIODevice::OpenMode mode) override
virtual void close() override
const char * typeName(int typeId)
Definition: item.h:44
virtual void close() override
Helper integration between Akonadi and Qt.
void setBuffer(QByteArray *byteArray)
char * data()
int size() const const
virtual bool seek(qint64 pos) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.