Consolidate protocol messages in one namespace
[quassel.git] / src / common / basichandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "basichandler.h"
22
23 #include <QMetaMethod>
24
25 #include "logger.h"
26
27 BasicHandler::BasicHandler(QObject *parent)
28     : QObject(parent),
29     _defaultHandler(-1),
30     _initDone(false),
31     _methodPrefix("handle")
32 {
33 }
34
35
36 BasicHandler::BasicHandler(const QString &methodPrefix, QObject *parent)
37     : QObject(parent),
38     _defaultHandler(-1),
39     _initDone(false),
40     _methodPrefix(methodPrefix)
41 {
42 }
43
44
45 QStringList BasicHandler::providesHandlers()
46 {
47     return handlerHash().keys();
48 }
49
50
51 const QHash<QString, int> &BasicHandler::handlerHash()
52 {
53     if (!_initDone) {
54         for (int i = metaObject()->methodOffset(); i < metaObject()->methodCount(); i++) {
55             QString methodSignature(metaObject()->method(i).signature());
56             if (methodSignature.startsWith("defaultHandler")) {
57                 _defaultHandler = i;
58                 continue;
59             }
60
61             if (!methodSignature.startsWith(_methodPrefix))
62                 continue;
63
64             methodSignature = methodSignature.section('(', 0, 0); // chop the attribute list
65             methodSignature = methodSignature.mid(_methodPrefix.length()); // strip "handle" or whatever the prefix is
66             _handlerHash[methodSignature] = i;
67         }
68         _initDone = true;
69     }
70     return _handlerHash;
71 }
72
73
74 void BasicHandler::handle(const QString &member, QGenericArgument val0,
75     QGenericArgument val1, QGenericArgument val2,
76     QGenericArgument val3, QGenericArgument val4,
77     QGenericArgument val5, QGenericArgument val6,
78     QGenericArgument val7, QGenericArgument val8)
79 {
80     // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
81     // and now we even have a fast lookup! Thanks thiago!
82
83     QString handler = member.toLower();
84     handler[0] = handler[0].toUpper();
85
86     if (!handlerHash().contains(handler)) {
87         if (_defaultHandler == -1) {
88             qWarning() << QString("No such Handler: %1::%2%3").arg(metaObject()->className(), _methodPrefix, handler);
89             return;
90         }
91         else {
92             void *param[] = { 0, Q_ARG(QString, member).data(), val0.data(), val1.data(), val2.data(), val3.data(), val4.data(),
93                               val5.data(), val6.data(), val7.data(), val8.data(), val8.data() };
94             qt_metacall(QMetaObject::InvokeMetaMethod, _defaultHandler, param);
95             return;
96         }
97     }
98
99     void *param[] = { 0, val0.data(), val1.data(), val2.data(), val3.data(), val4.data(),
100                       val5.data(), val6.data(), val7.data(), val8.data(), val8.data(), 0 };
101     qt_metacall(QMetaObject::InvokeMetaMethod, handlerHash()[handler], param);
102 }