8ddfc0bea0ea09b11d5ee9ed9bddf4936feb419f
[quassel.git] / src / core / basichandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 "networkconnection.h"
25
26 BasicHandler::BasicHandler(NetworkConnection *parent)
27   : QObject(parent),
28     server(parent) {
29   
30   connect(this, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)),
31           server, SIGNAL(displayMsg(Message::Type, QString, QString, QString, quint8)));
32
33   connect(this, SIGNAL(putCmd(QString, QStringList, QString)),
34           server, SLOT(putCmd(QString, QStringList, QString)));
35
36   connect(this, SIGNAL(putRawLine(QString)),
37           server, SLOT(putRawLine(QString)));
38 }
39
40 QStringList BasicHandler::providesHandlers() const {
41   QStringList handlers;
42   for(int i=0; i < metaObject()->methodCount(); i++) {
43     QString methodSignature(metaObject()->method(i).signature());
44     if(!methodSignature.startsWith("handle"))
45       continue;
46
47     methodSignature = methodSignature.section('(',0,0);  // chop the attribute list
48     methodSignature = methodSignature.mid(6); // strip "handle"
49     handlers << methodSignature;
50   }
51   return handlers;
52 }
53
54
55 void BasicHandler::handle(const QString &member, const QGenericArgument &val0,
56                           const QGenericArgument &val1, const QGenericArgument &val2,
57                           const QGenericArgument &val3, const QGenericArgument &val4,
58                           const QGenericArgument &val5, const QGenericArgument &val6,
59                           const QGenericArgument &val7, const QGenericArgument &val8) {
60
61   // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
62   QString handler = member.toLower();
63   handler[0] = handler[0].toUpper();
64   handler = "handle" + handler;
65
66   if(!QMetaObject::invokeMethod(this, handler.toAscii(), val0, val1, val2, val3, val4, val5, val6, val7, val8))
67     // Ok. Default handler it is.
68     QMetaObject::invokeMethod(this, "defaultHandler", Q_ARG(QString, member), val0, val1, val2, val3, val4, val5, val6, val7, val8);
69      
70 }
71
72 // ====================
73 //  protected:
74 // ====================
75 Network *BasicHandler::network() const {
76   return server->network();
77 }