~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to lib/kscript/kscript_corbafunc.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#if 0
2
 
 
3
 
#define WITH_CORBA
4
 
#include <CORBA.h>
5
 
 
6
 
#include "kscript_corbafunc.h"
7
 
#include "kscript_util.h"
8
 
#include "kscript_struct.h"
9
 
#include "kscript_proxy.h"
10
 
#include "kscript_types.h"
11
 
#include "kscript.h"
12
 
#include "kscript_parsenode.h"
13
 
 
14
 
#include <klocale.h>
15
 
 
16
 
/**********************************
17
 
 *
18
 
 * Marshalling and Demarshalling
19
 
 *
20
 
 **********************************/
21
 
 
22
 
/**
23
 
 * Unpack an Any in a KSValue
24
 
 */
25
 
bool ksUnpack( KSContext& context, KSValue* _arg, CORBA::Any& _any, CORBA::TypeCode_ptr _tc )
26
 
{
27
 
  switch( _tc->kind() )
28
 
  {
29
 
  case CORBA::tk_void:
30
 
    _arg->clear();
31
 
    return true;
32
 
  case CORBA::tk_any:
33
 
    {
34
 
      // TODO: CORBA::Any support
35
 
      /* PyObject* pany = pm_module->newAny();
36
 
      if ( pany == 0L )
37
 
      {
38
 
        cerr << "ERROR: Could not create a new any" << endl;
39
 
        return 0L;
40
 
      }
41
 
      int id = g_id++;
42
 
      CORBA::Any* any = new CORBA::Any();
43
 
      _any >>= *any;
44
 
      g_mapAny[ id ] = any;
45
 
      PyObject* p_id = Py_BuildValue( "i", id );
46
 
      PyObject_SetAttrString( pany, "_any_id_", p_id );
47
 
      Py_DECREF( p_id );
48
 
      return pany; */
49
 
      return false;
50
 
    }
51
 
  case CORBA::tk_enum:
52
 
    {
53
 
      CORBA::ULong i;
54
 
      assert( _any.enum_get( i ) );
55
 
      _arg->setValue( QString( _tc->member_name( i ) ) );
56
 
      return true;
57
 
    }
58
 
  case CORBA::tk_string:
59
 
    {
60
 
      char* text;
61
 
      bool res = ( _any >>= CORBA::Any::to_string( text, 0 ) );
62
 
      ASSERT( res );
63
 
      _arg->setValue( QString( text ) );
64
 
      return true;
65
 
    }
66
 
  case CORBA::tk_short:
67
 
    {
68
 
      CORBA::Short x;
69
 
      bool erg = ( _any >>= x );
70
 
      ASSERT( erg );
71
 
      _arg->setValue( (KScript::Long)x );
72
 
      return true;
73
 
    }
74
 
  case CORBA::tk_ushort:
75
 
    {
76
 
      CORBA::UShort x;
77
 
      bool erg = ( _any >>= x );
78
 
      ASSERT( erg );
79
 
      _arg->setValue( (KScript::Long)x );
80
 
      return true;
81
 
    }
82
 
  case CORBA::tk_long:
83
 
    {
84
 
      CORBA::Long x;
85
 
      bool erg = ( _any >>= x );
86
 
      ASSERT( erg );
87
 
      _arg->setValue( (KScript::Long)x );
88
 
      return true;
89
 
    }
90
 
  case CORBA::tk_ulong:
91
 
    {
92
 
      CORBA::ULong x;
93
 
      bool erg = ( _any >>= x );
94
 
      ASSERT( erg );
95
 
      _arg->setValue( (KScript::Long)x );
96
 
      return true;
97
 
    }
98
 
  case CORBA::tk_float:
99
 
    {
100
 
      CORBA::Float x;
101
 
      bool erg = ( _any >>= x );
102
 
      ASSERT( erg );
103
 
      _arg->setValue( (KScript::Double)x );
104
 
      return true;
105
 
    }
106
 
  case CORBA::tk_double:
107
 
    {
108
 
      CORBA::Double x;
109
 
      bool erg = ( _any >>= x );
110
 
      ASSERT( erg );
111
 
      _arg->setValue( (KScript::Double)x );
112
 
      return true;
113
 
    }
114
 
  case CORBA::tk_objref:
115
 
    {
116
 
      CORBA::Object_ptr x;
117
 
      if ( !( _any >>= CORBA::Any::to_object( x ) ) )
118
 
        return 0L;
119
 
 
120
 
      if ( CORBA::is_nil( x ) )
121
 
      {
122
 
        _arg->clear();
123
 
        return true;
124
 
      }
125
 
 
126
 
      if ( !x->_is_a( _tc->id() ) )
127
 
      {
128
 
        QString tmp( i18n("While unpacking a CORBA datatype, an object of type %1 was expected, but %2 was found") );
129
 
        context.setException( new KSException( "UnpackingError", tmp.arg( _tc->id() ).arg( x->_repoid() ) ) );
130
 
        return false;
131
 
      }
132
 
 
133
 
      // We try to instantiate the most specialized interface.
134
 
      // Please mention that C++-Corba does not do that, but since we
135
 
      // are dynamically typed ...
136
 
      KSValue *c = context.interpreter()->repoidImplementation( x->_repoid() );
137
 
      if ( !c )
138
 
        c = context.interpreter()->repoidImplementation( _tc->id() );
139
 
 
140
 
      if ( !c || !c->type() == KSValue::InterfaceType )
141
 
      {
142
 
        QString tmp(i18n( "Did not find an interface for the repoid %1. Seems to be an error of the idl compiler.") );
143
 
        context.setException( new KSException( "UnpackingError", tmp.arg( _tc->id() ) ) );
144
 
        return false;
145
 
      }
146
 
 
147
 
      KSProxy* proxy = c->interfaceValue()->constructor( (CORBA::Object*)x );
148
 
      ASSERT( proxy );
149
 
      _arg->setValue( proxy );
150
 
      return true;
151
 
    }
152
 
  case CORBA::tk_TypeCode:
153
 
    {
154
 
      CORBA::TypeCode_ptr x;
155
 
      bool erg = ( _any >>= x );
156
 
      ASSERT( erg );
157
 
      _arg->setValue( new KSTypeCode( (CORBA::TypeCode*)x ) );
158
 
      return true;
159
 
    }
160
 
  case CORBA::tk_boolean:
161
 
    {
162
 
      CORBA::Boolean x;
163
 
      bool erg = ( _any >>= CORBA::Any::to_boolean( x ) );
164
 
      ASSERT( erg );
165
 
      _arg->setValue( (KScript::Boolean)x );
166
 
      return true;
167
 
    }
168
 
  case CORBA::tk_octet:
169
 
    {
170
 
      CORBA::Octet x;
171
 
      bool erg = ( _any >>= CORBA::Any::to_octet( x ) );
172
 
      ASSERT( erg );
173
 
      _arg->setValue( (KScript::Long)x );
174
 
      return true;
175
 
    }
176
 
  case CORBA::tk_char:
177
 
    {
178
 
      CORBA::Char x;
179
 
      bool erg = ( _any >>= CORBA::Any::to_char( x ) );
180
 
      ASSERT( erg );
181
 
      char buf[ 2 ];
182
 
      buf[0] = x;
183
 
      buf[1] = 0;
184
 
      _arg->setValue( QString( buf ) );
185
 
      return true;
186
 
    }
187
 
  case CORBA::tk_sequence:
188
 
    {
189
 
      CORBA::ULong len;
190
 
      assert( _any.seq_get_begin( len ) );
191
 
      CORBA::TypeCode_var ctc = _tc->content_type();
192
 
 
193
 
      QValueList<KSValue::Ptr> lst;
194
 
 
195
 
      for( CORBA::ULong i = 0; i < len; i++ )
196
 
      {
197
 
        KSValue::Ptr v = new KSValue;
198
 
        if ( !ksUnpack( context, v, _any, ctc ) )
199
 
          return false;
200
 
        lst.append( v );
201
 
      }
202
 
      assert( _any.seq_get_end() );
203
 
 
204
 
      _arg->setValue( lst );
205
 
      return true;
206
 
    }
207
 
  case CORBA::tk_struct:
208
 
    {
209
 
      KSValue *c = context.interpreter()->repoidImplementation( _tc->id() );
210
 
 
211
 
      if ( !c || !c->type() == KSValue::StructClassType )
212
 
      {
213
 
        QString tmp( i18n("Did not find a struct for the repoid %1. Seems to be an error of the idl compiler.") );
214
 
        context.setException( new KSException( "UnpackingError", tmp.arg( _tc->id() ) ) );
215
 
        return false;
216
 
      }
217
 
 
218
 
      KSStruct::Ptr s = c->structClassValue()->constructor();
219
 
 
220
 
      CORBA::ULong len = _tc->member_count();
221
 
      assert( _any.struct_get_begin() );
222
 
      for( CORBA::ULong i = 0; i < len; i++ )
223
 
      {
224
 
        CORBA::TypeCode_var ntc = _tc->member_type( i );
225
 
        KSValue::Ptr m = new KSValue;
226
 
        if ( !ksUnpack( context, m, _any, ntc ) )
227
 
          return false;
228
 
        if ( !s->setMember( context, _tc->member_name( i ), m ) )
229
 
          return false;
230
 
      }
231
 
      assert( _any.struct_get_end() );
232
 
 
233
 
      s->ref();
234
 
      _arg->setValue( &*s );
235
 
      return true;
236
 
    }
237
 
  case CORBA::tk_except:
238
 
    {
239
 
      KSValue *c = context.interpreter()->repoidImplementation( _tc->id() );
240
 
 
241
 
      if ( !c || !c->type() == KSValue::StructClassType )
242
 
      {
243
 
        QString tmp( i18n("Did not find a struct for the repoid %1. Seems to be an error of the idl compiler.") );
244
 
        context.setException( new KSException( "UnpackingError", tmp.arg( _tc->id() ) ) );
245
 
        return false;
246
 
      }
247
 
 
248
 
      KSStruct::Ptr s = c->structClassValue()->constructor();
249
 
 
250
 
      CORBA::ULong len = _tc->member_count();
251
 
      CORBA::String_var repoid;
252
 
      assert( _any.except_get_begin( repoid ) );
253
 
      for( CORBA::ULong i = 0; i < len; i++ )
254
 
      {
255
 
        CORBA::TypeCode_var ntc = _tc->member_type( i );
256
 
        KSValue::Ptr m = new KSValue;
257
 
        if ( !ksUnpack( context, m, _any, ntc ) )
258
 
          return false;
259
 
        if ( !s->setMember( context, _tc->member_name( i ), m ) )
260
 
          return false;
261
 
      }
262
 
      assert( _any.except_get_end() );
263
 
 
264
 
      s->ref();
265
 
      _arg->setValue( &*s );
266
 
      return true;
267
 
    }
268
 
  case CORBA::tk_union:
269
 
    {
270
 
      // TODO: Union
271
 
      /*
272
 
      cerr << "Extracting Union " << _tc->id() << endl;
273
 
      CORBA::TypeCode_var dt = _tc->discriminator_type();
274
 
      CORBA::Long defidx = _tc->default_index();
275
 
 
276
 
      assert( _any.union_get_begin() );
277
 
 
278
 
      PyObject* disc = parseResult( _any, dt );
279
 
      bool found = false;
280
 
      CORBA::ULong idx;
281
 
      if ( disc == 0L )
282
 
      {
283
 
        cerr << "ERROR: Could not extract discriminator of union " << _tc->id() << endl;
284
 
        return 0L;
285
 
      }
286
 
      for ( idx = 0; idx < _tc->member_count(); idx++ )
287
 
      {
288
 
        if ( defidx != -1 && (CORBA::ULong) defidx == idx )
289
 
          continue;
290
 
 
291
 
        CORBA::Any_var lany = _tc->member_label( idx );
292
 
        PyObject* label = parseResult( *lany, dt );
293
 
        if ( label == 0L )
294
 
        {
295
 
          cerr << "ERROR: Could not extract label of union " << _tc->id() << endl;
296
 
          return 0L;
297
 
        }
298
 
        if ( PyObject_Compare( label, disc ) == 0 )
299
 
        {
300
 
          found = true;
301
 
          Py_DECREF( label );
302
 
          break;
303
 
        }
304
 
        Py_DECREF( label );
305
 
      }
306
 
 
307
 
      PyObject* uni = 0L;
308
 
 
309
 
      if ( found )
310
 
      {
311
 
        CORBA::TypeCode_var mtype = _tc->member_type( idx );
312
 
        assert( _any.union_get_selection( idx ) );
313
 
        PyObject* memb = parseResult( _any, mtype );
314
 
        if ( memb == 0L )
315
 
        {
316
 
          cerr << "ERROR: Could not extract value of union " << _tc->id() << endl;
317
 
          return 0L;
318
 
        }
319
 
        uni = pm_module->newUnion();
320
 
        assert( uni != 0L );
321
 
        PyObject_SetAttrString( uni, "d", disc );
322
 
        PyObject_SetAttrString( uni, "v", memb );
323
 
      }
324
 
      else if ( defidx == -1 )
325
 
      {
326
 
        uni = pm_module->newUnion();
327
 
        assert( uni != 0L );
328
 
        PyObject_SetAttrString( uni, "d", Py_None );
329
 
        PyObject_SetAttrString( uni, "v", Py_None );
330
 
      }
331
 
      else
332
 
      {
333
 
        assert( defidx != -1 );
334
 
        CORBA::TypeCode_var deftype = _tc->member_type( (CORBA::ULong) defidx);
335
 
        assert( _any.union_get_selection( defidx ) );
336
 
        uni = pm_module->newUnion();
337
 
        assert( uni != 0L );
338
 
        PyObject* memb = parseResult( _any, deftype );
339
 
        if ( memb == 0L )
340
 
        {
341
 
          cerr << "ERROR: Could not extract value of union " << _tc->id() << " using default discriminator" << endl;
342
 
          return 0L;
343
 
        }
344
 
        PyObject_SetAttrString( uni, "d", Py_None );
345
 
        PyObject_SetAttrString( uni, "v", memb );
346
 
      }
347
 
 
348
 
      assert( _any.union_get_end() );
349
 
 
350
 
      return uni; */
351
 
    }
352
 
  case CORBA::tk_alias:
353
 
    {
354
 
      const CORBA::TypeCode_var ntc = _tc->content_type();
355
 
      return ksUnpack( context, _arg, _any, ntc );
356
 
    }
357
 
  default:
358
 
    printf( "INTERNAL ERROR: Did not handle tk_XXXX=%i", (int)_tc->kind() );
359
 
    ASSERT( 0 );
360
 
  }
361
 
 
362
 
  return false;
363
 
}
364
 
 
365
 
/**
366
 
 * Pack a KScript value into a Any
367
 
 */
368
 
bool ksPack( KSContext& context, CORBA::Any& _any, KSValue* _arg, CORBA::TypeCode_ptr _tc )
369
 
{
370
 
  switch( _tc->kind() )
371
 
  {
372
 
  case CORBA::tk_void:
373
 
    return true;
374
 
  case CORBA::tk_any:
375
 
    {
376
 
      // TODO: Need any support
377
 
      // _any <<= *(it->second);
378
 
      return true;
379
 
    }
380
 
  case CORBA::tk_enum:
381
 
    {
382
 
      if ( !KSUtil::checkType( context, _arg, KSValue::StringType ) )
383
 
        return false;
384
 
 
385
 
      const char* value = _arg->stringValue().ascii();
386
 
      for ( CORBA::ULong i = 0; i < _tc->member_count(); i++ )
387
 
      {
388
 
        if ( strcmp( value, (char *) _tc->member_name (i) ) == 0 )
389
 
        {
390
 
          assert( _any.enum_put( i ) );
391
 
          return true;
392
 
        }
393
 
      }
394
 
 
395
 
      QString tmp( i18n("The value %1 is not a known enumerator\nPossible values are:\n") );
396
 
      for ( CORBA::ULong i = 0; i < _tc->member_count(); i++ )
397
 
      {
398
 
        tmp += _tc->member_name(i);
399
 
        tmp += " ";
400
 
      }
401
 
      context.setException( new KSException( "UnknownEnumerator", tmp.arg( value ) ) );
402
 
      return false;
403
 
    }
404
 
  case CORBA::tk_string:
405
 
    {
406
 
      if ( !KSUtil::checkType( context, _arg, KSValue::StringType ) )
407
 
        return false;
408
 
      _any <<= CORBA::Any::from_string( (char *) _arg->stringValue().ascii(), 0 );
409
 
      return true;
410
 
    }
411
 
  case CORBA::tk_short:
412
 
    {
413
 
      if ( !KSUtil::checkType( context, _arg, KSValue::IntType ) )
414
 
        return false;
415
 
      CORBA::Short x = _arg->intValue();
416
 
      _any <<= x;
417
 
      return true;
418
 
    }
419
 
  case CORBA::tk_ushort:
420
 
    {
421
 
      if ( !KSUtil::checkType( context, _arg, KSValue::IntType ) )
422
 
        return false;
423
 
      CORBA::UShort x = _arg->intValue();
424
 
      _any <<= x;
425
 
      return true;
426
 
    }
427
 
  case CORBA::tk_long:
428
 
    {
429
 
      if ( !KSUtil::checkType( context, _arg, KSValue::IntType ) )
430
 
        return false;
431
 
      CORBA::Long x = _arg->intValue();
432
 
      _any <<= x;
433
 
      return true;
434
 
    }
435
 
  case CORBA::tk_longlong:
436
 
    {
437
 
      if ( !KSUtil::checkType( context, _arg, KSValue::IntType ) )
438
 
        return false;
439
 
      CORBA::LongLong x = _arg->intValue();
440
 
      _any <<= x;
441
 
      return true;
442
 
    }
443
 
  case CORBA::tk_ulong:
444
 
    {
445
 
      if ( !KSUtil::checkType( context, _arg, KSValue::IntType ) )
446
 
        return false;
447
 
      CORBA::ULong x = _arg->intValue();
448
 
      _any <<= x;
449
 
      return true;
450
 
    }
451
 
  case CORBA::tk_double:
452
 
    {
453
 
      if ( !KSUtil::checkType( context, _arg, KSValue::DoubleType ) )
454
 
        return false;
455
 
      CORBA::Double x = _arg->doubleValue();
456
 
      _any <<= x;
457
 
      return true;
458
 
    }
459
 
  case CORBA::tk_float:
460
 
    {
461
 
      if ( !KSUtil::checkType( context, _arg, KSValue::DoubleType ) )
462
 
        return false;
463
 
      CORBA::Float x = _arg->doubleValue();
464
 
      _any <<= x;
465
 
      return true;
466
 
    }
467
 
  case CORBA::tk_longdouble:
468
 
    {
469
 
      if ( !KSUtil::checkType( context, _arg, KSValue::DoubleType ) )
470
 
        return false;
471
 
      CORBA::LongDouble x = _arg->doubleValue();
472
 
      _any <<= x;
473
 
      return true;
474
 
    }
475
 
  case CORBA::tk_boolean:
476
 
    {
477
 
      if ( !KSUtil::checkType( context, _arg, KSValue::BoolType ) )
478
 
        return false;
479
 
      CORBA::Boolean x = _arg->boolValue();
480
 
      _any <<= CORBA::Any::from_boolean( x );
481
 
      return true;
482
 
    }
483
 
  case CORBA::tk_char:
484
 
    {
485
 
      if ( !KSUtil::checkType( context, _arg, KSValue::StringType ) )
486
 
        return false;
487
 
      QString value = _arg->stringValue();
488
 
      if ( value.length() != 1 )
489
 
      {
490
 
        KSUtil::castingError( context, "multi-character String", "Character" );
491
 
        return false;
492
 
      }
493
 
      CORBA::Char x = *value.ascii();
494
 
      _any <<= CORBA::Any::from_char( x );
495
 
      return true;
496
 
    }
497
 
  case CORBA::tk_octet:
498
 
    {
499
 
      if ( !KSUtil::checkType( context, _arg, KSValue::IntType ) )
500
 
        return false;
501
 
      CORBA::Octet x = _arg->intValue();
502
 
      _any <<= CORBA::Any::from_octet( x );
503
 
      return true;
504
 
    }
505
 
  case CORBA::tk_TypeCode:
506
 
    {
507
 
      if ( !KSUtil::checkType( context, _arg, KSValue::TypeCodeType ) )
508
 
        return false;
509
 
      _any <<= (CORBA::TypeCode*)_arg->typeCodeValue()->tc();
510
 
      return true;
511
 
    }
512
 
  case CORBA::tk_objref:
513
 
    {
514
 
      if ( !KSUtil::checkType( context, _arg, KSValue::ProxyType ) )
515
 
        return false;
516
 
      CORBA::Object_ptr obj = (CORBA::Object*)_arg->proxyValue()->object();
517
 
      // TODO: Handle None here
518
 
      // if ( _arg == Py_None )
519
 
      // {
520
 
      // _any <<= CORBA::Any::from_object( obj, _tc->name() );
521
 
      // return true;
522
 
      //}
523
 
 
524
 
      if ( !obj->_is_a( _tc->id() ) )
525
 
      {
526
 
        KSUtil::castingError( context, obj->_repoid(), _tc->id() );
527
 
        return false;
528
 
      }
529
 
      _any <<= CORBA::Any::from_object( obj, _tc->name() );
530
 
      return true;
531
 
    }
532
 
  case CORBA::tk_sequence:
533
 
    {
534
 
      if ( !KSUtil::checkType( context, _arg, KSValue::ListType ) )
535
 
        return false;
536
 
 
537
 
      const CORBA::TypeCode_var ctc = _tc->content_type();
538
 
 
539
 
      CORBA::ULong len = _arg->listValue().count();
540
 
      assert( _any.seq_put_begin( len ) );
541
 
      QValueList<KSValue::Ptr>::Iterator it = _arg->listValue().begin();
542
 
      for ( CORBA::ULong i = 0; i < len; i++ )
543
 
      {
544
 
        if ( !ksPack( context, _any, *it, ctc ) )
545
 
          return false;
546
 
      }
547
 
      assert( _any.seq_put_end() );
548
 
      return true;
549
 
    }
550
 
  case CORBA::tk_struct:
551
 
    {
552
 
      if ( !KSUtil::checkType( context, _arg, KSValue::StructType ) )
553
 
        return false;
554
 
 
555
 
      assert( _any.struct_put_begin() );
556
 
      CORBA::ULong len = _tc->member_count();
557
 
      for( CORBA::ULong i = 0; i < len; i++ )
558
 
      {
559
 
        const CORBA::TypeCode_var ntc = _tc->member_type( i );
560
 
        KSValue::Ptr v = _arg->structValue()->member( context, _tc->member_name( i ) );
561
 
        if ( !v )
562
 
        {
563
 
          QString tmp( i18n("The struct misses the member %1") );
564
 
          context.setException( new KSException( "MissingMember", tmp.arg( _tc->member_name( i ) ) ) );
565
 
          return false;
566
 
        }
567
 
        if ( !ksPack( context, _any, v, ntc ) )
568
 
          return false;
569
 
      }
570
 
      assert( _any.struct_put_end() );
571
 
      return true;
572
 
    }
573
 
  case CORBA::tk_union:
574
 
  {
575
 
    // TODO
576
 
    return false;
577
 
      /*
578
 
    cerr << "Packing Union " << _tc->id() << endl;
579
 
 
580
 
    CORBA::TypeCode_var dt = _tc->discriminator_type();
581
 
    CORBA::Long defidx = _tc->default_index();
582
 
    assert( _any.union_put_begin() );
583
 
 
584
 
    bool found = false;
585
 
    CORBA::ULong idx;
586
 
    PyObject* disc = PyObject_GetAttrString( _arg, "d" );
587
 
    if ( disc == 0L || disc == Py_None )
588
 
    {
589
 
      cerr << "ERROR: Can not pack union " << _tc->id() << " because the descriminator is not set" << endl;
590
 
      return 0L;
591
 
    }
592
 
    for ( idx = 0; idx < _tc->member_count(); idx++ )
593
 
    {
594
 
      if ( defidx != -1 && (CORBA::ULong) defidx == idx )
595
 
        continue;
596
 
 
597
 
      CORBA::Any_var lany = _tc->member_label( idx );
598
 
      PyObject* label = parseResult( *lany, dt );
599
 
      if ( label == 0L )
600
 
      {
601
 
        cerr << "ERROR: Could not extract label of union " << _tc->id() << endl;
602
 
        return 0L;
603
 
      }
604
 
      if ( PyObject_Compare( label, disc ) == 0 )
605
 
      {
606
 
        found = true;
607
 
        Py_DECREF( label );
608
 
        break;
609
 
      }
610
 
      Py_DECREF( label );
611
 
    }
612
 
 
613
 
    if ( !parseResult( _any, disc, dt ) )
614
 
    {
615
 
      cerr << "ERROR: Could not pack discriminator for union " << _tc->id() << endl;
616
 
      return 0L;
617
 
    }
618
 
 
619
 
    PyObject* value = PyObject_GetAttrString( _arg, "v" );
620
 
 
621
 
    if ( found )
622
 
    {
623
 
      CORBA::TypeCode_var mtype = _tc->member_type( idx );
624
 
      assert( _any.union_put_selection( idx ) );
625
 
      if ( !parseResult( _any, value, mtype ) )
626
 
      {
627
 
        cerr << "ERROR: Could not pack value for union " << _tc->id() << endl;
628
 
        return 0L;
629
 
      }
630
 
    }
631
 
    else if ( defidx == -1 )
632
 
    {
633
 
      // Do not pack any value at all
634
 
    }
635
 
    else
636
 
    {
637
 
      CORBA::TypeCode_var mtype = _tc->member_type( idx );
638
 
      assert( _any.union_put_selection( idx ) );
639
 
      if ( !parseResult( _any, value, mtype ) )
640
 
      {
641
 
        cerr << "ERROR: Could not pack value for union " << _tc->id() << endl;
642
 
        return 0L;
643
 
      }
644
 
    }
645
 
 
646
 
    assert( _any.union_put_end() );
647
 
 
648
 
    return true; */
649
 
  }
650
 
  case CORBA::tk_alias:
651
 
  {
652
 
    const CORBA::TypeCode_var ntc = _tc->content_type();
653
 
    return ksPack( context, _any, _arg, ntc );
654
 
  }
655
 
  /**
656
 
   * Not handled here ...
657
 
   * tk_array, tk_alias, tk_except,
658
 
   */
659
 
  /* if ( _t == CORBA::_tc_wchar )
660
 
  if ( _t == CORBA::_tc_wstring )
661
 
  if ( _t == CORBA::_tc_Principal )
662
 
  if ( _t == CORBA::_tc_Context ) */
663
 
  default:
664
 
    assert( 0 );
665
 
  }
666
 
}
667
 
 
668
 
/**************************************
669
 
 *
670
 
 * KSCorbaFunc
671
 
 *
672
 
 **************************************/
673
 
 
674
 
KSCorbaFunc::KSCorbaFunc( KSModule* m, KSParseNode* node )
675
 
  : KSFunction( m ), m_node( node )
676
 
{
677
 
  m_name = node->getIdent();
678
 
}
679
 
 
680
 
bool KSCorbaFunc::init( KSContext& context )
681
 
{
682
 
  // Did we already initialize ?
683
 
  if ( !m_node )
684
 
    return true;
685
 
 
686
 
  KSContext d( context );
687
 
 
688
 
  // Get the return type
689
 
  ASSERT( m_node->branch1() );
690
 
  if ( !m_node->branch1()->eval( d ) )
691
 
  {
692
 
    context.setException( d );
693
 
    return false;
694
 
  }
695
 
 
696
 
  KSTypeCode::Ptr tc = KSTypeCode::typeCode( context, d.value() );
697
 
  if ( !tc )
698
 
    return false;
699
 
 
700
 
  setReturnTypeCode( tc );
701
 
 
702
 
  ref();
703
 
  d.setValue( new KSValue( this ) );
704
 
 
705
 
  ASSERT( d.value() && d.value()->type() == KSValue::FunctionType );
706
 
  // Get the list of parameters
707
 
  if ( m_node->branch2() )
708
 
  {
709
 
    if ( !m_node->branch2()->eval( d ) )
710
 
    {
711
 
      context.setException( d );
712
 
      return false;
713
 
    }
714
 
  }
715
 
 
716
 
  // Get the list of exceptions
717
 
  if ( m_node->branch3() )
718
 
  {
719
 
    if ( !m_node->branch3()->eval( d ) )
720
 
    {
721
 
      context.setException( d );
722
 
      return false;
723
 
    }
724
 
  }
725
 
 
726
 
  // Save memory since the code is no longer needed
727
 
  m_node->clear();
728
 
  m_node = 0;
729
 
 
730
 
  return true;
731
 
}
732
 
 
733
 
bool KSCorbaFunc::call( KSContext& context )
734
 
{
735
 
  QValueList<KSValue::Ptr>& args = context.value()->listValue();
736
 
  QValueList<KSValue::Ptr>::Iterator it = args.begin();
737
 
 
738
 
  if ( !init( context) )
739
 
    return false;
740
 
 
741
 
  uint params = m_parameters.count();
742
 
  // "params+1" because the object itself is passes as first parameter
743
 
  if ( !KSUtil::checkArgumentsCount( context, params + 1, m_name ) )
744
 
    return false;
745
 
 
746
 
  // Find the object
747
 
  if ( !KSUtil::checkType( context, *it, KSValue::ProxyType ) )
748
 
    return false;
749
 
 
750
 
  KSProxy* proxy = (*it)->proxyValue();
751
 
  CORBA::Object_ptr obj = (CORBA::Object*)proxy->object();
752
 
  ++it;
753
 
 
754
 
  CORBA::Request_var req = obj->_request( m_name );
755
 
  req->result()->value()->type( (CORBA::TypeCode*)m_returnTypeCode->tc() );
756
 
 
757
 
  // Pack the argument
758
 
  int i = 0;
759
 
  QValueList<Parameter>::Iterator pit = m_parameters.begin();
760
 
  for( ; pit != m_parameters.end(); ++pit, ++it, ++i )
761
 
  {
762
 
    CORBA::Any any( (CORBA::TypeCode*)(*pit).typecode->tc(), (void*)NULL );
763
 
    if ( !ksPack( context, any, *it, (CORBA::TypeCode*)(*pit).typecode->tc() ) )
764
 
      return false;
765
 
    if ( (*pit).mode == T_IN )
766
 
      req->add_in_arg( (*pit).name.ascii() ) = any;
767
 
    else if ( (*pit).mode == T_INOUT )
768
 
      req->add_inout_arg( (*pit).name.ascii() ) = any;
769
 
    else if ( (*pit).mode == T_OUT )
770
 
    {
771
 
      req->add_out_arg( (*pit).name.ascii() );
772
 
      req->arguments()->item( i )->value()->type( (CORBA::TypeCode*)(*pit).typecode->tc() );
773
 
    }
774
 
  }
775
 
 
776
 
  // Invoke the method
777
 
  req->invoke();
778
 
 
779
 
  // Check for exceptions
780
 
  if ( req->env()->exception() )
781
 
  {
782
 
    CORBA::Exception *_ex = req->env()->exception();
783
 
    CORBA::UnknownUserException *_uuex = CORBA::UnknownUserException::_downcast( _ex );
784
 
    if( _uuex )
785
 
    {
786
 
      KSValue* s = context.interpreter()->repoidImplementation( _uuex->_except_repoid() );
787
 
      if ( s && s->type() == KSValue::StructClassType )
788
 
      {
789
 
        KSTypeCode::Ptr tc = KSTypeCode::typeCode( context, s );
790
 
        if ( !tc )
791
 
          return false;
792
 
 
793
 
        KSValue::Ptr v = new KSValue;
794
 
        // Unpack the exception
795
 
        if ( !ksUnpack( context, v, _uuex->exception( (CORBA::TypeCode*)tc->tc() ), (CORBA::TypeCode*)tc->tc() ) )
796
 
          return false;
797
 
 
798
 
        // Emit a custom exception
799
 
        s->ref();
800
 
        context.setException( new KSException( s, v ) );
801
 
        return false;
802
 
      }
803
 
    }
804
 
 
805
 
    // Raise a default excpetion
806
 
    QString tmp( i18n("An unexpected CORBA exception occured\n%1") );
807
 
    context.setException( new KSException( "CORBAException", tmp.arg( _ex->_repoid() ), -1 ) );
808
 
    return false;
809
 
  }
810
 
 
811
 
  // Unpack out/inout parameters
812
 
  i = 0;
813
 
  // Go to first parameter
814
 
  it = args.begin();
815
 
  ++it;
816
 
  pit = m_parameters.begin();
817
 
  for( ; pit != m_parameters.end(); ++pit, ++it, ++i )
818
 
  {
819
 
    if ( (*pit).mode == T_INOUT || (*pit).mode == T_OUT )
820
 
    {
821
 
      if ( !ksUnpack( context, *it, *req->arguments()->item( i )->value(), (CORBA::TypeCode*)(*pit).typecode->tc() ) )
822
 
        return false;
823
 
    }
824
 
  }
825
 
 
826
 
  // Unpack the return value
827
 
  KSValue::Ptr v = new KSValue;
828
 
  if ( !ksUnpack( context, v, *req->result()->value(), (CORBA::TypeCode*)m_returnTypeCode->tc() ) )
829
 
    return false;
830
 
  context.setValue( v );
831
 
 
832
 
  printf("YEAH, back from CORBA\n");
833
 
 
834
 
  return true;
835
 
}
836
 
 
837
 
void KSCorbaFunc::addParameter( ParameterMode m, const QString& name, const KSTypeCode::Ptr &tc )
838
 
{
839
 
  Parameter p;
840
 
  p.mode = m;
841
 
  p.name = name;
842
 
  p.typecode = tc;
843
 
  m_parameters.append( p );
844
 
}
845
 
 
846
 
void KSCorbaFunc::addException( const KSValue::Ptr& exc )
847
 
{
848
 
  m_exceptions.append( exc );
849
 
}
850
 
 
851
 
void KSCorbaFunc::setReturnTypeCode( const KSTypeCode::Ptr& tc )
852
 
{
853
 
  m_returnTypeCode = tc;
854
 
}
855
 
 
856
 
#endif