Use g_get_charset instead of g_get_codeset.
[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
42 extern GList * ext_menubar_get_entries(void)
43 {
44     return menubar_entries;
45 }
46
47 extern ext_menu_t * ext_menubar_register_menu(int proto_id, const gchar * menulabel,
48         gboolean is_plugin)
49 {
50     ext_menubar_t * entry = NULL;
51     gchar * name = NULL;
52
53     /* A name for the entry must be provided */
54     g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
55
56     /* A protocol must exist for the given id */
57     g_assert(find_protocol_by_id(proto_id) != NULL);
58
59     /* Create unique name, which is used by GTK to provide the menu */
60     name = g_strconcat(proto_get_protocol_filter_name(proto_id), "Menu", NULL);
61
62     /* For now, a protocol may only register one main menu */
63     g_assert(g_list_find(menubar_menunames, name) == NULL);
64
65     entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
66     entry->type = EXT_MENUBAR_MENU;
67     entry->proto = proto_id;
68     entry->is_plugin = is_plugin;
69
70     entry->parent_menu = 0;
71
72     /* Create a name for this submenu */
73     entry->name = name;
74     entry->label = g_strdup(menulabel);
75     entry->tooltip = g_strdup(menulabel);
76
77     entry->submenu_cnt = 0;
78     entry->item_cnt = 0;
79
80     menubar_entries = g_list_append(menubar_entries, entry);
81     menubar_menunames = g_list_append(menubar_menunames, name);
82
83     return entry;
84 }
85
86 extern ext_menu_t * ext_menubar_set_parentmenu(ext_menu_t * menu, const gchar * parentmenu)
87 {
88     g_assert(menu != NULL && menu->parent == NULL);
89
90     g_assert(parentmenu != 0);
91
92     menu->parent_menu = g_strdup(parentmenu);
93
94     return menu;
95 }
96
97 extern ext_menu_t * ext_menubar_add_submenu(ext_menu_t * parent, const gchar *menulabel)
98 {
99     ext_menubar_t * entry = NULL;
100
101     /* A name for the entry must be provided */
102     g_assert(menulabel != NULL && strlen ( menulabel ) > 0 );
103
104     /* Parent must be a valid parent */
105     g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
106
107     parent->submenu_cnt++;
108
109     /* Create submenu entry */
110     entry = (ext_menubar_t *)g_malloc0(sizeof(ext_menubar_t));
111     entry->type = EXT_MENUBAR_MENU;
112     entry->parent = parent;
113     /* Just a convenience */
114     entry->proto = parent->proto;
115     entry->is_plugin = parent->is_plugin;
116     /* Create unique name, which is used by GTK to provide the menu */
117     entry->name = g_strdup_printf("%sS%02d", parent->name, parent->submenu_cnt);
118     entry->label = g_strdup(menulabel);
119     entry->tooltip = g_strdup(menulabel);
120
121     parent->children = g_list_append(parent->children, entry);
122
123     return entry;
124 }
125
126 static void ext_menubar_add_generic_entry (
127         ext_menubar_entry_t type, ext_menu_t * parent, const gchar * label,
128         const gchar * tooltip, ext_menubar_action_cb callback, gpointer user_data )
129 {
130     ext_menubar_t * entry = NULL;
131
132     /* A valid parent must exist */
133     g_assert(parent != NULL && parent->type == EXT_MENUBAR_MENU);
134     /* A label for the entry must be provided */
135     g_assert(label != NULL && strlen ( label ) > 0 );
136
137     parent->item_cnt++;
138
139     /* Create menu entry */
140     entry = (ext_menubar_t*)g_malloc0(sizeof(ext_menubar_t));
141     entry->type = type;
142     /* Create unique name, which is used by GTK to provide the menu */
143     entry->name = g_strdup_printf("%sI%02d", parent->name, parent->item_cnt);
144     entry->label = g_strdup(label);
145
146     if ( tooltip != NULL && strlen(tooltip) > 0 )
147         entry->tooltip = g_strdup(tooltip);
148
149     entry->callback = callback;
150     entry->user_data = user_data;
151
152     parent->children = g_list_append(parent->children, entry);
153 }
154
155 extern void ext_menubar_add_entry(ext_menu_t * parent, const gchar *label,
156         const gchar *tooltip, ext_menubar_action_cb callback, gpointer user_data)
157 {
158     /* A callback must be provided */
159     g_assert(callback != NULL);
160
161     ext_menubar_add_generic_entry ( EXT_MENUBAR_ITEM, parent, label, tooltip, callback, user_data );
162 }
163
164 extern void ext_menubar_add_website(ext_menu_t * parent, const gchar *label,
165         const gchar *tooltip, const gchar *url)
166 {
167     /* An url for the entry must be provided */
168     g_assert(url != NULL && strlen ( url ) > 0 );
169
170     ext_menubar_add_generic_entry ( EXT_MENUBAR_URL, parent, label, tooltip, NULL, (gpointer) g_strdup(url) );
171 }
172
173 extern void ext_menubar_add_separator(ext_menu_t *parent)
174 {
175     ext_menubar_add_generic_entry ( EXT_MENUBAR_SEPARATOR, parent, g_strdup("-"), NULL, NULL, NULL );
176 }
177
178 /* Implementation of GUI callback methods follows.
179  * This is a necessity, as using modern UI systems, gui interfaces often operate
180  * in different threads then the calling application. Even more so, if the calling
181  * application is implemented using a separate plugin. Therefore the external menubars
182  * cannot call gui functionality directly, the gui has to perform the function within
183  * it' own scope. */
184
185 static GHashTable * plugin_if_callback_functions;
186
187 static void
188 plugin_if_init_hashtable(void)
189 {
190     if ( plugin_if_callback_functions == 0 )
191         plugin_if_callback_functions = g_hash_table_new(g_int_hash, g_int_equal);
192 }
193
194 static void plugin_if_call_gui_cb(plugin_if_callback_t actionType, GHashTable * dataSet)
195 {
196     plugin_if_gui_cb action;
197     gint * key = 0;
198
199     key = (gint *)g_malloc0(sizeof(gint));
200     *key = (gint) actionType;
201
202     plugin_if_init_hashtable();
203
204     if ( g_hash_table_size(plugin_if_callback_functions) != 0 )
205     {
206         if ( g_hash_table_lookup_extended(plugin_if_callback_functions, key, NULL, (gpointer*)&action) )
207         {
208             if ( action != NULL )
209                 action(dataSet);
210         }
211     }
212 }
213
214 extern void plugin_if_apply_filter(const char * filter_string, gboolean force)
215 {
216     plugin_if_callback_t actionType;
217     GHashTable * dataSet = NULL;
218
219     actionType = ( force == TRUE ) ? PLUGIN_IF_FILTER_ACTION_APPLY : PLUGIN_IF_FILTER_ACTION_PREPARE;
220     dataSet = g_hash_table_new(g_str_hash, g_str_equal);
221
222     g_hash_table_insert( dataSet, g_strdup("action_type"), (gpointer) &actionType );
223     g_hash_table_insert( dataSet, g_strdup("filter_string"), g_strdup(filter_string) );
224     g_hash_table_insert( dataSet, g_strdup("force"), (gpointer) &force );
225
226     plugin_if_call_gui_cb(actionType, dataSet);
227 }
228
229 extern void plugin_if_goto_frame(guint32 framenr)
230 {
231     GHashTable * dataSet = NULL;
232
233     dataSet = g_hash_table_new(g_str_hash, g_str_equal);
234
235     g_hash_table_insert( dataSet, g_strdup("frame_nr"), GUINT_TO_POINTER(framenr) );
236
237     plugin_if_call_gui_cb(PLUGIN_IF_GOTO_FRAME, dataSet);
238 }
239
240 extern void plugin_if_save_preference(const char * pref_module, const char * pref_key, const char * pref_value)
241 {
242     GHashTable * dataSet = NULL;
243
244     dataSet = g_hash_table_new(g_str_hash, g_str_equal);
245
246     g_hash_table_insert( dataSet, g_strdup("pref_module"), g_strdup(pref_module) );
247     g_hash_table_insert( dataSet, g_strdup("pref_key"), g_strdup(pref_key) );
248     g_hash_table_insert( dataSet, g_strdup("pref_value"), g_strdup(pref_value) );
249
250     plugin_if_call_gui_cb(PLUGIN_IF_PREFERENCE_SAVE, dataSet);
251 }
252
253 extern void plugin_if_get_ws_info(ws_info_t **ws_info_ptr)
254 {
255     static ws_info_t ws_info = { FALSE, FILE_CLOSED, NULL, 0, 0, FALSE };
256 #ifdef HAVE_LIBPCAP
257
258     GHashTable * dataSet;
259     gchar * pluginKey = g_strdup("ws_info");
260
261     dataSet = g_hash_table_new(g_str_hash, g_str_equal);
262
263     g_hash_table_insert(dataSet, pluginKey, &ws_info);
264
265     plugin_if_call_gui_cb(PLUGIN_IF_GET_WS_INFO, dataSet);
266
267     g_hash_table_destroy(dataSet);
268     g_free(pluginKey);
269
270 #else
271
272     /* Initialise the ws_info structure */
273
274     ws_info.ws_info_supported = FALSE;
275     ws_info.cf_count = 0;
276     ws_info.cf_filename = NULL;
277     ws_info.cf_framenr = 0;
278     ws_info.frame_passed_dfilter = FALSE;
279     ws_info.cf_state = FILE_CLOSED;
280
281 #endif /* HAVE_LIBPCAP */
282
283     *ws_info_ptr = &ws_info;
284 }
285
286 extern void plugin_if_register_gui_cb(plugin_if_callback_t actionType, plugin_if_gui_cb callback)
287 {
288     gint * key = 0;
289
290     key = (gint *)g_malloc0(sizeof(gint));
291     *key = actionType;
292
293     plugin_if_init_hashtable();
294
295     if ( ! g_hash_table_lookup_extended(plugin_if_callback_functions, key, NULL, NULL ) )
296         g_hash_table_insert(plugin_if_callback_functions, key, (gpointer)callback);
297 }
298
299 /*
300  * Editor modelines
301  *
302  * Local Variables:
303  * c-basic-offset: 4
304  * tab-width: 8
305  * indent-tabs-mode: nil
306  * End:
307  *
308  * ex: set shiftwidth=4 tabstop=8 expandtab:
309  * :indentSize=4:tabSize=8:noTabs=true:
310  */