~ubuntu-branches/ubuntu/intrepid/kdebluetooth/intrepid-proposed

« back to all changes in this revision

Viewing changes to kdebluetooth/libqobex/qobex/qobexheader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2008-08-07 09:49:47 UTC
  • mto: This revision was merged to the branch mainline in revision 56.
  • Revision ID: james.westby@ubuntu.com-20080807094947-pj6q3uxwuv7l844q
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of libqobex.
3
 
 
4
 
    Copyright (c) 2003 Mathias Froehlich <Mathias.Froehlich@web.de>
5
 
 
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.
10
 
 
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.
15
 
 
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.
20
 
*/
21
 
 
22
 
#include <ctype.h>
23
 
 
24
 
#include <qstring.h>
25
 
#include <qcstring.h>
26
 
#include <qdatetime.h>
27
 
#include <qtextstream.h>
28
 
 
29
 
#include "qobexheader.h"
30
 
#include "qobexauth.h"
31
 
#include "qobexapparam.h"
32
 
 
33
 
#undef DEBUG
34
 
// #define DEBUG
35
 
#ifdef DEBUG
36
 
#define myDebug(a) qDebug a
37
 
#else
38
 
#define myDebug(a) (void)0
39
 
#endif
40
 
 
41
 
QObexHeader::QObexHeader( const Q_UINT8 id ) : mId( id ) {
42
 
  myDebug(( "QObexHeader::QObexHeader( %s )", stringHeaderId().ascii() ));
43
 
}
44
 
 
45
 
QObexHeader::QObexHeader( const Q_UINT8 id, const QByteArray& data )
46
 
  : mId( id ) {
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 );
52
 
  }
53
 
  mData = data;
54
 
}
55
 
 
56
 
QObexHeader::QObexHeader(const Q_UINT8 id, const QString& data)
57
 
  : mId(id) {
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] );
66
 
      tmp.qchar( 2*l, 0 );
67
 
      mData = tmp;
68
 
    }
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 );
73
 
    } else {
74
 
      mData.duplicate( data.latin1(), data.length());
75
 
    }
76
 
  } else if ( dataType() == QObexHeader::Byte ) {
77
 
    Q_ASSERT( data.length() != 1 );
78
 
    mData.resize( 1 );
79
 
    mData[0] = data[0].latin1();
80
 
  } else
81
 
    Q_ASSERT( false );
82
 
}
83
 
 
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 ) {
87
 
    mData.resize( 1 );
88
 
    mData.uint8( 0, data );
89
 
  } else if ( dataType() == QObexHeader::DWord ) {
90
 
    mData.resize( 4 );
91
 
    mData.uint32( 0, data );
92
 
  } else
93
 
    Q_ASSERT( false );
94
 
}
95
 
 
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 ) {
99
 
    mData.resize( 1 );
100
 
    mData.uint8( 0, data );
101
 
  } else if ( dataType() == QObexHeader::DWord ) {
102
 
    mData.resize( 4 );
103
 
    mData.uint32( 0, data );
104
 
  } else
105
 
    Q_ASSERT( false );
106
 
}
107
 
 
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();
117
 
    mData.resize( 4 );
118
 
    mData.uint32( 0, tt );
119
 
  } else
120
 
    Q_ASSERT( false );
121
 
}
122
 
 
123
 
Q_UINT16
124
 
QObexHeader::length() const {
125
 
  myDebug(( "QObexHeader::length() const" ));
126
 
  if ( dataType() == QObexHeader::Byte )
127
 
    return 2;
128
 
  else if ( dataType() == QObexHeader::DWord )
129
 
    return 5;
130
 
  else
131
 
    return 3 + mData.size();
132
 
}
133
 
 
134
 
QString QObexHeader::stringHeaderId() const {
135
 
  switch ( mId ) {
136
 
  case Count:
137
 
    return "Count";
138
 
  case Name:
139
 
    return "Name";
140
 
  case Type:
141
 
    return "Type";
142
 
  case Length:
143
 
    return "Length";
144
 
  case Time:
145
 
    return "Time";
146
 
  case TimeCompat:
147
 
    return "TimeCompat";
148
 
  case Description:
149
 
    return "Description";
150
 
  case Target:
151
 
    return "Target";
152
 
  case HTTP:
153
 
    return "HTTP";
154
 
  case Body:
155
 
    return "Body";
156
 
  case BodyEnd:
157
 
    return "BodyEnd";
158
 
  case Who:
159
 
    return "Who";
160
 
  case ConnectionId:
161
 
    return "ConnectionId";
162
 
  case AppParameters:
163
 
    return "AppParameters";
164
 
  case AuthChallenge:
165
 
    return "AuthChallenge";
166
 
  case AuthResponse:
167
 
    return "AuthResponse";
168
 
  case CreatorId:
169
 
    return "CreatorId";
170
 
  case WanUUID:
171
 
    return "WanUUID";
172
 
  case ObjectClass:
173
 
    return "ObjectClass";
174
 
  case SessParameters:
175
 
    return "SessParameters";
176
 
  case SessSequence:
177
 
    return "SessSequence";
178
 
  default:
179
 
    return "Invalid";
180
 
  }
181
 
}
182
 
 
183
 
QString
184
 
QObexHeader::stringData() const {
185
 
  myDebug(( "QObexHeader::stringData() const: id is %s", stringHeaderId().ascii() ));
186
 
  if ( headerId() == QObexHeader::Invalid )
187
 
    return QString::null;
188
 
  else {
189
 
    if ( dataType() == QObexHeader::Unicode ) {
190
 
      if ( 0 < mData.size() ) {
191
 
        QString ret = "";
192
 
        Q_ULONG l = mData.size()/2 - 1;
193
 
        for ( Q_ULONG i = 0; i < l; ++i )
194
 
          ret[i] = mData.qchar( 2*i );
195
 
        return ret;
196
 
      } else
197
 
        return QString::null;
198
 
    } else if ( dataType() == QObexHeader::ByteSequence ) {
199
 
      if ( 0 < mData.size() )
200
 
        return QString::fromLatin1( mData.data(), mData.size() );
201
 
      else
202
 
        return QString::null;
203
 
    } else if ( dataType() == QObexHeader::DWord ) {
204
 
      return QString::number( mData.uint32( 0 ) );
205
 
    } else {
206
 
      return QString::number( mData.uint8( 0 ) );
207
 
    }
208
 
  }
209
 
}
210
 
 
211
 
Q_UINT32
212
 
QObexHeader::uint32Data() const {
213
 
  myDebug(( "QObexHeader::uint32Data() const: id is %s", stringHeaderId().ascii() ));
214
 
  if ( headerId() == QObexHeader::Invalid )
215
 
    return 0;
216
 
  else {
217
 
    if ( dataType() == QObexHeader::DWord )
218
 
      return mData.uint32( 0 );
219
 
    else if ( dataType() == QObexHeader::Byte )
220
 
      return mData.uint8( 0 );
221
 
    else {
222
 
      Q_ASSERT( true );
223
 
      return 0;
224
 
    }
225
 
  }
226
 
}
227
 
 
228
 
Q_UINT8
229
 
QObexHeader::uint8Data() const {
230
 
  myDebug(( "QObexHeader::uint8Data() const: id is %s", stringHeaderId().ascii() ));
231
 
  if ( headerId() == QObexHeader::Invalid )
232
 
    return 0;
233
 
  else {
234
 
    if ( dataType() == QObexHeader::DWord )
235
 
      return mData.uint32( 0 );
236
 
    else if ( dataType() == QObexHeader::Byte )
237
 
      return mData.uint8( 0 );
238
 
    else {
239
 
      Q_ASSERT( false );
240
 
      return 0;
241
 
    }
242
 
  }
243
 
}
244
 
 
245
 
QDateTime QObexHeader::timeData() const {
246
 
  myDebug(( "QObexHeader::timeData() const: id is %s", stringHeaderId().ascii() ));
247
 
  QDateTime ret;
248
 
  if ( headerId() == QObexHeader::Time ) {
249
 
    QString str = stringData();
250
 
    str.insert(13, ':');
251
 
    str.insert(11, ':');
252
 
    str.insert(6, '-');
253
 
    str.insert(4, '-');
254
 
    ret.fromString( str, Qt::ISODate );
255
 
  } else if ( headerId() == QObexHeader::TimeCompat )
256
 
    ret.setTime_t( uint32Data() );
257
 
 
258
 
  return ret;
259
 
}
260
 
 
261
 
QByteArray QObexHeader::coreHeader() const {
262
 
  myDebug(( "QObexHeader::coreHeader() const: id is %s", stringHeaderId().ascii() ));
263
 
  if ( dataType() == QObexHeader::Byte ) {
264
 
    QObexArray ret( 2 );
265
 
    ret.uint8( 0, mId );
266
 
    ret.uint8( 1, mData.at( 0 ) );
267
 
    return ret;
268
 
  } else if ( dataType() == QObexHeader::DWord ) {
269
 
    QObexArray ret( 5 );
270
 
    ret.uint8( 0, mId );
271
 
    ::memcpy( ret.data()+1, mData.data(), 4 );
272
 
    return ret;
273
 
  } else {
274
 
    QObexArray ret( 3 );
275
 
    ret.uint8( 0, mId );
276
 
    ret.uint16( 1, 3 + mData.size() );
277
 
    return ret;
278
 
  }
279
 
}
280
 
 
281
 
QString QObexHeader::toString( unsigned indent, bool fullContent ) const {
282
 
  QString indentStr;
283
 
  indentStr.fill( QChar( ' ' ), indent );
284
 
  QString str;
285
 
  QTextStream stream( &str, IO_WriteOnly );
286
 
  
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 );
299
 
    } else {
300
 
      indentStr += "   ";
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);
307
 
      }
308
 
 
309
 
      // finally dump ...
310
 
      stream << indentStr << "<ascii>";
311
 
      for ( Q_ULONG i = 0; i < len; ++i ) {
312
 
        Q_UINT8 c = mData[i];
313
 
        if ( isprint( c ) )
314
 
          stream << QChar( c );
315
 
        else if ( c == '\r' )
316
 
          stream << "<CR>";
317
 
        else if ( c == '\n' )
318
 
          stream << "<LF>";
319
 
        else
320
 
          stream << ".";
321
 
      }
322
 
      if ( len == mData.size() )
323
 
        stream << "</ascii>\n";
324
 
      else
325
 
        stream << " ...\n";
326
 
 
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;
332
 
      }
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";
338
 
      else
339
 
        stream << " ...\n";
340
 
    }
341
 
  } else if ( dataType() == QObexHeader::Unicode ) {
342
 
    if ( dataType() == QObexHeader::Unicode )
343
 
      stream << " (Unicode)\n";
344
 
    stream << indentStr << "   \"" << stringData() << "\"\n";
345
 
  } else {
346
 
    stream << (dataType() == QObexHeader::Byte ? " (UINT8)\n" : " (UINT32)\n");
347
 
    // stringData does "the right thing" also for uint values
348
 
    stream << indentStr << "   " << stringData() << "\n";
349
 
  }
350
 
 
351
 
  return str;
352
 
}