MythTV  master
service.h
Go to the documentation of this file.
1 // Program Name: service.h
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 #ifndef SERVICE_H_
14 #define SERVICE_H_
15 
16 #include <QObject>
17 #include <QMetaType>
18 #include <QVariant>
19 #include <QFileInfo>
20 #include <QDateTime>
21 #include <QString>
22 
23 #include "serviceexp.h"
24 
27 //
28 // Notes for derived classes -
29 //
30 // * This implementation can't handle declared default parameters
31 //
32 // * When called, any missing params are sent default values for its datatype
33 //
34 // * Q_CLASSINFO( "<methodName>_Method", "BOTH" )
35 // is used to determine HTTP method type.
36 // Defaults to "BOTH", available values:
37 // "GET", "POST" or "BOTH"
38 //
41 
42 class SERVICE_PUBLIC Service : public QObject
43 {
44  Q_OBJECT
45 
46  public:
47 
48  explicit inline Service( QObject *parent = nullptr );
49 
50  public:
51 
53  // This method should be overridden to handle non-QObject based custom types
55 
56  virtual QVariant ConvertToVariant ( int nType, void *pValue );
57 
58  virtual void* ConvertToParameterPtr( int nTypeId,
59  const QString &sParamType,
60  void* pParam,
61  const QString &sValue );
62 
63  static bool ToBool( const QString &sVal );
64 
65  public:
66 
67  QList<QString> m_parsedParams; // lowercased
68 };
69 
71 //
73 
74 inline Service::Service(QObject *parent) : QObject(parent)
75 {
76  qRegisterMetaType< QFileInfo >();
77 }
78 
80 //
82 
83 #define SCRIPT_CATCH_EXCEPTION( default, code ) \
84  try \
85  { \
86  code \
87  } \
88  catch( QString msg ) \
89  { \
90  m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, msg ); \
91  return default; \
92  } \
93  catch( const char *msg ) \
94  { \
95  m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, msg ); \
96  return default; \
97  } \
98  catch( ... ) \
99  { \
100  m_pEngine->currentContext()->throwError( QScriptContext::UnknownError, "Unknown Exception" ); \
101  return default; \
102  }
103 
105 // The following replaces the standard Q_SCRIPT_DECLARE_QMETAOBJECT macro
106 // This implementation passes the QScriptEngine to the constructor as the
107 // first parameter.
109 
110 #define Q_SCRIPT_DECLARE_QMETAOBJECT_MYTHTV(T, _Arg1) \
111 template<> inline QScriptValue qscriptQMetaObjectConstructor<T>(QScriptContext *ctx, QScriptEngine *eng, T *) \
112 { \
113  _Arg1 arg1 = qscriptvalue_cast<_Arg1> (ctx->argument(0)); \
114  T* t = new T(eng, arg1); \
115  if (ctx->isCalledAsConstructor()) \
116  return eng->newQObject(ctx->thisObject(), t, QScriptEngine::AutoOwnership); \
117  QScriptValue o = eng->newQObject(t, QScriptEngine::AutoOwnership); \
118  o.setPrototype(ctx->callee().property(QString::fromLatin1("prototype"))); \
119  return o; \
120 }
121 
122 
123 #endif
Service(QObject *parent=nullptr)
Definition: service.h:74
QList< QString > m_parsedParams
Definition: service.h:67
#define SERVICE_PUBLIC
Definition: serviceexp.h:9