extcap: Add file extension check

The file-open dialog can now be set with file extensions, allowing
 the exclusion of unwanted file types. The syntax is the same
 as for the Qt QFileDialog, e.g.: "Wireshark (*.pcap *.pcapng)"

 Also, the mustexist option is now considered correctly

Change-Id: I9d4efbb5089ce1af640b2a894de07ed79520271e
Reviewed-on: https://code.wireshark.org/review/12913
Reviewed-by: Roland Knall <rknall@gmail.com>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Roland Knall 2015-12-29 14:57:53 +01:00 committed by Anders Broman
parent 0921c8214e
commit 0d47113ddc
5 changed files with 37 additions and 3 deletions

View File

@ -339,6 +339,8 @@ extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
tv->param_type = EXTCAP_PARAM_TOOLTIP;
} else if (g_ascii_strcasecmp(tv->arg, "mustexist") == 0) {
tv->param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
} else if (g_ascii_strcasecmp(tv->arg, "fileext") == 0) {
tv->param_type = EXTCAP_PARAM_FILE_EXTENSION;
} else if (g_ascii_strcasecmp(tv->arg, "name") == 0) {
tv->param_type = EXTCAP_PARAM_NAME;
} else if (g_ascii_strcasecmp(tv->arg, "enabled") == 0) {
@ -477,6 +479,7 @@ extcap_arg *extcap_new_arg(void) {
r->range_end = NULL;
r->default_complex = NULL;
r->fileexists = FALSE;
r->fileextension = NULL;
r->is_required = FALSE;
r->values = NULL;
@ -503,6 +506,9 @@ void extcap_free_arg(extcap_arg *a) {
if (a->tooltip != NULL)
g_free(a->tooltip);
if (a->fileextension != NULL)
g_free(a->fileextension);
if (a->range_start != NULL)
extcap_free_complex(a->range_start);
@ -594,6 +600,11 @@ extcap_arg *extcap_parse_arg_sentence(GList * args, extcap_token_sentence *s) {
target_arg->fileexists = (v->value[0] == 't' || v->value[0] == 'T');
}
if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_EXTENSION))
!= NULL) {
target_arg->fileextension = g_strdup(v->value);
}
if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_REQUIRED))
!= NULL) {
target_arg->is_required = (v->value[0] == 't' || v->value[0] == 'T');

View File

@ -67,6 +67,7 @@ typedef enum {
EXTCAP_PARAM_NAME,
EXTCAP_PARAM_ENABLED,
EXTCAP_PARAM_FILE_MUSTEXIST,
EXTCAP_PARAM_FILE_EXTENSION,
EXTCAP_PARAM_PARENT,
EXTCAP_PARAM_REQUIRED
} extcap_param_type;
@ -105,6 +106,8 @@ typedef struct _extcap_arg {
gchar *call;
gchar *display;
gchar *tooltip;
gchar * fileextension;
gboolean fileexists;
gboolean is_required;

View File

@ -506,6 +506,14 @@ bool ExtcapArgument::isRequired()
return FALSE;
}
bool ExtcapArgument::fileExists()
{
if ( _argument != NULL )
return _argument->fileexists;
return FALSE;
}
bool ExtcapArgument::isDefault()
{
if ( value().compare(defaultValue()) == 0 )

View File

@ -98,6 +98,8 @@ Q_SIGNALS:
protected:
bool fileExists();
void setDefault(GHashTable * defaultsList);
ExtcapValueList loadValues(QString parent);

View File

@ -71,7 +71,6 @@ QWidget * ExtcapArgumentFileSelection::createEditor(QWidget * parent)
button->setToolTip(QString().fromUtf8(_argument->tooltip));
}
connect(button, SIGNAL(clicked()), (QObject *)this, SLOT(openFileDialog()));
editLayout->addWidget(textBox);
@ -98,12 +97,23 @@ void ExtcapArgumentFileSelection::openFileDialog()
if (QFileInfo(filename).exists())
workingDir = QFileInfo(filename).dir();
QString fileExt(tr("Any File (*.*)"));
if ( _argument->fileextension != NULL )
{
QString givenExt = QString().fromUtf8(_argument->fileextension);
if ( givenExt.length() != 0 )
fileExt.prepend(";;").prepend(givenExt);
}
filename = QFileDialog::getOpenFileName((QWidget *)(textBox->parent()),
QString().fromUtf8(_argument->display) + " " + tr("Open File"),
workingDir.absolutePath(), tr("All Files (*.*)"));
workingDir.absolutePath(), fileExt);
if ( QFileInfo(filename).exists() )
if ( ! fileExists() || QFileInfo(filename).exists() )
{
textBox->setText(filename);
emit valueChanged();
}
}
bool ExtcapArgumentFileSelection::isValid()