summaryrefslogtreecommitdiffstats
path: root/ui/qt/extcap_argument.cpp
blob: 0a965a79883f3775458d21333e094b8403702a9d (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
/* extcap_argument.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <extcap_argument.h>

#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QDateTimeEdit>
#include <QIntValidator>
#include <QDoubleValidator>
#include <QCheckBox>
#include <QButtonGroup>
#include <QBoxLayout>
#include <QRadioButton>
#include <QComboBox>
#include <QPushButton>
#include <QMargins>
#include <QVariant>
#include <QAbstractItemModel>
#include <QStringList>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include <QRegularExpression>

#include <glib.h>

#include <extcap.h>
#include <epan/prefs.h>
#include <epan/prefs-int.h>
#include <wsutil/wslog.h>
#include <ui/qt/utils/color_utils.h>

#include <extcap_parser.h>
#include <extcap_argument_file.h>
#include <extcap_argument_multiselect.h>

#include <ui/qt/extcap_options_dialog.h>

ExtArgTimestamp::ExtArgTimestamp(extcap_arg * argument, QObject * parent) :
    ExtcapArgument(argument, parent), tsBox(0) {}

QWidget * ExtArgTimestamp::createEditor(QWidget * parent)
{
    QString text = defaultValue();

    if (_argument->pref_valptr && strlen(*_argument->pref_valptr))
    {
        QString storeValue(*_argument->pref_valptr);
        text = storeValue.trimmed();
    }

    ts = QDateTime::fromSecsSinceEpoch(text.toInt());
    tsBox = new QDateTimeEdit(ts, parent);
    tsBox->setDisplayFormat(QLocale::system().dateTimeFormat());
    tsBox->setCalendarPopup(true);
    tsBox->setAutoFillBackground(true);

    if (_argument->tooltip != NULL)
        tsBox->setToolTip(QString().fromUtf8(_argument->tooltip));

    connect(tsBox, SIGNAL(dateTimeChanged(QDateTime)), SLOT(onDateTimeChanged(QDateTime)));

    return tsBox;
}

void ExtArgTimestamp::onDateTimeChanged(QDateTime t)
{
    ts = t;
    emit valueChanged();
}

QString ExtArgTimestamp::defaultValue()
{
    return QString::number(QDateTime::currentDateTime().toSecsSinceEpoch());
}

bool ExtArgTimestamp::isValid()
{
    bool valid = true;

    if (value().length() == 0 && isRequired())
        valid = false;

    return valid;
}

QString ExtArgTimestamp::value()
{
    return QString::number(ts.toSecsSinceEpoch());
}

QString ExtArgTimestamp::prefValue()
{
    return value();
}

bool ExtArgTimestamp::isSetDefaultValueSupported()
{
    return TRUE;
}

void ExtArgTimestamp::setDefaultValue()
{
    QDateTime t;

    t = QDateTime::fromSecsSinceEpoch(defaultValue().toInt());
    tsBox->setDateTime(t);
}



ExtArgSelector::ExtArgSelector(extcap_arg * argument, QObject * parent) :
        ExtcapArgument(argument, parent), boxSelection(0) {}

QWidget * ExtArgSelector::createEditor(QWidget * parent)
{
    QWidget * editor = new QWidget(parent);
    QHBoxLayout * layout = new QHBoxLayout();
    QMargins margins = layout->contentsMargins();
    layout->setContentsMargins(0, margins.top(), 0, margins.bottom());

    boxSelection = new QComboBox(parent);
    boxSelection->setToolTip(QString().fromUtf8(_argument->tooltip));
    layout->addWidget(boxSelection);

    if (values.length() > 0)
    {
        ExtcapValueList::const_iterator iter = values.constBegin();

        while (iter != values.constEnd())
        {
            boxSelection->addItem((*iter).value(), (*iter).call());
            ++iter;
        }
    }

    setDefaultValue();

    if (reload())
    {
        QString btnText(tr("Reload data"));
        if (_argument->placeholder)
            btnText = QString(_argument->placeholder);

        QPushButton * reloadButton = new QPushButton(btnText, editor);
        layout->addWidget(reloadButton);
        reloadButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
        boxSelection->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);

        connect(reloadButton, SIGNAL(clicked()), this, SLOT(onReloadTriggered()));
    }

    connect (boxSelection, SIGNAL(currentIndexChanged(int)), SLOT(onIntChanged(int)));

    editor->setLayout(layout);

    return editor;
}

void ExtArgSelector::onReloadTriggered()
{
    int counter = 0;
    int selected = -1;

    QString call = boxSelection->currentData().toString();
    const char *prefval = (_argument->pref_valptr && strlen(*_argument->pref_valptr)) ? *_argument->pref_valptr : NULL;
    QString stored(prefval ? prefval : "");
    if (call != stored)
        stored = call;

    if (reloadValues() && values.length() > 0)
    {
        boxSelection->clear();

        ExtcapValueList::const_iterator iter = values.constBegin();

        while (iter != values.constEnd())
        {
            boxSelection->addItem((*iter).value(), (*iter).call());

            if (stored.compare((*iter).call()) == 0)
                selected = counter;
            else if ((*iter).isDefault() && selected == -1)
                selected = counter;

            counter++;
            ++iter;
        }

        if (selected > -1 && selected < boxSelection->count())
            boxSelection->setCurrentIndex(selected);
    }
}

bool ExtArgSelector::isValid()
{
    bool valid = true;

    if (value().length() == 0 && isRequired())
        valid = false;

    if (boxSelection)
    {
        QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();
        QString cmbBoxStyle("QComboBox { background-color: %1; } ");
        boxSelection->setStyleSheet(cmbBoxStyle.arg(valid ? QString("") : lblInvalidColor));
    }

    return valid;
}

QString ExtArgSelector::value()
{
    if (boxSelection == 0)
        return QString();

    QVariant data = boxSelection->currentData();

    return data.toString();
}

bool ExtArgSelector::isSetDefaultValueSupported()
{
    return TRUE;
}

void ExtArgSelector::setDefaultValue()
{
    int counter = 0;
    int selected = -1;

    const char *prefval = (_argument->pref_valptr && strlen(*_argument->pref_valptr)) ? *_argument->pref_valptr : NULL;
    QString stored(prefval ? prefval : "");

    if (values.length() > 0)
    {
        ExtcapValueList::const_iterator iter = values.constBegin();

        while (iter != values.constEnd())
        {
            if (!prefval && (*iter).isDefault())
                selected = counter;
            else if (prefval && stored.compare((*iter).call()) == 0)
                selected = counter;

            counter++;
            ++iter;
        }

        if (selected > -1 && selected < boxSelection->count())
            boxSelection->setCurrentIndex(selected);
    }

}


ExtArgEditSelector::ExtArgEditSelector(extcap_arg * argument, QObject * parent) :
        ExtArgSelector(argument, parent) {}

QWidget * ExtArgEditSelector::createEditor(QWidget * parent)
{
    QWidget *editor = ExtArgSelector::createEditor(parent);

    boxSelection->setEditable(true);
    boxSelection->setInsertPolicy(QComboBox::NoInsert);

    return editor;
}

QString ExtArgEditSelector::value()
{
    if (boxSelection == nullptr) {
        return QString();
    }

    if (boxSelection->currentIndex() > -1) {
        return ExtArgSelector::value();
    }

    return boxSelection->currentText();
}

void ExtArgEditSelector::setDefaultValue()
{
    ExtArgSelector::setDefaultValue();

    if (boxSelection == nullptr) {
        return;
    }

    const char *prefval = (_argument->pref_valptr && strlen(*_argument->pref_valptr)) ? *_argument->pref_valptr : NULL;
    QString stored(prefval ? prefval : "");
    QVariant data = boxSelection->currentData();

    if (data.toString() != stored) {
        // createEditor may not have been called at this point?
        boxSelection->setEditable(true);
        boxSelection->setInsertPolicy(QComboBox::NoInsert);
        boxSelection->setEditText(stored);
    }
}


ExtArgRadio::ExtArgRadio(extcap_arg * argument, QObject * parent) :
        ExtcapArgument(argument, parent), selectorGroup(0), callStrings(0) {}

QWidget * ExtArgRadio::createEditor(QWidget * parent)
{
    int count = 0;

    selectorGroup = new QButtonGroup(parent);
    QWidget * radioButtons = new QWidget;
    QVBoxLayout * vrLayout = new QVBoxLayout();
    QMargins margins = vrLayout->contentsMargins();
    vrLayout->setContentsMargins(0, 0, 0, margins.bottom());
    if (callStrings != 0)
        delete callStrings;

    callStrings = new QList<QString>();

    if (values.length() > 0)
    {
        ExtcapValueList::const_iterator iter = values.constBegin();

        while (iter != values.constEnd())
        {
            QRadioButton * radio = new QRadioButton((*iter).value());
            QString callString = (*iter).call();
            callStrings->append(callString);

            connect(radio, SIGNAL(clicked(bool)), SLOT(onBoolChanged(bool)));
            selectorGroup->addButton(radio, count);

            vrLayout->addWidget(radio);
            count++;

            ++iter;
        }
    }

    setDefaultValue();

    radioButtons->setLayout(vrLayout);

    return radioButtons;
}

QString ExtArgRadio::value()
{
    int idx = 0;
    if (selectorGroup == 0 || callStrings == 0)
        return QString();

    idx = selectorGroup->checkedId();
    if (idx > -1 && callStrings->length() > idx)
        return callStrings->at(idx);

    return QString();
}

bool ExtArgRadio::isValid()
{
    bool valid = true;
    int idx = 0;

    if (isRequired())
    {
        if (selectorGroup == 0 || callStrings == 0)
            valid = false;
        else
        {
            idx = selectorGroup->checkedId();
            if (idx == -1 || callStrings->length() <= idx)
                valid = false;
        }
    }

    /* If nothing is selected, but a selection is required, the only thing that
     * can be marked is the label */
    QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();
    _label->setStyleSheet (label_style.arg(valid ? QString("") : lblInvalidColor));

    return valid;
}

bool ExtArgRadio::isSetDefaultValueSupported()
{
    return TRUE;
}

void ExtArgRadio::setDefaultValue()
{
    int counter = 0;
    int selected = 0;

    const char *prefval = (_argument->pref_valptr && strlen(*_argument->pref_valptr)) ? *_argument->pref_valptr : NULL;
    QString stored(prefval ? prefval : "");

    if (values.length() > 0)
    {
        ExtcapValueList::const_iterator iter = values.constBegin();

        while (iter != values.constEnd())
        {
            if (!prefval && (*iter).isDefault())
                selected = counter;
            else if (prefval && stored.compare((*iter).call()) == 0)
                selected = counter;

            counter++;
            ++iter;
        }

        ((QRadioButton*)(selectorGroup->button(selected)))->setChecked(true);
    }
}



ExtArgBool::ExtArgBool(extcap_arg * argument, QObject * parent) :
        ExtcapArgument(argument, parent), boolBox(0) {}

QWidget * ExtArgBool::createLabel(QWidget * parent)
{
    return new QWidget(parent);
}

QWidget * ExtArgBool::createEditor(QWidget * parent)
{
    bool state = defaultBool();

    boolBox = new QCheckBox(QString().fromUtf8(_argument->display), parent);
    if (_argument->tooltip != NULL)
        boolBox->setToolTip(QString().fromUtf8(_argument->tooltip));

    const char *prefval = (_argument->pref_valptr && strlen(*_argument->pref_valptr)) ? *_argument->pref_valptr : NULL;
    if (prefval)
    {
        QRegularExpression regexp(EXTCAP_BOOLEAN_REGEX);
        QRegularExpressionMatch match = regexp.match(QString(prefval[0]));
        bool savedstate = match.hasMatch();
        if (savedstate != state)
            state = savedstate;
    }

    boolBox->setCheckState(state ? Qt::Checked : Qt::Unchecked);

    connect (boolBox, SIGNAL(stateChanged(int)), SLOT(onIntChanged(int)));

    return boolBox;
}

QString ExtArgBool::call()
{
    if (boolBox == NULL)
        return QString("");

    if (_argument->arg_type == EXTCAP_ARG_BOOLEAN)
        return ExtcapArgument::call();

    return QString(boolBox->checkState() == Qt::Checked ? _argument->call : "");
}

QString ExtArgBool::value()
{
    if (boolBox == NULL || _argument->arg_type == EXTCAP_ARG_BOOLFLAG)
        return QString();
    return QString(boolBox->checkState() == Qt::Checked ? "true" : "false");
}

QString ExtArgBool::prefValue()
{
    if (boolBox == NULL)
        return QString("false");
    return QString(boolBox->checkState() == Qt::Checked ? "true" : "false");
}

bool ExtArgBool::isValid()
{
    /* A bool is always valid, but the base function checks on string length,
     * which will fail with boolflags */
    return true;
}

bool ExtArgBool::defaultBool()
{
    bool result = false;

    if (_argument)
    {
        if (extcap_complex_get_bool(_argument->default_complex) == (gboolean)TRUE)
            result = true;
    }

    return result;
}

QString ExtArgBool::defaultValue()
{
    return defaultBool() ? QString("true") : QString("false");
}

bool ExtArgBool::isSetDefaultValueSupported()
{
    return TRUE;
}

void ExtArgBool::setDefaultValue()
{
    boolBox->setCheckState(defaultBool() ? Qt::Checked : Qt::Unchecked);
}



ExtArgText::ExtArgText(extcap_arg * argument, QObject * parent) :
    ExtcapArgument(argument, parent), textBox(0)
{
}

QWidget * ExtArgText::createEditor(QWidget * parent)
{
    QString text = defaultValue();

    /* Prefs can contain empty string. We accept it. */
    if (_argument->pref_valptr && (*_argument->pref_valptr))
    {
        QString storeValue(*_argument->pref_valptr);
        text = storeValue.trimmed();
    }

    textBox = new QLineEdit(text, parent);

    if (_argument->tooltip != NULL)
        textBox->setToolTip(QString().fromUtf8(_argument->tooltip));

    if (_argument->placeholder != NULL)
        textBox->setPlaceholderText(QString().fromUtf8(_argument->placeholder));

    if (_argument->arg_type == EXTCAP_ARG_PASSWORD)
        textBox->setEchoMode(QLineEdit::PasswordEchoOnEdit);

    connect(textBox , SIGNAL(textChanged(QString)), SLOT(onStringChanged(QString)));

    return textBox;
}

QString ExtArgText::value()
{
    if (textBox == 0)
        return QString();

    return textBox->text();
}

bool ExtArgText::isValid()
{
    bool valid = true;

    if (isRequired() && value().length() == 0)
        valid = false;

    /* Does the validator, if any, consider the value valid?
     *
     * If it considers it an "intermediate" value, rather than an "invalid"
     * value, the user will be able to transfer the input focus to another
     * widget, and, as long as all widgets have values for which isValid()
     * is true, they wil be able to click the "Start" button.
     *
     * For QIntValidator(), used for integral fields with minimum and
     * maximum values, a value that's larger than the maximum but has
     * the same number of digits as the maximum is "intermediate" rather
     * than "invalid", so the user will be able to cause that value to
     * be passed to the extcap module; see bug 16510.
     *
     * So we explicitly call the hasAcceptableInput() method on the
     * text box; that returns false if the value is not "valid", and
     * that includes "intermediate" values.
     *
     * This way, 1) non-colorblind users are informed that the value
     * is invalid by the text box background being red (perhaps the
     * user isn't fully colorblind, or perhaps the background is a
     * noticeably different grayscale), and 2) the user won't be able
     * to start the capture until they fix the problem.
     *
     * XXX - it might be nice to have some way of indicating to the
     * user what the problem is with the value - alert box?  Tooltip? */
    if (!textBox->hasAcceptableInput())
        valid = false;

    /* validation should only be checked if there is a value. if the argument
     * must be present (isRequired) the check above will handle that */
    if (valid && _argument->regexp != NULL && value().length() > 0)
    {
        QString regexp = QString().fromUtf8(_argument->regexp);
        if (regexp.length() > 0)
        {
            QRegularExpression expr(regexp, QRegularExpression::UseUnicodePropertiesOption);
            if (! expr.isValid() || ! expr.match(value()).hasMatch())
                valid = false;
        }
    }

    QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();
    QString txtStyle("QLineEdit { background-color: %1; } ");
    textBox->setStyleSheet(txtStyle.arg(valid ? QString("") : lblInvalidColor));

    return valid;
}

bool ExtArgText::isSetDefaultValueSupported()
{
    return TRUE;
}

void ExtArgText::setDefaultValue()
{
    textBox->setText(defaultValue());
}



ExtArgNumber::ExtArgNumber(extcap_arg * argument, QObject * parent) :
        ExtArgText(argument, parent) {}

QWidget * ExtArgNumber::createEditor(QWidget * parent)
{
    QString text = defaultValue();

    if (_argument->pref_valptr && strlen(*_argument->pref_valptr))
    {
        QString storeValue(*_argument->pref_valptr);
        text = storeValue;
    }

    textBox = (QLineEdit *)ExtArgText::createEditor(parent);
    textBox->disconnect(SIGNAL(textChanged(QString)));

    if (_argument->arg_type == EXTCAP_ARG_INTEGER || _argument->arg_type == EXTCAP_ARG_UNSIGNED)
    {
        QIntValidator * textValidator = new QIntValidator(parent);
        if (_argument->range_start != NULL)
        {
            int val = 0;
            if (_argument->arg_type == EXTCAP_ARG_INTEGER)
                val = extcap_complex_get_int(_argument->range_start);
            else if (_argument->arg_type == EXTCAP_ARG_UNSIGNED)
            {
                guint tmp = extcap_complex_get_uint(_argument->range_start);
                if (tmp > G_MAXINT)
                {
                    ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "Defined value for range_start of %s exceeds valid integer range", _argument->call);
                    val = G_MAXINT;
                }
                else
                    val = (gint)tmp;
            }

            textValidator->setBottom(val);
        }
        if (_argument->arg_type == EXTCAP_ARG_UNSIGNED && textValidator->bottom() < 0)
        {
            ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "%s sets negative bottom range for unsigned value, setting to 0", _argument->call);
            textValidator->setBottom(0);
        }

        if (_argument->range_end != NULL)
        {
            int val = 0;
            if (_argument->arg_type == EXTCAP_ARG_INTEGER)
                val = extcap_complex_get_int(_argument->range_end);
            else if (_argument->arg_type == EXTCAP_ARG_UNSIGNED)
            {
                guint tmp = extcap_complex_get_uint(_argument->range_end);
                if (tmp > G_MAXINT)
                {
                    ws_log(LOG_DOMAIN_CAPTURE, LOG_LEVEL_DEBUG, "Defined value for range_end of %s exceeds valid integer range", _argument->call);
                    val = G_MAXINT;
                }
                else
                    val = (gint)tmp;
            }

            textValidator->setTop(val);
        }
        textBox->setValidator(textValidator);
    }
    else if (_argument->arg_type == EXTCAP_ARG_DOUBLE)
    {
        QDoubleValidator * textValidator = new QDoubleValidator(parent);
        if (_argument->range_start != NULL)
            textValidator->setBottom(extcap_complex_get_double(_argument->range_start));
        if (_argument->range_end != NULL)
            textValidator->setTop(extcap_complex_get_double(_argument->range_end));

        textBox->setValidator(textValidator);
    }

    textBox->setText(text.trimmed());

    connect(textBox, SIGNAL(textChanged(QString)), SLOT(onStringChanged(QString)));

    return textBox;
}

QString ExtArgNumber::defaultValue()
{
    QString result;

    if (_argument != 0)
    {
        if (_argument->arg_type == EXTCAP_ARG_DOUBLE)
            result = QString::number(extcap_complex_get_double(_argument->default_complex));
        else if (_argument->arg_type == EXTCAP_ARG_INTEGER)
            result = QString::number(extcap_complex_get_int(_argument->default_complex));
        else if (_argument->arg_type == EXTCAP_ARG_UNSIGNED)
            result = QString::number(extcap_complex_get_uint(_argument->default_complex));
        else if (_argument->arg_type == EXTCAP_ARG_LONG)
            result = QString::number(extcap_complex_get_long(_argument->default_complex));
        else
        {
            QString defValue = ExtcapArgument::defaultValue();
            result = defValue.length() > 0 ? defValue : QString();
        }
    }

    return result;
}

ExtcapValue::~ExtcapValue() {}

void ExtcapValue::setChildren(ExtcapValueList children)
{
    ExtcapValueList::iterator iter = children.begin();
    while (iter != children.end())
    {
        (*iter)._depth = _depth + 1;
        ++iter;
    }

    _children.append(children);
}

ExtcapArgument::ExtcapArgument(QObject *parent) :
        QObject(parent), _argument(0), _label(0), _number(0),
        label_style(QString("QLabel { color: %1; }"))
{
}

ExtcapArgument::ExtcapArgument(extcap_arg * argument, QObject *parent) :
        QObject(parent), _argument(argument), _label(0),
        label_style(QString("QLabel { color: %1; }"))
{
    _number = argument->arg_num;

    if (_argument->values != 0)
    {
        ExtcapValueList elements = loadValues(QString(""));
        if (elements.length() > 0)
            values.append(elements);
    }
}

ExtcapArgument::ExtcapArgument(const ExtcapArgument &obj) :
        QObject(obj.parent()), _argument(obj._argument), _label(0),
        label_style(QString("QLabel { color: %1; }"))
{
    _number = obj._argument->arg_num;

    if (_argument->values != 0)
    {
        ExtcapValueList elements = loadValues(QString(""));
        if (elements.length() > 0)
            values.append(elements);
    }
}

ExtcapValueList ExtcapArgument::loadValues(QString parent)
{
    if (_argument == 0 || _argument->values == 0)
        return ExtcapValueList();

    GList * walker = 0;
    extcap_value * v;
    ExtcapValueList elements;

    for (walker = g_list_first((GList *)(_argument->values)); walker != NULL ; walker = walker->next)
    {
        v = (extcap_value *) walker->data;
        if (v == NULL || v->display == NULL || v->call == NULL)
            break;

        QString valParent = QString().fromUtf8(v->parent);

        if (parent.compare(valParent) == 0)
        {

            QString display = QString().fromUtf8(v->display);
            QString call = QString().fromUtf8(v->call);

            ExtcapValue element = ExtcapValue(display, call,
                            v->enabled == (gboolean)TRUE, v->is_default == (gboolean)TRUE);

            if (!call.isEmpty())
                element.setChildren(this->loadValues(call));

            elements.append(element);
        }
    }

    return elements;
}

bool ExtcapArgument::reloadValues()
{
    if (! qobject_cast<ExtcapOptionsDialog*> (parent()) )
        return false;

    ExtcapOptionsDialog * dialog = qobject_cast<ExtcapOptionsDialog*>(parent());
    ExtcapValueList list = dialog->loadValuesFor(_argument->arg_num, _argument->call);

    if (list.size() > 0)
    {
        values.clear();
        values << list;

        return true;
    }

    return false;
}

ExtcapArgument::~ExtcapArgument() {
}

QWidget * ExtcapArgument::createLabel(QWidget * parent)
{
    if (_argument == 0 || _argument->display == 0)
        return 0;

    QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();

    QString text = QString().fromUtf8(_argument->display);

    if (_label == 0)
        _label = new QLabel(text, parent);
    else
        _label->setText(text);

    _label->setProperty("isRequired", QString(isRequired() ? "true" : "false"));

    _label->setStyleSheet (label_style.arg(QString("")));

    if (_argument->tooltip != 0)
        _label->setToolTip(QString().fromUtf8(_argument->tooltip));

    return (QWidget *)_label;
}

QWidget * ExtcapArgument::createEditor(QWidget *)
{
    return 0;
}

QString ExtcapArgument::call()
{
    return QString(_argument->call);
}

QString ExtcapArgument::value()
{
    return QString();
}

QString ExtcapArgument::prefValue()
{
    return value();
}

void ExtcapArgument::resetValue()
{
    if (_argument->pref_valptr) {
        g_free(*_argument->pref_valptr);
        *_argument->pref_valptr = g_strdup("");
    }
}

bool ExtcapArgument::isValid()
{
    /* Unrequired arguments are always valid, except if validity checks fail,
     * which must be checked in an derived class, not here */
    if (! isRequired())
        return true;

    return value().length() > 0;
}

QString ExtcapArgument::defaultValue()
{
    if (_argument != 0 && _argument->default_complex != 0)
    {
        gchar * str = extcap_get_complex_as_string(_argument->default_complex);
        if (str != 0)
            return QString(str);
    }
    return QString();
}

QString ExtcapArgument::group() const
{
    if (_argument != 0 && _argument->group != 0)
        return QString(_argument->group);

    return QString();
}

int ExtcapArgument::argNr() const
{
    return _number;
}

QString ExtcapArgument::prefKey(const QString & device_name)
{
    struct preference * pref = NULL;

    if (_argument == 0 || ! _argument->save)
        return QString();

    pref = extcap_pref_for_argument(device_name.toStdString().c_str(), _argument);
    if (pref != NULL)
        return QString(prefs_get_name(pref));

    return QString();
}

bool ExtcapArgument::isRequired()
{
    if (_argument != NULL)
        return _argument->is_required;

    return FALSE;
}

bool ExtcapArgument::reload()
{
    if (_argument != NULL)
        return _argument->reload;

    return false;
}

bool ExtcapArgument::fileExists()
{
    if (_argument != NULL)
        return _argument->fileexists;

    return FALSE;
}

bool ExtcapArgument::isDefault()
{
    if (value().compare(defaultValue()) == 0)
        return true;

    return false;
}

ExtcapArgument * ExtcapArgument::create(extcap_arg * argument, QObject *parent)
{
    if (argument == 0 || argument->display == 0)
        return 0;

    ExtcapArgument * result = 0;

    if (argument->arg_type == EXTCAP_ARG_STRING || argument->arg_type == EXTCAP_ARG_PASSWORD)
        result = new ExtArgText(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_INTEGER || argument->arg_type == EXTCAP_ARG_LONG ||
            argument->arg_type == EXTCAP_ARG_UNSIGNED || argument->arg_type == EXTCAP_ARG_DOUBLE)
        result = new ExtArgNumber(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_BOOLEAN || argument->arg_type == EXTCAP_ARG_BOOLFLAG)
        result = new ExtArgBool(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_SELECTOR)
        result = new ExtArgSelector(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_EDIT_SELECTOR)
        result = new ExtArgEditSelector(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_RADIO)
        result = new ExtArgRadio(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_FILESELECT)
        result = new ExtcapArgumentFileSelection(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_MULTICHECK)
        result = new ExtArgMultiSelect(argument, parent);
    else if (argument->arg_type == EXTCAP_ARG_TIMESTAMP)
        result = new ExtArgTimestamp(argument, parent);
    else
    {
        /* For everything else, we just print the label */
        result = new ExtcapArgument(argument, parent);
    }

    return result;
}

/* The following is a necessity, because Q_Object does not do well with multiple inheritances */
void ExtcapArgument::onStringChanged(QString)
{
    emit valueChanged();
}

void ExtcapArgument::onIntChanged(int)
{
    if (isValid())
        emit valueChanged();
}

void ExtcapArgument::onBoolChanged(bool)
{
    emit valueChanged();
}

bool ExtcapArgument::isSetDefaultValueSupported()
{
    return FALSE;
}

void ExtcapArgument::setDefaultValue()
{
}