summaryrefslogtreecommitdiffstats
path: root/tqt3integration/utils/gen.cpp
blob: c82a838780b4b8c6865aa7c44f1237f3a18d6d51 (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
 /*
 *  This file is part of the Trinity Desktop Environment
 *
 *  Original file taken from the OpenSUSE tdebase builds
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <tqfile.h>
#include <tqstring.h>
#include <tqvaluelist.h>
#include <stdlib.h>

// TODO includes, forwards


/*

FUNCTION <name>
 RETURN_TYPE <type>
 DELAYED_RETURN   - use DCOP transaction in kded module, function will take some time to finish
 SKIP_QT - don't generate in qt file
 ONLY_QT - generate only in qt file
 ADD_APPINFO - generate wmclass arguments
 ARG <name>
  TYPE <type>
  ORIG_TYPE <type>          - for example when the function accepts TQWidget*, but WId is really used
  ORIG_CONVERSION <conversion>
  IGNORE
  NEEDS_DEREF
  CONST_REF
  OUT_ARGUMENT
  CONVERSION <function>
  BACK_CONVERSION <function> - for out arguments
  CREATE <function>  - doesn't exist in TQt, create in qtkde using function
  PARENT - the argument is a parent window to be used for windows
 ENDARG
ENDFUNCTION

*/

struct Arg
    {
    Arg() : ignore( false ), needs_deref( false ), const_ref( false ), out_argument( false ), parent( false ) {}
    TQString name;
    TQString type;
    TQString orig_type;
    TQString orig_conversion;
    bool ignore;
    bool needs_deref;
    bool const_ref;
    bool out_argument;
    TQString conversion;
    TQString back_conversion;
    TQString create;
    bool parent;
    };

struct Function
    {
    Function() : delayed_return( false ), skip_qt( false ), only_qt( false ), add_appinfo( false ) {}
    TQString name;
    TQString return_type;
    bool delayed_return;
    bool skip_qt;
    bool only_qt;
    bool add_appinfo;
    TQValueList< Arg > args;
    void stripNonOutArguments();
    void stripCreatedArguments();
    };

void Function::stripNonOutArguments()
    {
    TQValueList< Arg > new_args;
    for( TQValueList< Arg >::ConstIterator it = args.begin();
         it != args.end();
         ++it )
        {
        const Arg& arg = (*it);
        if( arg.out_argument )
            new_args.append( arg );
        }
    args = new_args;
    }

void Function::stripCreatedArguments()
    {
    TQValueList< Arg > new_args;
    for( TQValueList< Arg >::ConstIterator it = args.begin();
         it != args.end();
         ++it )
        {
        const Arg& arg = (*it);
        if( arg.create.isEmpty())
            new_args.append( arg );
        }
    args = new_args;
    }

TQValueList< Function > functions;

TQFile* input_file = NULL;
TQTextStream* input_stream = NULL;
static TQString last_line;
int last_lineno = 0;

#define check( arg ) my_check( __FILE__, __LINE__, arg )
#define error() my_error( __FILE__, __LINE__ )

void my_error( const char* file, int line )
    {
    fprintf( stderr, "Error: %s: %d\n", file, line );
    fprintf( stderr, "Line %d: %s\n", last_lineno, last_line.utf8().data());
    abort();
    }

void my_check( const char* file, int line, bool arg )
    {
    if( !arg )
        my_error( file, line );
    }

void openInputFile( const TQString& filename )
    {
    check( input_file == NULL );
    input_file = new TQFile( filename );
    printf("[INFO] Reading bindings definitions from file %s\n\r", filename.ascii());
    if( !input_file->open( IO_ReadOnly ))
        error();
    input_stream = new TQTextStream( input_file );
    last_lineno = 0;
    }

TQString getInputLine()
    {
    while( !input_stream->atEnd())
        {
        TQString line = input_stream->readLine().stripWhiteSpace();
        ++last_lineno;
        last_line = line;
        if( line.isEmpty() || line[ 0 ] == '#' )
            continue;
        return line;
        }
    return TQString();
    }

void closeInputFile()
    {
    delete input_stream;
    delete input_file;
    input_stream = NULL;
    input_file = NULL;
    }

void parseArg( Function& function, const TQString& details )
    {
    Arg arg;
    arg.name = details;
    TQString line = getInputLine();
    while( !line.isNull() )
        {
        if( line.startsWith( "ENDARG" ))
            {
            check( !arg.type.isEmpty());
            function.args.append( arg );
            return;
            }
        else if( line.startsWith( "TYPE" ))
            {
            check( arg.type.isEmpty());
            arg.type = line.mid( strlen( "TYPE" )).stripWhiteSpace();
            }
        else if( line.startsWith( "ORIG_TYPE" ))
            {
            check( arg.orig_type.isEmpty());
            arg.orig_type = line.mid( strlen( "ORIG_TYPE" )).stripWhiteSpace();
            }
        else if( line.startsWith( "ORIG_CONVERSION" ))
            {
            check( arg.orig_conversion.isEmpty());
            arg.orig_conversion = line.mid( strlen( "ORIG_CONVERSION" )).stripWhiteSpace();
            }
        else if( line.startsWith( "IGNORE" ))
            {
            check( !arg.out_argument );
            arg.ignore = true;
            }
        else if( line.startsWith( "NEEDS_DEREF" ))
            {
            check( !arg.const_ref );
            arg.needs_deref = true;
            }
        else if( line.startsWith( "CONST_REF" ))
            {
            check( !arg.needs_deref );
            check( !arg.out_argument );
            arg.const_ref = true;
            }
        else if( line.startsWith( "OUT_ARGUMENT" ))
            {
            check( !arg.ignore );
            check( !arg.const_ref );
            arg.out_argument = true;
            }
        else if( line.startsWith( "CONVERSION" ))
            {
            check( arg.conversion.isEmpty());
            arg.conversion = line.mid( strlen( "CONVERSION" )).stripWhiteSpace();
            }
        else if( line.startsWith( "BACK_CONVERSION" ))
            {
            check( arg.back_conversion.isEmpty());
            arg.back_conversion = line.mid( strlen( "BACK_CONVERSION" )).stripWhiteSpace();
            }
        else if( line.startsWith( "CREATE" ))
            {
            check( arg.create.isEmpty());
            arg.create = line.mid( strlen( "CREATE" )).stripWhiteSpace();
            }
        else if( line.startsWith( "PARENT" ))
            {
            arg.parent = true;
            }
        else
            error();
        line = getInputLine();
        }
    error();
    }

void parseFunction( const TQString& details )
    {
    Function function;
    function.name = details;
    TQString line = getInputLine();
    while( !line.isNull() )
        {
        if( line.startsWith( "ENDFUNCTION" ))
            {
            if( function.add_appinfo )
                {
                Arg arg;
                arg.name = "wmclass1";
                arg.type = "TQCString";
                arg.const_ref = true;
                arg.create = "tqAppName";
                function.args.append( arg );
                arg.name = "wmclass2";
                arg.create = "qAppClass";
                function.args.append( arg );
                }
            check( !function.return_type.isEmpty());
            functions.append( function );
            return;
            }
        else if( line.startsWith( "RETURN_TYPE" ))
            {
            check( function.return_type.isEmpty());
            function.return_type = line.mid( strlen( "RETURN_TYPE" )).stripWhiteSpace();
            }
        else if( line.startsWith( "DELAYED_RETURN" ))
            function.delayed_return = true;
        else if( line.startsWith( "SKIP_QT" ))
            function.skip_qt = true;
        else if( line.startsWith( "ONLY_QT" ))
            function.only_qt = true;
        else if( line.startsWith( "ADD_APPINFO" ))
            function.add_appinfo = true;
        else if( line.startsWith( "ARG" ))
            {
            parseArg( function, line.mid( strlen( "ARG" )).stripWhiteSpace());
            }
        else
            error();
        line = getInputLine();
        }
    error();
    }

void parse(TQString filename)
    {
    openInputFile( filename );
    TQString line = getInputLine();
    while( !line.isNull() )
        {
        if( line.startsWith( "FUNCTION" ))
            {
            parseFunction( line.mid( strlen( "FUNCTION" )).stripWhiteSpace());
            }
        else
            error();
        line = getInputLine();
        }
    closeInputFile();
    }

TQString makeIndent( int indent )
    {
    return indent > 0 ? TQString().fill( ' ', indent ) : "";
    }

void generateFunction( TQTextStream& stream, const Function& function, const TQString name,
    int indent, bool staticf, bool orig_type, bool ignore_deref, int ignore_level )
    {
    TQString line;
    line += makeIndent( indent );
    if( staticf )
        line += "static ";
    line += function.return_type + " " + name + "(";
    bool need_comma = false;
    for( TQValueList< Arg >::ConstIterator it = function.args.begin();
         it != function.args.end();
         ++it )
        {
        const Arg& arg = (*it);
        if( ignore_level >= 2 && arg.ignore )
            continue;
        if( need_comma )
            {
            line += ",";
            if( line.length() > 80 )
                {
                stream << line << "\n";
                line = makeIndent( indent + 4 );
                }
            else
                line += " ";
            }
        else
            line += " ";
        need_comma = true;
        if( orig_type && !arg.orig_type.isEmpty())
            line += arg.orig_type;
        else
            {
            if( arg.const_ref )
                line += "const ";
            line += arg.type;
            if( !ignore_deref && arg.needs_deref )
                line += "*";
            if( arg.const_ref )
                line += "&";
            }
        if( ignore_level >= 1 && arg.ignore )
            line += " /*" + arg.name + "*/";
        else
            line += " " + arg.name;
        }
    line += " )";
    stream << line;
    }

void generateTQtH()
    {
    TQFile file( "qtkdeintegration_x11_p.h.gen" );
    if( !file.open( IO_WriteOnly ))
        error();
    TQTextStream stream( &file );
    for( TQValueList< Function >::ConstIterator it = functions.begin();
         it != functions.end();
         ++it )
        {
        Function f = *it;
        if( f.skip_qt )
            continue;
        f.stripCreatedArguments();
        generateFunction( stream, f, f.name, 8,
            true /*static*/, true /*orig type*/, false /*ignore deref*/, 0 /*ignore level*/ );
        stream << ";\n";
        }
    }

void generateTQtCpp()
    {
    TQFile file( "qtkdeintegration_x11.cpp.gen" );
    if( !file.open( IO_WriteOnly ))
        error();
    TQTextStream stream( &file );
    for( TQValueList< Function >::ConstIterator it = functions.begin();
         it != functions.end();
         ++it )
        {
        Function f = *it;
        if( f.only_qt )
            continue;
        f.stripCreatedArguments();
        generateFunction( stream, f, "(*qtkde_" + f.name + ")", 0,
            true /*static*/, false /*orig type*/, false /*ignore deref*/, 0 /*ignore level*/ );
        stream << ";\n";
        }
    stream <<
"\n"
"void TQKDEIntegration::initLibrary()\n"
"    {\n"
"    if( !inited )\n"
"        {\n"
"        enable = false;\n"
"        inited = true;\n"
"        TQString libpath = findLibrary();\n"
"        if( libpath.isEmpty())\n"
"            return;\n"
"        TQLibrary lib( libpath );\n"
"        if( !TQFile::exists( lib.library())) // avoid stupid TQt warning\n"
"            return;\n"
"        lib.setAutoUnload( false );\n";
    for( TQValueList< Function >::ConstIterator it = functions.begin();
         it != functions.end();
         ++it )
        {
        Function function = *it;
        if( function.only_qt )
            continue;
        stream << makeIndent( 8 ) + "qtkde_" + function.name + " = (\n";
        function.stripCreatedArguments();
        generateFunction( stream, function, "(*)", 12,
            false /*static*/, false /*orig type*/, false /*ignore deref*/, 0 /*ignore level*/ );
        stream << "\n" + makeIndent( 12 ) + ")\n";
        stream << makeIndent( 12 ) + "lib.resolve(\"" + (*it).name + "\");\n";
        stream << makeIndent( 8 ) + "if( qtkde_" + (*it).name + " == NULL )\n";
        stream << makeIndent( 12 ) + "return;\n";
        }
    stream <<
"        enable = qtkde_initializeIntegration();\n"
"        }\n"
"    }\n"
"\n";
    for( TQValueList< Function >::ConstIterator it1 = functions.begin();
         it1 != functions.end();
         ++it1 )
        {
        Function function = *it1;
        if( function.skip_qt || function.only_qt )
            continue;
        function.stripCreatedArguments();
        generateFunction( stream, function, "QKDEIntegration::" + function.name, 0,
            false /*static*/, true /*orig type*/, false /*ignore deref*/, 0 /*ignore level*/ );
        stream << "\n";
        stream << makeIndent( 4 ) + "{\n";
        stream << makeIndent( 4 ) + "return qtkde_" + function.name + "(\n";
        stream << makeIndent( 8 );
        bool need_comma = false;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( need_comma )
                stream << ", ";
            need_comma = true;
            if( !arg.orig_conversion.isEmpty())
                {
                stream << arg.orig_conversion + "( " + arg.name + " )";
                }
            else
                stream << arg.name;
            }
        stream << " );\n";
        stream << makeIndent( 4 ) + "}\n";
        }
    }

void generateTQt()
    {
    generateTQtH();
    generateTQtCpp();
    }

void generateTQtKde()
    {
    TQFile file( "tqtkde_functions.cpp" );
    if( !file.open( IO_WriteOnly ))
        error();
    TQTextStream stream( &file );
    for( TQValueList< Function >::ConstIterator it1 = functions.begin();
         it1 != functions.end();
         ++it1 )
        {
        const Function& function = *it1;
        if( function.only_qt )
            continue;
        Function stripped_function = function;
        stripped_function.stripCreatedArguments();
        stream << "extern \"C\"\n";
        generateFunction( stream, stripped_function, stripped_function.name, 0,
            false /*static*/, false /*orig type*/, false /*ignore deref*/, 1 /*ignore level*/ );
        stream << "\n";
        stream <<
"    {\n"
"    if( qt_xdisplay() != NULL )\n"
"        XSync( qt_xdisplay(), False );\n";
        TQString parent_arg;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( arg.ignore )
                continue;
            if( arg.parent )
                {
                parent_arg = arg.name;
                break;
                }
            }
        if( !parent_arg.isEmpty())
            {
            stream << "    if( " << parent_arg << " == 0 )\n";
            stream << "        DCOPRef( \"kded\", \"MainApplication-Interface\" ).call( \"updateUserTimestamp\", qt_x_time );\n";
            }
        stream <<
"    TQByteArray data, replyData;\n"
"    TQCString replyType;\n";
        if( !function.args.isEmpty())
            {
            stream << "    TQDataStream datastream( data, IO_WriteOnly );\n";
            stream << "    datastream";
            for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
                 it2 != function.args.end();
                 ++it2 )
                {
                const Arg& arg = (*it2);
                if( arg.ignore )
                    continue;
                stream << " << ";
                if( !(arg.conversion).isNull() )
                    stream << arg.conversion + "( ";
                if( !arg.create.isEmpty())
                    stream << arg.create + "()";
                else
                    {
                    if( arg.needs_deref )
                        stream << "( " << arg.name << " != NULL ? *" << arg.name << " : " << arg.type << "())";
                    else
                        stream << arg.name;
                    }
                if( !(arg.conversion).isNull() )
                    stream << " )";
                }
            stream << ";\n";
            }
        stream << "    if( !dcopClient()->call( \"kded\", \"kdeintegration\",\"" + function.name + "(";
        bool need_comma = false;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( arg.ignore )
                continue;
            if( need_comma )
                stream << ",";
            need_comma = true;
            stream << arg.type;
            }
        stream << ")\", data, replyType, replyData, true ))\n";
        stream << "        {\n";
        if( function.return_type != "void" )
            {
            stream << "        " + function.return_type << " ret;\n";
            stream << "        dcopTypeInit( ret ); // set to false/0/whatever\n";
            stream << "        return ret;\n";
            }
        else
            stream << "        return;\n";
        stream << "        }\n";
        bool return_data = false;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             !return_data && it2 != function.args.end();
             ++it2 )
            {
            if( (*it2).out_argument )
                return_data = true;
            }
        if( return_data || function.return_type != "void" )
            stream << "    TQDataStream replystream( replyData, IO_ReadOnly );\n";
        if( function.return_type != "void" )
            {
            stream << "    " + function.return_type << " ret;\n";
            stream << "    replystream >> ret;\n";
            }
        if( return_data )
            {
            for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
                 it2 != function.args.end();
                 ++it2 )
                {
                const Arg& arg = (*it2);
                if( arg.out_argument && arg.needs_deref )
                    stream << "    " << arg.type << " " << arg.name + "_dummy;\n";
                }
            stream << "    replystream";
            for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
                 it2 != function.args.end();
                 ++it2 )
                {
                const Arg& arg = (*it2);
                if( arg.out_argument )
                    {
                    stream << " >> ";
                    if( !(arg.back_conversion).isNull() )
                        stream << arg.name + "_dummy";
                    else
                        {
                        if( arg.needs_deref )
                            stream << "( " << arg.name << " != NULL ? *" << arg.name << " : " << arg.name << "_dummy )";
                        else
                            stream << arg.name;
                        }
                    }
                }
            stream << ";\n";
            for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
                 it2 != function.args.end();
                 ++it2 )
                {
                const Arg& arg = (*it2);
                if( arg.out_argument && (!(arg.back_conversion).isNull()) )
                    stream << "    if( " << arg.name << " != NULL )\n"
                        << makeIndent( 8 ) << "*" << arg.name << " = " << arg.back_conversion << "( " << arg.name + "_dummy );\n";
                }
            }
        if( function.return_type != "void" )
            stream << "    return ret;\n";
        stream << "    }\n";
        stream << "\n";
        }
    }
    
void generateKdeDcop( TQTextStream& stream )
    {
    stream <<
"bool Module::process(const TQCString &fun, const TQByteArray &data,\n"
"    TQCString &replyType, TQByteArray &replyData)\n"
"    {\n";
    for( TQValueList< Function >::ConstIterator it1 = functions.begin();
         it1 != functions.end();
         ++it1 )
        {
        const Function& function = *it1;
        if( function.only_qt )
            continue;
        stream << "    if( fun == \"" + function.name + "(";
        bool need_comma = false;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( arg.ignore )
                continue;
            if( need_comma )
                stream << ",";
            need_comma = true;
            stream << arg.type;
            }
        stream << ")\" )\n";
        stream << "        {\n";
        if( function.delayed_return )
            stream << "        pre_" + function.name + "( data );\n";
        else
            {
            stream << "        pre_" + function.name + "( data, replyData );\n";
            stream << "        replyType = \"" << function.return_type << "\";\n";
            }
        stream << "        return true;\n";
        stream << "        }\n";
        }
    stream <<
"    return KDEDModule::process( fun, data, replyType, replyData );\n"
"    }\n"
"\n";
    stream <<
"QCStringList Module::functions()\n"
"    {\n"
"    QCStringList funcs = KDEDModule::functions();\n";
    for( TQValueList< Function >::ConstIterator it1 = functions.begin();
         it1 != functions.end();
         ++it1 )
        {
        const Function& function = *it1;
        if( function.only_qt )
            continue;
        stream << "    funcs << \"" + function.name + "(";
        bool need_comma = false;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( arg.ignore )
                continue;
            if( need_comma )
                stream << ",";
            need_comma = true;
            stream << arg.type;
            }
        stream << ")\";\n";
        }
    stream <<
"    return funcs;\n"
"    }\n"
"\n"
"QCStringList Module::interfaces()\n"
"    {\n"
"    QCStringList ifaces = KDEDModule::interfaces();\n"
"    ifaces << \"KDEIntegration\";\n"
"    return ifaces;\n"
"    }\n"
"\n";
    }

void generateKdePreStub( TQTextStream& stream )
    {
    for( TQValueList< Function >::ConstIterator it1 = functions.begin();
         it1 != functions.end();
         ++it1 )
        {
        const Function& function = *it1;
        if( function.only_qt )
            continue;
        stream << "void Module::pre_" + function.name + "( const TQByteArray& "
            + ( function.args.isEmpty() ? "" : "data" )
            + ( function.delayed_return ? "" : ", TQByteArray& replyData" )
            + " )\n";
        stream << "    {\n";
        if( function.delayed_return )
            {
            stream << "    JobData job;\n";
            stream << "    job.transaction = kapp->dcopClient()->beginTransaction();\n";
            stream << "    job.type = JobData::" + TQString( function.name[ 0 ].upper()) + function.name.mid( 1 ) + ";\n";
            }
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( arg.ignore )
                continue;
            stream << "    " + arg.type + " " + arg.name + ";\n";
            }
        if( !function.args.isEmpty())
            {
            stream << "    TQDataStream datastream( data, IO_ReadOnly );\n";
            stream << "    datastream";
            for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
                 it2 != function.args.end();
                 ++it2 )
                {
                const Arg& arg = (*it2);
                if( arg.ignore )
                    continue;
                stream << " >> " + arg.name;
                }
            stream << ";\n";
            }
        if( function.delayed_return )
            stream << "    void* handle = " + function.name + "( ";
        else
            stream << "    post_" + function.name + "( " + function.name + "( ";
        bool need_comma = false;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( arg.ignore )
                continue;
            if( need_comma )
                stream << ", ";
            need_comma = true;
            stream << arg.name;
            }
        if( function.delayed_return )
            {
            stream << " );\n";
            stream << "    jobs[ handle ] = job;\n";
            }
        else
            stream << " ), replyData );\n";
        stream << "    }\n";
        stream << "\n";
        }
    }

void generateKdePostStub( TQTextStream& stream )
    {
    for( TQValueList< Function >::ConstIterator it1 = functions.begin();
         it1 != functions.end();
         ++it1 )
        {
        const Function& function = *it1;
        if( function.only_qt )
            continue;
        stream << "void Module::post_" + function.name + "( ";
        bool needs_comma = false;
        if( function.delayed_return )
            {
            stream << "void* handle";
            needs_comma = true;
            }
        if( function.return_type != "void" )
            {
            if( needs_comma )
                stream << ", ";
            needs_comma = true;
            stream << function.return_type + " ret";
            }
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             it2 != function.args.end();
             ++it2 )
            {
            const Arg& arg = (*it2);
            if( arg.out_argument )
                {
                if( needs_comma )
                    stream << ", ";
                needs_comma = true;
                stream << arg.type + " " + arg.name;
                }
            }
        if( !function.delayed_return )
            stream << ( needs_comma ? "," : "" ) << " TQByteArray& replyData";
        stream << " )\n";
        stream << "    {\n";
        if( function.delayed_return )
            {
            stream << "    assert( jobs.contains( handle ));\n";
            stream << "    JobData job = jobs[ handle ];\n";
            stream << "    jobs.remove( handle );\n";
            stream << "    TQByteArray replyData;\n";
            stream << "    TQCString replyType = \"qtkde\";\n";
            }
        bool return_data = false;
        for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
             !return_data && it2 != function.args.end();
             ++it2 )
            {
            if( (*it2).out_argument )
                return_data = true;
            }
        if( function.return_type != "void" || return_data )
            stream << "    TQDataStream replystream( replyData, IO_WriteOnly );\n";
        if( function.return_type != "void" )
            stream << "    replystream << ret;\n";
        if( return_data )
            {
            stream << "    replystream";
            for( TQValueList< Arg >::ConstIterator it2 = function.args.begin();
                 it2 != function.args.end();
                 ++it2 )
                {
                const Arg& arg = (*it2);
                if( arg.out_argument )
                    stream << " << " + arg.name;
                }
            stream << ";\n";
            }
        if( function.delayed_return )
            stream << "    kapp->dcopClient()->endTransaction( job.transaction, replyType, replyData );\n";
        stream << "    }\n";
        stream << "\n";
        }
    }

void generateKdeStubs( TQTextStream& stream )
    {
    generateKdePreStub( stream );
    generateKdePostStub( stream );
//    TODO udelat i predbezne deklarace pro skutecne funkce?
    }

void generateKdeCpp()
    {
    TQFile file( "module_functions.cpp" );
    if( !file.open( IO_WriteOnly ))
        error();
    TQTextStream stream( &file );
    generateKdeDcop( stream );
    generateKdeStubs( stream );
    }

void generateKdeH()
    {
    TQFile file( "module_functions.h" );
    if( !file.open( IO_WriteOnly ))
        error();
    TQTextStream stream( &file );
    for( TQValueList< Function >::ConstIterator it1 = functions.begin();
         it1 != functions.end();
         ++it1 )
        {
        const Function& function = *it1;
        if( function.only_qt )
            continue;
        Function real_function = function;
        if( function.delayed_return )
            real_function.return_type = "void*";
        generateFunction( stream, real_function, real_function.name, 8,
            false /*static*/, false /*orig type*/, true /*ignore deref*/, 2 /*ignore level*/ );
        stream << ";\n";
        stream << makeIndent( 8 ) + "void pre_" + function.name + "( const TQByteArray& data"
            + ( function.delayed_return ? "" : ", TQByteArray& replyData" ) + " );\n";
        Function post_function = function;
        post_function.stripNonOutArguments();
        if( function.return_type != "void" )
            {
            Arg return_arg;
            return_arg.name = "ret";
            return_arg.type = function.return_type;
            post_function.args.prepend( return_arg );
            }
        if( function.delayed_return )
            {
            Arg handle_arg;
            handle_arg.name = "handle";
            handle_arg.type = "void*";
            post_function.args.prepend( handle_arg );
            }
        else
            {
            Arg handle_arg;
            handle_arg.name = "replyData";
            handle_arg.type = "TQByteArray&";
            post_function.args.append( handle_arg );
            }
        post_function.return_type = "void";
        generateFunction( stream, post_function, "post_" + post_function.name, 8,
            false /*static*/, false /*orig type*/, true /*ignore deref*/, 2 /*ignore level*/ );
        stream << ";\n";
        }
    }

void generateKde()
    {
    generateKdeCpp();
    generateKdeH();
    }

void generate()
    {
    generateTQt();
    generateTQtKde();
    generateKde();
    }

int main (int argc, char *argv[])
    {
    if (argc > 1) {
        parse(TQString(argv[1]));
    }
    else {
        parse(TQString("gen.txt"));
    }
    generate();
    return 0;
    }