summaryrefslogtreecommitdiffstats
path: root/qt/message.cpp
blob: 646289549b048d37f6b4ec13965c47c1624e341b (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
/* -*- mode: C++; c-file-style: "gnu" -*- */
/* message.cpp: Qt wrapper for DBusMessage
 *
 * Copyright (C) 2003  Zack Rusin <zack@kde.org>
 *
 * Licensed under the Academic Free License version 1.2
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "message.h"

#include <kdebug.h>

namespace DBus {

struct Message::iterator::IteratorData {
  DBusMessageIter *iter;
  QVariant         var;
  bool             end;
};

/**
 * Iterator.
 */
Message::iterator::iterator()
{
  d = new IteratorData;
  d->iter = 0; d->end = true;
}

/**
 * Constructs iterator for the message.
 * @param msg message whose fields we want to iterate
 */
Message::iterator::iterator( DBusMessage* msg)
{
  d = new IteratorData;
  d->iter = dbus_message_get_fields_iter( msg );
  d->end = false;
}

/**
 * Copy constructor for the iterator.
 * @param itr iterator
 */
Message::iterator::iterator( const iterator& itr )
{
  d = new IteratorData;
  dbus_message_iter_ref( itr.d->iter );
  d->iter = itr.d->iter;
  d->var  = itr.d->var;
  d->end  = itr.d->end;
}

/**
 * Destructor.
 */
Message::iterator::~iterator()
{
  dbus_message_iter_unref( d->iter );
  delete d; d=0;
}

/**
 * Creates an iterator equal to the @p itr iterator
 * @param itr other iterator
 * @return
 */
Message::iterator&
Message::iterator::operator=( const iterator& itr )
{
  //in case we'll ever go fot exception safety
  dbus_message_iter_ref( itr.d->iter );
  IteratorData *tmp = new IteratorData;
  tmp->iter = itr.d->iter;
  tmp->var  = itr.d->var;
  tmp->end  = itr.d->end;
  dbus_message_iter_unref( d->iter );
  delete d; d=tmp;
  return *this;
}

/**
 * Returns the constant QVariant held by the iterator.
 * @return the constant reference to QVariant held by this iterator
 */
const QVariant&
Message::iterator::operator*() const
{
  return d->var;
}

/**
 * Returns the QVariant held by the iterator.
 * @return reference to QVariant held by this iterator
 */
QVariant&
Message::iterator::operator*()
{
  return d->var;
}

/**
 * Moves to the next field and return a reference to itself after
 * incrementing.
 * @return reference to self after incrementing
 */
Message::iterator&
Message::iterator::operator++()
{
  if ( d->end )
    return *this;

  if (  dbus_message_iter_next( d->iter ) ) {
    fillVar();
  } else {
    d->end = true;
    d->var = QVariant();
  }
  return *this;
}

/**
 * Moves to the next field and returns self before incrementing.
 * @return self before incrementing
 */
Message::iterator
Message::iterator::operator++(int)
{
  iterator itr( *this );
  operator++();
  return itr;
}

/**
 * Compares this iterator to @p it iterator.
 * @param it the iterator to which we're comparing this one to
 * @return true if they're equal, false otherwise
 */
bool
Message::iterator::operator==( const iterator& it )
{
  if ( d->end == it.d->end ) {
    if ( d->end == true ) {
      return true;
    } else {
      return d->var == it.d->var;
    }
  } else
    return false;
}

/**
 * Compares two iterators.
 * @param it The other iterator.
 * @return true if two iterators are not equal, false
 *         otherwise
 */
bool
Message::iterator::operator!=( const iterator& it )
{
  return !operator==( it );
}

/**
 * Fills QVariant based on what current DBusMessageIter helds.
 */
void
Message::iterator::fillVar()
{
  switch ( dbus_message_iter_get_field_type( d->iter ) ) {
  case DBUS_TYPE_INT32:
    d->var = QVariant( dbus_message_iter_get_int32( d->iter ) );
    break;
  case DBUS_TYPE_UINT32:
    d->var = QVariant( dbus_message_iter_get_uint32( d->iter ) );
    break;
  case DBUS_TYPE_DOUBLE:
    d->var = QVariant( dbus_message_iter_get_double( d->iter ) );
    break;
  case DBUS_TYPE_STRING:
    d->var = QVariant( QString(dbus_message_iter_get_string( d->iter )) );
    break;
  case DBUS_TYPE_BYTE_ARRAY:
    {
      QByteArray a;
      int len;
      char *ar;
      ar = reinterpret_cast<char*>( dbus_message_iter_get_byte_array( d->iter, &len ) );
      a.setRawData( ar, len );
      QDataStream stream( a, IO_ReadOnly );
      stream >> d->var;
      a.resetRawData( ar, len );
    }
    break;
  case DBUS_TYPE_STRING_ARRAY:
#warning "String array not implemented"
    //d->var = QVariant( dbus_message_iter_get_string_array );
    break;
  default:
    kdWarning()<<k_funcinfo<<" Serious problem!! "<<endl;
    d->var = QVariant();
    break;
  }
}

/**
 * Returns a QVariant help by this iterator.
 * @return QVariant held by this iterator
 */
QVariant
Message::iterator::var() const
{
  return d->var;
}

struct Message::MessagePrivate {
  DBusMessage *msg;
};

/**
 * Constructs a new Message with the given service and name.
 * @param service service service that the message should be sent to
 * @param name name of the message
 */
Message::Message( const QString& service, const QString& name )
{
  d = new MessagePrivate;
  d->msg = dbus_message_new( service.latin1(), name.latin1() );
}

/**
 * Constructs a message that is a reply to some other
 * message.
 * @param name the name of the message
 * @param replayingTo original_message the message which the created
 * message is a reply to.
 */
Message::Message( const QString& name, const Message& replayingTo )
{
  d = new MessagePrivate;
  d->msg = dbus_message_new_reply( name.latin1(), replayingTo );
}

/**
 * Creates a message just like @p other
 * @param other the copied message
 */
Message::Message( const Message& other )
{
  d = new MessagePrivate;
  d->msg = dbus_message_new_from_message( other );
}

/**
 * Destructs message.
 */
Message::~Message()
{
  dbus_message_unref( d->msg );
  delete d; d=0;
}

/**
 * Sets the message sender.
 * @param sender the sender
 * @return false if unsuccessful
 */
bool
Message::setSender( const QString& sender )
{
  return dbus_message_set_sender( d->msg, sender.latin1() );
}

/**
 * Sets a flag indicating that the message is an error reply
 * message, i.e. an "exception" rather than a normal response.
 * @param error true if this is an error message.
 */
void
Message::setError( bool error )
{
  return dbus_message_set_is_error( d->msg, error );
}

/**
 * Returns name of this message.
 * @return name
 */
QString
Message::name() const
{
  return dbus_message_get_name( d->msg );
}

/**
 * Returns service associated with this message.
 * @return service
 */
QString
Message::service() const
{
  return dbus_message_get_service( d->msg );
}

/**
 * Returns sender of this message.
 * @return sender
 */
QString
Message::sender() const
{
  return dbus_message_get_sender( d->msg );
}

/**
 * Checks whether this message is an error indicating message.
 * @return true if this is an error message
 */
bool
Message::isError() const
{
  return dbus_message_get_is_error( d->msg );
}

/**
 * Message can be casted to DBusMessage* to make it easier to
 * use it with raw DBus.
 * @return underlying DBusMessage*
 */
Message::operator DBusMessage*() const
{
  return d->msg;
}

/**
 * Appends data to this message. It can be anything QVariant accepts.
 * @param var Data to append
 */
void
Message::append( const QVariant& var )
{
  switch ( var.type() ) {
  case QVariant::Int:
    dbus_message_append_int32( d->msg, var.toInt() );
    break;
  case QVariant::UInt:
    dbus_message_append_uint32( d->msg, var.toUInt() );
    break;
  case QVariant::String: //what about QVariant::CString ?
    dbus_message_append_string( d->msg, var.toString() );
    break;
  case QVariant::Double:
    dbus_message_append_double( d->msg, var.toDouble() );
    break;
  case QVariant::Invalid:
    break;
  default: // handles QVariant::ByteArray
    QByteArray a;
    QDataStream stream( a, IO_WriteOnly );
    stream<<var;
    dbus_message_append_byte_array( d->msg, a.data(), a.size() );
  }
}


/**
 * Returns the starting iterator for the fields of this
 * message.
 * @return starting iterator
 */
Message::iterator
Message::begin() const
{
  return iterator( d->msg );
}

/**
 * Returns the ending iterator for the fields of this
 * message.
 * @return ending iterator
 */
Message::iterator
Message::end() const
{
  return iterator();
}

/**
 * Returns the field at position @p i
 * @param i position of the wanted field
 * @return QVariant at position @p i or an empty QVariant
 */
QVariant
Message::at( int i )
{
  iterator itr( d->msg );

  while ( i-- ) {
    if ( itr == end() )
      return QVariant();//nothing there
    ++itr;
  }
  return *itr;
}

/**
 * The underlying DBusMessage of this class.
 * @return DBusMessage pointer.
 */
DBusMessage*
Message::message() const
{
  return d->msg;
}

}