summaryrefslogtreecommitdiffstats
path: root/qt/message.cpp
blob: 8064ed7a2485926062f628a7ef35eba40e92fb17 (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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* -*- 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 2.0
 *
 * 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 <qmap.h>

#include <cstdlib>

namespace DBusQt {

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

/**
 * 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->mesg = msg;
  d->iter = static_cast<DBusMessageIter *>( malloc( sizeof(DBusMessageIter) ) );
  dbus_message_iter_init( d->mesg, d->iter );
  if ( !d->iter ) {
    qDebug("No iterator??");
  }
  fillVar();
  d->end = false;
}

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

/**
 * Destructor.
 */
Message::iterator::~iterator()
{
  free( 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 )
{
  IteratorData *tmp = new IteratorData;
  tmp->iter = itr.d->iter;
  tmp->var  = itr.d->var;
  tmp->end  = itr.d->end;
  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 );
}

QVariant Message::iterator::marshallBaseType( DBusMessageIter* i )
{
  QVariant ret;
  switch (dbus_message_iter_get_arg_type(i)) {
  case DBUS_TYPE_INT32:
    {
      dbus_int32_t v;
      dbus_message_iter_get_basic (i, &v);
      ret = QVariant( v );
    }
    break;
  case DBUS_TYPE_UINT32:
    {
      dbus_uint32_t v;
      dbus_message_iter_get_basic (i, &v);
      ret = QVariant( v );
    }
    break;
  case DBUS_TYPE_DOUBLE:
    {
      double v;
      dbus_message_iter_get_basic (i, &v);
      ret = QVariant( v );
    }
    break;
  case DBUS_TYPE_STRING:
    {
      const char *v;
      dbus_message_iter_get_basic (i, &v);
      ret = QVariant( v );
    }
    break;
  default:
    ret = QVariant();
    break;
  }
  return ret;
}

/**
 * Fills QVariant based on what current DBusMessageIter helds.
 */
void
Message::iterator::fillVar()
{
  switch ( dbus_message_iter_get_arg_type( d->iter ) ) {
  case DBUS_TYPE_INT32:
  case DBUS_TYPE_UINT32:
  case DBUS_TYPE_DOUBLE:
  case DBUS_TYPE_STRING:
    d->var = marshallBaseType( d->iter );
    break;
  case DBUS_TYPE_ARRAY: {
    switch ( dbus_message_iter_get_element_type( d->iter ) ) {
    case DBUS_TYPE_STRING: {
      QStringList tempList;
      DBusMessageIter sub;
      dbus_message_iter_recurse (d->iter, &sub);
      while (dbus_message_iter_get_arg_type (&sub) != DBUS_TYPE_INVALID)
        {
          const char *v;
          dbus_message_iter_get_basic (&sub, &v);
          tempList.append( QString( v ) );
          dbus_message_iter_next (&sub);
        }
      d->var = QVariant( tempList );
      break;
    }
    default:
      qDebug( "Array of type not implemented" );
      d->var = QVariant();
      break;
    }
    break;
  }
#if 0
  /* DICT is gone for now, but expected to be reintroduced, or else
   * reintroduced as a flag on the introspection data that can
   * apply to array of struct of two fields
   */
  case DBUS_TYPE_DICT: {
    qDebug( "Got a hash!" );
    QMap<QString, QVariant> tempMap;
    DBusMessageIter dictIter;
    dbus_message_iter_init_dict_iterator( d->iter, &dictIter );
    do {
      char *key = dbus_message_iter_get_dict_key( &dictIter );
      tempMap[key] = marshallBaseType( &dictIter );
      dbus_free( key );
      dbus_message_iter_next( &dictIter );
    } while( dbus_message_iter_has_next( &dictIter ) );
    d->var = QVariant( tempMap );
    break;
    qDebug( "Hash/Dict type not implemented" );
    d->var = QVariant();
    break;
  }
#endif
  default:
    qDebug( "not implemented" );
    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::Private {
  DBusMessage *msg;
};

Message::Message( DBusMessage *m )
{
  d = new Private;
  d->msg = m;
}

/**
 *
 */
Message::Message( int messageType )
{
  d = new Private;
  d->msg = dbus_message_new( messageType );
}

/**
 * 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& path,
                  const QString& interface, const QString& method )
{
  d = new Private;
  d->msg = dbus_message_new_method_call( service.latin1(), path.latin1(),
                                         interface.latin1(), method.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 Message& replayingTo )
{
  d = new Private;
  d->msg = dbus_message_new_method_return( replayingTo.d->msg );
}

Message:: Message( const QString& path, const QString& interface,
                   const QString& name )
{
  d = new Private;
  d->msg = dbus_message_new_signal( path.ascii(), interface.ascii(),
                                    name.ascii() );
}

Message::Message( const Message& replayingTo, const QString& errorName,
                  const QString& errorMessage )
{
  d = new Private;
  d->msg = dbus_message_new_error( replayingTo.d->msg, errorName.utf8(),
                                   errorMessage.utf8() );
}

Message Message::operator=( const Message& other )
{
  //FIXME: ref the other.d->msg instead of copying it?
}
/**
 * Destructs message.
 */
Message::~Message()
{
  if ( d->msg ) {
    dbus_message_unref( d->msg );
  }
  delete d; d=0;
}

int Message::type() const
{
  return dbus_message_get_type( d->msg );
}

void Message::setPath( const QString& path )
{
  dbus_message_set_path( d->msg, path.ascii() );
}

QString Message::path() const
{
  return dbus_message_get_path( d->msg );
}

void Message::setInterface( const QString& iface )
{
  dbus_message_set_interface( d->msg, iface.ascii() );
}

QString Message::interface() const
{
  return dbus_message_get_interface( d->msg );
}

void Message::setMember( const QString& member )
{
  dbus_message_set_member( d->msg, member.ascii() );
}

QString Message::member() const
{
  return dbus_message_get_member( d->msg );
}

void Message::setErrorName( const QString& err )
{
  dbus_message_set_error_name( d->msg, err );
}

QString Message::errorName() const
{
  return dbus_message_get_error_name( d->msg );
}

void Message::setDestination( const QString& dest )
{
  dbus_message_set_destination( d->msg, dest );
}

QString Message::destination() const
{
  return dbus_message_get_destination( d->msg );
}

/**
 * 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() );
}

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

QString Message::signature() const
{
  return dbus_message_get_signature( d->msg );
}


/**
 * 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;
}

Message& Message::operator<<( bool b )
{
  const unsigned char byte = b;
  dbus_message_append_args( d->msg, DBUS_TYPE_BOOLEAN, &byte,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( Q_INT8 byte )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_BYTE, &byte,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( Q_INT32 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_INT32, &num,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( Q_UINT32 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_UINT32, &num,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( Q_INT64 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_INT64, &num,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( Q_UINT64 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_UINT64, &num,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( double num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_DOUBLE, &num,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( const QString& str )
{
  const char *u = str.utf8();
  dbus_message_append_args( d->msg, DBUS_TYPE_STRING, &u,
                            DBUS_TYPE_INVALID );
}

Message& Message::operator<<( const QVariant& custom )
{
  //FIXME: imeplement
}

}