summaryrefslogtreecommitdiffstats
path: root/korundum/rubylib/rbtdeconfig_compiler/rbtdeconfig_compiler.cpp
blob: f1f44f70a654aea25a6b8f5c0c8b355739aa2dca (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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
/*
    This file is part of KDE.

    Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
    Copyright (c) 2003 Waldo Bastian <bastian@kde.org>
    Copyright (c) 2003 Zack Rusin <zack@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tqfile.h>
#include <tqtextstream.h>
#include <tqdom.h>
#include <tqregexp.h>

#include <tdeaboutdata.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdecmdlineargs.h>
#include <tdeglobal.h>
#include <tdeconfig.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>

#include <iostream>

static const TDECmdLineOptions options[] =
{
  { "d", 0, 0 },
  { "directory <dir>", I18N_NOOP("Directory to generate files in"), "." },
  { "+file.kcfg", I18N_NOOP("Input kcfg XML file"), 0 },
  { "+file.kcfgc", I18N_NOOP("Code generation options file"), 0 },
  TDECmdLineLastOption
};


bool globalEnums;
bool itemAccessors;
TQStringList allNames;
TQRegExp *validNameRegexp;

class CfgEntry
{
  public:
    struct Choice
    {
      TQString name;
      TQString label;
      TQString whatsThis;
    };

    CfgEntry( const TQString &group, const TQString &type, const TQString &key,
              const TQString &name, const TQString &label,
              const TQString &whatsThis, const TQString &code,
              const TQString &defaultValue, const TQValueList<Choice> &choices,
              bool hidden )
      : mGroup( group ), mType( type ), mKey( key ), mName( name ),
        mLabel( label ), mWhatsThis( whatsThis ), mCode( code ),
        mDefaultValue( defaultValue ),
        mChoices( choices ), mHidden( hidden )
    {
    }

    void setGroup( const TQString &group ) { mGroup = group; }
    TQString group() const { return mGroup; }

    void setType( const TQString &type ) { mType = type; }
    TQString type() const { return mType; }

    void setKey( const TQString &key ) { mKey = key; }
    TQString key() const { return mKey; }

    void setName( const TQString &name ) { mName = name; }
    TQString name() const { return mName; }

    void setLabel( const TQString &label ) { mLabel = label; }
    TQString label() const { return mLabel; }

    void setWhatsThis( const TQString &whatsThis ) { mWhatsThis = whatsThis; }
    TQString whatsThis() const { return mWhatsThis; }

    void setDefaultValue( const TQString &d ) { mDefaultValue = d; }
    TQString defaultValue() const { return mDefaultValue; }

    void setCode( const TQString &d ) { mCode = d; }
    TQString code() const { return mCode; }

    void setMinValue( const TQString &d ) { mMin = d; }
    TQString minValue() const { return mMin; }

    void setMaxValue( const TQString &d ) { mMax = d; }
    TQString maxValue() const { return mMax; }

    void setParam( const TQString &d ) { mParam = d; }
    TQString param() const { return mParam; }

    void setParamName( const TQString &d ) { mParamName = d; }
    TQString paramName() const { return mParamName; }

    void setParamType( const TQString &d ) { mParamType = d; }
    TQString paramType() const { return mParamType; }

    void setChoices( const TQValueList<Choice> &d ) { mChoices = d; }
    TQValueList<Choice> choices() const { return mChoices; }

    void setParamValues( const TQStringList &d ) { mParamValues = d; }
    TQStringList paramValues() const { return mParamValues; }

    void setParamDefaultValues( const TQStringList &d ) { mParamDefaultValues = d; }
    TQString paramDefaultValue(int i) const { return mParamDefaultValues[i]; }

    void setParamMax( int d ) { mParamMax = d; }
    int paramMax() const { return mParamMax; }

    bool hidden() const { return mHidden; }

    void dump() const
    {
      kdDebug() << "<entry>" << endl;
      kdDebug() << "  group: " << mGroup << endl;
      kdDebug() << "  type: " << mType << endl;
      kdDebug() << "  key: " << mKey << endl;
      kdDebug() << "  name: " << mName << endl;
      kdDebug() << "  label: " << mLabel << endl;
// whatsthis
      kdDebug() << "  code: " << mCode << endl;
//      kdDebug() << "  values: " << mValues.join(":") << endl;
      
      if (!param().isEmpty())
      {
        kdDebug() << "  param name: "<< mParamName << endl;
        kdDebug() << "  param type: "<< mParamType << endl;
        kdDebug() << "  paramvalues: " << mParamValues.join(":") << endl;
      }
      kdDebug() << "  default: " << mDefaultValue << endl;
      kdDebug() << "  hidden: " << mHidden << endl;
      kdDebug() << "  min: " << mMin << endl;
      kdDebug() << "  max: " << mMax << endl;
      kdDebug() << "</entry>" << endl;
    }

  private:
    TQString mGroup;
    TQString mType;
    TQString mKey;
    TQString mName;
    TQString mLabel;
    TQString mWhatsThis;
    TQString mCode;
    TQString mDefaultValue;
    TQString mParam;
    TQString mParamName;
    TQString mParamType;
    TQValueList<Choice> mChoices;
    TQStringList mParamValues;
    TQStringList mParamDefaultValues;
    int mParamMax;
    bool mHidden;
    TQString mMin;
    TQString mMax;
};


static TQString varName(const TQString &n)
{
  TQString result = "@"+n;
  result[1] = result[1].lower();
  return result;
}

static TQString enumName(const TQString &n)
{
  TQString result = "Enum"+n;
  result[4] = result[4].upper();
  return result;
}

static TQString enumValue(const TQString &n)
{
  TQString result = n;
  result[0] = result[0].upper();
  return result;
}

static TQString setFunction(const TQString &n)
{
  TQString result = "set"+n;
  result[3] = result[3].upper();
  return result;
}


static TQString getFunction(const TQString &n)
{
  TQString result = n;
  result[0] = result[0].lower();
  return result;
}


static void addQuotes( TQString &s )
{
  if ( s.left( 1 ) != "\"" ) s.prepend( "\"" );
  if ( s.right( 1 ) != "\"" ) s.append( "\"" );
}

static TQString quoteString( const TQString &s )
{
  TQString r = s;
  r.replace( "\\", "\\\\" );
  r.replace( "\"", "\\\"" );
  r.replace( "\r", "" );
  r.replace( "\n", "\\n\"\n\"" );
  return "\"" + r + "\"";
}

static TQString literalString( const TQString &s )
{
  bool isAscii = true;
  for(int i = s.length(); i--;)
     if (s[i].unicode() > 127) isAscii = false;
  
  return quoteString(s);

//  if (isAscii)
//     return "TQString::fromLatin1( " + quoteString(s) + " )";
//  else
//     return "TQString::fromUtf8( " + quoteString(s) + " )";
}

static TQString dumpNode(const TQDomNode &node)
{
  TQString msg;
  TQTextStream s(&msg, IO_WriteOnly );
  node.save(s, 0);

  msg = msg.simplifyWhiteSpace();
  if (msg.length() > 40)
    return msg.left(37)+"...";
  return msg;
}

static TQString filenameOnly(TQString path)
{
   int i = path.findRev('/');
   if (i >= 0)
      return path.mid(i+1);
   return path;
}

static void preProcessDefault( TQString &defaultValue, const TQString &name,
                               const TQString &type,
                               const TQValueList<CfgEntry::Choice> &choices,
                               TQString &code )
{
    if ( type == "String" && !defaultValue.isEmpty() ) {
      defaultValue = literalString(defaultValue);

    } else if ( type == "Path" && !defaultValue.isEmpty() ) {
      defaultValue = literalString( defaultValue );

    } else if ( type == "StringList" && !defaultValue.isEmpty() ) {
      TQTextStream rb( &code, IO_WriteOnly | IO_Append );
      if (!code.isEmpty())
         rb << endl;

//      rb << "  TQStringList default" << name << ";" << endl;
      rb << "        default" << name << " = []" << endl;
      TQStringList defaults = TQStringList::split( ",", defaultValue );
      TQStringList::ConstIterator it;
      for( it = defaults.begin(); it != defaults.end(); ++it ) {
        rb << "        default" << name << " << \"" << *it << "\""
            << endl;
      }
      defaultValue = "default" + name;

    } else if ( type == "Color" && !defaultValue.isEmpty() ) {
      TQRegExp colorRe("\\d+,\\s*\\d+,\\s*\\d+");
      if (colorRe.exactMatch(defaultValue))
      {
        defaultValue = "TQt::Color.new( " + defaultValue + " )";
      }
      else
      {
        defaultValue = "TQt::Color.new( \"" + defaultValue + "\" )";
      }

    } else if ( type == "Enum" ) {
      if ( !globalEnums ) {
        TQValueList<CfgEntry::Choice>::ConstIterator it;
        for( it = choices.begin(); it != choices.end(); ++it ) {
          if ( (*it).name == defaultValue ) {
            defaultValue.prepend( enumName(name) + "_");
            break;
          }
        }
      }

    } else if ( type == "IntList" ) {
      TQTextStream rb( &code, IO_WriteOnly | IO_Append );
      if (!code.isEmpty())
         rb << endl;

      rb << "        default" << name << " = []" << endl;
      TQStringList defaults = TQStringList::split( ",", defaultValue );
      TQStringList::ConstIterator it;
      for( it = defaults.begin(); it != defaults.end(); ++it ) {
        rb << "        default" << name << " << " << *it << ""
            << endl;
      }
      defaultValue = "default" + name;
    }
}


CfgEntry *parseEntry( const TQString &group, const TQDomElement &element )
{
  bool defaultCode = false;
  TQString type = element.attribute( "type" );
  TQString name = element.attribute( "name" );
  TQString key = element.attribute( "key" );
  TQString hidden = element.attribute( "hidden" );
  TQString label;
  TQString whatsThis;
  TQString defaultValue;
  TQString code;
  TQString param;
  TQString paramName;
  TQString paramType;
  TQValueList<CfgEntry::Choice> choices;
  TQStringList paramValues;
  TQStringList paramDefaultValues;
  TQString minValue;
  TQString maxValue;
  int paramMax = 0;

  TQDomNode n;
  for ( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    TQDomElement e = n.toElement();
    TQString tag = e.tagName();
    if ( tag == "label" ) label = e.text();
    else if ( tag == "whatsthis" ) whatsThis = e.text();
    else if ( tag == "min" ) minValue = e.text();
    else if ( tag == "max" ) maxValue = e.text();
    else if ( tag == "code" ) code = e.text();
    else if ( tag == "parameter" )
    {
      param = e.attribute( "name" );
      paramType = e.attribute( "type" );
      if ( param.isEmpty() ) {
        kdError() << "Parameter must have a name: " << dumpNode(e) << endl;
        return 0;
      }
      if ( paramType.isEmpty() ) {
        kdError() << "Parameter must have a type: " << dumpNode(e) << endl;
        return 0;
      }
      if ((paramType == "Int") || (paramType == "UInt"))
      {
         bool ok;
         paramMax = e.attribute("max").toInt(&ok);
         if (!ok)
         {
           kdError() << "Integer parameter must have a maximum (e.g. max=\"0\"): " << dumpNode(e) << endl;
           return 0;
         }
      }
      else if (paramType == "Enum")
      {
         TQDomNode n2;
         for ( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
           TQDomElement e2 = n2.toElement();
           if (e2.tagName() == "values")
           {
             TQDomNode n3;
             for ( n3 = e2.firstChild(); !n3.isNull(); n3 = n3.nextSibling() ) {
               TQDomElement e3 = n3.toElement();
               if (e3.tagName() == "value")
               {
                  paramValues.append( e3.text() );
               }
             }
             break;
           }
         }
         if (paramValues.isEmpty())
         {
           kdError() << "No values specified for parameter '" << param << "'." << endl;
           return 0;
         }
         paramMax = paramValues.count()-1;
      }
      else
      {
        kdError() << "Parameter '" << param << "' has type " << paramType << " but must be of type int, uint or Enum." << endl;
        return 0;
      }
    }
    else if ( tag == "default" )
    {
      if (e.attribute("param").isEmpty())
      {
        defaultValue = e.text();
        if (e.attribute( "code" ) == "true")
          defaultCode = true;
      }
    }
    else if ( tag == "choices" ) {
      TQDomNode n2;
      for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
        TQDomElement e2 = n2.toElement();
        if ( e2.tagName() == "choice" ) {
          TQDomNode n3;
          CfgEntry::Choice choice;
          choice.name = e2.attribute( "name" );
          if ( choice.name.isEmpty() ) {
            kdError() << "Tag <choice> requires attribute 'name'." << endl;
          }
          for( n3 = e2.firstChild(); !n3.isNull(); n3 = n3.nextSibling() ) {
            TQDomElement e3 = n3.toElement();
            if ( e3.tagName() == "label" ) choice.label = e3.text();
            if ( e3.tagName() == "whatsthis" ) choice.whatsThis = e3.text();
          }
          choices.append( choice );
        }
      }
    }
  }

  bool nameIsEmpty = name.isEmpty();
  if ( nameIsEmpty && key.isEmpty() ) {
    kdError() << "Entry must have a name or a key: " << dumpNode(element) << endl;
    return 0;
  }

  if ( key.isEmpty() ) {
    key = name;
  }

  if ( nameIsEmpty ) {
    name = key;
    name.replace( " ", TQString::null );
  } else if ( name.contains( ' ' ) ) {
    kdWarning()<<"Entry '"<<name<<"' contains spaces! <name> elements can't contain speces!"<<endl;
    name.remove( ' ' );
  }

  if (name.contains("$("))
  {
    if (param.isEmpty())
    {
      kdError() << "Name may not be parameterized: " << name << endl;
      return 0;
    }
  }
  else
  {
    if (!param.isEmpty())
    {
      kdError() << "Name must contain '$(" << param << ")': " << name << endl;
      return 0;
    }
  }

  if ( label.isEmpty() ) {
    label = key;
  }

  if ( type.isEmpty() ) type = "String"; // XXX : implicit type might be bad

  if (!param.isEmpty())
  {
    // Adjust name
    paramName = name;
    name.replace("$("+param+")", TQString::null);
    // Lookup defaults for indexed entries
    for(int i = 0; i <= paramMax; i++)
    {
      paramDefaultValues.append(TQString::null);
    }

    TQDomNode n;
    for ( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
      TQDomElement e = n.toElement();
      TQString tag = e.tagName();
      if ( tag == "default" )
      {
        TQString index = e.attribute("param");
        if (index.isEmpty())
           continue;

        bool ok;
        int i = index.toInt(&ok);
        if (!ok)
        {
          i = paramValues.findIndex(index);
          if (i == -1)
          {
            kdError() << "Index '" << index << "' for default value is unknown." << endl;
            return 0;
          }
        }

        if ((i < 0) || (i > paramMax))
        {
          kdError() << "Index '" << i << "' for default value is out of range [0, "<< paramMax<<"]." << endl;
          return 0;
        }

        TQString tmpDefaultValue = e.text();

        if (e.attribute( "code" ) != "true")
           preProcessDefault(tmpDefaultValue, name, type, choices, code);

        paramDefaultValues[i] = tmpDefaultValue;
      }
    }
  }

  if (!validNameRegexp->exactMatch(name))
  {
    if (nameIsEmpty)
      kdError() << "The key '" << key << "' can not be used as name for the entry because "
                   "it is not a valid name. You need to specify a valid name for this entry." << endl;
    else
      kdError() << "The name '" << name << "' is not a valid name for an entry." << endl;
    return 0;
  }

  if (allNames.contains(name))
  {
    if (nameIsEmpty)
      kdError() << "The key '" << key << "' can not be used as name for the entry because "
                   "it does not result in a unique name. You need to specify a unique name for this entry." << endl;
    else
      kdError() << "The name '" << name << "' is not unique." << endl;
    return 0;
  }
  allNames.append(name);

  if (!defaultCode)
  {
    preProcessDefault(defaultValue, name, type, choices, code);
  }

  CfgEntry *result = new CfgEntry( group, type, key, name, label, whatsThis,
                                   code, defaultValue, choices,
                                   hidden == "true" );
  if (!param.isEmpty())
  {
    result->setParam(param);
    result->setParamName(paramName);
    result->setParamType(paramType);
    result->setParamValues(paramValues);
    result->setParamDefaultValues(paramDefaultValues);
    result->setParamMax(paramMax);
  }
  result->setMinValue(minValue);
  result->setMaxValue(maxValue);

  return result;
}

/**
  Return parameter declaration for given type.
*/
TQString param( const TQString &type )
{
    if ( type == "String" )           return "const TQString &";
    else if ( type == "StringList" )  return "const TQStringList &";
    else if ( type == "Font" )        return "const TQFont &";
    else if ( type == "Rect" )        return "const TQRect &";
    else if ( type == "Size" )        return "const TQSize &";
    else if ( type == "Color" )       return "const TQColor &";
    else if ( type == "Point" )       return "const TQPoint &";
    else if ( type == "Int" )         return "int";
    else if ( type == "UInt" )        return "uint";
    else if ( type == "Bool" )        return "bool";
    else if ( type == "Double" )      return "double";
    else if ( type == "DateTime" )    return "const TQDateTime &";
    else if ( type == "Int64" )       return "TQ_INT64";
    else if ( type == "UInt64" )      return "TQ_UINT64";
    else if ( type == "IntList" )     return "const TQValueList<int> &";
    else if ( type == "Enum" )        return "int";
    else if ( type == "Path" )        return "const TQString &";
    else if ( type == "Password" )    return "const TQString &";
    else {
        kdError() <<"rbtdeconfig_compiler does not support type \""<< type <<"\""<<endl;
        return "TQString"; //For now, but an assert would be better
    }
}

/**
  Actual Ruby initializer value to give a type.
*/
TQString rbType( const TQString &type )
{
    if ( type == "String" )           return "\"\"";
    else if ( type == "StringList" )  return "[]";
    else if ( type == "Font" )        return "TQt::Font.new";
    else if ( type == "Rect" )        return "TQt::Rect.new";
    else if ( type == "Size" )        return "TQt::Size.new";
    else if ( type == "Color" )       return "TQt::Color.new";
    else if ( type == "Point" )       return "TQt::Point.new";
    else if ( type == "Int" )         return "0";
    else if ( type == "UInt" )        return "0";
    else if ( type == "Bool" )        return "false, 42";
    else if ( type == "Double" )      return "0.0";
    else if ( type == "DateTime" )    return "TQt::DateTime.new";
    else if ( type == "Int64" )       return "0";
    else if ( type == "UInt64" )      return "0";
    else if ( type == "IntList" )     return "[]";
    else if ( type == "Enum" )        return "0";
    else if ( type == "Path" )        return "\"\"";
    else if ( type == "Password" )    return "\"\"";
    else {
        kdError()<<"rbtdeconfig_compiler does not support type \""<< type <<"\""<<endl;
        return "nil"; //For now, but an assert would be better
    }
}

TQString defaultValue( const TQString &type )
{
    if ( type == "String" )           return "\"\""; // Use empty string, not null string!
    else if ( type == "StringList" )  return "[]";
    else if ( type == "Font" )        return "KDE::GlobalSettings.generalFont()";
    else if ( type == "Rect" )        return "TQt::Rect.new()";
    else if ( type == "Size" )        return "TQt::Size.new()";
    else if ( type == "Color" )       return "TQt::Color.new(128, 128, 128)";
    else if ( type == "Point" )       return "TQt::Point.new()";
    else if ( type == "Int" )         return "0";
    else if ( type == "UInt" )        return "0";
    else if ( type == "Bool" )        return "false";
    else if ( type == "Double" )      return "0.0";
    else if ( type == "DateTime" )    return "TQt::DateTime.new()";
    else if ( type == "Int64" )       return "0";
    else if ( type == "UInt64" )      return "0";
    else if ( type == "IntList" )     return "[]";
    else if ( type == "Enum" )        return "0";
    else if ( type == "Path" )        return "\"\""; // Use empty string, not null string!
    else if ( type == "Password" )    return "\"\""; // Use empty string, not null string!
    else {
        kdWarning()<<"Error, rbtdeconfig_compiler doesn't support the \""<< type <<"\" type!"<<endl;
        return "String"; //For now, but an assert would be better
    }
}

TQString itemType( const TQString &type )
{
  TQString t;

  t = type;
  t.replace( 0, 1, t.left( 1 ).upper() );
  return t;
}

static TQString itemVar(const CfgEntry *e)
{
  if (itemAccessors)
     return varName( e->name() ) + "Item";

  return "item" + e->name();

}

TQString newItem( const TQString &type, const TQString &name, const TQString &key,
                 const TQString &defaultValue, const TQString &param = TQString::null)
{
  TQString t = "Item" + itemType( type ) +
              ".new( currentGroup(), " + key + ", " + varName( name ) + param;
  if ( type == "Enum" ) {
    t += ".toInt";  
    t += ", values" + name;
  } else if ( type == "Path" ) {
    t += ".toString";  
  } else if ( type == "Int64" ) {
    t += ".toLongLong";  
  } else {
    t += ".to" + itemType( type );
  }
  if ( !defaultValue.isEmpty() ) {
    t += ", ";
    if ( type == "String" ) t += defaultValue;
    else t+= defaultValue;
  }
  t += " )";

  return t;
}

TQString addItem( const TQString &type, const TQString &name, const TQString &key,
                 const TQString &defaultValue, const TQString &param = TQString::null,
                 const TQString &paramName = TQString::null )
{
  TQString t = "addItem" + itemType( type ) +
              "( " + key + ", " + varName( name ) + param;
  if ( type == "Enum" ) t += ", values" + name;
  if ( !defaultValue.isEmpty() ) {
    t += ", ";
    if ( type == "String" ) t += defaultValue;
    else if ( type == "Enum" ) t += enumValue(defaultValue);
    else t+= defaultValue;
  }
  
  if (!paramName.isNull()) {
    t += ", \"" + paramName + "\"";
  }
  
  t += " )";

  return t;
}

TQString paramString(const TQString &s, const CfgEntry *e, int i)
{
  TQString result = s;
  TQString needle = "$("+e->param()+")";
  if (result.contains(needle))
  {
    TQString tmp;
    if (e->paramType() == "Enum")
    {
      tmp = e->paramValues()[i];
    }
    else
    {
      tmp = TQString::number(i);
    }

    result.replace(needle, tmp);
  }
  return result;
}

TQString paramString(const TQString &group, const TQStringList &parameters)
{
  TQString paramString = group;
  TQString arguments;
  int i = 0;
  for( TQStringList::ConstIterator it = parameters.begin();
       it != parameters.end(); ++it)
  {
     if (paramString.contains("$("+*it+")"))
     {
       i++;
       paramString.replace("$("+*it+")", "%s");
       if (i > 1) {
         arguments += ", ";
       }
       arguments += " @param"+*it;
     }
  }
  if (arguments.isEmpty())
    return "\""+group+"\"";

  if (i == 1) {
    return "\""+paramString+"\" % "+arguments;
  } else {
    return "\""+paramString+"\" % ["+arguments+"]";
  }
}

/* int i is the value of the parameter */
TQString userTextsFunctions( CfgEntry *e, TQString itemVarStr=TQString::null, TQString i=TQString::null )
{
  TQString txt;
  if (itemVarStr.isNull()) itemVarStr=itemVar(e);
  if ( !e->label().isEmpty() ) {
    txt += "        " + itemVarStr + ".setLabel( i18n(";
    if ( !e->param().isEmpty() )
      txt += quoteString(e->label().replace("$("+e->param()+")", i));
    else 
      txt+= quoteString(e->label());
    txt+= ") )\n";
  }
  if ( !e->whatsThis().isEmpty() ) {
    txt += "        " + itemVarStr + ".setWhatsThis( i18n(";
    if ( !e->param().isEmpty() )
      txt += quoteString(e->whatsThis().replace("$("+e->param()+")", i));
    else 
      txt+= quoteString(e->whatsThis());
    txt+=") )\n";
  }
  return txt;
}

int main( int argc, char **argv )
{
  TDEAboutData aboutData( "rbtdeconfig_compiler", I18N_NOOP("TDE .kcfg compiler"), "0.3",
    I18N_NOOP("Ruby TDEConfig Compiler") , TDEAboutData::License_LGPL );
  aboutData.addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" );
  aboutData.addAuthor( "Waldo Bastian", 0, "bastian@kde.org" );
  aboutData.addAuthor( "Zack Rusin", 0, "zack@kde.org" );
  aboutData.addCredit( "Reinhold Kainhofer", "Fix for parametrized entries", 
      "reinhold@kainhofer.com", "http://reinhold.kainhofer.com" );
  aboutData.addCredit( "Richard Dale", "Ruby port", 
      "Richard_Dale@tipitina.demon.co.uk", "" );

  TDECmdLineArgs::init( argc, argv, &aboutData );
  TDECmdLineArgs::addCmdLineOptions( options );

  TDEInstance app( &aboutData );

  TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

  if ( args->count() < 2 ) {
    kdError() << "Too few arguments." << endl;
    return 1;
  }
  if ( args->count() > 2 ) {
    kdError() << "Too many arguments." << endl;
    return 1;
  }

  validNameRegexp = new TQRegExp("[a-zA-Z_][a-zA-Z0-9_]*");

  TQString baseDir = TQFile::decodeName(args->getOption("directory"));
  if (!baseDir.endsWith("/"))
    baseDir.append("/");

  TQString inputFilename = args->url( 0 ).path();
  TQString codegenFilename = args->url( 1 ).path();

  if (!codegenFilename.endsWith(".kcfgc"))
  {
    kdError() << "Codegen options file must have extension .kcfgc" << endl;
    return 1;
  }
  TQString baseName = args->url( 1 ).fileName();
  baseName = baseName.left(baseName.length() - 6);

  KSimpleConfig codegenConfig( codegenFilename, true );

  TQString nameSpace = codegenConfig.readEntry("NameSpace");
  TQString className = codegenConfig.readEntry("ClassName");
  TQString inherits = codegenConfig.readEntry("Inherits");
  TQString visibility = codegenConfig.readEntry("Visibility");
  if (!visibility.isEmpty()) visibility+=" ";
  bool singleton = codegenConfig.readBoolEntry("Singleton", false);
  bool customAddons = codegenConfig.readBoolEntry("CustomAdditions");
  TQString memberVariables = codegenConfig.readEntry("MemberVariables");
  TQStringList headerIncludes = codegenConfig.readListEntry("IncludeFiles");
  TQStringList mutators = codegenConfig.readListEntry("Mutators");
  bool allMutators = false;
  if ((mutators.count() == 1) && (mutators[0].lower() == "true"))
     allMutators = true;
  itemAccessors = codegenConfig.readBoolEntry( "ItemAccessors", false );
  bool setUserTexts = codegenConfig.readBoolEntry( "SetUserTexts", false );

  globalEnums = codegenConfig.readBoolEntry( "GlobalEnums", false );

  TQFile input( inputFilename );

  TQDomDocument doc;
  TQString errorMsg;
  int errorRow;
  int errorCol;
  if ( !doc.setContent( &input, &errorMsg, &errorRow, &errorCol ) ) {
    kdError() << "Unable to load document." << endl;
    kdError() << "Parse error in " << args->url( 0 ).fileName() << ", line " << errorRow << ", col " << errorCol << ": " << errorMsg << endl;
    return 1;
  }

  TQDomElement cfgElement = doc.documentElement();

  if ( cfgElement.isNull() ) {
    kdError() << "No document in kcfg file" << endl;
    return 1;
  }

  TQString cfgFileName;
  bool cfgFileNameArg = false;
  TQStringList parameters;
  TQStringList includes;

  TQPtrList<CfgEntry> entries;
  entries.setAutoDelete( true );

  TQDomNode n;
  for ( n = cfgElement.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    TQDomElement e = n.toElement();

    TQString tag = e.tagName();

    if ( tag == "include" ) {
      TQString includeFile = e.text();
      if (!includeFile.isEmpty())
        includes.append(includeFile);

    } else if ( tag == "kcfgfile" ) {
      cfgFileName = e.attribute( "name" );
      cfgFileNameArg = e.attribute( "arg" ).lower() == "true";
      TQDomNode n2;
      for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
        TQDomElement e2 = n2.toElement();
        if ( e2.tagName() == "parameter" ) {
          parameters.append( e2.attribute( "name" ) );
        }
      }

    } else if ( tag == "group" ) {
      TQString group = e.attribute( "name" );
      if ( group.isEmpty() ) {
        kdError() << "Group without name" << endl;
        return 1;
      }
      TQDomNode n2;
      for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
        TQDomElement e2 = n2.toElement();
        if ( e2.tagName() != "entry" ) continue;
        CfgEntry *entry = parseEntry( group, e2 );
        if ( entry ) entries.append( entry );
        else {
          kdError() << "Can't parse entry." << endl;
          return 1;
        }
      }
    }
  }

  if ( inherits.isEmpty() ) inherits = "KDE::ConfigSkeleton";

  if ( className.isEmpty() ) {
    kdError() << "Class name missing" << endl;
    return 1;
  }

  if ( singleton && !parameters.isEmpty() ) {
    kdError() << "Singleton class can not have parameters" << endl;
    return 1;
  }

  if ( singleton && cfgFileNameArg)
  {
    kdError() << "Singleton class can not use filename as argument." << endl;
    return 1;
  }

  if ( !cfgFileName.isEmpty() && cfgFileNameArg)
  {
    kdError() << "Having both a fixed filename and a filename as argument is not possible." << endl;
    return 1;
  }

  if ( entries.isEmpty() ) {
    kdWarning() << "No entries." << endl;
  }

#if 0
  CfgEntry *cfg;
  for( cfg = entries.first(); cfg; cfg = entries.next() ) {
    cfg->dump();
  }
#endif

  TQString implementationFileName = baseName + ".rb";

  TQFile implementation( baseDir + implementationFileName );
  if ( !implementation.open( IO_WriteOnly ) ) {
    kdError() << "Can't open '" << implementationFileName << "for writing." << endl;
    return 1;
  }

  TQTextStream rb( &implementation );

  rb << "# This file is generated by rbtdeconfig_compiler from " << args->url(0).fileName() << "." << endl;
  rb << "# All changes you do to this file will be lost." << endl;
  rb << endl << "require 'Korundum'" << endl;
  
  if (singleton) {
    rb << "require 'singleton'" << endl;
  }
  
  rb << endl;

//  rb << "#ifndef " << ( !nameSpace.isEmpty() ? nameSpace.upper() + "_" : "" )
//    << className.upper() << "_H" << endl;
//  rb << "#define " << ( !nameSpace.isEmpty() ? nameSpace.upper() + "_" : "" )
//    << className.upper() << "_H" << endl << endl;

  // Includes
//  TQStringList::ConstIterator it;
//  for( it = headerIncludes.begin(); it != headerIncludes.end(); ++it ) {
//    rb << "#include <" << *it << ">" << endl;
//  }

  if ( headerIncludes.count() > 0 ) rb << endl;

//  rb << "#include <tdeconfigskeleton.h>" << endl << endl;

  if ( !nameSpace.isEmpty() )
    rb << "module " << nameSpace << endl << endl;

  // Class declaration header
  rb << "class " <<  className << " < " << inherits << endl;

  if (singleton) {
    rb << "    include Singleton" << endl << endl;
  }
  
  // enums
  CfgEntry *e;
  for( e = entries.first(); e; e = entries.next() ) {
    TQValueList<CfgEntry::Choice> choices = e->choices();
    if ( !choices.isEmpty() ) {
      TQStringList values;
      TQValueList<CfgEntry::Choice>::ConstIterator itChoice;
      for( itChoice = choices.begin(); itChoice != choices.end(); ++itChoice ) {
        if (globalEnums) {
          values.append( enumValue((*itChoice).name) );
        } else {
          values.append( enumName(e->name()) + "_" + (*itChoice).name );
        }
      }
      if (!globalEnums) {
        values.append( enumName(e->name()) + "_COUNT" );
      }
      int count = 0;
      for ( TQStringList::Iterator it = values.begin(); it != values.end(); ++it, count++ ) {
        rb << "    " << *it << " = " << count << endl;
      }
      rb << endl;
    }
    
    TQStringList values = e->paramValues();
    if ( !values.isEmpty() ) {
      int count = 0;
      for ( TQStringList::Iterator it = values.begin(); it != values.end(); ++it, count++ ) {
        if (globalEnums) {
          rb << "    " << enumValue(*it) << " = " << count << endl;
        } else {
          rb << "    " << enumName(e->param()) << "_" << *it << " = " << count << endl;
        }
      }
      if (!globalEnums) {
        rb << "    " << enumName(e->param()) << "_COUNT = " << count << endl;
      }
      rb << endl;
      
      rb << "    def " << enumName(e->param()) << "ToString(i)" << endl;
      rb << "        [";
      count = 0;
      for ( TQStringList::Iterator it = values.begin(); it != values.end(); ++it, count++ ) {
          if (count > 0) {
          rb << ", ";
        }
        
        rb << "\"" << *it << "\"";
      }
      
      rb << "].at(i)" << endl;
      rb << "    end" << endl;
    }
  }

  rb << endl;

  for( e = entries.first(); e; e = entries.next() ) {
    TQString n = e->name();
    TQString t = e->type();

    // Manipulator
    if (allMutators || mutators.contains(n))
    {
      rb << "    #" << endl;
      rb << "    #  Set " << e->label() << endl;
      rb << "    #" << endl;
      rb << "    def " << setFunction(n) << "( ";
      if (!e->param().isEmpty())
        rb  << " i, ";
      rb << " v )" << endl;
      rb << "        item = findItem( \"";
      if (!e->param().isEmpty()) {
        rb << e->paramName().replace("$("+e->param()+")", "%s") << "\" % ";
        if ( e->paramType() == "Enum" ) {
          rb << " ";
          if (globalEnums) 
            rb << enumName(e->param()) << "ToString(i)";
          else 
            rb << enumName(e->param()) << "ToString(i)";
        } else {
          rb << "i";
        }
      } else {
        rb << n << "\"";
	  }
      rb << " )" << endl;
      rb << "        if !item.immutable? " << endl;
      rb << "            item.property = " << varName(n);
      if (!e->param().isEmpty())
        rb << "[i]";
      rb << " = TQt::Variant.new( v )" << endl;
      rb << "        end" << endl;
      rb << "    end" << endl << endl;
    }

    // Accessor
    rb << "    #" << endl;
    rb << "    # Get " << e->label() << endl;
    rb << "    #" << endl;
    rb << "    def " << getFunction(n) << "(";
    if (!e->param().isEmpty())
      rb << " "  <<" i ";
    rb << ")" << endl;
    rb << "        " << varName(n); 
    if (!e->param().isEmpty()) rb << "[i]";
    rb << " = findItem( \"";
    if (!e->param().isEmpty()) {
      rb << e->paramName().replace("$("+e->param()+")", "%s") << "\" % ";
      if ( e->paramType() == "Enum" ) {
        rb << " ";
        if (globalEnums) 
          rb << enumName(e->param()) << "ToString(i)";
        else 
          rb << enumName(e->param()) << "ToString(i)";
      } else {
        rb << "i";
      }
    } else {
      rb << n << "\"";
    }
    rb << " ).property" << endl;
    rb << "        return " << varName(n);
    if (!e->param().isEmpty()) rb << "[i]";
    if ( e->type() == "Enum" ) {
	  rb << ".toInt" << endl;
    } else if ( e->type() == "Int64" ) {
	  rb << ".toLongLong" << endl;
    } else if ( e->type() == "Path" ) {
	  rb << ".toString" << endl;
	} else {
	  rb << ".to" << itemType( e->type() ) << endl;
	}
    rb << "    end" << endl;

    // Item accessor
    if ( itemAccessors ) {
      rb << endl;
      rb << "    #" << endl;
      rb << "    # Get Item object corresponding to " << n << "()"
        << endl;
      rb << "    #" << endl;
      rb << "    def "
        << getFunction( n ) << "Item(";
      if (!e->param().isEmpty()) {
        rb << " "  << " i ";
      }
      rb << ")" << endl;
      rb << "        return " << itemVar(e);
      if (!e->param().isEmpty()) rb << "[i]";
      rb << endl << "    end" << endl;
    }

    rb << endl;
  }


  if (customAddons)
  {
     rb << "    # Include custom additions" << endl;
  }


  // Constructor
  rb << "    def initialize( ";
  if (cfgFileNameArg)
     rb << " config" << (parameters.isEmpty() ? " " : ", ");
  for (TQStringList::ConstIterator it = parameters.begin();
       it != parameters.end(); ++it)
  {
     if (it != parameters.begin())
       rb << ",";
     rb << " " << *it;
  }
  rb << " )" << endl;

  rb << "        super(";
  if ( !cfgFileName.isEmpty() ) rb << " \"" << cfgFileName << "\" ";
  if ( cfgFileNameArg ) rb << " config ";
//  if ( !cfgFileName.isEmpty() ) rb << ") ";
  rb << ")" << endl;

  // Store parameters
  for (TQStringList::ConstIterator it = parameters.begin();
       it != parameters.end(); ++it)
  {
     rb << "        @param" << *it << " = TQt::Variant.new( " << *it << " )" << endl;
  }

  TQString group;
  for( e = entries.first(); e; e = entries.next() ) {
    if ( e->group() != group ) {
      group = e->group();
      rb << endl;
      rb << "        # " << group << endl;
    }
    if (e->param().isEmpty()) {
      rb << "        " << varName(e->name()) << " = TQt::Variant.new( " << rbType(e->type()) << " )";
    } else {
      rb << "        " << varName(e->name()) << " = [ ";
	  for (int i = 0; i < e->paramMax()+1; i++) {
		if (i > 0) {
		  rb << ", ";
		}
	  	rb << "TQt::Variant.new( " << rbType(e->type()) << " )";
	  }
	  rb << " ]";
    }
    rb << endl;
  }

  rb << endl;


  group = TQString::null;
  for( e = entries.first(); e; e = entries.next() ) {
    if ( e->group() != group ) {
      if ( !group.isEmpty() ) rb << endl;
      group = e->group();
      rb << "        setCurrentGroup( " << paramString(group, parameters) << " )" << endl << endl;
    }

    TQString key = paramString(e->key(), parameters);
    if ( !e->code().isEmpty())
    {
      rb << e->code() << endl;
    }
    if ( e->type() == "Enum" ) {
      rb << "        values"
          << e->name() << " = []" << endl;
      TQValueList<CfgEntry::Choice> choices = e->choices();
      TQValueList<CfgEntry::Choice>::ConstIterator it;
      for( it = choices.begin(); it != choices.end(); ++it ) {
        rb << "        choice = ItemEnum::Choice.new" << endl;
        rb << "        choice.name = \"" << enumValue((*it).name) << "\" " << endl;
        if ( setUserTexts ) {
          if ( !(*it).label.isEmpty() )
            rb << "        choice.label = i18n(" << quoteString((*it).label) << ")" << endl;
          if ( !(*it).whatsThis.isEmpty() )
            rb << "        choice.whatsThis = i18n(" << quoteString((*it).whatsThis) << ")" << endl;
        }
        rb << "        values" << e->name() << " << choice" << endl;
      }
    }
	
    if (e->param().isEmpty())
    {
      // Normal case
      rb << "        " << itemVar(e) << " = "
          << newItem( e->type(), e->name(), key, e->defaultValue() ) << endl;
      
	  rb << "        " << itemVar(e) << ".property = " << varName(e->name()) << endl;
      
      if ( !e->minValue().isEmpty() )
        rb << "        " << itemVar(e) << ".setMinValue(" << e->minValue() << ")" << endl;
      if ( !e->maxValue().isEmpty() )
        rb << "        " << itemVar(e) << ".setMaxValue(" << e->maxValue() << ")" << endl;

      if ( setUserTexts )
        rb << userTextsFunctions( e );
      
	  rb << "        addItem( " << itemVar(e);
      TQString quotedName = e->name();
      addQuotes( quotedName );
      if ( quotedName != key ) rb << ", \"" << e->name() << "\"";
      rb << " )" << endl;
    }
    else
    {
      // Indexed
      rb << "        " << itemVar(e) << " = Array.new(" << e->paramMax()+1 << ")" << endl;
      for(int i = 0; i <= e->paramMax(); i++)
      {
        TQString defaultStr;
        TQString itemVarStr(itemVar(e)+TQString("[%1]").arg(i));
        
        if ( !e->paramDefaultValue(i).isEmpty() )
          defaultStr = e->paramDefaultValue(i);
        else if ( !e->defaultValue().isEmpty() )
          defaultStr = paramString(e->defaultValue(), e, i);
        else
          defaultStr = defaultValue( e->type() );
        
		rb << "        " << itemVarStr << " = "
            << newItem( e->type(), e->name(), paramString(key, e, i), defaultStr, TQString("[%1]").arg(i) )
            << endl;
			
	    rb << "        " << itemVarStr << ".property = " << varName(e->name())+TQString("[%1]").arg(i) << endl;

        if ( setUserTexts )
          rb << userTextsFunctions( e, itemVarStr, e->paramName() );

        // Make mutators for enum parameters work by adding them with $(..) replaced by the 
        // param name. The check for isImmutable in the set* functions doesn't have the param 
        // name available, just the corresponding enum value (int), so we need to store the 
        // param names in a separate static list!.
        rb << "        addItem( " << itemVarStr << ", \"";
        if ( e->paramType()=="Enum" )
          rb << e->paramName().replace( "$("+e->param()+")", "%1").arg(e->paramValues()[i] );
        else
          rb << e->paramName().replace( "$("+e->param()+")", "%1").arg(i);
        rb << "\" )" << endl;

      }
    }
  }

  rb << "    end" << endl << endl;

  rb << "end" << endl << endl;
  
  if ( !nameSpace.isEmpty() ) rb << "end" << endl << endl;

  implementation.close();
}