MythTV master
service.cpp
Go to the documentation of this file.
1
2// 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
22QVariant 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
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#if QT_VERSION < QT_VERSION_CHECK(6,5,0)
87 dt.setTimeSpec( Qt::UTC );
88#else
89 dt.setTimeZone( QTimeZone(QTimeZone::UTC) );
90#endif
91 *(( QDateTime *)pParam) = dt;
92 break;
93 }
94 case QMetaType::QTime : *(( QTime *)pParam) = QTime::fromString ( sValue, Qt::ISODate ); break;
95 case QMetaType::QDate : *(( QDate *)pParam) = QDate::fromString ( sValue, Qt::ISODate ); break;
96 case QMetaType::QJsonObject :
97 {
98 QJsonDocument doc = QJsonDocument::fromJson(sValue.toUtf8());
99
100 // check validity of the document
101 if(!doc.isNull())
102 {
103 if(doc.isObject())
104 *(( QJsonObject *)pParam) = doc.object();
105 }
106 else
107 {
108 throw QString("Invalid JSON: %1").arg(sValue);
109 }
110
111 break;
112 }
113 default:
114
115 // --------------------------------------------------------------
116 // Need to deal with Enums. For now, assume any type that contains :: is an enum.
117 // --------------------------------------------------------------
118
119 int nLastIdx = sParamType.lastIndexOf( "::" );
120
121 if (nLastIdx == -1)
122 break;
123
124 QString sParentFQN = sParamType.mid( 0, nLastIdx );
125 QString sEnumName = sParamType.mid( nLastIdx+2 );
126
127 // --------------------------------------------------------------
128 // Create Parent object so we can get to its metaObject
129 // --------------------------------------------------------------
130
131#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
132 int nParentId = QMetaType::type( sParentFQN.toUtf8() );
133
134 auto *pParentClass = (QObject *)QMetaType::create( nParentId );
135 if (pParentClass == nullptr)
136 break;
137
138 const QMetaObject *pMetaObject = pParentClass->metaObject();
139
140 QMetaType::destroy( nParentId, pParentClass );
141#else
142 const QMetaObject *pMetaObject =
143 QMetaType::fromName( sParentFQN.toUtf8() ).metaObject();
144#endif
145
146 // --------------------------------------------------------------
147 // Now look up enum
148 // --------------------------------------------------------------
149
150 int nEnumIdx = pMetaObject->indexOfEnumerator( sEnumName.toUtf8());
151
152 if (nEnumIdx < 0 )
153 break;
154
155 QMetaEnum metaEnum = pMetaObject->enumerator( nEnumIdx );
156
157 *(( int *)pParam) = metaEnum.keyToValue( sValue.toUtf8() );
158
159 break;
160 }
161
162 return pParam;
163}
164
166//
168
169bool Service::ToBool( const QString &sVal )
170{
171 if (sVal.compare( "1", Qt::CaseInsensitive ) == 0)
172 return true;
173
174 if (sVal.compare( "y", Qt::CaseInsensitive ) == 0)
175 return true;
176
177 if (sVal.compare( "true", Qt::CaseInsensitive ) == 0)
178 return true;
179
180 return false;
181}
virtual void * ConvertToParameterPtr(int nTypeId, const QString &sParamType, void *pParam, const QString &sValue)
Definition: service.cpp:49
static bool ToBool(const QString &sVal)
Definition: service.cpp:169
virtual QVariant ConvertToVariant(int nType, void *pValue)
Definition: service.cpp:22
unsigned int uint
Definition: freesurround.h:24
@ ISODate
Default UTC.
Definition: mythdate.h:17
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:39