MythTV  master
service.cpp
Go to the documentation of this file.
1 // Program Name: service.cpp
3 // Created : Jan. 19, 2010
4 //
5 // Purpose : Base class for all Web Services
6 //
7 // Copyright (c) 2010 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see LICENSE for details
10 //
12 
13 #include "service.h"
14 #include <QMetaEnum>
15 #include <QJsonDocument>
16 #include <QJsonObject>
17 
19 //
21 
22 QVariant Service::ConvertToVariant( int nType, void *pValue )
23 {
24  // -=>NOTE: This assumes any UserType will be derived from QObject...
25  // (Exception for QFileInfo )
26 
27  if ( nType == qMetaTypeId<QFileInfo>() )
28  return QVariant::fromValue< QFileInfo >( *((QFileInfo *)pValue) );
29 
30  if (nType > QMetaType::User)
31  {
32  QObject *pObj = *((QObject **)pValue);
33 
34  return QVariant::fromValue<QObject*>( pObj );
35  }
36 
37 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
38  return { nType, pValue };
39 #else
40  return QVariant( QMetaType(nType), pValue );
41 #endif
42 }
43 
44 
46 //
48 
49 void* Service::ConvertToParameterPtr( int nTypeId,
50  const QString &sParamType,
51  void* pParam,
52  const QString &sValue )
53 {
54  // -=>NOTE: Only intrinsic or Qt type conversions should be here
55  // All others should be added to overridden implementation.
56 
57  switch( nTypeId )
58  {
59  case QMetaType::Bool : *(( bool *)pParam) = ToBool( sValue ); break;
60 
61  case QMetaType::Char : *(( char *)pParam) = ( sValue.length() > 0) ? sValue.at( 0 ).toLatin1() : 0; break;
62  case QMetaType::UChar : *(( unsigned char *)pParam) = ( sValue.length() > 0) ? sValue.at( 0 ).toLatin1() : 0; break;
63  case QMetaType::QChar : *(( QChar *)pParam) = ( sValue.length() > 0) ? sValue.at( 0 ) : QChar(0); break;
64 
65  case QMetaType::Short : *(( short *)pParam) = sValue.toShort (); break;
66  case QMetaType::UShort : *(( ushort *)pParam) = sValue.toUShort (); break;
67 
68  case QMetaType::Int : *(( int *)pParam) = sValue.toInt (); break;
69  case QMetaType::UInt : *(( uint *)pParam) = sValue.toUInt (); break;
70 
71  case QMetaType::Long : *(( long *)pParam) = sValue.toLong (); break;
72  case QMetaType::ULong : *(( ulong *)pParam) = sValue.toULong (); break;
73 
74  case QMetaType::LongLong : *(( qlonglong *)pParam) = sValue.toLongLong (); break;
75  case QMetaType::ULongLong : *(( qulonglong *)pParam) = sValue.toULongLong (); break;
76 
77  case QMetaType::Double : *(( double *)pParam) = sValue.toDouble (); break;
78  case QMetaType::Float : *(( float *)pParam) = sValue.toFloat (); break;
79 
80  case QMetaType::QString : *(( QString *)pParam) = sValue; break;
81  case QMetaType::QByteArray : *(( QByteArray *)pParam) = sValue.toUtf8 (); break;
82 
83  case QMetaType::QDateTime :
84  {
85  QDateTime dt = QDateTime::fromString( sValue, Qt::ISODate );
86  dt.setTimeSpec( Qt::UTC );
87  *(( QDateTime *)pParam) = dt;
88  break;
89  }
90  case QMetaType::QTime : *(( QTime *)pParam) = QTime::fromString ( sValue, Qt::ISODate ); break;
91  case QMetaType::QDate : *(( QDate *)pParam) = QDate::fromString ( sValue, Qt::ISODate ); break;
92  case QMetaType::QJsonObject :
93  {
94  QJsonDocument doc = QJsonDocument::fromJson(sValue.toUtf8());
95 
96  // check validity of the document
97  if(!doc.isNull())
98  {
99  if(doc.isObject())
100  *(( QJsonObject *)pParam) = doc.object();
101  }
102  else
103  throw QString("Invalid JSON: %1").arg(sValue);
104 
105  break;
106  }
107  default:
108 
109  // --------------------------------------------------------------
110  // Need to deal with Enums. For now, assume any type that contains :: is an enum.
111  // --------------------------------------------------------------
112 
113  int nLastIdx = sParamType.lastIndexOf( "::" );
114 
115  if (nLastIdx == -1)
116  break;
117 
118  QString sParentFQN = sParamType.mid( 0, nLastIdx );
119  QString sEnumName = sParamType.mid( nLastIdx+2 );
120 
121  // --------------------------------------------------------------
122  // Create Parent object so we can get to its metaObject
123  // --------------------------------------------------------------
124 
125 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
126  int nParentId = QMetaType::type( sParentFQN.toUtf8() );
127 
128  auto *pParentClass = (QObject *)QMetaType::create( nParentId );
129  if (pParentClass == nullptr)
130  break;
131 
132  const QMetaObject *pMetaObject = pParentClass->metaObject();
133 
134  QMetaType::destroy( nParentId, pParentClass );
135 #else
136  const QMetaObject *pMetaObject =
137  QMetaType::fromName( sParentFQN.toUtf8() ).metaObject();
138 #endif
139 
140  // --------------------------------------------------------------
141  // Now look up enum
142  // --------------------------------------------------------------
143 
144  int nEnumIdx = pMetaObject->indexOfEnumerator( sEnumName.toUtf8());
145 
146  if (nEnumIdx < 0 )
147  break;
148 
149  QMetaEnum metaEnum = pMetaObject->enumerator( nEnumIdx );
150 
151  *(( int *)pParam) = metaEnum.keyToValue( sValue.toUtf8() );
152 
153  break;
154  }
155 
156  return pParam;
157 }
158 
160 //
162 
163 bool Service::ToBool( const QString &sVal )
164 {
165  if (sVal.compare( "1", Qt::CaseInsensitive ) == 0)
166  return true;
167 
168  if (sVal.compare( "y", Qt::CaseInsensitive ) == 0)
169  return true;
170 
171  if (sVal.compare( "true", Qt::CaseInsensitive ) == 0)
172  return true;
173 
174  return false;
175 }
service.h
Service::ConvertToVariant
virtual QVariant ConvertToVariant(int nType, void *pValue)
Definition: service.cpp:22
Service::ConvertToParameterPtr
virtual void * ConvertToParameterPtr(int nTypeId, const QString &sParamType, void *pParam, const QString &sValue)
Definition: service.cpp:49
uint
unsigned int uint
Definition: compat.h:81
MythDate::fromString
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:34
Service::ToBool
static bool ToBool(const QString &sVal)
Definition: service.cpp:163
MythDate::ISODate
@ ISODate
Default UTC.
Definition: mythdate.h:17