2 kopetechatsessionmanager.cpp - Creates chat sessions
4 Copyright (c) 2002-2003 by Duncan Mac-Vicar Prett <duncan@kde.org>
6 Kopete (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
10 * This library is free software; you can redistribute it and/or *
11 * modify it under the terms of the GNU Lesser General Public *
12 * License as published by the Free Software Foundation; either *
13 * version 2 of the License, or (at your option) any later version. *
15 *************************************************************************
18 #include "kopetechatsessionmanager.h"
19 #include "kopeteviewmanager.h"
21 #include <kapplication.h>
24 #include "ui/kopeteview.h"
25 #include "kopetecontact.h"
30 class ChatSessionManager::Private
33 QList
<ChatSession
*> sessions
;
34 // UI::ChatView *activeView;
37 ChatSessionManager
* ChatSessionManager::s_self
= 0L;
39 ChatSessionManager
* ChatSessionManager::self()
42 s_self
= new ChatSessionManager( kapp
);
47 ChatSessionManager::ChatSessionManager( QObject
* parent
)
54 ChatSessionManager::~ChatSessionManager()
57 QList
<ChatSession
*>::Iterator it
;
58 for ( it
=d
->sessions
.begin() ; it
!=d
->sessions
.end() ; ++it
)
60 kDebug( 14010 ) << "Unloading KMM: Why this KMM isn't yet unloaded?";
66 ChatSession
* ChatSessionManager::findChatSession(const Contact
*user
,
67 ContactPtrList chatContacts
, Protocol
*protocol
)
69 ChatSession
*result
= 0L;
70 QList
<ChatSession
*>::Iterator it
;
73 for ( it
= d
->sessions
.begin(); it
!=d
->sessions
.end() && !result
; ++it
)
75 ChatSession
* cs
=(*it
);
76 if ( cs
->protocol() == protocol
&& user
== cs
->myself() )
78 QList
<Contact
*> contactlist
= cs
->members();
80 // set this to false if chatContacts doesn't contain current cs's contact list
81 bool halfMatch
= true;
83 for ( i
= 0; i
!= contactlist
.size() && halfMatch
; i
++ )
85 if ( !chatContacts
.contains( contactlist
[i
] ) )
89 // If chatContacts contains current cs's contactlist, try the other way around
92 bool fullMatch
= true;
93 for ( i
= 0; i
!= chatContacts
.size() && fullMatch
; i
++ )
95 if ( !contactlist
.contains( chatContacts
[i
] ) )
107 ChatSession
*ChatSessionManager::create(
108 const Contact
*user
, ContactPtrList chatContacts
, Protocol
*protocol
, Kopete::ChatSession::Form form
)
110 ChatSession
*result
=findChatSession( user
, chatContacts
, protocol
);
113 result
= new ChatSession(user
, chatContacts
, protocol
, form
);
114 registerChatSession(result
);
119 void ChatSessionManager::slotReadMessage()
124 void ChatSessionManager::registerChatSession(ChatSession
* result
)
126 d
->sessions
.append( result
);
129 * There's no need for a slot here... just add a public remove()
130 * method and call from KMM's destructor
132 connect( result
, SIGNAL( messageAppended( Kopete::Message
&, Kopete::ChatSession
* ) ),
133 SIGNAL( aboutToDisplay( Kopete::Message
& ) ) );
134 connect( result
, SIGNAL( messageSent( Kopete::Message
&, Kopete::ChatSession
* ) ),
135 SIGNAL( aboutToSend(Kopete::Message
& ) ) );
136 connect( result
, SIGNAL( messageReceived( Kopete::Message
&, Kopete::ChatSession
* ) ),
137 SIGNAL( aboutToReceive(Kopete::Message
& ) ) );
139 connect( result
, SIGNAL(messageAppended( Kopete::Message
&, Kopete::ChatSession
*) ),
140 SIGNAL( display( Kopete::Message
&, Kopete::ChatSession
*) ) );
142 emit
chatSessionCreated(result
);
146 void ChatSessionManager::removeSession( ChatSession
*session
)
149 d
->sessions
.removeAll( session
);
152 QList
<ChatSession
*> ChatSessionManager::sessions( )
157 KopeteView
* ChatSessionManager::createView( ChatSession
*kmm
, const QString
&requestedPlugin
)
159 KopeteView
*newView
= KopeteViewManager::viewManager()->view(kmm
,requestedPlugin
);
162 kDebug(14010) << "View not successfuly created";
166 QObject
*viewObject
= dynamic_cast<QObject
*>(newView
);
169 connect(viewObject
, SIGNAL(activated(KopeteView
*)),
170 this, SIGNAL(viewActivated(KopeteView
*)));
171 connect(viewObject
, SIGNAL(closing(KopeteView
*)),
172 this, SIGNAL(viewClosing(KopeteView
*)));
176 kWarning(14010) << "Failed to cast view to QObject *";
179 emit
viewCreated( newView
) ;
183 void ChatSessionManager::postNewEvent(MessageEvent
*e
)
188 KopeteView
*ChatSessionManager::activeView()
190 return KopeteViewManager::viewManager()->activeView();
193 } //END namespace Kopete
195 #include "kopetechatsessionmanager.moc"
197 // vim: set noet ts=4 sts=4 sw=4: