MythTV  master
mythserialiser.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QMetaProperty>
3 // MythTV
4 #include "mythlogging.h"
6 #include "http/mythhttpdata.h"
12 
14  : m_result(MythHTTPData::Create())
15 {
16  m_buffer.setBuffer(static_cast<QByteArray*>(m_result.get()));
17  m_buffer.open(QIODevice::WriteOnly);
18 }
19 
21 {
22  return m_result;
23 }
24 
27 HTTPData MythSerialiser::Serialise(const QString &Name, const QVariant& Value, const QStringList &Accept)
28 {
29  /*
30  auto types = MythMimeDatabase().AllTypes();
31  QStringList names;
32  for (const auto & type : types)
33  names.append(type.Name());
34  names.sort();
35  for (const auto & name : names)
36  LOG(VB_GENERAL, LOG_INFO, name);
37  */
38 
39  static const MythMimeType s_xmlType = MythMimeDatabase::MimeTypeForName("application/xml");
40  static const MythMimeType s_xmlPList = MythMimeDatabase::MimeTypeForName("text/x-apple-plist+xml");
41  static const MythMimeType s_cbor = MythMimeDatabase::MimeTypeForName("application/cbor");
42 
43  auto WrapData = [](HTTPData Data, const MythMimeType& Mime, const QString& Alias)
44  {
45  Data->m_fileName = "result." + Mime.Suffix();
46  Data->m_mimeType = Mime;
47  Data->m_mimeType.SetAlias(Alias);
48  return Data;
49  };
50 
51  static const std::array<MythMimeType,2> s_jsonTypes =
52  {
53  MythMimeDatabase::MimeTypeForName("application/json"),
54  MythMimeDatabase::MimeTypeForName("text/javascript")
55  };
56 
57  static const std::array<MythMimeType,2> s_binaryPlists =
58  {
59  MythMimeDatabase::MimeTypeForName("application/x-plist"),
60  MythMimeDatabase::MimeTypeForName("application/x-apple-binary-plist")
61  };
62 
63  // Check for preformatted xml or html
64  if (Value.value<QObject*>())
65  {
66  auto * Object = Value.value<QObject*>();
67  const auto * meta = Object->metaObject();
68  int index = meta->indexOfClassInfo("preformat");
69  if (index >= 0
70  && QString("true") == meta->classInfo(index).value())
71  {
72  HTTPData result = MythHTTPData::Create();
73  QBuffer buffer;
74  buffer.setBuffer(static_cast<QByteArray*>(result.get()));
75  buffer.open(QIODevice::WriteOnly);
76  QString mimetype;
77  int count = meta->propertyCount();
78  for (int ix = 0; ix < count; ++ix )
79  {
80  QMetaProperty metaproperty = meta->property(ix);
81 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
82  bool user = metaproperty.isUser(Object);
83 #else
84  bool user = metaproperty.isUser();
85 #endif
86  if (user)
87  {
88  const char *rawname = metaproperty.name();
89  QString name(rawname);
90  QVariant value(Object->property(rawname));
91  if (name.compare("mimetype") == 0)
92  {
93  mimetype = value.toString();
94  continue;
95  }
96  if (name.compare("buffer") == 0)
97  {
98  buffer.write(value.toString().toUtf8());
99  continue;
100  }
101  }
102  }
104  auto alias = reqType.Aliases().indexOf(mimetype);
105  return WrapData(result, reqType, reqType.Aliases().at(alias));
106  }
107  }
108 
109  for (const auto & mime : Accept)
110  {
111  LOG(VB_HTTP, LOG_DEBUG, QString("Looking for serialiser for '%1'").arg(mime));
112  for (const auto & jsontype : s_jsonTypes)
113  {
114  if (const auto & alias = jsontype.Aliases().indexOf(mime); alias >= 0)
115  if (MythJSONSerialiser json(Name, Value); json.Result() != nullptr)
116  return WrapData(json.Result(), jsontype, jsontype.Aliases().at(alias));
117  }
118 
119  if (const auto & alias = s_xmlType.Aliases().indexOf(mime); alias >= 0)
120  if (MythXMLSerialiser xml(Name, Value); xml.Result() != nullptr)
121  return WrapData(xml.Result(), s_xmlType, s_xmlType.Aliases().at(alias));
122 
123  if (const auto & alias = s_xmlPList.Aliases().indexOf(mime); alias >= 0)
124  if (MythXMLPListSerialiser plist(Name, Value); plist.Result() != nullptr)
125  return WrapData(plist.Result(), s_xmlPList, s_xmlPList.Aliases().at(alias));
126 
127  if (const auto & alias = s_cbor.Aliases().indexOf(mime); alias >= 0)
128  if (MythCBORSerialiser cbor(Name, Value); cbor.Result() != nullptr)
129  return WrapData(cbor.Result(), s_cbor, s_cbor.Aliases().at(alias));
130  }
131 
132  // Default to XML
133  MythXMLSerialiser xml(Name, Value);
134  return WrapData(xml.Result(), s_xmlType, s_xmlType.Name());
135 }
MythSerialiser::Result
HTTPData Result()
Definition: mythserialiser.cpp:20
MythSerialiser::MythSerialiser
MythSerialiser()
Definition: mythserialiser.cpp:13
MythHTTPData
Definition: mythhttpdata.h:10
MythMimeType::Aliases
QStringList Aliases() const
Definition: mythmimetype.cpp:58
musicbrainzngs.musicbrainz.user
string user
Definition: musicbrainz.py:287
mythcborserialiser.h
MythSerialiser::Serialise
static HTTPData Serialise(const QString &Name, const QVariant &Value, const QStringList &Accept)
Serialise the given data with an encoding suggested by Accept.
Definition: mythserialiser.cpp:27
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
mythjsonserialiser.h
MythSerialiser::m_buffer
QBuffer m_buffer
Definition: mythserialiser.h:22
mythhttpdata.h
HTTPData
std::shared_ptr< MythHTTPData > HTTPData
Definition: mythhttptypes.h:36
mythxmlserialiser.h
MythSerialiser::m_result
HTTPData m_result
Definition: mythserialiser.h:23
mythlogging.h
MythHTTPData::Create
static HTTPData Create()
Definition: mythhttpdata.cpp:4
MythXMLPListSerialiser
Definition: mythxmlplistserialiser.h:12
mythxmlplistserialiser.h
mythmimedatabase.h
mythserialiser.h
MythMimeDatabase::MimeTypeForName
static MythMimeType MimeTypeForName(const QString &Name)
Return a mime type that matches the given name.
Definition: mythmimedatabase.cpp:201
Name
Definition: channelsettings.cpp:71
MythMimeType
Definition: mythmimetype.h:16
MythMimeType::Name
QString Name() const
Definition: mythmimetype.cpp:31
MythXMLSerialiser
Definition: mythxmlserialiser.h:12
MythJSONSerialiser
Definition: mythjsonserialiser.h:10
MythCBORSerialiser
Definition: mythcborserialiser.h:9