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
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"
22 #include <QPushButton>
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()
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
;
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
);
91 QString
ExtcapArgumentFileSelection::value()
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
);
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
);
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
);
139 void ExtcapArgumentFileSelection::clearFilename()
145 bool ExtcapArgumentFileSelection::isValid()
149 if (textBox
->text().length() > 0)
151 if (_argument
->fileexists
)
152 valid
= QFileInfo(textBox
->text()).exists();
156 else if (! isRequired())
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
));
166 void ExtcapArgumentFileSelection::setDefaultValue()