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
|
/***************************************************************************
functionlib.cpp - Standard library of functions
-------------------
copyright : (C) 2004-2006 Michal Rudolf <mrudolf@kdewebdwev.org>
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "parserdata.h"
#include "parser.h"
#include "specials.h"
#include "specialinformation.h"
#include "myprocess.h"
#include "kommanderwidget.h"
#include "invokeclass.h"
#include "kommanderfactory.h"
#include <iostream>
#include <stdlib.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qstringlist.h>
#include <qmetaobject.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <dcopclient.h>
#include <kapplication.h>
#include <kcolordialog.h>
#include <kfiledialog.h>
#include <kglobal.h>
#include <kinputdialog.h>
#include <klocale.h>
#include <kpassdlg.h>
using namespace Parse;
/******************* String functions ********************************/
static ParseNode f_stringLength(Parser*, const ParameterList& params)
{
return params[0].toString().length();
}
static ParseNode f_stringContains(Parser*, const ParameterList& params)
{
return params[0].toString().contains(params[1].toString());
}
static ParseNode f_stringCompare(Parser*, const ParameterList& params)
{
int result = QString::compare(params[0].toString(),params[1].toString());
if (result < 0)
{
result = -1;
} else
if (result > 0)
{
result = 1;
}
return result;
}
static ParseNode f_stringFind(Parser*, const ParameterList& params)
{
return params[0].toString().find(params[1].toString(), params.count() == 3 ? params[2].toInt() : 0);
}
static ParseNode f_stringFindRev(Parser*, const ParameterList& params)
{
return params[0].toString().findRev(params[1].toString(),
params.count() == 3 ? params[2].toInt() : params[0].toString().length());
}
static ParseNode f_stringLeft(Parser*, const ParameterList& params)
{
return params[0].toString().left(params[1].toInt());
}
static ParseNode f_stringRight(Parser*, const ParameterList& params)
{
return params[0].toString().right(params[1].toInt());
}
static ParseNode f_stringMid(Parser*, const ParameterList& params)
{
return params[0].toString().mid(params[1].toInt(), params.count() == 3 ? params[2].toInt() : 0xffffffff);
}
static ParseNode f_stringRemove(Parser*, const ParameterList& params)
{
return params[0].toString().remove(params[1].toString());
}
static ParseNode f_stringReplace(Parser*, const ParameterList& params)
{
return params[0].toString().replace(params[1].toString(), params[2].toString());
}
static ParseNode f_stringLower(Parser*, const ParameterList& params)
{
return params[0].toString().lower();
}
static ParseNode f_stringUpper(Parser*, const ParameterList& params)
{
return params[0].toString().upper();
}
static ParseNode f_stringIsEmpty(Parser*, const ParameterList& params)
{
return params[0].toString().isEmpty();
}
static ParseNode f_stringSection(Parser*, const ParameterList& params)
{
return params[0].toString().section(params[1].toString(), params[2].toInt(),
params.count() == 4 ? params[3].toInt() : params[2].toInt());
}
static ParseNode f_stringArgs(Parser*, const ParameterList& params)
{
if (params.count() == 2)
return params[0].toString().arg(params[1].toString());
else if (params.count() == 3)
return params[0].toString().arg(params[1].toString()).arg(params[2].toString());
else
return params[0].toString().arg(params[1].toString()).arg(params[2].toString()).arg(params[3].toString());
}
static ParseNode f_stringIsNumber(Parser*, const ParameterList& params)
{
bool ok;
params[0].toString().toDouble(&ok);
return ok;
}
static ParseNode f_stringToInt(Parser*, const ParameterList& params)
{
return params[0].toString().toInt();
}
static ParseNode f_stringToDouble(Parser*, const ParameterList& params)
{
return params[0].toString().toDouble();
}
static ParseNode f_return(Parser* p, const ParameterList& params)
{
KommanderWidget * w = p->currentWidget();
if (w)
w->setGlobal(w->widgetName() + "_RESULT", params[0].toString());
return params[0];
}
static ParseNode f_stringRound(Parser*, const ParameterList& params)
{
QString s;
s.sprintf("%."+params[1].toString()+"f", params[0].toDouble());
return s;
}
/******************* Debug function ********************************/
static ParseNode f_debug(Parser*, const ParameterList& params)
{
for (uint i=0; i<params.count(); i++)
std::cerr << params[i].toString();
std::cerr << "\n";
fflush(stderr);
return ParseNode();
}
static ParseNode f_echo(Parser*, const ParameterList& params)
{
for (uint i=0; i<params.count(); i++)
std::cout << params[i].toString();
fflush(stdout);
return ParseNode();
}
/******************* File function ********************************/
static ParseNode f_fileRead(Parser*, const ParameterList& params)
{
QFile file(params[0].toString());
if (!file.exists() || !file.open(IO_ReadOnly))
return ParseNode("");
QTextStream text(&file);
return text.read();
}
static ParseNode f_fileWrite(Parser*, const ParameterList& params)
{
QString fname = params[0].toString();
if (fname.isEmpty())
return 0;
QFile file(fname);
if (!file.open(IO_WriteOnly))
return 0;
QTextStream text(&file);
for (uint i=1; i<params.count(); i++)
text << params[i].toString();
return 1;
}
static ParseNode f_fileAppend(Parser*, const ParameterList& params)
{
QString fname = params[0].toString();
if (fname.isEmpty())
return 0;
QFile file(fname);
if (!file.open(IO_WriteOnly | IO_Append))
return 0;
QTextStream text(&file);
for (uint i=1; i<params.count(); i++)
text << params[i].toString();
return 1;
}
static ParseNode f_fileExists(Parser*, const ParameterList& params)
{
QFile file(params[0].toString());
if (!file.exists())
return 0;
else
return 1;
}
static ParseNode f_executeSlot(Parser* parser, const ParameterList& params)
{
ParameterList::ConstIterator it = params.begin();
QString slotName = (*it).toString()+"(";
++it;
QString widgetName = (*it).toString();
KommanderWidget* widget = parser->currentWidget();
if (!widget)
return ParseNode::error("unknown widget");
widget = widget->widgetByName(widgetName);
if (!widget)
return ParseNode::error("unknown widget");
QObject *object = widget->object();
if (!object)
return ParseNode::error("unknown widget");
QStrList slotSignatures = object->metaObject()->slotNames(true);
QStringList slotNames = QStringList::fromStrList(slotSignatures);
int slotNum = -1;
uint i = 0;
while (i < slotNames.count())
{
if (slotNames[i].startsWith(slotName))
{
slotNum = i;
break;
}
i++;
}
if (slotNum == -1)
return ParseNode::error("unknown function");
QStringList args;
++it; // skip widget
while (it != params.end())
{
args += (*it).toString();
++it;
}
InvokeClass* inv = new InvokeClass(0);
inv->invokeSlot(object, slotSignatures.at(slotNum), args);
inv->deleteLater();
return ParseNode();
}
/******************* DCOP function ********************************/
static ParseNode f_dcopid(Parser*, const ParameterList& )
{
return QString(kapp->dcopClient()->appId());
}
static ParseNode f_pid(Parser*, const ParameterList& )
{
return QString::number(getpid());
}
static ParseNode f_parentPid(Parser*p, const ParameterList& )
{
return p->variable("_PARENTPID").toString().isEmpty() ? QString::number(getppid()) : p->variable("_PARENTPID");
}
static ParseNode f_internalDcop(Parser* parser, const ParameterList& params)
{
SpecialFunction function = SpecialInformation::functionObject("DCOP", params[0].toString());
int functionId = SpecialInformation::function(Group::DCOP, params[0].toString());
if (functionId == -1)
return f_executeSlot(parser, params);
//return ParseNode::error("unknown function");
else if ((uint)function.minArg() > params.count() - 1)
return ParseNode::error("too few parameters");
else if ((uint)function.maxArg() < params.count() - 1)
return ParseNode::error("too many parameters");
KommanderWidget* widget = parser->currentWidget();
if (widget)
widget = widget->widgetByName(params[1].toString());
if (!widget)
return ParseNode::error("unknown widget");
QStringList args;
ParameterList::ConstIterator it = params.begin();
++it; // skip function
++it; // skip widget
while (it != params.end())
{
args += (*it).toString();
++it;
}
return widget->handleDCOP(functionId, args);
}
static ParseNode f_dcop(Parser*, const ParameterList& params)
{
QCString appId = params[0].toString().latin1();
QCString object = params[1].toString().latin1();
QString function = params[2].toString().section('(', 0, 0);
QStringList items = QStringList::split(",", params[2].toString().section('(', 1, 1).section(')', 0, 0));
QByteArray byteData;
QDataStream byteDataStream(byteData, IO_WriteOnly);
if (items.count() != params.count() - 3)
{
qDebug("Wrong number of parameters");
return ParseNode();
}
int i = 3;
for (QStringList::Iterator it = items.begin(); it != items.end(); ++it)
{
*it = (*it).stripWhiteSpace();
if (*it == "int")
byteDataStream << params[i++].toInt();
else if (*it == "long")
byteDataStream << params[i++].toInt();
else if (*it == "float")
byteDataStream << params[i++].toDouble();
else if (*it == "double")
byteDataStream << params[i++].toDouble();
else if (*it == "bool")
byteDataStream << (bool)params[i++].toInt();
else if (*it == "QStringList")
if (params[i].toString().find('\n') != -1)
byteDataStream << QStringList::split("\n", params[i++].toString(), true);
else
byteDataStream << QStringList::split("\\n", params[i++].toString(), true);
else
byteDataStream << params[i++].toString();
}
function.append(QString("(%1)").arg(items.join(",")));
QCString replyType, byteReply;
DCOPClient* cl = KApplication::dcopClient();
if (!cl || !cl->call(appId, object, function.latin1(),
byteData, replyType, byteReply))
{
qDebug("DCOP failure");
return ParseNode();
}
QDataStream byteReplyStream(byteReply, IO_ReadOnly);
if (replyType == "QString")
{
QString text;
byteReplyStream >> text;
return text;
}
else if(replyType == "int")
{
int i;
byteReplyStream >> i;
return i;
}
else if(replyType == "bool")
{
bool b;
byteReplyStream >> b;
return b;
}
else if (replyType == "QStringList")
{
QStringList text;
byteReplyStream >> text;
return text.join("\n");
}
else if(replyType != "void")
{
qDebug("%s", QString("DCOP return type %1 is not yet implemented.").arg(replyType.data()).latin1());
}
return ParseNode();
}
static ParseNode f_createWidget(Parser* p, const ParameterList& params)
{
QString widgetName = params[0].toString();
QString widgetType = params[1].toString();
QString parentName = params[2].toString();
KommanderWidget *widget = p->currentWidget()->widgetByName(parentName);
if (!widget)
return ParseNode::error("unknown widget");
QWidget *parent = dynamic_cast<QWidget*>(widget->object());
QWidget *w = KommanderFactory::createWidget(widgetType, parent, widgetName.latin1());
if (w)
w->adjustSize();
return ParseNode();
}
static ParseNode f_widgetExists(Parser* p, const ParameterList& params)
{
QString widgetName = params[0].toString();
KommanderWidget *widget = p->currentWidget()->widgetByName(widgetName);
return (widget ? true : false);
}
static ParseNode f_connect(Parser* p, const ParameterList& params)
{
QString sender = params[0].toString();
QString signal = QString::number(QSIGNAL_CODE) + params[1].toString();
QString receiver = params[2].toString();
QString slot = QString::number(QSLOT_CODE) + params[3].toString();
KommanderWidget *senderW = p->currentWidget()->widgetByName(sender);
if (!senderW)
return ParseNode::error("unknown widget");
KommanderWidget *receiverW = p->currentWidget()->widgetByName(receiver);
if (!receiverW)
return ParseNode::error("unknown widget");
dynamic_cast<QObject*>(senderW)->connect(dynamic_cast<QObject*>(senderW), signal.ascii(), dynamic_cast<QObject*>(receiverW), slot.ascii());
return ParseNode();
}
static ParseNode f_disconnect(Parser* p, const ParameterList& params)
{
QString sender = params[0].toString();
QString signal = QString::number(QSIGNAL_CODE) + params[1].toString();
QString receiver = params[2].toString();
QString slot = QString::number(QSLOT_CODE) + params[3].toString();
KommanderWidget *senderW = p->currentWidget()->widgetByName(sender);
if (!senderW)
return ParseNode::error("unknown widget");
KommanderWidget *receiverW = p->currentWidget()->widgetByName(receiver);
if (!receiverW)
return ParseNode::error("unknown widget");
dynamic_cast<QObject*>(senderW)->disconnect(dynamic_cast<QObject*>(senderW), signal.ascii(), dynamic_cast<QObject*>(receiverW), slot.ascii());
return ParseNode();
}
static ParseNode f_exec(Parser* P, const ParameterList& params)
{
MyProcess proc(P->currentWidget());
QString text;
// qDebug("Trying %s", params[0].toString().latin1());
if (params.count() > 1)
text = proc.run(params[0].toString().local8Bit(), params[1].toString());
else
text = proc.run(params[0].toString().local8Bit());
return text;
}
static ParseNode f_execBackground(Parser* P, const ParameterList& params)
{
MyProcess proc(P->currentWidget());
proc.setBlocking(false);
QString text;
qDebug("Trying %s", params[0].toString().latin1());
if (params.count() > 1)
text = proc.run(params[0].toString().local8Bit(), params[1].toString());
else
text = proc.run(params[0].toString().local8Bit());
return text;
}
static ParseNode f_dialog(Parser* P, const ParameterList& params)
{
QString a_dialog = params[0].toString().local8Bit();
QString a_params = params[1].toString().local8Bit();
QString pFileName = P->currentWidget()->global("_KDDIR") + QString("/") + a_dialog;
QFileInfo pDialogFile(pFileName);
if (!pDialogFile.exists())
{
pFileName = a_dialog;
pDialogFile.setFile(pFileName);
if (!pDialogFile.exists())
return QString();
}
QString cmd = QString("kmdr-executor %1 %2 _PARENTPID=%3 _PARENTDCOPID=kmdr-executor-%4")
.arg(pFileName).arg(a_params).arg(getpid()).arg(getpid());
MyProcess proc(P->currentWidget());
QString text;
text = proc.run(cmd);
return text;
}
static ParseNode f_i18n(Parser*, const ParameterList& params)
{
return KGlobal::locale()->translate(params[0].toString());
}
static ParseNode f_env(Parser*, const ParameterList& params)
{
return QString(getenv(params[0].toString().latin1()));
}
/******************* Array functions ********************************/
static ParseNode f_arrayClear(Parser* P, const ParameterList& params)
{
P->unsetArray(params[0].toString());
return ParseNode();
}
static ParseNode f_arrayCount(Parser* P, const ParameterList& params)
{
if (P->isArray(params[0].toString()))
return (uint)(P->array(params[0].toString()).count());
else
return (uint)0;
}
static ParseNode f_arrayKeys(Parser* P, const ParameterList& params)
{
if (!P->isArray(params[0].toString()))
return ParseNode();
return QStringList(P->array(params[0].toString()).keys()).join("\n");
}
static ParseNode f_arrayValues(Parser* P, const ParameterList& params)
{
if (!P->isArray(params[0].toString()))
return ParseNode();
QValueList<ParseNode> values = P->array(params[0].toString()).values();
QString array;
for (QValueList<ParseNode>::ConstIterator it = values.begin(); it != values.end(); ++it )
array += (*it).toString() + '\n';
return array;
}
static ParseNode f_arrayRemove(Parser* P, const ParameterList& params)
{
if (!P->isArray(params[0].toString()))
P->unsetArray(params[0].toString(), params[1].toString());
return ParseNode();
}
static ParseNode f_arrayToString(Parser* P, const ParameterList& params)
{
QString name = params[0].toString();
if (!P->isArray(name))
return ParseNode();
QString array;
QStringList keys = P->array(name).keys();
QValueList<ParseNode> values = P->array(name).values();
QStringList::Iterator it = keys.begin();
QValueList<ParseNode>::Iterator itval = values.begin();
while (*it)
{
array += QString("%1\t%2\n").arg(*it).arg((*itval).toString());
++it;
++itval;
}
return array;
}
static ParseNode f_arrayFromString(Parser* P, const ParameterList& params)
{
QString name = params[0].toString();
QStringList lines = QStringList::split("\n", params[1].toString());
for (QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
{
QString key = (*it).section('\t', 0, 0).stripWhiteSpace();
if (!key.isEmpty())
P->setArray(name, key, (*it).section('\t', 1));
}
return ParseNode();
}
static ParseNode f_arrayIndexedFromString(Parser* P, const ParameterList& params)
{
QString name = params[0].toString();
QStringList lines;
if (params.count() == 2)
lines = QStringList::split('\t', params[1].toString(), true);
else
lines = QStringList::split(params[2].toString(), params[1].toString(), true);
int i = 0;
for (QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
{
P->setArray(name, QString::number(i), (*it));
i++;
}
return ParseNode();
}
static ParseNode f_arrayIndexedToString(Parser* P, const ParameterList& params)
{
QString name = params[0].toString();
if (!P->isArray(name))
return ParseNode();
QString separator = "\t";
if (params.count() == 2)
separator = params[1].toString();
QString array;
int count = P->array(name).keys().count();
QValueList<ParseNode> values = P->array(name).values();
for (int i = 0; i < count; i++)
{
if (i != 0)
array.append(separator);
array.append(P->arrayValue(name, QString::number(i)).toString());
}
return array;
}
static ParseNode f_arrayIndexedRemoveElements(Parser* P, const ParameterList& params)
{
QString name = params[0].toString();
if (!P->isArray(name))
return ParseNode();
int key = params[1].toInt();
int num = 0;
if (params.count() == 3)
num = params[2].toInt() - 1;
if (num < 0)
num = 0;
QString array;
QStringList keys = P->array(name).keys();
int count = keys.count();
if (key + num > count - 1 || key < 0)
return ParseNode(); //out of index range
for (int i = 0; i < count; i++)
{
if (keys.contains(QString::number(i)) != 1)
return ParseNode(); //array is not indexed
}
for (int i = key; i <= key + num; i++)
{
P->unsetArray(name, QString::number(i));
}
int j = key;
for (int i = key + num + 1; i < count; i++)
{
P->setArray(name, QString::number(j), P->arrayValue(name, QString::number(i)));
j++;
}
for (int i = 1; i <= num + 1; i++)
{
P->unsetArray(name, QString::number(count - i));
}
return ParseNode();
}
static ParseNode f_arrayIndexedInsertElements(Parser* P, const ParameterList& params)
{
QString name = params[0].toString();
if (!P->isArray(name))
return ParseNode();
int key = params[1].toInt();
QStringList keys = P->array(name).keys();
int count = keys.count();
if (key > count || key < 0)
return ParseNode(); //out of index range
QString separator = "\t";
if (params.count() == 4)
separator = params[3].toString();
QStringList elements = QStringList::split(separator, params[2].toString(), true);
int num = elements.count();
for (int i = count - 1; i >= key; i--)
{
P->setArray(name, QString::number(i + num), P->arrayValue(name, QString::number(i)));
}
int i = key;
for (QStringList::Iterator it = elements.begin(); it != elements.end(); ++it )
{
P->setArray(name, QString::number(i), (*it));
i++;
}
return ParseNode();
}
/********** input functions *********************/
static ParseNode f_inputColor(Parser*, const ParameterList& params)
{
QColor color;
if (params.count())
color.setNamedColor(params[0].toString());
KColorDialog::getColor(color);
return color.name();
}
static ParseNode f_inputText(Parser*, const ParameterList& params)
{
QString value;
if (params.count() > 2)
value = params[2].toString();
return KInputDialog::getText(params[0].toString(), params[1].toString(), value);
}
static ParseNode f_inputPassword(Parser*, const ParameterList& params)
{
QCString value;
if (params.count() > 1)
value = params[1].toString().local8Bit();
KPasswordDialog::getPassword(value, params[0].toString());
return QString::fromLocal8Bit(value);
}
static ParseNode f_inputValue(Parser*, const ParameterList& params)
{
return KInputDialog::getInteger(params[0].toString(), params[1].toString(),
params[2].toInt(), params[3].toInt(), params[4].toInt(),
params.count() > 5 ? params[5].toInt() : 1,
(bool*)0);
}
static ParseNode f_inputValueDouble(Parser*, const ParameterList& params)
{
return KInputDialog::getDouble(params[0].toString(), params[1].toString(),
params[2].toDouble(), params[3].toDouble(), params[4].toDouble(),
params.count() > 5 ? params[5].toDouble() : 0.1);
}
static ParseNode f_inputOpenFile(Parser*, const ParameterList& params)
{
QString startdir, filter, caption;
if (params.count() > 0)
startdir = params[0].toString();
if (params.count() > 1)
filter = params[1].toString();
if (params.count() > 2)
caption = params[2].toString();
return KFileDialog::getOpenFileName(startdir, filter, 0, caption);
}
static ParseNode f_inputOpenFiles(Parser*, const ParameterList& params)
{
QString startdir, filter, caption;
if (params.count() > 0)
startdir = params[0].toString();
if (params.count() > 1)
filter = params[1].toString();
if (params.count() > 2)
caption = params[2].toString();
return KFileDialog::getOpenFileNames(startdir, filter, 0, caption).join("\n");
}
static ParseNode f_inputSaveFile(Parser*, const ParameterList& params)
{
QString startdir, filter, caption;
if (params.count() > 0)
startdir = params[0].toString();
if (params.count() > 1)
filter = params[1].toString();
if (params.count() > 2)
caption = params[2].toString();
return KFileDialog::getSaveFileName(startdir, filter, 0, caption);
}
static ParseNode f_inputDirectory(Parser*, const ParameterList& params)
{
QString startdir, caption;
if (params.count() > 0)
startdir = params[0].toString();
if (params.count() > 1)
caption = params[1].toString();
return KFileDialog::getExistingDirectory(startdir, 0, caption);
}
static ParseNode f_message_info(Parser*, const ParameterList& params)
{
QString text, caption;
if (params.count() > 0)
text = params[0].toString();
if (params.count() > 1)
caption = params[1].toString();
KMessageBox::information(0, text, caption);
return ParseNode();
}
static ParseNode f_message_error(Parser*, const ParameterList& params)
{
QString text, caption;
if (params.count() > 0)
text = params[0].toString();
if (params.count() > 1)
caption = params[1].toString();
KMessageBox::error(0, text, caption);
return ParseNode();
}
static ParseNode f_message_warning(Parser*, const ParameterList& params)
{
int result;
QString text, caption, button1, button2, button3;
if (params.count() > 0)
text = params[0].toString();
if (params.count() > 1)
caption = params[1].toString();
if (params.count() > 2)
button1 = params[2].toString();
if (params.count() > 3)
button2 = params[3].toString();
if (params.count() > 4)
button3 = params[4].toString();
if (button1.isNull())
result = KMessageBox::warningYesNo(0, text, caption);
else if (button3.isNull())
result = KMessageBox::warningYesNo(0, text, caption, button1, button2);
else
result = KMessageBox::warningYesNoCancel(0, text, caption, button1, button2, button3);
switch(result)
{
case KMessageBox::Yes:
return 1;
case KMessageBox::No:
return 2;
case KMessageBox::Cancel:
return 3;
default:
return 0;
}
}
static ParseNode f_message_question(Parser*, const ParameterList& params)
{
int result;
QString text, caption, button1, button2, button3;
if (params.count() > 0)
text = params[0].toString();
if (params.count() > 1)
caption = params[1].toString();
if (params.count() > 2)
button1 = params[2].toString();
if (params.count() > 3)
button2 = params[3].toString();
if (params.count() > 4)
button3 = params[4].toString();
if (button1.isNull())
result = KMessageBox::questionYesNo(0, text, caption);
else if (button3.isNull())
result = KMessageBox::questionYesNo(0, text, caption, button1, button2);
else
result = KMessageBox::questionYesNoCancel(0, text, caption, button1, button2, button3);
switch(result)
{
case KMessageBox::Yes:
return 1;
case KMessageBox::No:
return 2;
case KMessageBox::Cancel:
return 3;
default:
return 0;
}
}
static ParseNode f_read_setting(Parser* parser, const ParameterList& params)
{
QString def;
if (params.count() > 1)
def = params[1].toString();
if (parser->currentWidget())
{
QString fname = parser->currentWidget()->fileName();
if (fname.isEmpty())
return ParseNode();
KConfig cfg("kommanderrc", true);
cfg.setGroup(fname);
return cfg.readEntry(params[0].toString(), def);
}
return ParseNode();
}
static ParseNode f_write_setting(Parser* parser, const ParameterList& params)
{
if (parser->currentWidget())
{
QString fname = parser->currentWidget()->fileName();
if (fname.isEmpty())
return ParseNode();
KConfig cfg("kommanderrc", false);
cfg.setGroup(fname);
cfg.writeEntry(params[0].toString(), params[1].toString());
}
return ParseNode();
}
void ParserData::registerStandardFunctions()
{
registerFunction("str_length", Function(&f_stringLength, ValueInt, ValueString));
registerFunction("str_contains", Function(&f_stringContains, ValueInt, ValueString, ValueString));
registerFunction("str_compare", Function(&f_stringCompare, ValueInt, ValueString, ValueString));
registerFunction("str_find", Function(&f_stringFind, ValueInt, ValueString, ValueString, ValueInt, 2));
registerFunction("str_findrev", Function(&f_stringFindRev, ValueInt, ValueString, ValueString, ValueInt, 2));
registerFunction("str_left", Function(&f_stringLeft, ValueString, ValueString, ValueInt));
registerFunction("str_right", Function(&f_stringRight, ValueString, ValueString, ValueInt));
registerFunction("str_mid", Function(&f_stringMid, ValueString, ValueString, ValueInt, ValueInt, 2));
registerFunction("str_remove", Function(&f_stringRemove, ValueString, ValueString, ValueString));
registerFunction("str_replace", Function(&f_stringReplace, ValueString, ValueString, ValueString, ValueString));
registerFunction("str_lower", Function(&f_stringLower, ValueString, ValueString));
registerFunction("str_upper", Function(&f_stringUpper, ValueString, ValueString));
registerFunction("str_section", Function(&f_stringSection, ValueString, ValueString, ValueString, ValueInt, ValueInt, 3));
registerFunction("str_args", Function(&f_stringArgs, ValueString, ValueString, 2, 4));
registerFunction("str_isnumber", Function(&f_stringIsNumber, ValueInt, ValueString));
registerFunction("str_isempty", Function(&f_stringIsEmpty, ValueInt, ValueString));
registerFunction("str_toint", Function(&f_stringToInt, ValueString, ValueInt, 1));
registerFunction("str_todouble", Function(&f_stringToDouble, ValueString, ValueDouble, 1));
registerFunction("str_round", Function(&f_stringRound, ValueInt, ValueDouble, ValueInt, 2));
registerFunction("return", Function(&f_return, ValueNone, ValueString, 1, 1));
registerFunction("debug", Function(&f_debug, ValueNone, ValueString, 1, 100));
registerFunction("echo", Function(&f_echo, ValueNone, ValueString, 1, 100));
registerFunction("file_read", Function(&f_fileRead, ValueString, ValueString, 1, 1));
registerFunction("file_write", Function(&f_fileWrite, ValueInt, ValueString, ValueString, 2, 100));
registerFunction("file_append", Function(&f_fileAppend, ValueInt, ValueString, ValueString, 2, 100));
registerFunction("file_exists", Function(&f_fileExists, ValueString, ValueString, 1, 1));
registerFunction("internalDcop", Function(&f_internalDcop, ValueString, ValueString, ValueString, 2, 100));
registerFunction("executeSlot", Function(&f_executeSlot, ValueString, ValueString, ValueString, 2, 100));
registerFunction("createWidget", Function(&f_createWidget, ValueString, ValueString, ValueString, 3, 100));
registerFunction("widgetExists", Function(&f_widgetExists, ValueString, 1));
registerFunction("connect", Function(&f_connect, ValueString, ValueString, ValueString, ValueString, 4, 4));
registerFunction("disconnect", Function(&f_disconnect, ValueString, ValueString, ValueString, ValueString, 4, 4));
registerFunction("dcop", Function(&f_dcop, ValueString, ValueString, ValueString, 3, 100));
registerFunction("dcopid", Function(&f_dcopid, ValueString, ValueNone, 0, 0));
registerFunction("pid", Function(&f_pid, ValueString, ValueNone, 0, 0));
registerFunction("parentPid", Function(&f_parentPid, ValueString, ValueNone, 0, 0));
registerFunction("dialog", Function(&f_dialog, ValueString, ValueString, ValueString, 1, 2));
registerFunction("exec", Function(&f_exec, ValueString, ValueString, ValueString, 1, 2));
registerFunction("execBackground", Function(&f_execBackground, ValueString, ValueString, ValueString, 1, 2));
registerFunction("i18n", Function(&f_i18n, ValueString, ValueString));
registerFunction("env", Function(&f_env, ValueString, ValueString));
registerFunction("readSetting", Function(&f_read_setting, ValueString, ValueString, ValueString, 1));
registerFunction("writeSetting", Function(&f_write_setting, ValueNone, ValueString, ValueString));
registerFunction("array_clear", Function(&f_arrayClear, ValueNone, ValueString));
registerFunction("array_count", Function(&f_arrayCount, ValueInt, ValueString));
registerFunction("array_keys", Function(&f_arrayKeys, ValueString, ValueString));
registerFunction("array_values", Function(&f_arrayValues, ValueString, ValueString));
registerFunction("array_tostring", Function(&f_arrayToString, ValueString, ValueString));
registerFunction("array_fromstring", Function(&f_arrayFromString, ValueNone, ValueString, ValueString));
registerFunction("array_indexedfromstring", Function(&f_arrayIndexedFromString, ValueNone, ValueString, ValueString, ValueString, 2, 3));
registerFunction("array_indexedtostring", Function(&f_arrayIndexedToString, ValueNone, ValueString, ValueString, 1, 2));
registerFunction("array_indexedRemoveElements", Function(&f_arrayIndexedRemoveElements, ValueNone, ValueString, ValueInt, ValueInt, 2 , 3));
registerFunction("array_indexedInsertElements", Function(&f_arrayIndexedInsertElements, ValueNone, ValueString, ValueInt, ValueString, ValueString, 3, 4));
registerFunction("array_remove", Function(&f_arrayRemove, ValueNone, ValueString, ValueString));
registerFunction("input_color", Function(&f_inputColor, ValueString, ValueString, 0));
registerFunction("input_text", Function(&f_inputText, ValueString, ValueString, ValueString, ValueString, 2));
registerFunction("input_password", Function(&f_inputPassword, ValueString, ValueString, ValueString, 1));
registerFunction("input_value", Function(&f_inputValue, ValueInt, ValueString, ValueString, ValueInt, ValueInt,
ValueInt, ValueInt, 5));
registerFunction("input_double", Function(&f_inputValueDouble, ValueDouble, ValueString, ValueString, ValueDouble, ValueDouble,
ValueDouble, ValueDouble, 5));
registerFunction("input_openfile", Function(&f_inputOpenFile, ValueString, ValueString, ValueString, ValueString, 0));
registerFunction("input_openfiles", Function(&f_inputOpenFiles, ValueString, ValueString, ValueString, ValueString, 0));
registerFunction("input_savefile", Function(&f_inputSaveFile, ValueString, ValueString, ValueString, ValueString, 0));
registerFunction("input_directory", Function(&f_inputDirectory, ValueString, ValueString, ValueString, 0));
registerFunction("message_info", Function(&f_message_info, ValueNone, ValueString, ValueString, 1));
registerFunction("message_error", Function(&f_message_error, ValueNone, ValueString, ValueString, 1));
registerFunction("message_warning", Function(&f_message_warning, ValueInt, ValueString, ValueString,
ValueString, ValueString, ValueString, 1));
registerFunction("message_question", Function(&f_message_question, ValueInt, ValueString, ValueString,
ValueString, ValueString, ValueString, 1));
}
|