Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / libkopete / kopetechatsessionmanager.cpp
blob78041e27eadbfaf1885b80c53139da9e02805309
1 /*
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 *************************************************************************
9 * *
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. *
14 * *
15 *************************************************************************
18 #include "kopetechatsessionmanager.h"
19 #include "kopeteviewmanager.h"
21 #include <kapplication.h>
22 #include <kdebug.h>
24 #include "ui/kopeteview.h"
25 #include "kopetecontact.h"
26 #include <QList>
28 namespace Kopete {
30 class ChatSessionManager::Private
32 public:
33 QList <ChatSession*> sessions;
34 // UI::ChatView *activeView;
37 ChatSessionManager* ChatSessionManager::s_self = 0L;
39 ChatSessionManager* ChatSessionManager::self()
41 if( !s_self )
42 s_self = new ChatSessionManager( kapp );
44 return s_self;
47 ChatSessionManager::ChatSessionManager( QObject* parent )
48 : QObject( parent ), d(new Private())
50 s_self = this;
53 ChatSessionManager::~ChatSessionManager()
55 s_self = 0L;
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?";
60 (*it)->deleteLater();
62 delete d;
65 ChatSession* ChatSessionManager::findChatSession(const Contact *user,
66 ContactPtrList chatContacts, Protocol *protocol)
68 ChatSession *result = 0L;
69 QList<ChatSession*>::ConstIterator it;
70 int i;
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] ) )
85 halfMatch = false;
88 // If chatContacts contains current cs's contactlist, try the other way around
89 if (halfMatch)
91 bool fullMatch = true;
92 for ( i = 0; i != chatContacts.size() && fullMatch; i++ )
94 if ( !contactlist.contains( chatContacts[i] ) )
95 fullMatch = false;
97 // We have a winner
98 if (fullMatch)
99 result = cs;
103 return result;
106 ChatSession *ChatSessionManager::create(
107 const Contact *user, ContactPtrList chatContacts, Protocol *protocol, Kopete::ChatSession::Form form )
109 ChatSession *result=findChatSession( user, chatContacts, protocol);
110 if (!result)
112 result = new ChatSession(user, chatContacts, protocol, form );
113 registerChatSession(result);
115 return (result);
118 void ChatSessionManager::slotReadMessage()
120 emit readMessage();
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)
147 kDebug(14010) ;
148 d->sessions.removeAll( session );
151 QList<ChatSession*> ChatSessionManager::sessions( )
153 return d->sessions;
156 KopeteView * ChatSessionManager::createView( ChatSession *kmm , const QString &requestedPlugin )
158 KopeteView *newView = KopeteViewManager::viewManager()->view(kmm,requestedPlugin);
159 if(!newView)
161 kDebug(14010) << "View not successfuly created";
162 return 0L;
165 QObject *viewObject = dynamic_cast<QObject *>(newView);
166 if(viewObject)
168 connect(viewObject, SIGNAL(activated(KopeteView *)),
169 this, SIGNAL(viewActivated(KopeteView *)));
170 connect(viewObject, SIGNAL(closing(KopeteView *)),
171 this, SIGNAL(viewClosing(KopeteView *)));
173 else
175 kWarning(14010) << "Failed to cast view to QObject *";
178 emit viewCreated( newView ) ;
179 return newView;
182 void ChatSessionManager::postNewEvent(MessageEvent *e)
184 emit newEvent(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: