summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/oscarmessage.cpp
blob: f4f512d2fc313496fe707af6be4d99b903cbaffe (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
/*
    Kopete Oscar Protocol
    oscarmessage.cpp - Oscar Message Object

    Copyright (c) 2005 Matt Rogers <[email protected]>
    Copyright (c) 2005 Conrad Hoffmann <[email protected]>

    Kopete (c) 2002-2005 by the Kopete developers <[email protected]>

    *************************************************************************
    *                                                                       *
    * 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 "oscarmessage.h"

#include <qdeepcopy.h>
#include <qtextcodec.h>


Oscar::Message::Message()
: m_channel( -1 ),
  m_properties( -1 ),
  m_messageType( 0 ),
  m_protocolVersion( 0 ),
  m_channel2Counter( 0 ),
  m_encoding( UserDefined )
{
}

Oscar::Message::Message( Encoding messageEncoding, const QByteArray& messageText, int channel, int properties, QDateTime timestamp )
: m_channel( channel ),
  m_properties( properties ),
  m_messageType( 0 ),
  m_protocolVersion( 0 ),
  m_channel2Counter( 0 ),
  m_textArray( messageText ),
  m_timestamp( timestamp ),
  m_encoding( messageEncoding )
{
}

Oscar::Message::Message( Encoding messageEncoding, const QCString& messageText, int channel, int properties, QDateTime timestamp )
: m_channel( channel ),
  m_properties( properties ),
  m_messageType( 0 ),
  m_protocolVersion( 0 ),
  m_channel2Counter( 0 ),
  m_timestamp( timestamp ),
  m_encoding( messageEncoding )
{
	setTextArray( messageText );
}

Oscar::Message::Message( Encoding messageEncoding, const QString& messageText, int channel, int properties, QDateTime timestamp, QTextCodec* codec )
: m_channel( channel ),
  m_properties( properties ),
  m_messageType( 0 ),
  m_protocolVersion( 0 ),
  m_channel2Counter( 0 ),
  m_timestamp( timestamp )
{
	setText( messageEncoding, messageText, codec );
}

QString Oscar::Message::sender() const
{
	return m_sender;
}

void Oscar::Message::setSender( const QString& sender  )
{
	m_sender = sender;
}

QString Oscar::Message::receiver() const
{
	return m_receiver;
}

void Oscar::Message::setReceiver( const QString& receiver )
{
	m_receiver = receiver;
}

QByteArray Oscar::Message::textArray() const
{
    return m_textArray;
}

QString Oscar::Message::text( QTextCodec *codec ) const
{
	switch ( m_encoding )
	{
	case Oscar::Message::UserDefined:
		return codec->toUnicode( m_textArray );
	case Oscar::Message::UTF8:
		return QString::fromUtf8( m_textArray.data(), m_textArray.size() );
	case Oscar::Message::UCS2:
	{
		uint len = m_textArray.size() / 2;
		QString result;
		result.setLength( len );
		QByteArray::ConstIterator p = m_textArray.begin();
		for ( uint i = 0; i < len; i++)
		{
			char row = *p++;
			char cell = *p++;
			result[i] = QChar( cell, row );
		}
		//check if last character isn't null
		if ( result[len-1].isNull() )
			result.setLength( len - 1 );

		return result;
	}
	default:
		break; // Should never happen.
	}
	return QString::null;
	//FIXME: warn at least with kdWarning if an unrecognised encoding style was seen.
}

void Oscar::Message::setText( Oscar::Message::Encoding newEncoding, const QString& newText, QTextCodec* codec )
{
	uint len;
	switch ( newEncoding )
	{
	case Oscar::Message::UserDefined:
		// Oscar::Message::setTextArray( const QCString& )
		// strips trailing null byte automatically.
		setTextArray( codec->fromUnicode( newText ) );
		break;
	case Oscar::Message::UTF8:
		// Oscar::Message::setTextArray( const QCString& )
		// strips trailing null byte automatically.
		setTextArray( newText.utf8() );
		break;
	case Oscar::Message::UCS2:
	{
		len = newText.length();
		m_textArray.resize( len * 2 );
		QByteArray::Iterator p = m_textArray.begin();
		for ( uint i = 0; i < len; i++)
		{
			*p++ = newText[i].row();
			*p++ = newText[i].cell();
		}
		break;
	}
	default:
		break; // Should never happen.
	}
	m_encoding = newEncoding;
}

void Oscar::Message::setTextArray( const QByteArray& newTextArray )
{
	m_textArray.duplicate( newTextArray );
}

void Oscar::Message::setTextArray( const QCString& newTextArray )
{
	m_textArray.duplicate( newTextArray );
	uint len = m_textArray.size();
	if ( len > 0 )
	{
		--len;
		if ( m_textArray[len] == '\0' )
		{
			// Strip trailing null byte.
			m_textArray.resize( len );
		}
	}
}

int Oscar::Message::properties() const
{
	return m_properties;
}

void Oscar::Message::addProperty( int prop )
{
	if ( m_properties == -1  )
		m_properties = 0;

	m_properties = m_properties | prop;
}

bool Oscar::Message::hasProperty( int prop ) const
{
	if ( m_properties == -1 )
		return false;
	if ( ( m_properties & prop ) == 0 )
		return false;
	else
		return true;
}

int Oscar::Message::type() const
{
	return m_channel;
}

void Oscar::Message::setType( int newType )
{
	m_channel = newType;
}

QDateTime Oscar::Message::timestamp() const
{
	return m_timestamp;
}

void Oscar::Message::setTimestamp( QDateTime ts )
{
	m_timestamp = ts;
}

QByteArray Oscar::Message::icbmCookie() const
{
	return m_icbmCookie;
}

void Oscar::Message::setIcbmCookie( const QByteArray& cookie )
{
	m_icbmCookie.duplicate( cookie );
}

int Oscar::Message::protocolVersion() const
{
	return m_protocolVersion;
}

void Oscar::Message::setProtocolVersion( int version )
{
	m_protocolVersion = version;
}

int Oscar::Message::channel2Counter() const
{
	return m_channel2Counter;
}

void Oscar::Message::setChannel2Counter( int value )
{
	m_channel2Counter = value;
}

int Oscar::Message::messageType() const
{
	return m_messageType;
}

void Oscar::Message::setMessageType( int type )
{
	m_messageType = type;
}

Oscar::WORD Oscar::Message::exchange() const
{
    return m_exchange;
}

void Oscar::Message::setExchange( Oscar::WORD exchange )
{
    m_exchange = exchange;
}

QString Oscar::Message::chatRoom() const
{
    return m_chatRoom;
}

void Oscar::Message::setChatRoom( const QString& room )
{
    m_chatRoom = room;
}

Oscar::Message::Encoding Oscar::Message::encoding() const
{
	return m_encoding;
}

void Oscar::Message::setEncoding( Oscar::Message::Encoding newEncoding )
{
	m_encoding = newEncoding;
}

Oscar::Message::operator bool() const
{
	return m_channel != -1 && m_properties != -1;
}

//kate: indent-mode csands; auto-insert-doxygen on; tab-width 4;