summaryrefslogtreecommitdiffstats
path: root/kopete/libkopete/kopetemessage.cpp
blob: 566091e4209c93457480a3af724ecd517e262b0a (plain)
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
    kopetemessage.cpp  -  Base class for Kopete messages

    Copyright (c) 2002-2003 by Martijn Klingens       <klingens@kde.org>
    Copyright (c) 2002-2006 by Olivier Goffart        <ogoffart @ kde.org>

    Kopete    (c) 2002-2005 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 <stdlib.h>

#include <tqcolor.h>
#include <tqbuffer.h>
#include <tqimage.h>
#include <stylesheet.h>
#include <tqregexp.h>
#include <textcodec.h>
#include <kdebug.h>
#include <klocale.h>
#include <kiconloader.h>
#include <kstringhandler.h>
#include <kmdcodec.h>
#include <tqguardedptr.h>

#include "kopetemessage.h"
#include "kopetemetacontact.h"
#include "kopeteprotocol.h"
#include "kopetechatsession.h"
#include "kopeteprefs.h"
#include "kopetecontact.h"
#include "kopeteemoticons.h"


using namespace Kopete;

class Message::Private
	: public KShared
{
public:
	Private( const TQDateTime &timeStamp, const Contact *from, const ContactPtrList &to,
	         const TQString &subject, MessageDirection direction,
	         const TQString &requestedPlugin, MessageType type );

	TQGuardedPtr<const Contact> from;
	ContactPtrList to;
	ChatSession *manager;

	MessageDirection direction;
	MessageFormat format;
	MessageType type;
	TQString requestedPlugin;
	MessageImportance importance;
	bool bgOverride;
	bool fgOverride;
	bool rtfOverride;
	bool isRightToLeft;
	TQDateTime timeStamp;
	TQFont font;

	TQColor fgColor;
	TQColor bgColor;
	TQString body;
	TQString subject;
};

Message::Private::Private( const TQDateTime &timeStamp, const Contact *from,
		const ContactPtrList &to, const TQString &subject,
		MessageDirection direction, const TQString &requestedPlugin, MessageType type )
: from( from ), to( to ), manager( 0 ), direction( direction ), format( PlainText ), type( type ),
	requestedPlugin( requestedPlugin ), importance( (to.count() <= 1) ? Normal : Low ),
	bgOverride( false ), fgOverride( false ), rtfOverride( false ), isRightToLeft( false ),
	timeStamp( timeStamp ), body( TQString() ), subject( subject )
{
}

Message::Message()
: d( new Private( TQDateTime::currentDateTime(), 0L, TQPtrList<Contact>(), TQString(), Internal,
	TQString(), TypeNormal ) )
{
}

Message::Message( const Contact *fromKC, const TQPtrList<Contact> &toKC, const TQString &body,
		  MessageDirection direction, MessageFormat f, const TQString &requestedPlugin, MessageType type )
: d( new Private( TQDateTime::currentDateTime(), fromKC, toKC, TQString(), direction, requestedPlugin, type ) )
{
	doSetBody( body, f );
}

Message::Message( const Contact *fromKC, const Contact *toKC, const TQString &body,
		  MessageDirection direction, MessageFormat f, const TQString &requestedPlugin, MessageType type )
{
	TQPtrList<Contact> to;
	to.append(toKC);
	d = new Private( TQDateTime::currentDateTime(), fromKC, to, TQString(), direction, requestedPlugin, type );
	doSetBody( body, f );
}

Message::Message( const Contact *fromKC, const TQPtrList<Contact> &toKC, const TQString &body,
		  const TQString &subject, MessageDirection direction, MessageFormat f, const TQString &requestedPlugin, MessageType type )
    : d( new Private( TQDateTime::currentDateTime(), fromKC, toKC, subject, direction, requestedPlugin, type ) )
{
	doSetBody( body, f );
}

Message::Message( const TQDateTime &timeStamp, const Contact *fromKC, const TQPtrList<Contact> &toKC,
		  const TQString &body, MessageDirection direction, MessageFormat f, const TQString &requestedPlugin, MessageType type )
    : d( new Private( timeStamp, fromKC, toKC, TQString(), direction, requestedPlugin, type ) )
{
	doSetBody( body, f );
}


Message::Message( const TQDateTime &timeStamp, const Contact *fromKC, const TQPtrList<Contact> &toKC,
		  const TQString &body, const TQString &subject, MessageDirection direction, MessageFormat f, const TQString &requestedPlugin, MessageType type )
    : d( new Private( timeStamp, fromKC, toKC, subject, direction, requestedPlugin, type ) )
{
	doSetBody( body, f );
}

Kopete::Message::Message( const Message &other )
	: d(other.d)
{
}



Message& Message::operator=( const Message &other )
{
	d = other.d;
	return *this;
}

Message::~Message()
{
}

void Message::detach()
{
	// there is no detach in KSharedPtr :(
	if( d.count() == 1 )
		return;

	// Warning: this only works as long as the private object doesn't contain pointers to allocated objects.
	// The from contact for example is fine, but it's a shallow copy this way.
	d = new Private(*d);
}

void Message::setBgOverride( bool enabled )
{
	detach();
	d->bgOverride = enabled;
}

void Message::setFgOverride( bool enabled )
{
	detach();
	d->fgOverride = enabled;
}

void Message::setRtfOverride( bool enabled )
{
	detach();
	d->rtfOverride = enabled;
}

void Message::setFg( const TQColor &color )
{
	detach();
	d->fgColor=color;
}

void Message::setBg( const TQColor &color )
{
	detach();
	d->bgColor=color;
}

void Message::setFont( const TQFont &font )
{
	detach();
	d->font = font;
}

void Message::doSetBody( const TQString &_body, Message::MessageFormat f )
{
	TQString body = _body;

	//TODO: move that in ChatTextEditPart::contents
	if( f == RichText )
	{
		//This is coming from the RichTextEditor component.
		//Strip off the containing HTML document
		body.replace( TQRegExp( TQString::fromLatin1(".*<body[^>]*>(.*)</body>.*") ), TQString::fromLatin1("\\1") );

		//Strip <p> tags
		body.replace( TQString::fromLatin1("<p>"), TQString() );

		//Replace </p> with a <br/>
		body.replace( TQString::fromLatin1("</p>"), TQString::fromLatin1("<br/>") );

		//Remove trailing </br>
		if ( body.endsWith( TQString::fromLatin1("<br/>") ) )
			body.truncate( body.length() - 5 );
	
		body.remove( TQString::fromLatin1("\n") );
		body.replace( TQRegExp( TQString::fromLatin1( "\\s\\s" ) ), TQString::fromLatin1( " &nbsp;" ) );
	}
	/*
	else if( f == ParsedHTML )
	{
		kdWarning( 14000 ) << k_funcinfo << "using ParsedHTML which is internal! Message: '" <<
			body << "', Backtrace: " << kdBacktrace() << endl;
	}
	*/

	d->body = body;
	d->format = f;

	// unescaping is very expensive, do it only once and cache the result
	d->isRightToLeft = ( f & RichText ? unescape( d->body ).isRightToLeft() : d->body.isRightToLeft() );
}

void Message::setBody( const TQString &body, MessageFormat f )
{
	detach();

	doSetBody( body, f );
}

bool Message::isRightToLeft() const
{
	return d->isRightToLeft;
}

void Message::setImportance(Message::MessageImportance i)
{
	detach();
	d->importance = i;
}

TQString Message::unescape( const TQString &xml )
{
	TQString data = xml;

	// Remove linebreak and multiple spaces. First return nbsp's to normal spaces :)
	data.simplifyWhiteSpace();

	int pos;
	while ( ( pos = data.find( '<' ) ) != -1 )
	{
		int endPos = data.find( '>', pos + 1 );
		if( endPos == -1 )
			break;    // No more complete elements left

		// Take the part between < and >, and extract the element name from that
		int matchWidth = endPos - pos + 1;
		TQString match = data.mid( pos + 1, matchWidth - 2 ).simplifyWhiteSpace();
		int elemEndPos = match.find( ' ' );
		TQString elem = ( elemEndPos == -1 ? match.lower() : match.left( elemEndPos ).lower() );
		if ( elem == TQString::fromLatin1( "img" ) )
		{
			// Replace smileys with their original text'
			const TQString attrTitle  = TQString::fromLatin1( "title=\"" );
			int titlePos    = match.find( attrTitle, elemEndPos );
			int titleEndPos = match.find( '"',       titlePos + attrTitle.length() );
			if( titlePos == -1 || titleEndPos == -1 )
			{
				// Not a smiley but a normal <img>
				// Don't update pos, we restart at this position :)
				data.remove( pos, matchWidth );
			}
			else
			{
				TQString orig = match.mid( titlePos + attrTitle.length(),
					titleEndPos - titlePos - attrTitle.length() );
				data.replace( pos, matchWidth, orig );
				pos += orig.length();
			}
		}
		else if ( elem == TQString::fromLatin1( "/p" ) || elem == TQString::fromLatin1( "/div" ) ||
			elem == TQString::fromLatin1( "br" ) )
		{
			// Replace paragraph, div and line breaks with a newline
			data.replace( pos, matchWidth, '\n' );
			pos++;
		}
		else
		{
			// Remove all other elements entirely
			// Don't update pos, we restart at this position :)
			data.remove( pos, matchWidth );
		}
	}

	// Replace stuff starting with '&'
	data.replace( TQString::fromLatin1( "&gt;" ), TQString::fromLatin1( ">" ) );
	data.replace( TQString::fromLatin1( "&lt;" ), TQString::fromLatin1( "<" ) );
	data.replace( TQString::fromLatin1( "&quot;" ), TQString::fromLatin1( "\"" ) );
	data.replace( TQString::fromLatin1( "&nbsp;" ), TQString::fromLatin1( " " ) );
	data.replace( TQString::fromLatin1( "&amp;" ), TQString::fromLatin1( "&" ) );
	data.replace( TQString::fromLatin1( "&#160;" ), TQString::fromLatin1( " " ) );  //this one is used in jabber:  note, we should escape all &#xx;

	return data;
}

TQString Message::escape( const TQString &text )
{
	TQString html = TQStyleSheet::escape( text );
 	//Replace carriage returns inside the text
	html.replace( TQString::fromLatin1( "\n" ), TQString::fromLatin1( "<br />" ) );
	//Replace a tab with 4 spaces
	html.replace( TQString::fromLatin1( "\t" ), TQString::fromLatin1( "&nbsp;&nbsp;&nbsp;&nbsp;" ) );

	//Replace multiple spaces with &nbsp;
	//do not replace every space so we break the linebreak
	html.replace( TQRegExp( TQString::fromLatin1( "\\s\\s" ) ), TQString::fromLatin1( "&nbsp; " ) );

	return html;
}



TQString Message::plainBody() const
{
	TQString body=d->body;
	if( d->format & RichText )
	{
		body = unescape( body );
	}
	return body;
}

TQString Message::escapedBody() const
{
	TQString escapedBody=d->body;
//	kdDebug(14000) << k_funcinfo << escapedBody << " " << d->rtfOverride << endl;

	if( d->format & PlainText )
	{
		escapedBody=escape( escapedBody );
	}
	else if( d->format & RichText && d->rtfOverride)
	{
		//remove the rich text
		escapedBody = escape (unescape( escapedBody ) );
	}

	return escapedBody;
}

TQString Message::parsedBody() const
{
	//kdDebug(14000) << k_funcinfo << "messageformat: " << d->format << endl;

	if( d->format == ParsedHTML )
	{
		return d->body;
	}
	else
	{
		return Kopete::Emoticons::parseEmoticons(parseLinks(escapedBody(), RichText));
	}
}

static TQString makeRegExp( const char *pattern )
{
	const TQString urlChar = TQString::fromLatin1( "\\+\\-\\w\\./#@&;:=\\?~%_,\\!\\$\\*\\(\\)" );
	const TQString boundaryStart = TQString::fromLatin1( "(^|[^%1])(" ).arg( urlChar );
	const TQString boundaryEnd = TQString::fromLatin1( ")([^%1]|$)" ).arg( urlChar );

	return boundaryStart + TQString::fromLatin1(pattern) + boundaryEnd;
}

TQString Message::parseLinks( const TQString &message, MessageFormat format )
{
	if ( format == ParsedHTML )
		return message;

	if ( format & RichText )
	{
		// < in HTML *always* means start-of-tag
		TQStringList entries = TQStringList::split( TQChar('<'), message, true );

		TQStringList::Iterator it = entries.begin();

		// first one is different: it doesn't start with an HTML tag.
		if ( it != entries.end() )
		{
			*it = parseLinks( *it, PlainText );
			++it;
		}

		for ( ; it != entries.end(); ++it )
		{
			TQString curr = *it;
			// > in HTML means start-of-tag if and only if it's the first one after a <
			int tagclose = curr.find( TQChar('>') );
			// no >: the HTML is broken, but we can cope
			if ( tagclose == -1 )
				continue;
			TQString tag = curr.left( tagclose + 1 );
			TQString body = curr.mid( tagclose + 1 );
			*it = tag + parseLinks( body, PlainText );
		}
		return entries.join(TQString::fromLatin1("<"));
	}

	TQString result = message;

	// common subpatterns - may not contain matching parens!
	const TQString name = TQString::fromLatin1( "[\\w\\+\\-=_\\.]+" );
	const TQString userAndPassword = TQString::fromLatin1( "(?:%1(?::%1)?\\@)" ).arg( name );
	const TQString urlChar = TQString::fromLatin1( "\\+\\-\\w\\./#@&;:=\\?~%_,\\!\\$\\*\\(\\)" );
	const TQString urlSection = TQString::fromLatin1( "[%1]+" ).arg( urlChar );
	const TQString domain = TQString::fromLatin1( "[\\-\\w_]+(?:\\.[\\-\\w_]+)+" );

	//Replace http/https/ftp links:
	// Replace (stuff)://[user:password@](linkstuff) with a link
	result.replace(
		TQRegExp( makeRegExp("\\w+://%1?\\w%2").arg( userAndPassword, urlSection ) ),
		TQString::fromLatin1("\\1<a href=\"\\2\" title=\"\\2\">\\2</a>\\3" ) );

	// Replace www.X.Y(linkstuff) with a http: link
	result.replace(
		TQRegExp( makeRegExp("%1?www\\.%2%3").arg( userAndPassword, domain, urlSection ) ),
		TQString::fromLatin1("\\1<a href=\"http://\\2\" title=\"http://\\2\">\\2</a>\\3" ) );

	//Replace Email Links
	// Replace user@domain with a mailto: link
	result.replace(
		TQRegExp( makeRegExp("%1@%2").arg( name, domain ) ),
		TQString::fromLatin1("\\1<a href=\"mailto:\\2\" title=\"mailto:\\2\">\\2</a>\\3") );

	//Workaround for Bug 85061: Highlighted URLs adds a ' ' after the URL itself
	// the trailing  &nbsp; is included in the url.
	result.replace( TQRegExp( TQString::fromLatin1("(<a href=\"[^\"]+)(&nbsp;)(\")")  ) , TQString::fromLatin1("\\1\\3") );

	return result;
}



TQDateTime Message::timestamp() const
{
	return d->timeStamp;
}

const Contact *Message::from() const
{
	return d->from;
}

TQPtrList<Contact> Message::to() const
{
	return d->to;
}

Message::MessageType Message::type() const
{
	return d->type;
}

TQString Message::requestedPlugin() const
{
	return d->requestedPlugin;
}

TQColor Message::fg() const
{
	return d->fgColor;
}

TQColor Message::bg() const
{
	return d->bgColor;
}

TQFont Message::font() const
{
	//TQDomElement bodyNode = d->xmlDoc.elementsByTagName( TQString::fromLatin1("body") ).item(0).toElement();
	//return TQFont( bodyNode.attribute( TQString::fromLatin1("font") ), bodyNode.attribute( TQString::fromLatin1("fontsize") ).toInt() );
	return d->font;
}

TQString Message::subject() const
{
	return d->subject;
}

Message::MessageFormat Message::format() const
{
	return d->format;
}

Message::MessageDirection Message::direction() const
{
	return d->direction;
}

Message::MessageImportance Message::importance() const
{
	return d->importance;
}

ChatSession *Message::manager() const
{
	return d->manager;
}

void Message::setManager(ChatSession *kmm)
{
	detach();
	d->manager=kmm;
}

TQString Message::getHtmlStyleAttribute() const
{
	TQString styleAttribute;
	
	styleAttribute = TQString::fromUtf8("style=\"");

	// Affect foreground(color) and background color to message.
	if( !d->fgOverride && d->fgColor.isValid() )
	{
		styleAttribute += TQString::fromUtf8("color: %1; ").arg(d->fgColor.name());
	}
	if( !d->bgOverride && d->bgColor.isValid() )
	{
		styleAttribute += TQString::fromUtf8("background-color: %1; ").arg(d->bgColor.name());
	}
	
	// Affect font parameters.
	if( !d->rtfOverride && d->font!=TQFont() )
	{
		TQString fontstr;
		if(!d->font.family().isNull())
			fontstr+=TQString::fromLatin1("font-family: ")+d->font.family()+TQString::fromLatin1("; ");
		if(d->font.italic())
			fontstr+=TQString::fromLatin1("font-style: italic; ");
		if(d->font.strikeOut())
			fontstr+=TQString::fromLatin1("text-decoration: line-through; ");
		if(d->font.underline())
			fontstr+=TQString::fromLatin1("text-decoration: underline; ");
		if(d->font.bold())
			fontstr+=TQString::fromLatin1("font-weight: bold;");

		styleAttribute += fontstr;
	}

	styleAttribute += TQString::fromUtf8("\"");

	return styleAttribute;
}

// KDE4: Move that to a utils class/namespace
TQString Message::decodeString( const TQCString &message, const TQTextCodec *providedCodec, bool *success )
{
	/*
	Note to everyone. This function is not the most efficient, that is for sure.
	However, it *is* the only way we can be guarenteed that a given string is
	decoded properly.
	*/

	if( success )
		*success = true;

	// Avoid heavy codec tests on empty message.
	if( message.isEmpty() )
            return TQString::fromAscii( message );

	//Check first 128 chars
	int charsToCheck = message.length();
	charsToCheck = 128 > charsToCheck ? charsToCheck : 128;

	//They are providing a possible codec. Check if it is valid
	if( providedCodec && providedCodec->heuristicContentMatch( message, charsToCheck ) >= 0 )
	{
		//All chars decodable.
		return providedCodec->toUnicode( message );
	}

	//Check if it is UTF
	if( KStringHandler::isUtf8(message) )
	{
		//We have a UTF string almost for sure. At least we know it will be decoded.
		return TQString::fromUtf8( message );
	}

	//Try codecForContent - exact match
	TQTextCodec *testCodec = TQTextCodec::codecForContent(message, charsToCheck);
	if( testCodec && testCodec->heuristicContentMatch( message, charsToCheck ) >= 0 )
	{
		//All chars decodable.
		return testCodec->toUnicode( message );
	}

	kdWarning(14000) << k_funcinfo << "Unable to decode string using provided codec(s), taking best guesses!" << endl;
	if( success )
		*success = false;

	//We don't have any clues here.

	//Try local codec
	testCodec = TQTextCodec::codecForLocale();
	if( testCodec && testCodec->heuristicContentMatch( message, charsToCheck ) >= 0 )
	{
		//All chars decodable.
		kdDebug(14000) << k_funcinfo << "Using locale's codec" << endl;
		return testCodec->toUnicode( message );
	}

	//Try latin1 codec
	testCodec = TQTextCodec::codecForMib(4);
	if( testCodec && testCodec->heuristicContentMatch( message, charsToCheck ) >= 0 )
	{
		//All chars decodable.
		kdDebug(14000) << k_funcinfo << "Using latin1" << endl;
		return testCodec->toUnicode( message );
	}

	kdDebug(14000) << k_funcinfo << "Using latin1 and cleaning string" << endl;
	//No codec decoded. Just decode latin1, and clean out any junk.
	TQString result = TQString::fromLatin1( message );
	const uint length = message.length();
	for( uint i = 0; i < length; ++i )
	{
		if( !result[i].isPrint() )
			result[i] = '?';
	}

	return result;
}