aboutsummaryrefslogtreecommitdiffstats
path: root/ui/qt/extcap_argument.cpp
blob: 0150446ae510c0f8e9a6e00edd56c0ee5f46aad9 (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
/* extcap_argument.cpp
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * 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 <extcap_argument.h>

#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#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 <QTreeView>

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

class ExtArgSelector : public ExtcapArgument
{
public:
    ExtArgSelector(extcap_arg * argument) :
        ExtcapArgument(argument), boxSelection(0) {};

    virtual QWidget * createEditor(QWidget * parent)
    {
        int counter = 0;
        int selected = -1;

        boxSelection = new QComboBox(parent);

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

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

                counter++;
                ++iter;
            }

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

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

        return boxSelection;
    }

    virtual QString value()
    {
        if ( boxSelection == 0 )
            return QString();

#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
        QVariant data = boxSelection->currentData();
#else
        QVariant data = boxSelection->itemData(boxSelection->currentIndex());
#endif

        return data.toString();
    }

private:

    QComboBox * boxSelection;
};

class ExtArgRadio : public ExtcapArgument
{
public:
    ExtArgRadio(extcap_arg * argument) :
        ExtcapArgument(argument), selectorGroup(0), callStrings(0)
    {
    };

    virtual QWidget * createEditor(QWidget * parent)
    {

        int count = 0;
        bool anyChecked = false;

        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);

                if ( _default != NULL && (*iter).isDefault() )
                {
                    radio->setChecked(true);
                    anyChecked = true;
                }
                else if (_default != NULL)
                {
                    if ( callString.compare(_default->toString()) == 0 )
                    {
                        radio->setChecked(true);
                        anyChecked = true;
                    }
                }

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

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

                ++iter;
            }
        }

        /* No default was provided, and not saved value exists */
        if ( anyChecked == false && count > 0 )
            ((QRadioButton*)(selectorGroup->button(0)))->setChecked(true);

        radioButtons->setLayout(vrLayout);

        return radioButtons;
    }

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

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

        return QString();
    }

private:

    QButtonGroup * selectorGroup;
    QList<QString> * callStrings;
};

class ExtArgBool : public ExtcapArgument
{
public:
    ExtArgBool(extcap_arg * argument) :
        ExtcapArgument(argument), boolBox(0) {};

    virtual QWidget * createLabel(QWidget * parent)
    {
        return new QWidget(parent);
    }

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

        if ( _argument->default_complex != NULL )
            if ( extcap_complex_get_bool(_argument->default_complex) == (gboolean)TRUE )
                boolBox->setCheckState(Qt::Checked);

        if ( _default != NULL )
        {
            if ( _default->toString().compare("true") )
                boolBox->setCheckState(Qt::Checked);
        }

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

        return boolBox;
    }

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

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

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

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

    virtual QString defaultValue()
    {
        if ( _argument != 0 && _argument->default_complex != NULL )
            if ( extcap_complex_get_bool(_argument->default_complex) == (gboolean)TRUE )
                return QString("true");

        return QString("false");
    }

private:

    QCheckBox * boolBox;
};

class ExtArgText : public ExtcapArgument
{

public:
    ExtArgText(extcap_arg * argument) :
        ExtcapArgument(argument), textBox(0)
    {
        _default = new QVariant(QString(""));
    };

    virtual QWidget * createEditor(QWidget * parent)
    {
        textBox = new QLineEdit(_default->toString(), parent);

        textBox->setText(defaultValue());

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

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

        return textBox;
    }

    virtual QString value()
    {
        if ( textBox == 0 )
            return QString();

        return textBox->text();
    }

    virtual bool isValid()
    {
        if ( isRequired() && value().length() == 0 )
            return false;

        return true;
    }

    virtual QString 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();
    }

protected:

    QLineEdit * textBox;
};

class ExtArgNumber : public ExtArgText
{
public:
    ExtArgNumber(extcap_arg * argument) :
        ExtArgText(argument) {};

    virtual QWidget * createEditor(QWidget * parent)
    {
        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 )
                textValidator->setBottom(extcap_complex_get_int(_argument->range_start));

            if ( _argument->arg_type == EXTCAP_ARG_UNSIGNED && textValidator->bottom() < 0 )
                textValidator->setBottom(0);

            if ( _argument->range_end != NULL )
                textValidator->setTop(extcap_complex_get_int(_argument->range_end));
            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(defaultValue());

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

        return textBox;
    };

    virtual QString defaultValue()
    {
        QString result;

        if ( _argument != 0 && _argument->default_complex != NULL )
        {
            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
                result = 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(extcap_arg * argument, QObject *parent) :
        QObject(parent), _argument(argument), _default(0)
{
    if ( _argument->values != 0 )
    {
        ExtcapValueList elements = loadValues(QString(""));
        if ( elements.length() > 0 )
            values.append(elements);
    }
}

ExtcapValueList ExtcapArgument::loadValues(QString parent)
{
    if (_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(v->parent == 0 ? "" : 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);

            element.setChildren(this->loadValues(call));
            elements.append(element);
        }
    }

    return elements;
}

ExtcapArgument::~ExtcapArgument() {
    // TODO Auto-generated destructor stub
}

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

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

    QLabel * label = new QLabel(text, parent);
    if ( _argument->tooltip != 0 )
        label->setToolTip(QString().fromUtf8(_argument->tooltip));

    return label;
}

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

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

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


bool ExtcapArgument::isValid()
{
    return value().length() > 0;
}

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

void ExtcapArgument::setDefault(GHashTable * defaultsList)
{
    if ( defaultsList != NULL && g_hash_table_size(defaultsList) > 0 )
    {
        GList * keys = g_hash_table_get_keys(defaultsList);
        while ( keys != NULL )
        {
            if ( call().compare(QString().fromUtf8((gchar *)keys->data)) == 0 )
            {
                gpointer data = g_hash_table_lookup(defaultsList, keys->data);
                QString dataStr = QString().fromUtf8((gchar *)data);
                /* We assume an empty value but set entry must be a boolflag */
                if ( dataStr.length() == 0 )
                    dataStr = "true";
                _default = new QVariant(dataStr);
                break;
            }
            keys = keys->next;
        }
    }
}

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

    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, GHashTable * device_defaults)
{
    if ( argument == 0 || argument->display == 0 )
        return 0;

    ExtcapArgument * result = 0;

    if ( argument->arg_type == EXTCAP_ARG_STRING )
        result = new ExtArgText(argument);
    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);
    else if ( argument->arg_type == EXTCAP_ARG_BOOLEAN || argument->arg_type == EXTCAP_ARG_BOOLFLAG )
        result = new ExtArgBool(argument);
    else if ( argument->arg_type == EXTCAP_ARG_SELECTOR )
        result = new ExtArgSelector(argument);
    else if ( argument->arg_type == EXTCAP_ARG_RADIO )
        result = new ExtArgRadio(argument);
    else if ( argument->arg_type == EXTCAP_ARG_FILESELECT )
        result = new ExtcapArgumentFileSelection(argument);
    else if ( argument->arg_type == EXTCAP_ARG_MULTICHECK )
        result = new ExtArgMultiSelect(argument);
    else
    {
        /* For everything else, we just print the label */
        result = new ExtcapArgument(argument);
    }

    result->setDefault(device_defaults);

    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();
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */