summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/connectionhandler.cpp
blob: 3f2c1eff58e1fec1e4b3acbf36c6c547b909d0e6 (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
/*
    Kopete Oscar Protocol
    Oscar Multiple Connection Handling

    Copyright (c) 2005 Matt Rogers <mattr@kde.org>

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

    *************************************************************************
    *                                                                       *
    * 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 "connectionhandler.h"
#include <tqvaluelist.h>
#include <kdebug.h>
#include "connection.h"
#include "oscartypes.h"

class ConnectionHandler::Private
{
public:
	TQValueList<Connection*> connections;
    TQMap<Connection*, ConnectionRoomInfo> chatRoomConnections;
};

ConnectionHandler::ConnectionHandler()
{
	d = new Private;
}


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

void ConnectionHandler::append( Connection* c )
{
	d->connections.append( c );
}

void ConnectionHandler::remove( Connection* c )
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing connection "
		<< c << endl;
	d->connections.remove( c );
	c->deleteLater();
}

void ConnectionHandler::remove( int family )
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Removing all connections " <<
		"supporting family " << family << endl;
	TQValueList<Connection*>::iterator it = d->connections.begin();
	TQValueList<Connection*>::iterator itEnd = d->connections.end();
	for ( ; it != itEnd; ++it )
	{
		if ( ( *it )->isSupported( family ) )
		{
			Connection* c = ( *it );
			it = d->connections.remove( it );
			c->deleteLater();
		}
	}
}

void ConnectionHandler::clear()
{
	kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Clearing all connections"
		 << endl;
	while ( !d->connections.isEmpty() )
	{
		Connection *c = d->connections.front();
		d->connections.pop_front();
		c->deleteLater();
	}
}

Connection* ConnectionHandler::connectionForFamily( int family ) const
{
	TQValueList<Connection*>::iterator it = d->connections.begin();
	TQValueList<Connection*>::iterator itEnd = d->connections.end();
	int connectionCount = 0;
	Connection* lastConnection = 0;
	for ( ; it != itEnd; ++it )
	{
		if ( ( *it )->isSupported( family ) )
		{
			connectionCount++;
			lastConnection = ( *it );
		}
	}
	if ( connectionCount == 1 )
		return lastConnection;

	return 0;
}

Connection* ConnectionHandler::defaultConnection() const
{
	if ( d->connections.isEmpty() || d->connections.count() > 1 )
		return 0;

	return d->connections.first();
}

void ConnectionHandler::addChatInfoForConnection( Connection* c, Oscar::WORD exchange, const TQString& room )
{
    if ( d->connections.tqfindIndex( c ) == -1 )
        d->connections.append( c );

    ConnectionRoomInfo info = tqMakePair( exchange, room );
    d->chatRoomConnections[c] = info;
}

Connection* ConnectionHandler::connectionForChatRoom( Oscar::WORD exchange, const TQString& room )
{
    ConnectionRoomInfo infoToFind = tqMakePair( exchange, room );
    TQMap<Connection*, ConnectionRoomInfo>::iterator it,  itEnd = d->chatRoomConnections.end();
    for ( it = d->chatRoomConnections.begin(); it != itEnd; ++it )
    {
        if ( it.data() == infoToFind )
        {
            Connection* c = it.key();
            return c;
        }
    }

    return 0;
}

TQString ConnectionHandler::chatRoomForConnection( Connection* c )
{
    if ( d->connections.tqfindIndex( c ) == -1 )
        return TQString();

    TQMap<Connection*, ConnectionRoomInfo>::iterator it, itEnd = d->chatRoomConnections.end();
    for ( it = d->chatRoomConnections.begin(); it != itEnd; ++it )
    {
        if ( it.key() == c )
        {
            TQString room = it.data().second;
            return room;
        }
    }

    return TQString();
}

Oscar::WORD ConnectionHandler::exchangeForConnection( Connection* c )
{

    if ( d->connections.tqfindIndex( c ) == -1 )
        return 0xFFFF;

    TQMap<Connection*, ConnectionRoomInfo>::iterator it, itEnd = d->chatRoomConnections.end();
    for ( it = d->chatRoomConnections.begin(); it != itEnd; ++it )
    {
        if ( it.key() == c )
        {
            Oscar::WORD exchange = it.data().first;
            return exchange;
        }
    }

    return 0xFFFF;
}