TODO netlogon_user_flags_ntlmv2_enabled
[wireshark-sm.git] / ui / qt / extcap_argument_file.cpp
blob6b15ebf5b94194049860c67b0d7ee1ec2a8b034d
1 /* extcap_argument_file.cpp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include <extcap_argument.h>
11 #include <extcap_argument_file.h>
13 #include <wsutil/utf8_entities.h>
15 #include "ui/qt/widgets/wireshark_file_dialog.h"
17 #include <QObject>
18 #include <QWidget>
19 #include <QLabel>
20 #include <QLineEdit>
21 #include <QBoxLayout>
22 #include <QPushButton>
23 #include <QDir>
24 #include <QFileInfo>
25 #include <QVariant>
27 #include <epan/prefs.h>
28 #include <ui/qt/utils/color_utils.h>
30 #include <ui/all_files_wildcard.h>
32 #include <extcap_parser.h>
34 ExtcapArgumentFileSelection::ExtcapArgumentFileSelection (extcap_arg * argument, QObject *parent) :
35 ExtcapArgument(argument, parent), textBox(0)
39 ExtcapArgumentFileSelection::~ExtcapArgumentFileSelection()
41 if (textBox != NULL)
42 delete textBox;
45 QWidget * ExtcapArgumentFileSelection::createEditor(QWidget * parent)
47 QString text = defaultValue();
48 QString buttonText(UTF8_HORIZONTAL_ELLIPSIS);
49 QString buttonClearText(tr("Clear"));
51 QWidget * fileWidget = new QWidget(parent);
52 QHBoxLayout * editLayout = new QHBoxLayout();
53 QMargins margins = editLayout->contentsMargins();
54 editLayout->setContentsMargins(0, 0, 0, margins.bottom());
55 fileWidget->setContentsMargins(margins.left(), margins.right(), 0, margins.bottom());
56 QPushButton * buttonSelect = new QPushButton(buttonText, fileWidget);
57 QPushButton * buttonClear = new QPushButton(buttonClearText, fileWidget);
59 textBox = new QLineEdit(text, parent);
60 textBox->setReadOnly(true);
62 /* Value is empty if no file is selected */
63 const char *prefval = (_argument->pref_valptr && (*_argument->pref_valptr)) ? *_argument->pref_valptr : NULL;
64 if (prefval)
66 QString storeValue(prefval);
68 if (storeValue.length() > 0 && storeValue.compare(text) != 0)
69 text = storeValue.trimmed();
71 textBox->setText(text);
73 if (_argument->tooltip != NULL)
75 textBox->setToolTip(QString().fromUtf8(_argument->tooltip));
76 buttonSelect->setToolTip(QString().fromUtf8(_argument->tooltip));
79 connect(buttonSelect, &QPushButton::clicked, this, &ExtcapArgumentFileSelection::openFileDialog);
80 connect(buttonClear, &QPushButton::clicked, this, &ExtcapArgumentFileSelection::clearFilename);
82 editLayout->addWidget(textBox);
83 editLayout->addWidget(buttonSelect);
84 editLayout->addWidget(buttonClear);
86 fileWidget->setLayout(editLayout);
88 return fileWidget;
91 QString ExtcapArgumentFileSelection::value()
93 if (textBox == 0)
94 return QString();
95 return textBox->text();
98 /* opens the file dialog */
99 void ExtcapArgumentFileSelection::openFileDialog()
101 QString filename = textBox->text();
103 QDir workingDir = QDir::currentPath();
104 if (QFileInfo(filename).exists())
105 workingDir = QFileInfo(filename).dir();
107 QString fileExt(tr("All Files (" ALL_FILES_WILDCARD ")"));
108 if (_argument->fileextension != NULL)
110 QString givenExt = QString().fromUtf8(_argument->fileextension);
111 if (givenExt.length() != 0)
112 fileExt.prepend(";;").prepend(givenExt);
115 if (fileExists())
117 /* UI should check that the file exists */
118 filename = WiresharkFileDialog::getOpenFileName((QWidget*)(textBox->parent()),
119 tr("%1 Open File").arg(QString::fromUtf8(_argument->display)),
120 workingDir.absolutePath(), fileExt);
122 else
124 /* File might or might not exist. Actual overwrite handling is extcap specific
125 * (e.g. boolflag argument if user wants to always overwrite the file)
127 filename = WiresharkFileDialog::getSaveFileName((QWidget*)(textBox->parent()),
128 tr("%1 Select File").arg(QString::fromUtf8(_argument->display)),
129 workingDir.absolutePath(), fileExt, nullptr, QFileDialog::Option::DontConfirmOverwrite);
132 if (! filename.isEmpty() && (! fileExists() || QFileInfo(filename).exists()))
134 textBox->setText(filename);
135 emit valueChanged();
139 void ExtcapArgumentFileSelection::clearFilename()
141 textBox->clear();
142 emit valueChanged();
145 bool ExtcapArgumentFileSelection::isValid()
147 bool valid = false;
149 if (textBox->text().length() > 0)
151 if (_argument->fileexists)
152 valid = QFileInfo(textBox->text()).exists();
153 else
154 valid = true;
156 else if (! isRequired())
157 valid = true;
159 QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();
160 QString txtStyle("QLineEdit { background-color: %1; } ");
161 textBox->setStyleSheet(txtStyle.arg(valid ? QString("") : lblInvalidColor));
163 return valid;
166 void ExtcapArgumentFileSelection::setDefaultValue()
168 clearFilename();