1 /*
2 Kopete Oscar Protocol
3 Oscar Multiple Connection Handling
4
5 Copyright (c) 2005 Matt Rogers <mattr@kde.org>
6
7 Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
8
9 *************************************************************************
10 * *
11 * This library is free software; you can redistribute it and/or *
12 * modify it under the terms of the GNU Lesser General Public *
13 * License as published by the Free Software Foundation; either *
14 * version 2 of the License, or (at your option) any later version. *
15 * *
16 *************************************************************************
17 */
18
19 #include "connectionhandler.h"
20 #include <qlist.h>
21 #include <qmap.h>
22 #include <kdebug.h>
23 #include "connection.h"
24 #include "oscartypes.h"
25
26 class ConnectionHandler::Private
27 {
28 public:
29 QList<Connection*> connections;
30 QMap<Connection*, ConnectionRoomInfo> chatRoomConnections;
31 };
32
ConnectionHandler()33 ConnectionHandler::ConnectionHandler()
34 {
35 d = new Private;
36 }
37
~ConnectionHandler()38 ConnectionHandler::~ConnectionHandler()
39 {
40 delete d;
41 }
42
append(Connection * c)43 void ConnectionHandler::append( Connection* c )
44 {
45 d->connections.append( c );
46 }
47
remove(Connection * c)48 void ConnectionHandler::remove( Connection* c )
49 {
50 kDebug(OSCAR_RAW_DEBUG) << "Removing connection "
51 << c << endl;
52 d->connections.removeAll( c );
53 c->deleteLater();
54 }
55
remove(int family)56 void ConnectionHandler::remove( int family )
57 {
58 kDebug(OSCAR_RAW_DEBUG) << "Removing all connections " <<
59 "supporting family " << family << endl;
60 QList<Connection*>::iterator it = d->connections.begin();
61 QList<Connection*>::iterator itEnd = d->connections.end();
62 for ( ; it != itEnd; ++it )
63 {
64 if ( ( *it )->isSupported( family ) )
65 {
66 Connection* c = ( *it );
67 it = d->connections.erase( it );
68 c->deleteLater();
69 }
70 }
71 }
72
clear()73 void ConnectionHandler::clear()
74 {
75 kDebug(OSCAR_RAW_DEBUG) << "Clearing all connections"
76 << endl;
77 while ( !d->connections.isEmpty() )
78 {
79 Connection *c = d->connections.front();
80 d->connections.pop_front();
81 c->deleteLater();
82 }
83 }
84
connectionForFamily(int family) const85 Connection* ConnectionHandler::connectionForFamily( int family ) const
86 {
87 QList<Connection*>::iterator it = d->connections.begin();
88 QList<Connection*>::iterator itEnd = d->connections.end();
89 int connectionCount = 0;
90 Connection* lastConnection = 0;
91 for ( ; it != itEnd; ++it )
92 {
93 if ( ( *it )->isSupported( family ) )
94 {
95 connectionCount++;
96 lastConnection = ( *it );
97 }
98 }
99 if ( connectionCount == 1 )
100 return lastConnection;
101
102 return 0;
103 }
104
defaultConnection() const105 Connection* ConnectionHandler::defaultConnection() const
106 {
107 if ( d->connections.isEmpty() || d->connections.count() > 1 )
108 return 0;
109
110 return d->connections.first();
111 }
112
connections() const113 QList<Connection*> ConnectionHandler::connections() const
114 {
115 return d->connections;
116 }
117
addChatInfoForConnection(Connection * c,Oscar::WORD exchange,const QString & room)118 void ConnectionHandler::addChatInfoForConnection( Connection* c, Oscar::WORD exchange, const QString& room )
119 {
120 if ( d->connections.indexOf( c ) == -1 )
121 d->connections.append( c );
122
123 ConnectionRoomInfo info = qMakePair( exchange, room );
124 d->chatRoomConnections[c] = info;
125 }
126
connectionForChatRoom(Oscar::WORD exchange,const QString & room)127 Connection* ConnectionHandler::connectionForChatRoom( Oscar::WORD exchange, const QString& room )
128 {
129 ConnectionRoomInfo infoToFind = qMakePair( exchange, room );
130 QMap<Connection*, ConnectionRoomInfo>::iterator it, itEnd = d->chatRoomConnections.end();
131 for ( it = d->chatRoomConnections.begin(); it != itEnd; ++it )
132 {
133 if ( it.value() == infoToFind )
134 {
135 Connection* c = it.key();
136 return c;
137 }
138 }
139
140 return 0;
141 }
142
chatRoomForConnection(Connection * c)143 QString ConnectionHandler::chatRoomForConnection( Connection* c )
144 {
145 if ( d->connections.indexOf( c ) == -1 )
146 return QString();
147
148 QMap<Connection*, ConnectionRoomInfo>::iterator it, itEnd = d->chatRoomConnections.end();
149 for ( it = d->chatRoomConnections.begin(); it != itEnd; ++it )
150 {
151 if ( it.key() == c )
152 {
153 QString room = it.value().second;
154 return room;
155 }
156 }
157
158 return QString();
159 }
160
exchangeForConnection(Connection * c)161 Oscar::WORD ConnectionHandler::exchangeForConnection( Connection* c )
162 {
163
164 if ( d->connections.indexOf( c ) == -1 )
165 return 0xFFFF;
166
167 QMap<Connection*, ConnectionRoomInfo>::iterator it, itEnd = d->chatRoomConnections.end();
168 for ( it = d->chatRoomConnections.begin(); it != itEnd; ++it )
169 {
170 if ( it.key() == c )
171 {
172 Oscar::WORD exchange = it.value().first;
173 return exchange;
174 }
175 }
176
177 return 0xFFFF;
178 }
179
180