summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/kopetemessagehandlerchain.cpp
blob: 26def158e3fe5d2830d8c92bb11e8285a18d0015 (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
/*
    kopetemessagehandlerchain.h - Kopete Message Handler Chain

    Copyright (c) 2004      by Richard Smith         <[email protected]>
    Kopete    (c) 2002-2004 by the Kopete developers <[email protected]>

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

#include "kopetemessagehandlerchain.h"
#include "kopetemessagehandler.h"
#include "kopetemessageevent.h"
#include "kopetechatsession.h"

#include <kdebug.h>

#include <tqmap.h>
#include <tqtimer.h>
#include <tqvaluelist.h>

namespace Kopete
{

class MessageHandlerChainTerminator : public MessageHandler
{
public:
	void handleMessage( MessageEvent *event )
	{
		kdError( 14010 ) << k_funcinfo << "message got to end of chain!" << endl;
		event->discard();
	}
	int capabilities()
	{
		kdError( 14010 ) << k_funcinfo << "request got to end of chain!" << endl;
		return 0;
	}
};

// BEGIN MessageHandlerChain

class MessageHandlerChain::Private
{
public:
	Private() : first(0) {}
	MessageHandler *first;
};

MessageHandlerChain::Ptr MessageHandlerChain::create( ChatSession *manager, Message::MessageDirection direction )
{
	// create the handler chain
	MessageHandlerChain *chain = new MessageHandlerChain;
	
	// grab the list of handler factories
	typedef MessageHandlerFactory::FactoryList FactoryList;
	FactoryList factories = MessageHandlerFactory::messageHandlerFactories();
	
	// create a sorted list of handlers
	typedef TQValueList<MessageHandler*> HandlerList;
	typedef TQMap<int,HandlerList> HandlerMap;
	HandlerMap handlers;
	uint count = 0;
	for( FactoryList::Iterator it = factories.begin(); it != factories.end(); ++it )
	{
		int position = (*it)->filterPosition( manager, direction );
		if ( position == MessageHandlerFactory::StageDoNotCreate )
			continue;
		MessageHandler *handler = (*it)->create( manager, direction );
		if ( handler )
		{
			++count;
			handlers[ position ].append( handler );
		}
	}
	
	kdDebug(14010) << k_funcinfo << "got " << count << " handlers for chain" << endl;
	
	// add the handlers to the chain
	MessageHandler *curr = 0;
	for( HandlerMap::Iterator it = handlers.begin(); it != handlers.end(); ++it )
	{
		for ( HandlerList::Iterator handlerIt = (*it).begin(); handlerIt != (*it).end(); ++handlerIt )
		{
			if ( curr )
				curr->setNext( *handlerIt );
			else
				chain->d->first = *handlerIt;
			curr = *handlerIt;
		}
	}
	
	// add a terminator to avoid crashes if the message somehow manages to get to the
	// end of the chain. maybe we should use a MessageHandlerFactory for this too?
	MessageHandler *terminator = new MessageHandlerChainTerminator;
	if ( curr )
		curr->setNext( terminator );
	else // empty chain: might happen for dir == Internal
		chain->d->first = terminator;
	
	return chain;
}

MessageHandlerChain::MessageHandlerChain()
 : TQObject( 0 ), d( new Private )
{
}

MessageHandlerChain::~MessageHandlerChain()
{
	kdDebug(14010) << k_funcinfo << endl;
	MessageHandler *handler = d->first;
	while( handler )
	{
		MessageHandler *next = handler->next();
		delete handler;
		handler = next;
	}
	delete d;
}


ProcessMessageTask *MessageHandlerChain::processMessage( const Message &message )
{
	MessageEvent *event = new MessageEvent( message );
	return new ProcessMessageTask( this, event );
}

int MessageHandlerChain::capabilities()
{
	return d->first->capabilities();
}

// END MessageHandlerChain

// BEGIN ProcessMessageTask

class ProcessMessageTask::Private
{
public:
	Private( MessageHandlerChain::Ptr chain, MessageEvent *event ) : chain(chain), event(event) {}
	MessageHandlerChain::Ptr chain;
	MessageEvent *event;
};

ProcessMessageTask::ProcessMessageTask( MessageHandlerChain::Ptr chain, MessageEvent *event )
 : d( new Private(chain, event) )
{
	TQTimer::singleShot( 0, this, TQT_SLOT( slotStart() ) );
	connect( event, TQT_SIGNAL( done( Kopete::MessageEvent* ) ), this, TQT_SLOT( slotDone() ) );
	event->message().manager()->ref();
}

ProcessMessageTask::~ProcessMessageTask()
{
	delete d;
}

void ProcessMessageTask::slotStart()
{
	d->chain->d->first->handleMessageInternal( d->event );
}

void ProcessMessageTask::slotDone()
{
	d->event->message().manager()->deref();
	emitResult();
}

MessageEvent *ProcessMessageTask::event()
{
	return d->event;
}

//END ProcessMessageTask

}

#include "kopetemessagehandlerchain.moc"