Allow arbitrary method prefix for BasicHandler
[quassel.git] / src / common / basichandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-10 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include "basichandler.h"
21
22 #include <QMetaMethod>
23
24 #include "logger.h"
25
26 BasicHandler::BasicHandler(QObject *parent)
27   : QObject(parent),
28     _defaultHandler(-1),
29     _initDone(false),
30     _methodPrefix("handle")
31 {
32 }
33
34 BasicHandler::BasicHandler(const QString &methodPrefix, QObject *parent)
35   : QObject(parent),
36     _defaultHandler(-1),
37     _initDone(false),
38     _methodPrefix(methodPrefix)
39 {
40 }
41
42 QStringList BasicHandler::providesHandlers() {
43   return handlerHash().keys();
44 }
45
46 const QHash<QString, int> &BasicHandler::handlerHash() {
47   if(!_initDone) {
48     for(int i = metaObject()->methodOffset(); i < metaObject()->methodCount(); i++) {
49       QString methodSignature(metaObject()->method(i).signature());
50       if(methodSignature.startsWith("defaultHandler")) {
51         _defaultHandler = i;
52         continue;
53       }
54
55       if(!methodSignature.startsWith(_methodPrefix))
56         continue;
57
58       methodSignature = methodSignature.section('(',0,0);  // chop the attribute list
59       methodSignature = methodSignature.mid(_methodPrefix.length()); // strip "handle" or whatever the prefix is
60       _handlerHash[methodSignature] = i;
61     }
62     _initDone = true;
63   }
64   return _handlerHash;
65 }
66
67 void BasicHandler::handle(const QString &member, QGenericArgument val0,
68                           QGenericArgument val1, QGenericArgument val2,
69                           QGenericArgument val3, QGenericArgument val4,
70                           QGenericArgument val5, QGenericArgument val6,
71                           QGenericArgument val7, QGenericArgument val8) {
72   // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
73   // and now we even have a fast lookup! Thanks thiago!
74
75   QString handler = member.toLower();
76   handler[0] = handler[0].toUpper();
77
78   if(!handlerHash().contains(handler)) {
79     if(_defaultHandler == -1) {
80       qWarning() << QString("No such Handler: %1::%2%3").arg(metaObject()->className(), _methodPrefix, handler);
81       return;
82     } else {
83       void *param[] = {0, Q_ARG(QString, member).data(), val0.data(), val1.data(), val2.data(), val3.data(), val4.data(),
84                        val5.data(), val6.data(), val7.data(), val8.data(), val8.data()};
85       qt_metacall(QMetaObject::InvokeMetaMethod, _defaultHandler, param);
86       return;
87     }
88   }
89
90   void *param[] = {0, val0.data(), val1.data(), val2.data(), val3.data(), val4.data(),
91                    val5.data(), val6.data(), val7.data(), val8.data(), val8.data(), 0};
92   qt_metacall(QMetaObject::InvokeMetaMethod, handlerHash()[handler], param);
93 }