Make the changes compile on Linux also.
[obnox/wireshark/wip.git] / gtk / main_filter_toolbar.c
1 /* main_filter_toolbar.c
2  * The filter toolbar
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 /*
26  * This file implements the "filter" toolbar for Wireshark.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include <gtk/gtk.h>
37
38 #include "filter_dlg.h"
39 #include "filter_autocomplete.h"
40
41 #include "epan/prefs.h"
42
43 #include "keys.h"
44 #include "gtkglobals.h"
45 #include "stock_icons.h"
46 #include "recent.h"
47
48 #include "main.h"
49 #include "menus.h"
50 #include "main_toolbar.h"
51 #include "main_filter_toolbar.h"
52
53 GtkWidget   *main_display_filter_widget=NULL;
54
55 /* Run the current display filter on the current packet set, and
56    redisplay. */
57 static void
58 filter_activate_cb(GtkWidget *w _U_, gpointer data)
59 {
60   const char *s;
61 #ifdef NEW_FILTER_COMBO_BOX
62 #if GTK_CHECK_VERSION(2,6,0)
63 #else
64         GtkTreeIter   iter;
65         GtkTreeModel *model;
66 #endif
67   /* Since 2.6 */
68   #if GTK_CHECK_VERSION(2,6,0)
69   s = gtk_combo_box_get_active_text(GTK_COMBO_BOX(filter_cm))
70   #else
71   gtk_combo_box_get_active_iter(GTK_COMBO_BOX(filter_cm), &iter);
72   model = gtk_combo_box_get_model(GTK_COMBO_BOX(filter_cm));
73   gtk_tree_model_get(model, &iter, 0, &s, -1);
74   #endif
75
76 #else
77   s = gtk_entry_get_text(GTK_ENTRY(data));
78 #endif
79   main_filter_packets(&cfile, s, FALSE);
80 }
81
82 /* redisplay with no display filter */
83 static void
84 filter_reset_cb(GtkWidget *w, gpointer data _U_)
85 {
86   GtkWidget *filter_te = NULL;
87
88   if ((filter_te = g_object_get_data(G_OBJECT(w), E_DFILTER_TE_KEY))) {
89     gtk_entry_set_text(GTK_ENTRY(filter_te), "");
90   }
91   main_filter_packets(&cfile, NULL, FALSE);
92 }
93
94 /* #define NEW_FILTER_COMBO_BOX 1 */
95
96 GtkWidget *filter_toolbar_new()
97 {
98     GtkWidget     *filter_cm;
99         GtkWidget     *filter_te;
100     GtkWidget     *filter_tb;
101 #ifdef NEW_FILTER_COMBO_BOX
102 #else
103     GList         *dfilter_list = NULL;
104 #endif
105     GtkTooltips   *tooltips;
106     GtkToolItem   *filter_bt, *filter_add_expr_bt, *filter_reset;
107     GtkToolItem   *filter_apply, *item;
108
109
110     /* Display filter construct dialog has an Apply button, and "OK" not
111        only sets our text widget, it activates it (i.e., it causes us to
112        filter the capture). */
113     static construct_args_t args = {
114         "Wireshark: Display Filter",
115         TRUE,
116         TRUE,
117         FALSE
118     };
119
120     tooltips = gtk_tooltips_new();
121
122     /* filter toolbar */
123     filter_tb = gtk_toolbar_new();
124     gtk_toolbar_set_orientation(GTK_TOOLBAR(filter_tb),
125                                 GTK_ORIENTATION_HORIZONTAL);
126
127     g_object_set_data(G_OBJECT(top_level), E_TB_FILTER_KEY, filter_tb);
128     gtk_widget_show(filter_tb);
129
130     /* Create the "Filter:" button */
131     filter_bt = gtk_tool_button_new_from_stock (WIRESHARK_STOCK_DISPLAY_FILTER_ENTRY);
132     g_signal_connect(filter_bt, "clicked", G_CALLBACK(display_filter_construct_cb), &args);
133     gtk_widget_show(GTK_WIDGET (filter_bt));
134     g_object_set_data(G_OBJECT(top_level), E_FILT_BT_PTR_KEY, filter_bt);
135
136     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
137                        filter_bt,
138                        -1);
139     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_bt),
140                          "Open the \"Display Filter\" dialog, to edit/apply filters",
141                          "Private");
142
143     /* Create the filter combobox */
144 #ifdef NEW_FILTER_COMBO_BOX
145         filter_cm = gtk_combo_box_entry_new_text ();
146         filter_te = gtk_bin_get_child(GTK_BIN(filter_cm));
147 #else
148     filter_cm = gtk_combo_new();
149     dfilter_list = NULL;
150     gtk_combo_disable_activate(GTK_COMBO(filter_cm));
151     gtk_combo_set_case_sensitive(GTK_COMBO(filter_cm), TRUE);
152     g_object_set_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY, dfilter_list);
153     filter_te = GTK_COMBO(filter_cm)->entry;
154 #endif
155     main_display_filter_widget=filter_te;
156     g_object_set_data(G_OBJECT(filter_bt), E_FILT_TE_PTR_KEY, filter_te);
157     g_object_set_data(G_OBJECT(filter_te), E_DFILTER_CM_KEY, filter_cm);
158     g_object_set_data(G_OBJECT(top_level), E_DFILTER_CM_KEY, filter_cm);
159     g_signal_connect(filter_te, "activate", G_CALLBACK(filter_activate_cb), filter_te);
160     g_signal_connect(filter_te, "changed", G_CALLBACK(filter_te_syntax_check_cb), NULL);
161     g_object_set_data(G_OBJECT(filter_tb), E_FILT_AUTOCOMP_PTR_KEY, NULL);
162     g_object_set_data(G_OBJECT(filter_te), E_FILT_FIELD_USE_STATUSBAR_KEY, "");
163     g_signal_connect(filter_te, "key-press-event", G_CALLBACK (filter_string_te_key_pressed_cb), NULL);
164     g_signal_connect(filter_tb, "key-press-event", G_CALLBACK (filter_parent_dlg_key_pressed_cb), NULL);
165
166         gtk_widget_set_size_request(filter_cm, 400, -1);
167     gtk_widget_show(filter_cm);
168     item = gtk_tool_item_new ();
169     gtk_container_add (GTK_CONTAINER (item), filter_cm);
170     gtk_widget_show (GTK_WIDGET (item));
171
172     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
173                        item,
174                        -1);
175
176     /* setting a tooltip for a combobox will do nothing, so add it to the corresponding text entry */
177 #ifdef NEW_FILTER_COMBO_BOX
178     gtk_tooltips_set_tip(tooltips, filter_cm,
179 #else
180     gtk_tooltips_set_tip(tooltips, filter_te,
181 #endif
182         "Enter a display filter, or choose one of your recently used filters. "
183         "The background color of this field is changed by a continuous syntax check "
184         "(green is valid, red is invalid, yellow may have unexpected results).",
185         NULL);
186
187     /* Create the "Add Expression..." button, to pop up a dialog
188        for constructing filter comparison expressions. */
189     filter_add_expr_bt = gtk_tool_button_new_from_stock(WIRESHARK_STOCK_ADD_EXPRESSION);
190 #ifdef NEW_FILTER_COMBO_BOX
191 #else
192     g_object_set_data(G_OBJECT(filter_tb), E_FILT_FILTER_TE_KEY, filter_te);
193     g_signal_connect(filter_add_expr_bt, "clicked", G_CALLBACK(filter_add_expr_bt_cb), filter_tb);
194 #endif
195     gtk_widget_show(GTK_WIDGET(filter_add_expr_bt));
196
197     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
198                        filter_add_expr_bt,
199                        -1);
200
201     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_add_expr_bt),
202                          "Add an expression to this filter string", 
203                          "Private");
204
205     /* Create the "Clear" button */
206     filter_reset = gtk_tool_button_new_from_stock(WIRESHARK_STOCK_CLEAR_EXPRESSION);
207     g_object_set_data(G_OBJECT(filter_reset), E_DFILTER_TE_KEY, filter_te);
208     g_signal_connect(filter_reset, "clicked", G_CALLBACK(filter_reset_cb), NULL);
209     gtk_widget_show(GTK_WIDGET(filter_reset));
210     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
211                        filter_reset,
212                        -1);
213
214     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_reset),
215                          "Clear this filter string and update the display", 
216                          "Private");
217
218     /* Create the "Apply" button */
219     filter_apply = gtk_tool_button_new_from_stock(WIRESHARK_STOCK_APPLY_EXPRESSION);
220     g_object_set_data(G_OBJECT(filter_apply), E_DFILTER_CM_KEY, filter_cm);
221     g_signal_connect(filter_apply, "clicked", G_CALLBACK(filter_activate_cb), filter_te);
222     gtk_widget_show(GTK_WIDGET(filter_apply));
223
224     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
225                        filter_apply,
226                        -1);
227
228     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_apply),
229                          "Apply this filter string to the display",
230                          "Private");
231
232     /* Sets the text entry widget pointer as the E_DILTER_TE_KEY data
233      * of any widget that ends up calling a callback which needs
234      * that text entry pointer */
235     set_menu_object_data("/File/Open...", E_DFILTER_TE_KEY, filter_te);
236     set_menu_object_data("/Edit/Copy/As Filter", E_DFILTER_TE_KEY,
237                          filter_te);
238     set_menu_object_data("/Analyze/Display Filters...", E_FILT_TE_PTR_KEY,
239                          filter_te);
240     set_menu_object_data("/Analyze/Follow TCP Stream", E_DFILTER_TE_KEY,
241                          filter_te);
242     set_menu_object_data("/Analyze/Follow UDP Stream", E_DFILTER_TE_KEY,
243                          filter_te);
244     set_menu_object_data("/Analyze/Follow SSL Stream", E_DFILTER_TE_KEY,
245                          filter_te);
246     set_menu_object_data("/Analyze/Apply as Filter/Selected", E_DFILTER_TE_KEY,
247                          filter_te);
248     set_menu_object_data("/Analyze/Apply as Filter/Not Selected", E_DFILTER_TE_KEY,
249                          filter_te);
250     set_menu_object_data("/Analyze/Apply as Filter/... and Selected", E_DFILTER_TE_KEY,
251                          filter_te);
252     set_menu_object_data("/Analyze/Apply as Filter/... or Selected", E_DFILTER_TE_KEY,
253                          filter_te);
254     set_menu_object_data("/Analyze/Apply as Filter/... and not Selected", E_DFILTER_TE_KEY,
255                          filter_te);
256     set_menu_object_data("/Analyze/Apply as Filter/... or not Selected", E_DFILTER_TE_KEY,
257                          filter_te);
258     set_menu_object_data("/Analyze/Prepare a Filter/Selected", E_DFILTER_TE_KEY,
259                          filter_te);
260     set_menu_object_data("/Analyze/Prepare a Filter/Not Selected", E_DFILTER_TE_KEY,
261                          filter_te);
262     set_menu_object_data("/Analyze/Prepare a Filter/... and Selected", E_DFILTER_TE_KEY,
263                          filter_te);
264     set_menu_object_data("/Analyze/Prepare a Filter/... or Selected", E_DFILTER_TE_KEY,
265                          filter_te);
266     set_menu_object_data("/Analyze/Prepare a Filter/... and not Selected", E_DFILTER_TE_KEY,
267                          filter_te);
268     set_menu_object_data("/Analyze/Prepare a Filter/... or not Selected", E_DFILTER_TE_KEY,
269                          filter_te);
270     set_menu_object_data("/Conversation Filter/Ethernet", E_DFILTER_TE_KEY,
271                          filter_te);
272     set_menu_object_data("/Conversation Filter/IP", E_DFILTER_TE_KEY,
273                          filter_te);
274     set_menu_object_data("/Conversation Filter/TCP", E_DFILTER_TE_KEY,
275                          filter_te);
276     set_menu_object_data("/Conversation Filter/UDP", E_DFILTER_TE_KEY,
277                          filter_te);
278     set_menu_object_data("/Conversation Filter/PN-CBA Server", E_DFILTER_TE_KEY,
279                          filter_te);
280     set_toolbar_object_data(E_DFILTER_TE_KEY, filter_te);
281     g_object_set_data(G_OBJECT(popup_menu_object), E_DFILTER_TE_KEY, filter_te);
282
283     /* make current preferences effective */
284     toolbar_redraw_all();
285
286     return filter_tb;
287 }
288
289 static gint
290 dfilter_entry_match(gconstpointer a, gconstpointer b)
291 {
292     const char *s1 = a;
293     const char *s2 = b;
294
295     return strcmp(s1, s2);
296 }
297
298 #ifdef NEW_FILTER_COMBO_BOX
299 static gboolean
300 dfilter_entry_match_new(GtkWidget *filter_cm, char *s)
301 {
302         GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX(filter_cm));
303         GtkTreeIter   iter;
304         GValue value;
305         const char *filter_str;
306
307         if (!gtk_tree_model_get_iter_first (model, &iter))
308                 return FALSE;
309         do{
310                 gtk_tree_model_get_value (model, &iter, 0, &value);
311                 filter_str = g_value_get_string (&value);
312                 if(filter_str){
313                         if(strcmp(s, filter_str) == 0){
314                                 g_value_unset (&value);
315                                 return TRUE;
316                         }
317                 }
318         }while (gtk_tree_model_iter_next (model, &iter));
319
320         return FALSE;
321 }
322 #endif
323
324 /* add a display filter to the combo box */
325 /* Note: a new filter string will not replace an old identical one */
326 static gboolean
327 dfilter_combo_add(GtkWidget *filter_cm, char *s) {
328 #ifdef NEW_FILTER_COMBO_BOX
329         if(!dfilter_entry_match_new(filter_cm,s))
330                 gtk_combo_box_append_text(GTK_COMBO_BOX(filter_cm), s);
331 #else
332     GList     *dfilter_list = g_object_get_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY);
333
334     /* GtkCombos don't let us get at their list contents easily, so we maintain
335        our own filter list, and feed it to gtk_combo_set_popdown_strings when
336        a new filter is added. */
337     if (s && strlen(s) > 0 &&
338         g_list_length(dfilter_list) < prefs.gui_recent_df_entries_max &&
339         g_list_find_custom(dfilter_list, s, dfilter_entry_match) == NULL) {
340
341         dfilter_list = g_list_append(dfilter_list, s);
342         s = NULL;
343         g_object_set_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY, dfilter_list);
344         gtk_combo_set_popdown_strings(GTK_COMBO(filter_cm), dfilter_list);
345         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(filter_cm)->entry), g_list_first(dfilter_list)->data);
346     }
347 #endif
348     g_free(s);
349
350     return TRUE;
351 }
352
353
354 /* write all non empty display filters (until maximum count)
355  * of the combo box GList to the user's recent file */
356 void
357 dfilter_recent_combo_write_all(FILE *rf) {
358     GtkWidget *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
359 #ifdef NEW_FILTER_COMBO_BOX
360         GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX(filter_cm));
361         GtkTreeIter   iter;
362         GValue value;
363         const char *filter_str;
364         guint      max_count = 0;
365
366         if (!gtk_tree_model_get_iter_first (model, &iter))
367                 return;
368         do{
369                 gtk_tree_model_get_value (model, &iter, 0, &value);
370                 filter_str = g_value_get_string (&value);
371                 if(filter_str)
372                         fprintf (rf, RECENT_KEY_DISPLAY_FILTER ": %s\n", filter_str);
373                 g_value_unset (&value);
374
375         }while (gtk_tree_model_iter_next (model, &iter)&& (max_count++ < prefs.gui_recent_df_entries_max));
376
377 #else
378     GList     *dfilter_list = g_object_get_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY);
379     GList     *li;
380     guint      max_count = 0;
381
382     /* write all non empty display filter strings to the recent file (until max count) */
383     li = g_list_first(dfilter_list);
384     while ( li && (max_count++ < prefs.gui_recent_df_entries_max) ) {
385         if (strlen(li->data)) {
386             fprintf (rf, RECENT_KEY_DISPLAY_FILTER ": %s\n", (char *)li->data);
387         }
388         li = li->next;
389     }
390 #endif
391 }
392
393 /* empty the combobox entry field */
394 void
395 dfilter_combo_add_empty(void) {
396     GtkWidget *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
397
398     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(filter_cm)->entry), "");
399 }
400
401
402 /* add a display filter coming from the user's recent file to the dfilter combo box */
403 gboolean
404 dfilter_combo_add_recent(gchar *s) {
405     GtkWidget *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
406     char      *dup;
407
408     dup = g_strdup(s);
409
410     return dfilter_combo_add(filter_cm, dup);
411 }
412
413 /* call cf_filter_packets() and add this filter string to the recent filter list */
414 gboolean
415 main_filter_packets(capture_file *cf, const gchar *dftext, gboolean force)
416 {
417     GtkCombo  *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
418     GList     *dfilter_list = g_object_get_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY);
419     gboolean   free_filter = TRUE;
420     char      *s;
421     cf_status_t cf_status;
422
423     s = g_strdup(dftext);
424
425     cf_status = cf_filter_packets(cf, s, force);
426     if (!s)
427         return (cf_status == CF_OK);
428
429     /* GtkCombos don't let us get at their list contents easily, so we maintain
430        our own filter list, and feed it to gtk_combo_set_popdown_strings when
431        a new filter is added. */
432     if (cf_status == CF_OK && strlen(s) > 0) {
433         GList *li;
434
435         while ((li = g_list_find_custom(dfilter_list, s, dfilter_entry_match)) != NULL)
436             /* Delete old/duplicate entry now. We'll re-add it later */
437             dfilter_list = g_list_delete_link(dfilter_list, li);
438
439         /* trim list size first */
440         while (g_list_length(dfilter_list) >= prefs.gui_recent_df_entries_max)
441             dfilter_list = g_list_delete_link(dfilter_list, g_list_last(dfilter_list));
442
443         free_filter = FALSE;
444         /* Push the filter to the front of the list */
445         dfilter_list = g_list_prepend(dfilter_list, s);
446         g_object_set_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY, dfilter_list);
447         gtk_combo_set_popdown_strings(filter_cm, dfilter_list);
448         gtk_entry_set_text(GTK_ENTRY(filter_cm->entry), g_list_first(dfilter_list)->data);
449     }
450
451     if (free_filter)
452         g_free(s);
453
454     return (cf_status == CF_OK);
455 }
456
457