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
|
// -*- Mode: c++-mode; c-basic-offset: 2; indent-tabs-mode: t; tab-width: 2; -*-
//
// Copyright (C) 2004 Grzegorz Jaskiewicz <gj at pointblue.com.pl>
//
// gadurichtextformat.cpp
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02111-1307, USA.
#include <knotifyclient.h>
#include <kdebug.h>
#include <kopetemessage.h>
#include "gadurichtextformat.h"
#include "gadusession.h"
#include <qstring.h>
#include <qregexp.h>
GaduRichTextFormat::GaduRichTextFormat()
{
}
GaduRichTextFormat::~GaduRichTextFormat()
{
}
QString
GaduRichTextFormat::convertToHtml( const QString& msg, unsigned int formats, void* formatStructure)
{
QString tmp, nb;
gg_msg_richtext_format *format;
char *pointer = (char*) formatStructure;
unsigned int i,j;
int r, g, b;
r = g = b = 0;
bool opened = false;
if ( formatStructure == NULL || formats == 0 ) {
tmp = msg;
escapeBody( tmp );
return tmp;
}
for ( i = 0, j = 0 ; i < formats ; ) {
format = (gg_msg_richtext_format*) pointer;
unsigned int position = format->position;
char font = format->font;
QString style;
if ( position < j || position > msg.length() ) {
break;
}
if ( font & GG_FONT_IMAGE ) {
i += sizeof( gg_msg_richtext_image );
pointer += sizeof( gg_msg_richtext_image );
tmp += "<b>[this should be a picture, not yet implemented]</b>";
}
else {
nb = msg.mid( j, position - j );
tmp += escapeBody( nb );
j = position;
// add message bit between formating
if ( opened ) {
tmp += formatClosingTag("span");
opened = false;
}
// set font attributes
if ( font & GG_FONT_BOLD ) {
style += (" font-weight:bold; ");
}
if ( font & GG_FONT_ITALIC ) {
style += (" font-style:italic; ");
}
if ( font & GG_FONT_UNDERLINE ) {
style += (" text-decoration:underline; ");
}
// add color
if ( font & GG_FONT_COLOR ) {
pointer += sizeof( gg_msg_richtext_format );
i += sizeof( gg_msg_richtext_format );
gg_msg_richtext_color *color = (gg_msg_richtext_color*)( pointer );
r = (int)color->red;
g = (int)color->green;
b = (int)color->blue;
}
style += QString(" color: rgb( %1, %2, %3 ); ").arg( r ).arg( g ).arg( b );
tmp += formatOpeningTag( QString::fromLatin1("span"), QString::fromLatin1("style=\"%1\"").arg( style ) );
opened = true;
}
// advance to next structure in row
pointer += sizeof( gg_msg_richtext_format );
i += sizeof( gg_msg_richtext_format );
}
nb = msg.mid( j, msg.length() );
tmp += escapeBody( nb );
if ( opened ) {
tmp += formatClosingTag("span");
}
return tmp;
}
QString
GaduRichTextFormat::formatOpeningTag( const QString& tag, const QString& attributes )
{
QString res = "<" + tag;
if(!attributes.isEmpty())
res.append(" " + attributes);
return res + ">";
}
QString
GaduRichTextFormat::formatClosingTag( const QString& tag )
{
return "</" + tag + ">";
}
// the initial idea stolen from IRC plugin
KGaduMessage*
GaduRichTextFormat::convertToGaduMessage( const Kopete::Message& message )
{
QString htmlString = message.escapedBody();
KGaduMessage* output = new KGaduMessage;
rtcs.blue = rtcs.green = rtcs.red = 0;
color = QColor();
int position = 0;
rtf.resize( sizeof( gg_msg_richtext) );
output->rtf.resize(0);
// test first if there is any HTML formating in it
if( htmlString.find( QString::fromLatin1("</span") ) > -1 ) {
QRegExp findTags( QString::fromLatin1("<span style=\"(.*)\">(.*)</span>") );
findTags.setMinimal( true );
int pos = 0;
int lastpos = 0;
while ( pos >= 0 ){
pos = findTags.search( htmlString );
rtfs.font = 0;
if ( pos != lastpos ) {
QString tmp;
if ( pos < 0 ) {
tmp = htmlString.mid( lastpos );
}
else {
tmp = htmlString.mid( lastpos, pos - lastpos );
}
if ( !tmp.isEmpty() ) {
color.setRgb( 0, 0, 0 );
if ( insertRtf( position ) == false ) {
delete output;
return NULL;
}
tmp = unescapeGaduMessage( tmp );
output->message += tmp;
position += tmp.length();
}
}
if ( pos > -1 ) {
QString styleHTML = findTags.cap(1);
QString replacement = findTags.cap(2);
QStringList styleAttrs = QStringList::split( ';', styleHTML );
rtfs.font = 0;
lastpos = pos + replacement.length();
for( QStringList::Iterator attrPair = styleAttrs.begin(); attrPair != styleAttrs.end(); ++attrPair ) {
QString attribute = (*attrPair).section(':',0,0);
QString value = (*attrPair).section(':',1);
parseAttributes( attribute, value );
}
if ( insertRtf( position ) == false ) {
delete output;
return NULL;
}
QString rep = QString("<span style=\"%1\">%2</span>" ).arg( styleHTML ).arg( replacement );
htmlString.replace( findTags.pos( 0 ), rep.length(), replacement );
replacement = unescapeGaduMessage( replacement );
output->message += replacement;
position += replacement.length();
}
}
output->rtf = rtf;
// this is sick, but that's the way libgadu is designed
// here I am adding network header !, should sit in libgadu IMO
header = (gg_msg_richtext*) output->rtf.data();
header->length = output->rtf.size() - sizeof( gg_msg_richtext );
header->flag = 2;
}
else {
output->message = message.escapedBody();
output->message = unescapeGaduMessage( output->message );
}
return output;
}
void
GaduRichTextFormat::parseAttributes( const QString attribute, const QString value )
{
if( attribute == QString::fromLatin1("color") ) {
color.setNamedColor( value );
}
if( attribute == QString::fromLatin1("font-weight") && value == QString::fromLatin1("600") ) {
rtfs.font |= GG_FONT_BOLD;
}
if( attribute == QString::fromLatin1("text-decoration") && value == QString::fromLatin1("underline") ) {
rtfs.font |= GG_FONT_UNDERLINE ;
}
if( attribute == QString::fromLatin1("font-style") && value == QString::fromLatin1("italic") ) {
rtfs.font |= GG_FONT_ITALIC;
}
}
QString
GaduRichTextFormat::unescapeGaduMessage( QString& ns )
{
QString s;
s = Kopete::Message::unescape( ns );
s.replace( QString::fromAscii( "\n" ), QString::fromAscii( "\r\n" ) );
return s;
}
bool
GaduRichTextFormat::insertRtf( uint position)
{
if ( color != QColor( rtcs.red, rtcs.green, rtcs.blue ) ) {
rtcs.red = color.red();
rtcs.green = color.green();
rtcs.blue = color.blue();
rtfs.font |= GG_FONT_COLOR;
}
if ( rtfs.font ) {
// append font description
rtfs.position = position;
uint csize = rtf.size();
if ( rtf.resize( csize + sizeof( gg_msg_richtext_format ) ) == FALSE ) {
return false;
};
memcpy( rtf.data() + csize, &rtfs, sizeof( rtfs ) );
// append color description, if color has changed
if ( rtfs.font & GG_FONT_COLOR ) {
csize = rtf.size();
if ( rtf.resize( csize + sizeof( gg_msg_richtext_color ) ) == FALSE ) {
return false;
};
memcpy( rtf.data() + csize, &rtcs, sizeof( rtcs ) );
}
}
return true;
}
QString
GaduRichTextFormat::escapeBody( QString& input )
{
input.replace( '<', QString::fromLatin1("<") );
input.replace( '>', QString::fromLatin1(">") );
input.replace( '\n', QString::fromLatin1( "<br />" ) );
input.replace( '\t', QString::fromLatin1( " " ) );
input.replace( QRegExp( QString::fromLatin1( "\\s\\s" ) ), QString::fromLatin1( " " ) );
return input;
}
|