PageRenderTime 116ms CodeModel.GetById 40ms app.highlight 32ms RepoModel.GetById 27ms app.codeStats 1ms

/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
 27/*!
 28\class QxtWebSlotService
 29
 30\inmodule QxtWeb
 31
 32\brief The QxtWebSlotService class provides a Slot based webservice
 33
 34A WebService that resolves the first part of the path to a slot name and passes the rest as arguments.
 35
 36\code
 37class MyService : public QxtWebSlotService
 38{
 39Q_OBJECT
 40public slots:
 41    void hello(QxtWebRequestEvent* event, QString a)
 42    {
 43        postEvent(new QxtWebPageEvent(event->sessionID, event->requestID, "&lth1&gt"+a.toUtf8()+"&lt/h1&gt));
 44    }
 45}
 46\endcode
 47
 48
 49/hello/foo<br>
 50will output<br>
 51&lth1&gtFoo&lt/h1&gt<br>
 52
 53
 54\sa QxtAbstractWebService
 55*/
 56
 57#include "qxtwebslotservice.h"
 58#include "qxtwebevent.h"
 59
 60/*!
 61    Constructs a new QxtWebSlotService with \a sm and \a parent.
 62 */
 63QxtWebSlotService::QxtWebSlotService(QxtAbstractWebSessionManager* sm, QObject* parent): QxtAbstractWebService(sm, parent)
 64{
 65}
 66
 67/*!
 68    Returns the current absolute url of this service depending on the request \a event.
 69 */
 70QUrl QxtWebSlotService::self(QxtWebRequestEvent* event)
 71
 72{
 73    QStringList  u = event->url.path().split('/');
 74    QStringList  o = event->originalUrl.path().split('/');
 75    u.removeFirst();
 76    o.removeFirst();
 77    for (int i = 0;i < u.count();i++)
 78        o.removeLast();
 79
 80
 81    QString r = "/";
 82    foreach(const QString& d, o)
 83    {
 84        r += d + '/';
 85    }
 86    return r;
 87}
 88
 89/*!
 90    \reimp
 91 */
 92void QxtWebSlotService::pageRequestedEvent(QxtWebRequestEvent* event)
 93{
 94    QList<QString> args = event->url.path().split('/');
 95    args.removeFirst();
 96    if (args.at(args.count() - 1).isEmpty())
 97        args.removeLast();
 98
 99
100    ///--------------find action ------------------
101    QByteArray action = "index";
102    if (args.count())
103    {
104        action = args.at(0).toUtf8();
105        if (action.trimmed().isEmpty())
106            action = "index";
107        args.removeFirst();
108    }
109
110
111
112    bool ok = false;
113    if (args.count() > 7)
114    {
115        ok = QMetaObject::invokeMethod(this, action,
116                                       Q_ARG(QxtWebRequestEvent*, event),
117                                       Q_ARG(QString, args.at(0)),
118                                       Q_ARG(QString, args.at(1)),
119                                       Q_ARG(QString, args.at(2)),
120                                       Q_ARG(QString, args.at(3)),
121                                       Q_ARG(QString, args.at(4)),
122                                       Q_ARG(QString, args.at(5)),
123                                       Q_ARG(QString, args.at(6)),
124                                       Q_ARG(QString, args.at(7))
125                                      );
126    }
127    else if (args.count() > 6)
128    {
129        ok = QMetaObject::invokeMethod(this, action,
130                                       Q_ARG(QxtWebRequestEvent*, event),
131                                       Q_ARG(QString, args.at(0)),
132                                       Q_ARG(QString, args.at(1)),
133                                       Q_ARG(QString, args.at(2)),
134                                       Q_ARG(QString, args.at(3)),
135                                       Q_ARG(QString, args.at(4)),
136                                       Q_ARG(QString, args.at(5)),
137                                       Q_ARG(QString, args.at(6))
138                                      );
139    }
140    else if (args.count() > 5)
141    {
142        ok = QMetaObject::invokeMethod(this, action,
143                                       Q_ARG(QxtWebRequestEvent*, event),
144                                       Q_ARG(QString, args.at(0)),
145                                       Q_ARG(QString, args.at(1)),
146                                       Q_ARG(QString, args.at(2)),
147                                       Q_ARG(QString, args.at(3)),
148                                       Q_ARG(QString, args.at(4)),
149                                       Q_ARG(QString, args.at(5))
150                                      );
151    }
152    else if (args.count() > 4)
153    {
154        ok = QMetaObject::invokeMethod(this, action,
155                                       Q_ARG(QxtWebRequestEvent*, event),
156                                       Q_ARG(QString, args.at(0)),
157                                       Q_ARG(QString, args.at(1)),
158                                       Q_ARG(QString, args.at(2)),
159                                       Q_ARG(QString, args.at(3)),
160                                       Q_ARG(QString, args.at(4))
161                                      );
162    }
163    else if (args.count() > 3)
164    {
165        ok = QMetaObject::invokeMethod(this, action,
166                                       Q_ARG(QxtWebRequestEvent*, event),
167                                       Q_ARG(QString, args.at(0)),
168                                       Q_ARG(QString, args.at(1)),
169                                       Q_ARG(QString, args.at(2)),
170                                       Q_ARG(QString, args.at(3))
171                                      );
172    }
173    else if (args.count() > 2)
174    {
175        ok = QMetaObject::invokeMethod(this, action,
176                                       Q_ARG(QxtWebRequestEvent*, event),
177                                       Q_ARG(QString, args.at(0)),
178                                       Q_ARG(QString, args.at(1)),
179                                       Q_ARG(QString, args.at(2))
180                                      );
181    }
182    else if (args.count() > 1)
183    {
184        ok = QMetaObject::invokeMethod(this, action,
185                                       Q_ARG(QxtWebRequestEvent*, event),
186                                       Q_ARG(QString, args.at(0)),
187                                       Q_ARG(QString, args.at(1))
188                                      );
189    }
190    else if (args.count() > 0)
191    {
192        ok = QMetaObject::invokeMethod(this, action,
193                                       Q_ARG(QxtWebRequestEvent*, event),
194                                       Q_ARG(QString, args.at(0))
195                                      );
196    }
197    else
198    {
199        ok = QMetaObject::invokeMethod(this, action,
200                                       Q_ARG(QxtWebRequestEvent*, event)
201                                      );
202    }
203
204
205    if (!ok)
206    {
207        QByteArray err = "<h1>Can not find slot</h1> <pre>Class " + QByteArray(metaObject()->className()) + "\r{\npublic slots:\r    void " + action.replace('<', "&lt") + " ( QxtWebRequestEvent* event, ";
208        for (int i = 0;i < args.count();i++)
209            err += "QString arg" + QByteArray::number(i) + ", ";
210        err.chop(2);
211
212        err += " ); \r};\r</pre> ";
213
214        postEvent(new QxtWebErrorEvent(event->sessionID, event->requestID, 404, err));
215    }
216
217
218}
219
220/*!
221    \reimp
222 */
223void QxtWebSlotService::functionInvokedEvent(QxtWebRequestEvent* event)
224{
225    postEvent(new QxtWebErrorEvent(event->sessionID, event->requestID, 500, "<h1>Not supported</h1>"));
226}