MythTV  master
mythhttpmetamethod.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QDateTime>
3 #include <QFileInfo>
4 
5 // MythTV
6 #include "mythlogging.h"
8 
9 #define LOC QString("MetaMethod: ")
10 
18 MythHTTPMetaMethod::MythHTTPMetaMethod(int Index, QMetaMethod& Method, int RequestTypes,
19  const QString& ReturnName, bool Slot)
20  : m_index(Index),
21  m_requestTypes(RequestTypes),
22  m_method(Method)
23 {
24  // Static list of unsupported return types (cannot be serialised (QHash) or of no use (pointers))
25  static const std::vector<int> s_invalidTypes =
26  {
27  QMetaType::UnknownType, QMetaType::VoidStar, QMetaType::QObjectStar,
28  QMetaType::QVariantHash, QMetaType::QRect, QMetaType::QRectF,
29  QMetaType::QSize, QMetaType::QSizeF, QMetaType::QLine,
30  QMetaType::QLineF, QMetaType::QPoint, QMetaType::QPointF
31  };
32 
33  // Static list of unsupported parameters (all invalid types plus extras)
34  static const std::vector<int> s_invalidParams =
35  {
36  QMetaType::UnknownType, QMetaType::VoidStar, QMetaType::QObjectStar,
37  QMetaType::QVariantHash, QMetaType::QRect, QMetaType::QRectF,
38  QMetaType::QSize, QMetaType::QSizeF, QMetaType::QLine,
39  QMetaType::QLineF, QMetaType::QPoint, QMetaType::QPointF,
40  QMetaType::QVariantMap, QMetaType::QStringList, QMetaType::QVariantList
41  };
42 
43  int returntype = Method.returnType();
44 
45  // Discard methods with an unsupported return type
46  if (std::any_of(s_invalidTypes.cbegin(), s_invalidTypes.cend(), [&returntype](int Type) { return Type == returntype; }))
47  {
48  LOG(VB_HTTP, LOG_ERR, LOC + QString("Method '%1' has unsupported return type '%2'").arg(Method.name().constData(), Method.typeName()));
49  return;
50  }
51 
52  // Warn about complicated methods not supported by QMetaMethod - these will
53  // fail if all arguments are required and used
54  if (Method.parameterCount() > (Q_METAMETHOD_INVOKE_MAX_ARGS - 1))
55  {
56  LOG(VB_HTTP, LOG_WARNING, LOC + QString("Method '%1' takes more than %2 parameters; will probably fail")
57  .arg(Method.name().constData()).arg(Q_METAMETHOD_INVOKE_MAX_ARGS - 1));
58  }
59 
60  // Decide on the name of the returned type
61  if (Slot && ValidReturnType(returntype))
62  {
63  // Explicitly set via Q_CLASSINFO (name=XXX)
64  m_returnTypeName = ReturnName;
65  if (m_returnTypeName.isEmpty())
66  {
67  // If this is a user type, we assume its name should be used, otherwise
68  // prefer deduction from methodname
69  if (returntype <= QMetaType::User)
70  if (QString(Method.name()).startsWith(QStringLiteral("Get"), Qt::CaseInsensitive))
71  m_returnTypeName = Method.name().mid(3);
72  // Default to the type name
73  if (m_returnTypeName.isEmpty())
74  m_returnTypeName = Method.typeName();
75  // Duplicated from MythXMLSerialiser
76  if (m_returnTypeName.startsWith("Q"))
78  m_returnTypeName.remove("DTC::");
79  m_returnTypeName.remove(QChar('*'));
80  }
81  }
82 
83  // Add method name and return type
84  m_types.emplace_back(returntype >= 0 ? returntype : 0);
85  m_names.emplace_back(Method.name());
86 
87  // Add type/value for each method parameter
88  auto names = Method.parameterNames();
89  auto types = Method.parameterTypes();
90 
91  // Add type/value for each method parameter
92  for (int i = 0; i < names.size(); ++i)
93  {
94 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
95  int type = QMetaType::type(types[i]);
96 #else
97  int type = QMetaType::fromName(types[i]).id();
98 #endif
99 
100  // Discard methods that use unsupported parameter types.
101  // Note: slots only - these are supportable for signals
102  if (Slot && std::any_of(s_invalidParams.cbegin(), s_invalidParams.cend(), [&type](int Type) { return type == Type; }))
103  {
104  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Method '%1' has unsupported parameter type '%2' (%3)")
105  .arg(Method.name().constData(), types[i].constData()).arg(type));
106  return;
107  }
108 
109  m_names.emplace_back(names[i]);
110  m_types.emplace_back(type);
111  }
112 
113  m_valid = true;
114 }
115 
116 HTTPMethodPtr MythHTTPMetaMethod::Create(int Index, QMetaMethod &Method, int RequestTypes,
117  const QString &ReturnName, bool Slot)
118 {
119  HTTPMethodPtr result =
120  std::shared_ptr<MythHTTPMetaMethod>(new MythHTTPMetaMethod(Index, Method, RequestTypes, ReturnName, Slot));
121  if (result->m_valid)
122  return result;
123  return nullptr;
124 }
125 
131 void* MythHTTPMetaMethod::CreateParameter(void* Parameter, int Type, const QString& Value)
132 {
133  // Enum types
134 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
135  auto typeflags = QMetaType::typeFlags(Type);
136 #else
137  auto typeflags = QMetaType(Type).flags();
138 #endif
139  if ((typeflags & QMetaType::IsEnumeration) == QMetaType::IsEnumeration)
140  {
141  // QMetaEnum::keyToValue will return -1 for an unrecognised enumerant, so
142  // default to -1 for all error cases
143  int value = -1;
144 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
145  QByteArray type = QMetaType::typeName(Type);
146 #else
147  QByteArray type = QMetaType(Type).name();
148 #endif
149  if (int index = type.lastIndexOf("::" ); index > -1)
150  {
151  QString enumname = type.mid(index + 2);
152 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
153  const auto * metaobject = QMetaType::metaObjectForType(Type);
154 #else
155  const auto * metaobject = QMetaType(Type).metaObject();
156 #endif
157  if (metaobject)
158  {
159  int enumindex = metaobject->indexOfEnumerator(enumname.toUtf8());
160  if (enumindex >= 0)
161  {
162  QMetaEnum metaEnum = metaobject->enumerator(enumindex);
163  value = metaEnum.keyToValue(Value.toUtf8());
164  }
165  }
166  }
167  *(static_cast<int*>(Parameter)) = value;
168  return Parameter;
169  }
170 
171  switch (Type)
172  {
173  case QMetaType::QVariant : *(static_cast<QVariant *>(Parameter)) = QVariant(Value); break;
174  case QMetaType::Bool : *(static_cast<bool *>(Parameter)) = ToBool(Value ); break;
175  case QMetaType::Char : *(static_cast<char *>(Parameter)) = (Value.length() > 0) ? Value.at(0).toLatin1() : 0; break;
176  case QMetaType::UChar : *(static_cast<unsigned char*>(Parameter)) = (Value.length() > 0) ? static_cast<unsigned char>(Value.at(0).toLatin1()) : 0; break;
177  case QMetaType::QChar : *(static_cast<QChar *>(Parameter)) = (Value.length() > 0) ? Value.at(0) : QChar(0); break;
178  case QMetaType::Short : *(static_cast<short *>(Parameter)) = Value.toShort(); break;
179  case QMetaType::UShort : *(static_cast<ushort *>(Parameter)) = Value.toUShort(); break;
180  case QMetaType::Int : *(static_cast<int *>(Parameter)) = Value.toInt(); break;
181  case QMetaType::UInt : *(static_cast<uint *>(Parameter)) = Value.toUInt(); break;
182  case QMetaType::Long : *(static_cast<long *>(Parameter)) = Value.toLong(); break;
183  case QMetaType::ULong : *(static_cast<ulong *>(Parameter)) = Value.toULong(); break;
184  case QMetaType::LongLong : *(static_cast<qlonglong *>(Parameter)) = Value.toLongLong(); break;
185  case QMetaType::ULongLong : *(static_cast<qulonglong *>(Parameter)) = Value.toULongLong(); break;
186  case QMetaType::Double : *(static_cast<double *>(Parameter)) = Value.toDouble(); break;
187  case QMetaType::Float : *(static_cast<float *>(Parameter)) = Value.toFloat(); break;
188  case QMetaType::QString : *(static_cast<QString *>(Parameter)) = Value; break;
189  case QMetaType::QByteArray: *(static_cast<QByteArray *>(Parameter)) = Value.toUtf8(); break;
190  case QMetaType::QTime : *(static_cast<QTime *>(Parameter)) = QTime::fromString(Value, Qt::ISODate ); break;
191  case QMetaType::QDate : *(static_cast<QDate *>(Parameter)) = QDate::fromString(Value, Qt::ISODate ); break;
192  case QMetaType::QDateTime :
193  {
194  QDateTime dt = QDateTime::fromString(Value, Qt::ISODate);
195  dt.setTimeSpec(Qt::UTC);
196  *(static_cast<QDateTime*>(Parameter)) = dt;
197  break;
198  }
199  default: break;
200  }
201  return Parameter;
202 }
203 
204 QVariant MythHTTPMetaMethod::CreateReturnValue(int Type, void* Value)
205 {
206  if (!(ValidReturnType(Type)))
207  return {};
208 
209  // This assumes any user type will be derived from QObject...
210  // (Exception for QFileInfo)
211  if (
212 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
213  Type == QMetaType::type("QFileInfo")
214 #else
215  Type == QMetaType::fromName("QFileInfo").id()
216 #endif
217  )
218  return QVariant::fromValue<QFileInfo>(*(static_cast<QFileInfo*>(Value)));
219 
220  if (Type > QMetaType::User)
221  {
222  QObject* object = *(static_cast<QObject**>(Value));
223  return QVariant::fromValue<QObject*>(object);
224  }
225 
226 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
227  return {Type, Value};
228 #else
229  return QVariant(QMetaType(Type), Value);
230 #endif
231 }
types
static const struct wl_interface * types[]
Definition: idle_inhibit_unstable_v1.c:39
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythHTTPMetaMethod::Create
static HTTPMethodPtr Create(int Index, QMetaMethod &Method, int RequestTypes, const QString &ReturnName={}, bool Slot=true)
Definition: mythhttpmetamethod.cpp:116
MythHTTPMetaMethod::ToBool
static bool ToBool(const QString &Value)
Definition: mythhttpmetamethod.h:47
mythlogging.h
MythHTTPMetaMethod::CreateParameter
static void * CreateParameter(void *Parameter, int Type, const QString &Value)
Populate the QMetaType object referenced by Parameter with Value.
Definition: mythhttpmetamethod.cpp:131
LOC
#define LOC
Definition: mythhttpmetamethod.cpp:9
uint
unsigned int uint
Definition: compat.h:81
MythHTTPMetaMethod::ValidReturnType
static bool ValidReturnType(int Type)
Definition: mythhttpmetamethod.h:42
MythDate::fromString
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:34
MythHTTPMetaMethod::m_names
std::vector< QString > m_names
Definition: mythhttpmetamethod.h:31
MythDate::ISODate
@ ISODate
Default UTC.
Definition: mythdate.h:17
MythHTTPMetaMethod::MythHTTPMetaMethod
MythHTTPMetaMethod(int Index, QMetaMethod &Method, int RequestTypes, const QString &ReturnName, bool Slot)
Definition: mythhttpmetamethod.cpp:18
MythHTTPMetaMethod::CreateReturnValue
static QVariant CreateReturnValue(int Type, void *Value)
Definition: mythhttpmetamethod.cpp:204
MythHTTPMetaMethod::m_valid
bool m_valid
Definition: mythhttpmetamethod.h:26
mythhttpmetamethod.h
MythHTTPMetaMethod::m_returnTypeName
QString m_returnTypeName
Definition: mythhttpmetamethod.h:33
HTTPMethodPtr
std::shared_ptr< MythHTTPMetaMethod > HTTPMethodPtr
Definition: mythhttpmetamethod.h:14
MythHTTPMetaMethod::m_types
std::vector< int > m_types
Definition: mythhttpmetamethod.h:32