1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
/*
kopetechatsession.cpp - Manages all chats
Copyright (c) 2002 by Duncan Mac-Vicar Prett <duncan@kde.org>
Copyright (c) 2002 by Daniel Stone <dstone@kde.org>
Copyright (c) 2002-2003 by Martijn Klingens <klingens@kde.org>
Copyright (c) 2002-2004 by Olivier Goffart <ogoffart @ kde.org>
Copyright (c) 2003 by Jason Keirstead <jason@keirstead.org>
Copyright (c) 2005 by Michaƫl Larouche <michael.larouche@kdemail.net>
Kopete (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
*************************************************************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
*************************************************************************
*/
#include "kopetechatsession.h"
#include <tqapplication.h>
#include <tqregexp.h>
#include <kdebug.h>
#include <kdeversion.h>
#include <kglobal.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <knotification.h>
#include "kopeteaccount.h"
#include "kopetecommandhandler.h"
#include "kopetechatsessionmanager.h"
#include "kopetemessagehandlerchain.h"
#include "kopetemetacontact.h"
#include "knotification.h"
#include "kopeteprefs.h"
#include "kopeteuiglobal.h"
#include "kopeteglobal.h"
#include "kopeteview.h"
#include "kopetecontact.h"
class KMMPrivate
{
public:
Kopete::ContactPtrList mContactList;
const Kopete::Contact *mUser;
TQMap<const Kopete::Contact *, Kopete::OnlineStatus> contactStatus;
Kopete::Protocol *mProtocol;
bool isEmpty;
bool mCanBeDeleted;
unsigned int refcount;
bool customDisplayName;
TQDateTime awayTime;
TQString displayName;
KopeteView *view;
bool mayInvite;
Kopete::MessageHandlerChain::Ptr chains[3];
};
Kopete::ChatSession::ChatSession( const Kopete::Contact *user,
Kopete::ContactPtrList others, Kopete::Protocol *protocol, const char *name )
: TQObject( user->account(), name )
{
d = new KMMPrivate;
d->mUser = user;
d->mProtocol = protocol;
d->isEmpty = others.isEmpty();
d->mCanBeDeleted = true;
d->refcount = 0;
d->view = 0L;
d->customDisplayName = false;
d->mayInvite = false;
for ( Kopete::Contact *c = others.first(); c; c = others.next() )
addContact( c, true );
connect( user, TQT_SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ), this,
TQT_SLOT( slotOnlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ) );
if( user->metaContact() )
connect( user->metaContact(), TQT_SIGNAL( photoChanged() ), this, TQT_SIGNAL( photoChanged() ) );
slotUpdateDisplayName();
}
Kopete::ChatSession::~ChatSession()
{
//for ( Kopete::Contact *c = d->mContactList.first(); c; c = d->mContactList.next() )
// c->setConversations( c->conversations() - 1 );
if ( !d )
return;
d->mCanBeDeleted = false; //prevent double deletion
Kopete::ChatSessionManager::self()->removeSession( this );
emit closing( this );
delete d;
}
void Kopete::ChatSession::slotOnlineStatusChanged( Kopete::Contact *c, const Kopete::OnlineStatus &status, const Kopete::OnlineStatus &oldStatus )
{
slotUpdateDisplayName();
emit onlineStatusChanged((Kopete::Contact*)c, status, oldStatus);
}
void Kopete::ChatSession::setContactOnlineStatus( const Kopete::Contact *contact, const Kopete::OnlineStatus &status )
{
Kopete::OnlineStatus oldStatus = d->contactStatus[ contact ];
d->contactStatus[ contact ] = status;
disconnect( contact, TQT_SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ),
this, TQT_SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus &) ) );
emit onlineStatusChanged( (Kopete::Contact*)contact, status, oldStatus );
}
const Kopete::OnlineStatus Kopete::ChatSession::contactOnlineStatus( const Kopete::Contact *contact ) const
{
if ( d->contactStatus.contains( contact ) )
return d->contactStatus[ contact ];
return contact->onlineStatus();
}
const TQString Kopete::ChatSession::displayName()
{
if ( d->displayName.isNull() )
{
slotUpdateDisplayName();
}
return d->displayName;
}
void Kopete::ChatSession::setDisplayName( const TQString &newName )
{
d->displayName = newName;
d->customDisplayName = true;
emit displayNameChanged();
}
void Kopete::ChatSession::slotUpdateDisplayName()
{
if( d->customDisplayName )
return;
Kopete::Contact *c = d->mContactList.first();
//If there is no member yet, don't try to update the display name
if ( !c )
return;
d->displayName=TQString();
do
{
if(! d->displayName.isNull() )
d->displayName.append( TQString::fromLatin1( ", " ) ) ;
if ( c->metaContact() )
d->displayName.append( c->metaContact()->displayName() );
else
{
TQString nick=c->property(Kopete::Global::Properties::self()->nickName()).value().toString();
d->displayName.append( nick.isEmpty() ? c->contactId() : nick );
}
c=d->mContactList.next();
} while (c);
//If we have only 1 contact, add the status of him
if ( d->mContactList.count() == 1 )
{
d->displayName.append( TQString::fromLatin1( " (%1)" ).arg( d->mContactList.first()->onlineStatus().description() ) );
}
emit displayNameChanged();
}
const Kopete::ContactPtrList& Kopete::ChatSession::members() const
{
return d->mContactList;
}
const Kopete::Contact* Kopete::ChatSession::myself() const
{
return d->mUser;
}
Kopete::Protocol* Kopete::ChatSession::protocol() const
{
return d->mProtocol;
}
#include "kopetemessagehandler.h"
#include "kopetemessageevent.h"
// FIXME: remove this and the friend decl in KMM
class Kopete::TemporaryKMMCallbackAppendMessageHandler : public Kopete::MessageHandler
{
Kopete::ChatSession *manager;
public:
TemporaryKMMCallbackAppendMessageHandler( Kopete::ChatSession *manager )
: manager(manager)
{
}
void handleMessage( Kopete::MessageEvent *event )
{
Kopete::Message message = event->message();
emit manager->messageAppended( message, manager );
delete event;
}
};
class TempFactory : public Kopete::MessageHandlerFactory
{
public:
Kopete::MessageHandler *create( Kopete::ChatSession *manager, Kopete::Message::MessageDirection )
{
return new Kopete::TemporaryKMMCallbackAppendMessageHandler( manager );
}
int filterPosition( Kopete::ChatSession *, Kopete::Message::MessageDirection )
{
// FIXME: somewhere after everyone else.
return 100000;
}
};
Kopete::MessageHandlerChain::Ptr Kopete::ChatSession::chainForDirection( Kopete::Message::MessageDirection dir )
{
if( dir < 0 || dir > 2)
kdFatal(14000) << k_funcinfo << "invalid message direction " << dir << endl;
if( !d->chains[dir] )
{
TempFactory theTempFactory;
d->chains[dir] = Kopete::MessageHandlerChain::create( this, dir );
}
return d->chains[dir];
}
void Kopete::ChatSession::sendMessage( Kopete::Message &message )
{
message.setManager( this );
Kopete::Message sentMessage = message;
if ( !Kopete::CommandHandler::commandHandler()->processMessage( message, this ) )
{
emit messageSent( sentMessage, this );
if ( !account()->isAway() || KopetePrefs::prefs()->soundIfAway() )
{
KNotification::event(TQString::fromLatin1( "kopete_outgoing" ), i18n( "Outgoing Message Sent" ) );
}
}
else
{
messageSucceeded();
}
}
void Kopete::ChatSession::messageSucceeded()
{
emit messageSuccess();
}
void Kopete::ChatSession::emitNudgeNotification()
{
KNotification::event( TQString::fromLatin1("buzz_nudge"), i18n("A contact sent you a buzz/nudge.") );
}
void Kopete::ChatSession::appendMessage( Kopete::Message &msg )
{
msg.setManager( this );
if ( msg.direction() == Kopete::Message::Inbound )
{
TQString nick=myself()->property(Kopete::Global::Properties::self()->nickName()).value().toString();
if ( KopetePrefs::prefs()->highlightEnabled() && !nick.isEmpty() &&
msg.plainBody().contains( TQRegExp( TQString::fromLatin1( "\\b(%1)\\b" ).arg( nick ), false ) ) )
{
msg.setImportance( Kopete::Message::Highlight );
}
emit messageReceived( msg, this );
}
// outbound messages here are ones the user has sent that are now
// getting reflected back to the chatwindow. they should go down
// the incoming chain.
Kopete::Message::MessageDirection chainDirection = msg.direction();
if( chainDirection == Kopete::Message::Outbound )
chainDirection = Kopete::Message::Inbound;
chainForDirection( chainDirection )->processMessage( msg );
// emit messageAppended( msg, this );
}
void Kopete::ChatSession::addContact( const Kopete::Contact *c, const Kopete::OnlineStatus &initialStatus, bool suppress )
{
if( !d->contactStatus.contains(c) )
d->contactStatus[ c ] = initialStatus;
addContact( c, suppress );
}
void Kopete::ChatSession::addContact( const Kopete::Contact *c, bool suppress )
{
//kdDebug( 14010 ) << k_funcinfo << endl;
if ( d->mContactList.contains( c ) )
{
kdDebug( 14010 ) << k_funcinfo << "Contact already exists" <<endl;
emit contactAdded( c, suppress );
}
else
{
if ( d->mContactList.count() == 1 && d->isEmpty )
{
kdDebug( 14010 ) << k_funcinfo << " FUCKER ZONE " << endl;
/* We have only 1 contact before, so the status of the
message manager was given from that contact status */
Kopete::Contact *old = d->mContactList.first();
d->mContactList.remove( old );
d->mContactList.append( c );
disconnect( old, TQT_SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ),
this, TQT_SLOT( slotOnlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus &) ) );
if ( old->metaContact() )
{
disconnect( old->metaContact(), TQT_SIGNAL( displayNameChanged( const TQString &, const TQString & ) ), this, TQT_SLOT( slotUpdateDisplayName() ) );
disconnect( old->metaContact(), TQT_SIGNAL( photoChanged() ), this, TQT_SIGNAL( photoChanged() ) );
}
else
disconnect( old, TQT_SIGNAL( propertyChanged( Kopete::Contact *, const TQString &, const TQVariant &, const TQVariant & ) ), this, TQT_SLOT( slotUpdateDisplayName() ) );
emit contactAdded( c, suppress );
emit contactRemoved( old, TQString() );
}
else
{
d->mContactList.append( c );
emit contactAdded( c, suppress );
}
connect( c, TQT_SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ),
this, TQT_SLOT( slotOnlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus &) ) );
;
if ( c->metaContact() )
{
connect( c->metaContact(), TQT_SIGNAL( displayNameChanged( const TQString &, const TQString & ) ), this, TQT_SLOT( slotUpdateDisplayName() ) );
connect( c->metaContact(), TQT_SIGNAL( photoChanged() ), this, TQT_SIGNAL( photoChanged() ) );
}
else
connect( c, TQT_SIGNAL( propertyChanged( Kopete::Contact *, const TQString &, const TQVariant &, const TQVariant & ) ), this, TQT_SLOT( slotUpdateDisplayName() ) );
connect( c, TQT_SIGNAL( contactDestroyed( Kopete::Contact * ) ), this, TQT_SLOT( slotContactDestroyed( Kopete::Contact * ) ) );
slotUpdateDisplayName();
}
d->isEmpty = false;
}
void Kopete::ChatSession::removeContact( const Kopete::Contact *c, const TQString& reason, Kopete::Message::MessageFormat format, bool suppressNotification )
{
kdDebug( 14010 ) << k_funcinfo << endl;
if ( !c || !d->mContactList.contains( c ) )
return;
if ( d->mContactList.count() == 1 )
{
kdDebug( 14010 ) << k_funcinfo << "Contact not removed. Keep always one contact" << endl;
d->isEmpty = true;
}
else
{
d->mContactList.remove( c );
disconnect( c, TQT_SIGNAL( onlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus & ) ),
this, TQT_SLOT( slotOnlineStatusChanged( Kopete::Contact *, const Kopete::OnlineStatus &, const Kopete::OnlineStatus &) ) );
if ( c->metaContact() )
{
disconnect( c->metaContact(), TQT_SIGNAL( displayNameChanged( const TQString &, const TQString & ) ), this, TQT_SLOT( slotUpdateDisplayName() ) );
disconnect( c->metaContact(), TQT_SIGNAL( photoChanged() ), this, TQT_SIGNAL( photoChanged() ) );
}
else
disconnect( c, TQT_SIGNAL( propertyChanged( Kopete::Contact *, const TQString &, const TQVariant &, const TQVariant & ) ), this, TQT_SLOT( slotUpdateDisplayName() ) );
disconnect( c, TQT_SIGNAL( contactDestroyed( Kopete::Contact * ) ), this, TQT_SLOT( slotContactDestroyed( Kopete::Contact * ) ) );
slotUpdateDisplayName();
}
d->contactStatus.remove( c );
emit contactRemoved( c, reason, format, suppressNotification );
}
void Kopete::ChatSession::receivedTypingMsg( const Kopete::Contact *c, bool t )
{
emit remoteTyping( c, t );
}
void Kopete::ChatSession::receivedTypingMsg( const TQString &contactId, bool t )
{
for ( Kopete::Contact *it = d->mContactList.first(); it; it = d->mContactList.next() )
{
if ( it->contactId() == contactId )
{
receivedTypingMsg( it, t );
return;
}
}
}
void Kopete::ChatSession::typing( bool t )
{
emit myselfTyping( t );
}
void Kopete::ChatSession::receivedEventNotification( const TQString& notificationText)
{
emit eventNotification( notificationText );
}
void Kopete::ChatSession::setCanBeDeleted ( bool b )
{
d->mCanBeDeleted = b;
if (d->refcount < (b?1:0) && !d->view )
deleteLater();
}
void Kopete::ChatSession::ref ()
{
d->refcount++;
}
void Kopete::ChatSession::deref ()
{
d->refcount--;
if ( d->refcount < 1 && d->mCanBeDeleted && !d->view )
deleteLater();
}
KopeteView* Kopete::ChatSession::view( bool canCreate, const TQString &requestedPlugin )
{
if ( !d->view && canCreate )
{
d->view = Kopete::ChatSessionManager::self()->createView( this, requestedPlugin );
if ( d->view )
{
connect( d->view->mainWidget(), TQT_SIGNAL( closing( KopeteView * ) ), this, TQT_SLOT( slotViewDestroyed( ) ) );
}
else
{
KMessageBox::queuedMessageBox( Kopete::UI::Global::mainWidget(), KMessageBox::Error,
i18n( "<qt>An error has occurred while creating a new chat window. The chat window has not been created.</qt>" ),
i18n( "Error While Creating Chat Window" ) );
}
}
return d->view;
}
void Kopete::ChatSession::slotViewDestroyed()
{
d->view = 0L;
if ( d->mCanBeDeleted && d->refcount < 1)
deleteLater();
}
Kopete::Account *Kopete::ChatSession::account() const
{
return myself()->account();
}
void Kopete::ChatSession::slotContactDestroyed( Kopete::Contact *contact )
{
if(contact == myself())
deleteLater();
if( !contact || !d->mContactList.contains( contact ) )
return;
//This is a workaround to prevent crash if the contact get deleted.
// in the best case, we should ask the protocol to recreate a temporary contact.
// (remember: the contact may be deleted when the users removes it from the contactlist, or when closing kopete )
d->mContactList.remove( contact );
emit contactRemoved( contact, TQString() );
if ( d->mContactList.isEmpty() )
deleteLater();
}
bool Kopete::ChatSession::mayInvite() const
{
return d->mayInvite;
}
void Kopete::ChatSession::inviteContact(const TQString& )
{
//default implementation do nothing
}
void Kopete::ChatSession::setMayInvite( bool b )
{
d->mayInvite=b;
}
void Kopete::ChatSession::raiseView()
{
KopeteView *v=view(true, KopetePrefs::prefs()->interfacePreference() );
if(v)
v->raise(true);
}
#include "kopetechatsession.moc"
// vim: set noet ts=4 sts=4 sw=4:
|