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
)
48 : QObject( parent
), d(new Private())
53 ChatSessionManager::~ChatSessionManager()
56 QList
<ChatSession
*>::ConstIterator it
;
57 for ( it
=d
->sessions
.constBegin() ; it
!=d
->sessions
.constEnd() ; ++it
)
59 kDebug( 14010 ) << "Unloading KMM: Why this KMM isn't yet unloaded?";
65 ChatSession
* ChatSessionManager::findChatSession(const Contact
*user
,
66 ContactPtrList chatContacts
, Protocol
*protocol
)
68 ChatSession
*result
= 0L;
69 QList
<ChatSession
*>::ConstIterator it
;
72 for ( it
= d
->sessions
.constBegin(); it
!=d
->sessions
.constEnd() && !result
; ++it
)
74 ChatSession
* cs
=(*it
);
75 if ( cs
->protocol() == protocol
&& user
== cs
->myself() )
77 QList
<Contact
*> contactlist
= cs
->members();
79 // set this to false if chatContacts doesn't contain current cs's contact list
80 bool halfMatch
= true;
82 for ( i
= 0; i
!= contactlist
.size() && halfMatch
; i
++ )
84 if ( !chatContacts
.contains( contactlist
[i
] ) )
88 // If chatContacts contains current cs's contactlist, try the other way around
91 bool fullMatch
= true;
92 for ( i
= 0; i
!= chatContacts
.size() && fullMatch
; i
++ )
94 if ( !contactlist
.contains( chatContacts
[i
] ) )
106 ChatSession
*ChatSessionManager::create(
107 const Contact
*user
, ContactPtrList chatContacts
, Protocol
*protocol
, Kopete::ChatSession::Form form
)
109 ChatSession
*result
=findChatSession( user
, chatContacts
, protocol
);
112 result
= new ChatSession(user
, chatContacts
, protocol
, form
);
113 registerChatSession(result
);
118 void ChatSessionManager::slotReadMessage()
123 void ChatSessionManager::registerChatSession(ChatSession
* result
)
125 d
->sessions
.append( result
);
128 * There's no need for a slot here... just add a public remove()
129 * method and call from KMM's destructor
131 connect( result
, SIGNAL( messageAppended( Kopete::Message
&, Kopete::ChatSession
* ) ),
132 SIGNAL( aboutToDisplay( Kopete::Message
& ) ) );
133 connect( result
, SIGNAL( messageSent( Kopete::Message
&, Kopete::ChatSession
* ) ),
134 SIGNAL( aboutToSend(Kopete::Message
& ) ) );
135 connect( result
, SIGNAL( messageReceived( Kopete::Message
&, Kopete::ChatSession
* ) ),
136 SIGNAL( aboutToReceive(Kopete::Message
& ) ) );
138 connect( result
, SIGNAL(messageAppended( Kopete::Message
&, Kopete::ChatSession
*) ),
139 SIGNAL( display( Kopete::Message
&, Kopete::ChatSession
*) ) );
141 emit
chatSessionCreated(result
);
145 void ChatSessionManager::removeSession( ChatSession
*session
)
148 d
->sessions
.removeAll( session
);
151 QList
<ChatSession
*> ChatSessionManager::sessions( )
156 KopeteView
* ChatSessionManager::createView( ChatSession
*kmm
, const QString
&requestedPlugin
)
158 KopeteView
*newView
= KopeteViewManager::viewManager()->view(kmm
,requestedPlugin
);
161 kDebug(14010) << "View not successfuly created";
165 QObject
*viewObject
= dynamic_cast<QObject
*>(newView
);
168 connect(viewObject
, SIGNAL(activated(KopeteView
*)),
169 this, SIGNAL(viewActivated(KopeteView
*)));
170 connect(viewObject
, SIGNAL(closing(KopeteView
*)),
171 this, SIGNAL(viewClosing(KopeteView
*)));
175 kWarning(14010) << "Failed to cast view to QObject *";
178 emit
viewCreated( newView
) ;
182 void ChatSessionManager::postNewEvent(MessageEvent
*e
)
187 KopeteView
*ChatSessionManager::activeView()
189 return KopeteViewManager::viewManager()->activeView();
192 } //END namespace Kopete
194 #include "kopetechatsessionmanager.moc"
196 // vim: set noet ts=4 sts=4 sw=4: