2015-04-28 08:26:46 +00:00
|
|
|
/* extcap_argument_file.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 <extcap_argument_file.h>
|
2015-07-31 17:34:43 +00:00
|
|
|
|
2015-10-04 17:10:29 +00:00
|
|
|
#include <wsutil/utf8_entities.h>
|
2015-07-31 17:34:43 +00:00
|
|
|
|
2015-04-28 08:26:46 +00:00
|
|
|
#include <QObject>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QBoxLayout>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QVariant>
|
|
|
|
|
2015-12-29 14:35:43 +00:00
|
|
|
#include <epan/prefs.h>
|
2017-07-25 14:15:18 +00:00
|
|
|
#include <ui/qt/utils/color_utils.h>
|
2015-04-28 08:26:46 +00:00
|
|
|
|
2016-02-29 21:29:19 +00:00
|
|
|
#include <ui/all_files_wildcard.h>
|
|
|
|
|
2015-12-29 14:35:43 +00:00
|
|
|
#include <extcap_parser.h>
|
2015-04-28 08:26:46 +00:00
|
|
|
|
|
|
|
ExtcapArgumentFileSelection::ExtcapArgumentFileSelection (extcap_arg * argument) :
|
|
|
|
ExtcapArgument(argument), textBox(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-29 13:50:55 +00:00
|
|
|
ExtcapArgumentFileSelection::~ExtcapArgumentFileSelection()
|
|
|
|
{
|
|
|
|
if ( textBox != NULL )
|
|
|
|
delete textBox;
|
|
|
|
}
|
|
|
|
|
2015-04-28 08:26:46 +00:00
|
|
|
QWidget * ExtcapArgumentFileSelection::createEditor(QWidget * parent)
|
|
|
|
{
|
2016-02-01 08:53:31 +00:00
|
|
|
QString text = defaultValue();
|
2015-12-29 06:57:36 +00:00
|
|
|
QString buttonText(UTF8_HORIZONTAL_ELLIPSIS);
|
2016-02-01 08:53:31 +00:00
|
|
|
|
2015-04-28 08:26:46 +00:00
|
|
|
QWidget * fileWidget = new QWidget(parent);
|
|
|
|
QHBoxLayout * editLayout = new QHBoxLayout();
|
|
|
|
QMargins margins = editLayout->contentsMargins();
|
|
|
|
editLayout->setContentsMargins(0, 0, 0, margins.bottom());
|
|
|
|
fileWidget->setContentsMargins(margins.left(), margins.right(), 0, margins.bottom());
|
2015-12-29 06:57:36 +00:00
|
|
|
QPushButton * button = new QPushButton(buttonText, fileWidget);
|
2015-04-28 08:26:46 +00:00
|
|
|
|
2015-12-29 06:57:36 +00:00
|
|
|
textBox = new QLineEdit(text, parent);
|
2015-04-28 08:26:46 +00:00
|
|
|
textBox->setReadOnly(true);
|
|
|
|
|
2016-09-10 23:16:24 +00:00
|
|
|
const char *prefval = _argument->pref_valptr ? *_argument->pref_valptr : NULL;
|
|
|
|
if (prefval)
|
2016-02-01 08:53:31 +00:00
|
|
|
{
|
2016-09-10 23:16:24 +00:00
|
|
|
QString storeValue(prefval);
|
2016-02-01 08:53:31 +00:00
|
|
|
|
|
|
|
if ( storeValue.length() > 0 && storeValue.compare(text) != 0 )
|
|
|
|
text = storeValue.trimmed();
|
|
|
|
}
|
|
|
|
textBox->setText(text);
|
2015-04-28 08:26:46 +00:00
|
|
|
|
|
|
|
if ( _argument->tooltip != NULL )
|
|
|
|
{
|
|
|
|
textBox->setToolTip(QString().fromUtf8(_argument->tooltip));
|
|
|
|
button->setToolTip(QString().fromUtf8(_argument->tooltip));
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(button, SIGNAL(clicked()), (QObject *)this, SLOT(openFileDialog()));
|
|
|
|
|
|
|
|
editLayout->addWidget(textBox);
|
|
|
|
editLayout->addWidget(button);
|
|
|
|
|
|
|
|
fileWidget->setLayout(editLayout);
|
|
|
|
|
|
|
|
return fileWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ExtcapArgumentFileSelection::value()
|
|
|
|
{
|
|
|
|
if ( textBox == 0 )
|
|
|
|
return QString();
|
|
|
|
return textBox->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* opens the file dialog */
|
|
|
|
void ExtcapArgumentFileSelection::openFileDialog()
|
|
|
|
{
|
|
|
|
QString filename = textBox->text();
|
|
|
|
|
|
|
|
QDir workingDir = QDir::currentPath();
|
|
|
|
if (QFileInfo(filename).exists())
|
|
|
|
workingDir = QFileInfo(filename).dir();
|
|
|
|
|
2016-02-29 21:29:19 +00:00
|
|
|
QString fileExt(tr("All Files (" ALL_FILES_WILDCARD ")"));
|
2015-12-29 13:57:53 +00:00
|
|
|
if ( _argument->fileextension != NULL )
|
|
|
|
{
|
|
|
|
QString givenExt = QString().fromUtf8(_argument->fileextension);
|
|
|
|
if ( givenExt.length() != 0 )
|
|
|
|
fileExt.prepend(";;").prepend(givenExt);
|
|
|
|
}
|
|
|
|
|
2015-04-28 08:26:46 +00:00
|
|
|
filename = QFileDialog::getOpenFileName((QWidget *)(textBox->parent()),
|
|
|
|
QString().fromUtf8(_argument->display) + " " + tr("Open File"),
|
2015-12-29 13:57:53 +00:00
|
|
|
workingDir.absolutePath(), fileExt);
|
2015-04-28 08:26:46 +00:00
|
|
|
|
2015-12-29 13:57:53 +00:00
|
|
|
if ( ! fileExists() || QFileInfo(filename).exists() )
|
|
|
|
{
|
2015-04-28 08:26:46 +00:00
|
|
|
textBox->setText(filename);
|
2015-12-29 13:57:53 +00:00
|
|
|
emit valueChanged();
|
|
|
|
}
|
2015-04-28 08:26:46 +00:00
|
|
|
}
|
|
|
|
|
2015-12-29 13:50:55 +00:00
|
|
|
bool ExtcapArgumentFileSelection::isValid()
|
|
|
|
{
|
2015-12-29 14:35:43 +00:00
|
|
|
bool valid = false;
|
|
|
|
|
2016-02-01 08:53:31 +00:00
|
|
|
if ( textBox->text().length() > 0 )
|
|
|
|
{
|
2017-02-25 22:44:15 +00:00
|
|
|
if (_argument->fileexists)
|
|
|
|
valid = QFileInfo(textBox->text()).exists();
|
|
|
|
else
|
2016-02-01 08:53:31 +00:00
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
else if ( ! isRequired() )
|
2015-12-29 14:35:43 +00:00
|
|
|
valid = true;
|
|
|
|
|
|
|
|
QString lblInvalidColor = ColorUtils::fromColorT(prefs.gui_text_invalid).name();
|
|
|
|
QString txtStyle("QLineEdit { background-color: %1; } ");
|
|
|
|
textBox->setStyleSheet( txtStyle.arg(valid ? QString("") : lblInvalidColor) );
|
|
|
|
|
|
|
|
return valid;
|
2015-12-29 13:50:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 08:26:46 +00:00
|
|
|
/*
|
|
|
|
* 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:
|
|
|
|
*/
|