UI: Implementing menus for plugins

Plugins may utilize the tap interface to provide special tools
 or analysis options, not otherwise available in Wireshark, or
 perhaps not allowed to be distributed freely. Up until now, those
 tools either had to start automatically, or could not be started
 at all, or had to be started separately.

 It should be possible, that those tools may be started using a
 menu entry directly from Wireshark. This interface tries to achieve
 exactly that.

 This interface uses a clean interface, which can be implemented in
 any plugin or dissector. Documentation for this has been added to
 README.plugins.

 Separators are only supported for now in the Qt interface, but
 URLs can now be added as a simple item, and the UI will use the
 same methods used for other URL calls to open them.

Change-Id: I170107dafb66f6badaa864d05a9091e5cbbf52c2
Reviewed-on: https://code.wireshark.org/review/7865
Reviewed-by: Roland Knall <rknall@gmail.com>
Petri-Dish: Alexis La Goutte <alexis.lagoutte@gmail.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Anders Broman <a.broman58@gmail.com>
This commit is contained in:
Roland Knall 2015-03-30 16:10:01 +02:00 committed by Anders Broman
parent 035d741289
commit eeed4d1121
9 changed files with 730 additions and 1 deletions

View File

@ -320,6 +320,53 @@ is encouraged to update their plugins as outlined below:
o Change the Makefile.am and Makefile.nmake files to match those of
the DOCSIS plugin.
6 How to implement a plugin related menu
A plugin (as well as built-in dissectors) may implement a menu within
Wireshark to be used to trigger options, start tools, open Websites, ...
This menu structure is built using the ext_menubar.h interface and it's
corresponding functions.
The menu items all call a callback provided by the plugin, which takes
a pointer to the menuitem entry ad data. This pointer may be used to
provide userdata to each entry. The pointer must utilize WS_DLL_PUBLIC_DEF
and has the following structure:
WS_DLL_PUBLIC_DEF void
menu_cb(gpointer user_data)
{
... Do something ...
}
The menu entries themselves are generated with the following code structure:
ext_menu_t * ext_menu, *os_menu = NULL;
ext_menu = ext_menubar_register_menu (
<your_proto_item>, "TestMenu", "Some Menu Entry", TRUE );
ext_menubar_add_entry(ext_menu, "TestEntry1", "Test Entry 1",
"This is a tooltip", menu_cb, <user_data>);
ext_menubar_add_entry(ext_menu, "TestEntry2", "Test Entry 2",
NULL, menu_cb, <user_data>);
os_menu = ext_menubar_add_submenu(ext_menu, "TestSubMenu", "Sub Menu" );
ext_menubar_add_entry(os_menu, "SubEntry1", "Test Entry A",
NULL, menu_cb, <user_data>);
ext_menubar_add_entry(os_menu, "SubEntry2", "Test Entry B",
NULL, menu_cb, <user_data>);
The second parameter to ext_menubar_register_menu and ext_menubar_add_entry is
an internal definition for the menu, utilized by the GTK interface. It must not
contain any characters besides A-Z, a-z and 0-9.
Using the Gtk Version and a Mac OSX operating system, this will not work, and
the Gtk interface is currently not supported on this plattform. The Qt interface
on Mac provides the menu.
For a more detailed information, please refer to ext_menubar.h
----------------
Ed Warnicke <hagbard@physics.rutgers.edu>

View File

@ -1593,6 +1593,7 @@ set(LIBWIRESHARK_FILES
except.c
expert.c
exported_pdu.c
ext_menubar.c
filter_expressions.c
follow.c
frame_data.c

View File

@ -51,6 +51,7 @@ LIBWIRESHARK_SRC = \
except.c \
expert.c \
exported_pdu.c \
ext_menubar.c \
filter_expressions.c \
follow.c \
frame_data.c \
@ -190,6 +191,7 @@ LIBWIRESHARK_INCLUDES = \
exceptions.h \
expert.h \
exported_pdu.h \
ext_menubar.h \
filter_expressions.h \
follow.h \
frame_data.h \

186
epan/ext_menubar.c Normal file
View File

@ -0,0 +1,186 @@
/* ext_menubar.c
* A menubar API for Wireshark dissectors
*
* This enables wireshark dissectors, especially those implemented by plugins
* to register menubar entries, which then will call a pre-defined callback
* function for the dissector or plugin
*
* 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 "config.h"
#include <glib.h>
#include <epan/epan.h>
#include <epan/proto.h>
#include "ext_menubar.h"
static GList * menubar_entries = NULL;
static GList * menubar_menunames = NULL;
extern GList * ext_menubar_get_entries(void)
{
return menubar_entries;
}
extern ext_menu_t * ext_menubar_register_menu(int proto_id, const gchar * menulabel,
gboolean is_plugin)
{
ext_menubar_t * entry = NULL;
gchar * name = NULL;
/* A name for the entry must be provided */
g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
/* A protocol must exist for the given id */
g_assert(find_protocol_by_id(proto_id) != NULL);
/* Create unique name, which is used by GTK to provide the menu */
name = g_strconcat(proto_get_protocol_filter_name(proto_id), "Menu", NULL);
/* For now, a protocol may only register one main menu */
g_assert(g_list_find(menubar_menunames, name) == NULL);
entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
entry->type = EXT_MENUBAR_MENU;
entry->proto = proto_id;
entry->is_plugin = is_plugin;
/* Create a name for this submenu */
entry->name = g_strdup(name);
entry->label = g_strdup(menulabel);
entry->tooltip = g_strdup(menulabel);
entry->submenu_cnt = 0;
entry->item_cnt = 0;
menubar_entries = g_list_append(menubar_entries, entry);
menubar_menunames = g_list_append(menubar_menunames, name);
g_free(name);
return entry;
}
extern ext_menu_t * ext_menubar_add_submenu(ext_menu_t * parent, const gchar *menulabel)
{
ext_menubar_t * entry = NULL;
gchar * name = NULL;
/* A name for the entry must be provided */
g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
/* Parent must be a valid parent */
g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
parent->submenu_cnt++;
/* Create unique name, which is used by GTK to provide the menu */
name = (gchar *)g_malloc0(strlen(parent->name) + 4);
g_snprintf(name, strlen(parent->name) + 4, "%sS%02d",
parent->name, parent->submenu_cnt );
/* Create submenu entry */
entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
entry->type = EXT_MENUBAR_MENU;
entry->parent = parent;
/* Just a convenience */
entry->proto = parent->proto;
entry->is_plugin = parent->is_plugin;
entry->name = g_strdup(name);
entry->label = g_strdup(menulabel);
entry->tooltip = g_strdup(menulabel);
parent->children = g_list_append(parent->children, entry);
g_free(name);
return entry;
}
static void ext_menubar_add_generic_entry (
ext_menubar_entry_t type, ext_menu_t * parent, const gchar * label,
const gchar * tooltip, ext_menubar_action_cb callback, gpointer user_data )
{
ext_menubar_t * entry = NULL;
gchar * name = NULL;
/* A valid parent must exist */
g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
/* A label for the entry must be provided */
g_assert(label != NULL && strlen ( label ) > 0 );
parent->item_cnt++;
/* Create unique name, which is used by GTK to provide the menu */
name = (gchar *)g_malloc0(strlen(parent->name) + 4);
g_snprintf(name, strlen(parent->name) + 4, "%sI%02d",
parent->name, parent->item_cnt );
/* Create menu entry */
entry = (ext_menubar_t*)g_malloc0(sizeof(ext_menubar_t));
entry->type = type;
entry->name = g_strdup(name);
entry->label = g_strdup(label);
if ( tooltip != NULL && strlen(tooltip) > 0 )
entry->tooltip = g_strdup(tooltip);
entry->callback = callback;
entry->user_data = user_data;
parent->children = g_list_append(parent->children, entry);
g_free(name);
}
extern void ext_menubar_add_entry(ext_menu_t * parent, const gchar *label,
const gchar *tooltip, ext_menubar_action_cb callback, gpointer user_data)
{
/* A callback must be provided */
g_assert(callback != NULL);
ext_menubar_add_generic_entry ( EXT_MENUBAR_ITEM, parent, label, tooltip, callback, user_data );
}
extern void ext_menubar_add_website(ext_menu_t * parent, const gchar *label,
const gchar *tooltip, const gchar *url)
{
/* An url for the entry must be provided */
g_assert(url != NULL && strlen ( url ) > 0 );
ext_menubar_add_generic_entry ( EXT_MENUBAR_URL, parent, label, tooltip, NULL, (gpointer) g_strdup(url) );
}
extern void ext_menubar_add_separator(ext_menu_t *parent)
{
ext_menubar_add_generic_entry ( EXT_MENUBAR_SEPARATOR, parent, g_strdup("-"), NULL, NULL, NULL );
}
/*
* 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:
*/

165
epan/ext_menubar.h Normal file
View File

@ -0,0 +1,165 @@
/* ext_menubar.h
* A menubar API for Wireshark dissectors
*
* This enables wireshark dissectors, especially those implemented by plugins
* to register menubar entries, which then will call a pre-defined callback
* function for the dissector or plugin
*
* 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.
*/
#ifndef EPAN_EXT_MENUBAR_H_
#define EPAN_EXT_MENUBAR_H_
#include "config.h"
#include "ws_symbol_export.h"
#include <glib.h>
#include <epan/epan.h>
#include <epan/packet_info.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define EXT_MENUBAR_MAX_DEPTH 5
/* menubar callback */
typedef void (*ext_menubar_action_cb)(gpointer user_data);
typedef enum
{
EXT_MENUBAR_MENU,
EXT_MENUBAR_ITEM,
EXT_MENUBAR_SEPARATOR,
EXT_MENUBAR_URL
} ext_menubar_entry_t;
typedef struct _ext_menubar_t ext_menubar_t;
typedef ext_menubar_t ext_menu_t;
struct _ext_menubar_t
{
ext_menubar_entry_t type;
ext_menu_t * parent;
int proto;
GList * children;
guint submenu_cnt;
guint item_cnt;
gchar * name;
gchar * label;
gchar * tooltip;
gboolean is_plugin;
gpointer user_data;
ext_menubar_action_cb callback;
};
/* Registers a new main menu.
*
* This will register a new main menu entry, underneath all other menu entries will
* be sorted
*
* @param proto_id the proto item for the protocol this menu entry belongs too
* @param name the entry name (the internal used one) for the menu item
* @param menulabel the entry label (the displayed name) for the menu item
* @param is_plugin must be set to TRUE for plugin registration
*/
WS_DLL_PUBLIC ext_menu_t * ext_menubar_register_menu(
int proto_id, const gchar * menulabel, gboolean is_plugin);
/* Registers a new main menu.
*
* This will register a new sub menu entry, underneath the parent menu
*
* @param parent the parent menu for this submenu
* @param name the entry name (the internal used one) for the menu item
* @param menulabel the entry label (the displayed name) for the menu item
*/
WS_DLL_PUBLIC ext_menu_t * ext_menubar_add_submenu(
ext_menu_t * parent, const gchar *menulabel);
/* Registers a new menubar entry.
*
* This registers a new menubar entry, which will have the given name, and
* call the provided callback on activation
*
* @param parent_menu the parent menu for this entry
* @param name the entry name (the internal used one) for the menu item
* @param label the entry label (the displayed name) for the menu item
* @param tooltip a tooltip to be displayed on mouse-over
* @param callback the action which will be invoked after click on the menu item
*/
WS_DLL_PUBLIC void ext_menubar_add_entry(
ext_menu_t * parent_menu,
const gchar *label,
const gchar *tooltip,
ext_menubar_action_cb callback,
gpointer user_data);
/* Registers a new separator entry.
*
* @note This will not work using the legacy GTK interface, due to
* restrictions on how separators are handled in the menu
*
* @param parent_menu the parent menu for this entry
*/
WS_DLL_PUBLIC void ext_menubar_add_separator(ext_menu_t *parent_menu);
/* Registers a entry for a website call
*
* This registers a new menubar entry, which will call the given website, using
* the predefined webbrowser
*
* @param parent_menu the parent menu for this entry
* @param name the entry name (the internal used one) for the menu item
* @param label the entry label (the displayed name) for the menu item
* @param tooltip a tooltip to be displayed on mouse-over
* @param url the url for the website
*/
WS_DLL_PUBLIC void ext_menubar_add_website(ext_menu_t * parent, const gchar *label,
const gchar *tooltip, const gchar *url);
/* Private Method for retrieving the menubar entries
*
* Is only to be used by the UI interfaces to retrieve the menu entries
*/
WS_DLL_PUBLIC GList * ext_menubar_get_entries(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* EPAN_EXT_MENUBAR_H_ */
/*
* 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:
*/

View File

@ -36,6 +36,7 @@
#include <epan/epan_dissect.h>
#include <epan/column.h>
#include <epan/stats_tree_priv.h>
#include <epan/ext_menubar.h>
#include "globals.h"
#include "color_filters.h"
@ -100,6 +101,7 @@
#include "ui/gtk/addr_resolution_dlg.h"
#include "ui/gtk/export_pdu_dlg.h"
#include "ui/gtk/conversation_hastables_dlg.h"
#include "ui/gtk/webbrowser.h"
#include "ui/gtk/packet_list.h"
#include "ui/gtk/lbm_stream_dlg.h"
@ -140,6 +142,7 @@ static void add_tap_plugins (guint merge_id, GtkUIManager *ui_manager);
static void menus_init(void);
static void merge_menu_items(GList *node);
static void ws_menubar_external_menus(void);
static void ws_menubar_build_external_menus(void);
static void set_menu_sensitivity (GtkUIManager *ui_manager, const gchar *, gint);
static void menu_name_resolution_update_cb(GtkAction *action, gpointer data);
@ -3261,6 +3264,10 @@ menus_init(void)
menu_dissector_filter(&cfile);
menu_conversation_list(&cfile);
menu_hostlist_list(&cfile);
/* Add additional entries which may have been introduced by dissectors and/or plugins */
ws_menubar_external_menus();
merge_menu_items(merge_menu_items_list);
/* Add external menus and items */
@ -5450,6 +5457,219 @@ void set_menus_for_profiles(gboolean default_profile)
set_menu_sensitivity(ui_manager_statusbar_profiles_menu, "/ProfilesMenuPopup/Delete", !default_profile);
}
#ifndef __APPLE__
static void
ws_menubar_external_cb(GtkAction *action _U_, gpointer data _U_)
{
ext_menubar_t *entry = NULL;
gchar *url = NULL;
if ( data != NULL )
{
entry = (ext_menubar_t *)data;
if ( entry->type == EXT_MENUBAR_ITEM )
{
entry->callback(entry->user_data);
}
else if ( entry->type == EXT_MENUBAR_URL )
{
url = (gchar *)entry->user_data;
if(url != NULL)
browser_open_url(url);
}
}
}
static void
ws_menubar_create_ui(ext_menu_t * menu, const char * xpath_parent, GtkActionGroup * action_group, gint depth)
{
ext_menubar_t * item = NULL;
GList * children = NULL;
gchar * xpath, * submenu_xpath, *ac_xpath;
const gchar *xml;
gchar ** paths = NULL;
GError *err = NULL;
guint merge_id = 0;
/* There must exists an xpath parent */
g_assert(xpath_parent != NULL && strlen(xpath_parent) > 0);
/* If the depth counter exceeds, something must have gone wrong */
g_assert(depth < EXT_MENUBAR_MAX_DEPTH);
/* Create a correct xpath, and just keep the necessary action ref [which will be paths [1]] */
xpath = g_strconcat(xpath_parent, menu->name, NULL);
children = menu->children;
/* Iterate the child entries */
while ( children != NULL && children->data != NULL )
{
item = (ext_menubar_t *) children->data;
if ( item->type == EXT_MENUBAR_MENU )
{
/* Handle Submenu entry */
submenu_xpath = g_strconcat(xpath, "/", NULL);
ws_menubar_create_ui(item, submenu_xpath, action_group, depth++);
g_free(submenu_xpath);
}
else if ( item->type != EXT_MENUBAR_SEPARATOR )
{
/* Create the correct action path */
paths = g_strsplit(xpath, "|", -1);
/* Ensures that the above operation has not failed. If this fails, it is a major issue,
* so an assertion is appropriate here */
g_assert(paths != NULL && paths[1] != NULL && strlen(paths[1]) > 0);
/* Handle a menu bar item. This cannot be done by register_menu_bar_menu_items, as it
* will create it's own action group, assuming that the menu actions and submenu actions
* have been pre-registered and globally defined names. This is not the case here. Also
* register_menu_bar_menu_items adds a sorted list, completely destroying any sorting,
* a plugin might have intended */
#if 0
/* Left here as a reminder, that the code below does basically the same */
register_menu_bar_menu_items( g_strdup(paths[1]), item->name, NULL, item->label,
NULL, item->tooltip, ws_menubar_external_cb, item, TRUE, NULL, NULL);
#endif
/* Creating menu from entry */
ac_xpath = g_strdup_printf("%s/%s", g_strdup(paths[1]), item->name);
xml = make_menu_xml(ac_xpath);
/* Adding the menu to the UI if possible (code has been derived from merge_menu_items) */
err = NULL;
merge_id = gtk_ui_manager_add_ui_from_string(ui_manager_main_menubar, xml, -1, &err);
if ( err != NULL )
{
fprintf (stderr, "Warning: building menu for [%s] failed: %s\n", item->name, err->message);
g_error_free (err);
/* undo the mess */
gtk_ui_manager_remove_ui (ui_manager_main_menubar, merge_id);
g_free ((gchar*)xml);
}
g_strfreev(paths);
g_free(ac_xpath);
}
/* Iterate Loop */
children = g_list_next(children);
}
/* Cleanup */
g_free(xpath);
}
static void
ws_menubar_create_action_group(ext_menu_t * menu, const char * xpath_parent, GtkActionGroup *action_group, gint depth)
{
ext_menubar_t * item = NULL;
GList * children = NULL;
GtkAction * menu_item;
gchar * xpath, *submenu_xpath;
g_assert(xpath_parent != NULL && strlen(xpath_parent) > 0);
/* If the depth counter exceeds, something must have gone wrong */
g_assert(depth < EXT_MENUBAR_MAX_DEPTH);
xpath = g_strconcat(xpath_parent, menu->name, NULL);
/* Create the action for the menu item and add it to the action group */
menu_item = (GtkAction *)g_object_new ( GTK_TYPE_ACTION,
"name", menu->name, "label", menu->label, NULL );
gtk_action_group_add_action(action_group, menu_item);
children = menu->children;
/* Iterate children to create submenus */
while ( children != NULL && children->data != NULL )
{
item = (ext_menubar_t *) children->data;
/* Handle only menues, not individual items */
if ( item->type == EXT_MENUBAR_MENU )
{
submenu_xpath = g_strconcat(xpath, "/", NULL);
ws_menubar_create_action_group(item, submenu_xpath, action_group, depth++);
g_free(submenu_xpath);
}
else if ( item->type != EXT_MENUBAR_SEPARATOR )
{
menu_item = (GtkAction*) g_object_new( GTK_TYPE_ACTION,
"name", item->name,
"label", item->label,
"tooltip", item->tooltip,
NULL);
g_signal_connect(menu_item, "activate", G_CALLBACK(ws_menubar_external_cb), item );
gtk_action_group_add_action(action_group, menu_item);
}
children = g_list_next(children);
}
/* Cleanup */
g_free(xpath);
}
#endif
static void
ws_menubar_external_menus(void)
{
#ifndef __APPLE__
GList * user_menu = NULL;
ext_menu_t * menu = NULL;
GtkActionGroup *action_group = NULL;
gchar groupdef[20], * xpath;
guint8 cnt = 1;
user_menu = ext_menubar_get_entries();
while ( ( user_menu != NULL ) && ( user_menu->data != NULL ) )
{
menu = (ext_menu_t *) user_menu->data;
/* On this level only menu items should exist. Not doing an assert here,
* as it could be an honest mistake */
if ( menu->type != EXT_MENUBAR_MENU )
{
user_menu = g_list_next(user_menu);
continue;
}
/* Create unique main actiongroup name */
g_snprintf(groupdef, 20, "UserDefined%02d", cnt);
xpath = g_strconcat( "/MenuBar/", groupdef, "Menu|", NULL );
/* Create an action group per menu */
action_group = gtk_action_group_new(groupdef);
/* This will generate the action structure for each menu and it's items. It is recursive,
* therefore a sub-routine, and we have a depth counter to prevent endless loops. */
ws_menubar_create_action_group(menu, xpath, action_group, 0);
/* Register action structure for each menu */
gtk_ui_manager_insert_action_group(ui_manager_main_menubar, action_group, 0);
/* Now we iterate over the items and add them to the UI. This has to be done after the action
* group for this menu has been added, otherwise the actions will not be found */
ws_menubar_create_ui(menu, xpath, action_group, 0 );
/* Cleanup */
g_free(xpath);
g_object_unref(action_group);
/* Iterate Loop */
user_menu = g_list_next (user_menu);
cnt++;
}
#endif
}
/*
* Editor modelines
*

View File

@ -27,6 +27,7 @@
#include <wsutil/filesystem.h>
#include <epan/prefs.h>
#include <epan/stats_tree_priv.h>
#include <epan/ext_menubar.h>
//#include <wiretap/wtap.h>
@ -197,6 +198,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(wsApp, SIGNAL(appInitialized()), this, SLOT(setFeaturesEnabled()));
connect(wsApp, SIGNAL(appInitialized()), this, SLOT(zoomText()));
connect(wsApp, SIGNAL(appInitialized()), this, SLOT(addStatsPluginsToMenu()));
connect(wsApp, SIGNAL(appInitialized()), this, SLOT(addExternalMenus()));
connect(wsApp, SIGNAL(profileChanging()), this, SLOT(saveWindowGeometry()));
connect(wsApp, SIGNAL(preferencesChanged()), this, SLOT(layoutPanes()));
@ -1831,6 +1833,79 @@ void MainWindow::setForCaptureInProgress(gboolean capture_in_progress)
#endif
}
void MainWindow::externalMenuHelper(ext_menu_t * menu, QMenu * subMenu, gint depth)
{
QAction * itemAction = NULL;
ext_menubar_t * item = NULL;
GList * children = NULL;
/* There must exists an xpath parent */
g_assert(subMenu != NULL);
/* If the depth counter exceeds, something must have gone wrong */
g_assert(depth < EXT_MENUBAR_MAX_DEPTH);
children = menu->children;
/* Iterate the child entries */
while ( children != NULL && children->data != NULL )
{
item = (ext_menubar_t *) children->data;
if ( item->type == EXT_MENUBAR_MENU )
{
/* Handle Submenu entry */
this->externalMenuHelper(item, subMenu->addMenu(item->label), depth++ );
}
else if ( item->type == EXT_MENUBAR_SEPARATOR )
{
subMenu->addSeparator();
}
else if ( item->type == EXT_MENUBAR_ITEM || item->type == EXT_MENUBAR_URL )
{
itemAction = subMenu->addAction(item->name);
itemAction->setData(QVariant::fromValue((void *)item));
itemAction->setText(item->label);
connect(itemAction, SIGNAL(triggered()),
this, SLOT(on_actionExternalMenuItem_triggered()));
}
/* Iterate Loop */
children = g_list_next(children);
}
}
void MainWindow::addExternalMenus()
{
QMenu * subMenu = NULL;
GList * user_menu = NULL;
ext_menu_t * menu = NULL;
user_menu = ext_menubar_get_entries();
while ( ( user_menu != NULL ) && ( user_menu->data != NULL ) )
{
menu = (ext_menu_t *) user_menu->data;
/* On this level only menu items should exist. Not doing an assert here,
* as it could be an honest mistake */
if ( menu->type != EXT_MENUBAR_MENU )
{
user_menu = g_list_next(user_menu);
continue;
}
/* Create main submenu and add it to the menubar */
subMenu = main_ui_->menuBar->addMenu(menu->label);
/* This will generate the action structure for each menu. It is recursive,
* therefore a sub-routine, and we have a depth counter to prevent endless loops. */
this->externalMenuHelper(menu, subMenu, 0);
/* Iterate Loop */
user_menu = g_list_next (user_menu);
}
}
/*
* Editor modelines
*

View File

@ -33,6 +33,7 @@
#include "ui/ui_util.h"
#include <epan/prefs.h>
#include <epan/ext_menubar.h>
#ifdef HAVE_LIBPCAP
#include "capture_opts.h"
@ -174,6 +175,8 @@ private:
void setForCapturedPackets(bool have_captured_packets);
void setMenusForFileSet(bool enable_list_files);
void externalMenuHelper(ext_menu_t * menu, QMenu * subMenu, gint depth);
void setForCaptureInProgress(gboolean capture_in_progress = false);
QMenu* findOrAddMenu(QMenu *parent_menu, QString& menu_text);
@ -233,6 +236,7 @@ private slots:
void fieldsChanged();
void showColumnEditor(int column);
void addStatsPluginsToMenu();
void addExternalMenus();
void startInterfaceCapture(bool valid);
@ -453,6 +457,8 @@ private slots:
void on_actionATT_Server_Attributes_triggered();
void on_actionExternalMenuItem_triggered();
void changeEvent(QEvent* event);
};

View File

@ -110,7 +110,8 @@
#include <QMessageBox>
#include <QMetaObject>
#include <QToolBar>
#include <QDesktopServices>
#include <QUrl>
#include <QDebug>
//
@ -2900,6 +2901,32 @@ void MainWindow::on_actionCaptureRefreshInterfaces_triggered()
}
#endif
void MainWindow::on_actionExternalMenuItem_triggered()
{
QAction * triggerAction = NULL;
QVariant v;
ext_menubar_t * entry = NULL;
if ( QObject::sender() != NULL)
{
triggerAction = (QAction *)QObject::sender();
v = triggerAction->data();
if ( v.canConvert<void *>())
{
entry = (ext_menubar_t *)v.value<void *>();
if ( entry->type == EXT_MENUBAR_ITEM )
{
entry->callback(entry->user_data);
}
else
{
QDesktopServices::openUrl(QUrl(QString((gchar *)entry->user_data)));
}
}
}
}
/*
* Editor modelines