src: Yearly copyright bump
[quassel.git] / src / common / basichandler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 <utility>
24
25 #include <QDebug>
26 #include <QMetaMethod>
27
28 BasicHandler::BasicHandler(QObject* parent)
29     : QObject(parent)
30     , _methodPrefix("handle")
31 {}
32
33 BasicHandler::BasicHandler(QString methodPrefix, QObject* parent)
34     : QObject(parent)
35     , _methodPrefix(std::move(methodPrefix))
36 {}
37
38 QStringList BasicHandler::providesHandlers()
39 {
40     return handlerHash().keys();
41 }
42
43 const QHash<QString, int>& BasicHandler::handlerHash()
44 {
45     if (!_initDone) {
46         for (int i = metaObject()->methodOffset(); i < metaObject()->methodCount(); i++) {
47             QString methodSignature = metaObject()->method(i).methodSignature();
48             if (methodSignature.startsWith("defaultHandler")) {
49                 _defaultHandler = i;
50                 continue;
51             }
52
53             if (!methodSignature.startsWith(_methodPrefix))
54                 continue;
55
56             methodSignature = methodSignature.section('(', 0, 0);           // chop the attribute list
57             methodSignature = methodSignature.mid(_methodPrefix.length());  // strip "handle" or whatever the prefix is
58             _handlerHash[methodSignature] = i;
59         }
60         _initDone = true;
61     }
62     return _handlerHash;
63 }
64
65 void BasicHandler::handle(const QString& member,
66                           QGenericArgument val0,
67                           QGenericArgument val1,
68                           QGenericArgument val2,
69                           QGenericArgument val3,
70                           QGenericArgument val4,
71                           QGenericArgument val5,
72                           QGenericArgument val6,
73                           QGenericArgument val7,
74                           QGenericArgument val8)
75 {
76     // Now we try to find a handler for this message. BTW, I do love the Trolltech guys ;-)
77     // and now we even have a fast lookup! Thanks thiago!
78
79     QString handler = member.toLower();
80     handler[0] = handler[0].toUpper();
81
82     if (!handlerHash().contains(handler)) {
83         if (_defaultHandler == -1) {
84             qWarning() << QString("No such Handler: %1::%2%3").arg(metaObject()->className(), _methodPrefix, handler);
85             return;
86         }
87         else {
88             void* param[] = {nullptr,
89                              Q_ARG(QString, member).data(),
90                              val0.data(),
91                              val1.data(),
92                              val2.data(),
93                              val3.data(),
94                              val4.data(),
95                              val5.data(),
96                              val6.data(),
97                              val7.data(),
98                              val8.data(),
99                              val8.data()};
100             qt_metacall(QMetaObject::InvokeMetaMethod, _defaultHandler, param);
101             return;
102         }
103     }
104
105     void* param[] = {nullptr,
106                      val0.data(),
107                      val1.data(),
108                      val2.data(),
109                      val3.data(),
110                      val4.data(),
111                      val5.data(),
112                      val6.data(),
113                      val7.data(),
114                      val8.data(),
115                      val8.data(),
116                      nullptr};
117     qt_metacall(QMetaObject::InvokeMetaMethod, handlerHash()[handler], param);
118 }