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 COPYING for details
10 //
12 
13 #include "service.h"
14 #include <QMetaEnum>
15 
16 
18 //
20 
21 QVariant Service::ConvertToVariant( int nType, void *pValue )
22 {
23  // -=>NOTE: This assumes any UserType will be derived from QObject...
24  // (Exception for QFileInfo )
25 
26  if ( nType == QMetaType::type( "QFileInfo" ))
27  return QVariant::fromValue< QFileInfo >( *((QFileInfo *)pValue) );
28 
29  if (nType > QMetaType::User)
30  {
31  QObject *pObj = *((QObject **)pValue);
32 
33  return QVariant::fromValue<QObject*>( pObj );
34  }
35 
36  return QVariant( nType, pValue );
37 }
38 
39 
41 //
43 
44 void* Service::ConvertToParameterPtr( int nTypeId,
45  const QString &sParamType,
46  void* pParam,
47  const QString &sValue )
48 {
49  // -=>NOTE: Only intrinsic or Qt type conversions should be here
50  // All others should be added to overridden implementation.
51 
52  switch( nTypeId )
53  {
54  case QMetaType::Bool : *(( bool *)pParam) = ToBool( sValue ); break;
55 
56  case QMetaType::Char : *(( char *)pParam) = ( sValue.length() > 0) ? sValue.at( 0 ).toLatin1() : 0; break;
57  case QMetaType::UChar : *(( unsigned char *)pParam) = ( sValue.length() > 0) ? sValue.at( 0 ).toLatin1() : 0; break;
58  case QMetaType::QChar : *(( QChar *)pParam) = ( sValue.length() > 0) ? sValue.at( 0 ) : 0; break;
59 
60  case QMetaType::Short : *(( short *)pParam) = sValue.toShort (); break;
61  case QMetaType::UShort : *(( ushort *)pParam) = sValue.toUShort (); break;
62 
63  case QMetaType::Int : *(( int *)pParam) = sValue.toInt (); break;
64  case QMetaType::UInt : *(( uint *)pParam) = sValue.toUInt (); break;
65 
66  case QMetaType::Long : *(( long *)pParam) = sValue.toLong (); break;
67  case QMetaType::ULong : *(( ulong *)pParam) = sValue.toULong (); break;
68 
69  case QMetaType::LongLong : *(( qlonglong *)pParam) = sValue.toLongLong (); break;
70  case QMetaType::ULongLong : *(( qulonglong *)pParam) = sValue.toULongLong (); break;
71 
72  case QMetaType::Double : *(( double *)pParam) = sValue.toDouble (); break;
73  case QMetaType::Float : *(( float *)pParam) = sValue.toFloat (); break;
74 
75  case QMetaType::QString : *(( QString *)pParam) = sValue; break;
76  case QMetaType::QByteArray : *(( QByteArray *)pParam) = sValue.toUtf8 (); break;
77 
78  case QMetaType::QDateTime :
79  {
80  QDateTime dt = QDateTime::fromString( sValue, Qt::ISODate );
81  dt.setTimeSpec( Qt::UTC );
82  *(( QDateTime *)pParam) = dt;
83  break;
84  }
85  case QMetaType::QTime : *(( QTime *)pParam) = QTime::fromString ( sValue, Qt::ISODate ); break;
86  case QMetaType::QDate : *(( QDate *)pParam) = QDate::fromString ( sValue, Qt::ISODate ); break;
87 
88  default:
89 
90  // --------------------------------------------------------------
91  // Need to deal with Enums. For now, assume any type that contains :: is an enum.
92  // --------------------------------------------------------------
93 
94  int nLastIdx = sParamType.lastIndexOf( "::" );
95 
96  if (nLastIdx == -1)
97  break;
98 
99  QString sParentFQN = sParamType.mid( 0, nLastIdx );
100  QString sEnumName = sParamType.mid( nLastIdx+2 );
101 
102  // --------------------------------------------------------------
103  // Create Parent object so we can get to its metaObject
104  // --------------------------------------------------------------
105 
106  int nParentId = QMetaType::type( sParentFQN.toUtf8() );
107 
108  QObject *pParentClass = (QObject *)QMetaType::create( nParentId );
109  if (pParentClass == nullptr)
110  break;
111 
112  const QMetaObject *pMetaObject = pParentClass->metaObject();
113 
114  QMetaType::destroy( nParentId, pParentClass );
115 
116  // --------------------------------------------------------------
117  // Now look up enum
118  // --------------------------------------------------------------
119 
120  int nEnumIdx = pMetaObject->indexOfEnumerator( sEnumName.toUtf8());
121 
122  if (nEnumIdx < 0 )
123  break;
124 
125  QMetaEnum metaEnum = pMetaObject->enumerator( nEnumIdx );
126 
127  *(( int *)pParam) = metaEnum.keyToValue( sValue.toUtf8() );
128 
129  break;
130  }
131 
132  return pParam;
133 }
134 
136 //
138 
139 bool Service::ToBool( const QString &sVal )
140 {
141  if (sVal.compare( "1", Qt::CaseInsensitive ) == 0)
142  return true;
143 
144  if (sVal.compare( "y", Qt::CaseInsensitive ) == 0)
145  return true;
146 
147  if (sVal.compare( "true", Qt::CaseInsensitive ) == 0)
148  return true;
149 
150  return false;
151 }
unsigned int uint
Definition: compat.h:140
static bool ToBool(const QString &sVal)
Definition: service.cpp:139
virtual QVariant ConvertToVariant(int nType, void *pValue)
Definition: service.cpp:21
virtual void * ConvertToParameterPtr(int nTypeId, const QString &sParamType, void *pParam, const QString &sValue)
Definition: service.cpp:44
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:30
Default UTC.
Definition: mythdate.h:14