2
This file is part of libqobex.
4
Copyright (c) 2003 Mathias Froehlich <Mathias.Froehlich@web.de>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with this library; see the file COPYING.LIB. If not, write to
18
the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
19
Boston, MA 02110-1301, USA.
26
#include <qdatetime.h>
27
#include <qtextstream.h>
29
#include "qobexheader.h"
30
#include "qobexauth.h"
31
#include "qobexapparam.h"
36
#define myDebug(a) qDebug a
38
#define myDebug(a) (void)0
41
QObexHeader::QObexHeader( const Q_UINT8 id ) : mId( id ) {
42
myDebug(( "QObexHeader::QObexHeader( %s )", stringHeaderId().ascii() ));
45
QObexHeader::QObexHeader( const Q_UINT8 id, const QByteArray& data )
47
myDebug(( "QObexHeader::QObexHeader( %s, const QByteArray& )", stringHeaderId().ascii() ));
48
if ( dataType() == QObexHeader::Byte ) {
49
Q_ASSERT( data.size() == 1 );
50
} else if ( dataType() == QObexHeader::DWord ) {
51
Q_ASSERT( data.size() == 4 );
56
QObexHeader::QObexHeader(const Q_UINT8 id, const QString& data)
58
myDebug(( "QObexHeader::QObexHeader( %s, const QString& )", stringHeaderId().ascii() ));
59
if ( dataType() == QObexHeader::Unicode ) {
60
// Zero terminated unicode string in obex(network) byteorder
61
if ( !data.isNull() ) {
62
const Q_ULONG l = data.length();
63
QObexArray tmp( 2 + 2*l );
64
for ( Q_ULONG i = 0; i < l; ++i )
65
tmp.qchar( 2*i, data[i] );
69
} else if ( dataType() == QObexHeader::ByteSequence ) {
70
// The Type header should be null terminated.
71
if (id == QObexHeader::Type) {
72
mData.duplicate( data.latin1(), data.length()+1 );
74
mData.duplicate( data.latin1(), data.length());
76
} else if ( dataType() == QObexHeader::Byte ) {
77
Q_ASSERT( data.length() != 1 );
79
mData[0] = data[0].latin1();
84
QObexHeader::QObexHeader(const Q_UINT8 id, const Q_UINT32 data) : mId(id) {
85
myDebug(( "QObexHeader::QObexHeader( %s, %ud )", stringHeaderId().ascii(), data ));
86
if ( dataType() == QObexHeader::Byte ) {
88
mData.uint8( 0, data );
89
} else if ( dataType() == QObexHeader::DWord ) {
91
mData.uint32( 0, data );
96
QObexHeader::QObexHeader(const Q_UINT8 id, const Q_UINT8 data) : mId(id) {
97
myDebug(( "QObexHeader::QObexHeader( %s, %ud )", stringHeaderId().ascii(), data ));
98
if ( dataType() == QObexHeader::Byte ) {
100
mData.uint8( 0, data );
101
} else if ( dataType() == QObexHeader::DWord ) {
103
mData.uint32( 0, data );
108
QObexHeader::QObexHeader(const Q_UINT8 id, const QDateTime& date) : mId(id) {
109
myDebug(( "QObexHeader::QObexHeader( %s, const QDateTime& )", stringHeaderId().ascii() ));
110
if ( headerId() == QObexHeader::Time ) {
111
QString str = date.toString( Qt::ISODate );
112
str.replace( "-", "" );
113
str.replace( ":", "" );
114
mData.duplicate( str.latin1(), str.length() );
115
} else if ( headerId() == QObexHeader::TimeCompat ) {
116
Q_UINT32 tt = date.toTime_t();
118
mData.uint32( 0, tt );
124
QObexHeader::length() const {
125
myDebug(( "QObexHeader::length() const" ));
126
if ( dataType() == QObexHeader::Byte )
128
else if ( dataType() == QObexHeader::DWord )
131
return 3 + mData.size();
134
QString QObexHeader::stringHeaderId() const {
149
return "Description";
161
return "ConnectionId";
163
return "AppParameters";
165
return "AuthChallenge";
167
return "AuthResponse";
173
return "ObjectClass";
175
return "SessParameters";
177
return "SessSequence";
184
QObexHeader::stringData() const {
185
myDebug(( "QObexHeader::stringData() const: id is %s", stringHeaderId().ascii() ));
186
if ( headerId() == QObexHeader::Invalid )
187
return QString::null;
189
if ( dataType() == QObexHeader::Unicode ) {
190
if ( 0 < mData.size() ) {
192
Q_ULONG l = mData.size()/2 - 1;
193
for ( Q_ULONG i = 0; i < l; ++i )
194
ret[i] = mData.qchar( 2*i );
197
return QString::null;
198
} else if ( dataType() == QObexHeader::ByteSequence ) {
199
if ( 0 < mData.size() )
200
return QString::fromLatin1( mData.data(), mData.size() );
202
return QString::null;
203
} else if ( dataType() == QObexHeader::DWord ) {
204
return QString::number( mData.uint32( 0 ) );
206
return QString::number( mData.uint8( 0 ) );
212
QObexHeader::uint32Data() const {
213
myDebug(( "QObexHeader::uint32Data() const: id is %s", stringHeaderId().ascii() ));
214
if ( headerId() == QObexHeader::Invalid )
217
if ( dataType() == QObexHeader::DWord )
218
return mData.uint32( 0 );
219
else if ( dataType() == QObexHeader::Byte )
220
return mData.uint8( 0 );
229
QObexHeader::uint8Data() const {
230
myDebug(( "QObexHeader::uint8Data() const: id is %s", stringHeaderId().ascii() ));
231
if ( headerId() == QObexHeader::Invalid )
234
if ( dataType() == QObexHeader::DWord )
235
return mData.uint32( 0 );
236
else if ( dataType() == QObexHeader::Byte )
237
return mData.uint8( 0 );
245
QDateTime QObexHeader::timeData() const {
246
myDebug(( "QObexHeader::timeData() const: id is %s", stringHeaderId().ascii() ));
248
if ( headerId() == QObexHeader::Time ) {
249
QString str = stringData();
254
ret.fromString( str, Qt::ISODate );
255
} else if ( headerId() == QObexHeader::TimeCompat )
256
ret.setTime_t( uint32Data() );
261
QByteArray QObexHeader::coreHeader() const {
262
myDebug(( "QObexHeader::coreHeader() const: id is %s", stringHeaderId().ascii() ));
263
if ( dataType() == QObexHeader::Byte ) {
266
ret.uint8( 1, mData.at( 0 ) );
268
} else if ( dataType() == QObexHeader::DWord ) {
271
::memcpy( ret.data()+1, mData.data(), 4 );
276
ret.uint16( 1, 3 + mData.size() );
281
QString QObexHeader::toString( unsigned indent, bool fullContent ) const {
283
indentStr.fill( QChar( ' ' ), indent );
285
QTextStream stream( &str, IO_WriteOnly );
287
stream << indentStr << stringHeaderId();
288
if ( dataType() == QObexHeader::ByteSequence ) {
289
stream << " (Byte Sequence)\n";
290
if ( headerId() == AppParameters ) {
291
QObexApparam ap( mData );
292
stream << ap.toString( indent + 3 );
293
} else if ( headerId() == AuthChallenge ) {
294
QObexAuthDigestChallenge ch( mData );
295
stream << ch.toString( indent + 3 );
296
} else if ( headerId() == AuthResponse ) {
297
QObexAuthDigestResponse ar( mData );
298
stream << ar.toString( indent + 3 );
301
Q_ULONG len = mData.size();
302
if ( ( headerId() == Body || headerId() == BodyEnd ) && !fullContent ) {
303
// If we have a body header and we should not print the full content
304
// clip the display size to a few chars.
305
if ( 80 - (indent + 3 + 7 + 8) < len )
306
len = 80 - (indent + 3 + 7 + 8);
310
stream << indentStr << "<ascii>";
311
for ( Q_ULONG i = 0; i < len; ++i ) {
312
Q_UINT8 c = mData[i];
314
stream << QChar( c );
315
else if ( c == '\r' )
317
else if ( c == '\n' )
322
if ( len == mData.size() )
323
stream << "</ascii>\n";
327
if ( ( headerId() == Body || headerId() == BodyEnd ) && !fullContent ) {
328
// If we have a body header and we should not print the full content
329
// clip the display size to a few chars.
330
if ( 80 - (indent + 3 + 5 + 6) < 4*len )
331
len = ( 80 - (indent + 3 + 5 + 6) )/4;
333
stream << indentStr << "<hex>";
334
for ( Q_ULONG i = 0; i < len; ++i )
335
stream << QString().sprintf("<%02X>", (unsigned char)mData[i] );
336
if ( len == mData.size() )
337
stream << "</hex>\n";
341
} else if ( dataType() == QObexHeader::Unicode ) {
342
if ( dataType() == QObexHeader::Unicode )
343
stream << " (Unicode)\n";
344
stream << indentStr << " \"" << stringData() << "\"\n";
346
stream << (dataType() == QObexHeader::Byte ? " (UINT8)\n" : " (UINT32)\n");
347
// stringData does "the right thing" also for uint values
348
stream << indentStr << " " << stringData() << "\n";