LeechCraft  0.6.70-9312-g4cc613a2df
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 LeechCraft
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) == "LeechCraft::IHookProxy_ptr";
52  }
53 
54 #if QT_VERSION >= 0x050000
55  auto BuildHookSlots (const QObject *obj)
56  {
57  const auto objMo = obj->metaObject ();
58 
59  QHash<QByteArray, QList<QMetaMethod>> hookSlots;
60  for (int i = 0, size = objMo->methodCount (); i < size; ++i)
61  {
62  const auto& method = objMo->method (i);
63  if (IsHookMethod (method))
64  hookSlots [method.name ()] << method;
65  }
66 
67  return hookSlots;
68  }
69 #endif
70 
71  void CheckMatchingSigs (const QObject *snd, const QObject *rcv)
72  {
73 #if QT_VERSION >= 0x050000
74  if (!qEnvironmentVariableIsSet ("LC_VERBOSE_HOOK_CHECKS"))
75  return;
76 
77  const auto& hookSlots = BuildHookSlots (snd);
78 
79  const auto rcvMo = rcv->metaObject ();
80 
81  for (int i = 0, size = rcvMo->methodCount (); i < size; ++i)
82  {
83  const auto& rcvMethod = rcvMo->method (i);
84  if (!IsHookMethod (rcvMethod))
85  continue;
86 
87  const auto& rcvName = rcvMethod.name ();
88  if (!hookSlots.contains (rcvName))
89  {
90  qWarning () << Q_FUNC_INFO
91  << "no method matching method"
92  << rcvName
93  << "(receiver"
94  << rcv
95  << ") in sender object"
96  << snd;
97  continue;
98  }
99 
100  const auto& sndMethods = hookSlots [rcvName];
101  if (std::none_of (sndMethods.begin (), sndMethods.end (),
102  [&rcvMethod] (const QMetaMethod& sndMethod)
103  {
104  return QMetaObject::checkConnectArgs (sndMethod, rcvMethod);
105  }))
106  qWarning () << Q_FUNC_INFO
107  << "incompatible signatures for hook"
108  << rcvName
109  << "in"
110  << snd
111  << "and"
112  << rcv;
113  }
114 #else
115  Q_UNUSED (snd)
116  Q_UNUSED (rcv)
117 #endif
118  }
119 
120 #define LC_N(a) (QMetaObject::normalizedSignature(a))
121 #define LC_TOSLOT(a) ('1' + QByteArray(a))
122 #define LC_TOSIGNAL(a) ('2' + QByteArray(a))
123  void ConnectHookSignals (QObject *sender, QObject *receiver, bool destSlot)
124  {
125  if (destSlot)
126  CheckMatchingSigs (sender, receiver);
127 
128  const QMetaObject *mo = sender->metaObject ();
129  for (int i = 0, size = mo->methodCount (); i < size; ++i)
130  {
131  QMetaMethod method = mo->method (i);
132  if (method.methodType () != QMetaMethod::Signal)
133  continue;
134 
135  if (!IsHookMethod (method))
136  continue;
137 
138 #if QT_VERSION >= 0x050000
139  const auto& signature = method.methodSignature ();
140 #else
141  const auto& signature = method.signature ();
142 #endif
143 
144  if (receiver->metaObject ()->indexOfMethod (LC_N (signature)) == -1)
145  {
146  if (!destSlot)
147  {
148  qWarning () << Q_FUNC_INFO
149  << "not found meta method for"
150  << signature
151  << "in receiver object"
152  << receiver;
153  }
154  continue;
155  }
156 
157  if (!QObject::connect (sender,
158  LC_TOSIGNAL (signature),
159  receiver,
160  destSlot ? LC_TOSLOT (signature) : LC_TOSIGNAL (signature),
161  Qt::UniqueConnection))
162  {
163  qWarning () << Q_FUNC_INFO
164  << "connect for"
165  << sender
166  << "->"
167  << receiver
168  << ":"
169  << signature
170  << "failed";
171  }
172  }
173  }
174 #undef LC_N
175  };
176 
177  void BaseHookInterconnector::AddPlugin (QObject *plugin)
178  {
179  Plugins_.push_back (plugin);
180 
181  ConnectHookSignals (this, plugin, true);
182  }
183 
185  {
186  ConnectHookSignals (object, this, false);
187  }
188 }
189 }
BaseHookInterconnector(QObject *parent=0)
Creates the interconnector with the given parent.
virtual ~BaseHookInterconnector()
Virtual destructor.
void RegisterHookable(QObject *hookable)
Adds a hookable object from the root plugin.
#define LC_N(a)
#define LC_TOSLOT(a)
#define LC_TOSIGNAL(a)
virtual void AddPlugin(QObject *plugin)
Adds a subplugin to this interconnector.