/thirdparty/qxt/qxtweb-standalone/qxtweb/qxtwebslotservice.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 226 lines · 139 code · 21 blank · 66 comment · 21 complexity · 20433d0a7a5837d48ab40b6d9b101220 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) Qxt Foundation. Some rights reserved.
  4. **
  5. ** This file is part of the QxtWeb module of the Qxt library.
  6. **
  7. ** This library is free software; you can redistribute it and/or modify it
  8. ** under the terms of the Common Public License, version 1.0, as published
  9. ** by IBM, and/or under the terms of the GNU Lesser General Public License,
  10. ** version 2.1, as published by the Free Software Foundation.
  11. **
  12. ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
  13. ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
  14. ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
  15. ** FITNESS FOR A PARTICULAR PURPOSE.
  16. **
  17. ** You should have received a copy of the CPL and the LGPL along with this
  18. ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
  19. ** included with the source distribution for more information.
  20. ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
  21. **
  22. ** <http://libqxt.org> <foundation@libqxt.org>
  23. **
  24. ****************************************************************************/
  25. /*!
  26. \class QxtWebSlotService
  27. \inmodule QxtWeb
  28. \brief The QxtWebSlotService class provides a Slot based webservice
  29. A WebService that resolves the first part of the path to a slot name and passes the rest as arguments.
  30. \code
  31. class MyService : public QxtWebSlotService
  32. {
  33. Q_OBJECT
  34. public slots:
  35. void hello(QxtWebRequestEvent* event, QString a)
  36. {
  37. postEvent(new QxtWebPageEvent(event->sessionID, event->requestID, "&lth1&gt"+a.toUtf8()+"&lt/h1&gt));
  38. }
  39. }
  40. \endcode
  41. /hello/foo<br>
  42. will output<br>
  43. &lth1&gtFoo&lt/h1&gt<br>
  44. \sa QxtAbstractWebService
  45. */
  46. #include "qxtwebslotservice.h"
  47. #include "qxtwebevent.h"
  48. /*!
  49. Constructs a new QxtWebSlotService with \a sm and \a parent.
  50. */
  51. QxtWebSlotService::QxtWebSlotService(QxtAbstractWebSessionManager* sm, QObject* parent): QxtAbstractWebService(sm, parent)
  52. {
  53. }
  54. /*!
  55. Returns the current absolute url of this service depending on the request \a event.
  56. */
  57. QUrl QxtWebSlotService::self(QxtWebRequestEvent* event)
  58. {
  59. QStringList u = event->url.path().split('/');
  60. QStringList o = event->originalUrl.path().split('/');
  61. u.removeFirst();
  62. o.removeFirst();
  63. for (int i = 0;i < u.count();i++)
  64. o.removeLast();
  65. QString r = "/";
  66. foreach(const QString& d, o)
  67. {
  68. r += d + '/';
  69. }
  70. return r;
  71. }
  72. /*!
  73. \reimp
  74. */
  75. void QxtWebSlotService::pageRequestedEvent(QxtWebRequestEvent* event)
  76. {
  77. QList<QString> args = event->url.path().split('/');
  78. args.removeFirst();
  79. if (args.at(args.count() - 1).isEmpty())
  80. args.removeLast();
  81. ///--------------find action ------------------
  82. QByteArray action = "index";
  83. if (args.count())
  84. {
  85. action = args.at(0).toUtf8();
  86. if (action.trimmed().isEmpty())
  87. action = "index";
  88. args.removeFirst();
  89. }
  90. bool ok = false;
  91. if (args.count() > 7)
  92. {
  93. ok = QMetaObject::invokeMethod(this, action,
  94. Q_ARG(QxtWebRequestEvent*, event),
  95. Q_ARG(QString, args.at(0)),
  96. Q_ARG(QString, args.at(1)),
  97. Q_ARG(QString, args.at(2)),
  98. Q_ARG(QString, args.at(3)),
  99. Q_ARG(QString, args.at(4)),
  100. Q_ARG(QString, args.at(5)),
  101. Q_ARG(QString, args.at(6)),
  102. Q_ARG(QString, args.at(7))
  103. );
  104. }
  105. else if (args.count() > 6)
  106. {
  107. ok = QMetaObject::invokeMethod(this, action,
  108. Q_ARG(QxtWebRequestEvent*, event),
  109. Q_ARG(QString, args.at(0)),
  110. Q_ARG(QString, args.at(1)),
  111. Q_ARG(QString, args.at(2)),
  112. Q_ARG(QString, args.at(3)),
  113. Q_ARG(QString, args.at(4)),
  114. Q_ARG(QString, args.at(5)),
  115. Q_ARG(QString, args.at(6))
  116. );
  117. }
  118. else if (args.count() > 5)
  119. {
  120. ok = QMetaObject::invokeMethod(this, action,
  121. Q_ARG(QxtWebRequestEvent*, event),
  122. Q_ARG(QString, args.at(0)),
  123. Q_ARG(QString, args.at(1)),
  124. Q_ARG(QString, args.at(2)),
  125. Q_ARG(QString, args.at(3)),
  126. Q_ARG(QString, args.at(4)),
  127. Q_ARG(QString, args.at(5))
  128. );
  129. }
  130. else if (args.count() > 4)
  131. {
  132. ok = QMetaObject::invokeMethod(this, action,
  133. Q_ARG(QxtWebRequestEvent*, event),
  134. Q_ARG(QString, args.at(0)),
  135. Q_ARG(QString, args.at(1)),
  136. Q_ARG(QString, args.at(2)),
  137. Q_ARG(QString, args.at(3)),
  138. Q_ARG(QString, args.at(4))
  139. );
  140. }
  141. else if (args.count() > 3)
  142. {
  143. ok = QMetaObject::invokeMethod(this, action,
  144. Q_ARG(QxtWebRequestEvent*, event),
  145. Q_ARG(QString, args.at(0)),
  146. Q_ARG(QString, args.at(1)),
  147. Q_ARG(QString, args.at(2)),
  148. Q_ARG(QString, args.at(3))
  149. );
  150. }
  151. else if (args.count() > 2)
  152. {
  153. ok = QMetaObject::invokeMethod(this, action,
  154. Q_ARG(QxtWebRequestEvent*, event),
  155. Q_ARG(QString, args.at(0)),
  156. Q_ARG(QString, args.at(1)),
  157. Q_ARG(QString, args.at(2))
  158. );
  159. }
  160. else if (args.count() > 1)
  161. {
  162. ok = QMetaObject::invokeMethod(this, action,
  163. Q_ARG(QxtWebRequestEvent*, event),
  164. Q_ARG(QString, args.at(0)),
  165. Q_ARG(QString, args.at(1))
  166. );
  167. }
  168. else if (args.count() > 0)
  169. {
  170. ok = QMetaObject::invokeMethod(this, action,
  171. Q_ARG(QxtWebRequestEvent*, event),
  172. Q_ARG(QString, args.at(0))
  173. );
  174. }
  175. else
  176. {
  177. ok = QMetaObject::invokeMethod(this, action,
  178. Q_ARG(QxtWebRequestEvent*, event)
  179. );
  180. }
  181. if (!ok)
  182. {
  183. QByteArray err = "<h1>Can not find slot</h1> <pre>Class " + QByteArray(metaObject()->className()) + "\r{\npublic slots:\r void " + action.replace('<', "&lt") + " ( QxtWebRequestEvent* event, ";
  184. for (int i = 0;i < args.count();i++)
  185. err += "QString arg" + QByteArray::number(i) + ", ";
  186. err.chop(2);
  187. err += " ); \r};\r</pre> ";
  188. postEvent(new QxtWebErrorEvent(event->sessionID, event->requestID, 404, err));
  189. }
  190. }
  191. /*!
  192. \reimp
  193. */
  194. void QxtWebSlotService::functionInvokedEvent(QxtWebRequestEvent* event)
  195. {
  196. postEvent(new QxtWebErrorEvent(event->sessionID, event->requestID, 500, "<h1>Not supported</h1>"));
  197. }