kopete/libkopete
kopetechatsessionmanager.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "kopetechatsessionmanager.h"
00019 #include "kopeteviewmanager.h"
00020
00021 #include <kapplication.h>
00022 #include <kdebug.h>
00023
00024 #include "ui/kopeteview.h"
00025 #include "kopetecontact.h"
00026 #include <QList>
00027
00028 namespace Kopete {
00029
00030 class ChatSessionManager::Private
00031 {
00032 public:
00033 QList <ChatSession*> sessions;
00034
00035 };
00036
00037 ChatSessionManager* ChatSessionManager::s_self = 0L;
00038
00039 ChatSessionManager* ChatSessionManager::self()
00040 {
00041 if( !s_self )
00042 s_self = new ChatSessionManager( kapp );
00043
00044 return s_self;
00045 }
00046
00047 ChatSessionManager::ChatSessionManager( QObject* parent )
00048 : QObject( parent )
00049 {
00050 d=new Private;
00051 s_self = this;
00052 }
00053
00054 ChatSessionManager::~ChatSessionManager()
00055 {
00056 s_self = 0L;
00057 QList<ChatSession*>::Iterator it;
00058 for ( it=d->sessions.begin() ; it!=d->sessions.end() ; ++it )
00059 {
00060 kDebug( 14010 ) << "Unloading KMM: Why this KMM isn't yet unloaded?";
00061 (*it)->deleteLater();
00062 }
00063 delete d;
00064 }
00065
00066 ChatSession* ChatSessionManager::findChatSession(const Contact *user,
00067 ContactPtrList chatContacts, Protocol *protocol)
00068 {
00069 ChatSession *result = 0L;
00070 QList<ChatSession*>::Iterator it;
00071 int i;
00072
00073 for ( it= d->sessions.begin(); it!=d->sessions.end() && !result ; ++it )
00074 {
00075 ChatSession* cs=(*it);
00076 if ( cs->protocol() == protocol && user == cs->myself() )
00077 {
00078 QList<Contact*> contactlist = cs->members();
00079
00080
00081 bool halfMatch = true;
00082
00083 for ( i = 0; i != contactlist.size() && halfMatch; i++ )
00084 {
00085 if ( !chatContacts.contains( contactlist[i] ) )
00086 halfMatch = false;
00087 }
00088
00089
00090 if (halfMatch)
00091 {
00092 bool fullMatch = true;
00093 for ( i = 0; i != chatContacts.size() && fullMatch; i++ )
00094 {
00095 if ( !contactlist.contains( chatContacts[i] ) )
00096 fullMatch = false;
00097 }
00098
00099 if (fullMatch)
00100 result = cs;
00101 }
00102 }
00103 }
00104 return result;
00105 }
00106
00107 ChatSession *ChatSessionManager::create(
00108 const Contact *user, ContactPtrList chatContacts, Protocol *protocol, Kopete::ChatSession::Form form )
00109 {
00110 ChatSession *result=findChatSession( user, chatContacts, protocol);
00111 if (!result)
00112 {
00113 result = new ChatSession(user, chatContacts, protocol, form );
00114 registerChatSession(result);
00115 }
00116 return (result);
00117 }
00118
00119 void ChatSessionManager::slotReadMessage()
00120 {
00121 emit readMessage();
00122 }
00123
00124 void ChatSessionManager::registerChatSession(ChatSession * result)
00125 {
00126 d->sessions.append( result );
00127
00128
00129
00130
00131
00132 connect( result, SIGNAL( messageAppended( Kopete::Message &, Kopete::ChatSession * ) ),
00133 SIGNAL( aboutToDisplay( Kopete::Message & ) ) );
00134 connect( result, SIGNAL( messageSent( Kopete::Message &, Kopete::ChatSession * ) ),
00135 SIGNAL( aboutToSend(Kopete::Message & ) ) );
00136 connect( result, SIGNAL( messageReceived( Kopete::Message &, Kopete::ChatSession * ) ),
00137 SIGNAL( aboutToReceive(Kopete::Message & ) ) );
00138
00139 connect( result, SIGNAL(messageAppended( Kopete::Message &, Kopete::ChatSession *) ),
00140 SIGNAL( display( Kopete::Message &, Kopete::ChatSession *) ) );
00141
00142 emit chatSessionCreated(result);
00143 }
00144
00145
00146 void ChatSessionManager::removeSession( ChatSession *session)
00147 {
00148 kDebug(14010) ;
00149 d->sessions.removeAll( session );
00150 }
00151
00152 QList<ChatSession*> ChatSessionManager::sessions( )
00153 {
00154 return d->sessions;
00155 }
00156
00157 KopeteView * ChatSessionManager::createView( ChatSession *kmm , const QString &requestedPlugin )
00158 {
00159 KopeteView *newView = KopeteViewManager::viewManager()->view(kmm,requestedPlugin);
00160 if(!newView)
00161 {
00162 kDebug(14010) << "View not successfuly created";
00163 return 0L;
00164 }
00165
00166 QObject *viewObject = dynamic_cast<QObject *>(newView);
00167 if(viewObject)
00168 {
00169 connect(viewObject, SIGNAL(activated(KopeteView *)),
00170 this, SIGNAL(viewActivated(KopeteView *)));
00171 connect(viewObject, SIGNAL(closing(KopeteView *)),
00172 this, SIGNAL(viewClosing(KopeteView *)));
00173 }
00174 else
00175 {
00176 kWarning(14010) << "Failed to cast view to QObject *";
00177 }
00178
00179 emit viewCreated( newView ) ;
00180 return newView;
00181 }
00182
00183 void ChatSessionManager::postNewEvent(MessageEvent *e)
00184 {
00185 emit newEvent(e);
00186 }
00187
00188 KopeteView *ChatSessionManager::activeView()
00189 {
00190 return KopeteViewManager::viewManager()->activeView();
00191 }
00192
00193 }
00194
00195 #include "kopetechatsessionmanager.moc"
00196
00197
00198