MythTV  master
serializer.cpp
Go to the documentation of this file.
1 // Program Name: serializer.cpp
3 // Created : Dec. 30, 2009
4 //
5 // Purpose : Serialization Abstract Class
6 //
7 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 #include "serializer.h"
14 
15 #include <QMetaObject>
16 #include <QMetaProperty>
17 
19 //
21 
23 {
24  headers[ "Cache-Control" ] = "no-cache=\"Ext\", "
25  "max-age = 7200"; // 2 hours
26 
27  headers[ "ETag" ] = "\"" + m_hash.result().toHex() + "\"";
28 }
29 
31 //
33 
34 void Serializer::Serialize( const QObject *pObject, const QString &_sName )
35 {
36  QString sName = _sName;
37 
38  if (sName.isEmpty())
39  sName = pObject->objectName();
40 
41  if (sName.isEmpty())
42  {
43  sName = pObject->metaObject()->className();
44 
45  sName = sName.section( ":", -1 );
46 
47  if ((sName.length() > 0) && (sName.at(0) == 'Q'))
48  sName = sName.mid( 1 );
49  }
50 
51  // ---------------------------------------------------------------
52 
53  m_hash.reset();
54 
55  BeginSerialize( sName );
56 
57  SerializeObject( pObject, sName );
58 
59  EndSerialize();
60 
61 }
62 
64 //
66 
67 void Serializer::Serialize( const QVariant &vValue, const QString &_sName )
68 {
69  QString sName( _sName );
70 
71  if ((sName.length() > 0) && sName.at(0) == 'Q')
72  sName = sName.mid( 1 );
73 
74  if ( !vValue.isNull() )
75  m_hash.addData( vValue.toString().toUtf8() );
76 
77  BeginSerialize( sName );
78 
79  AddProperty( sName, vValue, nullptr, nullptr );
80 
81  EndSerialize();
82 }
83 
85 //
87 
88 void Serializer::SerializeObject( const QObject *pObject, const QString &sName )
89 {
90  m_hash.addData( sName.toUtf8() );
91 
92  BeginObject( sName, pObject );
93 
94  SerializeObjectProperties( pObject );
95 
96  EndObject( sName, pObject );
97 }
98 
100 //
102 
103 void Serializer::SerializeObjectProperties( const QObject *pObject )
104 {
105  if (pObject != nullptr)
106  {
107  const QMetaObject *pMetaObject = pObject->metaObject();
108 
109  int nCount = pMetaObject->propertyCount();
110 
111  for (int nIdx=0; nIdx < nCount; ++nIdx )
112  {
113  QMetaProperty metaProperty = pMetaObject->property( nIdx );
114 
115  if (metaProperty.isDesignable( pObject ))
116  {
117  const char *pszPropName = metaProperty.name();
118  QString sPropName( pszPropName );
119 
120  if ( sPropName.compare( "objectName" ) == 0)
121  continue;
122 
123  bool bHash = false;
124 
125  if (ReadPropertyMetadata( pObject,
126  sPropName,
127  "transient").toLower() != "true" )
128  {
129  bHash = true;
130  m_hash.addData( sPropName.toUtf8() );
131  }
132 
133  QVariant value( pObject->property( pszPropName ) );
134 
135  if (bHash && !value.canConvert< QObject* >())
136  {
137  m_hash.addData( value.toString().toUtf8() );
138  }
139 
140  AddProperty( sPropName, value, pMetaObject, &metaProperty );
141  }
142  }
143  }
144 }
145 
147 //
149 
150 QString Serializer::ReadPropertyMetadata( const QObject *pObject,
151  const QString& sPropName,
152  const QString& sKey )
153 {
154  const QMetaObject *pMeta = pObject->metaObject();
155 
156  int nIdx = -1;
157 
158  if (pMeta)
159  nIdx = pMeta->indexOfClassInfo( sPropName.toUtf8());
160 
161  if (nIdx >=0)
162  {
163  QString sMetadata = pMeta->classInfo( nIdx ).value();
164  QStringList sOptions = sMetadata.split( ';' );
165 
166  QString sFullKey = sKey + "=";
167 
168  for (nIdx = 0; nIdx < sOptions.size(); ++nIdx)
169  {
170  if (sOptions.at( nIdx ).startsWith( sFullKey ))
171  return sOptions.at( nIdx ).mid( sFullKey.length() );
172  }
173  }
174 
175  return QString();
176 }
177 
178 
QCryptographicHash m_hash
Definition: serializer.h:35
virtual void EndSerialize()
Definition: serializer.h:38
virtual void BeginSerialize(QString &)
Definition: serializer.h:37
void SerializeObjectProperties(const QObject *pObject)
Definition: serializer.cpp:103
void SerializeObject(const QObject *pObject, const QString &sName)
Definition: serializer.cpp:88
QMap< QString, QString > QStringMap
Definition: upnputil.h:40
virtual void Serialize(const QObject *pObject, const QString &_sName=QString())
Definition: serializer.cpp:34
virtual void EndObject(const QString &sName, const QObject *pObject)=0
virtual void AddHeaders(QStringMap &headers)
Definition: serializer.cpp:22
virtual void AddProperty(const QString &sName, const QVariant &vValue, const QMetaObject *pMetaParent, const QMetaProperty *pMetaProp)=0
static QString ReadPropertyMetadata(const QObject *pObject, const QString &sPropName, const QString &sKey)
Definition: serializer.cpp:150
virtual void BeginObject(const QString &sName, const QObject *pObject)=0