summaryrefslogtreecommitdiffstats
path: root/message.cpp
blob: 9f94955b4f1e9b446af4c33eaf303060bb349739 (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
562
563
564
565
566
567
568
569
570
571
/* -*- mode: C++; c-file-style: "gnu" -*- */
/* message.cpp: TQt wrapper for DBusMessage
 *
 * Copyright (C) 2003  Zack Rusin <[email protected]>
 *
 * 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 <tqmap.h>

#include <cstdlib>

namespace DBusQt {

struct Message::iterator::IteratorData {
  DBusMessageIter *iter;
  TQVariant         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 ) {
    tqDebug("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 TQVariant held by the iterator.
 * @return the constant reference to TQVariant held by this iterator
 */
const TQVariant&
Message::iterator::operator*() const
{
  return d->var;
}

/**
 * Returns the TQVariant held by the iterator.
 * @return reference to TQVariant held by this iterator
 */
TQVariant&
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 = TQVariant();
  }
  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 );
}

TQVariant Message::iterator::marshallBaseType( DBusMessageIter* i )
{
  TQVariant 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 = TQVariant( v );
    }
    break;
  case DBUS_TYPE_UINT32:
    {
      dbus_uint32_t v;
      dbus_message_iter_get_basic (i, &v);
      ret = TQVariant( v );
    }
    break;
  case DBUS_TYPE_DOUBLE:
    {
      double v;
      dbus_message_iter_get_basic (i, &v);
      ret = TQVariant( v );
    }
    break;
  case DBUS_TYPE_STRING:
    {
      const char *v;
      dbus_message_iter_get_basic (i, &v);
      ret = TQVariant( v );
    }
    break;
  default:
    ret = TQVariant();
    break;
  }
  return ret;
}

/**
 * Fills TQVariant 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: {
      TQStringList 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( TQString( v ) );
          dbus_message_iter_next (&sub);
        }
      d->var = TQVariant( tempList );
      break;
    }
    default:
      tqDebug( "Array of type not implemented" );
      d->var = TQVariant();
      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: {
    tqDebug( "Got a hash!" );
    TQMap<TQString, TQVariant> 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 = TQVariant( tempMap );
    break;
    tqDebug( "Hash/Dict type not implemented" );
    d->var = TQVariant();
    break;
  }
#endif
  default:
    tqDebug( "not implemented" );
    d->var = TQVariant();
    break;
  }
}

/**
 * Returns a TQVariant help by this iterator.
 * @return TQVariant held by this iterator
 */
TQVariant
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 TQString& service, const TQString& path,
                  const TQString& interface, const TQString& 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 TQString& path, const TQString& interface,
                   const TQString& name )
{
  d = new Private;
  d->msg = dbus_message_new_signal( path.ascii(), interface.ascii(),
                                    name.ascii() );
}

Message::Message( const Message& replayingTo, const TQString& errorName,
                  const TQString& 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?
  return *this;
}
/**
 * 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 TQString& path )
{
  dbus_message_set_path( d->msg, path.ascii() );
}

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

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

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

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

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

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

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

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

TQString 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 TQString& sender )
{
  return dbus_message_set_sender( d->msg, sender.latin1() );
}

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

TQString 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 TQVariant at position @p i or an empty TQVariant
 */
TQVariant
Message::at( int i )
{
  iterator itr( d->msg );

  while ( i-- ) {
    if ( itr == end() )
      return TQVariant();//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 dbus_bool_t right_size_bool = b;
  dbus_message_append_args( d->msg, DBUS_TYPE_BOOLEAN, &right_size_bool,
                            DBUS_TYPE_INVALID );
  return *this;
}

Message& Message::operator<<( TQ_INT8 byte )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_BYTE, &byte,
                            DBUS_TYPE_INVALID );
  return *this;
}

Message& Message::operator<<( TQ_INT32 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_INT32, &num,
                            DBUS_TYPE_INVALID );
  return *this;
}

Message& Message::operator<<( TQ_UINT32 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_UINT32, &num,
                            DBUS_TYPE_INVALID );
  return *this;
}

Message& Message::operator<<( TQ_INT64 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_INT64, &num,
                            DBUS_TYPE_INVALID );
  return *this;
}

Message& Message::operator<<( TQ_UINT64 num )
{
  dbus_message_append_args( d->msg, DBUS_TYPE_UINT64, &num,
                            DBUS_TYPE_INVALID );
  return *this;
}

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

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

Message& Message::operator<<( const TQVariant& custom )
{
  //FIXME: imeplement
  return *this;
}

}