ISO14443: Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
[metze/wireshark/wip.git] / epan / plugin_if.c
1 /* plugin_if.c
2  * An API for Wireshark plugins
3  *
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.
7  *
8  * Also it implements additional methods, which allow plugins to interoperate
9  * with the main GUI.
10  *
11  * Wireshark - Network traffic analyzer
12  * By Gerald Combs <gerald@wireshark.org>
13  * Copyright 1998 Gerald Combs
14  *
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.
19  *
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.
24  *
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.
28  */
29
30 #include "config.h"
31
32 #include <glib.h>
33 #include <epan/epan.h>
34 #include <epan/proto.h>
35
36 #include "plugin_if.h"
37
38 static GList * menubar_entries = NULL;
39 static GList * menubar_menunames = NULL;
40
41 extern GList * ext_menubar_get_entries(void)
42 {
43     return menubar_entries;
44 }
45
46 extern ext_menu_t * ext_menubar_register_menu(int proto_id, const gchar * menulabel,
47         gboolean is_plugin)
48 {
49     ext_menubar_t * entry = NULL;
50     gchar * name = NULL;
51
52     /* A name for the entry must be provided */
53     g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
54
55     /* A protocol must exist for the given id */
56     g_assert(find_protocol_by_id(proto_id) != NULL);
57
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);
60
61     /* For now, a protocol may only register one main menu */
62     g_assert(g_list_find(menubar_menunames, name) == NULL);
63
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;
68
69     entry->parent_menu = 0;
70
71     /* Create a name for this submenu */
72     entry->name = name;
73     entry->label = g_strdup(menulabel);
74     entry->tooltip = g_strdup(menulabel);
75
76     entry->submenu_cnt = 0;
77     entry->item_cnt = 0;
78
79     menubar_entries = g_list_append(menubar_entries, entry);
80     menubar_menunames = g_list_append(menubar_menunames, name);
81
82     return entry;
83 }
84
85 extern ext_menu_t * ext_menubar_set_parentmenu(ext_menu_t * menu, const gchar * parentmenu)
86 {
87     g_assert(menu != NULL && menu->parent == NULL);
88
89     g_assert(parentmenu != 0);
90
91     menu->parent_menu = g_strdup(parentmenu);
92
93     return menu;
94 }
95
96 extern ext_menu_t * ext_menubar_add_submenu(ext_menu_t * parent, const gchar *menulabel)
97 {
98     ext_menubar_t * entry = NULL;
99
100     /* A name for the entry must be provided */
101     g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
102
103     /* Parent must be a valid parent */
104     g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
105
106     parent->submenu_cnt++;
107
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);
119
120     parent->children = g_list_append(parent->children, entry);
121
122     return entry;
123 }
124
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 )
128 {
129     ext_menubar_t * entry = NULL;
130
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 );
135
136     parent->item_cnt++;
137
138     /* Create menu entry */
139     entry = (ext_menubar_t*)g_malloc0(sizeof(ext_menubar_t));
140     entry->type = type;
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);
144
145     if ( tooltip != NULL && strlen(tooltip) > 0 )
146         entry->tooltip = g_strdup(tooltip);
147
148     entry->callback = callback;
149     entry->user_data = user_data;
150
151     parent->children = g_list_append(parent->children, entry);
152 }
153
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)
156 {
157     /* A callback must be provided */
158     g_assert(callback != NULL);
159
160     ext_menubar_add_generic_entry ( EXT_MENUBAR_ITEM, parent, label, tooltip, callback, user_data );
161 }
162
163 extern void ext_menubar_add_website(ext_menu_t * parent, const gchar *label,
164         const gchar *tooltip, const gchar *url)
165 {
166     /* An url for the entry must be provided */
167     g_assert(url != NULL && strlen ( url ) > 0 );
168
169     ext_menubar_add_generic_entry ( EXT_MENUBAR_URL, parent, label, tooltip, NULL, (gpointer) g_strdup(url) );
170 }
171
172 extern void ext_menubar_add_separator(ext_menu_t *parent)
173 {
174     ext_menubar_add_generic_entry ( EXT_MENUBAR_SEPARATOR, parent, g_strdup("-"), NULL, NULL, NULL );
175 }
176
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
182  * it' own scope. */
183
184 static GHashTable * plugin_if_callback_functions;
185
186 static void
187 plugin_if_init_hashtable(void)
188 {
189     if ( plugin_if_callback_functions == 0 )
190         plugin_if_callback_functions = g_hash_table_new(g_int_hash, g_int_equal);
191 }
192
193 static void plugin_if_call_gui_cb(plugin_if_callback_t actionType, GHashTable * dataSet)
194 {
195     plugin_if_gui_cb action;
196     gint * key = 0;
197
198     key = (gint *)g_malloc0(sizeof(gint));
199     *key = (gint) actionType;
200
201     plugin_if_init_hashtable();
202
203     if ( g_hash_table_size(plugin_if_callback_functions) != 0 )
204     {
205         if ( g_hash_table_lookup_extended(plugin_if_callback_functions, key, NULL, (gpointer*)&action) )
206         {
207             if ( action != NULL )
208                 action(dataSet);
209         }
210     }
211 }
212
213 extern void plugin_if_apply_filter(const char * filter_string, gboolean force)
214 {
215     plugin_if_callback_t actionType;
216     GHashTable * dataSet = NULL;
217
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);
220
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 );
224
225     plugin_if_call_gui_cb(actionType, dataSet);
226 }
227
228 extern void plugin_if_goto_frame(guint32 framenr)
229 {
230     GHashTable * dataSet = NULL;
231
232     dataSet = g_hash_table_new(g_str_hash, g_str_equal);
233
234     g_hash_table_insert( dataSet, g_strdup("frame_nr"), GUINT_TO_POINTER(framenr) );
235
236     plugin_if_call_gui_cb(PLUGIN_IF_GOTO_FRAME, dataSet);
237 }
238
239 extern void plugin_if_save_preference(const char * pref_module, const char * pref_key, const char * pref_value)
240 {
241     GHashTable * dataSet = NULL;
242
243     dataSet = g_hash_table_new(g_str_hash, g_str_equal);
244
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) );
248
249     plugin_if_call_gui_cb(PLUGIN_IF_PREFERENCE_SAVE, dataSet);
250 }
251
252 extern void plugin_if_register_gui_cb(plugin_if_callback_t actionType, plugin_if_gui_cb callback)
253 {
254     gint * key = 0;
255
256     key = (gint *)g_malloc0(sizeof(gint));
257     *key = actionType;
258
259     plugin_if_init_hashtable();
260
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);
263 }
264
265 /*
266  * Editor modelines
267  *
268  * Local Variables:
269  * c-basic-offset: 4
270  * tab-width: 8
271  * indent-tabs-mode: nil
272  * End:
273  *
274  * ex: set shiftwidth=4 tabstop=8 expandtab:
275  * :indentSize=4:tabSize=8:noTabs=true:
276  */