2 * An API for Wireshark plugins
4 * This enables wireshark dissectors, especially those implemented by plugins
5 * to register menubar entries, which then will call a pre-defined callback
6 * function for the dissector or plugin.
8 * Also it implements additional methods, which allow plugins to interoperate
11 * Wireshark - Network traffic analyzer
12 * By Gerald Combs <gerald@wireshark.org>
13 * Copyright 1998 Gerald Combs
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <epan/epan.h>
34 #include <epan/proto.h>
36 #include "plugin_if.h"
38 static GList * menubar_entries = NULL;
39 static GList * menubar_menunames = NULL;
41 extern GList * ext_menubar_get_entries(void)
43 return menubar_entries;
46 extern ext_menu_t * ext_menubar_register_menu(int proto_id, const gchar * menulabel,
49 ext_menubar_t * entry = NULL;
52 /* A name for the entry must be provided */
53 g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
55 /* A protocol must exist for the given id */
56 g_assert(find_protocol_by_id(proto_id) != NULL);
58 /* Create unique name, which is used by GTK to provide the menu */
59 name = g_strconcat(proto_get_protocol_filter_name(proto_id), "Menu", NULL);
61 /* For now, a protocol may only register one main menu */
62 g_assert(g_list_find(menubar_menunames, name) == NULL);
64 entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
65 entry->type = EXT_MENUBAR_MENU;
66 entry->proto = proto_id;
67 entry->is_plugin = is_plugin;
69 entry->parent_menu = 0;
71 /* Create a name for this submenu */
73 entry->label = g_strdup(menulabel);
74 entry->tooltip = g_strdup(menulabel);
76 entry->submenu_cnt = 0;
79 menubar_entries = g_list_append(menubar_entries, entry);
80 menubar_menunames = g_list_append(menubar_menunames, name);
85 extern ext_menu_t * ext_menubar_set_parentmenu(ext_menu_t * menu, const gchar * parentmenu)
87 g_assert(menu != NULL && menu->parent == NULL);
89 g_assert(parentmenu != 0);
91 menu->parent_menu = g_strdup(parentmenu);
96 extern ext_menu_t * ext_menubar_add_submenu(ext_menu_t * parent, const gchar *menulabel)
98 ext_menubar_t * entry = NULL;
100 /* A name for the entry must be provided */
101 g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
103 /* Parent must be a valid parent */
104 g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
106 parent->submenu_cnt++;
108 /* Create submenu entry */
109 entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
110 entry->type = EXT_MENUBAR_MENU;
111 entry->parent = parent;
112 /* Just a convenience */
113 entry->proto = parent->proto;
114 entry->is_plugin = parent->is_plugin;
115 /* Create unique name, which is used by GTK to provide the menu */
116 entry->name = g_strdup_printf("%sS%02d", parent->name, parent->submenu_cnt);
117 entry->label = g_strdup(menulabel);
118 entry->tooltip = g_strdup(menulabel);
120 parent->children = g_list_append(parent->children, entry);
125 static void ext_menubar_add_generic_entry (
126 ext_menubar_entry_t type, ext_menu_t * parent, const gchar * label,
127 const gchar * tooltip, ext_menubar_action_cb callback, gpointer user_data )
129 ext_menubar_t * entry = NULL;
131 /* A valid parent must exist */
132 g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
133 /* A label for the entry must be provided */
134 g_assert(label != NULL && strlen ( label ) > 0 );
138 /* Create menu entry */
139 entry = (ext_menubar_t*)g_malloc0(sizeof(ext_menubar_t));
141 /* Create unique name, which is used by GTK to provide the menu */
142 entry->name = g_strdup_printf("%sI%02d", parent->name, parent->item_cnt);
143 entry->label = g_strdup(label);
145 if ( tooltip != NULL && strlen(tooltip) > 0 )
146 entry->tooltip = g_strdup(tooltip);
148 entry->callback = callback;
149 entry->user_data = user_data;
151 parent->children = g_list_append(parent->children, entry);
154 extern void ext_menubar_add_entry(ext_menu_t * parent, const gchar *label,
155 const gchar *tooltip, ext_menubar_action_cb callback, gpointer user_data)
157 /* A callback must be provided */
158 g_assert(callback != NULL);
160 ext_menubar_add_generic_entry ( EXT_MENUBAR_ITEM, parent, label, tooltip, callback, user_data );
163 extern void ext_menubar_add_website(ext_menu_t * parent, const gchar *label,
164 const gchar *tooltip, const gchar *url)
166 /* An url for the entry must be provided */
167 g_assert(url != NULL && strlen ( url ) > 0 );
169 ext_menubar_add_generic_entry ( EXT_MENUBAR_URL, parent, label, tooltip, NULL, (gpointer) g_strdup(url) );
172 extern void ext_menubar_add_separator(ext_menu_t *parent)
174 ext_menubar_add_generic_entry ( EXT_MENUBAR_SEPARATOR, parent, g_strdup("-"), NULL, NULL, NULL );
177 /* Implementation of GUI callback methods follows.
178 * This is a necessity, as using modern UI systems, gui interfaces often operate
179 * in different threads then the calling application. Even more so, if the calling
180 * application is implemented using a separate plugin. Therefore the external menubars
181 * cannot call gui functionality directly, the gui has to perform the function within
184 static GHashTable * plugin_if_callback_functions;
187 plugin_if_init_hashtable(void)
189 if ( plugin_if_callback_functions == 0 )
190 plugin_if_callback_functions = g_hash_table_new(g_int_hash, g_int_equal);
193 static void plugin_if_call_gui_cb(plugin_if_callback_t actionType, GHashTable * dataSet)
195 plugin_if_gui_cb action;
198 key = (gint *)g_malloc0(sizeof(gint));
199 *key = (gint) actionType;
201 plugin_if_init_hashtable();
203 if ( g_hash_table_size(plugin_if_callback_functions) != 0 )
205 if ( g_hash_table_lookup_extended(plugin_if_callback_functions, key, NULL, (gpointer*)&action) )
207 if ( action != NULL )
213 extern void plugin_if_apply_filter(const char * filter_string, gboolean force)
215 plugin_if_callback_t actionType;
216 GHashTable * dataSet = NULL;
218 actionType = ( force == TRUE ) ? PLUGIN_IF_FILTER_ACTION_APPLY : PLUGIN_IF_FILTER_ACTION_PREPARE;
219 dataSet = g_hash_table_new(g_str_hash, g_str_equal);
221 g_hash_table_insert( dataSet, g_strdup("action_type"), (gpointer) &actionType );
222 g_hash_table_insert( dataSet, g_strdup("filter_string"), g_strdup(filter_string) );
223 g_hash_table_insert( dataSet, g_strdup("force"), (gpointer) &force );
225 plugin_if_call_gui_cb(actionType, dataSet);
228 extern void plugin_if_goto_frame(guint32 framenr)
230 GHashTable * dataSet = NULL;
232 dataSet = g_hash_table_new(g_str_hash, g_str_equal);
234 g_hash_table_insert( dataSet, g_strdup("frame_nr"), GUINT_TO_POINTER(framenr) );
236 plugin_if_call_gui_cb(PLUGIN_IF_GOTO_FRAME, dataSet);
239 extern void plugin_if_save_preference(const char * pref_module, const char * pref_key, const char * pref_value)
241 GHashTable * dataSet = NULL;
243 dataSet = g_hash_table_new(g_str_hash, g_str_equal);
245 g_hash_table_insert( dataSet, g_strdup("pref_module"), g_strdup(pref_module) );
246 g_hash_table_insert( dataSet, g_strdup("pref_key"), g_strdup(pref_key) );
247 g_hash_table_insert( dataSet, g_strdup("pref_value"), g_strdup(pref_value) );
249 plugin_if_call_gui_cb(PLUGIN_IF_PREFERENCE_SAVE, dataSet);
252 extern void plugin_if_register_gui_cb(plugin_if_callback_t actionType, plugin_if_gui_cb callback)
256 key = (gint *)g_malloc0(sizeof(gint));
259 plugin_if_init_hashtable();
261 if ( ! g_hash_table_lookup_extended(plugin_if_callback_functions, key, NULL, NULL ) )
262 g_hash_table_insert(plugin_if_callback_functions, key, callback);
271 * indent-tabs-mode: nil
274 * ex: set shiftwidth=4 tabstop=8 expandtab:
275 * :indentSize=4:tabSize=8:noTabs=true: