summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/irc/ircsignalhandler.h
blob: 58a79e65f974bc3d090860c307c91676737f56ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

/*
    ircsignalhandler.h - Maps signals from the IRC engine to contacts

    Copyright (c) 2004      by Jason Keirstead <jason@keirstead.org>

    Kopete    (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; either version 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#ifndef _IRC_SIGNAL_HANDLER_H
#define _IRC_SIGNAL_HANDLER_H

#include <tqobject.h>
#include <tqstringlist.h>
#include <tqdatetime.h>

#include <kdebug.h>

#include "ircaccount.h"
#include "irccontactmanager.h"

/***
* IRC Signal handler. Mapps a KIRC engine signal to the right contact. Avoids
* Having a signal connected to 500+ slots where only one is valid, instead
* uses the contact dictionary.
*
* Warning: This file has a lot of black magic in it. Avoid it if
* you don't want your eyes to bleed. More below...
*
* Define some templated classes and methods to map a KIRC signal to the
* right contact. Having these templates greatly cuts down *A LOT* on the amount of
* code that would need to be in the signal mapper, at the expense of some readability.
*
* There are four IRCSignalMapping classes, one each for signals with 0, 1, 2,
* and 3 arguments ( plus the contact ID ). The classes take the signal, look
* up the contact it is for, and call the function passed into the class by the
* mapping function.
*
* Since TQObjects cannot be inside templates, the QMember classes that connect
* to the slots are seperate.
*/

/*** Pre-declare mapping types for the TQObjects **/
struct IRCSignalMappingBase{};

struct IRCSignalMappingT : IRCSignalMappingBase
{
	virtual void exec( const TQString & ) = 0;
	virtual ~IRCSignalMappingT() {};
};

struct IRCSignalMappingSingleT : IRCSignalMappingBase
{
	virtual void exec( const TQString &, const TQString & ) = 0;
	virtual ~IRCSignalMappingSingleT() {};
};

struct IRCSignalMappingDoubleT : IRCSignalMappingBase
{
	virtual void exec( const TQString &, const TQString &, const TQString & ) = 0;
	virtual ~IRCSignalMappingDoubleT() {};
};

struct IRCSignalMappingTripleT : IRCSignalMappingBase
{
	virtual void exec( const TQString &, const TQString &, const TQString &, const TQString & ) = 0;
	virtual ~IRCSignalMappingTripleT() {};
};

/***
TQObject members, these connect to the KIRC signals and call
the Mapping functions when they emit.
**/

class QMember : public TQObject
{
	Q_OBJECT
  

	public:
		QMember( IRCSignalMappingT *m, TQObject *p ) : TQObject( p ), mapping( m ){};

	public slots:
		void slotEmit( const TQString &id )
		{
			//kdDebug(14120) << k_funcinfo << id << endl;
			mapping->exec(id);
		}

	private:
		IRCSignalMappingT *mapping;
};

class QMemberSingle : public TQObject
{
	Q_OBJECT
  

	public:
		QMemberSingle( IRCSignalMappingSingleT *m, TQObject *p ) : TQObject( p ), mapping( m ){}

	public slots:
		void slotEmit( const TQString &id, const TQString &arg )
		{
			//kdDebug(14120) << k_funcinfo << id << " : " << arg  << endl;
			mapping->exec(id,arg);
		}

	private:
		IRCSignalMappingSingleT *mapping;
};

class QMemberDouble : public TQObject
{
	Q_OBJECT
  

	public:
		QMemberDouble( IRCSignalMappingDoubleT *m, TQObject *p ) : TQObject( p ), mapping( m ){}

	public slots:
		void slotEmit( const TQString &id, const TQString &arg, const TQString &arg2 )
		{
			//kdDebug(14120) << k_funcinfo << id << " : " << arg << " : " << arg2 << endl;
			mapping->exec(id,arg,arg2);
		}

	private:
		IRCSignalMappingDoubleT *mapping;
};

class QMemberTriple : public TQObject
{
	Q_OBJECT
  

	public:
		QMemberTriple( IRCSignalMappingTripleT *m, TQObject *p ) : TQObject( p ), mapping( m ){}

	public slots:
		void slotEmit( const TQString &id, const TQString &arg, const TQString &arg2, const TQString &arg3 )
		{
			//kdDebug(14120) << k_funcinfo << id << " : " << arg << " : " << arg2 << " : " << arg3 << endl;
			mapping->exec(id,arg,arg2,arg3);
		}

	private:
		IRCSignalMappingTripleT *mapping;
};

/***
Mapping classes. These contain pointers to the functions to call. We first
look up the right contact in the contact manager's dictionary, and then
call the method
**/

template <class TClass>
class IRCSignalMapping : public IRCSignalMappingT
{
	public:
		IRCSignalMapping( IRCContactManager *mgr, const char * /*signal*/,
			void (TClass::*m)() ) : manager(mgr), method(m){}

		void exec( const TQString &id )
		{
			TClass *c = (TClass*)manager->findContact( id );
			if( c )
			{
				void (TClass::*tmp)() = (void (TClass::*)())method;
				(*c.*tmp)();
			}
		}

	private:
		IRCContactManager *manager;
		void (TClass::*method)();
};

template <class TClass>
class IRCSignalMappingSingle : public IRCSignalMappingSingleT
{
	public:
		IRCSignalMappingSingle<TClass>( IRCContactManager *mgr, const char * /*signal*/,
			void (TClass::*m)(const TQString&) ) : manager(mgr), method(m){}

		void exec( const TQString &id, const TQString &arg )
		{
			TClass *c = (TClass*)manager->findContact( id );
			if( c )
			{
				void (TClass::*tmp)(const TQString&) = (void (TClass::*)(const TQString&))method;
				(*c.*tmp)( arg );
			}
		}

	private:
		IRCContactManager *manager;
		void (TClass::*method)(const TQString &);
};

template <class TClass>
class IRCSignalMappingDouble : public IRCSignalMappingDoubleT
{
	public:
		IRCSignalMappingDouble<TClass>( IRCContactManager *mgr, const char * /*signal*/,
			void (TClass::*m)(const TQString&,const TQString&) ) : manager(mgr), method(m){}

		void exec( const TQString &id,const TQString &arg, const TQString &arg2 )
		{
			TClass *c = (TClass*)manager->findContact( id );
			if( c )
			{
				void (TClass::*tmp)(const TQString&,const TQString&) =
					(void (TClass::*)(const TQString&,const TQString&))method;
				(*c.*tmp)(arg,arg2);
			}
		}

	private:
		IRCContactManager *manager;
		void (TClass::*method)(const TQString &,const TQString &);
};

template <class TClass>
class IRCSignalMappingTriple : public IRCSignalMappingTripleT
{
	public:
		IRCSignalMappingTriple<TClass>( IRCContactManager *mgr, const char * /*signal*/,
			void (TClass::*m)(const TQString&,const TQString&,const TQString&) )
			: manager(mgr), method(m){}

		void exec( const TQString &id,const TQString&arg, const TQString &arg2, const TQString &arg3 )
		{
			TClass *c = (TClass*)manager->findContact( id );
			if( c )
			{
				void (TClass::*tmp)(const TQString&,const TQString&,const TQString&) =
					(void (TClass::*)(const TQString&,const TQString&,const TQString&))method;
				(*c.*tmp)(arg,arg2,arg3);
			}
		}

	private:
		IRCContactManager *manager;
		void (TClass::*method)(const TQString &,const TQString &,const TQString &);
};

class IRCSignalHandler : public TQObject
{
	Q_OBJECT
  

	public:
		IRCSignalHandler( IRCContactManager *manager );
		~IRCSignalHandler();

	private slots:

		/****
		Slots for signals with non-TQString types
		*/

		//Channel contact slots
		void slotNamesList( const TQString &, const TQStringList & );
		void slotEndOfNames( const TQString & );
		void slotTopicUser( const TQString &, const TQString&, const TQDateTime &);

		//User contact slots
		void slotNewWhoIsIdle(const TQString &, unsigned long  );
		void slotNewWhoReply(const TQString &, const TQString &, const TQString &, const TQString &,
			const TQString &, bool , const TQString &, uint , const TQString & );

	private:
		IRCContactManager *manager;
		TQValueList<IRCSignalMappingBase*> mappings;

		/****
		Signal mapping functions
		*/

		template <class TClass>
		inline void map( IRCContactManager *m, const char* signal, void (TClass::*method)() )
		{
			IRCSignalMappingT *mapping = new IRCSignalMapping<TClass>( m, signal, method );
			mappings.append(mapping);
			TQObject::connect( static_cast<IRCAccount*>( m->mySelf()->account() )->engine(), signal,
				new QMember( mapping, this),
				TQT_SLOT( slotEmit( const TQString &) )
			);
		}

		template <class TClass>
		inline void mapSingle( IRCContactManager *m,
			const char* signal, void (TClass::*method)(const TQString&) )
		{
			IRCSignalMappingSingleT *mapping = new IRCSignalMappingSingle<TClass>( m, signal, method );
			mappings.append(mapping);
			TQObject::connect( static_cast<IRCAccount*>( m->mySelf()->account() )->engine(), signal,
				new QMemberSingle( mapping, this),
				TQT_SLOT( slotEmit( const TQString &, const TQString &) )
			);
		}

		template <class TClass>
		inline void mapDouble( IRCContactManager *m,
			const char* signal, void (TClass::*method)(const TQString&,const TQString&) )
		{
			IRCSignalMappingDoubleT *mapping = new IRCSignalMappingDouble<TClass>( m, signal, method );
			mappings.append(mapping);
			TQObject::connect( static_cast<IRCAccount*>( m->mySelf()->account() )->engine(), signal,
				new QMemberDouble( mapping, this),
				TQT_SLOT( slotEmit( const TQString &, const TQString &,const TQString &) )
			);
		}

		template <class TClass>
		inline void mapTriple( IRCContactManager *m,
			const char* signal,
			void (TClass::*method)(const TQString&,const TQString &, const TQString &) )
		{
			IRCSignalMappingTripleT *mapping = new IRCSignalMappingTriple<TClass>( m, signal, method );
			mappings.append(mapping);
			TQObject::connect( static_cast<IRCAccount*>( m->mySelf()->account() )->engine(), signal,
				new QMemberTriple( mapping, this),
				TQT_SLOT( slotEmit( const TQString &, const TQString &,const TQString &,const TQString &) )
			);
		}
};

#endif