LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
basehookinterconnector.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "basehookinterconnector.h"
31 #include <QMetaMethod>
32 #include <QtDebug>
33 
34 namespace LC
35 {
36 namespace Util
37 {
39  : QObject (parent)
40  {
41  }
42 
44  {
45  }
46 
47  namespace
48  {
49  bool IsHookMethod (const QMetaMethod& method)
50  {
51  return method.parameterTypes ().value (0) == "LC::IHookProxy_ptr";
52  }
53 
54  auto BuildHookSlots (const QObject *obj)
55  {
56  const auto objMo = obj->metaObject ();
57 
58  QHash<QByteArray, QList<QMetaMethod>> hookSlots;
59  for (int i = 0, size = objMo->methodCount (); i < size; ++i)
60  {
61  const auto& method = objMo->method (i);
62  if (IsHookMethod (method))
63  hookSlots [method.name ()] << method;
64  }
65 
66  return hookSlots;
67  }
68 
69  void CheckMatchingSigs (const QObject *snd, const QObject *rcv)
70  {
71  if (!qEnvironmentVariableIsSet ("LC_VERBOSE_HOOK_CHECKS"))
72  return;
73 
74  const auto& hookSlots = BuildHookSlots (snd);
75 
76  const auto rcvMo = rcv->metaObject ();
77 
78  for (int i = 0, size = rcvMo->methodCount (); i < size; ++i)
79  {
80  const auto& rcvMethod = rcvMo->method (i);
81  if (!IsHookMethod (rcvMethod))
82  continue;
83 
84  const auto& rcvName = rcvMethod.name ();
85  if (!hookSlots.contains (rcvName))
86  {
87  qWarning () << Q_FUNC_INFO
88  << "no method matching method"
89  << rcvName
90  << "(receiver"
91  << rcv
92  << ") in sender object"
93  << snd;
94  continue;
95  }
96 
97  const auto& sndMethods = hookSlots [rcvName];
98  if (std::none_of (sndMethods.begin (), sndMethods.end (),
99  [&rcvMethod] (const QMetaMethod& sndMethod)
100  {
101  return QMetaObject::checkConnectArgs (sndMethod, rcvMethod);
102  }))
103  qWarning () << Q_FUNC_INFO
104  << "incompatible signatures for hook"
105  << rcvName
106  << "in"
107  << snd
108  << "and"
109  << rcv;
110  }
111  }
112 
113 #define LC_N(a) (QMetaObject::normalizedSignature(a))
114 #define LC_TOSLOT(a) ('1' + QByteArray(a))
115 #define LC_TOSIGNAL(a) ('2' + QByteArray(a))
116  void ConnectHookSignals (QObject *sender, QObject *receiver, bool destSlot)
117  {
118  if (destSlot)
119  CheckMatchingSigs (sender, receiver);
120 
121  const QMetaObject *mo = sender->metaObject ();
122  for (int i = 0, size = mo->methodCount (); i < size; ++i)
123  {
124  QMetaMethod method = mo->method (i);
125  if (method.methodType () != QMetaMethod::Signal)
126  continue;
127 
128  if (!IsHookMethod (method))
129  continue;
130 
131  const auto& signature = method.methodSignature ();
132  if (receiver->metaObject ()->indexOfMethod (LC_N (signature)) == -1)
133  {
134  if (!destSlot)
135  {
136  qWarning () << Q_FUNC_INFO
137  << "not found meta method for"
138  << signature
139  << "in receiver object"
140  << receiver;
141  }
142  continue;
143  }
144 
145  if (!QObject::connect (sender,
146  LC_TOSIGNAL (signature),
147  receiver,
148  destSlot ? LC_TOSLOT (signature) : LC_TOSIGNAL (signature),
149  Qt::UniqueConnection))
150  {
151  qWarning () << Q_FUNC_INFO
152  << "connect for"
153  << sender
154  << "->"
155  << receiver
156  << ":"
157  << signature
158  << "failed";
159  }
160  }
161  }
162 #undef LC_N
163  };
164 
165  void BaseHookInterconnector::AddPlugin (QObject *plugin)
166  {
167  Plugins_.push_back (plugin);
168 
169  ConnectHookSignals (this, plugin, true);
170  }
171 
173  {
174  ConnectHookSignals (object, this, false);
175  }
176 }
177 }
void RegisterHookable(QObject *hookable)
Adds a hookable object from the root plugin.
virtual ~BaseHookInterconnector()
Virtual destructor.
BaseHookInterconnector(QObject *parent=0)
Creates the interconnector with the given parent.
virtual void AddPlugin(QObject *plugin)
Adds a subplugin to this interconnector.
#define LC_N(a)
#define LC_TOSLOT(a)
#define LC_TOSIGNAL(a)
Definition: constants.h:35